areusch commented on code in PR #11208:
URL: https://github.com/apache/tvm/pull/11208#discussion_r877247663


##########
src/relay/backend/aot/annotate_used_memory.cc:
##########
@@ -0,0 +1,156 @@
+/*
+ * 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/relay/backend/aot/annotate_used_memory.cc
+ * \brief Analyzes the memory pressure at the callsite of primitive functions.
+ */
+
+#include <tvm/ir/module.h>
+#include <tvm/relay/transform.h>
+
+#include "../../transforms/device_aware_visitors.h"
+#include "../manifest_lifetimes.h"
+
+namespace tvm {
+namespace relay {
+namespace backend {
+namespace aot {
+
+/*!
+ * \brief Annotates the memory usage of each primitive function by analysing 
the liveness
+ * of the input/output tensors at the function callsite and calculating the 
total amount of
+ * memory these tensors require.
+ */
+class AnnotateUsedMemoryMutator : public transform::DeviceAwareExprMutator {
+ public:
+  AnnotateUsedMemoryMutator(const IRModule& module, const 
transform::ControlFlowGraph& cfg,
+                            const transform::LivenessAnalysis& lva)
+      : DeviceAwareExprMutator(module), control_flow_graph_(cfg), 
liveness_(lva) {}
+
+  /*!
+   * \brief Get the memory required for a primitive Relay function by 
calculating the total
+   * bytes of the live tensors at the callsite of the function.
+   *
+   * \param live_tensors The tensors that are live when the function is called.
+   * \return int The total number of bytes a function requires.
+   */
+  int GetMemoryUsage(const transform::VarSet& live_tensors) {
+    Array<Type> types_stack = {};
+    int memory_usage = 0;
+
+    for (const Var& var : live_tensors) {
+      Type var_type = var->checked_type();
+      ICHECK(var_type.defined()) << "InferTypes pass should be run before 
AnnotateUsedMemory pass.";
+      types_stack.push_back(var_type);
+    }
+
+    while (!types_stack.empty()) {
+      Type current_type = types_stack.back();
+      types_stack.pop_back();
+
+      if (const auto* tt_node = current_type.as<TupleTypeNode>()) {
+        for (const Type& type : tt_node->fields) {
+          types_stack.push_back(type);
+        }
+        continue;
+      } else if (const auto* ft_node = current_type.as<FuncTypeNode>()) {
+        types_stack.push_back(ft_node->ret_type);
+        continue;
+      }
+
+      const auto* tt_node = current_type.as<TensorTypeNode>();
+      ICHECK(tt_node) << "Expected TensorTypeNode but was " << 
current_type->GetTypeKey();
+      int total_tensor_bytes = GetTensorBytes(tt_node);
+      memory_usage += total_tensor_bytes;
+    }
+    return memory_usage;
+  }
+
+  /*!
+   * \brief Get the number of bytes a tensor requires.
+   *
+   * \param tensor_type_node The checked type of the tensor.
+   * \return int The number of bytes required.
+   */
+  int GetTensorBytes(const TensorTypeNode* tensor_type_node) {
+    PrimExpr size = tensor_type_node->Size();
+    const auto* size_int_imm = size.as<IntImmNode>();
+    ICHECK(size_int_imm) << "Expected tensor size to be an IntImmNode but was "
+                         << size->GetTypeKey();
+
+    int total_size = size_int_imm->value;
+    int dtype_bytes = tensor_type_node->dtype.bytes();
+    return total_size * dtype_bytes;
+  }
+
+  Expr PostVisitLet_(const LetNode* pre_let_node, const LetNode* 
post_let_node) override {
+    if (const auto* func_node = pre_let_node->value.as<FunctionNode>()) {

Review Comment:
   is this matching a lowered function call? would be great if we could comment 
the expected construct here.



##########
src/relay/backend/aot_executor_codegen.cc:
##########
@@ -1063,6 +1064,8 @@ class AOTExecutorCodegen : public MixedModeVisitor {
     }
 
     mod = transform::ToANormalForm()(mod);
+    mod = transform::InferType()(mod);
+    mod = transform::AnnotateUsedMemory()(mod);

Review Comment:
   how come you want to do this at the relay level when we would further 
analyze this in USMP down below?



##########
src/relay/backend/aot/annotate_used_memory.cc:
##########
@@ -0,0 +1,156 @@
+/*
+ * 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/relay/backend/aot/annotate_used_memory.cc
+ * \brief Analyzes the memory pressure at the callsite of primitive functions.
+ */
+
+#include <tvm/ir/module.h>
+#include <tvm/relay/transform.h>
+
+#include "../../transforms/device_aware_visitors.h"
+#include "../manifest_lifetimes.h"
+
+namespace tvm {
+namespace relay {
+namespace backend {
+namespace aot {
+
+/*!
+ * \brief Annotates the memory usage of each primitive function by analysing 
the liveness
+ * of the input/output tensors at the function callsite and calculating the 
total amount of
+ * memory these tensors require.
+ */
+class AnnotateUsedMemoryMutator : public transform::DeviceAwareExprMutator {
+ public:
+  AnnotateUsedMemoryMutator(const IRModule& module, const 
transform::ControlFlowGraph& cfg,
+                            const transform::LivenessAnalysis& lva)
+      : DeviceAwareExprMutator(module), control_flow_graph_(cfg), 
liveness_(lva) {}
+
+  /*!
+   * \brief Get the memory required for a primitive Relay function by 
calculating the total
+   * bytes of the live tensors at the callsite of the function.
+   *
+   * \param live_tensors The tensors that are live when the function is called.
+   * \return int The total number of bytes a function requires.
+   */
+  int GetMemoryUsage(const transform::VarSet& live_tensors) {
+    Array<Type> types_stack = {};
+    int memory_usage = 0;
+
+    for (const Var& var : live_tensors) {
+      Type var_type = var->checked_type();
+      ICHECK(var_type.defined()) << "InferTypes pass should be run before 
AnnotateUsedMemory pass.";
+      types_stack.push_back(var_type);
+    }
+
+    while (!types_stack.empty()) {
+      Type current_type = types_stack.back();
+      types_stack.pop_back();
+
+      if (const auto* tt_node = current_type.as<TupleTypeNode>()) {
+        for (const Type& type : tt_node->fields) {
+          types_stack.push_back(type);
+        }
+        continue;
+      } else if (const auto* ft_node = current_type.as<FuncTypeNode>()) {
+        types_stack.push_back(ft_node->ret_type);
+        continue;
+      }
+
+      const auto* tt_node = current_type.as<TensorTypeNode>();
+      ICHECK(tt_node) << "Expected TensorTypeNode but was " << 
current_type->GetTypeKey();
+      int total_tensor_bytes = GetTensorBytes(tt_node);
+      memory_usage += total_tensor_bytes;
+    }
+    return memory_usage;
+  }
+
+  /*!
+   * \brief Get the number of bytes a tensor requires.
+   *
+   * \param tensor_type_node The checked type of the tensor.
+   * \return int The number of bytes required.
+   */
+  int GetTensorBytes(const TensorTypeNode* tensor_type_node) {

Review Comment:
   per 
https://github.com/apache/tvm/blob/main/src/relay/backend/te_compiler.cc#L1002 
there are many instances where we do this. could you use a utils function here 
instead of re-implementing?



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