You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2021/07/05 13:55:25 UTC

[GitHub] [incubator-mxnet] sfraczek commented on a change in pull request #20400: [FEATURE] Fuse FC + sum for quantization

sfraczek commented on a change in pull request #20400:
URL: https://github.com/apache/incubator-mxnet/pull/20400#discussion_r663939878



##########
File path: src/operator/subgraph/mkldnn/mkldnn_fc.cc
##########
@@ -298,10 +408,8 @@ void SgMKLDNNFCOp::Forward(const OpContext &ctx,
             full_param_.eltwise_param.scale =
               GetQuantizeScale(output.dtype(), cached_min_output_, cached_max_output_);
           } else {
-            tmp_scale_ =
-              GetQuantizeScale(output.dtype(),
-                               cached_min_output_,
-                               cached_max_output_) / data_scale_;
+          out_scale =  GetQuantizeScale(output.dtype(), cached_min_output_, cached_max_output_);

Review comment:
       It seems tabulation is not correct here somewhere

##########
File path: src/operator/subgraph/mkldnn/mkldnn_fc_sum_fuse.h
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.
+ */
+
+/*
+  \brief It fuses FC + SUM for floating point output in second post quantization pass
+*/
+
+#ifndef MXNET_OPERATOR_SUBGRAPH_MKLDNN_MKLDNN_FC_SUM_FUSE_H_
+#define MXNET_OPERATOR_SUBGRAPH_MKLDNN_MKLDNN_FC_SUM_FUSE_H_
+#if MXNET_USE_MKLDNN == 1
+
+#include <string>
+#include <vector>
+#include <memory>
+#include <unordered_set>
+#include <utility>
+#include "../common.h"
+#include "../../tensor/matrix_op-inl.h"
+#include "mkldnn_subgraph_base-inl.h"
+#include "mkldnn_fc-inl.h"
+
+namespace mxnet {
+namespace op {
+
+class SgMKLDNNFCSumFuzeSelector : public SubgraphSelector {
+ public:
+  /*! \brief pattern match status */
+  enum SelectStatus {
+    kFail = 0,
+    kStart,
+    kSuccess,
+  };
+
+ private:
+  bool quantized_;
+  SelectStatus status_;
+  std::vector<const nnvm::Node *> matched_list_;
+
+ public:
+  explicit SgMKLDNNFCSumFuzeSelector(bool quantized) :
+      quantized_(quantized) {}
+
+  bool Select(const nnvm::Node &n, const std::shared_ptr<NodeAttr>& node_attr) override {
+    if (n.op() == Op::Get("_sg_mkldnn_fully_connected") && SupportMKLDNNAttr(node_attr)) {
+      auto const &fc_param = nnvm::get<MKLDNNFCFullParam>(n.attrs.parsed);
+      // TODO(anko) remove fc_param.mkldnn_param.quantized from if below
+      //            to fuse even for not quantized?
+      if (fc_param.mkldnn_param.enable_float_output && fc_param.mkldnn_param.quantized) {
+        status_ = kStart;
+        matched_list_.clear();
+        matched_list_.push_back(&n);
+        return true;
+      }
+    }
+    return false;
+  }
+
+  bool SelectInput(const nnvm::Node &n, const nnvm::Node &new_node) override {
+    return false;
+  }
+
+  bool SelectOutput(const nnvm::Node &n, const nnvm::Node &new_node) override {
+    if (status_ == kFail || status_ == kSuccess || new_node.is_variable())
+      return false;
+    // If n isn't the last matched node, then we encoutered a internal
+    // branch, we should pop out the node behind n and stop fusion.
+    if (matched_list_.back() != &n) {
+      if (std::find(matched_list_.begin(), matched_list_.end(), &n) !=
+        matched_list_.end()) {
+        while (matched_list_.back() != &n) {
+          matched_list_.pop_back();
+        }
+      }
+      status_ = kSuccess;
+      return false;
+    }
+
+    switch (status_) {
+      case kStart:
+        if (new_node.op()->name == "elemwise_add") {
+          matched_list_.push_back(&new_node);
+          status_ = kSuccess;
+          return true;
+        }
+      default:
+        status_ = kSuccess;
+        return false;
+    }
+  }
+
+  std::vector<nnvm::Node *> Filter(
+      const std::vector<nnvm::Node *> &candidates) override {
+    if (status_ == kFail) {
+      return std::vector<nnvm::Node *>(0);
+    } else {
+      std::vector<nnvm::Node *> ret;
+      for (auto i : matched_list_) {
+        auto non_const_i = const_cast<nnvm::Node *>(i);
+        if (std::find(candidates.begin(), candidates.end(), non_const_i) !=
+            candidates.end()) {
+          ret.push_back(non_const_i);
+        }
+      }
+      return candidates;
+    }
+  }
+
+  void Reset() override {
+    CHECK_GE(matched_list_.size(), 1);
+    auto new_selector = SgMKLDNNFCSumFuzeSelector(quantized_);

Review comment:
       I think 'fuze'->'fuse'




-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@mxnet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org