Skip to content

Commit

Permalink
Add sweeps for complex unary ops: polar, angle, polar_bw and angle_bw (
Browse files Browse the repository at this point in the history
…#14041)

* #11512: Add sweeps for complex unary ops: polar, angle, polar_bw and angle_bw

* #11512: rebasing amalbasaTT/complex_ops-sweeps

* #11512: Minor fix
  • Loading branch information
amalbasaTT authored Oct 23, 2024
1 parent e8e25d1 commit e9272c1
Show file tree
Hide file tree
Showing 5 changed files with 478 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ttnn-run-sweeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ on:
- eltwise.unary.hardtanh.hardtanh_pytorch2
- eltwise.unary.leaky_relu.leaky_relu
- eltwise.unary.reglu.reglu
- eltwise.unary_complex.polar.polar
- eltwise.unary_complex.angle.angle
- eltwise.unary_complex.polar_bw.polar_bw
- eltwise.unary_complex.angle_bw.angle_bw
- eltwise.binary.subtract.subtract
- eltwise.binary.subtract.subtract_tensor_pytorch2
- eltwise.binary.multiply.multiply
Expand Down
103 changes: 103 additions & 0 deletions tests/sweep_framework/sweeps/eltwise/unary_complex/angle/angle.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 random
import ttnn
from tests.sweep_framework.sweep_utils.utils import gen_shapes, sanitize_shape_rm
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" 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": gen_shapes([1, 1, 1, 1], [6, 12, 256, 256], [1, 1, 1, 1], 16)
+ gen_shapes([1, 1, 1], [12, 256, 256], [1, 1, 1], 16)
+ gen_shapes([1, 1], [256, 256], [1, 1], 16),
"input_a_dtype": [ttnn.bfloat16],
"input_layout": [ttnn.TILE_LAYOUT, ttnn.ROW_MAJOR_LAYOUT],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_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_layout"] == ttnn.ROW_MAJOR_LAYOUT:
return True, "Inputs to eltwise binary must be tilized"
if test_vector["input_layout"] == ttnn.ROW_MAJOR_LAYOUT and test_vector["input_a_dtype"] == ttnn.bfloat8_b:
return True, "bfloat8_b is only supported on tiled layout"
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_layout,
input_a_memory_config,
output_memory_config,
*,
device,
) -> list:
data_seed = random.randint(0, 20000000)
torch.manual_seed(data_seed)

if input_layout == ttnn.ROW_MAJOR_LAYOUT:
input_shape = sanitize_shape_rm(input_shape)

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

golden_function = ttnn.get_golden_function(ttnn.angle)
torch_output_tensor = golden_function(torch_input_tensor_a)

input_tensor_a_real = ttnn.from_torch(
torch_real,
dtype=input_a_dtype,
layout=input_layout,
device=device,
memory_config=input_a_memory_config,
)
input_tensor_a_imag = ttnn.from_torch(
torch_imag,
dtype=input_a_dtype,
layout=input_layout,
device=device,
memory_config=input_a_memory_config,
)
input_tensor_a = ttnn.complex_tensor(input_tensor_a_real, input_tensor_a_imag)

start_time = start_measuring_time()
output_tensor = ttnn.angle(input_tensor_a, memory_config=output_memory_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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# 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.sweep_utils.utils import gen_shapes, sanitize_shape_rm
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" 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_shape": gen_shapes([1, 1, 1, 1], [6, 12, 256, 256], [1, 1, 1, 1], 8)
+ gen_shapes([1, 1, 1], [12, 256, 256], [1, 1, 1], 8)
+ gen_shapes([1, 1], [256, 256], [1, 1], 8),
"grad_dtype": [ttnn.bfloat16, ttnn.bfloat8_b],
"input_a_dtype": [ttnn.bfloat16, ttnn.bfloat8_b],
"input_layout": [ttnn.TILE_LAYOUT, ttnn.ROW_MAJOR_LAYOUT],
"grad_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_MEMORY_CONFIG],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_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_layout"] == ttnn.ROW_MAJOR_LAYOUT:
return True, "Inputs to eltwise binary must be tilized"
if test_vector["input_a_dtype"] == ttnn.bfloat8_b:
return True, "bfloat8_b is not supported on input_tensor_a"
if test_vector["input_layout"] == ttnn.ROW_MAJOR_LAYOUT and test_vector["input_a_dtype"] == ttnn.bfloat8_b:
return True, "bfloat8_b is only supported on tiled layout"
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,
grad_dtype,
input_a_dtype,
input_layout,
grad_memory_config,
input_a_memory_config,
output_memory_config,
*,
device,
) -> list:
data_seed = random.randint(0, 20000000)
torch.manual_seed(data_seed)

