MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / _binary_creator_

Function _binary_creator_

python/paddle/pir/math_op_patch.py:522–616  ·  view source on GitHub ↗
(
        method_name,
        python_api,
        reverse=False,
        scalar_method=None,
    )

Source from the content-addressed store, hash-verified

520 return paddle.abs(var)
521
522 def _binary_creator_(
523 method_name,
524 python_api,
525 reverse=False,
526 scalar_method=None,
527 ):
528 def __impl__(self, other_var):
529 # 1. scalar exists cases
530 # we need combine the tensor.dtype and scalar.dtype, cast correct object
531 if isinstance(other_var, float):
532 # in all cases(+, -, *, /, **, //, %), we need cast tensor.dtype to float
533 if self.dtype in _supported_int_dtype_:
534 self = astype(self, DataType.FLOAT32)
535 # here use `scale` replace `elementwise` to get better performance
536 # but only +, -, *, / can use this method
537 if scalar_method is not None:
538 return scalar_method(self, other_var)
539 elif isinstance(other_var, int):
540 # in all cases(+, -, *, /, **, //, %), we can cast it to float
541 # because the output tensor.dtype depend on the type of input tensor
542 other_var = float(other_var)
543 # division is a special case
544 # NOTE(chenweihang): because we cast tensor to float32 instead float64,
545 # the division result can only guarantee the numerical accuracy of 6 digits
546 # after the decimal point. The result of numpy calculation is of float64 type,
547 # so the calculation result here and the calculation result of numpy are
548 # different after 6 decimal point. If necessary, we can also use float64 here.
549 # torch's behavior here is consistent with ours
550 if (
551 python_api == paddle.divide
552 and self.dtype in _supported_int_dtype_
553 ):
554 self = paddle.cast(self, DataType.FLOAT32)
555 # bool(tensor) + int(scalar) will do type promotion to int64
556 if self.dtype == paddle.bool:
557 self = paddle.cast(self, DataType.INT64)
558 # here use `scale` replace `elementwise` to get better performance
559 # but only +, -, *, / can use this method
560 if scalar_method is not None:
561 return scalar_method(self, other_var)
562 elif other_var is None:
563 if method_name == "__eq__":
564 return False
565 elif method_name == "__ne__":
566 return True
567 else:
568 pass
569 else:
570 # do nothing
571 pass
572
573 # 2. create Value for scalar
574 lhs_dtype = safe_get_dtype(self)
575 if not isinstance(other_var, Value):
576 if reverse:
577 for elem in self.shape:
578 if elem < 0:
579 other_var = create_tensor_with_batchsize(

Callers 1

monkey_patch_valueFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected