Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[mkldnn-v1.0] Add MKL-DNN BN #16199

Merged
merged 4 commits into from
Sep 23, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change mkldnn_args_map_t
  • Loading branch information
rongzha1 committed Sep 23, 2019
commit 1eb2c81ce8be955f88c3d1899190ee29fc737585
38 changes: 18 additions & 20 deletions src/operator/nn/mkldnn/mkldnn_batch_norm-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ void MKLDNNBatchNormForward(const OpContext &ctx, const BatchNormParam &param,
}
}

std::unordered_map<int, mkldnn::memory> net_args;
net_args.insert({MKLDNN_ARG_SRC, *(data.GetMKLDNNData())});
net_args.insert({MKLDNN_ARG_SCALE_SHIFT, weight_mem});
net_args.insert({MKLDNN_ARG_DST, *out_mem});
mkldnn_args_map_t net_args;
net_args[MKLDNN_ARG_SRC] = *data.GetMKLDNNData();
net_args[MKLDNN_ARG_SCALE_SHIFT] = weight_mem;
net_args[MKLDNN_ARG_DST] = *out_mem;

if (!ctx.is_train || param.use_global_stats) {
DType* omean = out_data[batchnorm::kMean].data().dptr<DType>();
Expand All @@ -210,17 +210,15 @@ void MKLDNNBatchNormForward(const OpContext &ctx, const BatchNormParam &param,
omean[i] = inmean[i];
ovar[i] = VARIANCE_TO_INVSTD(invar[i], param.eps);
}

net_args.insert({MKLDNN_ARG_MEAN, *(aux_states[batchnorm::kMovingMean].GetMKLDNNData())});
net_args.insert({MKLDNN_ARG_VARIANCE, *(aux_states[batchnorm::kMovingVar].GetMKLDNNData())});
net_args[MKLDNN_ARG_MEAN] = *(aux_states[batchnorm::kMovingMean].GetMKLDNNData());
net_args[MKLDNN_ARG_VARIANCE] = *(aux_states[batchnorm::kMovingVar].GetMKLDNNData());
MKLDNNStream::Get()->RegisterPrimArgs(fwd.GetFwd(), net_args);
MKLDNNStream::Get()->Submit();
} else { // training
const NDArray &outMean = out_data[batchnorm::kMean];
const NDArray &outVar = out_data[batchnorm::kVar];

net_args.insert({MKLDNN_ARG_MEAN, *(outMean.GetMKLDNNData())});
net_args.insert({MKLDNN_ARG_VARIANCE, *(outVar.GetMKLDNNData())});
net_args[MKLDNN_ARG_MEAN] = *(outMean.GetMKLDNNData());
net_args[MKLDNN_ARG_VARIANCE] = *(outVar.GetMKLDNNData());
MKLDNNStream::Get()->RegisterPrimArgs(fwd.GetFwd(), net_args);
MKLDNNStream::Get()->Submit();

Expand Down Expand Up @@ -337,12 +335,12 @@ void MKLDNNBatchNormBackward(const OpContext &ctx, const BatchNormParam &param,
weight_buf[channels_ + i] = (beta.data().dptr<DType>())[i]; // bias
}

std::unordered_map<int, mkldnn::memory> net_args;
net_args.insert({MKLDNN_ARG_SRC, *data_mem});
net_args.insert({MKLDNN_ARG_DIFF_SRC, *gradi_mem});
net_args.insert({MKLDNN_ARG_SCALE_SHIFT, bwd.GetWeight()});
net_args.insert({MKLDNN_ARG_DIFF_SCALE_SHIFT, bwd.GetGradw()});
net_args.insert({MKLDNN_ARG_DIFF_DST, *diff_mem});
mkldnn_args_map_t net_args;
net_args[MKLDNN_ARG_SRC] = *data_mem;
net_args[MKLDNN_ARG_DIFF_SRC] = *gradi_mem;
net_args[MKLDNN_ARG_SCALE_SHIFT] = bwd.GetWeight();
net_args[MKLDNN_ARG_DIFF_SCALE_SHIFT] = bwd.GetGradw();
net_args[MKLDNN_ARG_DIFF_DST] = *diff_mem;

// training but no input mean and variance
if (ctx.is_train && !param.use_global_stats) {
Expand All @@ -362,13 +360,13 @@ void MKLDNNBatchNormBackward(const OpContext &ctx, const BatchNormParam &param,
moving_var_ptr[i] = moving_var_ptr[i] * param.momentum +
variance * minus_mom;
}
net_args.insert({MKLDNN_ARG_MEAN, *(out_mean.GetMKLDNNData())});
net_args.insert({MKLDNN_ARG_VARIANCE, var_mem});
net_args[MKLDNN_ARG_MEAN] = *(out_mean.GetMKLDNNData());
net_args[MKLDNN_ARG_VARIANCE] = var_mem;
MKLDNNStream::Get()->RegisterPrimArgs(bwd.GetBwd(), net_args);
MKLDNNStream::Get()->Submit();
} else {
net_args.insert({MKLDNN_ARG_MEAN, *(moving_mean.GetMKLDNNData())});
net_args.insert({MKLDNN_ARG_VARIANCE, *(moving_var.GetMKLDNNData())});
net_args[MKLDNN_ARG_MEAN] = *(moving_mean.GetMKLDNNData());
net_args[MKLDNN_ARG_VARIANCE] = *(moving_var.GetMKLDNNData());
MKLDNNStream::Get()->RegisterPrimArgs(bwd.GetBwd(), net_args);
MKLDNNStream::Get()->Submit();
}
Expand Down