Skip to content

Commit

Permalink
#7957: update doc- zeros, empty, zeros_like (#14104)
Browse files Browse the repository at this point in the history
  • Loading branch information
KalaivaniMCW authored Oct 25, 2024
1 parent e2b6d7f commit 01b6564
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ttnn-run-sweeps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ on:
- ccl.line_all_gather
- ccl.all_gather_n300
- ccl.all_gather_n300_focused
- creation.zeros.zeros
- creation.empty.empty
- creation.zeros_like.zeros_like
- eltwise.unary.abs.abs_pytorch2
- eltwise.unary.relu.relu
- eltwise.unary.relu.relu_pytorch2
Expand Down
72 changes: 72 additions & 0 deletions tests/sweep_framework/sweeps/creation/empty/empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple

import torch
import ttnn

from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time


# 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": {
"batch_sizes": [(1, 2), (3, 6)],
"height": [384, 1024],
"width": [1024, 4096],
"input_dtype": [ttnn.bfloat16, ttnn.bfloat8_b, ttnn.float32],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
"layout": [ttnn.TILE_LAYOUT, ttnn.ROW_MAJOR_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 and test_vector["input_dtype"] == ttnn.bfloat8_b:
return True, "Skipped as ROW_MAJOR_LAYOUT and ttnn.bfloat8_b not supported"
return False, None


def check_output(torch_output_tensor, output_tensor):
status = list(torch_output_tensor.shape) == list(output_tensor.shape)
msg = ""
msg = "pass" if status else "fail"

return status, msg


# 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(
batch_sizes,
height,
width,
input_dtype,
output_memory_config,
layout,
*,
device,
) -> list:
torch.manual_seed(0)

input_shape = (*batch_sizes, height, width)

torch_output_tensor = torch.empty(input_shape)

start_time = start_measuring_time()

output_tensor = ttnn.empty(input_shape, input_dtype, layout, device=device, memory_config=output_memory_config)
output_tensor = ttnn.to_torch(output_tensor)
e2e_perf = stop_measuring_time(start_time)

return [check_output(torch_output_tensor, output_tensor), e2e_perf]
64 changes: 64 additions & 0 deletions tests/sweep_framework/sweeps/creation/zeros/zeros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple

import torch
import ttnn

from tests.ttnn.utils_for_testing import check_with_pcc, start_measuring_time, stop_measuring_time


# 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": {
"batch_sizes": [(1, 2), (3, 6)],
"height": [384, 1024],
"width": [1024, 4096],
"input_dtype": [ttnn.bfloat16, ttnn.bfloat8_b, ttnn.float32],
"output_memory_config": [ttnn.DRAM_MEMORY_CONFIG],
"layout": [ttnn.TILE_LAYOUT, ttnn.ROW_MAJOR_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["input_dtype"] == ttnn.bfloat8_b:
return True, "Skipped as ROW_MAJOR_LAYOUT and ttnn.bfloat8_b not supported"
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 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(
batch_sizes,
height,
width,
input_dtype,
output_memory_config,
layout,
*,
device,
) -> list:
torch.manual_seed(0)

input_shape = (*batch_sizes, height, width)

torch_output_tensor = torch.zeros(input_shape)

start_time = start_measuring_time()

output_tensor = ttnn.zeros(input_shape, input_dtype, layout, memory_config=output_memory_config)
output_tensor = ttnn.to_torch(output_tensor)
e2e_perf = stop_measuring_time(start_time)

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

# SPDX-License-Identifier: Apache-2.0

from typing import Optional, Tuple
from functools import partial

import torch
import ttnn
from tests.sweep_framework.sweep_utils.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


# 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, 32, 32], [2, 6, 256, 256], [1, 1, 320, 320], 128),
"input_dtype": [ttnn.bfloat16, ttnn.float32, ttnn.bfloat8_b],
"input_a_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_dtype"] == ttnn.bfloat8_b:
return True, "Skipped as bfloat8_b dtype not supported"
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_dtype,
input_a_layout,
input_a_memory_config,
output_memory_config,
*,
device,
) -> list:
torch.manual_seed(0)

torch_input_tensor_a = gen_func_with_cast_tt(
partial(torch_random, low=-100, high=100, dtype=torch.float32), input_dtype
)(input_shape)

torch_output_tensor = torch.zeros_like(torch_input_tensor_a)

input_tensor_a = ttnn.from_torch(
torch_input_tensor_a,
dtype=input_dtype,
layout=input_a_layout,
device=device,
memory_config=input_a_memory_config,
)

start_time = start_measuring_time()
result = ttnn.zeros_like(input_tensor_a, 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]
54 changes: 45 additions & 9 deletions ttnn/cpp/pybind11/operations/creation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void bind_full_operation(py::module& module, const creation_operation_t& operati
}

template <typename creation_operation_t>
void bind_full_operation_with_hard_coded_value(py::module& module, const creation_operation_t& operation, const std::string& value_string) {
void bind_full_operation_with_hard_coded_value(py::module& module, const creation_operation_t& operation, const std::string& value_string, const std::string& info_doc = "") {
auto doc = fmt::format(
R"doc(
Creates a tensor with the specified shape and fills it with the value of {1}.
Expand All @@ -115,6 +115,9 @@ void bind_full_operation_with_hard_coded_value(py::module& module, const creatio
Returns:
ttnn.Tensor: A tensor filled with {1}.
Note:
{2}
Example:
>>> tensor = ttnn.{0}(shape=[1, 2, 2, 2], dtype=ttnn.bfloat16, layout=ttnn.ROW_MAJOR_LAYOUT)
>>> print(tensor)
Expand All @@ -124,7 +127,8 @@ void bind_full_operation_with_hard_coded_value(py::module& module, const creatio
[{1}, {1}]]]]], shape=Shape([1, 2, 2, 2]), dtype=DataType::BFLOAT16, layout=Layout::ROW_MAJOR)
)doc",
operation.base_name(),
value_string);
value_string,
info_doc);

