-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8958ba3
commit 52add8b
Showing
31 changed files
with
2,423 additions
and
204 deletions.
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
174 changes: 174 additions & 0 deletions
174
tests/tt_eager/python_api_testing/unit_testing/test_moreh_sum.py
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,174 @@ | ||
# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
import torch | ||
from loguru import logger | ||
|
||
import tt_lib as ttl | ||
from models.utility_functions import comp_allclose_and_pcc, skip_for_wormhole_b0 | ||
|
||
TILE_HEIGHT = 32 | ||
TILE_WIDTH = 32 | ||
|
||
|
||
def get_tensors(input_shape, output_shape, device): | ||
torch.manual_seed(2023) | ||
npu_dtype = ttl.tensor.DataType.BFLOAT16 | ||
cpu_dtype = torch.bfloat16 | ||
npu_layout = ttl.tensor.Layout.TILE | ||
|
||
torch_input = torch.randint(-2, 3, input_shape, dtype=cpu_dtype, requires_grad=True) | ||
torch_output = torch.randint(-2, 3, output_shape, dtype=cpu_dtype) | ||
|
||
tt_input = ttl.tensor.Tensor(torch_input, npu_dtype).pad_to_tile(float("nan")).to(npu_layout).to(device) | ||
tt_output = ttl.tensor.Tensor(torch_output, npu_dtype).pad_to_tile(float("nan")).to(npu_layout).to(device) | ||
|
||
return tt_input, tt_output, torch_input | ||
|
||
|
||
def get_backward_tensors(output_grad_shape, input_grad_shape, device): | ||
torch.manual_seed(2023) | ||
npu_dtype = ttl.tensor.DataType.BFLOAT16 | ||
cpu_dtype = torch.bfloat16 | ||
npu_layout = ttl.tensor.Layout.TILE | ||
|
||
torch_output_grad = torch.randint(-2, 3, output_grad_shape, dtype=cpu_dtype, requires_grad=True) | ||
torch_input_grad = torch.randint(-2, 3, input_grad_shape, dtype=cpu_dtype) | ||
|
||
tt_output_grad = ttl.tensor.Tensor(torch_output_grad, npu_dtype).pad_to_tile(float("nan")).to(npu_layout).to(device) | ||
tt_input_grad = ttl.tensor.Tensor(torch_input_grad, npu_dtype).pad_to_tile(float("nan")).to(npu_layout).to(device) | ||
|
||
return tt_output_grad, tt_input_grad, torch_output_grad | ||
|
||
|
||
# Dongjin : WH_B0 skips this test due to the problem of sum reduction for w-dim. | ||
@skip_for_wormhole_b0() | ||
@pytest.mark.parametrize( | ||
"input_shape", | ||
( | ||
([1, 1, TILE_HEIGHT - 1, TILE_WIDTH - 1]), | ||
([4, 4, TILE_HEIGHT * 9 - 1, TILE_WIDTH * 12 - 1]), | ||
([4, 4, TILE_HEIGHT * 12 - 1, TILE_WIDTH * 9 - 1]), | ||
([8, 8, TILE_HEIGHT * 4 - 1, TILE_WIDTH * 4 - 1]), | ||
), | ||
ids=[ | ||
"1, 1, TILE_HEIGHT-1,TILE_WIDTH - 1", | ||
"4, 4, TILE_HEIGHT * 9 - 1, TILE_WIDTH * 12 - 1", | ||
"4, 4, TILE_HEIGHT * 12 - 1, TILE_WIDTH * 9 - 1", | ||
"8, 8, TILE_HEIGHT * 4 - 1, TILE_WIDTH * 4 - 1", | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"dims", | ||
( | ||
[0], | ||
[0, 1], | ||
[0, 1, 2], | ||
[0, 1, 2, 3], | ||
[0, 1, 3], | ||
[0, 2, 3], | ||
[1], | ||
[1, 2], | ||
[1, 2, 3], | ||
[1, 3], | ||
[2], | ||
[2, 3], | ||
[3], | ||
), | ||
ids=["0", "0,1", "0,1,2", "0,1,2,3", "0,1,3", "0,2,3", "1", "1,2", "1,2,3", "1,3", "2", "2,3", "3"], | ||
) | ||
def test_moreh_sum_dims(input_shape, dims, device): | ||
output_shape = input_shape.copy() | ||
|
||
for dim in dims: | ||
output_shape[dim] = 1 | ||
|
||
(tt_input, tt_output, torch_input) = get_tensors(input_shape, output_shape, device) | ||
|
||
torch_output = torch.sum(torch_input, dims, True) | ||
|
||
cpu_layout = ttl.tensor.Layout.ROW_MAJOR | ||
tt_output_cpu = ( | ||
ttl.operations.primary.moreh_sum(tt_input, tt_output, dims=dims) | ||
.cpu() | ||
.to(cpu_layout) | ||
.unpad_from_tile(output_shape) | ||
.to_torch() | ||
) | ||
|
||
# test for equivalance | ||
# TODO(Dongjin) : check while changing rtol after enabling fp32_dest_acc_en | ||
rtol = atol = 0.12 | ||
passing, output_pcc = comp_allclose_and_pcc(torch_output, tt_output_cpu, pcc=0.999, rtol=rtol, atol=atol) | ||
|
||
logger.info(f"Out passing={passing}") | ||
logger.info(f"Output pcc={output_pcc}") | ||
|
||
assert passing | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_shape", | ||
( | ||
([1, 1, TILE_HEIGHT - 1, TILE_WIDTH - 1]), | ||
([4, 4, TILE_HEIGHT * 12 - 1, TILE_WIDTH * 30 - 1]), | ||
([4, 4, TILE_HEIGHT * 30 - 1, TILE_WIDTH * 12 - 1]), | ||
([8, 8, TILE_HEIGHT * 20 - 1, TILE_WIDTH * 20 - 1]), | ||
), | ||
ids=[ | ||
"1, 1, TILE_HEIGHT-1,TILE_WIDTH - 1", | ||
"4, 4, TILE_HEIGHT * 12 - 1, TILE_WIDTH * 30 - 1", | ||
"4, 4, TILE_HEIGHT * 30 - 1, TILE_WIDTH * 12 - 1", | ||
"8, 8, TILE_HEIGHT * 20 - 1, TILE_WIDTH * 20 - 1", | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"dims", | ||
( | ||
[0], | ||
[0, 1], | ||
[0, 1, 2], | ||
[0, 1, 2, 3], | ||
[0, 1, 3], | ||
[0, 2, 3], | ||
[1], | ||
[1, 2], | ||
[1, 2, 3], | ||
[1, 3], | ||
[2], | ||
[2, 3], | ||
[3], | ||
), | ||
ids=["0", "0,1", "0,1,2", "0,1,2,3", "0,1,3", "0,2,3", "1", "1,2", "1,2,3", "1,3", "2", "2,3", "3"], | ||
) | ||
def test_moreh_sum_backward(input_shape, dims, device): | ||
output_shape = input_shape.copy() | ||
|
||
for dim in dims: | ||
output_shape[dim] = 1 | ||
|
||
(_, _, torch_input) = get_tensors(input_shape, output_shape, device) | ||
(tt_output_grad, tt_input_grad, torch_output_grad) = get_backward_tensors(output_shape, input_shape, device) | ||
|
||
torch_output = torch.sum(torch_input, dims, True) | ||
torch_output.backward(torch_output_grad) | ||
|
||
cpu_layout = ttl.tensor.Layout.ROW_MAJOR | ||
tt_input_grad_cpu = ( | ||
ttl.operations.primary.moreh_sum_backward(tt_output_grad, tt_input_grad) | ||
.cpu() | ||
.to(cpu_layout) | ||
.unpad_from_tile(input_shape) | ||
.to_torch() | ||
) | ||
|
||
# test for equivalance | ||
rtol = atol = 0.1 | ||
passing, output_pcc = comp_allclose_and_pcc(torch_input.grad, tt_input_grad_cpu, pcc=0.999, rtol=rtol, atol=atol) | ||
|
||
logger.info(f"Out passing={passing}") | ||
logger.info(f"Output pcc={output_pcc}") | ||
|
||
assert passing |
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
52 changes: 0 additions & 52 deletions
52
tt_eager/tt_dnn/op_library/moreh_matmul_backward/kernels/reader_moreh_sum.cpp
This file was deleted.
Oops, something went wrong.
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.