diff --git a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_abs.py b/tests/ttnn/unit_tests/operations/backward/test_backward_abs.py similarity index 87% rename from tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_abs.py rename to tests/ttnn/unit_tests/operations/backward/test_backward_abs.py index 02b5481f7ec3..4da39c4a4366 100644 --- a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_abs.py +++ b/tests/ttnn/unit_tests/operations/backward/test_backward_abs.py @@ -5,7 +5,7 @@ import torch import pytest import ttnn -from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import data_gen_with_range, compare_pcc +from tests.ttnn.unit_tests.operations.backward.utility_funcs import data_gen_with_range, compare_pcc @pytest.mark.parametrize( diff --git a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_floor.py b/tests/ttnn/unit_tests/operations/backward/test_backward_floor.py similarity index 87% rename from tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_floor.py rename to tests/ttnn/unit_tests/operations/backward/test_backward_floor.py index c9efe6c5e86d..bedf8ea2fef7 100644 --- a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_floor.py +++ b/tests/ttnn/unit_tests/operations/backward/test_backward_floor.py @@ -5,7 +5,7 @@ import torch import pytest import ttnn -from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import compare_pcc, data_gen_with_range +from tests.ttnn.unit_tests.operations.backward.utility_funcs import data_gen_with_range, compare_pcc @pytest.mark.parametrize( diff --git a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_log.py b/tests/ttnn/unit_tests/operations/backward/test_backward_log.py similarity index 94% rename from tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_log.py rename to tests/ttnn/unit_tests/operations/backward/test_backward_log.py index 3418ea67dfae..1fcec1f39584 100644 --- a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_log.py +++ b/tests/ttnn/unit_tests/operations/backward/test_backward_log.py @@ -5,7 +5,7 @@ import torch import pytest import ttnn -from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import ( +from tests.ttnn.unit_tests.operations.backward.utility_funcs import ( data_gen_with_val, compare_pcc, data_gen_with_range, diff --git a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_relu6.py b/tests/ttnn/unit_tests/operations/backward/test_backward_relu6.py similarity index 87% rename from tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_relu6.py rename to tests/ttnn/unit_tests/operations/backward/test_backward_relu6.py index 15aff1a6855c..21e72e32a28c 100644 --- a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_relu6.py +++ b/tests/ttnn/unit_tests/operations/backward/test_backward_relu6.py @@ -5,7 +5,7 @@ import torch import pytest import ttnn -from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import compare_pcc, data_gen_with_range +from tests.ttnn.unit_tests.operations.backward.utility_funcs import data_gen_with_range, compare_pcc @pytest.mark.parametrize( diff --git a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_round.py b/tests/ttnn/unit_tests/operations/backward/test_backward_round.py similarity index 87% rename from tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_round.py rename to tests/ttnn/unit_tests/operations/backward/test_backward_round.py index baabffc00095..0ea2865a08c6 100644 --- a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_round.py +++ b/tests/ttnn/unit_tests/operations/backward/test_backward_round.py @@ -5,7 +5,7 @@ import torch import pytest import ttnn -from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import compare_pcc, data_gen_with_range +from tests.ttnn.unit_tests.operations.backward.utility_funcs import data_gen_with_range, compare_pcc @pytest.mark.parametrize( diff --git a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_selu.py b/tests/ttnn/unit_tests/operations/backward/test_backward_selu.py similarity index 87% rename from tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_selu.py rename to tests/ttnn/unit_tests/operations/backward/test_backward_selu.py index 4218ffa15245..7adc3c09ee1c 100644 --- a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_selu.py +++ b/tests/ttnn/unit_tests/operations/backward/test_backward_selu.py @@ -5,7 +5,7 @@ import torch import pytest import ttnn -from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import data_gen_with_range, compare_pcc +from tests.ttnn.unit_tests.operations.backward.utility_funcs import data_gen_with_range, compare_pcc @pytest.mark.parametrize( diff --git a/tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_silu.py b/tests/ttnn/unit_tests/operations/backward/test_backward_silu.py similarity index 100% rename from tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_silu.py rename to tests/ttnn/unit_tests/operations/backward/test_backward_silu.py diff --git a/tests/ttnn/unit_tests/operations/backward/utility_funcs.py b/tests/ttnn/unit_tests/operations/backward/utility_funcs.py new file mode 100644 index 000000000000..b83d435d32db --- /dev/null +++ b/tests/ttnn/unit_tests/operations/backward/utility_funcs.py @@ -0,0 +1,123 @@ +# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + + +import torch +import tt_lib +from loguru import logger +from tests.tt_eager.python_api_testing.sweep_tests import ( + comparison_funcs, +) + + +def data_gen_pt_tt(input_shapes, device, required_grad=False): + torch.manual_seed(213919) + pt_tensor = torch.randn(input_shapes, requires_grad=required_grad).bfloat16() + tt_tensor = ( + tt_lib.tensor.Tensor(pt_tensor, tt_lib.tensor.DataType.BFLOAT16).to(tt_lib.tensor.Layout.TILE).to(device) + ) + return pt_tensor, tt_tensor + + +def data_gen_with_range(input_shapes, low, high, device, required_grad=False, is_row_major=False): + assert high > low, "Incorrect range provided" + torch.manual_seed(213919) + pt_tensor = torch.rand(input_shapes, requires_grad=required_grad).bfloat16() * (high - low) + low + if is_row_major: + tt_tensor = ( + tt_lib.tensor.Tensor(pt_tensor, tt_lib.tensor.DataType.BFLOAT16) + .to(tt_lib.tensor.Layout.ROW_MAJOR) + .to(device) + ) + else: + tt_tensor = ( + tt_lib.tensor.Tensor(pt_tensor, tt_lib.tensor.DataType.BFLOAT16).to(tt_lib.tensor.Layout.TILE).to(device) + ) + return pt_tensor, tt_tensor + + +def data_gen_with_val(input_shapes, device, required_grad=False, val=1, is_row_major=False): + pt_tensor = (torch.ones(input_shapes, requires_grad=required_grad) * val).bfloat16() + if is_row_major: + tt_tensor = ( + tt_lib.tensor.Tensor(pt_tensor, tt_lib.tensor.DataType.BFLOAT16) + .to(tt_lib.tensor.Layout.ROW_MAJOR) + .to(device) + ) + else: + tt_tensor = ( + tt_lib.tensor.Tensor(pt_tensor, tt_lib.tensor.DataType.BFLOAT16).to(tt_lib.tensor.Layout.TILE).to(device) + ) + return pt_tensor, tt_tensor + + +def data_gen_pt_tt_prod(input_shapes, device, all_dimensions, dim, required_grad=False): + torch.manual_seed(213919) + pt_tensor_temp = torch.zeros(input_shapes, requires_grad=required_grad).bfloat16() + shape_Required = torch.Size( + [ + input_shapes[0] if (dim != 0 and dim != -4) else 1, + input_shapes[1] if (dim != 1 and dim != -3) else 1, + input_shapes[2] if (dim != 2 and dim != -2) else 1, + input_shapes[3] if (dim != 3 and dim != -1) else 1, + ] + ) + if all_dimensions == False and (dim == 1 or dim == 0 or dim == -4 or dim == -3): + pt_tensor = torch.randn(shape_Required, requires_grad=required_grad).bfloat16() + tt_tensor = ( + tt_lib.tensor.Tensor(pt_tensor, tt_lib.tensor.DataType.BFLOAT16).to(tt_lib.tensor.Layout.TILE).to(device) + ) + return pt_tensor, tt_tensor + elif all_dimensions == False: + pt_tensor = torch.randn(shape_Required, requires_grad=required_grad).bfloat16() + if dim == 3 or dim == -1: + pt_tensor_temp[:, :, :, :1] = pt_tensor + elif dim == 2 or dim == -2: + pt_tensor_temp[:, :, :1, :] = pt_tensor + else: + shape_Required = torch.Size([1, 1, 1, 1]) + pt_tensor = torch.randn(shape_Required, requires_grad=required_grad).bfloat16() + pt_tensor_temp[:1, :1, :1, :1] = pt_tensor + tt_tensor = ( + tt_lib.tensor.Tensor(pt_tensor_temp, tt_lib.tensor.DataType.BFLOAT16).to(tt_lib.tensor.Layout.TILE).to(device) + ) + return pt_tensor, tt_tensor + + +def compare_results(tt_tensor, golden_tensor, pcc=0.99): + status = True + for i in range(len(tt_tensor)): + tt_out_tensor = tt_tensor[i].cpu().to(tt_lib.tensor.Layout.ROW_MAJOR).to_torch() + pt_out_tensor = golden_tensor[i] + comp_pass, comp_out = comparison_funcs.comp_pcc(pt_out_tensor, tt_out_tensor, pcc=pcc) + comp_all, _ = comparison_funcs.comp_allclose(pt_out_tensor, tt_out_tensor, atol=4, rtol=1e-1) + logger.debug(comp_pass) + logger.debug(comp_all) + logger.debug(comp_out) + status = status & (comp_pass | comp_all) + return status + + +def compare_pcc(tt_tensor, golden_tensor, pcc=0.99): + status = True + for i in range(len(tt_tensor)): + tt_out_tensor = tt_tensor[i].cpu().to(tt_lib.tensor.Layout.ROW_MAJOR).to_torch() + pt_out_tensor = golden_tensor[i] + comp_pass, comp_out = comparison_funcs.comp_pcc(pt_out_tensor, tt_out_tensor, pcc=pcc) + logger.debug(comp_pass) + logger.debug(comp_out) + status = status & comp_pass + return status + + +def compare_all_close(tt_tensor, golden_tensor, atol=4, rtol=1e-1): + status = True + for i in range(len(tt_tensor)): + tt_out_tensor = tt_tensor[i].cpu().to(tt_lib.tensor.Layout.ROW_MAJOR).to_torch() + pt_out_tensor = golden_tensor[i] + comp_all, comp_out = comparison_funcs.comp_allclose(pt_out_tensor, tt_out_tensor, atol=atol, rtol=rtol) + logger.debug(comp_all) + logger.debug(comp_out) + status = status & comp_all + return status