csullivan commented on code in PR #13349: URL: https://github.com/apache/tvm/pull/13349#discussion_r1023074066
########## src/meta_schedule/postproc/verify_vtcm_limit.cc: ########## @@ -0,0 +1,102 @@ +/* + * 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/target/utils.h> +#include <tvm/tir/transform.h> + +#include "../utils.h" + +namespace tvm { +namespace meta_schedule { + +/*! \brief Verify the correctness of the generated Hexagon code. */ +class VerifyVTCMLimitNode : public PostprocNode { + public: + int64_t vtcm_capacity = 0; + + void InitializeWithTuneContext(const TuneContext& context) final { + ICHECK(context->target.defined()); + Target target = context->target.value(); + vtcm_capacity = Extract(target, "vtcm-capacity").IntValue(); + } + + bool Verify(const IRModule& mod) const { + for (const auto& kv : mod->functions) { + if (auto* n = kv.second.as<tir::PrimFuncNode>()) { + auto func = GetRef<tir::PrimFunc>(n); + auto sizes = CalculateAllocatedBytes(func); + const auto vtcm_allocated = sizes.Get("global.vtcm").value_or(0); + if (vtcm_capacity > 0 && vtcm_allocated.IntValue() > vtcm_capacity) { + return false; + } + } + } + return true; + } + + bool Apply(const tir::Schedule& sch) final { + IRModule mod = sch->mod(); + for (const auto& kv : mod->functions) { + const GlobalVar& g_var = kv.first; + const BaseFunc& base_func = kv.second; + if (const auto* prim_func = base_func.as<tir::PrimFuncNode>()) { + IRModule lowered{nullptr}; + try { + auto pass_list = Array<tvm::transform::Pass>(); + // Convert Function to IRModule + transform::PassContext pass_ctx = transform::PassContext::Current(); + tir::PrimFunc f = WithAttr(GetRef<tir::PrimFunc>(prim_func), "global_symbol", + runtime::String(g_var->name_hint)); + bool noalias = pass_ctx->GetConfig<Bool>("tir.noalias", Bool(true)).value(); + if (noalias) { + f = WithAttr(std::move(f), "tir.noalias", Bool(true)); + } + IRModule mod = IRModule(Map<GlobalVar, BaseFunc>({{GlobalVar(g_var->name_hint), f}})); + lowered = tvm::transform::Sequential(pass_list)(std::move(mod)); + } catch (const dmlc::Error& e) { + return false; + } + if (!Verify(lowered)) { + return false; + } + } + } + return true; + } + + Postproc Clone() const { + ObjectPtr<VerifyVTCMLimitNode> n = make_object<VerifyVTCMLimitNode>(*this); + n->vtcm_capacity = this->vtcm_capacity; + return Postproc(n); + } + + static constexpr const char* _type_key = "meta_schedule.VerifyVTCMLimit"; Review Comment: USMP is a whole program analysis, whereas the VerifyVTCMLimit pass is a single op/primfunc analysis. In principle to achieve what you are suggesting @janetsc we would want to update the MS preprocessor to be replaced with a preprocessor _generator_ which can generate a preprocessor to use based on the live VTCM as calculated by USMP. This should be quite doable in Relax. As a side note, with the introduction of whole program memory planning, the phase ordering of memory planning and tuning can become complex. -- 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]
