Skip to content

Commit

Permalink
#6123: Add support for backward mvlgamma
Browse files Browse the repository at this point in the history
  • Loading branch information
bharane-ab committed Mar 12, 2024
1 parent b76f05a commit f895ddb
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/source/ttnn/dependencies/tt_lib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,8 @@ Backward Operations

.. autofunction:: tt_lib.tensor.le_bw

.. autofunction:: tt_lib.tensor.mvlgamma_bw

Loss Functions
==============

Expand Down
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions tt_eager/tt_dnn/op_library/backward/backward_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,27 @@ std::vector<Tensor> le_bw(const Tensor& grad, const MemoryConfig& output_mem_con
return operation::decorate_as_composite(__func__, _le_bw)(grad, output_mem_config);
}

std::vector<Tensor> _mvlgamma_bw(const Tensor& grad, const Tensor& input, const MemoryConfig& output_mem_config) {
std::vector<Tensor> 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<Tensor> 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
2 changes: 2 additions & 0 deletions tt_eager/tt_dnn/op_library/backward/backward_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ std::vector<Tensor> log2_bw(const Tensor& grad, const Tensor& input, const Memor
std::vector<Tensor> ge_bw(const Tensor& grad, const MemoryConfig& output_mem_config = operation::DEFAULT_OUTPUT_MEMORY_CONFIG);

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

std::vector<Tensor> mvlgamma_bw(const Tensor& grad, const Tensor& input, const MemoryConfig& output_mem_config = operation::DEFAULT_OUTPUT_MEMORY_CONFIG);
} //namespace tt_metal

} //namespace tt
16 changes: 16 additions & 0 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 @@ -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");
}
}

0 comments on commit f895ddb

Please sign in to comment.