-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtest_trilu.py
94 lines (69 loc) · 3.01 KB
/
test_trilu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright (C) 2018-2025 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import pytest
from pytorch_layer_test_class import PytorchLayerTest
class TestTriuTril(PytorchLayerTest):
def _prepare_input(self, shape, dtype):
import numpy as np
return (np.random.randn(*shape).astype(dtype),)
def create_model(self, op, diagonal):
import torch
op_map = {
"tril": torch.tril,
"triu": torch.triu
}
pt_op = op_map[op]
class aten_trilu(torch.nn.Module):
def __init__(self, op, diagonal):
super(aten_trilu, self).__init__()
self.op = op
self.diagonal = diagonal
def forward(self, x):
return self.op(x, self.diagonal)
ref_net = None
return aten_trilu(pt_op, diagonal), ref_net, f"aten::{op}"
@pytest.mark.parametrize("dtype", ["float32", "float64", "int32", "int64", "int8", "uint8", "bool"])
@pytest.mark.parametrize("diagonal", [0, 1, 2, -1, -2])
@pytest.mark.parametrize("op", ["triu", "tril"])
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_fx_backend
def test_trilu(self, dtype, diagonal, op, ie_device, precision, ir_version):
self._test(*self.create_model(op, diagonal), ie_device, precision, ir_version,
kwargs_to_prepare_input={"shape": (4, 6), "dtype": dtype})
class TestTriuTrilTensor(PytorchLayerTest):
def _prepare_input(self, shape, dtype):
import numpy as np
return (np.random.randn(*shape).astype(dtype),)
def create_model(self, op, diagonal):
import torch
class aten_trilu(torch.nn.Module):
def __init__(self, op, diagonal):
super(aten_trilu, self).__init__()
op_map = {
"tril": self.tril,
"tril_": self.tril_,
"triu": self.triu,
"triu_": self.triu_
}
self.diagonal = diagonal
self.forward = op_map[op]
def tril(self, x):
return x.tril(self.diagonal), x
def tril_(self, x):
return x.tril_(self.diagonal), x
def triu(self, x):
return x.triu(self.diagonal), x
def triu_(self, x):
return x.triu_(self.diagonal), x
ref_net = None
return aten_trilu(op, diagonal), ref_net, f"aten::{op}"
@pytest.mark.parametrize("dtype", ["float32", "float64", "int32", "int64", "int8", "uint8", "bool"])
@pytest.mark.parametrize("diagonal", [0, 1, 2, -1, -2])
@pytest.mark.parametrize("op", ["triu", "tril", "triu_", "tril_"])
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_fx_backend
def test_trilu(self, dtype, diagonal, op, ie_device, precision, ir_version):
self._test(*self.create_model(op, diagonal), ie_device, precision, ir_version,
kwargs_to_prepare_input={"shape": (4, 6), "dtype": dtype})