Lunderberg commented on code in PR #16798:
URL: https://github.com/apache/tvm/pull/16798#discussion_r1547095539


##########
src/relax/transform/lazy_transform_params.cc:
##########
@@ -0,0 +1,277 @@
+/*
+ * 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 src/relax/transform/lazy_transform_params.cc */
+
+#include <tvm/relax/analysis.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relax/expr_functor.h>
+#include <tvm/relax/transform.h>
+
+#include <optional>
+#include <unordered_map>
+
+#include "utils.h"
+
+namespace tvm {
+namespace relax {
+
+namespace {
+std::optional<int64_t> GetNumInputParams(const FunctionNode* func) {
+  if (auto opt_int_imm = func->GetAttr<IntImm>(attr::kNumInput)) {
+    int64_t num_input_params = opt_int_imm.value()->value;
+    CHECK_GE(num_input_params, 0) << "ValueError: "
+                                  << "Annotation for attr::kNumInput (\"" << 
attr::kNumInput
+                                  << "\") must be non-negative, but was " << 
num_input_params;
+    CHECK_LE(static_cast<size_t>(num_input_params), func->params.size())
+        << "ValueError: "
+        << "Annotation for attr::kNumInput (\"" << attr::kNumInput << "\") 
specifies "
+        << num_input_params << " parameters to be provided at runtime, "
+        << "but the function only accepts " << func->params.size() << " 
parameters in total";
+    return num_input_params;
+  } else {
+    return std::nullopt;
+  }
+}
+
+class LazyInputMutator : public ExprMutator {
+ public:
+  Expr VisitExpr_(const FunctionNode* func) override {
+    if (plan_.has_value()) {
+      return ExprMutator::VisitExpr_(func);
+    }
+
+    int64_t num_input_params = GetNumInputParams(func).value_or(0);
+
+    std::unordered_map<Var, size_t, ObjectPtrHash, ObjectPtrEqual> 
param_lookup;
+    for (size_t i = 0; i < func->params.size(); i++) {
+      param_lookup.insert({func->params[i], i - num_input_params});

Review Comment:
   The second parameter is the index of the model weight, ignoring any 
additional parameters that are provided at runtime.  So, you can have 
additional small parameters (e.g. `rank: R.Prim('int64')` or `num_gpus: 
R.Prim('int64')`), which are kept separate from the bundled parameters in the 
callback.
   
   That said, I'm re-reading it and am not sure how this indexing worked in the 
first place.  The `i - num_input_params` would convert `i` to a signed integer, 
but that should cause some erroneous usage when used.  It's enough to raise my 
concerns on it as well, so that's enough to warrant changing it.
   
   Updating the loop to start at `size_t i=num_input_params` instead of `size_t 
i=0`.



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