diff --git a/docs/source/ttnn/dependencies/tt_lib.rst b/docs/source/ttnn/dependencies/tt_lib.rst index ce7ca7d0fef1..75f61f1e34c8 100644 --- a/docs/source/ttnn/dependencies/tt_lib.rst +++ b/docs/source/ttnn/dependencies/tt_lib.rst @@ -1040,6 +1040,8 @@ Backward Operations .. autofunction:: tt_lib.tensor.le_bw +.. autofunction:: tt_lib.tensor.mvlgamma_bw + Loss Functions ============== diff --git a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_mvlgamma.py b/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_mvlgamma.py new file mode 100644 index 000000000000..fb640c3e9096 --- /dev/null +++ b/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_mvlgamma.py @@ -0,0 +1,39 @@ +# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + +import torch +import pytest +import tt_lib +from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import compare_results, data_gen_pt_tt + + +@pytest.mark.parametrize( + "input_shapes", + ( + (torch.Size([1, 1, 32, 32])), + (torch.Size([1, 1, 320, 384])), + (torch.Size([1, 3, 320, 384])), + ), +) +def test_bw_mvlgamma(input_shapes, device): + grad_data, grad_tensor = data_gen_pt_tt(input_shapes, device) + + in_data = torch.Tensor(size=input_shapes).uniform_(3, 10) + in_data.requires_grad = True + input_tensor = ( + tt_lib.tensor.Tensor(in_data, tt_lib.tensor.DataType.BFLOAT16).to(tt_lib.tensor.Layout.TILE).to(device) + ) + + pyt_y = torch.mvlgamma(in_data, 4) + + tt_output_tensor_on_device = tt_lib.tensor.mvlgamma_bw(grad_tensor, input_tensor) + + in_data.retain_grad() + + pyt_y.backward(gradient=grad_data) + + golden_tensor = [in_data.grad] + comp_pass = compare_results(tt_output_tensor_on_device, golden_tensor) + + assert comp_pass diff --git a/tt_eager/tt_dnn/op_library/backward/backward_ops.cpp b/tt_eager/tt_dnn/op_library/backward/backward_ops.cpp index 9664c13a833a..065730813fbc 100644 --- a/tt_eager/tt_dnn/op_library/backward/backward_ops.cpp +++ b/tt_eager/tt_dnn/op_library/backward/backward_ops.cpp @@ -1551,6 +1551,27 @@ std::vector le_bw(const Tensor& grad, const MemoryConfig& output_mem_con return operation::decorate_as_composite(__func__, _le_bw)(grad, output_mem_config); } +std::vector _mvlgamma_bw(const Tensor& grad, const Tensor& input, const MemoryConfig& output_mem_config) { + std::vector grad_tensor; + Tensor digamma_result = mul(grad, digamma(input, output_mem_config), std::nullopt, output_mem_config); + Tensor digamma_result_2 = mul(grad, digamma(add_unary(-0.5 , input, output_mem_config), output_mem_config), std::nullopt, output_mem_config); + + Tensor grad_result = add(digamma_result, digamma_result_2, std::nullopt, output_mem_config); + + digamma_result = mul(grad, digamma(add_unary(-1.0 , input, output_mem_config), output_mem_config), std::nullopt, output_mem_config); + grad_result = add(grad_result, digamma_result, std::nullopt, output_mem_config); + + digamma_result = mul(grad, digamma(add_unary(-1.5 , input, output_mem_config), output_mem_config), std::nullopt, output_mem_config); + grad_result = add(grad_result, digamma_result, std::nullopt, output_mem_config); + + grad_tensor.emplace_back(grad_result); + return grad_tensor; +} +std::vector mvlgamma_bw(const Tensor& grad, const Tensor& input, const MemoryConfig& output_mem_config) +{ + return operation::decorate_as_composite(__func__, _mvlgamma_bw)(grad, input, output_mem_config); +} + }//namespace tt_metal }//namespace tt diff --git a/tt_eager/tt_dnn/op_library/backward/backward_ops.hpp b/tt_eager/tt_dnn/op_library/backward/backward_ops.hpp index d3ca15904010..54a44746e979 100644 --- a/tt_eager/tt_dnn/op_library/backward/backward_ops.hpp +++ b/tt_eager/tt_dnn/op_library/backward/backward_ops.hpp @@ -236,6 +236,8 @@ std::vector log2_bw(const Tensor& grad, const Tensor& input, const Memor std::vector ge_bw(const Tensor& grad, const MemoryConfig& output_mem_config = operation::DEFAULT_OUTPUT_MEMORY_CONFIG); std::vector le_bw(const Tensor& grad, const MemoryConfig& output_mem_config = operation::DEFAULT_OUTPUT_MEMORY_CONFIG); + +std::vector mvlgamma_bw(const Tensor& grad, const Tensor& input, const MemoryConfig& output_mem_config = operation::DEFAULT_OUTPUT_MEMORY_CONFIG); } //namespace tt_metal } //namespace tt diff --git a/tt_eager/tt_lib/csrc/tt_lib_bindings_tensor_backward_ops.cpp b/tt_eager/tt_lib/csrc/tt_lib_bindings_tensor_backward_ops.cpp index 30e61a45f1d0..204358ae80a8 100644 --- a/tt_eager/tt_lib/csrc/tt_lib_bindings_tensor_backward_ops.cpp +++ b/tt_eager/tt_lib/csrc/tt_lib_bindings_tensor_backward_ops.cpp @@ -1811,5 +1811,21 @@ namespace tt::tt_metal::detail{ "grad", "Gradient tensor", "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("mvlgamma_bw", &tt::tt_metal::mvlgamma_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 mvlgamma of ``input`` tensors with given ``grad`` value of P is fixed as 4. + + Input tensors must have BFLOAT16 data type. + + Output tensors 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 mvlgamma 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"); } }