Skip to content
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

Add TOSA to TTIR conversions for some simple ops #1445

Merged
merged 11 commits into from
Dec 3, 2024
22 changes: 21 additions & 1 deletion lib/Conversion/TosaToTTIR/TosaToTTIRPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ void addElementwiseUnaryOpsConversionPatterns(MLIRContext *ctx,
patterns.add<TosaToTTIRDefaultDPSOpConversionPattern<tosa::NegateOp,
mlir::tt::ttir::NegOp>>(
typeConverter, ctx);
patterns.add<TosaToTTIRDefaultDPSOpConversionPattern<tosa::SinOp,
mlir::tt::ttir::SinOp>>(
typeConverter, ctx);
patterns.add<TosaToTTIRDefaultDPSOpConversionPattern<
tosa::SigmoidOp, mlir::tt::ttir::SigmoidOp>>(typeConverter, ctx);
patterns.add<TosaToTTIRDefaultDPSOpConversionPattern<
tosa::ReciprocalOp, mlir::tt::ttir::ReciprocalOp>>(typeConverter, ctx);
patterns.add<TosaToTTIRDefaultDPSOpConversionPattern<
tosa::RsqrtOp, mlir::tt::ttir::RsqrtOp>>(typeConverter, ctx);
}

void addElementwiseBinaryOpsConversionPatterns(MLIRContext *ctx,
Expand All @@ -102,6 +111,10 @@ void addElementwiseBinaryOpsConversionPatterns(MLIRContext *ctx,
patterns.add<TosaToTTIRMultiplyOpConversionPattern>(typeConverter, ctx);
patterns.add<TosaToTTIRDefaultDPSOpConversionPattern<
tosa::SubOp, mlir::tt::ttir::SubtractOp>>(typeConverter, ctx);
patterns.add<TosaToTTIRDefaultDPSOpConversionPattern<
tosa::MaximumOp, mlir::tt::ttir::MaximumOp>>(typeConverter, ctx);
patterns.add<TosaToTTIRDefaultDPSOpConversionPattern<
tosa::MinimumOp, mlir::tt::ttir::MinimumOp>>(typeConverter, ctx);
}

void addCompareOpsConversionPatterns(MLIRContext *ctx,
Expand All @@ -112,14 +125,21 @@ void addCompareOpsConversionPatterns(MLIRContext *ctx,
ctx);
}

} // namespace
void addElementwiseTernaryOpsConversionPatterns(MLIRContext *ctx,
RewritePatternSet &patterns,
TypeConverter &typeConverter) {
patterns.add<TosaToTTIRDefaultDPSOpConversionPattern<
tosa::SelectOp, mlir::tt::ttir::WhereOp>>(typeConverter, ctx);
}
} // namespace
Copy link
Contributor

Choose a reason for hiding this comment

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

Extra spacing here


