Skip to content

[mlir][math] lower rsqrt to sqrt + fdiv #91344

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

Merged
merged 3 commits into from
May 13, 2024
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
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/Math/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void populateExpandPowFPattern(RewritePatternSet &patterns);
void populateExpandFPowIPattern(RewritePatternSet &patterns);
void populateExpandRoundFPattern(RewritePatternSet &patterns);
void populateExpandRoundEvenPattern(RewritePatternSet &patterns);
void populateExpandRsqrtPattern(RewritePatternSet &patterns);
void populateMathAlgebraicSimplificationPatterns(RewritePatternSet &patterns);

struct MathPolynomialApproximationOptions {
Expand Down
21 changes: 21 additions & 0 deletions mlir/lib/Dialect/Math/Transforms/ExpandPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,23 @@ static LogicalResult convertRoundEvenOp(math::RoundEvenOp op,
return success();
}

// Convert `math.rsqrt` into `arith.divf` + `math.sqrt`
static LogicalResult convertRsqrtOp(math::RsqrtOp op,
PatternRewriter &rewriter) {

auto operand = op.getOperand();
auto operandTy = operand.getType();
auto eTy = getElementTypeOrSelf(operandTy);
if (!isa<FloatType>(eTy))
Copy link
Member

@pashu123 pashu123 May 7, 2024

Choose a reason for hiding this comment

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

I think the expansion will work fine for f16, f32 and f64. You can enable them.

Copy link
Contributor

Choose a reason for hiding this comment

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

doesn't FloatType encompass all of those?

Copy link
Member

Choose a reason for hiding this comment

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

I see, yes. I only saw the tests. Could you edit the tests and add the F16 or F64 ones, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure -- @srcarroll, you're right, F16 and F64 are indeed supported, let me add tests for them.

return failure();

Location loc = op->getLoc();
auto constOneFloat = createFloatConst(loc, operandTy, 1.0, rewriter);
auto sqrtOp = rewriter.create<math::SqrtOp>(loc, operand);
rewriter.replaceOpWithNewOp<arith::DivFOp>(op, constOneFloat, sqrtOp);
return success();
}

void mlir::populateExpandCtlzPattern(RewritePatternSet &patterns) {
patterns.add(convertCtlzOp);
}
Expand Down Expand Up @@ -678,3 +695,7 @@ void mlir::populateExpandFloorFPattern(RewritePatternSet &patterns) {
void mlir::populateExpandRoundEvenPattern(RewritePatternSet &patterns) {
patterns.add(convertRoundEvenOp);
}

void mlir::populateExpandRsqrtPattern(RewritePatternSet &patterns) {
patterns.add(convertRsqrtOp);
}
70 changes: 70 additions & 0 deletions mlir/test/Dialect/Math/expand-math.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,73 @@ func.func @math_fpowi_to_powf_scalar(%0 : f32, %1: i64) -> f32 {
// CHECK: %[[AND:.*]] = arith.andi %[[CMPF1]], %[[CMPF]] : i1
// CHECK: %[[SEL:.*]] = arith.select %[[AND]], %[[MUL1]], %[[EXP]] : f32
// CHECK: return %[[SEL]] : f32

// -----

// CHECK-LABEL: func.func @rsqrt
// CHECK-SAME: (%[[ARG:.*]]: f16)
// CHECK-SAME: -> f16
// CHECK-DAG: %[[CST:.*]] = arith.constant 1.000000e+00 : f16
// CHECK-DAG: %[[SQRT:.*]] = math.sqrt %[[ARG]] : f16
// CHECK-DAG: %[[DIV:.*]] = arith.divf %[[CST]], %[[SQRT]] : f16
// CHECK: return %[[DIV]] : f16
func.func @rsqrt16(%float: f16) -> (f16) {
%float_result = math.rsqrt %float : f16
return %float_result : f16
}

// -----

// CHECK-LABEL: func.func @rsqrt
// CHECK-SAME: (%[[ARG:.*]]: f32)
// CHECK-SAME: -> f32
// CHECK-DAG: %[[CST:.*]] = arith.constant 1.000000e+00 : f32
// CHECK-DAG: %[[SQRT:.*]] = math.sqrt %[[ARG]] : f32
// CHECK-DAG: %[[DIV:.*]] = arith.divf %[[CST]], %[[SQRT]] : f32
// CHECK: return %[[DIV]] : f32
func.func @rsqrt32(%float: f32) -> (f32) {
%float_result = math.rsqrt %float : f32
return %float_result : f32
}

// -----

// CHECK-LABEL: func.func @rsqrt
// CHECK-SAME: (%[[ARG:.*]]: f64)
// CHECK-SAME: -> f64
// CHECK-DAG: %[[CST:.*]] = arith.constant 1.000000e+00 : f64
// CHECK-DAG: %[[SQRT:.*]] = math.sqrt %[[ARG]] : f64
// CHECK-DAG: %[[DIV:.*]] = arith.divf %[[CST]], %[[SQRT]] : f64
// CHECK: return %[[DIV]] : f64
func.func @rsqrt64(%float: f64) -> (f64) {
%float_result = math.rsqrt %float : f64
return %float_result : f64
}

// -----

// CHECK-LABEL: func.func @rsqrt_vec
// CHECK-SAME: (%[[ARG:.*]]: vector<5xf32>)
// CHECK-SAME: -> vector<5xf32>
// CHECK-DAG: %[[CST:.*]] = arith.constant dense<1.000000e+00> : vector<5xf32>
// CHECK-DAG: %[[SQRT:.*]] = math.sqrt %[[ARG]] : vector<5xf32>
// CHECK-DAG: %[[DIV:.*]] = arith.divf %[[CST]], %[[SQRT]] : vector<5xf32>
// CHECK: return %[[DIV]] : vector<5xf32>
func.func @rsqrt_vec(%float: vector<5xf32>) -> (vector<5xf32>) {
%float_result = math.rsqrt %float : vector<5xf32>
return %float_result : vector<5xf32>
}

// -----

// CHECK-LABEL: func.func @rsqrt_tns
// CHECK-SAME: (%[[ARG:.*]]: tensor<5x8xf32>)
// CHECK-SAME: -> tensor<5x8xf32>
// CHECK-DAG: %[[CST:.*]] = arith.constant dense<1.000000e+00> : tensor<5x8xf32>
// CHECK-DAG: %[[SQRT:.*]] = math.sqrt %[[ARG]] : tensor<5x8xf32>
// CHECK-DAG: %[[DIV:.*]] = arith.divf %[[CST]], %[[SQRT]] : tensor<5x8xf32>
// CHECK: return %[[DIV]] : tensor<5x8xf32>
func.func @rsqrt_tns(%float: tensor<5x8xf32>) -> (tensor<5x8xf32>) {
%float_result = math.rsqrt %float : tensor<5x8xf32>
return %float_result : tensor<5x8xf32>
}
1 change: 1 addition & 0 deletions mlir/test/lib/Dialect/Math/TestExpandMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void TestExpandMathPass::runOnOperation() {
populateExpandFPowIPattern(patterns);
populateExpandRoundFPattern(patterns);
populateExpandRoundEvenPattern(patterns);
populateExpandRsqrtPattern(patterns);
(void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns));
}

Expand Down
41 changes: 41 additions & 0 deletions mlir/test/mlir-cpu-runner/test-expand-math-approx.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,46 @@ func.func @atanh() {
return
}

// -------------------------------------------------------------------------- //
// Rsqrt.
// -------------------------------------------------------------------------- //

func.func @rsqrt_f32(%a : f32) {
%r = math.rsqrt %a : f32
vector.print %r : f32
return
}

func.func @rsqrt_3xf32(%a : vector<3xf32>) {
%r = math.rsqrt %a : vector<3xf32>
vector.print %r : vector<3xf32>
return
}

func.func @rsqrt() {
// CHECK: 1
%zero = arith.constant 1.0 : f32
call @rsqrt_f32(%zero) : (f32) -> ()

// CHECK: 0.707107
%cst1 = arith.constant 2.0 : f32
call @rsqrt_f32(%cst1) : (f32) -> ()

// CHECK: inf
%cst2 = arith.constant 0.0 : f32
call @rsqrt_f32(%cst2) : (f32) -> ()

// CHECK: -nan
%cst3 = arith.constant -1.0 : f32
call @rsqrt_f32(%cst3) : (f32) -> ()

// CHECK: 0.5, 1.41421, 0.57735
%vec_x = arith.constant dense<[4.0, 0.5, 3.0]> : vector<3xf32>
call @rsqrt_3xf32(%vec_x) : (vector<3xf32>) -> ()

return
}

func.func @main() {
call @exp2f() : () -> ()
call @roundf() : () -> ()
Expand All @@ -844,5 +884,6 @@ func.func @main() {
call @asinh() : () -> ()
call @acosh() : () -> ()
call @atanh() : () -> ()
call @rsqrt() : () -> ()
return
}
Loading