From dd4eaf5c23046d07a4578a219e2dd3622e5620fa Mon Sep 17 00:00:00 2001 From: Zhennan Qin Date: Thu, 24 Oct 2019 05:05:06 -0500 Subject: [PATCH] [MKLDNN]Fix reorder2default (#16602) * Fix reorder2default Change-Id: I74c87af9535f6264e6d1ea7eaed089a6480a3358 * fix Change-Id: I6d07b43b520a47e7c78bd4b4b6390f5fb95e6957 * Fix Change-Id: Id72f25c34291be4711f55569c6d61467edd6113d * Fix CI Change-Id: I8c33a82555d5ace2d0b682c1e3eefa13f3a44768 * Run CI Change-Id: Ie8a6dab80ef91c0337cafbae4e3db277e0c7ebf7 --- src/ndarray/ndarray.cc | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/ndarray/ndarray.cc b/src/ndarray/ndarray.cc index 44da670b800d..aaa7aedf8bcd 100644 --- a/src/ndarray/ndarray.cc +++ b/src/ndarray/ndarray.cc @@ -1635,11 +1635,13 @@ void NDArray::Save(dmlc::Stream *strm) const { nd_cpu.WaitToRead(); save_data = nd_cpu.data(); } else { +#if MXNET_USE_MKLDNN == 1 + // For mkldnn, a copy of *this can ensure no write access pending on *this. + nd_cpu = this->Copy(Context::CPU()); + nd_cpu.WaitToRead(); +#else this->WaitToRead(); nd_cpu = *this; -#if MXNET_USE_MKLDNN == 1 - if (nd_cpu.IsMKLDNNData()) - nd_cpu = nd_cpu.Reorder2Default(); #endif save_data = nd_cpu.data(); } @@ -2024,15 +2026,18 @@ void NDArray::SyncCopyToCPU(void *data, size_t size) const { TBlob dst(data, dshape, cpu::kDevMask, this->dtype_, 0); // NOLINT(*) if (this->ctx().dev_mask() == cpu::kDevMask) { - this->WaitToRead(); - RunContext rctx{this->ctx(), nullptr, nullptr, false}; - NDArray src = *this; + Engine::Get()->PushAsync( + [&](RunContext rctx, Engine::CallbackOnComplete on_complete) { + RunContext ctx{this->ctx(), nullptr, nullptr, false}; + NDArray src = *this; #if MXNET_USE_MKLDNN == 1 - if (src.IsMKLDNNData()) - src = this->Reorder2Default(); + src = this->Reorder2Default(); #endif - ndarray::Copy(src.data(), &dst, - Context::CPU(), Context::CPU(), rctx); + ndarray::Copy(src.data(), &dst, Context::CPU(), Context::CPU(), ctx); + on_complete(); + }, + this->ctx(), {this->var()}, {}, FnProperty::kNormal, 0, "SyncCopyCPU2CPU"); + this->WaitToWrite(); } else { #if MXNET_USE_CUDA Engine::Get()->PushAsync(