forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_op_patch.py
More file actions
372 lines (316 loc) · 11.2 KB
/
math_op_patch.py
File metadata and controls
372 lines (316 loc) · 11.2 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# Copyright (c) 2018 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.
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
from paddle import _C_ops
from .. import core
from ..framework import convert_np_dtype_to_dtype_
if TYPE_CHECKING:
from paddle import Tensor
from paddle._typing import DTypeLike
_supported_int_dtype_ = [
core.VarDesc.VarType.UINT8,
core.VarDesc.VarType.INT8,
core.VarDesc.VarType.INT16,
core.VarDesc.VarType.INT32,
core.VarDesc.VarType.INT64,
core.VarDesc.VarType.BOOL,
]
# NOTE(chenweihang): We currently do not fully support the type promotion
# between tensors. Parting support here is because the interoperation of
# real and complex numbers in paddle quantum is very frequent, such as the
# binary operation between `float` and `complex64`, so we must support the
# correct type promotion on the APIs paddle quantum used.
# Now only check in dygraph (paddle quantum based dygraph)
# Full type promotion support will need to be fully verified later.
_supported_promote_complex_types_ = [
'__add__',
'__radd__',
'__sub__',
'__rsub__',
'__mul__',
'__rmul__',
'__div__',
'__truediv__',
'__rdiv__',
'__rtruediv__',
'__matmul__',
]
_complex_dtypes = [
core.VarDesc.VarType.COMPLEX64,
core.VarDesc.VarType.COMPLEX128,
]
_already_patch_eager_tensor = False
_supported_dtype_conversions = {
# float
'float16': 'float16',
'half': 'float16',
'bfloat16': 'bfloat16',
'float32': 'float32',
'float': 'float32',
'float64': 'float64',
'double': 'float64',
# int
'int8': 'int8',
'char': 'int8',
# We handle uint8 conversion separately
# 'uint8': 'uint8',
# 'byte': 'uint8',
'int16': 'int16',
'short': 'int16',
'int32': 'int32',
'int': 'int32',
'int64': 'int64',
'long': 'int64',
# other
'bool': 'bool',
'complex64': 'complex64',
'complex128': 'complex128',
'cfloat': 'complex64',
'cdouble': 'complex128',
}
def monkey_patch_math_tensor():
"""
Similar to monkey_patch_variable.
The difference is, in dygraph mode, use auto-generated op functions for better performance.
"""
def astype(self: Tensor, dtype: DTypeLike) -> Tensor:
"""
Cast a Tensor to a specified data type if it differs from the current dtype;
otherwise, return the original Tensor.
Args:
dtype: The target data type.
Returns:
Tensor: a new Tensor with target dtype
Examples:
.. code-block:: python
>>> import paddle
>>> import numpy as np
>>> original_tensor = paddle.ones([2, 2])
>>> print("original tensor's dtype is: {}".format(original_tensor.dtype))
original tensor's dtype is: paddle.float32
>>> new_tensor = original_tensor.astype('float32')
>>> print("new tensor's dtype is: {}".format(new_tensor.dtype))
new tensor's dtype is: paddle.float32
"""
if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)):
dtype = convert_np_dtype_to_dtype_(dtype)
if self.dtype == dtype:
return self
return _C_ops.cast(self, dtype)
def byte(self: Tensor) -> Tensor:
# since paddle don't support float to uint8, so we need to convert it to int8 first
if self.is_floating_point():
tensor = astype(self, 'int8')
return astype(tensor, 'uint8')
elif self.is_complex():
real = astype(self.real(), 'int8')
return astype(real, 'uint8')
else:
return astype(self, 'uint8')
def _create_dtype_conversion_methods():
"""
Batch create all data type conversion methods
"""
methods = []
for method_name, target_dtype in _supported_dtype_conversions.items():
def make_conversion_method(dtype):
def conversion_method(self: Tensor) -> Tensor:
return astype(self, dtype)
return conversion_method
method_impl = make_conversion_method(target_dtype)
method_impl.__name__ = method_name
method_impl.__doc__ = f"""
Cast a Tensor to {target_dtype} data type if it differs from the current dtype;
otherwise, return the original Tensor.
Returns:
Tensor: a new Tensor with {target_dtype} dtype
"""
methods.append((method_name, method_impl))
return methods
def type_as(self: Tensor, other: Tensor) -> Tensor:
return self.astype(other.dtype)
def _scalar_elementwise_op_(
var: Tensor, scale: float, bias: float
) -> Tensor:
return _C_ops.scale(var, float(scale), bias, True)
def _neg_(var: Tensor) -> Tensor:
return _scalar_elementwise_op_(var, -1.0, 0.0)
def _abs_(var: Tensor) -> Tensor:
return var.abs()
def _complex_(var: Tensor) -> complex:
numel = np.prod(var.shape)
assert (
numel == 1
), "only one element variable can be converted to complex."
assert var._is_initialized(), "variable's tensor is not initialized"
if not var.is_complex():
var = var.astype('complex64')
return complex(np.array(var))
def _float_(var: Tensor) -> float:
numel = np.prod(var.shape)
assert (
numel == 1
), "only one element variable can be converted to float."
assert var._is_initialized(), "variable's tensor is not initialized"
if (
var.dtype == core.VarDesc.VarType.BF16
or var.dtype == core.DataType.BFLOAT16
):
var = var.astype('float32')
return float(np.array(var))
def _long_(var: Tensor) -> int:
numel = np.prod(var.shape)
assert numel == 1, "only one element variable can be converted to long."
assert var._is_initialized(), "variable's tensor is not initialized"
if (
var.dtype == core.VarDesc.VarType.BF16
or var.dtype == core.DataType.BFLOAT16
):
var = var.astype('float32')
return int(np.array(var))
def _int_(var: Tensor) -> int:
numel = np.prod(var.shape)
assert numel == 1, "only one element variable can be converted to int."
assert var._is_initialized(), "variable's tensor is not initialized"
if (
var.dtype == core.VarDesc.VarType.BF16
or var.dtype == core.DataType.BFLOAT16
):
var = var.astype('float32')
return int(np.array(var))
def _len_(var: Tensor) -> int:
assert var.ndim > 0, "len() of a 0-D tensor is wrong"
if var.type == core.VarDesc.VarType.VOCAB:
return len(var.value().get_map_tensor())
elif var.type == core.VarDesc.VarType.STRINGS:
return len(var.value().get_string_tensor())
else:
return var.shape[0]
def _index_(var: Tensor) -> int:
numel = np.prod(var.shape)
assert (
numel == 1
), "only one element variable can be converted to python index."
assert var._is_initialized(), "variable's tensor is not initialized"
if (
var.dtype == core.VarDesc.VarType.BF16
or var.dtype == core.DataType.BFLOAT16
):
var = var.astype('float32')
return int(np.array(var))
@property
def _ndim(var: Tensor) -> int:
return len(var.shape)
def ndimension(var: Tensor) -> int:
return len(var.shape)
def dim(var: Tensor) -> int:
return len(var.shape)
@property
def _size_(var: Tensor) -> int:
return int(np.prod(var.shape))
@property
def _T_(var: Tensor) -> Tensor:
if len(var.shape) == 1:
return var
perm = list(reversed(range(len(var.shape))))
out = _C_ops.transpose(var, perm)
return out
@property
def _mT_(var: Tensor) -> Tensor:
if len(var.shape) < 2:
raise ValueError(
f"Tensor.ndim({var.ndim}) is required to be greater than or equal to 2."
)
perm = list(range(len(var.shape)))
perm[-1], perm[-2] = perm[-2], perm[-1]
out = _C_ops.transpose(var, perm)
return out
eager_methods = [
('__neg__', _neg_),
('__abs__', _abs_),
('__complex__', _complex_),
('__float__', _float_),
('__long__', _long_),
('__int__', _int_),
('__len__', _len_),
('__index__', _index_),
('astype', astype),
('byte', byte),
('uint8', byte),
('type_as', type_as),
('dim', dim),
('ndimension', ndimension),
('ndim', _ndim),
('size', _size_),
('T', _T_),
('mT', _mT_),
# for logical compare
('__array_ufunc__', None),
]
dtype_conversion_methods = _create_dtype_conversion_methods()
eager_methods.extend(dtype_conversion_methods)
eager_cpp_level_patch = [
"__add__",
"__radd__",
'__sub__',
'__rsub__',
'__mul__',
'__rmul__',
'__div__',
'__truediv__',
'__rdiv__',
'__rtruediv__',
'__mod__',
'__rmod__',
'__matmul__',
'__rmatmul__',
'__gt__',
'__ge__',
'__lt__',
'__le__',
'__floordiv__',
'__rfloordiv__',
'__pow__',
'__rpow__',
'__eq__',
'__ne__',
]
global _already_patch_eager_tensor
local_already_patch = _already_patch_eager_tensor
_already_patch_eager_tensor = True
local_tensor = core.eager.Tensor
if not local_already_patch:
for method_name in eager_cpp_level_patch:
method_impl = getattr(local_tensor, method_name, None)
if method_impl:
setattr(local_tensor, method_name, method_impl)
for method in eager_methods:
method_name = method[0]
method_impl = method[1]
setattr(local_tensor, method_name, method_impl)
else:
import paddle.tensor
# Tensor method from module paddle.tensor
for method_name in paddle.tensor.tensor_method_func:
if hasattr(local_tensor, method_name):
continue
method_impl = getattr(paddle.tensor, method_name, None)
if method_impl:
setattr(local_tensor, method_name, method_impl)
for magic_method, origin_method in paddle.tensor.magic_method_func:
impl = getattr(paddle.tensor, origin_method, None)
if impl:
setattr(local_tensor, magic_method, impl)