diff --git a/mindspore/ccsrc/runtime/device/cpu/cpu_device_address.cc b/mindspore/ccsrc/runtime/device/cpu/cpu_device_address.cc index c4e39bd196..222e4daf11 100644 --- a/mindspore/ccsrc/runtime/device/cpu/cpu_device_address.cc +++ b/mindspore/ccsrc/runtime/device/cpu/cpu_device_address.cc @@ -36,6 +36,11 @@ bool CPUDeviceAddress::DumpMemToFile(const std::string &filepath, const std::str } bool CPUDeviceAddress::SyncDeviceToHost(const ShapeVector &, size_t size, TypeId type, void *host_ptr) const { + // The input or output may be empty. + if ((size == 0) || (size_ == 0)) { + MS_LOG(INFO) << "No need sync, host size: " << size << ", device size: " << size_; + return true; + } if (ptr_ == nullptr) { MS_LOG(ERROR) << "The pointer ptr_ is null!"; return false; @@ -46,7 +51,7 @@ bool CPUDeviceAddress::SyncDeviceToHost(const ShapeVector &, size_t size, TypeId } if (type == type_id_) { - if ((size == 0) || (size_ == 0) || (size > size_)) { + if (size > size_) { MS_LOG(INFO) << "No need sync, host size: " << size << ", device size: " << size_; return true; } @@ -73,6 +78,11 @@ bool CPUDeviceAddress::SyncDeviceToHost(const ShapeVector &, size_t size, TypeId bool CPUDeviceAddress::SyncHostToDevice(const ShapeVector & /* shape */, size_t size, TypeId type, const void *host_ptr, const std::string &format) const { + // The input or output may be empty. + if ((size == 0) || (size_ == 0)) { + MS_LOG(INFO) << "No need sync, host size: " << size << ", device size: " << size_; + return true; + } if (ptr_ == nullptr) { MS_LOG(ERROR) << "The pointer ptr_ is null!"; return false; @@ -83,7 +93,7 @@ bool CPUDeviceAddress::SyncHostToDevice(const ShapeVector & /* shape */, size_t } if (type == type_id_) { - if ((size == 0) || (size_ == 0) || (size > size_)) { + if (size > size_) { MS_LOG(INFO) << "No need sync, host size: " << size << ", device size: " << size_; return true; } diff --git a/mindspore/ccsrc/runtime/device/gpu/gpu_device_address.cc b/mindspore/ccsrc/runtime/device/gpu/gpu_device_address.cc index a0332788be..eed333d7a1 100644 --- a/mindspore/ccsrc/runtime/device/gpu/gpu_device_address.cc +++ b/mindspore/ccsrc/runtime/device/gpu/gpu_device_address.cc @@ -35,16 +35,17 @@ namespace mindspore { namespace device { namespace gpu { bool GPUDeviceAddress::SyncDeviceToHost(size_t size, void *host_ptr) const { - MS_EXCEPTION_IF_NULL(host_ptr); - if (ptr_ == nullptr) { - MS_LOG(ERROR) << "The device address is null!"; - return false; - } + // The input or output may be empty. bool need_sync = (size != 0) && (size_ != 0) && (size <= size_); if (!need_sync) { MS_LOG(INFO) << "No need sync, host size: " << size << ", device size: " << size_; return true; } + MS_EXCEPTION_IF_NULL(host_ptr); + if (ptr_ == nullptr) { + MS_LOG(ERROR) << "The device address is null!"; + return false; + } auto &stream = GPUDeviceManager::GetInstance().default_stream(); MS_EXCEPTION_IF_NULL(stream); @@ -64,16 +65,17 @@ bool GPUDeviceAddress::SyncDeviceToHost(size_t size, void *host_ptr) const { } bool GPUDeviceAddress::SyncHostToDevice(size_t size, const void *host_ptr) const { - MS_EXCEPTION_IF_NULL(host_ptr); - if (ptr_ == nullptr) { - MS_LOG(ERROR) << "The device address is null!"; - return false; - } + // The input or output may be empty. bool need_sync = (size != 0) && (size_ != 0) && (size <= size_); if (!need_sync) { MS_LOG(INFO) << "No need sync, host size: " << size << ", device size: " << size_; return true; } + MS_EXCEPTION_IF_NULL(host_ptr); + if (ptr_ == nullptr) { + MS_LOG(ERROR) << "The device address is null!"; + return false; + } if (size != size_) { // nccl kernel input and output device address is aligned, may lead to host size is not equal to device size