From da909dc380445b17bb6a27941a4776d4d3963321 Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Tue, 29 Nov 2022 10:35:00 +0000 Subject: [PATCH 1/3] [Eager] fix set_value logic when input's dtype is different --- paddle/fluid/pybind/eager_method.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 17d210cc2f176..02c6b014c217e 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -1227,7 +1227,6 @@ static PyObject* tensor_method__setitem_eager_tensor(TensorObject* self, Py_TYPE(value_obj))); } } - { // Release gil and do tracing py::gil_scoped_release release; @@ -1243,6 +1242,9 @@ static PyObject* tensor_method__setitem_eager_tensor(TensorObject* self, value_tensor = egr::EagerAmpAutoCast( value_tensor.name(), value_tensor, amp_dtype, "set_value"); } + if (self->tensor.dtype() != value_tensor.dtype()) { + value_tensor = cast_ad_func(value_tensor, self->tensor.dtype()); + } self->tensor = set_value__dygraph_function( self->tensor, value_tensor, {}, {}, {}, attrs); } From c1518728f90a676664f08a960f7057fd8bb24a58 Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Thu, 1 Dec 2022 13:00:16 +0000 Subject: [PATCH 2/3] value_tensor --- paddle/fluid/pybind/eager_method.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 02c6b014c217e..8d7fbc3c1120b 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -1242,7 +1242,8 @@ static PyObject* tensor_method__setitem_eager_tensor(TensorObject* self, value_tensor = egr::EagerAmpAutoCast( value_tensor.name(), value_tensor, amp_dtype, "set_value"); } - if (self->tensor.dtype() != value_tensor.dtype()) { + if (value_tensor.initialized() && + self->tensor.dtype() != value_tensor.dtype()) { value_tensor = cast_ad_func(value_tensor, self->tensor.dtype()); } self->tensor = set_value__dygraph_function( From 706f28afa60cbe1093a208cc1303d14e2f1fedf3 Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Fri, 2 Dec 2022 04:48:58 +0000 Subject: [PATCH 3/3] fix set_value logic when input's dtype is different --- paddle/fluid/pybind/eager_method.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 8d7fbc3c1120b..d4516943f4612 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -1241,10 +1241,9 @@ static PyObject* tensor_method__setitem_eager_tensor(TensorObject* self, self->tensor.name(), self->tensor, amp_dtype, "set_value"); value_tensor = egr::EagerAmpAutoCast( value_tensor.name(), value_tensor, amp_dtype, "set_value"); - } - if (value_tensor.initialized() && - self->tensor.dtype() != value_tensor.dtype()) { - value_tensor = cast_ad_func(value_tensor, self->tensor.dtype()); + if (self->tensor.dtype() != value_tensor.dtype()) { + value_tensor = cast_ad_func(value_tensor, self->tensor.dtype()); + } } self->tensor = set_value__dygraph_function( self->tensor, value_tensor, {}, {}, {}, attrs);