Skip to content

Commit 41500d1

Browse files
Revert "change '/' method from scale Op to elementwise_div Op (#33279)"
This reverts commit ae93d9c.
1 parent 5bdca05 commit 41500d1

4 files changed

Lines changed: 22 additions & 23 deletions

File tree

python/paddle/fluid/dygraph/math_op_patch.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
'__rsub__',
4747
'__mul__',
4848
'__rmul__',
49+
'__div__',
4950
'__truediv__',
51+
'__rdiv__',
5052
'__rtruediv__',
5153
'__matmul__',
5254
]
@@ -170,6 +172,9 @@ def _scalar_rsub_(var, value):
170172
def _scalar_mul_(var, value):
171173
return _scalar_elementwise_op_(var, value, 0.0)
172174

175+
def _scalar_div_(var, value):
176+
return _scalar_elementwise_op_(var, 1.0 / value, 0.0)
177+
173178
# for binary operator such as elementwise, compare
174179
def _binary_creator_(method_name,
175180
op_type,
@@ -200,10 +205,7 @@ def __impl__(self, other_var):
200205
if op_type == 'elementwise_div' and self.dtype in _supported_int_dtype_:
201206
self = astype(self, 'float32')
202207
# here use `scale` replace `elementwise` to get better performance
203-
# but only +, -, * can use this method
204-
# NOTE(chentianyu03): / can not use `scale` method,because the result of
205-
# `scale` method (self*(1/other_var)) do not exactly equal with the result
206-
# of `elementwise_div` method.
208+
# but only +, -, *, / can use this method
207209
if scalar_method is not None:
208210
return scalar_method(self, other_var)
209211
else:
@@ -296,8 +298,12 @@ def __impl__(self, other_var):
296298
## a*b == b*a. Do not need to reverse explicitly
297299
('__rmul__',
298300
_binary_creator_('__rmul__', 'elementwise_mul', False, _scalar_mul_)),
301+
('__div__', _binary_creator_('__div__', 'elementwise_div', False,
302+
_scalar_div_)),
299303
('__truediv__', _binary_creator_('__truediv__', 'elementwise_div',
300-
False, None)),
304+
False, _scalar_div_)),
305+
('__rdiv__', _binary_creator_('__rdiv__', 'elementwise_div', True,
306+
None)),
301307
('__rtruediv__', _binary_creator_('rtruediv__', 'elementwise_div', True,
302308
None)),
303309
('__pow__', _binary_creator_('__pow__', 'elementwise_pow', False,

python/paddle/fluid/layers/math_op_patch.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
"__rsub__": "A -= B",
4141
"__mul__": "A * B",
4242
"__rmul__": "A *= B",
43+
"__div__": "A / B",
4344
"__truediv__": "A / B",
45+
"__rdiv__": "A /= B",
4446
"__rtruediv__": "A /= B",
4547
"__pow__": "A ** B",
4648
"__rpow__": "A **= B",
@@ -249,6 +251,9 @@ def _scalar_rsub_(var, value):
249251
def _scalar_mul_(var, value):
250252
return _scalar_op_(var, value, 0.0)
251253

254+
def _scalar_div_(var, value):
255+
return _scalar_op_(var, 1.0 / value, 0.0)
256+
252257
def _binary_creator_(method_name,
253258
op_type,
254259
reverse=False,
@@ -278,10 +283,7 @@ def __impl__(self, other_var):
278283
if op_type == 'elementwise_div' and self.dtype in _supported_int_dtype_:
279284
self = astype(self, 'float32')
280285
# here use `scale` replace `elementwise` to get better performance
281-
# but only +, -, * can use this method
282-
# NOTE(chentianyu03): / can not use `scale` method,because the result of
283-
# `scale` method (self*(1/other_var)) do not exactly equal with the result
284-
# of `elementwise_div` method.
286+
# but only +, -, *, / can use this method
285287
if scalar_method is not None:
286288
return scalar_method(self, other_var)
287289
else:
@@ -381,8 +383,12 @@ def __impl__(self, other_var):
381383
# a*b == b*a. Do not need to reverse explicitly
382384
('__rmul__',
383385
_binary_creator_('__rmul__', 'elementwise_mul', False, _scalar_mul_)),
386+
('__div__', _binary_creator_('__div__', 'elementwise_div', False,
387+
_scalar_div_)),
384388
('__truediv__', _binary_creator_('__truediv__', 'elementwise_div',
385-
False, None)),
389+
False, _scalar_div_)),
390+
('__rdiv__', _binary_creator_('__rdiv__', 'elementwise_div', True,
391+
None)),
386392
('__rtruediv__', _binary_creator_('__rtruediv__', 'elementwise_div',
387393
True, None)),
388394
('__pow__', _binary_creator_('__pow__', 'elementwise_pow', False,

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,6 @@ 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-
197190
# tensor(int64) / scalar(float, .0)
198191
a = paddle.ones([2, 2, 2], dtype='int64')
199192
b = 2.0

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,6 @@ 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-
227221
# tensor(int64) / scalar(float, .0)
228222
with program_guard(Program()):
229223
a = paddle.ones([2, 2, 2], dtype='int64')

0 commit comments

Comments
 (0)