This repository was archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
add int8 bn mkldnn implementation and test #15664
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1ecdd67
add int8 bn mkldnn implementation and test
ElaineBao 9eeaf41
fix lint
ElaineBao 5b0e4a3
fix ci
ElaineBao 7609b7a
enable int8 bn test only in mkldnn backend
ElaineBao 69c6ac5
disable int8 bn forward test with gpu backend
ElaineBao 973331f
update int8 bn with reference to comments
ElaineBao e9e11cf
fix lint
ElaineBao f1ae5de
Merge branch 'official-master'
ElaineBao 16c150a
disable int8 bn gluon forward test with gpu backend
ElaineBao 45d891d
disable uint8 bn forward test with mkldnn backend
ElaineBao 9c92f79
restore support mkldnn bn condition
ElaineBao a8a5155
rm duplicate code
ElaineBao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,8 +132,8 @@ class MKLDNNBNForward { | |
return *var_m; | ||
} | ||
|
||
void SetDataHandle(const NDArray &data, const NDArray &mean, | ||
const NDArray &var, const mkldnn::memory &out) { | ||
void SetDataHandle(const NDArray &data, const mkldnn::memory *mean, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't duplicate code. Make old version on top of this one. |
||
const mkldnn::memory *var, const mkldnn::memory *out) { | ||
auto _data = data.GetMKLDNNData(); | ||
if (data_m) { | ||
data_m->set_data_handle(_data->get_data_handle()); | ||
|
@@ -142,24 +142,22 @@ class MKLDNNBNForward { | |
_data->get_data_handle())); | ||
} | ||
if (out_m) { | ||
out_m->set_data_handle(out.get_data_handle()); | ||
out_m->set_data_handle(out->get_data_handle()); | ||
} else { | ||
out_m.reset(new mkldnn::memory(out.get_primitive_desc(), | ||
out.get_data_handle())); | ||
out_m.reset(new mkldnn::memory(out->get_primitive_desc(), | ||
out->get_data_handle())); | ||
} | ||
auto mean_ptr = mean.data().dptr_; | ||
if (mean_m) { | ||
mean_m->set_data_handle(mean_ptr); | ||
mean_m->set_data_handle(mean->get_data_handle()); | ||
} else { | ||
mean_m.reset(new mkldnn::memory(pd.mean_primitive_desc(), | ||
mean_ptr)); | ||
mean_m.reset(new mkldnn::memory(mean->get_primitive_desc(), | ||
mean->get_data_handle())); | ||
} | ||
auto var_ptr = var.data().dptr_; | ||
if (var_m) { | ||
var_m->set_data_handle(var_ptr); | ||
var_m->set_data_handle(var->get_data_handle()); | ||
} else { | ||
var_m.reset(new mkldnn::memory(pd.variance_primitive_desc(), | ||
var_ptr)); | ||
var_m.reset(new mkldnn::memory(var->get_primitive_desc(), | ||
var->get_data_handle())); | ||
} | ||
|
||
if (fwd == nullptr) { | ||
|
@@ -175,6 +173,11 @@ class MKLDNNBNForward { | |
} | ||
} | ||
|
||
void SetDataHandle(const NDArray &data, const NDArray &mean, | ||
const NDArray &var, const mkldnn::memory &out) { | ||
SetDataHandle(data, mean.GetMKLDNNData(), var.GetMKLDNNData(), &out); | ||
} | ||
|
||
const mkldnn::batch_normalization_forward &GetFwd() const { | ||
return *fwd; | ||
} | ||
|
123 changes: 123 additions & 0 deletions
123
src/operator/quantization/mkldnn/mkldnn_quantized_batch_norm.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* \file mkldnn_quantized_batch_norm.cc | ||
* \brief | ||
* \author Yixin Bao | ||
*/ | ||
|
||
#if MXNET_USE_MKLDNN == 1 | ||
#include "../../nn/mkldnn/mkldnn_batch_norm-inl.h" | ||
#include "../quantization_utils.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
static void MKLDNNQuantizedBatchNormForward(const nnvm::NodeAttrs &attrs, const OpContext &ctx, | ||
const std::vector<NDArray> &in_data, | ||
const std::vector<OpReqType> &req, | ||
const std::vector<NDArray> &outputs) { | ||
CHECK_EQ(in_data.size(), 7U); | ||
CHECK_EQ(outputs.size(), 3U); | ||
|
||
TmpMemMgr::Get()->Init(ctx.requested[batchnorm::kTempSpace]); | ||
const BatchNormParam ¶m = nnvm::get<BatchNormParam>(attrs.parsed); | ||
const NDArray &data = in_data[quantized_batchnorm::kData]; | ||
const size_t channelAxis = static_cast<size_t>( | ||
param.axis < 0 ? static_cast<int>(data.shape().ndim()) + param.axis : param.axis); | ||
const int channel_count = data.shape()[channelAxis]; | ||
const float min_data = in_data[quantized_batchnorm::kDataMin].data().dptr<float>()[0]; | ||
const float max_data = in_data[quantized_batchnorm::kDataMax].data().dptr<float>()[0]; | ||
const float max_abs_data = std::max(std::abs(min_data), std::abs(max_data)); | ||
|
||
float *min_output_ptr = outputs[quantized_batchnorm::kOutMin].data().dptr<float>(); | ||
float *max_output_ptr = outputs[quantized_batchnorm::kOutMax].data().dptr<float>(); | ||
if (param.min_calib_range.has_value() && param.max_calib_range.has_value()) { | ||
*max_output_ptr = param.max_calib_range.value(); | ||
*min_output_ptr = param.min_calib_range.value(); | ||
} else { | ||
LOG(FATAL) << "min_calib_range or max_calib_range is not available. Quantized BN currently " | ||
"don't support calib_mode=None"; | ||
} | ||
const float max_abs_output = std::max(std::abs(*min_output_ptr), std::abs(*max_output_ptr)); | ||
|
||
unsigned flags = mkldnn::use_global_stats | mkldnn::use_scale_shift; | ||
auto &fwd = GetBNForward<float>(param, ctx, data, flags); | ||
const mkldnn::memory &weight_mem = fwd.GetWeight(); | ||
CHECK_EQ(weight_mem.get_primitive_desc().get_size(), channel_count * sizeof(float) * 2); | ||
float *weight_buf = reinterpret_cast<float *>(weight_mem.get_data_handle()); | ||
|
||
float *gamma_ptr = in_data[quantized_batchnorm::kGamma].data().dptr<float>(); | ||
float *beta_ptr = in_data[quantized_batchnorm::kBeta].data().dptr<float>(); | ||
|
||
const NDArray &moving_mean = in_data[quantized_batchnorm::kInMovingMean]; | ||
const NDArray &moving_var = in_data[quantized_batchnorm::kInMovingVar]; | ||
float *moving_mean_ptr = moving_mean.data().dptr<float>(); | ||
float *moving_var_ptr = moving_var.data().dptr<float>(); | ||
|
||
// rescale gamma and beta, to make mean=0 and var=1 | ||
auto rescaled_mean_mem = | ||
TmpMemMgr::Get()->Alloc(moving_mean.GetMKLDNNData()->get_primitive_desc()); | ||
auto rescaled_var_mem = TmpMemMgr::Get()->Alloc(moving_var.GetMKLDNNData()->get_primitive_desc()); | ||
float *rescaled_mean_ptr = reinterpret_cast<float *>(rescaled_mean_mem->get_data_handle()); | ||
float *rescaled_var_ptr = reinterpret_cast<float *>(rescaled_var_mem->get_data_handle()); | ||
|
||
#pragma omp parallel for num_threads(engine::OpenMP::Get()->GetRecommendedOMPThreadCount()) | ||
for (int channel = 0; channel < channel_count; ++channel) { | ||
float invstd = 1.0 / std::sqrt(moving_var_ptr[channel] + param.eps); | ||
weight_buf[channel] = gamma_ptr[channel] * invstd * max_abs_data / max_abs_output; | ||
weight_buf[channel_count + channel] = | ||
(beta_ptr[channel] - moving_mean_ptr[channel] * gamma_ptr[channel] * invstd) * kInt8Range / | ||
max_abs_output; | ||
rescaled_mean_ptr[channel] = 0.0f; | ||
rescaled_var_ptr[channel] = 1.0f; | ||
} | ||
|
||
auto out_mem = CreateMKLDNNMem(outputs[batchnorm::kOut], | ||
fwd.GetPd().dst_primitive_desc(), req[batchnorm::kOut], &data); | ||
fwd.SetDataHandle(data, rescaled_mean_mem, rescaled_var_mem, out_mem.second); | ||
|
||
MKLDNNStream::Get()->RegisterPrim(fwd.GetFwd()); | ||
MKLDNNStream::Get()->Submit(); | ||
} | ||
|
||
inline static bool QuantizedBatchNormStorageType(const nnvm::NodeAttrs &attrs, const int dev_mask, | ||
DispatchMode *dispatch_mode, | ||
std::vector<int> *in_attrs, | ||
std::vector<int> *out_attrs) { | ||
bool dispatched = false; | ||
if (!dispatched) { | ||
dispatched = MKLDNNStorageType(attrs, dev_mask, true, dispatch_mode, in_attrs, out_attrs); | ||
} | ||
return dispatched; | ||
} | ||
|
||
NNVM_REGISTER_OP(_contrib_quantized_batch_norm) | ||
.set_attr<FInferStorageType>("FInferStorageType", QuantizedBatchNormStorageType) | ||
.set_attr<FComputeEx>("FComputeEx<cpu>", MKLDNNQuantizedBatchNormForward) | ||
.set_attr<FResourceRequest>("FResourceRequest", [](const NodeAttrs& n) { | ||
return std::vector<ResourceRequest>{ResourceRequest::kTempSpace}; | ||
}) | ||
.set_attr<bool>("TIsMKLDNN", true); | ||
|
||
} // namespace op | ||
} // namespace mxnet | ||
|
||
#endif // MXNET_USE_MKLDNN == 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.