if input_layout == ttnn.ROW_MAJOR_LAYOUT:
input_shape = sanitize_shape_rm(input_shape)

torch_grad_tensor = gen_func_with_cast_tt(
partial(torch_random, low=-100, high=100, dtype=torch.float32), grad_dtype
)(input_shape).to(torch.float32)

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

torch_input_tensor_a = torch.complex(torch_real, torch_imag)

torch_input_tensor_a.requires_grad = True

golden_function = ttnn.get_golden_function(ttnn.angle_bw)
torch_output_tensor = golden_function(torch_grad_tensor, torch_input_tensor_a)[0]

grad_tensor = ttnn.from_torch(
torch_grad_tensor,
dtype=grad_dtype,
layout=input_layout,
device=device,
memory_config=grad_memory_config,
)

input_tensor_a_real = ttnn.from_torch(
torch_real,
dtype=input_a_dtype,
layout=input_layout,
device=device,
memory_config=input_a_memory_config,
)
input_tensor_a_imag = ttnn.from_torch(
torch_imag,
dtype=input_a_dtype,
layout=input_layout,
device=device,
memory_config=input_a_memory_config,
)

input_tensor_a = ttnn.complex_tensor(input_tensor_a_real, input_tensor_a_imag)

start_time = start_measuring_time()
output_tensor = ttnn.angle_bw(grad_tensor, input_tensor_a, memory_config=output_memory_config)[0]
e2e_perf = stop_measuring_time(start_time)

output_tensor = torch.cat((ttnn.to_torch(output_tensor.real), ttnn.to_torch(output_tensor.imag)), dim=-1)

return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf]
109 changes: 109 additions & 0 deletions tests/sweep_framework/sweeps/eltwise/unary_complex/polar/polar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# 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.sweep_utils.utils import gen_shapes, sanitize_shape_rm
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" 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_shape": gen_shapes([1, 1, 1, 1], [6, 12, 256, 256], [1, 1, 1, 1], 16)
+ gen_shapes([1, 1, 1], [12, 256, 256], [1, 1, 1], 16)
+ gen_shapes([1, 1], [256, 256], [1, 1], 16),
"input_a_dtype": [ttnn.bfloat16, ttnn.bfloat8_b],
"input_layout": [ttnn.TILE_LAYOUT, ttnn.ROW_MAJOR_LAYOUT],
"input_a_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_MEMORY_CONFIG],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG, ttnn.L1_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_layout"] == ttnn.ROW_MAJOR_LAYOUT:
return True, "Inputs to eltwise binary must be tilized"
if test_vector["input_layout"] == ttnn.ROW_MAJOR_LAYOUT and test_vector["input_a_dtype"] == ttnn.bfloat8_b:
return True, "bfloat8_b is only supported on tiled layout"
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_layout,
input_a_memory_config,
output_memory_config,
*,
device,
) -> list:
data_seed = random.randint(0, 20000000)
torch.manual_seed(data_seed)

if input_layout == ttnn.ROW_MAJOR_LAYOUT:
input_shape = sanitize_shape_rm(input_shape)

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

golden_function = torch.polar
torch_output_tensor = golden_function(torch_real, torch_imag)

input_tensor_a_real = ttnn.from_torch(
torch_real,
dtype=input_a_dtype,
layout=input_layout,
device=device,
memory_config=input_a_memory_config,
)
input_tensor_a_imag = ttnn.from_torch(
torch_imag,
dtype=input_a_dtype,
layout=input_layout,
device=device,
memory_config=input_a_memory_config,
)
input_tensor_a = ttnn.complex_tensor(input_tensor_a_real, input_tensor_a_imag)

start_time = start_measuring_time()
output_tensor = ttnn.polar(input_tensor_a, memory_config=output_memory_config)
e2e_perf = stop_measuring_time(start_time)

output_tensor = torch.complex(
ttnn.to_torch(output_tensor.real).to(torch.float32), ttnn.to_torch(output_tensor.imag).to(torch.float32)
)

return [
check_with_pcc(
torch.view_as_real(torch_output_tensor.clone()), torch.view_as_real(output_tensor.clone()), 0.999
),
e2e_perf,
]
Loading

0 comments on commit e9272c1

Please sign in to comment.