Skip to content

Commit

Permalink
#10071: Merge abs_bw to TTNN
Browse files Browse the repository at this point in the history
  • Loading branch information
mouliraj-mcw committed Jul 11, 2024
1 parent 8bd3e5d commit 3f24708
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 30 deletions.
1 change: 1 addition & 0 deletions docs/source/ttnn/ttnn/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ Pointwise Unary
ttnn/round_bw
ttnn/log_bw
ttnn/relu6_bw
ttnn/abs_bw

Pointwise Binary
================
Expand Down
2 changes: 0 additions & 2 deletions docs/source/ttnn/ttnn/dependencies/tt_lib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,6 @@ Backward Operations

.. autofunction:: tt_lib.tensor.unary_sub_bw

.. autofunction:: tt_lib.tensor.abs_bw

.. autofunction:: tt_lib.tensor.complex_abs_bw

.. autofunction:: tt_lib.tensor.lt_bw
Expand Down
6 changes: 6 additions & 0 deletions docs/source/ttnn/ttnn/ttnn/abs_bw.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _ttnn.abs_bw:

ttnn.abs_bw
############

.. autofunction:: ttnn.abs_bw
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import torch
import pytest
import tt_lib
import ttnn
from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import data_gen_with_range, compare_pcc


Expand All @@ -22,7 +22,7 @@ def test_bw_abs(input_shapes, device):

pyt_y = torch.abs(in_data)

tt_output_tensor_on_device = tt_lib.tensor.abs_bw(grad_tensor, input_tensor)
tt_output_tensor_on_device = ttnn.abs_bw(grad_tensor, input_tensor)

in_data.retain_grad()

Expand Down
18 changes: 14 additions & 4 deletions tt_eager/tt_dnn/op_library/backward/backward_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,24 @@ std::vector<Tensor> ne_bw(const Tensor& grad, const MemoryConfig& output_mem_con
return operation::decorate_as_composite(__func__, _ne_bw)(grad, output_mem_config);
}

