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

Method assertNestedAllClose

axlearn/common/test_utils.py:244–262  ·  view source on GitHub ↗
(self, a, b, atol=1e-6, rtol=1e-3)

Source from the content-addressed store, hash-verified

242 return test_outputs, ref_outputs
243
244 def assertNestedAllClose(self, a, b, atol=1e-6, rtol=1e-3):
245 a_items = sorted(flatten_items(a), key=lambda x: x[0])
246 b_items = sorted(flatten_items(b), key=lambda x: x[0])
247 self.assertEqual([name for name, _ in a_items], [name for name, _ in b_items])
248 for (a_name, a_value), (b_name, b_value) in zip(a_items, b_items):
249 self.assertEqual(a_name, b_name)
250 if isinstance(a_value, Tensor):
251 a_value = as_local_tensor(a_value)
252 if isinstance(b_value, Tensor):
253 b_value = as_local_tensor(b_value)
254 if isinstance(a_value, (np.ndarray, jnp.ndarray)) or isinstance(
255 b_value, (np.ndarray, jnp.ndarray)
256 ):
257 a_value, b_value = as_tensor(a_value), as_tensor(b_value)
258 self.assertEqual(a_value.dtype, b_value.dtype, msg=f"{a_name}")
259 self.assertEqual(a_value.shape, b_value.shape, msg=f"{a_name}")
260 assert_allclose(a_value, b_value, atol=atol, rtol=rtol, err_msg=f"{a_name}")
261 else:
262 self.assertAlmostEqual(a_value, b_value, msg=f"{a_name}")
263
264 def assertNestedEqual(self, a, b):
265 a_kv = flatten_items(a)

Calls 4

flatten_itemsFunction · 0.90
as_tensorFunction · 0.90
as_local_tensorFunction · 0.85
assert_allcloseFunction · 0.85

Tested by

no test coverage detected