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

Added Atan2Op support #1056

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions include/ttmlir/Dialect/TTIR/IR/TTIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,22 @@ def TTIR_MaximumOp : TTIR_ElementwiseBinaryOp<"maximum"> {
}];
}

def TTIR_Atan2Op : TTIR_ElementwiseBinaryOp<"atan2"> {
let summary = "Eltwise atan2 OP.";
let description = [{
Performs element-wise atan2 operation on lhs and rhs tensor and produces a result
tensor.

Example:
```
// %lhs: [0.0, 1.0, -1.0]
// %rhs: [0.0, 0.0, 0.0]
%result = "ttir.atan2"(%lhs, %rhs) : (tensor<3xf64>, tensor<3xf64>) -> tensor<3xf64>
// %result: [0.0, 1.57079637, -1.57079637] // [0.0, pi/2, -pi/2]
```
}];
}

class TTIR_ReductionOp<string mnemonic, list<Trait> traits = []> :
TTIR_DPSOp<mnemonic, !listconcat(traits, [TTIR_GenericRegionOpInterface])> {

Expand Down
16 changes: 16 additions & 0 deletions include/ttmlir/Dialect/TTNN/IR/TTNNOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,22 @@ def TTNN_MaximumOp : TTNN_ElementwiseBinaryOp<"maximum"> {
}];
}

def TTNN_Atan2Op : TTNN_ElementwiseBinaryOp<"atan2"> {
let summary = "Eltwise atan2 OP.";
let description = [{
Performs element-wise atan2 operation on lhs and rhs tensor and produces a result
tensor.

Example:
```
// %lhs: [0.0, 1.0, -1.0]
// %rhs: [0.0, 0.0, 0.0]
%result = "ttnn.atan2"(%lhs, %rhs) : (tensor<3xf64>, tensor<3xf64>) -> tensor<3xf64>
// %result: [0.0, 1.57079637, -1.57079637] // [0.0, pi/2, -pi/2]
```
}];
}

