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

Function tensor__add__method

paddle/fluid/pybind/eager_math_op_patch.cc:256–368  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

254}
255
256static PyObject* tensor__add__method(TensorObject* self,
257 PyObject* args,
258 PyObject* kwargs) {
259 phi::RecordEvent pythonc_record_event("__add__ or __radd_ pybind_patch_func",
260 phi::TracerEventType::UserDefined,
261 1);
262
263 EAGER_TRY
264 VLOG(6) << "Running Eager tensor__add__method";
265
266 SetPythonStack();
267
268 // Set Device ID
269 auto place = egr::Controller::Instance().GetExpectedPlace();
270 SetDevice(place);
271
272 Tensor ret;
273 Tensor self_tensor = self->tensor;
274 PyObject* other_obj = PyTuple_GET_ITEM(args, 0);
275
276 // 1. scalar exists cases
277 if (PyFloat_Check(other_obj) || PyCheckInteger(other_obj) ||
278 IsNumpyType(other_obj)) {
279 if (PyFloat_Check(other_obj)) {
280 if (_supported_int_dtype_.find(self_tensor.dtype()) !=
281 _supported_int_dtype_.end()) {
282 eager_gil_scoped_release guard;
283 self_tensor = cast_ad_func(self_tensor, DataType::FLOAT32);
284 }
285 } else if (PyCheckInteger(other_obj) &&
286 self_tensor.dtype() == DataType::BOOL) {
287 eager_gil_scoped_release guard;
288 self_tensor = cast_ad_func(self_tensor, DataType::INT64);
289 }
290
291 double other = CastPyArg2Double(other_obj, "__add__", 0);
292 {
293 eager_gil_scoped_release guard;
294 ret = CallScalarFunction(self_tensor, other, "add");
295 }
296 return ToPyObject(ret);
297 } else if (PyComplex_Check(other_obj)) {
298 if (is_support_complex(self_tensor.dtype()) == false) {
299 eager_gil_scoped_release guard;
300 self_tensor = cast_ad_func(
301 self_tensor, promoteTypes(self_tensor.dtype(), DataType::COMPLEX64));
302 }
303 }
304
305 // 2. create or get tensor for other_obj
306 Tensor other_tensor;
307
308 if (PyCheckTensor(other_obj)) {
309 auto& self_tensor_ref_addr = self->tensor;
310 auto& other_tensor_ref_addr = CastPyArg2Tensor(other_obj, 0);
311 const phi::distributed::ProcessMesh* mesh = nullptr;
312 if (InputsContainDistTensor(
313 &mesh, self_tensor_ref_addr, other_tensor_ref_addr)) {

Callers

nothing calls this directly

Calls 15

SetPythonStackFunction · 0.85
InstanceFunction · 0.85
CastPyArg2DoubleFunction · 0.85
CallScalarFunctionFunction · 0.85
is_support_complexFunction · 0.85
promoteTypesFunction · 0.85
PyCheckTensorFunction · 0.85
InputsContainDistTensorFunction · 0.85
CastPyArg2ScalarFunction · 0.85
GetExpectedPlaceMethod · 0.80
SetDeviceFunction · 0.70

Tested by

no test coverage detected