-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#6285: Add backward support for floor round and div_no_nan
- Loading branch information
Showing
7 changed files
with
211 additions
and
0 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
37 changes: 37 additions & 0 deletions
37
tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_floor.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,37 @@ | ||
# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import torch | ||
import pytest | ||
import tt_lib | ||
from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import compare_results, data_gen_pt_tt | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_shapes", | ||
( | ||
(torch.Size([1, 1, 32, 32])), | ||
(torch.Size([1, 1, 320, 384])), | ||
(torch.Size([1, 3, 320, 384])), | ||
), | ||
) | ||
def test_bw_floor(input_shapes, device): | ||
grad_data, grad_tensor = data_gen_pt_tt(input_shapes, device) | ||
in_data = torch.Tensor(size=input_shapes).uniform_() | ||
in_data.requires_grad = True | ||
input_tensor = ( | ||
tt_lib.tensor.Tensor(in_data, tt_lib.tensor.DataType.BFLOAT16).to(tt_lib.tensor.Layout.TILE).to(device) | ||
) | ||
|
||
pyt_y = torch.floor(in_data) | ||
|
||
tt_output_tensor_on_device = tt_lib.tensor.floor_bw(grad_tensor) | ||
|
||
in_data.retain_grad() | ||
|
||
pyt_y.backward(gradient=grad_data) | ||
|
||
golden_tensor = [in_data.grad] | ||
comp_pass = compare_results(tt_output_tensor_on_device, golden_tensor) | ||
assert comp_pass |
37 changes: 37 additions & 0 deletions
37
tests/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_round.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,37 @@ | ||
# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import torch | ||
import pytest | ||
import tt_lib | ||
from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import compare_results, data_gen_pt_tt | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_shapes", | ||
( | ||
(torch.Size([1, 1, 32, 32])), | ||
(torch.Size([1, 1, 320, 384])), | ||
(torch.Size([1, 3, 320, 384])), | ||
), | ||
) | ||
def test_bw_round(input_shapes, device): | ||
grad_data, grad_tensor = data_gen_pt_tt(input_shapes, device) | ||
in_data = torch.Tensor(size=input_shapes).uniform_() | ||
in_data.requires_grad = True | ||
input_tensor = ( | ||
tt_lib.tensor.Tensor(in_data, tt_lib.tensor.DataType.BFLOAT16).to(tt_lib.tensor.Layout.TILE).to(device) | ||
) | ||
|
||
pyt_y = torch.round(in_data) | ||
|
||
tt_output_tensor_on_device = tt_lib.tensor.round_bw(grad_tensor) | ||
|
||
in_data.retain_grad() | ||
|
||
pyt_y.backward(gradient=grad_data) | ||
|
||
golden_tensor = [in_data.grad] | ||
comp_pass = compare_results(tt_output_tensor_on_device, golden_tensor) | ||
assert comp_pass |
40 changes: 40 additions & 0 deletions
40
...s/tt_eager/python_api_testing/unit_testing/backward_ops/test_backward_unary_div_no_nan.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,40 @@ | ||
# SPDX-FileCopyrightText: © 2023 Tenstorrent Inc. | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import torch | ||
import pytest | ||
import tt_lib | ||
from tests.tt_eager.python_api_testing.unit_testing.backward_ops.utility_funcs import data_gen_pt_tt, compare_results | ||
|
||
|
||
def torch_div_no_nan(input, scalar): | ||
return torch.where(torch.tensor(scalar) == 0, torch.zeros_like(input), torch.div(input, scalar)) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_shapes", | ||
( | ||
(torch.Size([1, 1, 32, 32])), | ||
(torch.Size([1, 1, 320, 384])), | ||
(torch.Size([1, 3, 320, 384])), | ||
), | ||
) | ||
@pytest.mark.parametrize("scalar", [0.05, 0.0, -0.5, 5.12]) | ||
def test_bw_unary_div_no_nan(input_shapes, scalar, device): | ||
in_data, input_tensor = data_gen_pt_tt(input_shapes, device, True) | ||
grad_data, grad_tensor = data_gen_pt_tt(input_shapes, device) | ||
|
||
tt_output_tensor_on_device = tt_lib.tensor.unary_div_no_nan_bw(grad_tensor, input_tensor, scalar=scalar) | ||
|
||
in_data.retain_grad() | ||
|
||
pyt_y = torch_div_no_nan(in_data, scalar) | ||
|
||
pyt_y.backward(gradient=grad_data) | ||
|
||
golden_tensor = [in_data.grad] | ||
golden_tensor[0] = torch.where(torch.isnan(golden_tensor[0]), torch.zeros_like(in_data), golden_tensor[0]) | ||
|
||
status = compare_results(tt_output_tensor_on_device, golden_tensor) | ||
assert status |
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
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