Skip to content

Commit

Permalink
Add test of NNX with orbax.checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jakevdp committed Aug 21, 2024
1 parent fc4a574 commit 66d1dd3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
4 changes: 4 additions & 0 deletions jax_ml_stack/tests/test_nnx_with_optax.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ def loss(model, x=x, y=y):
final_loss = loss(model)

self.assertNotAlmostEqual(initial_loss, final_loss)


if __name__ == '__main__':
unittest.main()
75 changes: 75 additions & 0 deletions jax_ml_stack/tests/test_nnx_with_orbax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright 2024 The JAX 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
#
# https://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.


import contextlib
import tempfile
import unittest

from flax import nnx
import jax
import numpy as np
import orbax.checkpoint


class SimpleModel(nnx.Module):

def __init__(self, rngs):
self.layer1 = nnx.Linear(2, 5, rngs=rngs)
self.layer2 = nnx.Linear(5, 3, rngs=rngs)

def __call__(self, x):
for layer in [self.layer1, self.layer2]:
x = layer(x)
return x


class NNXOrbaxTest(unittest.TestCase):

def setUp(self):
self.tmp_dir = tempfile.TemporaryDirectory()

if hasattr(self, 'enterContext'): # Python 3.11 or newer
self.enterContext(self.tmp_dir)
else:
with contextlib.ExitStack() as stack:
stack.enter_context(self.tmp_dir)
self.addCleanup(stack.pop_all().close)

def test_nnx_orbax_checkpoint(self):
model = SimpleModel(nnx.Rngs(0))

# Create the checkpoint
state = nnx.state(model)
checkpointer = orbax.checkpoint.PyTreeCheckpointer()
checkpointer.save(f'{self.tmp_dir.name}/state', item=state)
restore_args = orbax.checkpoint.checkpoint_utils.construct_restore_args(
state
)

# update the model with the loaded state
restored_model = nnx.eval_shape(SimpleModel, nnx.Rngs(1))
restored_state = checkpointer.restore(
f'{self.tmp_dir.name}/state',
item=nnx.state(restored_model),
restore_args=restore_args,
)
nnx.update(restored_model, restored_state)

self.assertEqual(type(model), type(restored_model))
jax.tree.map(np.testing.assert_array_equal, state, restored_state)


if __name__ == '__main__':
unittest.main()

0 comments on commit 66d1dd3

Please sign in to comment.