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

Function tree_paths

axlearn/common/utils.py:396–418  ·  view source on GitHub ↗

Returns a tree of the same structure as `nested_tensor` but with corresponding paths instead of values. E.g., tree_paths({'a': 1, 'b': [2, {'c': 3}]}) = {'a': 'a', 'b': ['b/0', {'c': 'b/1/c'}]} Args: tree: A nested structure. separator: The separator between par

(
    tree: NestedTree, separator: str = "/", is_leaf: Optional[Callable[[Any], bool]] = None
)

Source from the content-addressed store, hash-verified

394
395
396def tree_paths(
397 tree: NestedTree, separator: str = "/", is_leaf: Optional[Callable[[Any], bool]] = None
398) -> NestedTree:
399 """Returns a tree of the same structure as `nested_tensor` but with corresponding paths instead
400 of values.
401
402 E.g.,
403 tree_paths({'a': 1, 'b': [2, {'c': 3}]}) = {'a': 'a', 'b': ['b/0', {'c': 'b/1/c'}]}
404
405 Args:
406 tree: A nested structure.
407 separator: The separator between parts of a path.
408 is_leaf: A Callable to evaluate whether the given node should be considered a leaf when
409 it otherwise would not, similarly to the is_leaf in jax.tree.map.
410
411 Returns:
412 A nested structure with the same structure as `tree`, but each leaf will be a string path.
413 Note that None is not considered a leaf by jax.tree_util, hence also preserved by
414 tree_paths.
415 """
416 return jax.tree.map_with_path(
417 lambda kp, _: separator.join(_key_entry_to_str(k) for k in kp), tree, is_leaf=is_leaf
418 )
419
420
421def flatten_items(

Callers 15

_pathsMethod · 0.90
__call__Method · 0.90
__call__Method · 0.90
test_with_tree_pathsMethod · 0.90
forwardMethod · 0.90
_is_passthroughMethod · 0.90
test_tree_pathsMethod · 0.90
_update_typesMethod · 0.90
_learner_treeMethod · 0.90
fnFunction · 0.90
callback_shardingMethod · 0.90

Calls 2

_key_entry_to_strFunction · 0.85
joinMethod · 0.80

Tested by 8

test_with_tree_pathsMethod · 0.72
forwardMethod · 0.72
test_tree_pathsMethod · 0.72
fnFunction · 0.72
callback_shardingMethod · 0.72
test_learnerMethod · 0.72