Skip to content
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
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 Nov 7, 2024
d673337
#11512: Add triu_sharded sweep, refactor triu sweep
amalbasaTT Nov 7, 2024
f61e131
#11512: Refactor remainder_unary sharded, tril_sharded and triu_shard…
amalbasaTT Nov 8, 2024
8dcab60
#11512: Fix sharded sweeps generation function
amalbasaTT Nov 8, 2024
17442d5
#11512: Add sweeps for sharded unary ops: hardsigmoid, hardswish and …
amalbasaTT Nov 13, 2024
def6cb9
#11512: Refactoring unary sharded ops sweeps
amalbasaTT Nov 18, 2024
a9fcfd8
11512: Add some unary sharded ops sweeps
amalbasaTT Nov 21, 2024
0c90967
#11512: Refactoring triu and tril sharded, creating sharding_utils.py
amalbasaTT Nov 21, 2024
a3940e2
#11512: Refactoring
amalbasaTT Nov 22, 2024
af89e8f
#11512: Minor fixes
amalbasaTT Nov 22, 2024
c5354e6
#11512 Ad sharded sweeps to ttnn-run-sweeps.yaml
amalbasaTT Nov 22, 2024
c74ced7
#11512: Refactor hardtanh.py sweep
amalbasaTT Nov 22, 2024
6a384b2
#11512: Minor fix
amalbasaTT Nov 22, 2024
e85182e
#11512: Hardsigmod sweep minor fix
amalbasaTT Nov 25, 2024
651c54e
#11512: Hardtanh sweep minor fix
amalbasaTT Nov 25, 2024
7852b6a
#11512: Refactoring
amalbasaTT Nov 26, 2024
9002e9c
#11512: Add heaviside sharded sweep, modify existing heaviside sweep
amalbasaTT Nov 26, 2024
012c593
#11512: Add heaviside shard to ttnn-run-sweeps
amalbasaTT Nov 26, 2024
7230861
#11512: Refactor heaviside_sharded sweep
amalbasaTT Nov 27, 2024
da507a0
#11512: Modifying invalidate_vector functions in sharded sweeps
amalbasaTT Nov 27, 2024
c16e3c0
#11512: Add new sharding function to existing sharded sweeps
amalbasaTT Nov 29, 2024
75906f9
#11512: Update heaviside sharded to use new sharding funciton
amalbasaTT Nov 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ttnn-run-sweeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,21 @@ on:
- eltwise.unary.sigmoid.sigmoid_pytorch2
- eltwise.unary.sigmoid_accurate.sigmoid_accurate
- eltwise.unary.tril.tril
- eltwise.unary.tril.tril_sharded
- eltwise.unary.triu.triu
- eltwise.unary.triu.triu_sharded
- eltwise.unary.normalize_hw.normalize_hw
- eltwise.unary.normalize_global.normalize_global
- eltwise.unary.heaviside.heaviside
- eltwise.unary.heaviside.heaviside_sharded
- eltwise.unary.hardtanh.hardtanh
- eltwise.unary.hardtanh.hardtanh_sharded
- eltwise.unary.hardswish.hardswish
- eltwise.unary.hardswish.hardswish_sharded
- eltwise.unary.hardsigmoid.hardsigmoid
- eltwise.unary.hardsigmoid.hardsigmoid_sharded
- eltwise.unary.hardshrink.hardshrink
- eltwise.unary.hardshrink.hardshrink_sharded
- eltwise.unary.softmax.softmax
- eltwise.unary.identity.identity
- eltwise.unary.neg.neg
Expand Down
282 changes: 282 additions & 0 deletions tests/sweep_framework/sweep_utils/sharding_utils.py
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, ""
43 changes: 43 additions & 0 deletions tests/sweep_framework/sweep_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0


import os
import random
from loguru import logger
from itertools import product
Expand Down Expand Up @@ -247,3 +248,45 @@ def complex_from_torch(torch_tensor, dtype, layout, memory_config, device):
memory_config=memory_config,
)
return ttnn.complex_tensor(tt_real, tt_imag)


def get_device_grid_size():
device_name = os.environ.get("ARCH_NAME", os.environ.get("TT_ARCH_NAME", "default")).lower()
assert device_name in ["wormhole_b0", "grayskull"]
if device_name == "grayskull":
y, x = 9, 12
else:
y, x = 8, 8

return y, x


def get_sharded_config(input_shape, sharding_strategy, device_grid_size, shard_orientation):
assert sharding_strategy in ["block", "width", "height", "tensor_hw"]
assert shard_orientation in ["col_major", "row_major"]

if shard_orientation == "col_major":
orientation = ttnn.ShardOrientation.COL_MAJOR
else:
orientation = ttnn.ShardOrientation.ROW_MAJOR

if sharding_strategy == "block":
strategy = ttnn.ShardStrategy.BLOCK
elif sharding_strategy == "width":
strategy = ttnn.ShardStrategy.WIDTH
elif sharding_strategy == "height":
strategy = ttnn.ShardStrategy.HEIGHT
else:
strategy = ttnn.ShardStrategy.BLOCK

tensor_hw_as_shard_shape = True if sharding_strategy == "tensor_hw" else False

sharded_config = ttnn.create_sharded_memory_config(
Copy link
Contributor

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_?

shape=input_shape,
core_grid=device_grid_size,
strategy=strategy,
orientation=orientation,
use_height_and_width_as_shard_shape=tensor_hw_as_shard_shape,
)

return sharded_config
Loading
Loading