forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_tensor_attr_consistency.py
More file actions
186 lines (171 loc) · 5.35 KB
/
test_tensor_attr_consistency.py
File metadata and controls
186 lines (171 loc) · 5.35 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import paddle
from paddle.jit.utils import OrderedSet
DYGRAPH_ONLY_TENSOR_ATTRS_ALLOW_LIST = OrderedSet(
[
'__array__',
'__array_ufunc__',
'__deepcopy__',
'__index__',
'__len__',
'__long__',
'__nonzero__',
'__dict__',
'apply_',
'backward',
'clear_grad',
'coalesce',
'cols',
'copy_',
'crows',
'data',
'data_ptr',
'detach_',
'fill_',
'fill_diagonal_',
'fill_diagonal_tensor',
'fill_diagonal_tensor_',
'get_map_tensor',
'get_selected_rows',
'get_strides',
'get_tensor',
'grad',
'grad_',
'grad_fn',
'gradient',
'inplace_version',
'is_dense',
'is_dist',
'is_leaf',
'is_same_shape',
'is_selected_rows',
'is_sparse',
'is_sparse_coo',
'is_sparse_csr',
'layout',
'nnz',
'num_shard',
'offset',
'pin_memory',
'placements',
'process_mesh',
'reconstruct_from_',
'retain_grads',
'rows',
'set_string_list',
'set_value',
'set_tensor',
'set_vocab',
'strides',
'to_sparse_coo',
'to_sparse_csr',
'value',
'zero_',
"__cuda_array_interface__",
'__dlpack__',
"__dlpack_device__",
"__tvm_ffi_env_stream__",
"__c_dlpack_exchange_api__",
]
)
STATIC_ONLY_TENSOR_ATTRS_ALLOW_LIST = OrderedSet(
[
'__dict__',
'all_used_ops',
'append',
'first_use',
'index',
'get_defining_op',
'has_one_use',
'has_name',
'hash',
'id',
'initialized',
'is_dense_tensor_array_type',
'is_dense_tensor_type',
'is_same',
'is_selected_row_type',
'pop',
'replace_all_uses_with',
'set_shape',
'set_type',
'use_empty',
'is_dist_dense_tensor_type',
'dist_attr',
'place_attr',
'update_dist_attr',
'is_combine',
'value_assign',
'replace_grad_users_with',
'do_model_average',
'is_distributed',
'is_parameter',
'need_clip',
'optimize_attr',
'regularizer',
'trainable',
'is_sparse_coo_tensor_type',
'is_sparse_csr_tensor_type',
'all_used_ops_in_same_block',
]
)
def is_magic_method(attr):
return attr.startswith('__') and attr.endswith('__')
def get_public_attributes(cls):
return [
attr
for attr in dir(cls)
if not attr.startswith('_') or is_magic_method(attr)
]
class TestTensorAttrConsistency(unittest.TestCase):
def get_tensor_dygraph_and_static_attrs(self):
dygraph_tensor_cls = paddle.Tensor
static_tensor_cls = paddle.pir.Value
dygraph_tensor_attrs = get_public_attributes(dygraph_tensor_cls)
static_tensor_attrs = get_public_attributes(static_tensor_cls)
return OrderedSet(dygraph_tensor_attrs), OrderedSet(static_tensor_attrs)
def test_dygraph_tensor_attr_consistency_check(self):
(
dygraph_tensor_attrs,
static_tensor_attrs,
) = self.get_tensor_dygraph_and_static_attrs()
dygraph_only_attrs = dygraph_tensor_attrs - static_tensor_attrs
not_allow_dygraph_only_attrs = (
dygraph_only_attrs - DYGRAPH_ONLY_TENSOR_ATTRS_ALLOW_LIST
)
self.assertEqual(
len(not_allow_dygraph_only_attrs),
0,
f"Value should have same attributes as Tensor, but found dygraph only tensor attributes: {not_allow_dygraph_only_attrs}.\n"
+ "If you believe these attributes are not supported in static graph, please add them to DYGRAPH_ONLY_TENSOR_ATTRS_ALLOW_LIST in test_dygraph_to_static/test_tensor_attr_consistency.py.",
)
def test_static_tensor_attr_consistency_check(self):
(
dygraph_tensor_attrs,
static_tensor_attrs,
) = self.get_tensor_dygraph_and_static_attrs()
static_only_attrs = static_tensor_attrs - dygraph_tensor_attrs
not_allow_static_only_attrs = (
static_only_attrs - STATIC_ONLY_TENSOR_ATTRS_ALLOW_LIST
)
self.assertEqual(
len(not_allow_static_only_attrs),
0,
f"Tensor should have same attributes as Value, but found static only tensor attributes: {not_allow_static_only_attrs}.\n"
+ "If you believe these attributes are not supported in dygraph, please add them to STATIC_ONLY_TENSOR_ATTRS_ALLOW_LIST in test_dygraph_to_static/test_tensor_attr_consistency.py.",
)
if __name__ == '__main__':
unittest.main()