bind_registered_operation(
module,
Expand Down Expand Up @@ -221,7 +225,7 @@ void bind_full_like_operation(py::module& module, const creation_operation_t& op
}

template <typename creation_operation_t>
void bind_full_like_operation_with_hard_coded_value(py::module& module, const creation_operation_t& operation, const std::string& value_string) {
void bind_full_like_operation_with_hard_coded_value(py::module& module, const creation_operation_t& operation, const std::string& value_string, const std::string& info_doc = "") {
auto doc = fmt::format(
R"doc(
Creates a tensor of the same shape as the input tensor and fills it with the value of {1}. The data type, layout, device, and memory configuration of the resulting tensor can be specified.
Expand All @@ -238,6 +242,9 @@ void bind_full_like_operation_with_hard_coded_value(py::module& module, const cr
Returns:
ttnn.Tensor: A tensor filled with {1}.
Note:
{2}
Example:
>>> tensor = ttnn.{0}(ttnn.from_torch(torch.randn(1, 2, 2, 2), ttnn.bfloat16, ttnn.TILE_LAYOUT)
>>> output_tensor = ttnn.{0}(tensor=input_tensor, dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT)
Expand All @@ -248,7 +255,8 @@ void bind_full_like_operation_with_hard_coded_value(py::module& module, const cr
[{1}, {1}]]]]], shape=Shape([1, 2, 2, 2]), dtype=DataType::BFLOAT16, layout=Layout::TILE_LAYOUT)
)doc",
operation.base_name(),
value_string);
value_string,
info_doc);

bind_registered_operation(
module,
Expand Down Expand Up @@ -320,7 +328,7 @@ void bind_arange_operation(py::module& module, const creation_operation_t& opera
py::arg("memory_config") = ttnn::DRAM_MEMORY_CONFIG});
}

void bind_empty_operation(py::module& module) {
void bind_empty_operation(py::module& module, const std::string& info_doc = "") {
auto doc = fmt::format(
R"doc(
Creates a device tensor with uninitialized values of the specified shape, data type, layout, and memory configuration.
Expand All @@ -335,12 +343,16 @@ void bind_empty_operation(py::module& module) {
Returns:
ttnn.Tensor: The output uninitialized tensor.
Note:
{1}
Example:
>>> tensor = ttnn.empty(shape=[2, 3], device=device)
>>> print(tensor)
ttnn.Tensor([[[[0.9, 0.21, 0.5], [0.67, 0.11, 0.30]]]], shape=Shape([2, 3]), dtype=DataType::BFLOAT16, layout=Layout::TILE)
)doc",
ttnn::empty.base_name());
ttnn::empty.base_name(),
info_doc);

using EmptyType = decltype(ttnn::empty);
bind_registered_operation(
Expand Down Expand Up @@ -414,16 +426,40 @@ void bind_empty_like_operation(py::module& module) {

void py_module(py::module& module) {
detail::bind_full_operation(module, ttnn::full);
detail::bind_full_operation_with_hard_coded_value(module, ttnn::zeros, "0.0");
detail::bind_full_operation_with_hard_coded_value(module, ttnn::zeros, "0.0",
R"doc(Supported dtypes, layouts, and ranks:
+----------------------------+---------------------------------+-------------------+
| Dtypes | Layouts | Ranks |
+----------------------------+---------------------------------+-------------------+
| BFLOAT16, FLOAT32 | ROW_MAJOR, TILE | 2, 3, 4 |
+----------------------------+---------------------------------+-------------------+)doc");

detail::bind_full_operation_with_hard_coded_value(module, ttnn::ones, "1.0");

detail::bind_full_like_operation(module, ttnn::full_like);
detail::bind_full_like_operation_with_hard_coded_value(module, ttnn::zeros_like, "0.0");
detail::bind_full_like_operation_with_hard_coded_value(module, ttnn::zeros_like, "0.0",
R"doc(Supported dtypes, layouts, and ranks:
+----------------------------+---------------------------------+-------------------+
| Dtypes | Layouts | Ranks |
+----------------------------+---------------------------------+-------------------+
| BFLOAT16, FLOAT32 | ROW_MAJOR, TILE | 2, 3, 4 |
+----------------------------+---------------------------------+-------------------+)doc");
detail::bind_full_like_operation_with_hard_coded_value(module, ttnn::ones_like, "1.0");

detail::bind_arange_operation(module, ttnn::arange);

detail::bind_empty_operation(module);
detail::bind_empty_operation(module,
R"doc(Supported dtypes, layouts, and ranks:
+----------------------------+---------------------------------+-------------------+
| Dtypes | Layouts | Ranks |
+----------------------------+---------------------------------+-------------------+
| BFLOAT16, FLOAT32 | ROW_MAJOR, TILE | 2, 3, 4 |
+----------------------------+---------------------------------+-------------------+
| BFLOAT_8 | TILE | 2, 3, 4 |
+----------------------------+---------------------------------+-------------------+)doc");
detail::bind_empty_like_operation(module);
}

Expand Down

0 comments on commit 01b6564

Please sign in to comment.