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
8 changes: 7 additions & 1 deletion onnxscript/function_libs/torch_lib/ops/prims.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,12 +772,18 @@ def prims_sub(self: TTensor, other: TTensor) -> TTensor:
return op.Sub(self, other)


@torch_op("prims::sum", trace_only=True)
def prims_sum(
inp: TensorType, dims: Optional[Sequence[int]], output_dtype: Optional[int] = None
) -> TensorType:
"""sum(Tensor inp, int[]? dims, *, ScalarType? output_dtype=None) -> Tensor"""

Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dims parameter should be normalized to None when it's an empty sequence, following the pattern used in prims_var (lines 839-841). ONNX's ReduceSum operator may not handle empty sequences correctly, and converting empty sequences to None ensures the operator reduces over all dimensions as expected.

Suggested change
if dims is not None and len(dims) == 0:
dims = None

Copilot uses AI. Check for mistakes.
raise NotImplementedError()
result = op.ReduceSum(inp, dims, keepdims=False)

if output_dtype is not None and output_dtype != -1:
result = op.Cast(result, to=output_dtype)

return result


def prims_svd(A: TensorType, full_matrices: bool) -> tuple[TensorType, TensorType, TensorType]:
Expand Down
12 changes: 12 additions & 0 deletions tests/function_libs/torch_lib/e2e_ops_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,18 @@ def forward(self, x, index, update):
)
_testing.assert_onnx_program(onnx_program)

def test_std_mean(self):
"""Test torch.std_mean which will be decomposed into prims.sum."""

class Model(torch.nn.Module):
def forward(self, x):
return torch.std_mean(x)

onnx_program = torch.onnx.export(
Model(), (torch.rand(10, 10, 10),), dynamo=True, verbose=False
)
_testing.assert_onnx_program(onnx_program)


if __name__ == "__main__":
unittest.main()
Loading