Skip to content

Commit

Permalink
#8157: Add golden function
Browse files Browse the repository at this point in the history
  • Loading branch information
mcw-anasuya committed Oct 16, 2024
1 parent 2ad71d6 commit 471de1b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 25 deletions.
55 changes: 31 additions & 24 deletions tests/sweep_framework/sweeps/eltwise/unary/leaky_relu/leaky_relu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple
from functools import partial

import torch
import random
import ttnn

from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time
from tests.sweep_framework.utils import gen_shapes
from tests.tt_eager.python_api_testing.sweep_tests.generation_funcs import gen_func_with_cast_tt
from models.utility_functions import torch_random

# 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],
"leaky_relu11": {
"input_shape": gen_shapes([1, 1, 32, 32], [6, 12, 256, 256], [1, 1, 32, 32], 16),
"input_a_dtype": [ttnn.bfloat16, ttnn.bfloat8_b],
"input_a_layout": [ttnn.TILE_LAYOUT, ttnn.ROW_MAJOR_LAYOUT],
"input_a_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],
},
}
Expand All @@ -38,7 +38,7 @@
# 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:
if test_vector["input_a_layout"] == ttnn.ROW_MAJOR_LAYOUT:
return True, "Row Major layout is not supported"
return False, None

Expand All @@ -48,28 +48,35 @@ def invalidate_vector(test_vector) -> Tuple[bool, Optional[str]]:
# 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,
input_shape,
input_a_dtype,
input_a_layout,
input_a_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
data_seed = random.randint(0, 20000000)
torch.manual_seed(data_seed)

torch_input_tensor_a = gen_func_with_cast_tt(
partial(torch_random, low=-100, high=100, dtype=torch.float32), input_a_dtype
)(input_shape)

golden_function = ttnn.get_golden_function(ttnn.leaky_relu)
torch_output_tensor = golden_function(torch_input_tensor_a, negative_slope=negative_slope)

input_tensor_a = ttnn.from_torch(
torch_input_tensor_a,
dtype=input_a_dtype,
layout=input_a_layout,
device=device,
memory_config=input_a_memory_config,
)

start_time = start_measuring_time()
result = ttnn.leaky_relu(input_tensor, negative_slope, memory_config=output_memory_config)
result = ttnn.leaky_relu(input_tensor_a, negative_slope, memory_config=output_memory_config)
output_tensor = ttnn.to_torch(result)
e2e_perf = stop_measuring_time(start_time)

Expand Down
55 changes: 55 additions & 0 deletions tests/ttnn/sweep_tests/sweeps/sweeps/leaky_relu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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)
1 change: 0 additions & 1 deletion ttnn/cpp/ttnn/operations/eltwise/unary/unary_pybind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,6 @@ void py_module(py::module& module) {
)doc");


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:
Expand Down
9 changes: 9 additions & 0 deletions ttnn/ttnn/operations/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ def _golden_function_relu_min(input_tensor_a, *args, lower_limit, **kwargs):
return torch.max(input_tensor_a, torch.tensor(lower_limit))


def _golden_function_leaky_relu(input_tensor_a, *args, negative_slope, **kwargs):
import torch

return torch.nn.functional.leaky_relu(input_tensor_a, negative_slope)


ttnn.attach_golden_function(ttnn.leaky_relu, golden_function=_golden_function_leaky_relu)


ttnn.attach_golden_function(ttnn.relu_min, golden_function=_golden_function_relu_min)


Expand Down

0 comments on commit 471de1b

Please sign in to comment.