-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unary sharded sweeps #15300
Open
amalbasaTT
wants to merge
22
commits into
main
Choose a base branch
from
amalbasaTT/unary_sharded-sweeps-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add unary sharded sweeps #15300
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8c3b52e
#11512: Add tril_sharded sweep
amalbasaTT d673337
#11512: Add triu_sharded sweep, refactor triu sweep
amalbasaTT f61e131
#11512: Refactor remainder_unary sharded, tril_sharded and triu_shard…
amalbasaTT 8dcab60
#11512: Fix sharded sweeps generation function
amalbasaTT 17442d5
#11512: Add sweeps for sharded unary ops: hardsigmoid, hardswish and …
amalbasaTT def6cb9
#11512: Refactoring unary sharded ops sweeps
amalbasaTT a9fcfd8
11512: Add some unary sharded ops sweeps
amalbasaTT 0c90967
#11512: Refactoring triu and tril sharded, creating sharding_utils.py
amalbasaTT a3940e2
#11512: Refactoring
amalbasaTT af89e8f
#11512: Minor fixes
amalbasaTT c5354e6
#11512 Ad sharded sweeps to ttnn-run-sweeps.yaml
amalbasaTT c74ced7
#11512: Refactor hardtanh.py sweep
amalbasaTT 6a384b2
#11512: Minor fix
amalbasaTT e85182e
#11512: Hardsigmod sweep minor fix
amalbasaTT 651c54e
#11512: Hardtanh sweep minor fix
amalbasaTT 7852b6a
#11512: Refactoring
amalbasaTT 9002e9c
#11512: Add heaviside sharded sweep, modify existing heaviside sweep
amalbasaTT 012c593
#11512: Add heaviside shard to ttnn-run-sweeps
amalbasaTT 7230861
#11512: Refactor heaviside_sharded sweep
amalbasaTT da507a0
#11512: Modifying invalidate_vector functions in sharded sweeps
amalbasaTT c16e3c0
#11512: Add new sharding function to existing sharded sweeps
amalbasaTT 75906f9
#11512: Update heaviside sharded to use new sharding funciton
amalbasaTT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,282 @@ | ||
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
import torch | ||
import random | ||
import ttnn | ||
import math | ||
|
||
from tests.sweep_framework.sweep_utils.utils import get_device_grid_size | ||
from tests.tt_eager.python_api_testing.sweep_tests.generation_funcs import ( | ||
gen_func_with_cast_tt, | ||
_gen_reshape_args_from_volume, | ||
_get_factors, | ||
) | ||
|
||
|
||
Y, X = get_device_grid_size() | ||
|
||
|
||
def divup(a, b): | ||
return (a + b - 1) // b | ||
|
||
|
||
def roundup(a, b): | ||
result = divup(a, b) * b | ||
return result | ||
|
||
|
||
def gen_unary_sharded_spec( | ||
num_shapes, | ||
num_core_samples, | ||
shard_orientation, | ||
sharding_strategy, | ||
shard_height_mul_of_32=True, | ||
max_tensor_size_per_core=256 * 256, | ||
): | ||
assert sharding_strategy in ["BLOCK", "WIDTH", "HEIGHT", "TENSOR_HW"] | ||
|
||
assert shard_orientation in ["COL_MAJOR", "ROW_MAJOR"] | ||
|
||
for i in range(num_core_samples): | ||
y = random.randint(1, Y) | ||
x = random.randint(1, X) | ||
max_tensor_size = y * x * max_tensor_size_per_core | ||
for j in range(num_shapes): | ||
for rank in [2, 3, 4]: | ||
if sharding_strategy == "TENSOR_HW": | ||
min_tensor_height = 32 | ||
min_tensor_width = 32 | ||
max_tensor_height = int(math.sqrt(max_tensor_size_per_core)) | ||
max_tensor_width = int(math.sqrt(max_tensor_size_per_core)) | ||
interval = 32 | ||
|
||
tensor_height = random.randrange(min_tensor_height, max_tensor_height + 1, interval) | ||
tensor_width = random.randrange(min_tensor_width, max_tensor_width + 1, interval) | ||
input_shape = [tensor_height, tensor_width] | ||
if rank != 2: | ||
rest_volume = random.randint(1, max_tensor_size // (tensor_height * tensor_width)) | ||
rest_dims = random.choice(_gen_reshape_args_from_volume(rest_volume, step=1, out_dims=rank - 2)) | ||
rest_dims = list(rest_dims["reshape_dims"]) | ||
|
||
elif sharding_strategy == "BLOCK": | ||
if shard_orientation == "ROW_MAJOR": | ||
if not shard_height_mul_of_32: | ||
min_pre_sharded_height = 32 * y | ||
else: | ||
min_pre_sharded_height = 1 | ||
min_pre_sharded_width = 32 * x | ||
max_pre_sharded_height = int(math.sqrt(max_tensor_size_per_core)) * y | ||
max_pre_sharded_width = int(math.sqrt(max_tensor_size_per_core)) * x | ||
interval_height = 32 * y | ||
interval_width = 32 * x | ||
else: | ||
if not shard_height_mul_of_32: | ||
min_pre_sharded_height = 32 * x | ||
else: | ||
min_pre_sharded_height = 1 | ||
min_pre_sharded_width = 32 * y | ||
max_pre_sharded_height = int(math.sqrt(max_tensor_size_per_core)) * x | ||
max_pre_sharded_width = int(math.sqrt(max_tensor_size_per_core)) * y | ||
interval_height = 32 * x | ||
interval_width = 32 * y | ||
|
||
pre_sharded_height = random.randrange( | ||
min_pre_sharded_height, max_pre_sharded_height + 1, interval_height | ||
) | ||
pre_sharded_width = random.randrange( | ||
min_pre_sharded_width, max_pre_sharded_width + 1, interval_width | ||
) | ||
|
||
if ( | ||
shard_height_mul_of_32 | ||
): # tensor height could grow beyond the maximum allowed when padding it to be multiple of total_num_cores * 32 | ||
height_round_up = 32 * y if shard_orientation is "ROW_MAJOR" else 32 * x | ||
width_round_up = 32 * x if shard_orientation is "ROW_MAJOR" else 32 * y | ||
while roundup(pre_sharded_height, height_round_up) > max_pre_sharded_height: | ||
pre_sharded_height = random.randrange( | ||
min_pre_sharded_height, max_pre_sharded_height + 1, interval_height | ||
) | ||
while roundup(pre_sharded_width, width_round_up) > max_pre_sharded_width: | ||
pre_sharded_width = random.randrange( | ||
min_pre_sharded_width, max_pre_sharded_width + 1, interval_width | ||
) | ||
|
||
input_shape = random.choice( | ||
_gen_reshape_args_from_volume(pre_sharded_height, step=1, out_dims=rank - 1) | ||
) | ||
input_shape = list(input_shape["reshape_dims"]) | ||
input_shape.append(pre_sharded_width) | ||
|
||
elif sharding_strategy == "HEIGHT": | ||
if not shard_height_mul_of_32: | ||
min_pre_sharded_height = 32 * y * x | ||
interval = 32 * y * x | ||
else: | ||
min_pre_sharded_height = 1 | ||
interval = 1 | ||
min_pre_sharded_width = 32 | ||
|
||
max_pre_sharded_height = int(math.sqrt(max_tensor_size)) | ||
max_pre_sharded_width = max_pre_sharded_height | ||
|
||
pre_sharded_width = random.randrange(min_pre_sharded_width, max_pre_sharded_width + 1, 32) | ||
|
||
pre_sharded_height = random.randrange(min_pre_sharded_height, max_pre_sharded_height + 1, interval) | ||
if ( | ||
shard_height_mul_of_32 | ||
): # tensor height could grow beyond the maximum allowed when padding it to be multiple of total_num_cores * 32 | ||
while roundup(pre_sharded_height, y * x * 32) > max_tensor_size // pre_sharded_width: | ||
pre_sharded_height = random.randrange( | ||
min_pre_sharded_height, max_pre_sharded_height + 1, interval | ||
) | ||
|
||
input_shape = random.choice( | ||
_gen_reshape_args_from_volume(pre_sharded_height, step=1, out_dims=rank - 1) | ||
) | ||
input_shape = list(input_shape["reshape_dims"]) | ||
input_shape.append(pre_sharded_width) | ||
else: | ||
if not shard_height_mul_of_32: | ||
min_pre_sharded_height = 32 | ||
interval = 32 | ||
else: | ||
min_pre_sharded_height = 1 | ||
interval = 1 | ||
|
||
min_pre_sharded_width = 32 * y * x | ||
max_pre_sharded_height = int(math.sqrt(max_tensor_size)) | ||
max_pre_sharded_width = max_pre_sharded_height | ||
|
||
pre_sharded_height = random.randrange(min_pre_sharded_height, max_pre_sharded_height + 1, interval) | ||
if ( | ||
shard_height_mul_of_32 | ||
): # tensor height could grow beyond the maximum allowed when padding it to be multiple of total_num_cores * 32 | ||
while roundup(pre_sharded_height, y * x * 32) > max_pre_sharded_height: | ||
pre_sharded_height = random.randrange( | ||
min_pre_sharded_height, max_pre_sharded_height + 1, interval | ||
) | ||
pre_sharded_width = random.randrange(min_pre_sharded_width, max_pre_sharded_width + 1, 32 * y * x) | ||
|
||
input_shape = random.choice( | ||
_gen_reshape_args_from_volume(pre_sharded_height, step=1, out_dims=rank - 1) | ||
) | ||
input_shape = list(input_shape["reshape_dims"]) | ||
input_shape.append(pre_sharded_width) | ||
|
||
yield { | ||
"input_shape": input_shape, | ||
"core_grid_size": (y, x), | ||
"sharding_strategy": sharding_strategy, | ||
"shard_orientation": shard_orientation, | ||
"shard_height_mul_of_32": shard_height_mul_of_32, | ||
} | ||
|
||
|
||
def parse_sharding_spec(input_spec): | ||
input_shape = input_spec["input_shape"] | ||
sharding_strategy = input_spec["sharding_strategy"] | ||
shard_orientation = input_spec["shard_orientation"] | ||
core_grid_size = input_spec["core_grid_size"] | ||
shard_height_mul_of_32 = input_spec["shard_height_mul_of_32"] | ||
|
||
assert sharding_strategy in ["HEIGHT", "WIDTH", "BLOCK", "TENSOR_HW"] | ||
|
||
tensor_hw_as_shard_shape = False | ||
|
||
if sharding_strategy == "HEIGHT": | ||
sharding_strategy = ttnn.ShardStrategy.HEIGHT | ||
elif sharding_strategy == "WIDTH": | ||
sharding_strategy = ttnn.ShardStrategy.WIDTH | ||
elif sharding_strategy == "BLOCK": | ||
sharding_strategy = ttnn.ShardStrategy.BLOCK | ||
else: | ||
sharding_strategy = ttnn.ShardStrategy.BLOCK | ||
tensor_hw_as_shard_shape = True | ||
|
||
if shard_orientation == "COL_MAJOR": | ||
shard_orientation = ttnn.ShardOrientation.COL_MAJOR | ||
else: | ||
shard_orientation = ttnn.ShardOrientation.ROW_MAJOR | ||
|
||
return ( | ||
input_shape, | ||
core_grid_size, | ||
shard_orientation, | ||
sharding_strategy, | ||
tensor_hw_as_shard_shape, | ||
shard_height_mul_of_32, | ||
) | ||
|
||
|
||
def invalidate_vector_sharding( | ||
input_shape, input_layout, core_grid_size, sharding_strategy, shard_orientation, tensor_hw_as_shard_shape | ||
): | ||
y, x = core_grid_size | ||
pre_sharded_height = math.prod(input_shape[:-1]) | ||
pre_sharded_width = input_shape[-1] | ||
|
||
if not tensor_hw_as_shard_shape: | ||
if sharding_strategy == ttnn.ShardStrategy.BLOCK: | ||
if shard_orientation == ttnn.ShardOrientation.ROW_MAJOR: | ||
if pre_sharded_height % y != 0: | ||
return ( | ||
True, | ||
"Prod of all dimensions except the innermost must be divisible by the y coordinate of coregrid when using block sharding", | ||
) | ||
if pre_sharded_width % x != 0: | ||
return ( | ||
True, | ||
"Innermost dimension must be divisible by the x coordinate of coregrid when using block sharding", | ||
) | ||
if (pre_sharded_height // y) % 32 != 0: | ||
return True, "Shard height must be a multiple of input tile size" | ||
if (pre_sharded_width // x) % 32 != 0: | ||
return True, "Shard width must be a multiple of input tile size" | ||
else: | ||
if pre_sharded_height % x != 0: | ||
return ( | ||
True, | ||
"Prod of all dimensions except the innermost must be divisible by the x coordinate of coregrid when using block sharding", | ||
) | ||
if pre_sharded_width % y != 0: | ||
return ( | ||
True, | ||
"Innermost dimension must be divisible by the y coordinate of coregrid when using block sharding", | ||
) | ||
if (pre_sharded_height // x) % 32 != 0: | ||
return True, "Shard height must be a multiple of input tile size" | ||
if (pre_sharded_width // y) % 32 != 0: | ||
return True, "Shard width must be a multiple of input tile size" | ||
|
||
elif sharding_strategy == ttnn.ShardStrategy.WIDTH: | ||
if pre_sharded_width % (y * x) != 0: | ||
return True, "Last dimension must be divisible by a total number of cores when using width sharding" | ||
if pre_sharded_height % 32 != 0: | ||
return True, "Shard height must be a multiple of input tile size" | ||
if (pre_sharded_width // (x * y)) % 32 != 0: | ||
return True, "Shard width must be a multiple of input tile size" | ||
|
||
else: | ||
if pre_sharded_height % (y * x) != 0: | ||
return ( | ||
True, | ||
"Prod of all dimensions except the innermost must be divisible by a total number of cores when using width sharding", | ||
) | ||
if (pre_sharded_height // (x * y)) % 32 != 0: | ||
return True, "Shard height must be a multiple of input tile size" | ||
if pre_sharded_width % 32 != 0: | ||
return True, "Shard width must be a multiple of input tile size" | ||
|
||
else: | ||
if input_shape[-2] % 32 != 0 or input_shape[-1] % 32 != 0: | ||
return ( | ||
True, | ||
"Last two dimensions must be multiples of tile size when using tensor heght and width as shard shape", | ||
) | ||
if input_layout == ttnn.ROW_MAJOR_LAYOUT and (input_shape[-1] % input_shape[-2] != 0): | ||
return True, "Physical size <width, height> must be a multuple of page size <1, width>" | ||
|
||
return False, "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you replace with
ttnn.create_sharded_memory_config_
?