Skip to content

Commit

Permalink
#16013: sweeps created, awaiting ability to run them on corp
Browse files Browse the repository at this point in the history
  • Loading branch information
yugi957 committed Dec 19, 2024
1 parent 7ed63fd commit 8879c5e
Show file tree
Hide file tree
Showing 7 changed files with 545 additions and 11 deletions.
97 changes: 97 additions & 0 deletions tests/sweep_framework/sweeps/data_movement/concat/concat_BH.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple

import torch
import random
import ttnn

from tests.ttnn.utils_for_testing import (
check_with_pcc,
start_measuring_time,
stop_measuring_time,
get_per_core_size_and_num_cores,
)
from models.utility_functions import torch_random
import pytest

# Override the default timeout in seconds for hang detection.
TIMEOUT = 20
random.seed(0)

# List[Tensor] tensors = [<[1, 100, 14, 14]>, <[1, 100, 14, 14]>],
# int dim = 1
# List[Tensor] tensors = [<[1, 1056, 7, 7]>, <[1, 48, 7, 7]>],
# int dim = 1

parameters = {
"nightly": {
"concat_specs": [
{"dim": 3, "shapes": [[1, 1, 32, 32064], [1, 1, 32, 32064], [1, 1, 32, 32064], [1, 1, 32, 32064]]}
],
"dtype": [ttnn.bfloat8_b],
"layout": [ttnn.TILE_LAYOUT],
}
}


# 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["layout"] == ttnn.ROW_MAJOR_LAYOUT:
if test_vector["dtype"] == ttnn.bfloat8_b:
return True, "bfloat8_b not supported with ROW_MAJOR_LAYOUT"

return False, None


def run(
concat_specs,
dtype,
layout,
*,
device,
) -> list:
device.enable_async(False)
torch_input_tensors = [torch_random(shape, -0.1, 0.1, dtype=torch.bfloat16) for shape in concat_specs["shapes"]]
torch_output_tensor = torch.concat(torch_input_tensors, dim=concat_specs["dim"])

ttnn_input_tensors = [
ttnn.from_torch(torch_input_tensor, device=device, layout=layout, dtype=dtype)
for torch_input_tensor in torch_input_tensors
]
start_time = start_measuring_time()
result_tensor = ttnn.concat(ttnn_input_tensors, dim=concat_specs["dim"])
e2e_perf = stop_measuring_time(start_time)
output_tensor = ttnn.to_torch(result_tensor)

return [check_with_pcc(torch_output_tensor, output_tensor, 0.999), e2e_perf]


@pytest.mark.parametrize("concat_spec", parameters["nightly"]["concat_specs"])
@pytest.mark.parametrize("dtype", parameters["nightly"]["dtype"])
@pytest.mark.parametrize("layout", parameters["nightly"]["layout"])
def test_concat_pytorch2(concat_spec, dtype, layout, device):
shapes = concat_spec["shapes"]
dim = concat_spec["dim"]
device.enable_async(False)
if dtype == ttnn.bfloat16 and any([shape[-1] % 2 != 0 for shape in shapes]) and layout == ttnn.ROW_MAJOR_LAYOUT:
pytest.skip("Skipping test for RM bfloat16 with odd last dimension")

torch_input_tensors = [torch_random(shape, -0.1, 0.1, dtype=torch.bfloat16) for shape in shapes]
torch_output_tensor = torch.cat(torch_input_tensors, dim=dim)

ttnn_input_tensors = [
ttnn.from_torch(torch_input_tensor, device=device, layout=layout, dtype=dtype)
for torch_input_tensor in torch_input_tensors
]

result_tensor = ttnn.concat(ttnn_input_tensors, dim=dim)
output_tensor = ttnn.to_torch(result_tensor)

assert check_with_pcc(
torch_output_tensor, output_tensor, 0.999
), "Output tensors do not match within the specified precision"
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@
}


# 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["layout"] == ttnn.ROW_MAJOR_LAYOUT:
if test_vector["dtype"] == ttnn.bfloat8_b:
return True, "bfloat8_b not supported with ROW_MAJOR_LAYOUT"

return False, None


