[compile][graph_partition]Add tensor size handling (#36038)
Signed-off-by: Xiao Fu <xiaofu@meta.com>
This commit is contained in:
@@ -473,9 +473,65 @@ def _merge_empty_only_subgraphs(
|
||||
prev_non_splitting_subgraph_id = subgraph_id
|
||||
|
||||
|
||||
def _decompose_size_nodes(graph: fx.GraphModule) -> None:
|
||||
"""Decompose x.size() into per-dim sym_size.int calls.
|
||||
|
||||
torch.Size objects cannot cross split boundaries because aot_autograd
|
||||
cannot handle them as submodule outputs. This replaces each size() call
|
||||
with individual sym_size.int(x, dim) nodes:
|
||||
- Dynamic dims (SymInt) → new sym_size.int node
|
||||
- Static dims (plain int) → inlined as literal constant
|
||||
"""
|
||||
# Dynamo captures x.size()/x.shape as call_method target="size".
|
||||
size_nodes = list(graph.graph.find_nodes(op="call_method", target="size"))
|
||||
|
||||
for node in size_nodes:
|
||||
tensor_node = node.args[0]
|
||||
ev = tensor_node.meta.get("example_value")
|
||||
assert ev is not None, (
|
||||
f"Tensor node '{tensor_node.name}' has no example_value metadata. "
|
||||
f"Cannot decompose size node '{node.name}'."
|
||||
)
|
||||
|
||||
# Build per-dim replacements: sym_size.int node or literal int.
|
||||
dims: list[fx.Node | int] = []
|
||||
with graph.graph.inserting_after(tensor_node):
|
||||
for i in range(ev.dim()):
|
||||
dim_val = ev.shape[i]
|
||||
if isinstance(dim_val, torch.SymInt):
|
||||
dn = graph.graph.call_function(
|
||||
torch.ops.aten.sym_size.int, args=(tensor_node, i)
|
||||
)
|
||||
dn.meta["example_value"] = dim_val
|
||||
dims.append(dn)
|
||||
elif isinstance(dim_val, int):
|
||||
dims.append(dim_val)
|
||||
else:
|
||||
raise AssertionError(
|
||||
f"dim_val is either torch.SymInt or int, "
|
||||
f"got {type(dim_val)} for dim {i} of "
|
||||
f"'{node.name}'"
|
||||
)
|
||||
|
||||
# Replace size node in each user's args.
|
||||
# Dynamo always passes size as a direct arg: view(clone, size)
|
||||
# → view(clone, d0, d1, ...)
|
||||
for user in list(node.users):
|
||||
new_args = []
|
||||
for arg in user.args:
|
||||
if arg is node:
|
||||
new_args.extend(dims)
|
||||
else:
|
||||
new_args.append(arg)
|
||||
user.args = tuple(new_args)
|
||||
graph.graph.erase_node(node)
|
||||
|
||||
|
||||
def split_graph(
|
||||
graph: fx.GraphModule, splitting_ops: list[str]
|
||||
) -> tuple[fx.GraphModule, list[SplitItem]]:
|
||||
_decompose_size_nodes(graph)
|
||||
|
||||
# split graph by ops
|
||||
subgraph_id = 0
|
||||
node_to_subgraph_id: dict[fx.Node, int] = {}
|
||||
|
||||
Reference in New Issue
Block a user