Skip to content

Commit

Permalink
#8157: Update doc and move sweep test for leaky_relu
Browse files Browse the repository at this point in the history
  • Loading branch information
mcw-anasuya committed Oct 4, 2024
1 parent 0fb4249 commit 7359458
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 66 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ttnn-run-sweeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ on:
- eltwise.unary.hardswish.hardswish
- eltwise.unary.hardsigmoid.hardsigmoid
- eltwise.unary.hardshrink.hardshrink
- eltwise.unary.leaky_relu.leaky_relu
- eltwise.binary.subtract.subtract
- eltwise.binary.multiply.multiply
- eltwise.binary.div.div
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
55 changes: 0 additions & 55 deletions tests/ttnn/sweep_tests/sweeps/sweeps/leaky_relu.py

This file was deleted.

34 changes: 23 additions & 11 deletions ttnn/cpp/ttnn/operations/eltwise/unary/unary_pybind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ void bind_unary_operation_with_float_parameter(
const unary_operation_t& operation,
const std::string& parameter_name,
const std::string& parameter_doc,
const std::string& info_doc) {
const std::string& info_doc,
const std::string& note=" ") {
auto doc = fmt::format(
R"doc(
Applies {0} to :attr:`input_tensor` element-wise.
Expand All @@ -275,7 +276,7 @@ void bind_unary_operation_with_float_parameter(
ttnn.Tensor: the output tensor.
Note:
{4}
{5}
Example:
>>> tensor = ttnn.from_torch(torch.tensor((1, 2), dtype=torch.bfloat16), device=device)
Expand All @@ -285,7 +286,8 @@ void bind_unary_operation_with_float_parameter(
operation.python_fully_qualified_name(),
parameter_name,
parameter_doc,
info_doc);
info_doc,
note);

bind_registered_operation(
module,
Expand Down Expand Up @@ -1410,18 +1412,28 @@ void py_module(py::module& module) {
detail::bind_unary_operation_with_fast_and_approximate_mode(module, ttnn::rsqrt);

// Unaries with float parameter
detail::bind_unary_operation_with_float_parameter(module, ttnn::elu, "alpha", "The alpha parameter for the ELU function",
R"doc(Supported dtypes, layouts, and ranks:
detail::bind_unary_operation_with_float_parameter(module, ttnn::elu, "alpha", "The alpha parameter for the ELU function", "",
R"doc(Supported dtypes, layouts, and ranks:
+----------------------------+---------------------------------+-------------------+
| Dtypes | Layouts | Ranks |
+----------------------------+---------------------------------+-------------------+
| BFLOAT16, BFLOAT8_B | TILE | 2, 3, 4 |
+----------------------------+---------------------------------+-------------------+)doc");
+----------------------------+---------------------------------+-------------------+
| Dtypes | Layouts | Ranks |
+----------------------------+---------------------------------+-------------------+
| BFLOAT16, BFLOAT8_B | TILE | 2, 3, 4 |
+----------------------------+---------------------------------+-------------------+
)doc");

detail::bind_unary_operation_with_float_parameter(module, ttnn::rsub, "value", "subtrahent value which is actually calculated as minuend", "Returns tensor with respective elements of the input tensor subtracted from the value.");
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, "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");

Expand Down

0 comments on commit 7359458

Please sign in to comment.