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

Method test_tree_paths

axlearn/common/utils_test.py:203–284  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

201 self.assertEqual(cfg.layer[1].input_dim, updated_input_dim)
202
203
204class TreeUtilsTest(TestCase):
205 def test_tree_paths(self):
206 tree = {"a": 1, "b": [2, {"c": 3}]}
207 self.assertEqual({"a": "a", "b": ["b/0", {"c": "b/1/c"}]}, tree_paths(tree))
208
209 # Tuple.
210 self.assertEqual(("0", ("1/0", "1/1"), "2"), tree_paths(("a", ("b", "c"), "d")))
211
212 # NamedTuple.
213 self.assertEqual(
214 Combo(head="head", tail=Combo(head="tail/head", tail="tail/tail")),
215 tree_paths(Combo(head=1, tail=Combo(head=2, tail=3))),
216 )
217
218 # flax_struct.PyTreeNode.
219 self.assertEqual(
220 WeightedSummary(mean="mean", weight="weight"),
221 tree_paths(WeightedSummary(mean=2, weight=3)),
222 )
223
224 # Nested flax_struct.PyTreeNode.
225 self.assertEqual(
226 StructContainer(WeightedSummary(mean="contents/mean", weight="contents/weight")),
227 tree_paths(StructContainer(WeightedSummary(mean=2, weight=3))),
228 )
229
230 # str-Enum key.
231 class MyEnum(str, enum.Enum):
232 RED = "red"
233
234 self.assertEqual({MyEnum.RED: "MyEnum.RED"}, tree_paths({MyEnum.RED: 3}))
235
236 # With is_leaf set.
237 self.assertEqual(
238 ["0", {"a": "1/a", "b": "1/b"}],
239 tree_paths(
240 [Combo(head=1, tail=2), {"a": Combo(head=3, tail=4), "b": 5}],
241 is_leaf=lambda x: isinstance(x, Combo),
242 ),
243 )
244
245 class DataclassCombo(flax_struct.PyTreeNode):
246 scalar: int
247 dataclass_combo: Any
248 none: type[None]
249 nested_tensor: NestedTensor
250
251 # Nested custom pytree.
252 self.assertEqual(
253 DataclassCombo( # pytype: disable=wrong-arg-types
254 scalar="scalar",
255 dataclass_combo=DataclassCombo( # pytype: disable=wrong-arg-types
256 scalar="dataclass_combo/scalar",
257 dataclass_combo=Combo(
258 head="dataclass_combo/dataclass_combo/head",
259 tail="dataclass_combo/dataclass_combo/tail",
260 ),

Callers

nothing calls this directly

Calls 5

tree_pathsFunction · 0.90
WeightedSummaryClass · 0.90
ComboClass · 0.85
StructContainerClass · 0.85
DataclassComboClass · 0.85

Tested by

no test coverage detected