Skip to content
Closed
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: 5 additions & 2 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6350,10 +6350,13 @@ def aten_maximum(self: TTensor, other: TTensor) -> TTensor:
return op.Max(self, other)


@torch_op("aten::mean")
def aten_mean(self: TReal) -> TReal:
@torch_op("aten::mean", trace_only=True)
def aten_mean(self: TReal, dtype: int = -1) -> TReal:
"""mean(Tensor self, *, ScalarType? dtype=None) -> Tensor"""

if dtype != -1:
self = op.Cast(self, to=dtype)

result = op.ReduceMean(self)
return op.Squeeze(result)

Expand Down
13 changes: 13 additions & 0 deletions tests/function_libs/torch_lib/e2e_ops_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,19 @@ def forward(self, x):
)
_testing.assert_onnx_program(onnx_program)

def test_mean_dtype_casts_before_reduction(self):
class Model(torch.nn.Module):
def forward(self, x):
return torch.mean(x, dtype=torch.float64)

model = Model()
x = torch.tensor([1e8, 1.0, -1e8], dtype=torch.float32)
onnx_program = torch.onnx.export(model, (x,), dynamo=True, optimize=False)

_testing.assert_onnx_program(onnx_program)
actual = onnx_program.call_reference({onnx_program.model.graph.inputs[0].name: x})[0]
torch.testing.assert_close(actual, model(x))

def test_aten_histc_float(self):
class Model(torch.nn.Module):
def forward(self, x):
Expand Down