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