From c9a7fb027f0175e1f76e4112af62c1ff1394d104 Mon Sep 17 00:00:00 2001 From: "Jin, Youzhi" Date: Tue, 6 Oct 2026 18:27:38 +0000 Subject: [PATCH] Read AutoSP shape metadata without tensor truthiness get_node_shape_meta() used `meta["val"] or meta["example_value"]`, which calls bool() on the stored tensor. Once shape propagation fills meta["val"] with a real tensor, this raises for multi-element tensors and silently falls back to example_value for a zero-valued scalar. Compare with None instead. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Jin, Youzhi --- deepspeed/compile/fx.py | 7 +++++- tests/unit/v1/compile/test_compile_autosp.py | 23 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/deepspeed/compile/fx.py b/deepspeed/compile/fx.py index 232f967fa328..f3a4dc7c63eb 100644 --- a/deepspeed/compile/fx.py +++ b/deepspeed/compile/fx.py @@ -172,7 +172,12 @@ def find_node_by_name(gm: GraphModule, name: str) -> Optional[Node]: def get_node_shape_meta(node: Node) -> Optional[torch.Tensor]: - return node.meta.get("val") or node.meta.get("example_value") + # Compare with None explicitly: `or` would call bool() on the tensor, which raises for + # multi-element tensors and skips a valid zero-valued scalar. + value = node.meta.get("val") + if value is not None: + return value + return node.meta.get("example_value") def find_node_by_tag(gm: GraphModule, tag: str) -> Optional[Node]: diff --git a/tests/unit/v1/compile/test_compile_autosp.py b/tests/unit/v1/compile/test_compile_autosp.py index ecdfe6f74ec3..25f082e02221 100644 --- a/tests/unit/v1/compile/test_compile_autosp.py +++ b/tests/unit/v1/compile/test_compile_autosp.py @@ -216,6 +216,29 @@ def test(self, seq_len): assert node.target == F.scaled_dot_product_attention +class TestNodeShapeMeta: + + # Shape propagation stores real tensors in meta["val"]; reading them must not depend on their truthiness. + @pytest.mark.parametrize("value", [torch.empty(2, 3), torch.zeros(())], ids=["multi_element", "zero_scalar"]) + def test_prefers_val_over_example_value(self, value): + from deepspeed.compile.fx import get_node_shape_meta + + node = Graph().placeholder("input") + node.meta["val"] = value + node.meta["example_value"] = torch.empty(4) + + assert get_node_shape_meta(node) is value + + def test_falls_back_to_example_value(self): + from deepspeed.compile.fx import get_node_shape_meta + + node = Graph().placeholder("input") + example_value = torch.empty(4) + node.meta["example_value"] = example_value + + assert get_node_shape_meta(node) is example_value + + class TestInputIdCompile: @pytest.mark.sequential