masahi commented on a change in pull request #7401: URL: https://github.com/apache/tvm/pull/7401#discussion_r822564072
########## File path: src/runtime/contrib/libtorch/libtorch_runtime.cc ########## @@ -0,0 +1,173 @@ +/* + * 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/runtime/contrib/libtorch/libtorch_runtime.cc + * \brief runtime implementation for LibTorch/TorchScript. + */ + +// we do not want clang to reorder our includes +// clang-format off +#include <tvm/runtime/module.h> +#include <tvm/runtime/ndarray.h> +#include <tvm/runtime/registry.h> + +#include <ATen/dlpack.h> +#include <ATen/DLConvertor.h> +#include <torch/csrc/jit/serialization/import.h> +#include <torch/torch.h> + +// clang-format on + +#include <cstddef> +#include <string> +#include <tuple> +#include <type_traits> +#include <unordered_map> +#include <utility> +#include <vector> + +namespace tvm { +namespace runtime { +namespace contrib { + +static void monly_deleter(DLManagedTensor* self) { delete self; } + +void run_torch_module(torch::jit::Module* module, TVMArgs args, TVMRetValue* rv) { + std::vector<torch::jit::IValue> inputs; + std::vector<torch::Tensor> outputs; + auto m = module->get_method("forward"); + for (int i = 0; i < args.size(); i++) { + const DLTensor* arg; + if (args[i].IsObjectRef<NDArray>()) { + NDArray arr = args[i]; + arg = arr.operator->(); + } else { + arg = args[i].operator DLTensor*(); + } + DLManagedTensor* inp = new DLManagedTensor{}; + inp->dl_tensor = *arg; + inp->deleter = &monly_deleter; + // m.num_inputs includes the self argument of forward(self, ...) + // num_inputs - 1 is the number of (Tensor) inputs + if (i < static_cast<int>(m.num_inputs()) - 1) { + inputs.emplace_back(at::fromDLPack(inp)); + } else { + outputs.emplace_back(at::fromDLPack(inp)); + } + } + ICHECK(outputs.size() == 1) << "wrong number of args, can handle only one output"; + torch::Tensor res = module->forward(inputs).toTensor(); + outputs[0].copy_(res); // too bad Review comment: yeah... -- 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]
