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

Class TestCase

axlearn/common/test_utils.py:163–328  ·  view source on GitHub ↗

Base test class.

Source from the content-addressed store, hash-verified

161
162
163class TestCase(parameterized.TestCase):
164 """Base test class."""
165
166 @property
167 def data_dir(self):
168 return "FAKE"
169
170 def setUp(self):
171 super().setUp()
172 push_data_dir(self.data_dir)
173 utils_spmd.setup(jax_backend=self._jax_backend())
174
175 def _jax_backend(self) -> str:
176 # Setup without distributed initialization by default.
177 return "cpu"
178
179 def tearDown(self) -> None:
180 super().tearDown()
181 self.assertEqual(pop_data_dir(), self.data_dir)
182
183 def _compute_layer_outputs(
184 self,
185 *,
186 test_layer: BaseLayer,
187 ref_layer: Any,
188 test_inputs: Any,
189 ref_inputs: Any,
190 parameters_from_ref_layer: ParameterConversionFn,
191 require_same_num_params: bool = True,
192 require_same_tree_structure: bool = True,
193 ) -> tuple[Any, Any]:
194 layer_params = test_layer.initialize_parameters_recursively(prng_key=jax.random.PRNGKey(0))
195 total_num_layer_params = 0
196 for name, param in flatten_items(layer_params):
197 logging.info("Test: %s=%s", name, param.shape)
198 total_num_layer_params += param.size
199 logging.info("Total test layer params: %s", total_num_layer_params)
200
201 total_num_ref_params = 0
202 for name, param in ref_layer.named_parameters(recurse=True):
203 logging.info("Ref: %s=%s", name, param.shape)
204 total_num_ref_params += param.numel()
205 for name, param in ref_layer.named_buffers(recurse=True):
206 logging.info("Ref buffer: %s=%s", name, param.shape)
207 logging.info("Total ref layer params: %s", total_num_ref_params)
208
209 if require_same_num_params:
210 self.assertEqual(total_num_layer_params, total_num_ref_params)
211
212 params_from_ref = parameters_from_ref_layer(ref_layer, dst_layer=test_layer)
213
214 # Test that leaves have same paths and shapes. Note that trees with different structures can
215 # still have the same leaves: {"a": 1, "b": {}} will match {"a": 1}, because there are no
216 # leaves under subtree "b".
217 self.assertSequenceEqual(
218 [f"{name}={value.shape}" for name, value in flatten_items(params_from_ref)],
219 [f"{name}={value.shape}" for name, value in flatten_items(layer_params)],
220 )

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected