Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force double precision in aten.mean.dim #1308

Closed
wants to merge 1 commit into from
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
1 change: 0 additions & 1 deletion e2e_testing/xfail_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"SqueezeDimModule_unitDim",
"MeanModule_basic",
"MeanDynamicSizesModule_basic",
"MeanDimEmptyDimModule_basic",
"NumToTensorFloatModule_basic",
"AtenToDeviceModule_basic",
"AvgPool2dStaticModule_basic",
Expand Down
17 changes: 15 additions & 2 deletions lib/Dialect/Torch/Transforms/DecomposeComplexOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,9 +1214,18 @@ class DecomposeAtenMeanDimOp : public OpRewritePattern<AtenMeanDimOp> {
op, "expected `dim` to be `None` or constructed from list construct");
}

// Upcasting the input tensor to `F64` dtype for higher precision during the
// computation of the result.
BaseTensorType outputTensorType = outputType.cast<BaseTensorType>();
Type outputTypeAsF64 = outputTensorType.getWithSizesAndDtype(
outputTensorType.getSizes(), rewriter.getF64Type());;
if (inputType.getDtype().getIntOrFloatBitWidth() != 64) {
input = convertTensorToDtype(rewriter, loc, input, rewriter.getF64Type());
}

// Compute sum along dimensions specified in `dimList`.
Value sumAlongDims = rewriter.create<AtenSumDimIntListOp>(
loc, outputType, input, dimList, keepDim, dtype);
loc, outputTypeAsF64, input, dimList, keepDim, dtype);

// `productDimSize` is product of sizes of dimensions to be reduced.
Value productDimSize;
Expand All @@ -1232,8 +1241,12 @@ class DecomposeAtenMeanDimOp : public OpRewritePattern<AtenMeanDimOp> {
rewriter.create<AtenMulIntOp>(loc, productDimSize, dimSize);
}
}
rewriter.replaceOpWithNewOp<AtenDivScalarOp>(op, outputType, sumAlongDims,
Value result = rewriter.create<AtenDivScalarOp>(loc, outputTypeAsF64, sumAlongDims,
productDimSize);
if (outputTensorType.getDtype().getIntOrFloatBitWidth() != 64) {
result = convertTensorToDtype(rewriter, loc, result, outputTensorType.getDtype());
}
rewriter.replaceOp(op, result);
return success();
}
};
Expand Down
40 changes: 40 additions & 0 deletions python/torch_mlir_e2e_test/test_suite/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,46 @@ def MeanDimNoneDimModule_basic(module, tu: TestUtils):

# ==============================================================================

class MeanDimLargeInputModule(torch.nn.Module):

def __init__(self):
super().__init__()

@export
@annotate_args([
None,
([-1, -1, -1, -1], torch.float32, True),
])
def forward(self, x):
return torch.ops.aten.mean(x, (2, 3))


@register_test_case(module_factory=lambda: MeanDimLargeInputModule())
def MeanDimLargeInputModule_basic(module, tu: TestUtils):
module.forward(100 + tu.rand(3, 4, 1024, 8192))

# ==============================================================================

class MeanDimLargeInputDtypeModule(torch.nn.Module):

def __init__(self):
super().__init__()

@export
@annotate_args([
None,
([-1, -1, -1, -1], torch.float32, True),
])
def forward(self, x):
return torch.ops.aten.mean(x, (2, 3), dtype=torch.float32)


@register_test_case(module_factory=lambda: MeanDimLargeInputDtypeModule())
def MeanDimLargeInputDtypeModule_basic(module, tu: TestUtils):
module.forward(100 + tu.rand(3, 4, 1024, 8192))

# ==============================================================================

class VarUnbiasedModule(torch.nn.Module):
def __init__(self):
super().__init__()
Expand Down