Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions flax/nnx/filterlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ def __repr__(self):

@dataclasses.dataclass(frozen=True)
class PathContains:
key: Key
key: Key | str
exact: bool = True

def __call__(self, path: PathParts, x: tp.Any):
return self.key in path
if self.exact:
return self.key in path
return any(str(self.key) in str(part) for part in path)

def __repr__(self):
return f'PathContains({self.key!r})'
return f'PathContains({self.key!r}, exact={self.exact})'


class PathIn:
Expand Down
7 changes: 6 additions & 1 deletion tests/nnx/filters_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ class TestFilters(absltest.TestCase):
def test_path_contains(self):
class Model(nnx.Module):
def __init__(self, rngs):
self.backbone = nnx.Linear(2, 3, rngs=rngs)
self.backbone1 = nnx.Linear(2, 3, rngs=rngs)
self.backbone2 = nnx.Linear(3, 3, rngs=rngs)
self.head = nnx.Linear(3, 10, rngs=rngs)

model = Model(nnx.Rngs(0))

head_state = nnx.state(model, nnx.PathContains('head'))
backbones_state = nnx.state(model, nnx.PathContains('backbone', exact=False))

self.assertIn('head', head_state)
self.assertNotIn('backbone', head_state)
self.assertIn('backbone1', backbones_state)
self.assertIn('backbone2', backbones_state)
self.assertNotIn('head', backbones_state)

if __name__ == '__main__':
absltest.main()
Loading