tvm_backend.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include "ultra_infer/core/fd_tensor.h"
  16. #include "ultra_infer/runtime/backends/backend.h"
  17. #include <cstring>
  18. #include <iostream>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include <dlpack/dlpack.h>
  23. #include <tvm/runtime/module.h>
  24. #include <tvm/runtime/packed_func.h>
  25. #include <tvm/runtime/registry.h>
  26. #include <unistd.h>
  27. namespace ultra_infer {
  28. class TVMBackend : public BaseBackend {
  29. public:
  30. TVMBackend() = default;
  31. virtual ~TVMBackend() = default;
  32. bool Init(const RuntimeOption &runtime_option) override;
  33. int NumInputs() const override { return inputs_desc_.size(); }
  34. int NumOutputs() const override { return outputs_desc_.size(); }
  35. TensorInfo GetInputInfo(int index) override { return inputs_desc_[index]; }
  36. TensorInfo GetOutputInfo(int index) override { return outputs_desc_[index]; }
  37. std::vector<TensorInfo> GetInputInfos() override { return inputs_desc_; }
  38. std::vector<TensorInfo> GetOutputInfos() override { return outputs_desc_; }
  39. bool Infer(std::vector<FDTensor> &inputs, std::vector<FDTensor> *outputs,
  40. bool copy_to_fd = true) override;
  41. private:
  42. DLDevice dev_{};
  43. tvm::runtime::Module gmod_;
  44. std::vector<TensorInfo> inputs_desc_;
  45. std::vector<TensorInfo> outputs_desc_;
  46. bool BuildDLDevice(Device device);
  47. bool BuildModel(const RuntimeOption &runtime_option);
  48. bool InitInputAndOutputTensor();
  49. std::vector<tvm::runtime::NDArray> input_tensor_;
  50. std::vector<tvm::runtime::NDArray> output_tensor_;
  51. FDDataType TVMTensorTypeToFDDataType(tvm::String type);
  52. DLDataType FDDataTypeToDLDataType(FDDataType dtype);
  53. };
  54. } // namespace ultra_infer