Skip to content

Commit

Permalink
Add bitwise ops sweeps, add gen_rand_bitwise_left_shift function (#13366
Browse files Browse the repository at this point in the history
)

* #11512: Add bitwise ops, add gen_rand_bitwise_left_shift function in sweep_framework/utils.py

* #11512: Minor fix

* #11512: Change bitwise ops names
  • Loading branch information
amalbasaTT authored Oct 2, 2024
1 parent 74af50a commit 893f708
Show file tree
Hide file tree
Showing 8 changed files with 548 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .github/workflows/ttnn-run-sweeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ on:
- eltwise.unary.log1p.log1p
- eltwise.unary.log2.log2
- eltwise.unary.log10.log10
- eltwise.unary.bitwise.bitwise_and
- eltwise.unary.bitwise.bitwise_left_shift
- eltwise.unary.bitwise.bitwise_not
- eltwise.unary.bitwise.bitwise_or
- eltwise.unary.bitwise.bitwise_right_shift
- eltwise.unary.bitwise.bitwise_xor
- eltwise.binary.subtract.subtract
- eltwise.binary.multiply.multipl
- eltwise.binary.multiply.multiply
- eltwise.binary.div.div
- eltwise.binary.div_no_nan.div_no_nan
- eltwise.binary.logical_or_.logical_or_
Expand Down
84 changes: 84 additions & 0 deletions tests/sweep_framework/sweeps/eltwise/unary/bitwise/bitwise_and.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple
from functools import partial

import torch
import random
import ttnn
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 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 = 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": {
"input_shape": gen_shapes([1, 1, 32, 32], [6, 12, 128, 128], [1, 1, 32, 32], 32)
+ gen_shapes([1, 32, 32], [12, 256, 256], [1, 32, 32], 32)
+ gen_shapes([32, 32], [256, 256], [32, 32], 32),
"input_a_dtype": [ttnn.int32],
"input_a_layout": [ttnn.TILE_LAYOUT],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_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


# 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(
input_shape,
input_a_dtype,
input_a_layout,
input_a_memory_config,
output_memory_config,
*,
device,
) -> list:
data_seed = random.randint(0, 20000000)
torch.manual_seed(data_seed)

torch_input_tensor_a = gen_func_with_cast_tt(
partial(torch_random, low=0, high=2147483648, dtype=torch.int64), input_a_dtype
)(input_shape)

scalar = torch.randint(0, 2147483648, (1,), dtype=torch.int32).item()

torch_output_tensor = torch.bitwise_and(torch_input_tensor_a, scalar)

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.bitwise_and(input_tensor_a, value=scalar, 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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple
from functools import partial

import torch
import random
import ttnn
from tests.sweep_framework.utils import gen_shapes, gen_rand_bitwise_left_shift
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 = 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": {
"input_shape": gen_shapes([1, 1, 32, 32], [6, 12, 128, 128], [1, 1, 32, 32], 4)
+ gen_shapes([1, 32, 32], [12, 256, 256], [1, 32, 32], 4)
+ gen_shapes([32, 32], [256, 256], [32, 32], 4),
"shift_bits": list(range(1, 31)),
"use_safe_nums": [True],
"input_a_dtype": [ttnn.int32],
"input_a_layout": [ttnn.TILE_LAYOUT],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_MEMORY_CONFIG],
},
"xfail": {
"input_shape": gen_shapes([1, 1, 32, 32], [6, 12, 128, 128], [1, 1, 32, 32], 4)
+ gen_shapes([1, 32, 32], [12, 256, 256], [1, 32, 32], 4)
+ gen_shapes([32, 32], [256, 256], [32, 32], 4),
"shift_bits": list(range(1, 31)),
"use_safe_nums": [False],
"input_a_dtype": [ttnn.int32],
"input_a_layout": [ttnn.TILE_LAYOUT],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_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


# 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(
input_shape,
shift_bits,
use_safe_nums,
input_a_dtype,
input_a_layout,
input_a_memory_config,
output_memory_config,
*,
device,
) -> list:
data_seed = random.randint(0, 20000000)
torch.manual_seed(data_seed)

