Skip to content

Commit

Permalink
#15857: Binary Forge Sweep Tests Set2 (#16087)
Browse files Browse the repository at this point in the history
### Ticket
#15857 

### Problem description
Add forge sweep tests for binary ops

### What's changed
Added automation script to generate forge sweep tests for the following
ops:
- eq
- ge
- gt
- lt
- ne
- logical_and
- logical_or
- div
- remainder
- where

### Checklist
- [ ] Post commit CI passes
  • Loading branch information
mcw-anasuya authored Dec 21, 2024
1 parent aa4ca74 commit 31dca41
Show file tree
Hide file tree
Showing 11 changed files with 1,420 additions and 1 deletion.
12 changes: 11 additions & 1 deletion .github/workflows/ttnn-run-sweeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -234,24 +234,31 @@ on:
- eltwise.binary.multiply.multiply_scalar_pytorch2
- eltwise.binary.div.div
- eltwise.binary.div.div_tensor_pytorch2
- eltwise.binary.div.div_forge
- eltwise.binary.div_no_nan.div_no_nan
- eltwise.binary.logical_or.logical_or_
- eltwise.binary.logical_or.logical_or
- eltwise.binary.logical_or.logical_or_output
- eltwise.binary.logical_or.logical_or_forge
- eltwise.binary.logical_xor.logical_xor_
- eltwise.binary.logical_xor.logical_xor
- eltwise.binary.logical_and.logical_and_
- eltwise.binary.logical_and.logical_and
- eltwise.binary.logical_and.logical_and_output
- eltwise.binary.logical_and.logical_and_forge
- eltwise.binary.polyval.polyval
- eltwise.binary.remainder.remainder
- eltwise.binary.remainder.remainder_scalar_pytorch2
- eltwise.binary.remainder.remainder_forge
- eltwise.binary.squared_difference.squared_difference
- eltwise.binary.squared_difference_output.squared_difference_output
- eltwise.binary.remainder.remainder_scalar_pytorch2
- eltwise.binary.bcast.bcast_h_sharded
- eltwise.binary.bcast.bcast
- eltwise.binary.eq.eq_scalar_pytorch2
- eltwise.binary.eq.eq_forge
- eltwise.binary.ge.ge_forge
- eltwise.binary.gt.gt_scalar_pytorch2
- eltwise.binary.gt.gt_forge
- eltwise.binary.le.le_tensor_pytorch2
- eltwise.binary.fmod.fmod
- eltwise.binary.floor_divide.floor_divide_pytorch2
Expand All @@ -260,7 +267,9 @@ on:
- eltwise.binary.ldexp.ldexp
- eltwise.binary.lt.lt_tensor_pytorch2
- eltwise.binary.lt.lt_scalar_pytorch2
- eltwise.binary.lt.lt_forge
- eltwise.binary.ne.ne_scalar_pytorch2
- eltwise.binary.ne.ne_forge
- eltwise.binary.hypot.hypot
- eltwise.binary.xlogy.xlogy
- eltwise.binary_backward.ldexp_bw.ldexp_bw
Expand Down Expand Up @@ -293,6 +302,7 @@ on:
- eltwise.ternary.lerp.lerp
- eltwise.ternary.where.where
- eltwise.ternary.where.where_pytorch2
- eltwise.ternary.where.where_forge
- eltwise.ternary_backward.addcmul_bw
- eltwise.ternary_backward.addcdiv_bw
- embedding.embedding
Expand Down
265 changes: 265 additions & 0 deletions tests/sweep_framework/sweeps/eltwise/binary/div/div_forge.py

Large diffs are not rendered by default.

104 changes: 104 additions & 0 deletions tests/sweep_framework/sweeps/eltwise/binary/eq/eq_forge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple
from functools import partial

import torch
import ttnn
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


# 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 = {
"xfail": {
"input_shape": [
{"self": [1, 1, 5, 5], "other": [1], "input_dtype": "ttnn.bfloat16"},
{"self": [1, 1, 7, 7], "other": [1], "input_dtype": "ttnn.bfloat16"},
{"self": [1, 45], "other": [1, 45], "input_dtype": "ttnn.int32"},
{"self": [1, 5], "other": [1, 5], "input_dtype": "ttnn.int32"},
{"self": [1, 6], "other": [1, 6], "input_dtype": "ttnn.int32"},
{"self": [1, 7], "other": [1, 7], "input_dtype": "ttnn.int32"},
],
"input_a_layout": [ttnn.TILE_LAYOUT],
"input_b_layout": [ttnn.TILE_LAYOUT],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
"input_b_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
},
}


