diff --git a/ttnn/CMakeLists.txt b/ttnn/CMakeLists.txt index 1b75ee63685..6fc29565b99 100644 --- a/ttnn/CMakeLists.txt +++ b/ttnn/CMakeLists.txt @@ -34,6 +34,7 @@ set(TTNN_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ttnn/operations/upsample/upsample_op.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ttnn/operations/upsample/device/upsample_op_multi_core.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ttnn/operations/upsample/device/upsample_op_single_core.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ttnn/operations/eltwise/unary/unary.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ttnn/operations/eltwise/unary/device/unary_op.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ttnn/operations/eltwise/unary_backward/device/unary_backward_op.cpp ${CMAKE_CURRENT_SOURCE_DIR}/cpp/ttnn/operations/eltwise/complex_unary/device/complex_unary_op.cpp diff --git a/ttnn/cpp/ttnn/deprecated/tt_dnn/op_library/reduce/reduce_op.cpp b/ttnn/cpp/ttnn/deprecated/tt_dnn/op_library/reduce/reduce_op.cpp index 3f1a0bf359c..426a286f1a6 100644 --- a/ttnn/cpp/ttnn/deprecated/tt_dnn/op_library/reduce/reduce_op.cpp +++ b/ttnn/cpp/ttnn/deprecated/tt_dnn/op_library/reduce/reduce_op.cpp @@ -10,6 +10,8 @@ #include "ttnn/deprecated/tt_dnn/op_library/reshape/reshape_op.hpp" #include "ttnn/run_operation.hpp" +#include "ttnn/operations/core/core.hpp" + #include "tt_metal/host_api.hpp" #include "tt_metal/common/constants.hpp" #include "ttnn/operations/eltwise/unary/unary.hpp" diff --git a/ttnn/cpp/ttnn/operations/eltwise/unary/common/unary_op_types.hpp b/ttnn/cpp/ttnn/operations/eltwise/unary/common/unary_op_types.hpp new file mode 100644 index 00000000000..20fb9d78a41 --- /dev/null +++ b/ttnn/cpp/ttnn/operations/eltwise/unary/common/unary_op_types.hpp @@ -0,0 +1,85 @@ +// SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. +// +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +namespace ttnn::operations::unary { + +// These operations have a corresponding LLK available +enum class UnaryOpType { + EXP, + RECIP, + GELU, + RELU, + SQRT, + SIGMOID, + LOG, + TANH, + LOG2, + LOG10, + SIN, + COS, + ABS, + SIGN, + SQUARE, + EQZ, + NEZ, + GTZ, + LTZ, + GEZ, + LEZ, + RELU_MAX, + RELU_MIN, + POWER, + LEAKY_RELU, + ELU, + EXP2, + HEAVISIDE, + EXPM1, + SIGNBIT, + ASIN, + ACOS, + RSQRT, + RELU6, + ATAN, + ERF, + ERFC, + ISINF, + ISPOSINF, + ISNEGINF, + ISNAN, + LOGICAL_NOT_UNARY, + ISFINITE, + ERFINV, + I0, + TAN, + RSUB, + RDIV, + SILU, + SOFTPLUS, + IDENTITY, + NEG, + ADD_UNARY_SFPU, + SUB_UNARY_SFPU, + MUL_UNARY_SFPU, + DIV_UNARY_SFPU, + IDENTITY_UINT32, + UNARY_NE, + UNARY_GT, + UNARY_LT, + TILED_PROD, + TYPECAST, + BITWISE_XOR, + BITWISE_NOT, + BITWISE_AND, + BITWISE_OR, + RIGHT_SHIFT, + FLOOR, + CEIL, + LEFT_SHIFT, + REMAINDER, + FMOD, +}; + +} diff --git a/ttnn/cpp/ttnn/operations/eltwise/unary/device/unary_op.hpp b/ttnn/cpp/ttnn/operations/eltwise/unary/device/unary_op.hpp index 506f078174a..be6c50b672d 100644 --- a/ttnn/cpp/ttnn/operations/eltwise/unary/device/unary_op.hpp +++ b/ttnn/cpp/ttnn/operations/eltwise/unary/device/unary_op.hpp @@ -13,87 +13,12 @@ #include "ttnn/tensor/tensor_utils.hpp" #include "ttnn/run_operation.hpp" +#include "ttnn/operations/eltwise/unary/common/unary_op_types.hpp" namespace ttnn::operations::unary { -// These operations have a corresponding LLK available -enum class UnaryOpType { - EXP, - RECIP, - GELU, - RELU, - SQRT, - SIGMOID, - LOG, - TANH, - LOG2, - LOG10, - SIN, - COS, - ABS, - SIGN, - SQUARE, - EQZ, - NEZ, - GTZ, - LTZ, - GEZ, - LEZ, - RELU_MAX, - RELU_MIN, - POWER, - LEAKY_RELU, - ELU, - EXP2, - HEAVISIDE, - EXPM1, - SIGNBIT, - ASIN, - ACOS, - RSQRT, - RELU6, - ATAN, - ERF, - ERFC, - ISINF, - ISPOSINF, - ISNEGINF, - ISNAN, - LOGICAL_NOT_UNARY, - ISFINITE, - ERFINV, - I0, - TAN, - RSUB, - RDIV, - SILU, - SOFTPLUS, - IDENTITY, - NEG, - ADD_UNARY_SFPU, - SUB_UNARY_SFPU, - MUL_UNARY_SFPU, - DIV_UNARY_SFPU, - IDENTITY_UINT32, - UNARY_NE, - UNARY_GT, - UNARY_LT, - TILED_PROD, - TYPECAST, - BITWISE_XOR, - BITWISE_NOT, - BITWISE_AND, - BITWISE_OR, - RIGHT_SHIFT, - FLOOR, - CEIL, - LEFT_SHIFT, - REMAINDER, - FMOD, -}; - template bool is_parametrized_type(T val) { switch (val) { diff --git a/ttnn/cpp/ttnn/operations/eltwise/unary/unary.cpp b/ttnn/cpp/ttnn/operations/eltwise/unary/unary.cpp new file mode 100644 index 00000000000..7741ca39570 --- /dev/null +++ b/ttnn/cpp/ttnn/operations/eltwise/unary/unary.cpp @@ -0,0 +1,447 @@ +// SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. +// +// SPDX-License-Identifier: Apache-2.0 + +#include "unary.hpp" + +#include "device/unary_op.hpp" +#include "ttnn/run_operation.hpp" +#include "ttnn/operations/data_movement/downsample/device/downsample_op.hpp" +#include "ttnn/operations/core/core.hpp" + +namespace ttnn::operations::unary { + +namespace detail { + +inline Tensor unary_impl( + uint8_t queue_id, + const Tensor& input_tensor, + const std::vector& op_chain, + const std::optional& memory_config = std::nullopt, + const std::optional& optional_output_tensor = std::nullopt) { + DataType output_dtype = (op_chain[0].op_type == UnaryOpType::TYPECAST) ? static_cast(op_chain[0].params[1]) : input_tensor.get_dtype(); + bool preserve_fp32_precision = (op_chain[0].op_type == UnaryOpType::TYPECAST) and (input_tensor.get_dtype() == DataType::FLOAT32); + bool fp32_dest_acc_en = preserve_fp32_precision or + output_dtype == DataType::UINT32 or + output_dtype == DataType::INT32 or + output_dtype == DataType::FLOAT32 or + input_tensor.get_dtype() == DataType::UINT32 or + input_tensor.get_dtype() == DataType::INT32; // MT: Currently only uint32/int32 is moved to + // DST directly, fp32 is converted to fp16b + + auto output_memory_config = optional_output_tensor.has_value() ? optional_output_tensor.value().memory_config() : memory_config.value_or(input_tensor.memory_config()); + return operation::run( + Unary{op_chain, output_memory_config, fp32_dest_acc_en, preserve_fp32_precision, output_dtype}, + {input_tensor}, {}, {optional_output_tensor}, queue_id).at(0); +} + +} // namespace detail + +template +Tensor ExecuteUnary::operator()( + uint8_t queue_id, + const Tensor& input_tensor, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + queue_id, input_tensor, {UnaryWithParam{unary_op_types}...}, memory_config, optional_output_tensor); +} + +template +Tensor ExecuteUnary::operator()( + const Tensor& input_tensor, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + DefaultQueueId, input_tensor, {UnaryWithParam{unary_op_types}...}, memory_config, optional_output_tensor); +} + +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; +template struct ExecuteUnary; + +template +Tensor ExecuteUnaryWithFastAndApproximateMode::operator()( + uint8_t queue_id, + const Tensor& input_tensor, + const bool parameter, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + queue_id, + input_tensor, + {UnaryWithParam{unary_op_type, static_cast(parameter)}}, + memory_config, + optional_output_tensor); +} + +template +Tensor ExecuteUnaryWithFastAndApproximateMode::operator()( + const Tensor& input_tensor, + const bool parameter, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + DefaultQueueId, + input_tensor, + {UnaryWithParam{unary_op_type, static_cast(parameter)}}, + memory_config, + optional_output_tensor); +} + +template struct ExecuteUnaryWithFastAndApproximateMode; +template struct ExecuteUnaryWithFastAndApproximateMode; +template struct ExecuteUnaryWithFastAndApproximateMode; +template struct ExecuteUnaryWithFastAndApproximateMode; +template struct ExecuteUnaryWithFastAndApproximateMode; + +template +Tensor ExecuteUnaryWithFloatParameter::operator()( + uint8_t queue_id, + const Tensor& input_tensor, + const float parameter, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + queue_id, + input_tensor, + {UnaryWithParam{unary_op_type, static_cast(parameter)}}, + memory_config, + optional_output_tensor); +} + +template +Tensor ExecuteUnaryWithFloatParameter::operator()( + const Tensor& input_tensor, + const float parameter, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + DefaultQueueId, + input_tensor, + {UnaryWithParam{unary_op_type, static_cast(parameter)}}, + memory_config, + optional_output_tensor); +} + +template struct ExecuteUnaryWithFloatParameter; +template struct ExecuteUnaryWithFloatParameter; +template struct ExecuteUnaryWithFloatParameter; +template struct ExecuteUnaryWithFloatParameter; +template struct ExecuteUnaryWithFloatParameter; +template struct ExecuteUnaryWithFloatParameter; +template struct ExecuteUnaryWithFloatParameter; +template struct ExecuteUnaryWithFloatParameter; +template struct ExecuteUnaryWithFloatParameter; +template struct ExecuteUnaryWithFloatParameter; +template struct ExecuteUnaryWithFloatParameter; + +Tensor Sigmoid_accurate::operator()( + uint8_t queue_id, + const Tensor& input, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + queue_id, + input, + {UnaryWithParam(UnaryOpType::NEG), + UnaryWithParam(UnaryOpType::EXP, 1.0f), + UnaryWithParam(UnaryOpType::ADD_UNARY_SFPU, 1.0f), + UnaryWithParam(UnaryOpType::RECIP)}, + memory_config, + optional_output_tensor); +} + +Tensor Sigmoid_accurate::operator()( + const Tensor& input, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + DefaultQueueId, + input, + {UnaryWithParam(UnaryOpType::NEG), + UnaryWithParam(UnaryOpType::EXP, 1.0f), + UnaryWithParam(UnaryOpType::ADD_UNARY_SFPU, 1.0f), + UnaryWithParam(UnaryOpType::RECIP)}, + memory_config, + optional_output_tensor); +} + +Tensor Unary_chain::operator()( + uint8_t queue_id, + const Tensor& input_tensor, + const std::vector& ops_chain, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + TT_FATAL(ops_chain.size() > 0, "Op chain cannot be empty"); + return detail::unary_impl(queue_id, input_tensor, ops_chain, memory_config, optional_output_tensor); +} + +Tensor Unary_chain::operator()( + const Tensor& input_tensor, + const std::vector& ops_chain, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + TT_FATAL(ops_chain.size() > 0, "Op chain cannot be empty"); + return detail::unary_impl(DefaultQueueId, input_tensor, ops_chain, memory_config, optional_output_tensor); +} + +Tensor Softplus::operator()( + uint8_t queue_id, + const Tensor& input, + const float beta, + const float threshold, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + TT_ASSERT(input.device()->arch() != tt::ARCH::GRAYSKULL, "Softplus is not currently supported on Grayskull"); + return detail::unary_impl( + queue_id, + input, + {UnaryWithParam{UnaryOpType::SOFTPLUS, {beta, threshold}}}, + memory_config, + optional_output_tensor); +} + +Tensor Softplus::operator()( + const Tensor& input, + const float beta, + const float threshold, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + TT_ASSERT(input.device()->arch() != tt::ARCH::GRAYSKULL, "Softplus is not currently supported on Grayskull"); + return detail::unary_impl( + DefaultQueueId, + input, + {UnaryWithParam{UnaryOpType::SOFTPLUS, {beta, threshold}}}, + memory_config, + optional_output_tensor); +} + +Tensor Identity::operator()( + uint8_t queue_id, + const Tensor& input_tensor, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + UnaryOpType op_type = UnaryOpType::IDENTITY; + if (input_tensor.get_dtype() == DataType::UINT32) { + op_type = UnaryOpType::IDENTITY_UINT32; + } + + return detail::unary_impl( + queue_id, input_tensor, {UnaryWithParam{op_type}}, memory_config, optional_output_tensor); +} + +Tensor Identity::operator()( + const Tensor& input_tensor, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + UnaryOpType op_type = UnaryOpType::IDENTITY; + if (input_tensor.get_dtype() == DataType::UINT32) { + op_type = UnaryOpType::IDENTITY_UINT32; + } + + return detail::unary_impl( + DefaultQueueId, input_tensor, {UnaryWithParam{op_type}}, memory_config, optional_output_tensor); +} + +template +Tensor ExecuteUnaryWithIntegerParameter::operator()( + uint8_t queue_id, + const Tensor& input_tensor, + T parameter, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + queue_id, + input_tensor, + {UnaryWithParam{unary_op_type, static_cast(parameter)}}, + memory_config, + optional_output_tensor); +} + +template +Tensor ExecuteUnaryWithIntegerParameter::operator()( + const Tensor& input_tensor, + T parameter, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + DefaultQueueId, + input_tensor, + {UnaryWithParam{unary_op_type, static_cast(parameter)}}, + memory_config, + optional_output_tensor); +} + +template struct ExecuteUnaryWithIntegerParameter; +template struct ExecuteUnaryWithIntegerParameter; +template struct ExecuteUnaryWithIntegerParameter; +template struct ExecuteUnaryWithIntegerParameter; +template struct ExecuteUnaryWithIntegerParameter; +template struct ExecuteUnaryWithIntegerParameter; +template struct ExecuteUnaryWithIntegerParameter; + + +template +Tensor SymmetricBinop::operator()( + uint8_t queue_id, + const Tensor& input_tensor, + T param, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + queue_id, + input_tensor, + {UnaryWithParam(unary_op_type, static_cast(param))}, + memory_config, + optional_output_tensor); +} + +template +Tensor SymmetricBinop::operator()( + uint8_t queue_id, + T param, + const Tensor& input_tensor, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + queue_id, + input_tensor, + {UnaryWithParam(unary_op_type, static_cast(param))}, + memory_config, + optional_output_tensor); +} + +template +Tensor SymmetricBinop::operator()( + const Tensor& input_tensor, + T param, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + DefaultQueueId, + input_tensor, + {UnaryWithParam(unary_op_type, static_cast(param))}, + memory_config, + optional_output_tensor); +} + +template +Tensor SymmetricBinop::operator()( + T param, + const Tensor& input_tensor, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + DefaultQueueId, + input_tensor, + {UnaryWithParam(unary_op_type, static_cast(param))}, + memory_config, + optional_output_tensor); +} + +// Explicit template instantiation +template struct SymmetricBinop; +template struct SymmetricBinop; + + +template +Tensor AsymmetricBinop::operator()( + uint8_t queue_id, + const Tensor& input_tensor, + float param, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + queue_id, + input_tensor, + {UnaryWithParam(unary_op_type, static_cast(param))}, + memory_config, + optional_output_tensor); +} + +template +Tensor AsymmetricBinop::operator()( + uint8_t queue_id, + float param, + const Tensor& input_tensor, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + queue_id, + input_tensor, + {UnaryWithParam(unary_op_rev_type, static_cast(param))}, + memory_config, + optional_output_tensor); +} + +template +Tensor AsymmetricBinop::operator()( + const Tensor& input_tensor, + float param, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + DefaultQueueId, + input_tensor, + {UnaryWithParam(unary_op_type, static_cast(param))}, + memory_config, + optional_output_tensor); +} + +template +Tensor AsymmetricBinop::operator()( + float param, + const Tensor& input_tensor, + const std::optional& memory_config, + const std::optional& optional_output_tensor) { + return detail::unary_impl( + DefaultQueueId, + input_tensor, + {UnaryWithParam(unary_op_rev_type, static_cast(param))}, + memory_config, + optional_output_tensor); +} + +template struct AsymmetricBinop; +template struct AsymmetricBinop; + +} diff --git a/ttnn/cpp/ttnn/operations/eltwise/unary/unary.hpp b/ttnn/cpp/ttnn/operations/eltwise/unary/unary.hpp index cae51682111..1cf740c8df3 100644 --- a/ttnn/cpp/ttnn/operations/eltwise/unary/unary.hpp +++ b/ttnn/cpp/ttnn/operations/eltwise/unary/unary.hpp @@ -4,11 +4,8 @@ #pragma once -#include "device/unary_op.hpp" -#include "ttnn/run_operation.hpp" -#include "ttnn/operations/data_movement/downsample/device/downsample_op.hpp" #include "ttnn/decorators.hpp" -#include "ttnn/operations/core/core.hpp" +#include "ttnn/operations/eltwise/unary/common/unary_op_types.hpp" namespace ttnn { @@ -16,31 +13,7 @@ namespace operations { namespace unary { -namespace detail { - -inline Tensor unary_impl( - uint8_t queue_id, - const Tensor& input_tensor, - const std::vector& op_chain, - const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - DataType output_dtype = (op_chain[0].op_type == UnaryOpType::TYPECAST) ? static_cast(op_chain[0].params[1]) : input_tensor.get_dtype(); - bool preserve_fp32_precision = (op_chain[0].op_type == UnaryOpType::TYPECAST) and (input_tensor.get_dtype() == DataType::FLOAT32); - bool fp32_dest_acc_en = preserve_fp32_precision or - output_dtype == DataType::UINT32 or - output_dtype == DataType::INT32 or - output_dtype == DataType::FLOAT32 or - input_tensor.get_dtype() == DataType::UINT32 or - input_tensor.get_dtype() == DataType::INT32; // MT: Currently only uint32/int32 is moved to - // DST directly, fp32 is converted to fp16b - - auto output_memory_config = optional_output_tensor.has_value() ? optional_output_tensor.value().memory_config() : memory_config.value_or(input_tensor.memory_config()); - return operation::run( - Unary{op_chain, output_memory_config, fp32_dest_acc_en, preserve_fp32_precision, output_dtype}, - {input_tensor}, {}, {optional_output_tensor}, queue_id).at(0); -} - -} // namespace detail +struct UnaryWithParam; template struct ExecuteUnary { @@ -48,17 +21,12 @@ struct ExecuteUnary { uint8_t queue_id, const Tensor& input_tensor, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - queue_id, input_tensor, {UnaryWithParam{unary_op_types}...}, memory_config, optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); + static Tensor operator()( const Tensor& input_tensor, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - DefaultQueueId, input_tensor, {UnaryWithParam{unary_op_types}...}, memory_config, optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); }; template @@ -68,26 +36,13 @@ struct ExecuteUnaryWithFastAndApproximateMode { const Tensor& input_tensor, const bool parameter = false, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - queue_id, - input_tensor, - {UnaryWithParam{unary_op_type, static_cast(parameter)}}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); + static Tensor operator()( const Tensor& input_tensor, const bool parameter = false, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - DefaultQueueId, - input_tensor, - {UnaryWithParam{unary_op_type, static_cast(parameter)}}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); }; template @@ -97,113 +52,58 @@ struct ExecuteUnaryWithFloatParameter { const Tensor& input_tensor, const float parameter, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - queue_id, - input_tensor, - {UnaryWithParam{unary_op_type, static_cast(parameter)}}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); static Tensor operator()( const Tensor& input_tensor, const float parameter, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - DefaultQueueId, - input_tensor, - {UnaryWithParam{unary_op_type, static_cast(parameter)}}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); }; -struct Softplus { +struct Sigmoid_accurate { static Tensor operator()( uint8_t queue_id, const Tensor& input, - const float beta, - const float threshold, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - TT_ASSERT(input.device()->arch() != tt::ARCH::GRAYSKULL, "Softplus is not currently supported on Grayskull"); - return detail::unary_impl( - queue_id, - input, - {UnaryWithParam{UnaryOpType::SOFTPLUS, {beta, threshold}}}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); + static Tensor operator()( const Tensor& input, - const float beta, - const float threshold, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - TT_ASSERT(input.device()->arch() != tt::ARCH::GRAYSKULL, "Softplus is not currently supported on Grayskull"); - return detail::unary_impl( - DefaultQueueId, - input, - {UnaryWithParam{UnaryOpType::SOFTPLUS, {beta, threshold}}}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); }; -struct Sigmoid_accurate { +struct Unary_chain { static Tensor operator()( uint8_t queue_id, - const Tensor& input, + const Tensor& input_tensor, + const std::vector& ops_chain, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - queue_id, - input, - {UnaryWithParam(UnaryOpType::NEG), - UnaryWithParam(UnaryOpType::EXP, 1.0f), - UnaryWithParam(UnaryOpType::ADD_UNARY_SFPU, 1.0f), - UnaryWithParam(UnaryOpType::RECIP)}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); static Tensor operator()( - const Tensor& input, + const Tensor& input_tensor, + const std::vector& ops_chain, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - DefaultQueueId, - input, - {UnaryWithParam(UnaryOpType::NEG), - UnaryWithParam(UnaryOpType::EXP, 1.0f), - UnaryWithParam(UnaryOpType::ADD_UNARY_SFPU, 1.0f), - UnaryWithParam(UnaryOpType::RECIP)}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); }; -struct Unary_chain { +struct Softplus { static Tensor operator()( uint8_t queue_id, - const Tensor& input_tensor, - const std::vector& ops_chain, + const Tensor& input, + const float beta, + const float threshold, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - TT_FATAL(ops_chain.size() > 0, "Op chain cannot be empty"); - return detail::unary_impl(queue_id, input_tensor, ops_chain, memory_config, optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); static Tensor operator()( - const Tensor& input_tensor, - const std::vector& ops_chain, + const Tensor& input, + const float beta, + const float threshold, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - TT_FATAL(ops_chain.size() > 0, "Op chain cannot be empty"); - return detail::unary_impl(DefaultQueueId, input_tensor, ops_chain, memory_config, optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); }; struct Identity { @@ -211,57 +111,28 @@ struct Identity { uint8_t queue_id, const Tensor& input_tensor, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - UnaryOpType op_type = UnaryOpType::IDENTITY; - if (input_tensor.get_dtype() == DataType::UINT32) { - op_type = UnaryOpType::IDENTITY_UINT32; - } - - return detail::unary_impl( - queue_id, input_tensor, {UnaryWithParam{op_type}}, memory_config, optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); + static Tensor operator()( const Tensor& input_tensor, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - UnaryOpType op_type = UnaryOpType::IDENTITY; - if (input_tensor.get_dtype() == DataType::UINT32) { - op_type = UnaryOpType::IDENTITY_UINT32; - } - - return detail::unary_impl( - DefaultQueueId, input_tensor, {UnaryWithParam{op_type}}, memory_config, optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); }; -template +template struct ExecuteUnaryWithIntegerParameter { static Tensor operator()( uint8_t queue_id, const Tensor& input_tensor, T parameter, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - queue_id, - input_tensor, - {UnaryWithParam{unary_op_type, static_cast(parameter)}}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); static Tensor operator()( const Tensor& input_tensor, T parameter, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - DefaultQueueId, - input_tensor, - {UnaryWithParam{unary_op_type, static_cast(parameter)}}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); }; template @@ -271,105 +142,55 @@ struct SymmetricBinop { const Tensor& input_tensor, T param, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - queue_id, - input_tensor, - {UnaryWithParam(unary_op_type, static_cast(param))}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); + static Tensor operator()( uint8_t queue_id, T param, const Tensor& input_tensor, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - queue_id, - input_tensor, - {UnaryWithParam(unary_op_type, static_cast(param))}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); + static Tensor operator()( const Tensor& input_tensor, T param, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - DefaultQueueId, - input_tensor, - {UnaryWithParam(unary_op_type, static_cast(param))}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); + static Tensor operator()( T param, const Tensor& input_tensor, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - DefaultQueueId, - input_tensor, - {UnaryWithParam(unary_op_type, static_cast(param))}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); }; -template +template struct AsymmetricBinop { static Tensor operator()( uint8_t queue_id, const Tensor& input_tensor, - T param, + float param, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - queue_id, - input_tensor, - {UnaryWithParam(unary_op_type, static_cast(param))}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); + static Tensor operator()( uint8_t queue_id, - T param, + float param, const Tensor& input_tensor, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - queue_id, - input_tensor, - {UnaryWithParam(unary_op_rev_type, static_cast(param))}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); + static Tensor operator()( const Tensor& input_tensor, - T param, + float param, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - DefaultQueueId, - input_tensor, - {UnaryWithParam(unary_op_type, static_cast(param))}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); + static Tensor operator()( - T param, + float param, const Tensor& input_tensor, const std::optional& memory_config = std::nullopt, - const std::optional& optional_output_tensor = std::nullopt) { - return detail::unary_impl( - DefaultQueueId, - input_tensor, - {UnaryWithParam(unary_op_rev_type, static_cast(param))}, - memory_config, - optional_output_tensor); - } + const std::optional& optional_output_tensor = std::nullopt); }; } // namespace unary