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

Function host_to_global_array

axlearn/common/utils.py:842–900  ·  view source on GitHub ↗

Converts the given host device arrays to global device arrays. Must be called within the context of a Mesh. Args: host_arrays: A nested tree of device arrays in host memory. Usually these present the per-host portion of the global input batch. We currently assume that p

(
    host_arrays: Nested[Union[np.ndarray, Tensor]],
    *,
    partition: Union[Nested[PartitionSpec], DataPartitionType] = DataPartitionType.FULL,
)

Source from the content-addressed store, hash-verified

840 f"corresponding value, got specification {partition_spec_tree} "
841 f"for value tree {treedef}. Original ValueError: {err}"
842 ) from None
843 axes = [None if a is proxy else a for a in axes]
844 assert len(axes) == treedef.num_leaves, (
845 f"({len(axes)} vs. {treedef.num_leaves}) {axes} {treedef}"
846 )
847 return jax.tree_util.tree_unflatten(treedef, axes)
848
849
850def input_partition_spec() -> PartitionSpec:
851 """Returns partition spec for the input batch.
852
853 We partition the inputs along all axes. For example, if the mesh has shape (64, 4) and axis
854 names of ("data", "model"), the partition spec will be (("data", "model"), None...) so that the
855 batch axis of every global tensor will be partitioned 256 (= 64 * 4) ways.
856
857 Must be called within the context of a Mesh.
858 """
859 mesh = thread_resources.env.physical_mesh
860 return PartitionSpec(
861 mesh.axis_names,
862 )
863
864
865# Key associated with per-example dataset dispatch index tensor, indicating which logical
866# batch index the example maps to.
867PHYSICAL_TO_LOGICAL_DISPATCH_KEY = "__physical_to_logical_batch_dispatch"
868
869
870def dispatch_input_batch(
871 input_batch: NestedTensor, *, batch_axis_names: Union[str, Sequence[str]] = "data"
872) -> NestedTensor:
873 """Constrains all leaf values in the input batch, then (optionally) dispatches examples
874 to a subset along the batch axis.
875
876 The dispatchings are applied to all nested dicts which contain a special dispatching key in
877 their root.
878
879 This is deprecated in favor of `axlearn.common.input_dispatch`.
880
881 Args:
882 input_batch: The input batch, where the first dimension of each leaf is the batch dim.
883 batch_axis_names: The name(s) of the batch axes.
884
885 Returns:
886 A nested tensor like the input batch, where each leaf contains
887 a subset of the input batch, and has been wrapped with sharding annotations.
888 N.B. some internal key-value pairs (like PHYSICAL_TO_LOGICAL_DISPATCH_KEY)
889 may be dropped after use if present.
890 """
891 logging.log_first_n(
892 logging.WARNING,
893 "dispatch_input_batch is deprecated. Please use `axlearn.common.input_dispatch` instead.",
894 n=1,
895 )
896
897 # Constrain the input batch.
898 input_batch = jax.tree.map(
899 lambda x: maybe_shard(x, PartitionSpec(batch_axis_names)), input_batch

Callers 1

Calls 3

mapMethod · 0.80

Tested by

no test coverage detected