-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4011 from google:nnx-path-filter
PiperOrigin-RevId: 645354554
- Loading branch information
Showing
6 changed files
with
260 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
filterlib | ||
------------------------ | ||
|
||
.. automodule:: flax.nnx | ||
.. currentmodule:: flax.nnx | ||
|
||
.. autofunction:: flax.nnx.filterlib.to_predicate | ||
|
||
.. autoclass:: WithTag | ||
.. autoclass:: PathContains | ||
.. autoclass:: OfType | ||
.. autoclass:: Any | ||
.. autoclass:: All | ||
.. autoclass:: Not | ||
.. autoclass:: Everything | ||
.. autoclass:: Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Using Filters\n", | ||
"\n", | ||
"Filters are used extensively in NNX as a way to create `State` groups in APIs\n", | ||
"such as `nnx.split` and `nnx.state`. For example:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"params = State({\n", | ||
" 'a': VariableState(\n", | ||
" type=Param,\n", | ||
" value=0\n", | ||
" )\n", | ||
"})\n", | ||
"batch_stats = State({\n", | ||
" 'b': VariableState(\n", | ||
" type=BatchStat,\n", | ||
" value=True\n", | ||
" )\n", | ||
"})\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from flax import nnx\n", | ||
"\n", | ||
"class Foo(nnx.Module):\n", | ||
" def __init__(self):\n", | ||
" self.a = nnx.Param(0)\n", | ||
" self.b = nnx.BatchStat(True)\n", | ||
"\n", | ||
"foo = Foo()\n", | ||
"\n", | ||
"graphdef, params, batch_stats = nnx.split(foo, nnx.Param, nnx.BatchStat)\n", | ||
"\n", | ||
"print(f'{params = }')\n", | ||
"print(f'{batch_stats = }')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Here `nnx.Param` and `nnx.BatchStat` are used as Filters to split the model into two groups: one with the parameters and the other with the batch statistics. However this begs the questions:\n", | ||
"\n", | ||
"* What is a Filter?\n", | ||
"* Why are types, such as `Param` or `BatchStat`, Filters?\n", | ||
"* How is `State` grouped / filtered?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n", | ||
"## The Filter Protocol\n", | ||
"\n", | ||
"In general Filter are predicate functions of the form:\n", | ||
"\n", | ||
"```python\n", | ||
"\n", | ||
"def f(path: tuple[Key, ...], value: Any) -> bool:\n", | ||
"\n", | ||
"```\n", | ||
"where `Key` is a hashable and comparable type, `path` is a tuple of `Key`s representing the path to the value in a nested structure, and `value` is the value at the path. The function returns `True` if the value should be included in the group and `False` otherwise.\n", | ||
"\n", | ||
"Types are obviously not functions. The reason why types are used as Filters is because internally they are converted to functions. For example, `Param` is roughly converted to a function like this:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def is_param(path, value) -> bool:\n", | ||
" return isinstance(value, nnx.Param) or (\n", | ||
" hasattr(value, 'type') and issubclass(value.type, nnx.Param)\n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Such a function matches any value that is an instance of `Param` or any value that hash a `type` attribute that is an instance of a subclass of `Param`. Internally NNX uses `OfType` which creates a callable of the form for a given type." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"is_param((), nnx.Param(0)) = True\n", | ||
"is_param((), nnx.VariableState(type=nnx.Param, value=0)) = True\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"is_param = nnx.OfType(nnx.Param)\n", | ||
"\n", | ||
"print(f'{is_param((), nnx.Param(0)) = }')\n", | ||
"print(f'{is_param((), nnx.VariableState(type=nnx.Param, value=0)) = }')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2024 The Flax Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from absl.testing import absltest | ||
|
||
from flax import nnx | ||
|
||
|
||
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.head = nnx.Linear(3, 10, rngs=rngs) | ||
|
||
model = Model(nnx.Rngs(0)) | ||
|
||
head_state = nnx.state(model, nnx.PathContains('head')) | ||
|
||
self.assertIn('head', head_state) | ||
self.assertNotIn('backbone', head_state) |