def run(
embedding_specs,
*,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple

import torch
import random
import ttnn
import traceback

from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time
from models.utility_functions import torch_random

TIMEOUT = 15
# seed for random
random.seed(0)

parameters = {
"nightly": {
"shard_specs": [
{
"shape": [1, 32, 128],
"shard_shape": [32, 128],
"output_mem_config": {
"layout": "TensorMemoryLayout::HEIGHT_SHARDED",
"buffer_type": "BufferType::L1",
"shard_spec": {
"grid": [[{"x": 0, "y": 0}, {"x": 0, "y": 0}]],
"shape": [32, 128],
"orientation": "ShardOrientation::ROW_MAJOR",
"halo": 0,
"mode": "ShardMode::PHYSICAL",
"physical_shard_shape": None,
},
},
},
],
"strategy": [ttnn.ShardStrategy.HEIGHT],
"orientation": [ttnn.ShardOrientation.ROW_MAJOR],
"core_grid": [ttnn.CoreGrid(y=1, x=1)],
"dtype": [ttnn.bfloat16],
"layout": [ttnn.ROW_MAJOR_LAYOUT],
"input_buffer_type": [ttnn.L1_MEMORY_CONFIG],
"output_buffer_type": [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["layout"] == ttnn.ROW_MAJOR_LAYOUT:
if test_vector["dtype"] == ttnn.bfloat8_b:
return True, "bfloat8_b not supported with ROW_MAJOR_LAYOUT"

return False, None


def run(
shard_specs,
strategy,
orientation,
core_grid,
dtype,
layout,
input_buffer_type,
output_buffer_type,
*,
device,
):
device.enable_async(False)

shape = shard_specs["shape"]
shard_shape = shard_specs["shard_shape"]
output_mem_config = shard_specs["output_mem_config"]

# Parse memory configuration parameters
mem_layout = output_mem_config["layout"]
buffer_type = output_mem_config["buffer_type"]
shard_spec = output_mem_config["shard_spec"]
shard_grid = shard_spec["grid"]
shard_shape = shard_spec["shape"]
shard_orientation = shard_spec["orientation"]

# Create the memory config using pybind-defined function
shard_config = ttnn.create_sharded_memory_config(
shape=shard_shape,
core_grid=core_grid,
strategy=strategy,
orientation=orientation,
use_height_and_width_as_shard_shape=True,
)

# Create a random tensor of the specified shape
torch.manual_seed(0)
input_data = torch.randn(shape, dtype=torch.bfloat16)
interleaved_data = ttnn.from_torch(
input_data,
device=device,
layout=layout,
memory_config=input_buffer_type,
dtype=ttnn.bfloat16,
)

# Measure performance of the interleaved-to-sharded operation
start_time = start_measuring_time()

# Use the pybind-defined function to convert interleaved to sharded
sharded_data = ttnn.operations.data_movement.interleaved_to_sharded(
input_tensor=interleaved_data,
grid=ttnn.CoreGrid(*shard_grid[0][0].values()),
shard_shape=shard_shape,
shard_scheme=mem_layout,
shard_orientation=shard_orientation,
output_dtype=dtype,
queue_id=0,
keep_l1_aligned=False,
)

# Convert back to interleaved for validation
interleaved_output = ttnn.to_memory_config(sharded_data, output_buffer_type)

e2e_perf = stop_measuring_time(start_time)

output_data = ttnn.from_device(interleaved_output)
output_data = ttnn.to_torch(output_data)

# Compare the concatenated tensors and return performance and accuracy check
result = check_with_pcc(input_data, output_data, 0.999)
return [result, e2e_perf]
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple

import torch
import random
import ttnn
import traceback

from framework.device_fixtures import default_device
from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time
from models.utility_functions import torch_random

TIMEOUT = 15
# seed for random
random.seed(0)

parameters = {
"nightly": {
"shard_specs": [
{
"shape": [1, 32, 1280],
"output_mem_config": {
"layout": "TensorMemoryLayout::INTERLEAVED",
"buffer_type": "BufferType::L1",
"shard_spec": None,
},
"output_dtype": "DataType::BFLOAT16",
},
],
"dtype": [ttnn.bfloat16],
"layout": [ttnn.TILE_LAYOUT],
}
}


shard_specs = {
"shape": [1, 32, 1280],
"output_mem_config": {
"layout": "TensorMemoryLayout::INTERLEAVED",
"buffer_type": "BufferType::L1",
"shard_spec": None,
},
"output_dtype": "DataType::BFLOAT16",
}

device = default_device()
device.enable_async(False)

shape = shard_specs["shape"]
output_mem_config = shard_specs["output_mem_config"]

# Parse memory configuration parameters
mem_layout = output_mem_config["layout"]
buffer_type = output_mem_config["buffer_type"]
shard_spec = output_mem_config["shard_spec"]
output_dtype = ttnn.bfloat16

# Create the memory config
memory_config = ttnn.create_interleaved_memory_config(
layout=mem_layout,
buffer_type=buffer_type,
shard_spec=shard_spec,
)

# Create a random tensor of the specified shape
torch.manual_seed(0)
input_data = torch.randn(shape, dtype=torch.bfloat16)
sharded_data = ttnn.from_torch(
input_data,
device=device,
layout=ttnn.TILE_LAYOUT,
memory_config=ttnn.L1_MEMORY_CONFIG,
dtype=ttnn.bfloat16,
)

# Measure performance of the sharded-to-interleaved operation
start_time = start_measuring_time()

# Use the pybind-defined function to convert sharded to interleaved
interleaved_data = ttnn.operations.data_movement.sharded_to_interleaved(
input_tensor=sharded_data,
memory_config=memory_config,
output_dtype=output_dtype,
queue_id=0,
is_l1_aligned=False,
)

e2e_perf = stop_measuring_time(start_time)

output_data = ttnn.from_device(interleaved_data)
output_data = ttnn.to_torch(output_data)

# Compare the tensors and return performance and accuracy check
result = check_with_pcc(input_data, output_data, 0.999)
print([result, e2e_perf])
Loading

0 comments on commit 8879c5e

Please sign in to comment.