-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for stablehlo.reduce op for logical or operator (#2160)
TTNN does not support reduction for logical or operator. So stablehlo.reduce for stablehlo.or operator is decomposed into reduction sum op along give dimension. If ttnn.sum output is zero then reduce_or output is false; otherwise the output is true. ### Ticket #1143 ### Problem description Add support for reduction operation for logical or operator ### What's changed - `ttir.reduce_or` op is added in TTIR dialect - `ttir.reduce_or` op is decomposed/converted to `ttir.sum` op as tt-metal does not support reduction or operation. - Stablehlo conversion for reduce or op. ### Checklist - [X] New tests provide coverage for changes
- Loading branch information
1 parent
3af5027
commit b020a93
Showing
10 changed files
with
259 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
test/ttmlir/Conversion/StableHLOToTTIR/reduction/reduce_or_op.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// REQUIRES: stablehlo | ||
// RUN: ttmlir-opt --stablehlo-to-ttir-pipeline %s | FileCheck %s | ||
module @jit_reduce_or attributes {} { | ||
func.func public @test_reduce_or_4to3dim(%arg0: tensor<128x10x32x4xi1>, %cst_0: tensor<i1>) -> tensor<128x10x32xi1> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_4to3dim | ||
// CHECK: tensor.empty | ||
// CHECK: "ttir.reduce_or" | ||
// CHECK-SAME: dim_arg = [3 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10x32x4xbf16> | ||
// CHECK-SAME: -> tensor<128x10x32xbf16> | ||
%0 = stablehlo.reduce(%arg0 init: %cst_0) applies stablehlo.or across dimensions = [3] : (tensor<128x10x32x4xi1>, tensor<i1>) -> tensor<128x10x32xi1> | ||
return %0 : tensor<128x10x32xi1> | ||
} | ||
|
||
func.func public @test_reduce_or_3to2dim(%arg0: tensor<128x10x4xi1>, %cst_0: tensor<i1>) -> tensor<128x4xi1> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_3to2dim | ||
// CHECK: tensor.empty | ||
// CHECK: "ttir.reduce_or" | ||
// CHECK-SAME: dim_arg = [1 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10x4xbf16> | ||
// CHECK-SAME: -> tensor<128x4xbf16> | ||
%0 = stablehlo.reduce(%arg0 init: %cst_0) applies stablehlo.or across dimensions = [1] : (tensor<128x10x4xi1>, tensor<i1>) -> tensor<128x4xi1> | ||
return %0 : tensor<128x4xi1> | ||
} | ||
|
||
func.func public @test_reduce_or_2to1dim(%arg0: tensor<128x10xi1>, %cst_0: tensor<i1>) -> tensor<10xi1> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_2to1dim | ||
// CHECK: tensor.empty | ||
// CHECK: "ttir.reduce_or" | ||
// CHECK-SAME: dim_arg = [0 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10xbf16> | ||
// CHECK-SAME: -> tensor<10xbf16> | ||
%0 = stablehlo.reduce(%arg0 init: %cst_0) applies stablehlo.or across dimensions = [0] : (tensor<128x10xi1>, tensor<i1>) -> tensor<10xi1> | ||
return %0 : tensor<10xi1> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// RUN: ttmlir-opt --ttir-to-ttir-decomposition %s | FileCheck %s | ||
module attributes {} { | ||
func.func public @test_reduce_or_4to3dim(%arg0: tensor<128x10x32x4xbf16>, %arg1: tensor<1xbf16>) -> tensor<128x10x32xbf16> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_4to3dim | ||
// CHECK: %[[SUM:[0-9]+]] = "ttir.sum" | ||
// CHECK-SAME: dim_arg = [3 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10x32x4xbf16> | ||
// CHECK-SAME: -> tensor<128x10x32xbf16> | ||
// CHECK: return %[[SUM]] | ||
%0 = tensor.empty() : tensor<128x10x32xbf16> | ||
%1 = "ttir.reduce_or"(%arg0, %0) <{dim_arg = [3 : i32], keep_dim = false}> : (tensor<128x10x32x4xbf16>, tensor<128x10x32xbf16>) -> tensor<128x10x32xbf16> | ||
return %1 : tensor<128x10x32xbf16> | ||
} | ||
|
||
func.func public @test_reduce_or_3to2dim(%arg0: tensor<128x10x4xbf16>, %arg1: tensor<1xbf16>) -> tensor<128x4xbf16> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_3to2dim | ||
// CHECK: %[[SUM:[0-9]+]] = "ttir.sum" | ||
// CHECK-SAME: dim_arg = [1 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10x4xbf16> | ||
// CHECK-SAME: -> tensor<128x4xbf16> | ||
// CHECK: return %[[SUM]] | ||
%0 = tensor.empty() : tensor<128x4xbf16> | ||
%1 = "ttir.reduce_or"(%arg0, %0) <{dim_arg = [1 : i32], keep_dim = false}> : (tensor<128x10x4xbf16>, tensor<128x4xbf16>) -> tensor<128x4xbf16> | ||
return %1 : tensor<128x4xbf16> | ||
} | ||
|
||
func.func public @test_reduce_or_2to1dim(%arg0: tensor<128x10xbf16>, %arg1: tensor<1xbf16>) -> tensor<10xbf16> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_2to1dim | ||
// CHECK: %[[SUM:[0-9]+]] = "ttir.sum" | ||
// CHECK-SAME: dim_arg = [0 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10xbf16> | ||
// CHECK-SAME: -> tensor<10xbf16> | ||
// CHECK: return %[[SUM]] | ||
%0 = tensor.empty() : tensor<10xbf16> | ||
%1 = "ttir.reduce_or"(%arg0, %0) <{dim_arg = [0 : i32], keep_dim = false}> : (tensor<128x10xbf16>, tensor<10xbf16>) -> tensor<10xbf16> | ||
return %1 : tensor<10xbf16> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// RUN: ttmlir-opt --ttir-to-ttnn-backend-pipeline %s | FileCheck %s | ||
|
||
module attributes {} { | ||
func.func public @test_reduce_or_4to3dim(%arg0: tensor<128x10x32x4xbf16>, %arg1: tensor<1xbf16>) -> tensor<128x10x32xbf16> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_4to3dim | ||
// CHECK: %[[SUM:[0-9]+]] = "ttnn.sum" | ||
// CHECK-SAME: dim_arg = [3 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10x32x4xbf16, | ||
// CHECK-SAME: -> tensor<128x10x32xbf16, | ||
%0 = tensor.empty() : tensor<128x10x32xbf16> | ||
%1 = "ttir.reduce_or"(%arg0, %0) <{dim_arg = [3 : i32], keep_dim = false}> : (tensor<128x10x32x4xbf16>, tensor<128x10x32xbf16>) -> tensor<128x10x32xbf16> | ||
return %1 : tensor<128x10x32xbf16> | ||
} | ||
|
||
func.func public @test_reduce_or_3to2dim(%arg0: tensor<128x10x4xbf16>, %arg1: tensor<1xbf16>) -> tensor<128x4xbf16> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_3to2dim | ||
// CHECK: %[[SUM:[0-9]+]] = "ttnn.sum" | ||
// CHECK-SAME: dim_arg = [1 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10x4xbf16, | ||
// CHECK-SAME: -> tensor<128x4xbf16, | ||
%0 = tensor.empty() : tensor<128x4xbf16> | ||
%1 = "ttir.reduce_or"(%arg0, %0) <{dim_arg = [1 : i32], keep_dim = false}> : (tensor<128x10x4xbf16>, tensor<128x4xbf16>) -> tensor<128x4xbf16> | ||
return %1 : tensor<128x4xbf16> | ||
} | ||
|
||
func.func public @test_reduce_or_2to1dim(%arg0: tensor<128x10xbf16>, %arg1: tensor<1xbf16>) -> tensor<10xbf16> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_2to1dim | ||
// CHECK: %[[SUM:[0-9]+]] = "ttnn.sum" | ||
// CHECK-SAME: dim_arg = [0 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10xbf16, | ||
// CHECK-SAME: -> tensor<10xbf16, | ||
%0 = tensor.empty() : tensor<10xbf16> | ||
%1 = "ttir.reduce_or"(%arg0, %0) <{dim_arg = [0 : i32], keep_dim = false}> : (tensor<128x10xbf16>, tensor<10xbf16>) -> tensor<10xbf16> | ||
return %1 : tensor<10xbf16> | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
test/ttmlir/Silicon/StableHLO/n150/reduction/reduce_or_op.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// REQUIRES: stablehlo | ||
// RUN: rm -rf %t.ttnn | ||
// RUN: rm -rf %t.mlir | ||
// RUN: ttmlir-opt --stablehlo-to-ttir-pipeline \ | ||
// RUN: --ttir-to-ttnn-backend-pipeline="system-desc-path=%system_desc_path%" %s > %t.mlir | ||
// RUN: ttmlir-translate --ttnn-to-flatbuffer %t.mlir > %t.ttnn | ||
// RUN: FileCheck --input-file=%t.mlir %s | ||
|
||
module @jit_reduce_add attributes {} { | ||
func.func public @test_reduce_or_4to3dim(%arg0: tensor<128x10x32x4xi1>, %cst_0: tensor<i1>) -> tensor<128x10x32xi1> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_4to3dim | ||
// CHECK: "ttnn.sum" | ||
// CHECK-SAME: dim_arg = [3 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: -> tensor<128x10x32xbf16, | ||
%0 = stablehlo.reduce(%arg0 init: %cst_0) applies stablehlo.or across dimensions = [3] : (tensor<128x10x32x4xi1>, tensor<i1>) -> tensor<128x10x32xi1> | ||
return %0 : tensor<128x10x32xi1> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// RUN: rm -rf %t.ttnn | ||
// RUN: rm -rf %t.mlir | ||
// RUN: ttmlir-opt --ttir-to-ttnn-backend-pipeline="system-desc-path=%system_desc_path%" %s > %t.mlir | ||
// RUN: FileCheck %s --input-file=%t.mlir | ||
// RUN: ttmlir-translate --ttnn-to-flatbuffer %t.mlir > %t.ttnn | ||
|
||
module attributes {} { | ||
func.func public @test_reduce_or_4to2dim(%arg0: tensor<128x10x32x4xbf16>, %arg1: tensor<1xbf16>) -> tensor<128x32xbf16> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_4to2dim | ||
// CHECK: %[[SUM:[0-9]+]] = "ttnn.sum" | ||
// CHECK-SAME: dim_arg = [1 : i32, 3 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10x32x4xbf16, | ||
// CHECK-SAME: -> tensor<128x32xbf16, | ||
%0 = tensor.empty() : tensor<128x32xbf16> | ||
%1 = "ttir.reduce_or"(%arg0, %0) <{dim_arg = [1: i32, 3 : i32], keep_dim = false}> : (tensor<128x10x32x4xbf16>, tensor<128x32xbf16>) -> tensor<128x32xbf16> | ||
return %1 : tensor<128x32xbf16> | ||
} | ||
|
||
func.func public @test_reduce_or_3to2dim(%arg0: tensor<128x10x4xbf16>, %arg1: tensor<1xbf16>) -> tensor<128x4xbf16> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_3to2dim | ||
// CHECK: %[[SUM:[0-9]+]] = "ttnn.sum" | ||
// CHECK-SAME: dim_arg = [1 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10x4xbf16, | ||
// CHECK-SAME: -> tensor<128x4xbf16, | ||
%0 = tensor.empty() : tensor<128x4xbf16> | ||
%1 = "ttir.reduce_or"(%arg0, %0) <{dim_arg = [1 : i32], keep_dim = false}> : (tensor<128x10x4xbf16>, tensor<128x4xbf16>) -> tensor<128x4xbf16> | ||
return %1 : tensor<128x4xbf16> | ||
} | ||
|
||
func.func public @test_reduce_or_2to1dim(%arg0: tensor<128x10xbf16>, %arg1: tensor<1xbf16>) -> tensor<10xbf16> { | ||
// CHECK-LABEL: func.func public @test_reduce_or_2to1dim | ||
// CHECK: %[[SUM:[0-9]+]] = "ttnn.sum" | ||
// CHECK-SAME: dim_arg = [0 : i32] | ||
// CHECK-SAME: keep_dim = false | ||
// CHECK-SAME: tensor<128x10xbf16, | ||
// CHECK-SAME: -> tensor<10xbf16, | ||
%0 = tensor.empty() : tensor<10xbf16> | ||
%1 = "ttir.reduce_or"(%arg0, %0) <{dim_arg = [0 : i32], keep_dim = false}> : (tensor<128x10xbf16>, tensor<10xbf16>) -> tensor<10xbf16> | ||
return %1 : tensor<10xbf16> | ||
} | ||
} |