std::vector<Tensor> _abs_bw(const Tensor& grad, const Tensor& input, const MemoryConfig& output_mem_config) {
std::vector<Tensor> _rsqrt_bw(const Tensor& grad, const Tensor& input, const MemoryConfig& output_mem_config) {
std::vector<Tensor> grad_tensor;
Tensor result = ttnn::multiply(grad, ttnn::sign(input, output_mem_config), std::nullopt, output_mem_config);
Tensor rsqrt_result = power(rsqrt(input, true, output_mem_config), 3, output_mem_config);
Tensor result = mul_unary(ttnn::multiply(grad, rsqrt_result, std::nullopt, output_mem_config), -0.5, output_mem_config);
float t_inf = std::numeric_limits<float>::infinity();
result = where(eqz(input, output_mem_config), t_inf, result, output_mem_config);
float t_nan = std::nanf("");
result = where(ltz(input, output_mem_config), t_nan, result, output_mem_config);
result = where(
ttnn::logical_and(eqz(input, output_mem_config), eqz(grad, output_mem_config), std::nullopt, output_mem_config),
t_nan,
result,
output_mem_config);
grad_tensor.emplace_back(result);
return grad_tensor;
}
std::vector<Tensor> abs_bw(const Tensor& grad, const Tensor& input, const MemoryConfig& output_mem_config) {
return operation::decorate_as_composite(__func__, _abs_bw)(grad, input, output_mem_config);
std::vector<Tensor> rsqrt_bw(const Tensor& grad, const Tensor& input, const MemoryConfig& output_mem_config) {
return operation::decorate_as_composite(__func__, _rsqrt_bw)(grad, input, output_mem_config);
}

// bw(expm1) = grad * expm1(input) + 1
Expand Down
5 changes: 0 additions & 5 deletions tt_eager/tt_dnn/op_library/backward/backward_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ std::vector<Tensor> binary_le_bw(
const Tensor& input,
const MemoryConfig& output_mem_config = operation::DEFAULT_OUTPUT_MEMORY_CONFIG);

std::vector<Tensor> abs_bw(
const Tensor& grad,
const Tensor& input,
const MemoryConfig& output_mem_config = operation::DEFAULT_OUTPUT_MEMORY_CONFIG);

std::vector<Tensor> complex_abs_bw(
const Tensor& grad,
const Tensor& input,
Expand Down
16 changes: 0 additions & 16 deletions tt_eager/tt_lib/csrc/tt_lib_bindings_tensor_backward_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,22 +328,6 @@ namespace tt::tt_metal::detail{
"output_mem_config", "Layout of tensor in TT Accelerator device memory banks", "MemoryConfig", "Default is interleaved in DRAM", "No"
)doc");

m_tensor.def("abs_bw", &tt::tt_metal::abs_bw,
py::arg("grad").noconvert(), py::arg("input").noconvert(), py::arg("output_mem_config").noconvert() = operation::DEFAULT_OUTPUT_MEMORY_CONFIG, R"doc(
Performs backward operations for abs of ``input`` tensors with given ``grad``.
Input tensors must have BFLOAT16 data type.
Output tensor will have BFLOAT16 data type.
.. csv-table::
:header: "Argument", "Description", "Data type", "Valid range", "Required"
"grad", "Gradient tensor", "Tensor", "Tensor of shape [W, Z, Y, X]", "Yes"
"input", "Tensor add is applied to", "Tensor", "Tensor of shape [W, Z, Y, X]", "Yes"
"output_mem_config", "Layout of tensor in TT Accelerator device memory banks", "MemoryConfig", "Default is interleaved in DRAM", "No"
)doc");

m_tensor.def("complex_abs_bw", py::overload_cast<const Tensor&, const Tensor&, const MemoryConfig&>(&complex_abs_bw),
py::arg("grad").noconvert(), py::arg("input").noconvert(), py::arg("output_mem_config").noconvert() = operation::DEFAULT_OUTPUT_MEMORY_CONFIG, R"doc(
Performs backward operations for abs of complex ``input`` tensor with given ``grad``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ std::vector<Tensor> _relu6_bw(const Tensor& grad, const Tensor& input, const Mem
return grad_tensor;
}

std::vector<Tensor> _abs_bw(const Tensor& grad, const Tensor& input, const MemoryConfig& output_mem_config) {
std::vector<Tensor> grad_tensor;
Tensor result = ttnn::multiply(grad, ttnn::sign(input, output_mem_config), std::nullopt, output_mem_config);
grad_tensor.emplace_back(result);
return grad_tensor;
}

std::function<std::vector<ttnn::Tensor>(const Tensor&, const Tensor&, const MemoryConfig&)> UnaryBackwardFunction::get_function_type1(UnaryBackwardOpType OpType){
switch (OpType) {
case UnaryBackwardOpType::ASSIGN_BW:
Expand Down Expand Up @@ -449,6 +456,8 @@ std::function<std::vector<ttnn::Tensor>(const Tensor&, const Tensor&, const Memo
return _log_bw;
case UnaryBackwardOpType::RELU6_BW:
return _relu6_bw;
case UnaryBackwardOpType::ABS_BW:
return _abs_bw;
default:
TT_ASSERT(false && "Undefined op type");
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ enum class UnaryBackwardOpType {
FLOOR_BW,
ROUND_BW,
LOG_BW,
RELU6_BW
RELU6_BW,
ABS_BW,
};

struct UnaryBackwardFunction{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ constexpr auto floor_bw = ttnn::register_operation<operations::unary_backward::E
constexpr auto round_bw = ttnn::register_operation<operations::unary_backward::ExecuteUnaryBackward<operations::unary_backward::UnaryBackwardOpType::ROUND_BW>>("ttnn::round_bw");
constexpr auto log_bw = ttnn::register_operation<operations::unary_backward::ExecuteUnaryBackward<operations::unary_backward::UnaryBackwardOpType::LOG_BW>>("ttnn::log_bw");
constexpr auto relu6_bw = ttnn::register_operation<operations::unary_backward::ExecuteUnaryBackward<operations::unary_backward::UnaryBackwardOpType::RELU6_BW>>("ttnn::relu6_bw");
constexpr auto abs_bw = ttnn::register_operation<operations::unary_backward::ExecuteUnaryBackward<operations::unary_backward::UnaryBackwardOpType::ABS_BW>>("ttnn::abs_bw");


} // namespace ttnn
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ void py_module(py::module& module) {
ttnn::relu6_bw,
R"doc(Performs backward operations for relu6 on :attr:`input_tensor` with given :attr:`grad_tensor`)doc");

detail::bind_unary_backward(
module,
ttnn::abs_bw,
R"doc(Performs backward operations for abs on :attr:`input_tensor` with given :attr:`grad_tensor`)doc");

}

} // namespace binary_backward
Expand Down

0 comments on commit 3f24708

Please sign in to comment.