namespace mlir::tt {

void populateTosaToTTIRPatterns(MLIRContext *ctx, RewritePatternSet &patterns,
TypeConverter &typeConverter) {
addElementwiseUnaryOpsConversionPatterns(ctx, patterns, typeConverter);
addElementwiseBinaryOpsConversionPatterns(ctx, patterns, typeConverter);
addElementwiseTernaryOpsConversionPatterns(ctx, patterns, typeConverter);
addCompareOpsConversionPatterns(ctx, patterns, typeConverter);
}

Expand Down
9 changes: 9 additions & 0 deletions test/ttmlir/Conversion/TosaToTTIR/maximum_op.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: ttmlir-opt --convert-tosa-to-ttir %s | FileCheck %s
module attributes {} {
func.func @test_maximum(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
%0 = tosa.maximum %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
// CHECK: %[[C:.*]] = tensor.empty[[C:.*]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add more detailed tests (with detailed regexes), for some examples, take a look at test/ttmlir/Conversion/StableHLOToTTIR/exponential_minus_one_op.mlir

Similar comments for all tests, I'm leaving this on just this one for brevity.

// CHECK: %[[C:.*]] = "ttir.maximum"[[C:.*]]
return %0 : tensor<13x21x3xf32>
}
}
9 changes: 9 additions & 0 deletions test/ttmlir/Conversion/TosaToTTIR/minimum_op.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: ttmlir-opt --convert-tosa-to-ttir %s | FileCheck %s
module attributes {} {
func.func @test_minimum(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
%0 = tosa.minimum %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
// CHECK: %[[C:.*]] = tensor.empty[[C:.*]]
// CHECK: %[[C:.*]] = "ttir.minimum"[[C:.*]]
return %0 : tensor<13x21x3xf32>
}
}
9 changes: 9 additions & 0 deletions test/ttmlir/Conversion/TosaToTTIR/negate_op.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: ttmlir-opt --convert-tosa-to-ttir %s | FileCheck %s
module attributes {} {
func.func @test_negate(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
%0 = tosa.negate %arg0 : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
// CHECK: %[[C:.*]] = tensor.empty[[C:.*]]
// CHECK: %[[C:.*]] = "ttir.neg"[[C:.*]]
return %0 : tensor<13x21x3xf32>
}
}
9 changes: 9 additions & 0 deletions test/ttmlir/Conversion/TosaToTTIR/reciprocal_op.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: ttmlir-opt --convert-tosa-to-ttir %s | FileCheck %s
module attributes {} {
func.func @test_reciprocal(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
%0 = tosa.reciprocal %arg0 : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
// CHECK: %[[C:.*]] = tensor.empty[[C:.*]]
// CHECK: %[[C:.*]] = "ttir.reciprocal"[[C:.*]]
return %0 : tensor<13x21x3xf32>
}
}
9 changes: 9 additions & 0 deletions test/ttmlir/Conversion/TosaToTTIR/rsqrt_op.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: ttmlir-opt --convert-tosa-to-ttir %s | FileCheck %s
module attributes {} {
func.func @test_rsqrt(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
%0 = tosa.rsqrt %arg0 : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
// CHECK: %[[C:.*]] = tensor.empty[[C:.*]]
// CHECK: %[[C:.*]] = "ttir.rsqrt"[[C:.*]]
return %0 : tensor<13x21x3xf32>
}
}
9 changes: 9 additions & 0 deletions test/ttmlir/Conversion/TosaToTTIR/select_op.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: ttmlir-opt --convert-tosa-to-ttir %s | FileCheck %s
module attributes {} {
func.func @test_select(%arg0: tensor<32x128xi1>, %arg1: tensor<32x128xf32>, %arg2: tensor<32x128xf32>) -> tensor<32x128xf32> {
%0 = tosa.select %arg0, %arg1, %arg2 : (tensor<32x128xi1>, tensor<32x128xf32>, tensor<32x128xf32>) -> tensor<32x128xf32>
// CHECK: %[[C:.*]] = tensor.empty[[C:.*]]
// CHECK: %[[C:.*]] = "ttir.where"[[C:.*]]
return %0 : tensor<32x128xf32>
}
}
9 changes: 9 additions & 0 deletions test/ttmlir/Conversion/TosaToTTIR/sigmoid_op.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: ttmlir-opt --convert-tosa-to-ttir %s | FileCheck %s
module attributes {} {
func.func @test_sigmoid(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
%0 = tosa.sigmoid %arg0 : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
// CHECK: %[[C:.*]] = tensor.empty[[C:.*]]
// CHECK: %[[C:.*]] = "ttir.sigmoid"[[C:.*]]
return %0 : tensor<13x21x3xf32>
}
}
9 changes: 9 additions & 0 deletions test/ttmlir/Conversion/TosaToTTIR/sin_op.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: ttmlir-opt --convert-tosa-to-ttir %s | FileCheck %s
module attributes {} {
func.func @test_sin(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
%0 = tosa.sin %arg0 : (tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
// CHECK: %[[C:.*]] = tensor.empty[[C:.*]]
// CHECK: %[[C:.*]] = "ttir.sin"[[C:.*]]
return %0 : tensor<13x21x3xf32>
}
}
9 changes: 9 additions & 0 deletions test/ttmlir/Conversion/TosaToTTIR/subtract_op.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: ttmlir-opt --convert-tosa-to-ttir %s | FileCheck %s
module attributes {} {
func.func @test_sub(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
%0 = tosa.sub %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<13x21x3xf32>) -> tensor<13x21x3xf32>
// CHECK: %[[C:.*]] = tensor.empty[[C:.*]]
// CHECK: %[[C:.*]] = "ttir.subtract"[[C:.*]]
return %0 : tensor<13x21x3xf32>
}
}
Loading