|
| 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 | +/*! |
| 21 | + * \file src/relax/backend/contrib/gna/codegen.cc |
| 22 | + * \brief Implementation of the GNA JSON serializer. |
| 23 | + */ |
| 24 | +#include <tvm/ffi/reflection/registry.h> |
| 25 | +#include <tvm/ir/module.h> |
| 26 | +#include <tvm/relax/expr.h> |
| 27 | +#include <tvm/relax/struct_info.h> |
| 28 | +#include <tvm/runtime/module.h> |
| 29 | +#include <tvm/runtime/packed_func.h> |
| 30 | +#include <tvm/tir/expr.h> |
| 31 | + |
| 32 | +#include <string> |
| 33 | + |
| 34 | +#include "../codegen_json/codegen_json.h" |
| 35 | +#include "../utils.h" |
| 36 | + |
| 37 | +namespace tvm { |
| 38 | +namespace relax { |
| 39 | +namespace contrib { |
| 40 | + |
| 41 | +using JSONGraphNode = tvm::runtime::json::JSONGraphNode; |
| 42 | +using JSONGraphNodeEntry = tvm::runtime::json::JSONGraphNodeEntry; |
| 43 | +using JSONSerializer = backend::contrib::JSONSerializer; |
| 44 | +using backend::contrib::NodeEntries; |
| 45 | + |
| 46 | +class GNAJSONSerializer : public JSONSerializer { |
| 47 | + public: |
| 48 | + GNAJSONSerializer(Map<Constant, String> constant_names, Map<Var, Expr> bindings) |
| 49 | + : JSONSerializer(constant_names), bindings_(bindings) {} |
| 50 | + |
| 51 | + using JSONSerializer::VisitExpr_; |
| 52 | + |
| 53 | + NodeEntries VisitExpr_(const CallNode* call_node) final { |
| 54 | + const auto* fn_var = call_node->op.as<VarNode>(); |
| 55 | + ICHECK(fn_var); |
| 56 | + const auto fn = Downcast<Function>(bindings_[GetRef<Var>(fn_var)]); |
| 57 | + ICHECK(fn.defined()) << "Expects the callee to be a function."; |
| 58 | + |
| 59 | + auto composite_opt = fn->GetAttr<String>(attr::kComposite); |
| 60 | + ICHECK(composite_opt.has_value()) << "Only composite functions are supported."; |
| 61 | + |
| 62 | + std::string composite_name = composite_opt.value(); |
| 63 | + |
| 64 | + NodeEntries inputs; |
| 65 | + for (const auto& arg : call_node->args) { |
| 66 | + auto res = VisitExpr(arg); |
| 67 | + inputs.insert(inputs.end(), res.begin(), res.end()); |
| 68 | + } |
| 69 | + |
| 70 | + auto node = std::make_shared<JSONGraphNode>(composite_name, /* name_ */ |
| 71 | + "kernel", /* op_type_ */ |
| 72 | + inputs, 1 /* num_outputs_ */); |
| 73 | + |
| 74 | + const CallNode* root_call = nullptr; |
| 75 | + if (composite_name.find("gna.dense") != std::string::npos) { |
| 76 | + root_call = backend::GetOpInFunction(fn, "relax.matmul"); |
| 77 | + } else if (composite_name.find("gna.conv1d") != std::string::npos) { |
| 78 | + root_call = backend::GetOpInFunction(fn, "relax.nn.conv1d"); |
| 79 | + } else if (composite_name.find("gna.relu") != std::string::npos) { |
| 80 | + root_call = backend::GetOpInFunction(fn, "relax.nn.relu"); |
| 81 | + } else { |
| 82 | + LOG(FATAL) << "Unimplemented GNA pattern: " << composite_name; |
| 83 | + } |
| 84 | + |
| 85 | + SetCallNodeAttribute(node, root_call); |
| 86 | + return AddNode(node, GetRef<Expr>(call_node)); |
| 87 | + } |
| 88 | + |
| 89 | + private: |
| 90 | + /*! \brief The bindings to look up composite functions. */ |
| 91 | + Map<Var, Expr> bindings_; |
| 92 | + |
| 93 | + void SetCallNodeAttribute(std::shared_ptr<JSONGraphNode> node, const CallNode* call) { |
| 94 | + // First call the base implementation to extract standard attributes |
| 95 | + JSONSerializer::SetCallNodeAttribute(node, call); |
| 96 | + |
| 97 | + // Add GNA-specific attributes based on the operation |
| 98 | + if (call && call->op.as<OpNode>()) { |
| 99 | + auto op = Downcast<Op>(call->op); |
| 100 | + std::string op_name = op->name; |
| 101 | + |
| 102 | + // Extract shape information from struct_info |
| 103 | + if (!call->args.empty()) { |
| 104 | + StructInfo input_sinfo = GetStructInfo(call->args[0]); |
| 105 | + if (const auto* tensor_sinfo = input_sinfo.as<TensorStructInfoNode>()) { |
| 106 | + if (tensor_sinfo->shape.defined()) { |
| 107 | + std::vector<std::string> shape_strs; |
| 108 | + ShapeExpr shape = Downcast<ShapeExpr>(tensor_sinfo->shape.value()); |
| 109 | + for (const PrimExpr& dim : shape->values) { |
| 110 | + if (const auto* int_imm = dim.as<tvm::tir::IntImmNode>()) { |
| 111 | + shape_strs.push_back(std::to_string(int_imm->value)); |
| 112 | + } else { |
| 113 | + shape_strs.push_back("-1"); |
| 114 | + } |
| 115 | + } |
| 116 | + std::vector<dmlc::any> shape_attr; |
| 117 | + shape_attr.emplace_back(shape_strs); |
| 118 | + node->SetAttr("input_shape", shape_attr); |
| 119 | + } |
| 120 | + |
| 121 | + std::vector<std::string> dtype_strs{tensor_sinfo->dtype.code() == kDLFloat ? "float32" |
| 122 | + : "int32"}; |
| 123 | + std::vector<dmlc::any> dtype_attr; |
| 124 | + dtype_attr.emplace_back(dtype_strs); |
| 125 | + node->SetAttr("input_dtype", dtype_attr); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (op_name == "relax.nn.conv1d") { |
| 130 | + if (call->attrs.defined()) { |
| 131 | + std::vector<std::string> op_attrs{"conv1d_op"}; |
| 132 | + std::vector<dmlc::any> op_attr; |
| 133 | + op_attr.emplace_back(op_attrs); |
| 134 | + node->SetAttr("gna_op_type", op_attr); |
| 135 | + } |
| 136 | + } else if (op_name == "relax.matmul") { |
| 137 | + std::vector<std::string> op_attrs{"dense_op"}; |
| 138 | + std::vector<dmlc::any> op_attr; |
| 139 | + op_attr.emplace_back(op_attrs); |
| 140 | + node->SetAttr("gna_op_type", op_attr); |
| 141 | + } else if (op_name == "relax.nn.relu") { |
| 142 | + std::vector<std::string> op_attrs{"activation_op"}; |
| 143 | + std::vector<dmlc::any> op_attr; |
| 144 | + op_attr.emplace_back(op_attrs); |
| 145 | + node->SetAttr("gna_op_type", op_attr); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +}; |
| 150 | + |
| 151 | +/*! |
| 152 | + * \brief Create a GNA JSON runtime module. |
| 153 | + * \param functions The functions to be compiled. |
| 154 | + * \param unused Unused config options. |
| 155 | + * \param constant_names The constant names to be used. |
| 156 | + * \return Array of runtime modules. |
| 157 | + */ |
| 158 | +Array<runtime::Module> GNACompiler(Array<Function> functions, Map<String, ffi::Any> /*unused*/, |
| 159 | + Map<Constant, String> constant_names) { |
| 160 | + Array<runtime::Module> compiled_functions; |
| 161 | + |
| 162 | + for (const auto& func : functions) { |
| 163 | + GNAJSONSerializer serializer(constant_names, AnalyzeVar2Value(func)); |
| 164 | + serializer.serialize(func); |
| 165 | + auto graph_json = serializer.GetJSON(); |
| 166 | + auto constant_names_used = serializer.GetConstantNames(); |
| 167 | + |
| 168 | + const auto pf = tvm::ffi::Function::GetGlobalRequired("runtime.GNAJSONRuntimeCreate"); |
| 169 | + auto func_name = GetExtSymbol(func); |
| 170 | + compiled_functions.push_back( |
| 171 | + pf(func_name, graph_json, constant_names_used).cast<runtime::Module>()); |
| 172 | + } |
| 173 | + |
| 174 | + return compiled_functions; |
| 175 | +} |
| 176 | + |
| 177 | +// Register the external codegen entrypoint via FFI reflection (new TVM registry) |
| 178 | +TVM_FFI_STATIC_INIT_BLOCK({ |
| 179 | + namespace refl = tvm::ffi::reflection; |
| 180 | + refl::GlobalDef().def("relax.ext.gna", GNACompiler); |
| 181 | +}); |
| 182 | + |
| 183 | +} // namespace contrib |
| 184 | +} // namespace relax |
| 185 | + |
| 186 | +namespace target { |
| 187 | + |
| 188 | +// Register GNA target kind |
| 189 | +TVM_REGISTER_TARGET_KIND("gna", kDLExtDev).set_default_keys({"gna"}); |
| 190 | + |
| 191 | +} // namespace target |
| 192 | + |
| 193 | +} // namespace tvm |
0 commit comments