-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][math] powf(a, b) drop support when a < 0
#126338
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 7 commits
0e790b6
b248c22
23d2f09
0e7dc19
c52ba9f
e1e06ec
9cf3d3b
ec9f128
95c3d55
79c4ef4
393aaa8
f5205a6
d5ee522
48b9405
640ec45
e976ba6
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 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -311,40 +311,83 @@ static LogicalResult convertFPowIOp(math::FPowIOp op, | |||||||||||||||||||||||||||||
| return success(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Converts Powf(float a, float b) (meaning a^b) to exp^(b * ln(a)) | ||||||||||||||||||||||||||||||
| // Convert Powf(float a, float b) for special cases when b is constant: | ||||||||||||||||||||||||||||||
| // when b == 0, or |b| == 0.5, 1.0, or 2.0. | ||||||||||||||||||||||||||||||
| static LogicalResult convertSpecialPowfOp(math::PowFOp op, | ||||||||||||||||||||||||||||||
| PatternRewriter &rewriter) { | ||||||||||||||||||||||||||||||
| ImplicitLocOpBuilder b(op->getLoc(), rewriter); | ||||||||||||||||||||||||||||||
| Value operandA = op.getOperand(0); | ||||||||||||||||||||||||||||||
| Value operandB = op.getOperand(1); | ||||||||||||||||||||||||||||||
| auto opType = operandA.getType(); | ||||||||||||||||||||||||||||||
| auto baseType = operandB.getType(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| auto &sem = dyn_cast<mlir::FloatType>(getElementTypeOrSelf(baseType)) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| .getFloatSemantics(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| auto valueB = APFloat(sem); | ||||||||||||||||||||||||||||||
| if (!matchPattern(operandB, m_ConstantFloat(&valueB))) { | ||||||||||||||||||||||||||||||
| // Not a constant, return failure | ||||||||||||||||||||||||||||||
| return failure(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| float floatValueB = valueB.convertToFloat(); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| /// We don't rely on operator== working on double values, as | |
| /// it returns true for things that are clearly not equal, like -0.0 and 0.0. | |
| /// As such, this method can be used to do an exact bit-for-bit comparison of | |
| /// two floating point values. | |
| /// | |
| /// We leave the version with the double argument here because it's just so | |
| /// convenient to write "2.0" and the like. Without this function we'd | |
| /// have to duplicate its logic everywhere it's called. | |
| bool isExactlyValue(double V) const { | |
| bool ignored; | |
| APFloat Tmp(V); | |
| Tmp.convert(getSemantics(), APFloat::rmNearestTiesToEven, &ignored); | |
| return bitwiseIsEqual(Tmp); | |
| } |
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.
Fixed!
|
Contributor
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. At least we need all the special cases are tested in this file.
Contributor
Author
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. my apology for that tihs PR got verbose, I made appropriate tests and runs well!! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,25 +202,11 @@ func.func @roundf_func(%a: f32) -> f32 { | |
|
|
||
| // CHECK-LABEL: func @powf_func | ||
| // CHECK-SAME: ([[ARG0:%.+]]: f64, [[ARG1:%.+]]: f64) | ||
| func.func @powf_func(%a: f64, %b: f64) ->f64 { | ||
| // CHECK-DAG: [[CST0:%.+]] = arith.constant 0.000000e+00 | ||
| // CHECK-DAG: [[CST1:%.+]] = arith.constant 1.0 | ||
| // CHECK-DAG: [[TWO:%.+]] = arith.constant 2.000000e+00 | ||
| // CHECK-DAG: [[NEGONE:%.+]] = arith.constant -1.000000e+00 | ||
| // CHECK-DAG: [[SQR:%.+]] = arith.mulf [[ARG0]], [[ARG0]] | ||
| // CHECK-DAG: [[HALF:%.+]] = arith.divf [[ARG1]], [[TWO]] | ||
| // CHECK-DAG: [[LOG:%.+]] = math.log [[SQR]] | ||
| // CHECK-DAG: [[MULT:%.+]] = arith.mulf [[HALF]], [[LOG]] | ||
| // CHECK-DAG: [[EXPR:%.+]] = math.exp [[MULT]] | ||
| // CHECK-DAG: [[NEGEXPR:%.+]] = arith.mulf [[EXPR]], [[NEGONE]] | ||
| // CHECK-DAG: [[REMF:%.+]] = arith.remf [[ARG1]], [[TWO]] | ||
| // CHECK-DAG: [[CMPNEG:%.+]] = arith.cmpf olt, [[ARG0]] | ||
| // CHECK-DAG: [[CMPZERO:%.+]] = arith.cmpf one, [[REMF]] | ||
| // CHECK-DAG: [[AND:%.+]] = arith.andi [[CMPZERO]], [[CMPNEG]] | ||
| // CHECK-DAG: [[CMPZERO:%.+]] = arith.cmpf oeq, [[ARG1]], [[CST0]] | ||
| // CHECK-DAG: [[SEL:%.+]] = arith.select [[AND]], [[NEGEXPR]], [[EXPR]] | ||
| // CHECK-DAG: [[SEL1:%.+]] = arith.select [[CMPZERO]], [[CST1]], [[SEL]] | ||
| // CHECK: return [[SEL1]] | ||
| func.func @powf_func(%a: f64, %b: f64) -> f64 { | ||
| // CHECK: [[LOGA:%.+]] = math.log [[ARG0]] : f64 | ||
| // CHECK: [[MUL:%.+]] = arith.mulf [[ARG1]], [[LOGA]] : f64 | ||
| // CHECK: [[EXP:%.+]] = math.exp [[MUL]] : f64 | ||
| // CHECK: return [[EXP]] : f64 | ||
|
||
| %ret = math.powf %a, %b : f64 | ||
| return %ret : f64 | ||
| } | ||
|
|
@@ -602,26 +588,11 @@ func.func @math_fpowi_to_powf_tensor(%0 : tensor<8xf32>, %1: tensor<8xi32>) -> t | |
| return %2 : tensor<8xf32> | ||
| } | ||
| // CHECK-SAME: (%[[ARG0:.*]]: tensor<8xf32>, %[[ARG1:.*]]: tensor<8xi32>) -> tensor<8xf32> { | ||
| // CHECK-DAG: %[[CSTNEG1:.*]] = arith.constant dense<-1.000000e+00> : tensor<8xf32> | ||
| // CHECK-DAG: %[[CST2:.*]] = arith.constant dense<2.000000e+00> : tensor<8xf32> | ||
| // CHECK-DAG: %[[CST0:.*]] = arith.constant dense<0.000000e+00> : tensor<8xf32> | ||
| // CHECK-DAG: %[[CST1:.+]] = arith.constant dense<1.000000e+00> : tensor<8xf32> | ||
| // CHECK: %[[TOFP:.*]] = arith.sitofp %[[ARG1]] : tensor<8xi32> to tensor<8xf32> | ||
| // CHECK: %[[SQ:.*]] = arith.mulf %[[ARG0]], %[[ARG0]] : tensor<8xf32> | ||
| // CHECK: %[[DIV:.*]] = arith.divf %[[TOFP]], %[[CST2]] : tensor<8xf32> | ||
| // CHECK: %[[LG:.*]] = math.log %[[SQ]] : tensor<8xf32> | ||
| // CHECK: %[[MUL:.*]] = arith.mulf %[[DIV]], %[[LG]] : tensor<8xf32> | ||
| // CHECK: %[[EXP:.*]] = math.exp %[[MUL]] : tensor<8xf32> | ||
| // CHECK: %[[MUL1:.*]] = arith.mulf %[[EXP]], %[[CSTNEG1]] : tensor<8xf32> | ||
| // CHECK: %[[REM:.*]] = arith.remf %[[TOFP]], %[[CST2]] : tensor<8xf32> | ||
| // CHECK: %[[CMPF:.*]] = arith.cmpf olt, %[[ARG0]], %[[CST0]] : tensor<8xf32> | ||
| // CHECK: %[[CMPF1:.*]] = arith.cmpf one, %[[REM]], %[[CST0]] : tensor<8xf32> | ||
| // CHECK: %[[AND:.*]] = arith.andi %[[CMPF1]], %[[CMPF]] : tensor<8xi1> | ||
| // CHECK: %[[CMPZERO:.*]] = arith.cmpf oeq, %[[TOFP]], %[[CST0]] | ||
| // CHECK: %[[SEL:.*]] = arith.select %[[AND]], %[[MUL1]], %[[EXP]] : tensor<8xi1>, tensor<8xf32> | ||
| // CHECK: %[[SEL1:.+]] = arith.select %[[CMPZERO]], %[[CST1]], %[[SEL]] | ||
| // CHECK: return %[[SEL1]] : tensor<8xf32> | ||
|
|
||
| // CHECK: %[[TOFP:.*]] = arith.sitofp %[[ARG1]] : tensor<8xi32> to tensor<8xf32> | ||
| // CHECK: %[[LOGA:.*]] = math.log %[[ARG0]] : tensor<8xf32> | ||
| // CHECK: %[[MUL:.*]] = arith.mulf %[[TOFP]], %[[LOGA]] : tensor<8xf32> | ||
| // CHECK: %[[EXP:.*]] = math.exp %[[MUL]] : tensor<8xf32> | ||
| // CHECK: return %[[EXP]] : tensor<8xf32> | ||
| // ----- | ||
|
|
||
| // CHECK-LABEL: func.func @math_fpowi_to_powf_scalar | ||
|
|
@@ -630,25 +601,11 @@ func.func @math_fpowi_to_powf_scalar(%0 : f32, %1: i64) -> f32 { | |
| return %2 : f32 | ||
| } | ||
| // CHECK-SAME: (%[[ARG0:.*]]: f32, %[[ARG1:.*]]: i64) -> f32 { | ||
| // CHECK-DAG: %[[CSTNEG1:.*]] = arith.constant -1.000000e+00 : f32 | ||
| // CHECK-DAG: %[[CST2:.*]] = arith.constant 2.000000e+00 : f32 | ||
| // CHECK-DAG: %[[CST0:.*]] = arith.constant 0.000000e+00 : f32 | ||
| // CHECK-DAG: %[[CST1:.+]] = arith.constant 1.000000e+00 : f32 | ||
| // CHECK: %[[TOFP:.*]] = arith.sitofp %[[ARG1]] : i64 to f32 | ||
| // CHECK: %[[SQ:.*]] = arith.mulf %[[ARG0]], %[[ARG0]] : f32 | ||
| // CHECK: %[[DIV:.*]] = arith.divf %[[TOFP]], %[[CST2]] : f32 | ||
| // CHECK: %[[LG:.*]] = math.log %[[SQ]] : f32 | ||
| // CHECK: %[[MUL:.*]] = arith.mulf %[[DIV]], %[[LG]] : f32 | ||
| // CHECK: %[[LOGA:.*]] = math.log %[[ARG0]] : f32 | ||
| // CHECK: %[[MUL:.*]] = arith.mulf %[[TOFP]], %[[LOGA]] : f32 | ||
| // CHECK: %[[EXP:.*]] = math.exp %[[MUL]] : f32 | ||
| // CHECK: %[[MUL1:.*]] = arith.mulf %[[EXP]], %[[CSTNEG1]] : f32 | ||
| // CHECK: %[[REM:.*]] = arith.remf %[[TOFP]], %[[CST2]] : f32 | ||
| // CHECK: %[[CMPF:.*]] = arith.cmpf olt, %[[ARG0]], %[[CST0]] : f32 | ||
| // CHECK: %[[CMPF1:.*]] = arith.cmpf one, %[[REM]], %[[CST0]] : f32 | ||
| // CHECK: %[[AND:.*]] = arith.andi %[[CMPF1]], %[[CMPF]] : i1 | ||
| // CHECK: %[[CMPZERO:.*]] = arith.cmpf oeq, %[[TOFP]], %[[CST0]] | ||
| // CHECK: %[[SEL:.*]] = arith.select %[[AND]], %[[MUL1]], %[[EXP]] : f32 | ||
| // CHECK: %[[SEL1:.+]] = arith.select %[[CMPZERO]], %[[CST1]], %[[SEL]] | ||
| // CHECK: return %[[SEL1]] : f32 | ||
| // CHECK: return %[[EXP]] : f32 | ||
|
|
||
| // ----- | ||
|
|
||
|
|
||
ita9naiwa marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Can you rename
opTypetotypeA, andbaseTypetotypeB?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.
Done!