|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <tvm/relax/expr_functor.h> |
| 21 | +#include <tvm/relax/transform.h> |
| 22 | + |
| 23 | +#include <algorithm> |
| 24 | +#include <tuple> |
| 25 | + |
| 26 | +namespace tvm { |
| 27 | +namespace relax { |
| 28 | + |
| 29 | +namespace { |
| 30 | + |
| 31 | +template <typename T, typename U> |
| 32 | +using PMap = std::unordered_map<T, U, ObjectPtrHash, ObjectPtrEqual>; |
| 33 | + |
| 34 | +Optional<Function> ExpandParams(Function func) { |
| 35 | + bool is_exposed = func->attrs.GetAttr<String>(tvm::attr::kGlobalSymbol).defined(); |
| 36 | + if (is_exposed) return NullOpt; |
| 37 | + |
| 38 | + bool has_tuple_param = std::any_of( |
| 39 | + func->params.begin(), func->params.end(), |
| 40 | + [](const Var& param) -> bool { return param->struct_info_.as<TupleStructInfoNode>(); }); |
| 41 | + |
| 42 | + if (!has_tuple_param) return NullOpt; |
| 43 | + |
| 44 | + Array<Var> params; |
| 45 | + Array<Binding> bindings; |
| 46 | + |
| 47 | + std::function<void(const Var&)> expand_param = [&](const Var& param) { |
| 48 | + if (auto sinfo = param->struct_info_.as<TupleStructInfoNode>()) { |
| 49 | + Array<Expr> internal_tuple; |
| 50 | + for (size_t i = 0; i < sinfo->fields.size(); i++) { |
| 51 | + auto name = static_cast<const std::stringstream&>(std::stringstream() |
| 52 | + << param->name_hint() << "_" << i) |
| 53 | + .str(); |
| 54 | + Var new_param(name, sinfo->fields[i]); |
| 55 | + internal_tuple.push_back(new_param); |
| 56 | + expand_param(new_param); |
| 57 | + } |
| 58 | + bindings.push_back(VarBinding(param, Tuple(internal_tuple))); |
| 59 | + } else { |
| 60 | + params.push_back(param); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + for (const auto& param : func->params) { |
| 65 | + expand_param(param); |
| 66 | + } |
| 67 | + |
| 68 | + FuncStructInfo new_sinfo(params.Map([](const auto& var) { return GetStructInfo(var); }), |
| 69 | + func->ret_struct_info, |
| 70 | + Downcast<FuncStructInfo>(func->struct_info_)->purity); |
| 71 | + |
| 72 | + auto write_ptr = func.CopyOnWrite(); |
| 73 | + write_ptr->params = params; |
| 74 | + write_ptr->body = SeqExpr({BindingBlock(bindings)}, func->body); |
| 75 | + write_ptr->struct_info_ = new_sinfo; |
| 76 | + |
| 77 | + return func; |
| 78 | +} |
| 79 | + |
| 80 | +class TupleExpander : public ExprMutator { |
| 81 | + public: |
| 82 | + explicit TupleExpander(PMap<GlobalVar, GlobalVar> callees) : replacements_(callees) {} |
| 83 | + |
| 84 | + using ExprMutator::VisitExpr_; |
| 85 | + |
| 86 | + Expr VisitExpr_(const CallNode* op) override { |
| 87 | + auto node = Downcast<Call>(ExprMutator::VisitExpr_(op)); |
| 88 | + |
| 89 | + if (auto gvar = node->op.as<GlobalVar>()) { |
| 90 | + if (auto it = replacements_.find(gvar.value()); it != replacements_.end()) { |
| 91 | + Array<Expr> new_args; |
| 92 | + |
| 93 | + std::function<void(const Expr&)> expand_arg = [&](const Expr& arg) { |
| 94 | + if (auto sinfo = arg->struct_info_.as<TupleStructInfoNode>()) { |
| 95 | + for (size_t i = 0; i < sinfo->fields.size(); i++) { |
| 96 | + expand_arg(TupleGetItem(arg, i)); |
| 97 | + } |
| 98 | + } else { |
| 99 | + new_args.push_back(arg); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + for (const auto& arg : node->args) { |
| 104 | + expand_arg(arg); |
| 105 | + } |
| 106 | + |
| 107 | + auto write_ptr = node.CopyOnWrite(); |
| 108 | + write_ptr->op = it->second; |
| 109 | + write_ptr->args = new_args; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return node; |
| 114 | + } |
| 115 | + |
| 116 | + PMap<GlobalVar, GlobalVar> replacements_; |
| 117 | +}; |
| 118 | + |
| 119 | +} // namespace |
| 120 | + |
| 121 | +namespace transform { |
| 122 | + |
| 123 | +Pass ExpandTupleArguments() { |
| 124 | + runtime::TypedPackedFunc<IRModule(IRModule, PassContext)> pass_func = |
| 125 | + [=](IRModule mod, PassContext pc) -> IRModule { |
| 126 | + PMap<GlobalVar, GlobalVar> gvar_replacements; |
| 127 | + |
| 128 | + { |
| 129 | + PMap<GlobalVar, Function> new_callees; |
| 130 | + |
| 131 | + for (const auto& [gvar, base_func] : mod->functions) { |
| 132 | + if (auto func = base_func.as<Function>()) { |
| 133 | + if (auto opt = ExpandParams(func.value())) { |
| 134 | + auto new_func = opt.value(); |
| 135 | + GlobalVar new_gvar(gvar->name_hint, new_func->checked_type_); |
| 136 | + new_gvar->struct_info_ = new_func->struct_info_; |
| 137 | + gvar_replacements[gvar] = new_gvar; |
| 138 | + new_callees[new_gvar] = new_func; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if (gvar_replacements.empty()) { |
| 144 | + return mod; |
| 145 | + } |
| 146 | + auto write_ptr = mod.CopyOnWrite(); |
| 147 | + for (auto [old_gvar, new_gvar] : gvar_replacements) { |
| 148 | + write_ptr->Remove(old_gvar); |
| 149 | + write_ptr->Add(new_gvar, new_callees.at(new_gvar)); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + TupleExpander mutator(std::move(gvar_replacements)); |
| 154 | + |
| 155 | + IRModule caller_updates; |
| 156 | + |
| 157 | + for (const auto& [gvar, base_func] : mod->functions) { |
| 158 | + if (auto func = base_func.as<Function>()) { |
| 159 | + auto mutated = Downcast<Function>(mutator.VisitExpr(func.value())); |
| 160 | + if (!mutated.same_as(base_func)) { |
| 161 | + caller_updates->Add(gvar, mutated); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (caller_updates->functions.size()) { |
| 167 | + mod.CopyOnWrite()->Update(caller_updates); |
| 168 | + } |
| 169 | + return mod; |
| 170 | + }; |
| 171 | + auto inner_pass = CreateModulePass(pass_func, 0, "ExpandTupleArgumentsInner", {}); |
| 172 | + |
| 173 | + return tvm::transform::Sequential( |
| 174 | + { |
| 175 | + inner_pass, |
| 176 | + CanonicalizeBindings(), |
| 177 | + DeadCodeElimination({}), |
| 178 | + }, |
| 179 | + "ExpandTupleArguments"); |
| 180 | +} |
| 181 | + |
| 182 | +TVM_REGISTER_GLOBAL("relax.transform.ExpandTupleArguments").set_body_typed(ExpandTupleArguments); |
| 183 | + |
| 184 | +} // namespace transform |
| 185 | + |
| 186 | +} // namespace relax |
| 187 | +} // namespace tvm |
0 commit comments