Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion deepspeed/compile/fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/v1/compile/test_compile_autosp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading