Skip to content

Commit ae93d9c

Browse files
change '/' method from scale Op to elementwise_div Op (#33279)
* fix the bug of div operation result using scale method do not exactly equal the result of elementwise_div method * remove __div__ , __rdiv__ methods which do not define in python3 * modify the note * add test case * add test case
1 parent 44054ba commit ae93d9c

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

python/paddle/fluid/dygraph/math_op_patch.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@
4646
'__rsub__',
4747
'__mul__',
4848
'__rmul__',
49-
'__div__',
5049
'__truediv__',
51-
'__rdiv__',
5250
'__rtruediv__',
5351
'__matmul__',
5452
]
@@ -168,9 +166,6 @@ def _scalar_rsub_(var, value):
168166
def _scalar_mul_(var, value):
169167
return _scalar_elementwise_op_(var, value, 0.0)
170168

171-
def _scalar_div_(var, value):
172-
return _scalar_elementwise_op_(var, 1.0 / value, 0.0)
173-
174169
# for binary operator such as elementwise, compare
175170
def _binary_creator_(method_name,
176171
op_type,
@@ -201,7 +196,10 @@ def __impl__(self, other_var):
201196
if op_type == 'elementwise_div' and self.dtype in _supported_int_dtype_:
202197
self = astype(self, 'float32')
203198
# here use `scale` replace `elementwise` to get better performance
204-
# but only +, -, *, / can use this method
199+
# but only +, -, * can use this method
200+
# NOTE(chentianyu03): / can not use `scale` method,because the result of
201+
# `scale` method (self*(1/other_var)) do not exactly equal with the result
202+
# of `elementwise_div` method.
205203
if scalar_method is not None:
206204
return scalar_method(self, other_var)
207205
else:
@@ -288,12 +286,8 @@ def __impl__(self, other_var):
288286
## a*b == b*a. Do not need to reverse explicitly
289287
('__rmul__',
290288
_binary_creator_('__rmul__', 'elementwise_mul', False, _scalar_mul_)),
291-
('__div__', _binary_creator_('__div__', 'elementwise_div', False,
292-
_scalar_div_)),
293289
('__truediv__', _binary_creator_('__truediv__', 'elementwise_div',
294-
False, _scalar_div_)),
295-
('__rdiv__', _binary_creator_('__rdiv__', 'elementwise_div', True,
296-
None)),
290+
False, None)),
297291
('__rtruediv__', _binary_creator_('rtruediv__', 'elementwise_div', True,
298292
None)),
299293
('__pow__', _binary_creator_('__pow__', 'elementwise_pow', False,

python/paddle/fluid/layers/math_op_patch.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939
"__rsub__": "A -= B",
4040
"__mul__": "A * B",
4141
"__rmul__": "A *= B",
42-
"__div__": "A / B",
4342
"__truediv__": "A / B",
44-
"__rdiv__": "A /= B",
4543
"__rtruediv__": "A /= B",
4644
"__pow__": "A ** B",
4745
"__rpow__": "A **= B",
@@ -209,9 +207,6 @@ def _scalar_rsub_(var, value):
209207
def _scalar_mul_(var, value):
210208
return _scalar_op_(var, value, 0.0)
211209

212-
def _scalar_div_(var, value):
213-
return _scalar_op_(var, 1.0 / value, 0.0)
214-
215210
def _binary_creator_(method_name,
216211
op_type,
217212
reverse=False,
@@ -241,7 +236,10 @@ def __impl__(self, other_var):
241236
if op_type == 'elementwise_div' and self.dtype in _supported_int_dtype_:
242237
self = astype(self, 'float32')
243238
# here use `scale` replace `elementwise` to get better performance
244-
# but only +, -, *, / can use this method
239+
# but only +, -, * can use this method
240+
# NOTE(chentianyu03): / can not use `scale` method,because the result of
241+
# `scale` method (self*(1/other_var)) do not exactly equal with the result
242+
# of `elementwise_div` method.
245243
if scalar_method is not None:
246244
return scalar_method(self, other_var)
247245
else:
@@ -337,12 +335,8 @@ def __impl__(self, other_var):
337335
# a*b == b*a. Do not need to reverse explicitly
338336
('__rmul__',
339337
_binary_creator_('__rmul__', 'elementwise_mul', False, _scalar_mul_)),
340-
('__div__', _binary_creator_('__div__', 'elementwise_div', False,
341-
_scalar_div_)),
342338
('__truediv__', _binary_creator_('__truediv__', 'elementwise_div',
343-
False, _scalar_div_)),
344-
('__rdiv__', _binary_creator_('__rdiv__', 'elementwise_div', True,
345-
None)),
339+
False, None)),
346340
('__rtruediv__', _binary_creator_('__rtruediv__', 'elementwise_div',
347341
True, None)),
348342
('__pow__', _binary_creator_('__pow__', 'elementwise_pow', False,

python/paddle/fluid/tests/unittests/test_tensor_scalar_type_promotion_dynamic.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ def test_tensor_div_scalar(self):
187187
c = paddle.full([2, 2, 2], 0.5, dtype="float32")
188188
self.check_operation(a, b, c, '/')
189189

190+
# tensor(float32) / scalar(int)
191+
# this behavior should be equal to elementwise_div Op
192+
a = paddle.to_tensor([99, 99, 99], dtype='float32')
193+
b = 100
194+
c = a / paddle.to_tensor([100, 100, 100], dtype='float32')
195+
self.check_operation(a, b, c, '/')
196+
190197
# tensor(int64) / scalar(float, .0)
191198
a = paddle.ones([2, 2, 2], dtype='int64')
192199
b = 2.0

python/paddle/fluid/tests/unittests/test_tensor_scalar_type_promotion_static.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ def test_tensor_div_scalar(self):
218218
c = paddle.full([2, 2, 2], 0.5, dtype="float32")
219219
self.check_operation(a, b, c, '/')
220220

221+
# this behavior should be equal to elementwise_div Op
222+
a = paddle.full([2, 2, 2], 99, dtype="float32")
223+
b = 100
224+
c = a / paddle.full([2, 2, 2], 100, dtype="float32")
225+
self.check_operation(a, b, c, '/')
226+
221227
# tensor(int64) / scalar(float, .0)
222228
with program_guard(Program()):
223229
a = paddle.ones([2, 2, 2], dtype='int64')

0 commit comments

Comments
 (0)