From 1fee28919520e07141d07dbb34cab4c8329f294f Mon Sep 17 00:00:00 2001 From: umadevimcw Date: Mon, 23 Dec 2024 12:52:13 +0000 Subject: [PATCH] #11512: Add sweeps for eltwise sharded ops --- .github/workflows/ttnn-run-sweeps.yaml | 8 ++ .../sweeps/eltwise/unary/cos/cos_sharded.py | 110 ++++++++++++++ .../sweeps/eltwise/unary/cosh/cosh_sharded.py | 110 ++++++++++++++ .../eltwise/unary/deg2rad/deg2rad_sharded.py | 110 ++++++++++++++ .../eltwise/unary/log10/log10_sharded.py | 136 ++++++++++++++++++ .../eltwise/unary/log1p/log1p_sharded.py | 112 +++++++++++++++ .../sweeps/eltwise/unary/log2/log2_sharded.py | 110 ++++++++++++++ .../sweeps/eltwise/unary/nez/nez_sharded.py | 110 ++++++++++++++ .../eltwise/unary/relu6/relu6_sharded.py | 110 ++++++++++++++ .../unary/device/unary_composite_op.cpp | 6 +- 10 files changed, 918 insertions(+), 4 deletions(-) create mode 100644 tests/sweep_framework/sweeps/eltwise/unary/cos/cos_sharded.py create mode 100644 tests/sweep_framework/sweeps/eltwise/unary/cosh/cosh_sharded.py create mode 100644 tests/sweep_framework/sweeps/eltwise/unary/deg2rad/deg2rad_sharded.py create mode 100644 tests/sweep_framework/sweeps/eltwise/unary/log10/log10_sharded.py create mode 100644 tests/sweep_framework/sweeps/eltwise/unary/log1p/log1p_sharded.py create mode 100644 tests/sweep_framework/sweeps/eltwise/unary/log2/log2_sharded.py create mode 100644 tests/sweep_framework/sweeps/eltwise/unary/nez/nez_sharded.py create mode 100644 tests/sweep_framework/sweeps/eltwise/unary/relu6/relu6_sharded.py diff --git a/.github/workflows/ttnn-run-sweeps.yaml b/.github/workflows/ttnn-run-sweeps.yaml index f789e3e678f..6f7df7b4e58 100644 --- a/.github/workflows/ttnn-run-sweeps.yaml +++ b/.github/workflows/ttnn-run-sweeps.yaml @@ -21,6 +21,14 @@ on: - creation.zeros.zeros - creation.empty.empty - creation.zeros_like.zeros_like + - eltwise.unary.cos.cos_sharded + - eltwise.unary.cosh.cosh_sharded + - eltwise.unary.deg2rad.deg2rad_sharded + - eltwise.unary.log1p.log1p_sharded + - eltwise.unary.log10.log10_sharded + - eltwise.unary.log2.log2_sharded + - eltwise.unary.nez.nez_sharded + - eltwise.unary.relu6.relu6_sharded - eltwise.unary.abs.abs_pytorch2 - eltwise.unary.relu.relu - eltwise.unary.relu.relu_pytorch2 diff --git a/tests/sweep_framework/sweeps/eltwise/unary/cos/cos_sharded.py b/tests/sweep_framework/sweeps/eltwise/unary/cos/cos_sharded.py new file mode 100644 index 00000000000..52643d48be5 --- /dev/null +++ b/tests/sweep_framework/sweeps/eltwise/unary/cos/cos_sharded.py @@ -0,0 +1,110 @@ +# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + +from typing import Optional, Tuple +from functools import partial + +import json +import torch +import random +import ttnn +import math +from tests.sweep_framework.sweep_utils.utils import gen_shapes, sanitize_shape_rm +from tests.sweep_framework.sweep_utils.sharding_utils import ( + gen_sharded_spec_unary, + parse_sharding_spec, + invalidate_vector_sharding, +) +from tests.tt_eager.python_api_testing.sweep_tests.generation_funcs import gen_func_with_cast_tt +from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time +from models.utility_functions import torch_random + +# Override the default timeout in seconds for hang detection. +TIMEOUT = 120 + +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" and "suite_2") 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": { + "input_spec": gen_sharded_spec_unary(16, layouts=["TILE_LAYOUT"]), + "input_a_dtype": [ttnn.bfloat16, ttnn.bfloat8_b], + }, +} + + +# 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]]: + input_layout = test_vector["input_spec"]["input_layout"] + sharding_invalidated, output_str = invalidate_vector_sharding(test_vector["input_spec"]) + if input_layout == "ROW_MAJOR_LAYOUT": + return True, "Inputs to eltwise binary must be tilized" + if sharding_invalidated: + return sharding_invalidated, output_str + 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 mesh_device_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( + input_spec, + input_a_dtype, + *, + device, +) -> list: + data_seed = random.randint(0, 20000000) + torch.manual_seed(data_seed) + + ( + input_shape, + core_grid, + sharding_strategy, + shard_orientation, + tensor_hw_as_shard_shape, + input_layout, + shard_height_mul_of_32, + ) = parse_sharding_spec(input_spec) + + if input_layout == ttnn.ROW_MAJOR_LAYOUT: + input_shape = sanitize_shape_rm(input_shape) + + sharded_config = ttnn.create_sharded_memory_config_( + shape=input_shape, + core_grid=core_grid, + strategy=sharding_strategy, + orientation=shard_orientation, + use_height_and_width_as_shard_shape=tensor_hw_as_shard_shape, + tile_layout=shard_height_mul_of_32, + ) + + 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.cos) + torch_output_tensor = golden_function(torch_input_tensor_a) + + input_tensor_a = ttnn.from_torch( + torch_input_tensor_a, + dtype=input_a_dtype, + layout=input_layout, + device=device, + memory_config=sharded_config, + ) + + start_time = start_measuring_time() + output_tensor = ttnn.cos(input_tensor_a, memory_config=sharded_config) + e2e_perf = stop_measuring_time(start_time) + + output_tensor = ttnn.to_torch(output_tensor) + + return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf] diff --git a/tests/sweep_framework/sweeps/eltwise/unary/cosh/cosh_sharded.py b/tests/sweep_framework/sweeps/eltwise/unary/cosh/cosh_sharded.py new file mode 100644 index 00000000000..27f0663d962 --- /dev/null +++ b/tests/sweep_framework/sweeps/eltwise/unary/cosh/cosh_sharded.py @@ -0,0 +1,110 @@ +# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + +from typing import Optional, Tuple +from functools import partial + +import json +import torch +import random +import ttnn +import math +from tests.sweep_framework.sweep_utils.utils import gen_shapes, sanitize_shape_rm +from tests.sweep_framework.sweep_utils.sharding_utils import ( + gen_sharded_spec_unary, + parse_sharding_spec, + invalidate_vector_sharding, +) +from tests.tt_eager.python_api_testing.sweep_tests.generation_funcs import gen_func_with_cast_tt +from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time +from models.utility_functions import torch_random + +# Override the default timeout in seconds for hang detection. +TIMEOUT = 120 + +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" and "suite_2") 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": { + "input_spec": gen_sharded_spec_unary(42, layouts=["TILE_LAYOUT"]), + "input_a_dtype": [ttnn.bfloat16], + }, +} + + +# 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]]: + input_layout = test_vector["input_spec"]["input_layout"] + sharding_invalidated, output_str = invalidate_vector_sharding(test_vector["input_spec"]) + if input_layout == "ROW_MAJOR_LAYOUT": + return True, "Inputs to eltwise binary must be tilized" + if sharding_invalidated: + return sharding_invalidated, output_str + 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 mesh_device_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( + input_spec, + input_a_dtype, + *, + device, +) -> list: + data_seed = random.randint(0, 20000000) + torch.manual_seed(data_seed) + + ( + input_shape, + core_grid, + sharding_strategy, + shard_orientation, + tensor_hw_as_shard_shape, + input_layout, + shard_height_mul_of_32, + ) = parse_sharding_spec(input_spec) + + if input_layout == ttnn.ROW_MAJOR_LAYOUT: + input_shape = sanitize_shape_rm(input_shape) + + sharded_config = ttnn.create_sharded_memory_config_( + shape=input_shape, + core_grid=core_grid, + strategy=sharding_strategy, + orientation=shard_orientation, + use_height_and_width_as_shard_shape=tensor_hw_as_shard_shape, + tile_layout=shard_height_mul_of_32, + ) + + torch_input_tensor_a = gen_func_with_cast_tt( + partial(torch_random, low=-9, high=9, dtype=torch.float32), input_a_dtype + )(input_shape) + + golden_function = ttnn.get_golden_function(ttnn.cosh) + torch_output_tensor = golden_function(torch_input_tensor_a) + + input_tensor_a = ttnn.from_torch( + torch_input_tensor_a, + dtype=input_a_dtype, + layout=input_layout, + device=device, + memory_config=sharded_config, + ) + + start_time = start_measuring_time() + output_tensor = ttnn.cosh(input_tensor_a, memory_config=sharded_config) + e2e_perf = stop_measuring_time(start_time) + + output_tensor = ttnn.to_torch(output_tensor) + + return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf] diff --git a/tests/sweep_framework/sweeps/eltwise/unary/deg2rad/deg2rad_sharded.py b/tests/sweep_framework/sweeps/eltwise/unary/deg2rad/deg2rad_sharded.py new file mode 100644 index 00000000000..7ad5be9655b --- /dev/null +++ b/tests/sweep_framework/sweeps/eltwise/unary/deg2rad/deg2rad_sharded.py @@ -0,0 +1,110 @@ +# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + +from typing import Optional, Tuple +from functools import partial + +import json +import torch +import random +import ttnn +import math +from tests.sweep_framework.sweep_utils.utils import gen_shapes, sanitize_shape_rm +from tests.sweep_framework.sweep_utils.sharding_utils import ( + gen_sharded_spec_unary, + parse_sharding_spec, + invalidate_vector_sharding, +) +from tests.tt_eager.python_api_testing.sweep_tests.generation_funcs import gen_func_with_cast_tt +from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time +from models.utility_functions import torch_random + +# Override the default timeout in seconds for hang detection. +TIMEOUT = 120 + +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" and "suite_2") 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": { + "input_spec": gen_sharded_spec_unary(40, layouts=["TILE_LAYOUT"]), + "input_a_dtype": [ttnn.bfloat16], + }, +} + + +# 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]]: + input_layout = test_vector["input_spec"]["input_layout"] + sharding_invalidated, output_str = invalidate_vector_sharding(test_vector["input_spec"]) + if input_layout == "ROW_MAJOR_LAYOUT": + return True, "Inputs to eltwise binary must be tilized" + if sharding_invalidated: + return sharding_invalidated, output_str + 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 mesh_device_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( + input_spec, + input_a_dtype, + *, + device, +) -> list: + data_seed = random.randint(0, 20000000) + torch.manual_seed(data_seed) + + ( + input_shape, + core_grid, + sharding_strategy, + shard_orientation, + tensor_hw_as_shard_shape, + input_layout, + shard_height_mul_of_32, + ) = parse_sharding_spec(input_spec) + + if input_layout == ttnn.ROW_MAJOR_LAYOUT: + input_shape = sanitize_shape_rm(input_shape) + + sharded_config = ttnn.create_sharded_memory_config_( + shape=input_shape, + core_grid=core_grid, + strategy=sharding_strategy, + orientation=shard_orientation, + use_height_and_width_as_shard_shape=tensor_hw_as_shard_shape, + tile_layout=shard_height_mul_of_32, + ) + + 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.deg2rad) + torch_output_tensor = golden_function(torch_input_tensor_a) + + input_tensor_a = ttnn.from_torch( + torch_input_tensor_a, + dtype=input_a_dtype, + layout=input_layout, + device=device, + memory_config=sharded_config, + ) + + start_time = start_measuring_time() + output_tensor = ttnn.deg2rad(input_tensor_a, memory_config=sharded_config) + e2e_perf = stop_measuring_time(start_time) + + output_tensor = ttnn.to_torch(output_tensor) + + return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf] diff --git a/tests/sweep_framework/sweeps/eltwise/unary/log10/log10_sharded.py b/tests/sweep_framework/sweeps/eltwise/unary/log10/log10_sharded.py new file mode 100644 index 00000000000..d795a90f70b --- /dev/null +++ b/tests/sweep_framework/sweeps/eltwise/unary/log10/log10_sharded.py @@ -0,0 +1,136 @@ +# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + +from typing import Optional, Tuple +from functools import partial + +import json +import torch +import random +import ttnn +import math +from tests.sweep_framework.sweep_utils.utils import gen_shapes, sanitize_shape_rm +from tests.sweep_framework.sweep_utils.sharding_utils import ( + gen_sharded_spec_unary, + parse_sharding_spec, + invalidate_vector_sharding, +) +from tests.tt_eager.python_api_testing.sweep_tests.generation_funcs import gen_func_with_cast_tt +from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time +from models.utility_functions import torch_random + +# Override the default timeout in seconds for hang detection. +TIMEOUT = 120 + +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" and "suite_2") 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": { + "input_spec": gen_sharded_spec_unary(16, layouts=["TILE_LAYOUT"]), + "input_a_dtype": [ttnn.bfloat16, ttnn.bfloat8_b], + }, +} + + +# 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]]: + input_layout = test_vector["input_spec"]["input_layout"] + sharding_invalidated, output_str = invalidate_vector_sharding(test_vector["input_spec"]) + if input_layout == "ROW_MAJOR_LAYOUT": + return True, "Inputs to eltwise binary must be tilized" + if sharding_invalidated: + return sharding_invalidated, output_str + 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 mesh_device_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( + input_spec, + input_a_dtype, + *, + device, +) -> list: + data_seed = random.randint(0, 20000000) + torch.manual_seed(data_seed) + + ( + input_shape, + core_grid, + sharding_strategy, + shard_orientation, + tensor_hw_as_shard_shape, + input_layout, + shard_height_mul_of_32, + ) = parse_sharding_spec(input_spec) + + if input_layout == ttnn.ROW_MAJOR_LAYOUT: + input_shape = sanitize_shape_rm(input_shape) + + sharded_config = ttnn.create_sharded_memory_config_( + shape=input_shape, + core_grid=core_grid, + strategy=sharding_strategy, + orientation=shard_orientation, + use_height_and_width_as_shard_shape=tensor_hw_as_shard_shape, + tile_layout=shard_height_mul_of_32, + ) + + 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.log10) + torch_output_tensor = golden_function(torch_input_tensor_a) + + input_tensor_a = ttnn.from_torch( + torch_input_tensor_a, + dtype=input_a_dtype, + layout=input_layout, + device=device, + memory_config=sharded_config, + ) + + start_time = start_measuring_time() + output_tensor = ttnn.log10(input_tensor_a, memory_config=sharded_config) + e2e_perf = stop_measuring_time(start_time) + + output_tensor = ttnn.to_torch(output_tensor) + + return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf] + + +from tests.sweep_framework.framework.permutations import * + +start_time = start_measuring_time() +for suite in parameters.keys(): + device_id = 0 + device = ttnn.open_device(device_id=device_id) + suite_vectors = list(permutations(parameters[suite])) + print(len(suite_vectors)) + failed = 0 + crashed = 0 + for vector in suite_vectors: + try: + passed, _ = run(**vector, device=device) + if passed[0] != True: + print(passed) + failed += 1 + except Exception as e: + print(e) + crashed += 1 + +passed = len(suite_vectors) - failed - crashed +percent_passed = (passed / len(suite_vectors)) * 100 +print(f"{crashed} crash, {failed} fail, {passed} pass ({percent_passed}%)") +ttnn.close_device(device) diff --git a/tests/sweep_framework/sweeps/eltwise/unary/log1p/log1p_sharded.py b/tests/sweep_framework/sweeps/eltwise/unary/log1p/log1p_sharded.py new file mode 100644 index 00000000000..b0d7b85dcf6 --- /dev/null +++ b/tests/sweep_framework/sweeps/eltwise/unary/log1p/log1p_sharded.py @@ -0,0 +1,112 @@ +# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + +from typing import Optional, Tuple +from functools import partial + +import json +import torch +import random +import ttnn +import math +from tests.sweep_framework.sweep_utils.utils import gen_shapes, sanitize_shape_rm +from tests.sweep_framework.sweep_utils.sharding_utils import ( + gen_sharded_spec_unary, + parse_sharding_spec, + invalidate_vector_sharding, +) +from tests.tt_eager.python_api_testing.sweep_tests.generation_funcs import gen_func_with_cast_tt +from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time +from models.utility_functions import torch_random + +# Override the default timeout in seconds for hang detection. +TIMEOUT = 120 + +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" and "suite_2") 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": { + "input_spec": gen_sharded_spec_unary(40, layouts=["TILE_LAYOUT"]), + "input_a_dtype": [ + ttnn.bfloat16, + ], + }, +} + + +# 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]]: + input_layout = test_vector["input_spec"]["input_layout"] + sharding_invalidated, output_str = invalidate_vector_sharding(test_vector["input_spec"]) + if input_layout == "ROW_MAJOR_LAYOUT": + return True, "Inputs to eltwise binary must be tilized" + if sharding_invalidated: + return sharding_invalidated, output_str + 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 mesh_device_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( + input_spec, + input_a_dtype, + *, + device, +) -> list: + data_seed = random.randint(0, 20000000) + torch.manual_seed(data_seed) + + ( + input_shape, + core_grid, + sharding_strategy, + shard_orientation, + tensor_hw_as_shard_shape, + input_layout, + shard_height_mul_of_32, + ) = parse_sharding_spec(input_spec) + + if input_layout == ttnn.ROW_MAJOR_LAYOUT: + input_shape = sanitize_shape_rm(input_shape) + + sharded_config = ttnn.create_sharded_memory_config_( + shape=input_shape, + core_grid=core_grid, + strategy=sharding_strategy, + orientation=shard_orientation, + use_height_and_width_as_shard_shape=tensor_hw_as_shard_shape, + tile_layout=shard_height_mul_of_32, + ) + + torch_input_tensor_a = gen_func_with_cast_tt( + partial(torch_random, low=-1, high=1, dtype=torch.float32), input_a_dtype + )(input_shape) + + golden_function = ttnn.get_golden_function(ttnn.log1p) + torch_output_tensor = golden_function(torch_input_tensor_a) + + input_tensor_a = ttnn.from_torch( + torch_input_tensor_a, + dtype=input_a_dtype, + layout=input_layout, + device=device, + memory_config=sharded_config, + ) + + start_time = start_measuring_time() + output_tensor = ttnn.log1p(input_tensor_a, memory_config=sharded_config) + e2e_perf = stop_measuring_time(start_time) + + output_tensor = ttnn.to_torch(output_tensor) + + return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf] diff --git a/tests/sweep_framework/sweeps/eltwise/unary/log2/log2_sharded.py b/tests/sweep_framework/sweeps/eltwise/unary/log2/log2_sharded.py new file mode 100644 index 00000000000..9d8473d81a1 --- /dev/null +++ b/tests/sweep_framework/sweeps/eltwise/unary/log2/log2_sharded.py @@ -0,0 +1,110 @@ +# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + +from typing import Optional, Tuple +from functools import partial + +import json +import torch +import random +import ttnn +import math +from tests.sweep_framework.sweep_utils.utils import gen_shapes, sanitize_shape_rm +from tests.sweep_framework.sweep_utils.sharding_utils import ( + gen_sharded_spec_unary, + parse_sharding_spec, + invalidate_vector_sharding, +) +from tests.tt_eager.python_api_testing.sweep_tests.generation_funcs import gen_func_with_cast_tt +from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time +from models.utility_functions import torch_random + +# Override the default timeout in seconds for hang detection. +TIMEOUT = 120 + +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" and "suite_2") 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": { + "input_spec": gen_sharded_spec_unary(16, layouts=["TILE_LAYOUT"]), + "input_a_dtype": [ttnn.bfloat16, ttnn.bfloat8_b], + }, +} + + +# 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]]: + input_layout = test_vector["input_spec"]["input_layout"] + sharding_invalidated, output_str = invalidate_vector_sharding(test_vector["input_spec"]) + if input_layout == "ROW_MAJOR_LAYOUT": + return True, "Inputs to eltwise binary must be tilized" + if sharding_invalidated: + return sharding_invalidated, output_str + 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 mesh_device_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( + input_spec, + input_a_dtype, + *, + device, +) -> list: + data_seed = random.randint(0, 20000000) + torch.manual_seed(data_seed) + + ( + input_shape, + core_grid, + sharding_strategy, + shard_orientation, + tensor_hw_as_shard_shape, + input_layout, + shard_height_mul_of_32, + ) = parse_sharding_spec(input_spec) + + if input_layout == ttnn.ROW_MAJOR_LAYOUT: + input_shape = sanitize_shape_rm(input_shape) + + sharded_config = ttnn.create_sharded_memory_config_( + shape=input_shape, + core_grid=core_grid, + strategy=sharding_strategy, + orientation=shard_orientation, + use_height_and_width_as_shard_shape=tensor_hw_as_shard_shape, + tile_layout=shard_height_mul_of_32, + ) + + 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.log2) + torch_output_tensor = golden_function(torch_input_tensor_a) + + input_tensor_a = ttnn.from_torch( + torch_input_tensor_a, + dtype=input_a_dtype, + layout=input_layout, + device=device, + memory_config=sharded_config, + ) + + start_time = start_measuring_time() + output_tensor = ttnn.log2(input_tensor_a, memory_config=sharded_config) + e2e_perf = stop_measuring_time(start_time) + + output_tensor = ttnn.to_torch(output_tensor) + + return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf] diff --git a/tests/sweep_framework/sweeps/eltwise/unary/nez/nez_sharded.py b/tests/sweep_framework/sweeps/eltwise/unary/nez/nez_sharded.py new file mode 100644 index 00000000000..c25b3631508 --- /dev/null +++ b/tests/sweep_framework/sweeps/eltwise/unary/nez/nez_sharded.py @@ -0,0 +1,110 @@ +# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + +from typing import Optional, Tuple +from functools import partial + +import json +import torch +import random +import ttnn +import math +from tests.sweep_framework.sweep_utils.utils import gen_shapes, sanitize_shape_rm +from tests.sweep_framework.sweep_utils.sharding_utils import ( + gen_sharded_spec_unary, + parse_sharding_spec, + invalidate_vector_sharding, +) +from tests.tt_eager.python_api_testing.sweep_tests.generation_funcs import gen_func_with_cast_tt +from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time +from models.utility_functions import torch_random + +# Override the default timeout in seconds for hang detection. +TIMEOUT = 120 + +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" and "suite_2") 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": { + "input_spec": gen_sharded_spec_unary(16, layouts=["TILE_LAYOUT"]), + "input_a_dtype": [ttnn.bfloat16, ttnn.bfloat8_b], + }, +} + + +# 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]]: + input_layout = test_vector["input_spec"]["input_layout"] + sharding_invalidated, output_str = invalidate_vector_sharding(test_vector["input_spec"]) + if input_layout == "ROW_MAJOR_LAYOUT": + return True, "Inputs to eltwise binary must be tilized" + if sharding_invalidated: + return sharding_invalidated, output_str + 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 mesh_device_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( + input_spec, + input_a_dtype, + *, + device, +) -> list: + data_seed = random.randint(0, 20000000) + torch.manual_seed(data_seed) + + ( + input_shape, + core_grid, + sharding_strategy, + shard_orientation, + tensor_hw_as_shard_shape, + input_layout, + shard_height_mul_of_32, + ) = parse_sharding_spec(input_spec) + + if input_layout == ttnn.ROW_MAJOR_LAYOUT: + input_shape = sanitize_shape_rm(input_shape) + + sharded_config = ttnn.create_sharded_memory_config_( + shape=input_shape, + core_grid=core_grid, + strategy=sharding_strategy, + orientation=shard_orientation, + use_height_and_width_as_shard_shape=tensor_hw_as_shard_shape, + tile_layout=shard_height_mul_of_32, + ) + + 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.nez) + torch_output_tensor = golden_function(torch_input_tensor_a) + + input_tensor_a = ttnn.from_torch( + torch_input_tensor_a, + dtype=input_a_dtype, + layout=input_layout, + device=device, + memory_config=sharded_config, + ) + + start_time = start_measuring_time() + output_tensor = ttnn.nez(input_tensor_a, memory_config=sharded_config) + e2e_perf = stop_measuring_time(start_time) + + output_tensor = ttnn.to_torch(output_tensor) + + return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf] diff --git a/tests/sweep_framework/sweeps/eltwise/unary/relu6/relu6_sharded.py b/tests/sweep_framework/sweeps/eltwise/unary/relu6/relu6_sharded.py new file mode 100644 index 00000000000..3517b25e5b6 --- /dev/null +++ b/tests/sweep_framework/sweeps/eltwise/unary/relu6/relu6_sharded.py @@ -0,0 +1,110 @@ +# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. + +# SPDX-License-Identifier: Apache-2.0 + +from typing import Optional, Tuple +from functools import partial + +import json +import torch +import random +import ttnn +import math +from tests.sweep_framework.sweep_utils.utils import gen_shapes, sanitize_shape_rm +from tests.sweep_framework.sweep_utils.sharding_utils import ( + gen_sharded_spec_unary, + parse_sharding_spec, + invalidate_vector_sharding, +) +from tests.tt_eager.python_api_testing.sweep_tests.generation_funcs import gen_func_with_cast_tt +from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time +from models.utility_functions import torch_random + +# Override the default timeout in seconds for hang detection. +TIMEOUT = 120 + +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" and "suite_2") 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": { + "input_spec": gen_sharded_spec_unary(16, layouts=["TILE_LAYOUT"]), + "input_a_dtype": [ttnn.bfloat16, ttnn.bfloat8_b], + }, +} + + +# 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]]: + input_layout = test_vector["input_spec"]["input_layout"] + sharding_invalidated, output_str = invalidate_vector_sharding(test_vector["input_spec"]) + if input_layout == "ROW_MAJOR_LAYOUT": + return True, "Inputs to eltwise binary must be tilized" + if sharding_invalidated: + return sharding_invalidated, output_str + 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 mesh_device_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( + input_spec, + input_a_dtype, + *, + device, +) -> list: + data_seed = random.randint(0, 20000000) + torch.manual_seed(data_seed) + + ( + input_shape, + core_grid, + sharding_strategy, + shard_orientation, + tensor_hw_as_shard_shape, + input_layout, + shard_height_mul_of_32, + ) = parse_sharding_spec(input_spec) + + if input_layout == ttnn.ROW_MAJOR_LAYOUT: + input_shape = sanitize_shape_rm(input_shape) + + sharded_config = ttnn.create_sharded_memory_config_( + shape=input_shape, + core_grid=core_grid, + strategy=sharding_strategy, + orientation=shard_orientation, + use_height_and_width_as_shard_shape=tensor_hw_as_shard_shape, + tile_layout=shard_height_mul_of_32, + ) + + 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.relu6) + torch_output_tensor = golden_function(torch_input_tensor_a) + + input_tensor_a = ttnn.from_torch( + torch_input_tensor_a, + dtype=input_a_dtype, + layout=input_layout, + device=device, + memory_config=sharded_config, + ) + + start_time = start_measuring_time() + output_tensor = ttnn.relu6(input_tensor_a, memory_config=sharded_config) + e2e_perf = stop_measuring_time(start_time) + + output_tensor = ttnn.to_torch(output_tensor) + + return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf] diff --git a/ttnn/cpp/ttnn/operations/eltwise/unary/device/unary_composite_op.cpp b/ttnn/cpp/ttnn/operations/eltwise/unary/device/unary_composite_op.cpp index b967bf65a05..ff25c6d0111 100644 --- a/ttnn/cpp/ttnn/operations/eltwise/unary/device/unary_composite_op.cpp +++ b/ttnn/cpp/ttnn/operations/eltwise/unary/device/unary_composite_op.cpp @@ -515,13 +515,11 @@ Tensor ExecuteUnaryCompositeClamp::invoke( } else if (min.value() > max.value()) { return full_like(a, max.value()); } - const Tensor h_const = full_like(a, max.value()); - Tensor a_max = ttnn::minimum(a, h_const, output_memory_config); + Tensor a_max = ttnn::minimum(a, max.value(), output_memory_config); if (min.value() == 0.0f) { return ttnn::relu(a_max, output_memory_config); } else { - const Tensor l_const = full_like(a, min.value()); - return ttnn::maximum(a_max, l_const, output_memory_config); + return ttnn::maximum(a_max, min.value(), output_memory_config); } }