slyubomirsky commented on code in PR #16117:
URL: https://github.com/apache/tvm/pull/16117#discussion_r1393566895


##########
src/relax/transform/remove_unused_outputs.cc:
##########
@@ -0,0 +1,326 @@
+/*
+ * 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.
+ */
+
+#include <tvm/relax/analysis.h>
+#include <tvm/relax/expr_functor.h>
+#include <tvm/relax/transform.h>
+#include <tvm/relax/utils.h>
+
+#include <algorithm>
+#include <optional>
+#include <tuple>
+
+namespace tvm {
+namespace relax {
+
+namespace {
+
+template <typename T>
+using PSet = std::unordered_set<T, ObjectPtrHash, ObjectPtrEqual>;
+
+template <typename T, typename U>
+using PMap = std::unordered_map<T, U, ObjectPtrHash, ObjectPtrEqual>;
+
+class PartialTupleUsageCollector : ExprVisitor {
+ public:
+  static PMap<GlobalVar, std::vector<bool>> Collect(const IRModule& mod) {
+    PMap<GlobalVar, size_t> num_outputs;
+
+    for (const auto& [gvar, base_func] : mod->functions) {
+      bool is_exposed = 
base_func->attrs.GetAttr<String>(tvm::attr::kGlobalSymbol).defined();
+
+      if (!is_exposed) {
+        if (auto relax_func = base_func.as<FunctionNode>()) {
+          if (auto out_tuple = 
relax_func->ret_struct_info.as<TupleStructInfoNode>()) {
+            num_outputs[gvar] = out_tuple->fields.size();
+          }
+        }
+      }
+    }
+
+    if (num_outputs.empty()) {
+      // Early bail-out if the module has no private functions that
+      // return tuples.
+      return {};
+    }
+
+    PartialTupleUsageCollector visitor(std::move(num_outputs));
+    for (const auto& [gvar, base_func] : mod->functions) {
+      if (auto func = base_func.as<Function>()) {
+        visitor.VisitExpr(func.value());
+      }
+    }
+
+    PMap<GlobalVar, std::vector<bool>> to_update;
+    for (const auto& [gvar, mask] : visitor.output_usage_mask_) {
+      bool has_unused_output =
+          std::any_of(mask.begin(), mask.end(), [](const bool is_used) { 
return !is_used; });
+      if (has_unused_output) {
+        to_update[gvar] = mask;
+      }
+    }
+
+    return to_update;
+  }
+
+ private:
+  PartialTupleUsageCollector(PMap<GlobalVar, size_t> num_outputs) {
+    for (const auto& [gvar, num_output] : num_outputs) {
+      output_usage_mask_[gvar] = std::vector<bool>(num_output, false);
+    }
+  }
+
+  void VisitBinding(const Binding& binding) override {
+    ExprVisitor::VisitBinding(binding);
+    known_bindings_.Set(binding->var, GetBoundValue(binding));
+  }
+
+  void VisitExpr_(const TupleGetItemNode* op) override {
+    Expr tuple = UnwrapBindings(op->tuple);
+
+    if (auto call = tuple.as<CallNode>()) {
+      if (auto opt_callee = call->op.as<GlobalVar>()) {
+        auto callee = opt_callee.value();
+        if (auto it = output_usage_mask_.find(callee); it != 
output_usage_mask_.end()) {
+          auto& used_indices = it->second;
+
+          CHECK_GE(op->index, 0) << "IndexError: "
+                                 << "Indices for TupleGetItem must be 
non-negative, "
+                                 << "but expression " << GetRef<Expr>(op)
+                                 << " uses a tuple index of " << op->index;
+          size_t index = op->index;
+
+          CHECK_LT(index, used_indices.size())
+              << "IndexError: "
+              << "Indices for TupleGetItem must be less than the size of the 
tuple, "
+              << "but expression " << GetRef<Expr>(op) << " uses a tuple index 
of " << op->index
+              << " for a tuple of size " << used_indices.size();
+          used_indices[index] = true;
+        }
+      }
+    }
+  }
+
+  Expr UnwrapBindings(Expr expr) const {

Review Comment:
   Maybe we should try to generalize some version of this logic? Similar code 
appears in the `FNormalize` PR.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to