Skip to content

Commit

Permalink
[mlir][linalg] Fix numerical issue with softmax (#96090)
Browse files Browse the repository at this point in the history
  • Loading branch information
pashu123 authored Jun 20, 2024
1 parent ad79a14 commit fa06668
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2714,8 +2714,8 @@ FailureOr<SmallVector<Value>> SoftmaxOp::decomposeOperation(OpBuilder &b) {
Value neutralForMaxFInit =
b.create<linalg::FillOp>(loc, Value{neutralForMaxF}, outputReduce)
.result();
Value max = reduce<arith::MaximumFOp>(b, loc, input, neutralForMaxFInit,
reductionDim);
Value max =
reduce<arith::MaxNumFOp>(b, loc, input, neutralForMaxFInit, reductionDim);

// Step 2: Subtract max from input and exponentiate.
Value numerator = buildSubAndExpOp(b, loc, input, max, output, reductionDim);
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/Linalg/transform-op-decompose.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func.func @softmax(%arg0: tensor<2x16x32xf32>, %dst: tensor<2x16x32xf32>) -> ten
// CHECK: %[[D3:.+]] = linalg.generic {indexing_maps = [#[[$MAP]], #[[$MAP1]]], iterator_types = ["parallel",
// CHECK-SAME: "parallel", "reduction"]} ins(%[[ARG0]] : tensor<2x16x32xf32>) outs(%[[D2]] : tensor<2x16xf32>) {
// CHECK: ^bb0(%[[IN:.+]]: f32, %[[OUT:.+]]: f32):
// CHECK: %[[D8:.+]] = arith.maximumf %[[IN]], %[[OUT]] : f32
// CHECK: %[[D8:.+]] = arith.maxnumf %[[IN]], %[[OUT]] : f32
// CHECK: linalg.yield %[[D8]] : f32
// CHECK: } -> tensor<2x16xf32>
// CHECK: %[[D4:.+]] = linalg.generic {indexing_maps = [#[[$MAP]], #[[$MAP1]], #[[$MAP]]], iterator_types =
Expand Down

0 comments on commit fa06668

Please sign in to comment.