From e30bef0e7556ad111ccb4f41b9249dbfc98ed9e4 Mon Sep 17 00:00:00 2001 From: mcw-anasuya Date: Fri, 4 Oct 2024 14:47:48 +0000 Subject: [PATCH] #8157: Update doc and move sweep test for leaky_relu --- .github/workflows/ttnn-run-sweeps.yaml | 1 + .../eltwise/unary/leaky_relu/leaky_relu.py | 76 +++++++++++++++++++ .../sweep_tests/sweeps/sweeps/leaky_relu.py | 55 -------------- .../operations/eltwise/unary/unary_pybind.hpp | 11 ++- 4 files changed, 87 insertions(+), 56 deletions(-) create mode 100644 tests/sweep_framework/sweeps/eltwise/unary/leaky_relu/leaky_relu.py delete mode 100644 tests/ttnn/sweep_tests/sweeps/sweeps/leaky_relu.py diff --git a/.github/workflows/ttnn-run-sweeps.yaml b/.github/workflows/ttnn-run-sweeps.yaml index 1464a3113b60..f683ddbf0018 100644 --- a/.github/workflows/ttnn-run-sweeps.yaml +++ b/.github/workflows/ttnn-run-sweeps.yaml @@ -147,6 +147,7 @@ on: - eltwise.unary.gez.gez - eltwise.unary.lez.lez - eltwise.unary.nez.nez + - eltwise.unary.leaky_relu.leaky_relu - eltwise.binary.subtract.subtract - eltwise.binary.multiply.multiply - eltwise.binary.multiply.mul_tensor_pytorch2 diff --git a/tests/sweep_framework/sweeps/eltwise/unary/leaky_relu/leaky_relu.py b/tests/sweep_framework/sweeps/eltwise/unary/leaky_relu/leaky_relu.py new file mode 100644 index 000000000000..93a3ea15e825 --- /dev/null +++ b/tests/sweep_framework/sweeps/eltwise/unary/leaky_relu/leaky_relu.py @@ -0,0 +1,76 @@ +# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + +from typing import Optional, Tuple + +import torch +import random +import ttnn + +from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time + +# Override the default timeout in seconds for hang detection. +TIMEOUT = 30 + +random.seed(0) + + +# Parameters provided to the test vector generator are defined here. +# They are defined as dict-type suites that contain the arguments to the run function as keys, and lists of possible inputs as values. +# Each suite has a key name (in this case "suite_1") which will associate the test vectors to this specific suite of inputs. +# Developers can create their own generator functions and pass them to the parameters as inputs. +parameters = { + "nightly": { + "batch_sizes": [(1,)], + "height": [384, 1024], + "width": [1024, 4096], + "dtype": [ttnn.bfloat16, ttnn.bfloat8_b], + "input_memory_config": [ttnn.DRAM_MEMORY_CONFIG], + "output_memory_config": [ttnn.DRAM_MEMORY_CONFIG], + "layout": [ttnn.TILE_LAYOUT, ttnn.ROW_MAJOR_LAYOUT], + "negative_slope": [-0.5, 0, 0.01, 0.5], + }, +} + + +# Invalidate vector is called during the generation phase where each vector will be passed in. +# If invalidated, the vector will still be stored but will be skipped. +# Returns False, None if the vector is valid, and True, str with a reason for invalidation if it is invalid. +def invalidate_vector(test_vector) -> Tuple[bool, Optional[str]]: + if test_vector["layout"] == ttnn.ROW_MAJOR_LAYOUT: + return True, "Row Major layout is not supported" + return False, None + + +# This is the run instructions for the test, defined by the developer. +# The run function must take the above-defined parameters as inputs. +# The runner will call this run function with each test vector, and the returned results from this function will be stored. +# If you defined a device_mesh_fixture above, the object you yielded will be passed into this function as 'device'. Otherwise, it will be the default ttnn device opened by the infra. +def run( + batch_sizes, + height, + width, + dtype, + input_memory_config, + output_memory_config, + layout, + negative_slope, + *, + device, +) -> list: + input_shape = (*batch_sizes, height, width) + + torch_input_tensor = torch.randn(input_shape, dtype=torch.float32) + torch_output_tensor = torch.nn.functional.leaky_relu(torch_input_tensor, negative_slope) + + input_tensor = ttnn.from_torch( + torch_input_tensor, dtype=dtype, device=device, memory_config=input_memory_config, layout=layout + ) + + start_time = start_measuring_time() + result = ttnn.leaky_relu(input_tensor, negative_slope, memory_config=output_memory_config) + output_tensor = ttnn.to_torch(result) + e2e_perf = stop_measuring_time(start_time) + + return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf] diff --git a/tests/ttnn/sweep_tests/sweeps/sweeps/leaky_relu.py b/tests/ttnn/sweep_tests/sweeps/sweeps/leaky_relu.py deleted file mode 100644 index e98de16ad767..000000000000 --- a/tests/ttnn/sweep_tests/sweeps/sweeps/leaky_relu.py +++ /dev/null @@ -1,55 +0,0 @@ -# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. - -# SPDX-License-Identifier: Apache-2.0 - -from typing import Optional, Tuple - -import torch -import torch.nn.functional as F - -import ttnn - -from tests.ttnn.utils_for_testing import check_with_pcc -from models.utility_functions import torch_random - - -parameters = { - "batch_sizes": [(1,)], - "height": [384, 1024], - "width": [1024, 4096], - "input_dtype": [ttnn.bfloat16], - "input_memory_config": [ttnn.DRAM_MEMORY_CONFIG], - "output_memory_config": [ttnn.DRAM_MEMORY_CONFIG], - "layout": [ttnn.TILE_LAYOUT], - "negative_slope": [-0.5, 0, 0.01, 0.5], -} - - -def run( - batch_sizes, - height, - width, - input_dtype, - input_memory_config, - output_memory_config, - layout, - negative_slope, - *, - device, -) -> Tuple[bool, Optional[str]]: - input_shape = (*batch_sizes, height, width) - - low = -100.0 - high = 100.0 - - torch_input_tensor = torch_random(input_shape, low, high, dtype=torch.float32) - torch_output_tensor = F.leaky_relu(torch_input_tensor, negative_slope) - - input_tensor = ttnn.from_torch( - torch_input_tensor, dtype=input_dtype, device=device, layout=layout, memory_config=input_memory_config - ) - - output_tensor = ttnn.leaky_relu(input_tensor, negative_slope, memory_config=output_memory_config) - output_tensor = ttnn.to_torch(output_tensor) - - return check_with_pcc(torch_output_tensor, output_tensor, 0.999) diff --git a/ttnn/cpp/ttnn/operations/eltwise/unary/unary_pybind.hpp b/ttnn/cpp/ttnn/operations/eltwise/unary/unary_pybind.hpp index 7191b492d440..5587443db460 100644 --- a/ttnn/cpp/ttnn/operations/eltwise/unary/unary_pybind.hpp +++ b/ttnn/cpp/ttnn/operations/eltwise/unary/unary_pybind.hpp @@ -1506,7 +1506,16 @@ void py_module(py::module& module) { )doc"); detail::bind_unary_operation_with_float_parameter(module, ttnn::heaviside, "value", "The value parameter for the Heaviside function", ""); - detail::bind_unary_operation_with_float_parameter(module, ttnn::leaky_relu, "negative_slope", "The slope parameter for the Leaky ReLU function", ""); + detail::bind_unary_operation_with_float_parameter(module, ttnn::leaky_relu, "slope", "The slope parameter for the Leaky ReLU function", "", + R"doc(Supported dtypes, layouts, and ranks: + + +----------------------------+---------------------------------+-------------------+ + | Dtypes | Layouts | Ranks | + +----------------------------+---------------------------------+-------------------+ + | BFLOAT16, BFLOAT8_B | TILE | 2, 3, 4 | + +----------------------------+---------------------------------+-------------------+ + )doc"); + detail::bind_unary_operation_with_float_parameter(module, ttnn::relu_max, "upper_limit", "The max value for ReLU function", "This function caps off the input to a max value and a min value of 0"); detail::bind_unary_operation_with_float_parameter(module, ttnn::relu_min, "lower_limit", "The min value for ReLU function", "This will carry out ReLU operation at min value instead of the standard 0");