sophgo_backend.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "bmlib_runtime.h" // NOLINT
  16. #include "bmruntime_interface.h" // NOLINT
  17. #include "ultra_infer/core/fd_tensor.h"
  18. #include "ultra_infer/runtime/backends/backend.h"
  19. #include "ultra_infer/runtime/backends/sophgo/option.h"
  20. #include <cstring>
  21. #include <iostream>
  22. #include <memory>
  23. #include <string>
  24. #include <vector>
  25. namespace ultra_infer {
  26. class SophgoBackend : public BaseBackend {
  27. public:
  28. SophgoBackend() = default;
  29. virtual ~SophgoBackend();
  30. bool Init(const RuntimeOption &option);
  31. int NumInputs() const override {
  32. return static_cast<int>(inputs_desc_.size());
  33. }
  34. int NumOutputs() const override {
  35. return static_cast<int>(outputs_desc_.size());
  36. }
  37. TensorInfo GetInputInfo(int index) override;
  38. TensorInfo GetOutputInfo(int index) override;
  39. std::vector<TensorInfo> GetInputInfos() override;
  40. std::vector<TensorInfo> GetOutputInfos() override;
  41. bool Infer(std::vector<FDTensor> &inputs, std::vector<FDTensor> *outputs,
  42. bool copy_to_fd = true) override;
  43. private:
  44. bool LoadModel(void *model);
  45. bool GetSDKAndDeviceVersion();
  46. bool GetModelInputOutputInfos();
  47. std::vector<TensorInfo> inputs_desc_;
  48. std::vector<TensorInfo> outputs_desc_;
  49. std::string net_name_;
  50. bm_handle_t handle_;
  51. void *p_bmrt_ = nullptr;
  52. bool infer_init = false;
  53. const bm_net_info_t *net_info_ = nullptr;
  54. // SophgoTPU2BackendOption option_;
  55. static FDDataType SophgoTensorTypeToFDDataType(bm_data_type_t type);
  56. static bm_data_type_t FDDataTypeToSophgoTensorType(FDDataType type);
  57. };
  58. } // namespace ultra_infer