You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

model_process.h 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef INC_MODEL_PROCESS_ACL
  17. #define INC_MODEL_PROCESS_ACL
  18. #include <vector>
  19. #include <string>
  20. #include "acl/acl.h"
  21. #include "acl/acl_mdl.h"
  22. #include "acl/acl_rt.h"
  23. #include "include/inference.h"
  24. namespace mindspore {
  25. namespace inference {
  26. struct AclTensorInfo {
  27. void *device_data;
  28. size_t buffer_size;
  29. aclDataType data_type;
  30. std::vector<int64_t> dims;
  31. };
  32. struct ImagesDvppOutput {
  33. void *buffer_device = nullptr;
  34. size_t buffer_size = 0;
  35. size_t input_index = 0;
  36. };
  37. class ModelProcess {
  38. public:
  39. ModelProcess() {}
  40. ~ModelProcess() {}
  41. Status LoadModelFromFile(const std::string &file_name, uint32_t &model_id);
  42. void UnLoad();
  43. // override this method to avoid request/reply data copy
  44. Status Execute(const RequestBase &request, ReplyBase &reply);
  45. Status Execute(const void *dvpp_outputs_buffer_dev, size_t dvpp_outputs_buffer_size, ReplyBase &reply);
  46. void SetIsDevice(bool is_device) { is_run_on_device_ = is_device; }
  47. size_t GetBatchSize() const;
  48. private:
  49. uint32_t model_id_ = 0xffffffff;
  50. // if run one device(AICPU), there is no need to alloc device memory and copy inputs to(/outputs from) device
  51. bool is_run_on_device_ = false;
  52. aclmdlDesc *model_desc_ = nullptr;
  53. aclmdlDataset *inputs_ = nullptr;
  54. aclmdlDataset *outputs_ = nullptr;
  55. std::vector<AclTensorInfo> input_infos_;
  56. std::vector<AclTensorInfo> output_infos_;
  57. Status PreInitModelResource();
  58. Status CreateDataBuffer(void *&data_mem_buffer, size_t buffer_size, aclmdlDataset *dataset);
  59. Status CheckAndInitInput(const RequestBase &request);
  60. Status CheckAndInitDvppInput(const void *dvpp_outputs_buffer_dev, size_t dvpp_outputs_buffer_size,
  61. size_t input_index);
  62. Status BuildOutputs(ReplyBase &reply);
  63. Status InitInputsBuffer();
  64. Status InitOutputsBuffer();
  65. void DestroyInputsDataset();
  66. void DestroyInputsDataMem();
  67. void DestroyInputsBuffer();
  68. void DestroyOutputsBuffer();
  69. };
  70. } // namespace inference
  71. } // namespace mindspore
  72. #endif