lite_backend.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <iostream>
  16. #include <memory>
  17. #include <string>
  18. #include <vector>
  19. #include "paddle_api.h" // NOLINT
  20. #include "ultra_infer/runtime/backends/backend.h"
  21. #include "ultra_infer/runtime/backends/lite/option.h"
  22. #include "ultra_infer/runtime/runtime_option.h"
  23. namespace ultra_infer {
  24. class LiteBackend : public BaseBackend {
  25. public:
  26. LiteBackend() {}
  27. virtual ~LiteBackend() = default;
  28. bool Init(const RuntimeOption &option) override;
  29. bool Infer(std::vector<FDTensor> &inputs, std::vector<FDTensor> *outputs,
  30. bool copy_to_fd = true) override; // NOLINT
  31. int NumInputs() const override { return inputs_desc_.size(); }
  32. int NumOutputs() const override { return outputs_desc_.size(); }
  33. TensorInfo GetInputInfo(int index) override;
  34. TensorInfo GetOutputInfo(int index) override;
  35. std::vector<TensorInfo> GetInputInfos() override;
  36. std::vector<TensorInfo> GetOutputInfos() override;
  37. private:
  38. // Build CxxConfig from option for Paddle Lite full api.
  39. void BuildOption(const LiteBackendOption &option);
  40. // Configure many hardwares for Paddle Lite full api.
  41. void ConfigureCpu(const LiteBackendOption &option);
  42. void ConfigureGpu(const LiteBackendOption &option);
  43. void ConfigureTimvx(const LiteBackendOption &option);
  44. void ConfigureAscend(const LiteBackendOption &option);
  45. void ConfigureKunlunXin(const LiteBackendOption &option);
  46. void ConfigureNNAdapter(const LiteBackendOption &option);
  47. paddle::lite_api::CxxConfig config_;
  48. std::shared_ptr<paddle::lite_api::PaddlePredictor> predictor_;
  49. paddle::lite_api::MobileConfig mobile_config_;
  50. std::vector<TensorInfo> inputs_desc_;
  51. std::vector<TensorInfo> outputs_desc_;
  52. std::map<std::string, int> inputs_order_;
  53. LiteBackendOption option_;
  54. };
  55. // Convert data type from paddle lite to ultra_infer
  56. FDDataType LiteDataTypeToFD(const paddle::lite_api::PrecisionType &dtype);
  57. // Helper function to read file
  58. bool ReadFile(const std::string &filename, std::vector<char> *contents,
  59. bool binary = true);
  60. } // namespace ultra_infer