om_backend.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2024 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 <cstring>
  16. #include <iostream>
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #include "acl/acl.h"
  21. #include "ultra_infer/core/fd_tensor.h"
  22. #include "ultra_infer/runtime/backends/backend.h"
  23. namespace ultra_infer {
  24. class OmBackend : public BaseBackend {
  25. public:
  26. OmBackend() = default;
  27. virtual ~OmBackend();
  28. // OM Backend implementation.
  29. bool Init(const RuntimeOption &runtime_option) override;
  30. int NumInputs() const override {
  31. return static_cast<int>(inputs_desc_.size());
  32. }
  33. int NumOutputs() const override {
  34. return static_cast<int>(outputs_desc_.size());
  35. }
  36. TensorInfo GetInputInfo(int index) override;
  37. TensorInfo GetOutputInfo(int index) override;
  38. std::vector<TensorInfo> GetInputInfos() override;
  39. std::vector<TensorInfo> GetOutputInfos() override;
  40. bool Infer(std::vector<FDTensor> &inputs, std::vector<FDTensor> *outputs,
  41. bool copy_to_fd = true) override;
  42. static bool aclInitFlag;
  43. private:
  44. std::vector<TensorInfo> inputs_desc_;
  45. std::vector<TensorInfo> outputs_desc_;
  46. std::vector<void *> inputBuffer;
  47. std::vector<void *> outputBuffer;
  48. bool loadFlag_ = false; // model load flag
  49. int32_t deviceId_;
  50. uint32_t modelId_;
  51. size_t modelWorkSize_; // model work memory buffer size
  52. size_t modelWeightSize_; // model weight memory buffer size
  53. void *modelWorkPtr_; // model work memory buffer
  54. void *modelWeightPtr_; // model weight memory buffer
  55. aclmdlDesc *modelDesc_;
  56. aclmdlDataset *input_;
  57. aclmdlDataset *output_;
  58. aclrtContext context_;
  59. aclrtStream stream_;
  60. bool LoadModel(const char *modelPath);
  61. bool Execute();
  62. bool CreateInput();
  63. void DestroyInput();
  64. bool CreateOutput();
  65. void DestroyOutput();
  66. void DestroyResource();
  67. bool CreateModelDesc();
  68. void FreeInputBuffer();
  69. void FreeOutputBuffer();
  70. bool InitResource();
  71. };
  72. } // namespace ultra_infer