@@ -114,9 +114,7 @@ def nnx_apply_fn(params, inputs):
114114 # 2. Vmap this new wrapper function
115115 vmapped_apply = jax .vmap (nnx_apply_fn , in_axes = (None , 0 ))
116116
117- def _test_train_step (
118- state : train_state .TrainState , batch , prng_key : diloco .PRNGKey
119- ):
117+ def _test_train_step (state : train_state .TrainState , batch , prng_key : diloco .PRNGKey ):
120118 """A simple MSE loss train step to enable numerics testing."""
121119 del prng_key
122120
@@ -148,13 +146,9 @@ def loss_fn(params, batch):
148146 lambda x : x .value if hasattr (x , "value" ) else x ,
149147 diloco_test_state .params ,
150148 )
151- chex .assert_trees_all_equal (
152- diloco_params_pure , params_pure .to_pure_dict ()
153- )
149+ chex .assert_trees_all_equal (diloco_params_pure , params_pure .to_pure_dict ())
154150 else :
155- chex .assert_trees_all_equal (
156- diloco_test_state .params , initial_test_state .params
157- )
151+ chex .assert_trees_all_equal (diloco_test_state .params , initial_test_state .params )
158152
159153 diloco_train_step = diloco .build_diloco_train_step (test_config , _test_train_step )
160154 inputs = jnp .array (
@@ -211,13 +205,9 @@ def loss_fn(params, batch):
211205 lambda x : x .value if hasattr (x , "value" ) else x ,
212206 diloco_test_state .params ,
213207 )
214- chex .assert_trees_all_equal (
215- diloco_params_pure , params_pure .to_pure_dict ()
216- )
208+ chex .assert_trees_all_equal (diloco_params_pure , params_pure .to_pure_dict ())
217209 else :
218- chex .assert_trees_all_equal (
219- diloco_test_state .params , initial_test_state .params
220- )
210+ chex .assert_trees_all_equal (diloco_test_state .params , initial_test_state .params )
221211
222212 # Run the second step (no synchronization).
223213 # Replica 0:
@@ -245,7 +235,7 @@ def loss_fn(params, batch):
245235 # = 0.81
246236 diloco_test_state , loss = diloco_train_step (diloco_test_state , (inputs , labels ), jax .random .key (seed = 42 ))
247237 chex .assert_equal (diloco_test_state .step , 2.0 )
248- chex .assert_trees_all_close (loss , 0.65 )
238+ chex .assert_trees_all_close (loss , 0.65 , rtol = 1e-2 , atol = 1e-2 )
249239 # Assert no updates to the global model yet (no synchronization)
250240 if test_config .pure_nnx :
251241 _ , params_pure , _ = nnx .split (initial_test_state .model , nnx .Param , ...)
@@ -256,13 +246,9 @@ def loss_fn(params, batch):
256246 lambda x : x .value if hasattr (x , "value" ) else x ,
257247 diloco_test_state .params ,
258248 )
259- chex .assert_trees_all_equal (
260- diloco_params_pure , params_pure .to_pure_dict ()
261- )
249+ chex .assert_trees_all_equal (diloco_params_pure , params_pure .to_pure_dict ())
262250 else :
263- chex .assert_trees_all_equal (
264- diloco_test_state .params , initial_test_state .params
265- )
251+ chex .assert_trees_all_equal (diloco_test_state .params , initial_test_state .params )
266252
267253 # Run the third step, which synchronizes afterwards.
268254 # Replica 0:
@@ -294,13 +280,11 @@ def loss_fn(params, batch):
294280 # based outer optimizer.
295281 diloco_test_state , loss = diloco_train_step (diloco_test_state , (inputs , labels ), jax .random .key (seed = 42 ))
296282 chex .assert_equal (diloco_test_state .step , 3.0 )
297- chex .assert_trees_all_close (loss , 0.4481 )
283+ chex .assert_trees_all_close (loss , 0.4481 , rtol = 1e-2 , atol = 1e-2 )
298284 # Assert that inner and outer parameters are all equal now that
299285 # synchronization has happened.
300286 if test_config .pure_nnx :
301- _ , inner_params , _ = nnx .split (
302- diloco_test_state .inner_state .model , nnx .Param , ...
303- )
287+ _ , inner_params , _ = nnx .split (diloco_test_state .inner_state .model , nnx .Param , ...)
304288 inner_params_pure = jax .tree_util .tree_map (
305289 lambda x : x .value if hasattr (x , "value" ) else x ,
306290 inner_params .to_pure_dict (),
@@ -320,15 +304,11 @@ def loss_fn(params, batch):
320304 else :
321305 chex .assert_trees_all_equal (
322306 diloco_test_state .params ,
323- jax .tree .map (
324- lambda arr : arr [0 , ...], diloco_test_state .inner_state .params
325- ),
307+ jax .tree .map (lambda arr : arr [0 , ...], diloco_test_state .inner_state .params ),
326308 )
327309 chex .assert_trees_all_equal (
328310 diloco_test_state .params ,
329- jax .tree .map (
330- lambda arr : arr [1 , ...], diloco_test_state .inner_state .params
331- ),
311+ jax .tree .map (lambda arr : arr [1 , ...], diloco_test_state .inner_state .params ),
332312 )
333313
334314 # Run the fourth step (no synchronization).
@@ -358,7 +338,7 @@ def loss_fn(params, batch):
358338 step_three_outer_params = diloco_test_state .params
359339 diloco_test_state , loss = diloco_train_step (diloco_test_state , (inputs , labels ), jax .random .key (seed = 42 ))
360340 chex .assert_equal (diloco_test_state .step , 4.0 )
361- chex .assert_trees_all_close (loss , 0.574244 )
341+ chex .assert_trees_all_close (loss , 0.574244 , rtol = 1e-2 , atol = 1e-2 )
362342 # Assert no updates to the global model since previous step (no
363343 # synchronization).
364344 chex .assert_trees_all_equal (diloco_test_state .params , step_three_outer_params )
0 commit comments