zhiics commented on a change in pull request #5753: URL: https://github.com/apache/incubator-tvm/pull/5753#discussion_r454046393
########## File path: src/runtime/graph/graph_runtime_factory.cc ########## @@ -0,0 +1,175 @@ +/* + * 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 graph_runtime_factory.cc + * \brief Graph runtime factory implementations + */ + +#include "./graph_runtime_factory.h" + +#include <tvm/node/container.h> +#include <tvm/runtime/device_api.h> +#include <tvm/runtime/registry.h> + +#include <iterator> +#include <vector> + +namespace tvm { +namespace runtime { + +GraphRuntimeFactory::GraphRuntimeFactory( + const std::string& graph_json, + const std::unordered_map<std::string, tvm::runtime::NDArray>& params, + const std::string& module_name) { + graph_json_ = graph_json; + params_ = params; + module_name_ = module_name; +} + +PackedFunc GraphRuntimeFactory::GetFunction( + const std::string& name, const tvm::runtime::ObjectPtr<tvm::runtime::Object>& sptr_to_self) { + if (name == module_name_) { + return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { + std::vector<TVMContext> contexts; + for (int i = 0; i < args.num_args; ++i) { + contexts.emplace_back(args[i].operator TVMContext()); + } + *rv = this->RuntimeCreate(contexts); + }); + } else if (name == "debug_create") { + return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { + CHECK_GE(args.size(), 2); + std::string module_name = args[0].operator String(); + CHECK(module_name == module_name_) << "Currently we only support single model for now."; + std::vector<TVMContext> contexts; + for (int i = 1; i < args.num_args; ++i) { + contexts.emplace_back(args[i].operator TVMContext()); + } + *rv = this->DebugRuntimeCreate(contexts); + }); + } else if (name == "remove_params") { + return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) { + std::unordered_map<std::string, tvm::runtime::NDArray> empty_params{}; + auto exec = + make_object<GraphRuntimeFactory>(this->graph_json_, empty_params, this->module_name_); + exec->Import(this->imports_[0]); + *rv = Module(exec); + }); + } else { + return PackedFunc(); + } +} + +void GraphRuntimeFactory::SaveToBinary(dmlc::Stream* stream) { + stream->Write(graph_json_); + std::vector<std::string> names; + std::vector<DLTensor*> arrays; + for (const auto& v : params_) { + names.emplace_back(v.first); + arrays.emplace_back(const_cast<DLTensor*>(v.second.operator->())); + } + uint64_t sz = arrays.size(); Review comment: It was introduced in #5770. We should not do it in this pr. This just makes you aware of it. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
