You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/02/01 20:57:00 UTC

[GitHub] [tvm] MargaretQian commented on a change in pull request #10089: [Relay][Pass] Add a relay pass to extract fake quantized ops

MargaretQian commented on a change in pull request #10089:
URL: https://github.com/apache/tvm/pull/10089#discussion_r797012395



##########
File path: src/relay/analysis/extract_fake_quantized_ops.cc
##########
@@ -0,0 +1,82 @@
+/*
+ * 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 extract_fake_quantized_ops.cc
+ * \brief Extract fake quantized operators from an IRModule
+ */
+#include <tvm/relay/expr.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/transform.h>
+
+#include "../transforms/fake_quantization_to_integer.h"
+
+namespace tvm {
+namespace relay {
+
+using ExprSet = std::unordered_set<Expr, ObjectPtrHash, ObjectPtrEqual>;
+
+class ExtractFakeQuantizedOpsWrapper : private MixedModeVisitor {
+ public:
+  Map<String, tvm::Integer> Extract(const IRModule& m) {
+    IRModule mod(m);
+    mod = transform::InferType()(mod);
+    VisitExpr(mod->Lookup("main"));
+
+    return fake_quantized_op_freqs_;
+  }
+
+ private:
+  using MixedModeVisitor::VisitExpr_;
+  /*! \brief Dict of fake quantized op names to frequency counts */
+  Map<String, tvm::Integer> fake_quantized_op_freqs_;
+
+  void VisitExpr_(const CallNode* call_node) override {
+    if (call_node->op == quantize_op_) {
+      SubgraphExtractor extractor;
+      ExprSet subgraph = extractor.GetSubgraph(GetRef<Expr>(call_node));
+
+      for (auto expr : subgraph) {
+        const Op op = Downcast<Op>(expr.as<CallNode>()->op);
+        auto op_name = op->name;
+        if (op != dequantize_op_) {
+          if (fake_quantized_op_freqs_.find(op_name) != fake_quantized_op_freqs_.end()) {
+            fake_quantized_op_freqs_.Set(op_name,
+                                         int64_t(fake_quantized_op_freqs_.at(op_name)) + 1);

Review comment:
       i was getting compile-time errors that `fake_quantized_op_freqs_.at(op_name) + 1` is a `PrimExpr` instead of a `tvm::Integer` and it seemed like casting worked around the issue -- lmk if there's a better way around this?




-- 
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@tvm.apache.org

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