diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 17d210cc2f176..d4516943f4612 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; @@ -1242,6 +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 (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);