google / google/flax

Unexpected behavior for @nn.compact_name_scope

Open
#3,637 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
7.3k
Forks
833
Avg merge
5h 11m
Merged PRs (30d)
5

Description

```py
import jax
from flax import linen as nn
from jax import random
import jax.numpy as jnp

class Foo(nn.Module):
@nn.compact_name_scope
def up(self, x):
return self._embed(x) + nn.Dense(4)(x)

def _embed(self, x):
return nn.Dense(4)(x)

Foo().init(random.PRNGKey(0), jnp.zeros((3, 4)), method=Foo.up)
```

This throws error: `flax.errors.AssignSubModuleError: Submodule Dense must be defined in `setup()` or in a method wrapped in `@compact` (https://flax.readthedocs.io/en/latest/api_reference/flax.errors.html#flax.errors.AssignSubModuleError)`

However when I replace `@nn.compact_name_scope` with `@nn.compact` it does work.

So it seems methods wrapped in `@nn.compact_name_scope` can only call other methods that are wrapped in that as well, which is different behavior than `@nn.compact`.

Is this behavior intended? If so, it should probably be documented. Otherwise would it be possible to fix it?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.