Preserve layout-changing clones (#44574)

Signed-off-by: Michael Gschwind <mgschwind@nvidia.com>
Co-authored-by: Michael Gschwind <mgschwind@nvidia.com>
This commit is contained in:
Mike G
2026-06-05 20:45:24 -04:00
committed by GitHub
co-authored by Michael Gschwind
parent 4765f0f189
commit 2f27c9a150
2 changed files with 48 additions and 0 deletions
@@ -16,6 +16,26 @@ from ..vllm_inductor_pass import VllmInductorPass
logger = init_logger(__name__)
def clone_preserves_layout(node: fx.Node, original_node: fx.Node) -> bool:
node_val = node.meta.get("val")
original_val = original_node.meta.get("val")
if node_val is None or original_val is None:
return True
try:
node_stride = tuple(node_val.stride())
original_stride = tuple(original_val.stride())
node_storage_offset = node_val.storage_offset()
original_storage_offset = original_val.storage_offset()
except (AttributeError, RuntimeError):
return True
return (
node_stride == original_stride
and node_storage_offset == original_storage_offset
)
def user_writes_to_node(user: fx.Node, node: fx.Node) -> bool:
if user.op == "output":
return False
@@ -79,6 +99,15 @@ class UnsafeCloneEliminationPass(VllmInductorPass):
original_node = node.args[0]
assert isinstance(original_node, fx.Node)
if not clone_preserves_layout(node, original_node):
logger.debug(
"Clone removal not possible, clone changes layout: "
"original_node=%s node=%s",
original_node,
node,
)
continue
# Clone needs to be preserved if node is getting written to and
# the old value is used again.
# This could only happen if an inplace implementation was lowered.