rongzha1 commented on a change in pull request #14614: [MKLDNN] add quantized 
sum
URL: https://github.com/apache/incubator-mxnet/pull/14614#discussion_r273301352
 
 

 ##########
 File path: src/operator/quantization/mkldnn/mkldnn_quantized_sum.cc
 ##########
 @@ -0,0 +1,203 @@
+/*
+ * 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.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file mkldnn_quantized_sum.cc
+ * \brief
+ */
+
+#if MXNET_USE_MKLDNN == 1
+#include "./mkldnn_quantized_sum-inl.h"
+#include "../../nn/mkldnn/mkldnn_ops-inl.h"
+#include "../../nn/mkldnn/mkldnn_base-inl.h"
+#include "../quantization_utils.h"
+
+namespace mxnet {
+namespace op {
+
+DMLC_REGISTER_PARAMETER(RequantizeSumParam);
+
+static float GetScale(const NDArray& data, float min, float max) {
+  auto data_range = (data.dtype() == mshadow::kInt8) ? kInt8Range : 
kUint8Range;
+  return data_range / MaxAbs(min, max);
+}
+
+static void MKLDNNQuantizedSumForward(const nnvm::NodeAttrs& attrs, const 
OpContext& ctx,
+                                         const std::vector<NDArray>& in_data,
+                                         const std::vector<OpReqType>& req,
+                                         const std::vector<NDArray>& out_data) 
{
+  const RequantizeSumParam& params = 
nnvm::get<RequantizeSumParam>(attrs.parsed);
+  // A, B, A_min, A_max, B_min, B_max
+  CHECK_EQ(in_data.size(), static_cast<size_t>(6));
+  // C, C_min, C_max
+  CHECK_EQ(out_data.size(), 3U);
+
+  // Collect data min,max,absmax
+  float dataA_min = in_data[quantized_sum_enum::kAMin].data().dptr<float>()[0];
+  float dataB_min = in_data[quantized_sum_enum::kBMin].data().dptr<float>()[0];
+  float dataA_max = in_data[quantized_sum_enum::kAMax].data().dptr<float>()[0];
+  float dataB_max = in_data[quantized_sum_enum::kBMax].data().dptr<float>()[0];
+  float dataA_absmax = MaxAbs(dataA_min, dataA_max);
+  float dataB_absmax = MaxAbs(dataB_min, dataB_max);
+
+  auto dataA_mem  = in_data[quantized_sum_enum::kDataA].GetMKLDNNData();
+  auto dataB_mem  = in_data[quantized_sum_enum::kDataB].GetMKLDNNData();
+  bool dataA_int8 = (in_data[quantized_sum_enum::kDataA].dtype() == 
mshadow::kInt8) ? true : false;
+  size_t dataA_range = dataA_int8 ? kInt8Range : kUint8Range;
+
+  float A_scale = GetScale(in_data[quantized_sum_enum::kDataA], dataA_min, 
dataA_max);
+  float B_scale = GetScale(in_data[quantized_sum_enum::kDataB], dataB_min, 
dataB_max);
+  // rescaled_mem is for reorder mkldnn memory
+  std::shared_ptr<mkldnn::memory> rescaled_mem;
+  // output default set as int32
+  size_t output_data_range = kInt32Range;
+  auto output_data_type = mkldnn::memory::s32;
+  // dataA && dataB are uint8
+  if (out_data[quantized_sum_enum::kDataA].dtype() == mshadow::kInt8) {
+  output_data_range = kInt8Range;
+  output_data_type = mkldnn::memory::s8;
+  } else if (out_data[quantized_sum_enum::kDataA].dtype() == mshadow::kUint8) {
+    output_data_range = kUint8Range;
+    output_data_type = mkldnn::memory::u8;
+  }
+
+  float output_min = 0;
+  float output_max = 0;
+  float out_data_scale = 0;
+  if (params.max_calib_range.has_value() && 
params.min_calib_range.has_value()) {
+    output_min = params.min_calib_range.value();
+    output_max = params.max_calib_range.value();
+    out_data_scale = output_data_range/MaxAbs(output_min, output_max);
+  } else {
+    output_max = dataA_absmax + dataB_absmax;
+    output_min = 0 - output_max;
+  }
+
+  std::vector<float> scales;
+  if (in_data[quantized_sum_enum::kDataA].dtype() != 
in_data[quantized_sum_enum::kDataB].dtype()) {
+    auto s8_pd = (dataA_int8 == true)
+                 ? dataA_mem->get_primitive_desc()
+                 : dataB_mem->get_primitive_desc();
+    rescaled_mem = std::make_shared<mkldnn::memory>(s8_pd);
 
 Review comment:
   mkldnn sum doesn't support int8 + uint8, so need to reorder them to the same 
data type first.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to