# In ttnn.bitwise_left_shift, only bits from positions 0 to 30 are included during shifting (sign bit remains the same).
# That is not the case with torch.bitwise_left_shift, all of the bits are included during shifting.
# use_safe_nums argument makes sure that those two bits are the same, so the results between the two versions are the same.
if use_safe_nums is True:
torch_input_tensor_a = gen_func_with_cast_tt(
partial(gen_rand_bitwise_left_shift, shift_bits=shift_bits, low=-2147483647, high=2147483648), input_a_dtype
)(input_shape)
else:
torch_input_tensor_a = gen_func_with_cast_tt(
partial(torch_random, low=-2147483647, high=2147483648, dtype=torch.int64), input_a_dtype
)(input_shape)

torch_output_tensor = torch.bitwise_left_shift(torch_input_tensor_a, shift_bits).to(torch.int32)

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.bitwise_left_shift(input_tensor_a, shift_bits=shift_bits, memory_config=output_memory_config)
output_tensor = ttnn.to_torch(result).to(torch.int32)
e2e_perf = stop_measuring_time(start_time)

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

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple
from functools import partial

import torch
import random
import ttnn
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 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 = 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": {
"input_shape": gen_shapes([1, 1, 32, 32], [6, 12, 512, 512], [1, 1, 32, 32], 16)
+ gen_shapes([1, 32, 32], [12, 1024, 1024], [1, 32, 32], 16)
+ gen_shapes([32, 32], [1024, 1024], [32, 32], 16),
"input_a_dtype": [ttnn.int32],
"input_a_layout": [ttnn.TILE_LAYOUT],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_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


# 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(
input_shape,
input_a_dtype,
input_a_layout,
input_a_memory_config,
output_memory_config,
*,
device,
) -> list:
data_seed = random.randint(0, 20000000)
torch.manual_seed(data_seed)

torch_input_tensor_a = gen_func_with_cast_tt(
partial(torch_random, low=-2147483647, high=2147483648, dtype=torch.int64), input_a_dtype
)(input_shape)

torch_input_tensor_a = torch.full(size=input_shape, fill_value=-2147483647).to(torch.int32)

scalar = torch.randint(-100, 101, (1,)).item()

torch_output_tensor = torch.bitwise_not(torch_input_tensor_a)

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.bitwise_not(input_tensor_a, value=scalar, 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]
84 changes: 84 additions & 0 deletions tests/sweep_framework/sweeps/eltwise/unary/bitwise/bitwise_or.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple
from functools import partial

import torch
import random
import ttnn
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 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 = 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": {
"input_shape": gen_shapes([1, 1, 32, 32], [6, 12, 128, 128], [1, 1, 32, 32], 8)
+ gen_shapes([1, 32, 32], [12, 256, 256], [1, 32, 32], 8)
+ gen_shapes([32, 32], [256, 256], [32, 32], 8),
"input_a_dtype": [ttnn.int32],
"input_a_layout": [ttnn.TILE_LAYOUT],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_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


# 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(
input_shape,
input_a_dtype,
input_a_layout,
input_a_memory_config,
output_memory_config,
*,
device,
) -> list:
data_seed = random.randint(0, 20000000)
torch.manual_seed(data_seed)

torch_input_tensor_a = gen_func_with_cast_tt(
partial(torch_random, low=0, high=2147483648, dtype=torch.int64), input_a_dtype
)(input_shape)

scalar = torch.randint(0, 2147483648, (1,), dtype=torch.int32).item()

torch_output_tensor = torch.bitwise_or(torch_input_tensor_a, scalar)

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.bitwise_or(input_tensor_a, value=scalar, 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 893f708

Please sign in to comment.