# 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["input_a_layout"] == ttnn.ROW_MAJOR_LAYOUT or test_vector["input_b_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 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_shape,
input_a_layout,
input_b_layout,
input_a_memory_config,
input_b_memory_config,
output_memory_config,
*,
device,
) -> list:
torch.manual_seed(0)
if input_shape["input_dtype"] == "ttnn.bfloat16":
input_dtype = ttnn.bfloat16
elif input_shape["input_dtype"] == "ttnn.float32":
input_dtype = ttnn.float32
elif input_shape["input_dtype"] == "ttnn.int32":
input_dtype = ttnn.int32

torch_input_tensor_a = gen_func_with_cast_tt(
partial(torch_random, low=-100, high=100, dtype=torch.float32), input_dtype
)(input_shape["self"])
torch_input_tensor_b = gen_func_with_cast_tt(
partial(torch_random, low=-100, high=100, dtype=torch.float32), input_dtype
)(input_shape["other"])

golden_function = ttnn.get_golden_function(ttnn.eq)
torch_output_tensor = golden_function(torch_input_tensor_a, torch_input_tensor_b)

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

input_tensor_b = ttnn.from_torch(
torch_input_tensor_b,
dtype=input_dtype,
layout=input_b_layout,
device=device,
memory_config=input_b_memory_config,
)

start_time = start_measuring_time()
result = ttnn.eq(input_tensor_a, input_tensor_b, memory_config=output_memory_config)
# ToDo: Update it once the tensor layout support with rank < 2 is supported in mid of Jan
output_tensor = ttnn.to_torch(result, torch_rank=len(input_shape["self"]))
e2e_perf = stop_measuring_time(start_time)

return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf]
103 changes: 103 additions & 0 deletions tests/sweep_framework/sweeps/eltwise/binary/ge/ge_forge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple
from functools import partial

import torch
import ttnn
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


# 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 = {
"xfail": {
"input_shape": [[1]],
"input_a_dtype": [ttnn.int32],
"input_b_dtype": [ttnn.int32],
"input_a_layout": [ttnn.TILE_LAYOUT],
"input_b_layout": [ttnn.TILE_LAYOUT],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
"input_b_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
},
}


def mesh_device_fixture():
device = ttnn.open_device(device_id=0)
assert ttnn.device.is_wormhole_b0(device), "This op is available for Wormhole_B0 only"
yield (device, "Wormhole_B0")
ttnn.close_device(device)
del device


# 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["input_a_layout"] == ttnn.ROW_MAJOR_LAYOUT or test_vector["input_b_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 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_shape,
input_a_dtype,
input_b_dtype,
input_a_layout,
input_b_layout,
input_a_memory_config,
input_b_memory_config,
output_memory_config,
*,
device,
) -> list:
torch.manual_seed(0)

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

golden_function = ttnn.get_golden_function(ttnn.ge)
torch_output_tensor = golden_function(torch_input_tensor_a, torch_input_tensor_b)

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,
)

input_tensor_b = ttnn.from_torch(
torch_input_tensor_b,
dtype=input_b_dtype,
layout=input_b_layout,
device=device,
memory_config=input_b_memory_config,
)

start_time = start_measuring_time()
result = ttnn.ge(input_tensor_a, input_tensor_b, memory_config=output_memory_config)
# ToDo: Update it once the tensor layout support with rank < 2 is supported in mid of Jan
output_tensor = ttnn.to_torch(result, torch_rank=len(input_shape))
e2e_perf = stop_measuring_time(start_time)

return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf]
102 changes: 102 additions & 0 deletions tests/sweep_framework/sweeps/eltwise/binary/gt/gt_forge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple
from functools import partial

import torch
import ttnn
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


# 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 = {
"xfail": {
"input_shape": [[15, 15]],
"input_a_dtype": [ttnn.int32],
"input_b_dtype": [ttnn.int32],
"input_a_layout": [ttnn.TILE_LAYOUT],
"input_b_layout": [ttnn.TILE_LAYOUT],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
"input_b_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
},
}


def mesh_device_fixture():
device = ttnn.open_device(device_id=0)
assert ttnn.device.is_wormhole_b0(device), "This op is available for Wormhole_B0 only"
yield (device, "Wormhole_B0")
ttnn.close_device(device)
del device


# 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["input_a_layout"] == ttnn.ROW_MAJOR_LAYOUT or test_vector["input_b_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 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_shape,
input_a_dtype,
input_b_dtype,
input_a_layout,
input_b_layout,
input_a_memory_config,
input_b_memory_config,
output_memory_config,
*,
device,
) -> list:
torch.manual_seed(0)

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

golden_function = ttnn.get_golden_function(ttnn.gt)
torch_output_tensor = golden_function(torch_input_tensor_a, torch_input_tensor_b)

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,
)

input_tensor_b = ttnn.from_torch(
torch_input_tensor_b,
dtype=input_b_dtype,
layout=input_b_layout,
device=device,
memory_config=input_b_memory_config,
)

start_time = start_measuring_time()
result = ttnn.gt(input_tensor_a, input_tensor_b, 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]
Loading

0 comments on commit 31dca41

Please sign in to comment.