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

Function vectorized_tree_map

axlearn/common/utils.py:467–480  ·  view source on GitHub ↗

Similar to jax.tree.map(), but vectorizes `fn` on VDict's.

(fn, tree, *rest)

Source from the content-addressed store, hash-verified

465
466
467def vectorized_tree_map(fn, tree, *rest):
468 """Similar to jax.tree.map(), but vectorizes `fn` on VDict's."""
469
470 def vectorized_fn(*nodes):
471 if isinstance(nodes[0], VDict):
472 if not jax.tree_util.tree_leaves(nodes[0]):
473 # This can happen when all VDict values are None and cause issues with jax.vmap.
474 return nodes[0]
475 nodes = [dict(**node) for node in nodes]
476 result = jax.vmap(functools.partial(vectorized_tree_map, fn))(*nodes)
477 return VDict(**result)
478 return fn(*nodes)
479
480 return jax.tree.map(vectorized_fn, tree, *rest, is_leaf=lambda t: isinstance(t, VDict))
481
482
483def expand_vdicts(tree: NestedTensor) -> NestedTensor:

Callers 11

tokens_to_scoresMethod · 0.90
_gather_beamsFunction · 0.90
_beam_initFunction · 0.90
beam_search_loop_body_fnFunction · 0.90
_decode_initFunction · 0.90
update_fnFunction · 0.90
_compute_rms_normsFunction · 0.90
_compute_covarianceFunction · 0.90

Calls 1

mapMethod · 0.80