MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / shard_tensor_node

Function shard_tensor_node

deepspeed/compile/util.py:596–625  ·  view source on GitHub ↗
(gm: GraphModule, tensor_node: Node)

Source from the content-addressed store, hash-verified

594
595
596def shard_tensor_node(gm: GraphModule, tensor_node: Node):
597 from .fx import find_node_by_name, get_node_shape_meta, replace_node_users
598 val = get_node_shape_meta(tensor_node)
599 assert val is not None, f"Node {tensor_node.name} has no shape metadata"
600
601 seq_dim = get_autosp_seq_dim(tensor_node)
602 seq_len = val.shape[seq_dim]
603
604 assert isinstance(
605 seq_len,
606 torch.SymInt), (f"Expected sequence dimension to be {torch.SymInt!r} but instead found {type(seq_len)!r}")
607
608 symb_seq_int_node = find_node_by_name(gm, str(seq_len))
609 assert symb_seq_int_node, f"Unable to find symbolic placeholder for {seq_len}"
610
611 slice_all, slice_range = create_symbolic_slice_indices(gm, symb_seq_int_node)
612 indices = tuple(slice_range if dim == seq_dim else slice_all for dim in range(val.ndim))
613
614 positions = {node: i for i, node in enumerate(gm.graph.nodes)}
615 # Insert after the later dependency so the new getitem does not appear
616 # before the symbolic slice nodes in graph order. Torch 2.9 bf16 can place
617 # the SymInt placeholder after the tensor placeholder.
618 anchor_node = slice_range if positions[slice_range] > positions[tensor_node] else tensor_node
619 with gm.graph.inserting_after(anchor_node):
620 sliced_node = gm.graph.call_function(
621 operator.getitem,
622 args=(tensor_node, indices),
623 )
624
625 replace_node_users(tensor_node, sliced_node, exclude=[sliced_node])

Calls 5

get_node_shape_metaFunction · 0.85
get_autosp_seq_dimFunction · 0.85
find_node_by_nameFunction · 0.85
replace_node_usersFunction · 0.85