MCPcopy Create free account
hub / github.com/apple/axlearn / test_save_and_restore

Method test_save_and_restore

axlearn/common/checkpointer_test.py:85–174  ·  view source on GitHub ↗
(self, checkpointer_cls: Type[BaseCheckpointer])

Source from the content-addressed store, hash-verified

83class CheckpointerTest(test_utils.TestCase):
84 @parameterized.parameters(Checkpointer, OrbaxCheckpointer)
85 def test_save_and_restore(self, checkpointer_cls: Type[BaseCheckpointer]):
86 mesh_shape = (1, 1)
87 if not test_utils.is_supported_mesh_shape(mesh_shape):
88 return
89 with _mesh(mesh_shape):
90 cfg = _checkpointer_config(checkpointer_cls)
91 cfg.save_policy.min_step = 0
92 ckpt: BaseCheckpointer = cfg.instantiate(parent=None)
93 state0 = dict(x=jnp.zeros([], dtype=jnp.int32), y=jnp.ones([2], dtype=jnp.float32))
94 state1 = dict(x=jnp.ones([], dtype=jnp.int32), y=jnp.ones([2], dtype=jnp.float32) + 1)
95
96 # Restoring from an empty dir returns the input state if step=None.
97 self.assertNestedEqual((None, state0), ckpt.restore(step=None, state=state0))
98 self.assertNestedEqual((None, state1), ckpt.restore(step=None, state=state1))
99 # With an explicit step, ValueError will be raised.
100 with self.assertRaises(ValueError):
101 ckpt.restore(step=0, state=state0)
102
103 ckpt.save(step=0, state=state0)
104 ckpt.wait_until_finished()
105 self.assertNestedEqual((0, state0), ckpt.restore(step=0, state=state1))
106 # step=None restores from the latest ckpt.
107 self.assertNestedEqual((0, state0), ckpt.restore(step=None, state=state1))
108
109 ckpt.save(step=1, state=state1)
110 ckpt.wait_until_finished()
111 self.assertNestedEqual((1, state1), ckpt.restore(step=1, state=state0))
112 # step=None restores from the latest ckpt.
113 self.assertNestedEqual((1, state1), ckpt.restore(step=None, state=state0))
114
115 # When the given state has a different dict key: 'z' instead of 'y'.
116 with self.assertRaisesRegex((ValueError, KeyError), "z"):
117 ckpt.restore(
118 step=None,
119 state=dict(
120 x=jnp.zeros([], dtype=jnp.int32), z=jnp.ones([2], dtype=jnp.float32)
121 ),
122 )
123
124 # When the given state has a different array shape: [3] instead of [2] for y.
125 with self.assertRaisesRegex(
126 ValueError, "(checkpoint tree dtypes or shapes|not compatible)"
127 ):
128 ckpt.restore(
129 step=None,
130 state=dict(
131 x=jnp.zeros([], dtype=jnp.int32), y=jnp.ones([3], dtype=jnp.float32)
132 ),
133 )
134 # TODO(matthew_e_hopkins): revert it once upgrade jax version.
135 if checkpointer_cls is Checkpointer:
136 # When the given state has a different dict shape: [1] instead of [] for x.
137 # Orbax throws AssertionError in this case.
138 with self.assertRaisesRegex(
139 (AssertionError, ValueError),
140 "(checkpoint tree dtypes or shapes|not compatible)",
141 ):
142 ckpt.restore(

Callers

nothing calls this directly

Calls 8

assertNestedEqualMethod · 0.80
_meshFunction · 0.70
_checkpointer_configFunction · 0.70
instantiateMethod · 0.45
restoreMethod · 0.45
saveMethod · 0.45
wait_until_finishedMethod · 0.45
stopMethod · 0.45

Tested by

no test coverage detected