lhutton1 commented on code in PR #11208: URL: https://github.com/apache/tvm/pull/11208#discussion_r878085445
########## 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); Review Comment: Yeah this is due to currently visiting the function, when it should really be the call node - which would mean this isn't required when its changed -- 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]