class TTNN_ReductionOp<string mnemonic, list<Trait> traits = []> : TTNN_Op<mnemonic, traits> {
let summary = "Reduction op.";
let description = [{
Expand Down
1 change: 1 addition & 0 deletions include/ttmlir/Target/TTNN/program.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ enum EltwiseOpType: uint32 {
LogicalAnd = 20,
LogicalOr = 21,
LogicalNot = 22,
Atan2 = 23,
}

table EltwiseOp {
Expand Down
2 changes: 2 additions & 0 deletions lib/Conversion/StableHLOToTTIR/StableHLOToTTIRPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,8 @@ void addElementwiseBinaryOpsConversionPatterns(MLIRContext *ctx,
mlir::stablehlo::DivOp, mlir::tt::ttir::DivOp>>(typeConverter, ctx);
patterns.add<StableHLOToTTIROpDefaultConversionPattern<
mlir::stablehlo::MaxOp, mlir::tt::ttir::MaximumOp>>(typeConverter, ctx);
patterns.add<StableHLOToTTIROpDefaultConversionPattern<
mlir::stablehlo::Atan2Op, mlir::tt::ttir::Atan2Op>>(typeConverter, ctx);
}

void addReduceOpsConversionPatterns(MLIRContext *ctx,
Expand Down
1 change: 1 addition & 0 deletions lib/Conversion/TTIRToTTNN/TTIRToTTNN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ void populateTTIRToTTNNPatterns(MLIRContext *ctx, RewritePatternSet &patterns,
ElementwiseOpConversionPattern<ttir::ReciprocalOp, ttnn::ReciprocalOp>,
ElementwiseOpConversionPattern<ttir::ExpOp, ttnn::ExpOp>,
ElementwiseOpConversionPattern<ttir::DivOp, ttnn::DivOp>,
ElementwiseOpConversionPattern<ttir::Atan2Op, ttnn::Atan2Op>,
ReductionOpConversionPattern<ttir::SumOp, ttnn::SumOp>,
ReductionOpConversionPattern<ttir::MeanOp, ttnn::MeanOp>,
ReductionOpConversionPattern<ttir::MaxOp, ttnn::MaxOp>,
Expand Down
3 changes: 2 additions & 1 deletion lib/Conversion/TTNNToEmitC/TTNNToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,8 @@ void populateTTNNToEmitCPatterns(mlir::MLIRContext *ctx,
DefaultOpConversionPattern<ttnn::LessEqualOp>,
DefaultOpConversionPattern<ttnn::LessThanOp>,
DefaultOpConversionPattern<ttnn::MaximumOp>,
DefaultOpConversionPattern<ttnn::DivOp>>(typeConverter, ctx);
DefaultOpConversionPattern<ttnn::DivOp>,
DefaultOpConversionPattern<ttnn::Atan2Op>>(typeConverter, ctx);

// Tensor manipulation ops
//
Expand Down
5 changes: 5 additions & 0 deletions lib/Target/TTNN/TTNNToFlatbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ createEltwiseOp(FlatbufferObjectCache &cache, EltwiseOp op) {
type = ::tt::target::ttnn::EltwiseOpType::Typecast;
} else if constexpr (std::is_same_v<EltwiseOp, ExpOp>) {
type = ::tt::target::ttnn::EltwiseOpType::Exp;
} else if constexpr (std::is_same_v<EltwiseOp, Atan2Op>) {
type = ::tt::target::ttnn::EltwiseOpType::Atan2;
} else {
llvm_unreachable("unhandled EltwiseOp");
}
Expand Down Expand Up @@ -546,6 +548,9 @@ emitTTNNOperation(FlatbufferObjectCache &cache, Operation *op,
if (auto divOp = dyn_cast<DivOp>(op); divOp) {
return createOperation(cache, createEltwiseOp(cache, divOp), debugString);
}
if (auto atan2Op = dyn_cast<Atan2Op>(op); atan2Op) {
return createOperation(cache, createEltwiseOp(cache, atan2Op), debugString);
}
if (auto matmulOp = dyn_cast<MatmulOp>(op); matmulOp) {
return createOperation(cache, createOp(cache, matmulOp), debugString);
}
Expand Down
5 changes: 5 additions & 0 deletions runtime/lib/ttnn/operations/eltwise/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "tt/runtime/detail/logger.h"
#include "tt/runtime/detail/ttnn.h"
#include "tt/runtime/ttnn/operations/utils.h"
#include "ttnn/operations/eltwise/binary/binary_composite.hpp"

namespace tt::runtime::ttnn::operations::binary {

Expand Down Expand Up @@ -123,6 +124,10 @@ void run(const ::tt::target::ttnn::EltwiseOp *op, ProgramContext &context) {
runEltwiseBinaryCompositeOP(op, tensorPool, ::ttnn::maximum);
break;
}
case ::tt::target::ttnn::EltwiseOpType::Atan2: {
runEltwiseBinaryCompositeOP(op, tensorPool, ::ttnn::atan2);
break;
}
default:
throw std::invalid_argument("Unsupported Eltwise Binary operation");
}
Expand Down
11 changes: 11 additions & 0 deletions test/ttmlir/Conversion/StableHLOToTTIR/atan2_op.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// REQUIRES: stablehlo
// RUN: ttmlir-opt --stablehlo-to-ttir-pipeline %s | FileCheck %s
#any_device = #tt.operand_constraint<dram|l1|scalar|tile|any_device|any_device_tile>
module @jit_eltwise_atan2 attributes {} {
func.func public @test_atan2(%arg0: tensor<32x32xf32>, %arg1: tensor<32x32xf32>) -> tensor<32x32xf32> {
%0 = stablehlo.atan2 %arg0, %arg1 : tensor<32x32xf32>
// CHECK: %[[C:.*]] = tensor.empty[[C:.*]]
// CHECK: %[[C:.*]] = "ttir.atan2"[[C:.*]]
return %0 : tensor<32x32xf32>
}
}
11 changes: 11 additions & 0 deletions test/ttmlir/Dialect/TTNN/eltwise/binary/atan2/simple_atan2.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: ttmlir-opt --ttir-to-ttnn-backend-pipeline %s | FileCheck %s
#any_device = #tt.operand_constraint<dram|l1|scalar|tile|any_device|any_device_tile>
module attributes {} {
func.func @atan2(%arg0: tensor<32x32xf32>, %arg1: tensor<32x32xf32>) -> tensor<32x32xf32> {
// CHECK: %[[C:.*]] = "ttnn.empty"[[C:.*]]
%0 = tensor.empty() : tensor<32x32xf32>
// CHECK: %[[C:.*]] = "ttnn.atan2"[[C:.*]]
%1 = "ttir.atan2"(%arg0, %arg1, %0) <{operandSegmentSizes = array<i32: 2, 1>, operand_constraints = [#any_device, #any_device, #any_device]}> : (tensor<32x32xf32>, tensor<32x32xf32>, tensor<32x32xf32>) -> tensor<32x32xf32>
return %1 : tensor<32x32xf32>
}
}
8 changes: 8 additions & 0 deletions test/ttmlir/Silicon/TTNN/simple_eltwise.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,11 @@ func.func @maximum(%arg0: tensor<64x128xf32>, %arg1: tensor<64x128xf32>) -> tens
%1 = "ttir.maximum"(%arg0, %arg1, %0) <{operandSegmentSizes = array<i32: 2, 1>, operand_constraints = [#any_device, #any_device, #any_device]}> : (tensor<64x128xf32>, tensor<64x128xf32>, tensor<64x128xf32>) -> tensor<64x128xf32>
return %1 : tensor<64x128xf32>
}

func.func @atan2(%arg0: tensor<32x32xf32>, %arg1: tensor<32x32xf32>) -> tensor<32x32xf32> {
// CHECK: %[[C:.*]] = "ttnn.empty"[[C:.*]]
%0 = tensor.empty() : tensor<32x32xf32>
// CHECK: %[[C:.*]] = "ttnn.atan2"[[C:.*]]
%1 = "ttir.atan2"(%arg0, %arg1, %0) <{operandSegmentSizes = array<i32: 2, 1>, operand_constraints = [#any_device, #any_device, #any_device]}> : (tensor<32x32xf32>, tensor<32x32xf32>, tensor<32x32xf32>) -> tensor<32x32xf32>
return %1 : tensor<32x32xf32>
}
2 changes: 1 addition & 1 deletion third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include(ExternalProject)

set(TT_METAL_VERSION "047cdd97fa26b229208ce2f6f8b149c3df277be0")
set(TT_METAL_VERSION "fccd6929c7a065b9145af92b1a429b2e8f63907b")
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if this was intentional, or just got picked up during git add. Leaving comment as a reminder to remove before merging.


if ("$ENV{ARCH_NAME}" STREQUAL "grayskull")
set(ARCH_NAME "grayskull")
Expand Down
Loading