-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir][complex] Prevent underflow in complex.abs (#79786) #81092
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,29 +26,59 @@ namespace mlir { | |
using namespace mlir; | ||
|
||
namespace { | ||
// The algorithm is listed in https://dl.acm.org/doi/pdf/10.1145/363717.363780. | ||
struct AbsOpConversion : public OpConversionPattern<complex::AbsOp> { | ||
using OpConversionPattern<complex::AbsOp>::OpConversionPattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(complex::AbsOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
auto loc = op.getLoc(); | ||
auto type = op.getType(); | ||
mlir::ImplicitLocOpBuilder b(op.getLoc(), rewriter); | ||
|
||
arith::FastMathFlagsAttr fmf = op.getFastMathFlagsAttr(); | ||
|
||
Value real = | ||
rewriter.create<complex::ReOp>(loc, type, adaptor.getComplex()); | ||
Value imag = | ||
rewriter.create<complex::ImOp>(loc, type, adaptor.getComplex()); | ||
Value realSqr = | ||
rewriter.create<arith::MulFOp>(loc, real, real, fmf.getValue()); | ||
Value imagSqr = | ||
rewriter.create<arith::MulFOp>(loc, imag, imag, fmf.getValue()); | ||
Value sqNorm = | ||
rewriter.create<arith::AddFOp>(loc, realSqr, imagSqr, fmf.getValue()); | ||
|
||
rewriter.replaceOpWithNewOp<math::SqrtOp>(op, sqNorm); | ||
Type elementType = op.getType(); | ||
Value arg = adaptor.getComplex(); | ||
|
||
Value zero = | ||
b.create<arith::ConstantOp>(elementType, b.getZeroAttr(elementType)); | ||
Value one = b.create<arith::ConstantOp>(elementType, | ||
b.getFloatAttr(elementType, 1.0)); | ||
|
||
Value real = b.create<complex::ReOp>(elementType, arg); | ||
Value imag = b.create<complex::ImOp>(elementType, arg); | ||
|
||
Value realIsZero = | ||
b.create<arith::CmpFOp>(arith::CmpFPredicate::OEQ, real, zero); | ||
Value imagIsZero = | ||
b.create<arith::CmpFOp>(arith::CmpFPredicate::OEQ, imag, zero); | ||
|
||
// Real > Imag | ||
Value imagDivReal = b.create<arith::DivFOp>(imag, real, fmf.getValue()); | ||
Value imagSq = | ||
b.create<arith::MulFOp>(imagDivReal, imagDivReal, fmf.getValue()); | ||
Value imagSqPlusOne = b.create<arith::AddFOp>(imagSq, one, fmf.getValue()); | ||
Value imagSqrt = b.create<math::SqrtOp>(imagSqPlusOne, fmf.getValue()); | ||
Value realAbs = b.create<math::AbsFOp>(real, fmf.getValue()); | ||
Value absImag = b.create<arith::MulFOp>(imagSqrt, realAbs, fmf.getValue()); | ||
|
||
// Real <= Imag | ||
Value realDivImag = b.create<arith::DivFOp>(real, imag, fmf.getValue()); | ||
Value realSq = | ||
b.create<arith::MulFOp>(realDivImag, realDivImag, fmf.getValue()); | ||
Value realSqPlusOne = b.create<arith::AddFOp>(realSq, one, fmf.getValue()); | ||
Value realSqrt = b.create<math::SqrtOp>(realSqPlusOne, fmf.getValue()); | ||
Value imagAbs = b.create<math::AbsFOp>(imag, fmf.getValue()); | ||
Value absReal = b.create<arith::MulFOp>(realSqrt, imagAbs, fmf.getValue()); | ||
|
||
rewriter.replaceOpWithNewOp<arith::SelectOp>( | ||
op, realIsZero, imagAbs, | ||
b.create<arith::SelectOp>( | ||
imagIsZero, realAbs, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
b.create<arith::SelectOp>( | ||
b.create<arith::CmpFOp>(arith::CmpFPredicate::OGT, real, imag), | ||
absImag, absReal))); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,27 @@ func.func @angle(%arg: complex<f32>) -> f32 { | |
func.return %angle : f32 | ||
} | ||
|
||
func.func @test_element_f64(%input: tensor<?xcomplex<f64>>, | ||
%func: (complex<f64>) -> f64) { | ||
%c0 = arith.constant 0 : index | ||
%c1 = arith.constant 1 : index | ||
%size = tensor.dim %input, %c0: tensor<?xcomplex<f64>> | ||
|
||
scf.for %i = %c0 to %size step %c1 { | ||
%elem = tensor.extract %input[%i]: tensor<?xcomplex<f64>> | ||
|
||
%val = func.call_indirect %func(%elem) : (complex<f64>) -> f64 | ||
vector.print %val : f64 | ||
scf.yield | ||
} | ||
func.return | ||
} | ||
|
||
func.func @abs(%arg: complex<f64>) -> f64 { | ||
%abs = complex.abs %arg : complex<f64> | ||
func.return %abs : f64 | ||
} | ||
|
||
func.func @entry() { | ||
// complex.sqrt test | ||
%sqrt_test = arith.constant dense<[ | ||
|
@@ -300,5 +321,38 @@ func.func @entry() { | |
call @test_element(%angle_test_cast, %angle_func) | ||
: (tensor<?xcomplex<f32>>, (complex<f32>) -> f32) -> () | ||
|
||
// complex.abs test | ||
%abs_test = arith.constant dense<[ | ||
(1.0, 1.0), | ||
// CHECK: 1.414 | ||
(1.0e300, 1.0e300), | ||
// CHECK-NEXT: 1.41421e+300 | ||
(1.0e-300, 1.0e-300), | ||
// CHECK-NEXT: 1.41421e-300 | ||
(5.0, 0.0), | ||
// CHECK-NEXT: 5 | ||
(0.0, 6.0), | ||
// CHECK-NEXT: 6 | ||
(7.0, 8.0), | ||
// CHECK-NEXT: 10.6301 | ||
(-1.0, -1.0), | ||
// CHECK-NEXT: 1.414 | ||
(-1.0e300, -1.0e300), | ||
// CHECK-NEXT: 1.41421e+300 | ||
(-1.0, 0.0), | ||
// CHECK-NOT: -1 | ||
// CHECK-NEXT: 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add test case the imag part is zero. |
||
(0.0, -1.0) | ||
// CHECK-NOT: -1 | ||
// CHECK-NEXT: 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add test case where the real is zero. |
||
]> : tensor<10xcomplex<f64>> | ||
%abs_test_cast = tensor.cast %abs_test | ||
: tensor<10xcomplex<f64>> to tensor<?xcomplex<f64>> | ||
|
||
%abs_func = func.constant @abs : (complex<f64>) -> f64 | ||
|
||
call @test_element_f64(%abs_test_cast, %abs_func) | ||
: (tensor<?xcomplex<f64>>, (complex<f64>) -> f64) -> () | ||
|
||
func.return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
imagAbs
if real is zero.