• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 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 
17 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_ACL_ADAPTER_H_
18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_ACL_ADAPTER_H_
19 
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 
26 #include "minddata/dataset/kernels/image/dvpp/utils/acl_plugin.h"
27 #include "minddata/dataset/kernels/image/dvpp/utils/AclLiteError.h"
28 #include "minddata/dataset/kernels/image/dvpp/utils/ErrorCode.h"
29 
30 namespace mindspore::dataset {
31 class AclAdapter {
32  public:
33   static AclAdapter &GetInstance();
34 
HasAclPlugin()35   bool HasAclPlugin() const { return plugin_handle_ != nullptr; }
36 
37   void *CreateDvppVideo(void *context, uint8_t *data, uint32_t size, uint32_t width, uint32_t height, uint32_t type,
38                         uint32_t out_format, const std::string &output) const;
39   AclLiteError InitDvppVideo(void *dvpp_video) const;
40   AclLiteError CloseDvppVideo(void *dvpp_video) const;
41   AclLiteError DvppVideoDumpFrame(void *dvpp_video) const;
42   APP_ERROR InitResource(ResourceInfo *resource_info) const;
43   void *GetContext(int device_id) const;
44   void Release() const;
45   void *CreateAclProcessWithResize(uint32_t resize_width, uint32_t resize_height, uint32_t crop_width,
46                                    uint32_t crop_height, void *context, bool is_crop, void *stream,
47                                    const std::shared_ptr<DvppCommon> &dvpp_common) const;
48   void *CreateAclProcessWithPara(uint32_t para_width, uint32_t para_height, void *context, bool is_crop, void *stream,
49                                  const std::shared_ptr<DvppCommon> &dvpp_common) const;
50   void *CreateAclProcess(void *context, bool is_crop, void *stream,
51                          const std::shared_ptr<DvppCommon> &dvpp_common) const;
52   void DestroyAclProcess(void *acl_process) const;
53   APP_ERROR ReleaseAclProcess(void *acl_process) const;
54   APP_ERROR InitAclProcess(void *acl_process) const;
55   void *GetContextFromAclProcess(void *acl_process) const;
56   void *GetStreamFromAclProcess(void *acl_process) const;
57   APP_ERROR JPEG_DRC_WITH_DATA(void *acl_process, const RawData &data) const;
58   APP_ERROR JPEG_DR_WITH_DATA(void *acl_process, const RawData &data) const;
59   APP_ERROR JPEG_D_WITH_DATA(void *acl_process, const RawData &data) const;
60   APP_ERROR JPEG_R_WITH_DATA(void *acl_process, const DvppDataInfo &data) const;
61   APP_ERROR JPEG_C_WITH_DATA(void *acl_process, const DvppDataInfo &data) const;
62   APP_ERROR PNG_D_WITH_DATA(void *acl_process, const RawData &data) const;
63   APP_ERROR JPEG_DRC(void *acl_process) const;
64   APP_ERROR JPEG_DR(void *acl_process) const;
65   APP_ERROR JPEG_D(void *acl_process) const;
66   APP_ERROR JPEG_R(void *acl_process, const std::string &last_step) const;
67   APP_ERROR JPEG_C(void *acl_process, const std::string &last_step) const;
68   APP_ERROR PNG_D(void *acl_process) const;
69   void *GetMemoryData(void *acl_process) const;
70   DvppDataInfo *GetCropedDeviceData(void *acl_process) const;
71   DvppDataInfo *GetResizedDeviceData(void *acl_process) const;
72   DvppDataInfo *GetDecodeDeviceData(void *acl_process) const;
73   APP_ERROR H2D_Sink(void *acl_process, const std::shared_ptr<mindspore::dataset::Tensor> &input,
74                      std::shared_ptr<mindspore::dataset::DeviceTensor> *device_input) const;
75   APP_ERROR D2H_Pop(void *acl_process, const std::shared_ptr<mindspore::dataset::DeviceTensor> &device_output,
76                     std::shared_ptr<mindspore::dataset::Tensor> *output) const;
77   APP_ERROR DeviceMemoryRelease(void *acl_process) const;
78   APP_ERROR SetResizeParas(void *acl_process, uint32_t width, uint32_t height) const;
79   APP_ERROR SetCropParas(void *acl_process, uint32_t width, uint32_t height) const;
80   int Memcpy(void *dst, size_t dest_max, const void *src, size_t count, int kind) const;
81   int MallocHost(void **host_ptr, size_t size) const;
82   int FreeHost(void *host_ptr) const;
83 
84 #if !defined(BUILD_LITE) && defined(ENABLE_D)
85   // Ascend910B
86   APP_ERROR DvppAdjustBrightness(const std::shared_ptr<DeviceTensorAscend910B> &input,
87                                  std::shared_ptr<DeviceTensorAscend910B> *output, float factor);
88 
89   APP_ERROR DvppAdjustContrast(const std::shared_ptr<DeviceTensorAscend910B> &input,
90                                std::shared_ptr<DeviceTensorAscend910B> *output, float factor);
91 
92   APP_ERROR DvppAdjustHue(const std::shared_ptr<DeviceTensorAscend910B> &input,
93                           std::shared_ptr<DeviceTensorAscend910B> *output, float factor);
94 
95   APP_ERROR DvppAdjustSaturation(const std::shared_ptr<DeviceTensorAscend910B> &input,
96                                  std::shared_ptr<DeviceTensorAscend910B> *output, float factor);
97 
98   APP_ERROR DvppAdjustSharpness(const std::shared_ptr<DeviceTensorAscend910B> &input,
99                                 std::shared_ptr<DeviceTensorAscend910B> *output, float factor);
100 
101   APP_ERROR DvppAffine(const std::shared_ptr<DeviceTensorAscend910B> &input,
102                        std::shared_ptr<DeviceTensorAscend910B> *output, const std::vector<float> &matrix,
103                        uint32_t interpolation_mode, uint32_t padding_mode, const std::vector<float> &fill);
104 
105   APP_ERROR DvppAutoContrast(const std::shared_ptr<DeviceTensorAscend910B> &input,
106                              std::shared_ptr<DeviceTensorAscend910B> *output, const std::vector<float> &cutoff,
107                              const std::vector<uint32_t> &ignore);
108   APP_ERROR DvppConvertColor(const std::shared_ptr<DeviceTensorAscend910B> &input,
109                              std::shared_ptr<DeviceTensorAscend910B> *output, ConvertMode convertMode);
110 
111   APP_ERROR DvppCrop(const std::shared_ptr<DeviceTensorAscend910B> &input,
112                      std::shared_ptr<DeviceTensorAscend910B> *output, uint32_t top, uint32_t left, uint32_t height,
113                      uint32_t width);
114 
115   APP_ERROR DvppDecode(const std::shared_ptr<DeviceTensorAscend910B> &input,
116                        std::shared_ptr<DeviceTensorAscend910B> *output);
117 
118   APP_ERROR DvppEqualize(const std::shared_ptr<DeviceTensorAscend910B> &input,
119                          std::shared_ptr<DeviceTensorAscend910B> *output);
120 
121   APP_ERROR DvppErase(const std::shared_ptr<DeviceTensorAscend910B> &input,
122                       std::shared_ptr<DeviceTensorAscend910B> *output, uint32_t top, uint32_t left, uint32_t height,
123                       uint32_t width, const std::vector<float> &value);
124 
125   APP_ERROR DvppGaussianBlur(const std::shared_ptr<DeviceTensorAscend910B> &input,
126                              std::shared_ptr<DeviceTensorAscend910B> *output, const std::vector<int64_t> &kernel_size,
127                              const std::vector<float> &sigma, uint32_t padding_mode);
128 
129   APP_ERROR DvppHorizontalFlip(const std::shared_ptr<DeviceTensorAscend910B> &input,
130                                std::shared_ptr<DeviceTensorAscend910B> *output);
131 
132   APP_ERROR DvppInvert(const std::shared_ptr<DeviceTensorAscend910B> &input,
133                        std::shared_ptr<DeviceTensorAscend910B> *output);
134 
135   APP_ERROR DvppNormalize(const std::shared_ptr<DeviceTensorAscend910B> &input,
136                           std::shared_ptr<DeviceTensorAscend910B> *output, std::vector<float> mean,
137                           std::vector<float> std, bool is_hwc);
138 
139   APP_ERROR DvppPad(const std::shared_ptr<DeviceTensorAscend910B> &input,
140                     std::shared_ptr<DeviceTensorAscend910B> *output, const std::vector<int64_t> &padding,
141                     uint32_t padding_mode, const std::vector<float> &fill);
142 
143   APP_ERROR DvppPerspective(const std::shared_ptr<DeviceTensorAscend910B> &input,
144                             std::shared_ptr<DeviceTensorAscend910B> *output,
145                             const std::vector<std::vector<int32_t>> &start_points,
146                             const std::vector<std::vector<int32_t>> &end_points, InterpolationMode interpolation);
147 
148   APP_ERROR DvppPosterize(const std::shared_ptr<DeviceTensorAscend910B> &input,
149                           std::shared_ptr<DeviceTensorAscend910B> *output, uint8_t bits);
150 
151   APP_ERROR DvppResize(const std::shared_ptr<DeviceTensorAscend910B> &input,
152                        std::shared_ptr<DeviceTensorAscend910B> *output, int32_t output_height, int32_t output_width,
153                        double fx, double fy, InterpolationMode mode);
154 
155   APP_ERROR DvppResizedCrop(const std::shared_ptr<DeviceTensorAscend910B> &input,
156                             std::shared_ptr<DeviceTensorAscend910B> *output, int32_t top, int32_t left, int32_t height,
157                             int32_t width, int32_t output_height, int32_t output_width,
158                             InterpolationMode interpolation);
159 
160   APP_ERROR DvppRotate(const std::shared_ptr<DeviceTensorAscend910B> &input,
161                        std::shared_ptr<DeviceTensorAscend910B> *output, float degrees, InterpolationMode mode,
162                        bool expand, const std::vector<float> &center, const std::vector<float> &fill);
163   APP_ERROR DvppSolarize(const std::shared_ptr<DeviceTensorAscend910B> &input,
164                          std::shared_ptr<DeviceTensorAscend910B> *output, const std::vector<float> &threshold);
165 
166   APP_ERROR DvppVerticalFlip(const std::shared_ptr<DeviceTensorAscend910B> &input,
167                              std::shared_ptr<DeviceTensorAscend910B> *output);
168 
169   // acl
170   APP_ERROR GetSocName(std::string *soc_name);
171 
172   APP_ERROR CreateAclTensor(const int64_t *view_dims, uint64_t view_dims_num, mindspore::TypeId data_type,
173                             const int64_t *stride, int64_t offset, const int64_t *storage_dims,
174                             uint64_t storage_dims_num, void *tensor_data, bool is_hwc, void **acl_tensor);
175 
176   APP_ERROR DestroyTensor(void *tensor);
177 
178   APP_ERROR DestroyFloatArray(void *float_array);
179 
180   APP_ERROR DestroyIntArray(void *int_array);
181 #endif
182 
183  private:
184   AclAdapter() = default;
~AclAdapter()185   ~AclAdapter() { FinalizePlugin(); }
186   void InitPlugin();
187   void FinalizePlugin();
188 
189   void *plugin_handle_ = nullptr;
190 
191   CreateDvppVideoFunObj create_dvpp_video_fun_obj_;
192   InitDvppVideoFunObj init_dvpp_video_fun_obj_;
193   CloseDvppVideoFunObj close_dvpp_video_fun_obj_;
194   DvppVideoDumpFrameFunObj dvpp_video_dump_frame_fun_obj_;
195   InitResourceFunObj init_resource_fun_obj_;
196   GetContextFunObj get_context_fun_obj_;
197   ReleaseFunObj release_fun_obj_;
198   CreateAclProcessWithResizeFunObj create_acl_process_with_resize_fun_obj_;
199   CreateAclProcessWithParaFunObj create_acl_process_with_para_fun_obj_;
200   CreateAclProcessFunObj create_acl_process_fun_obj_;
201   DestroyAclProcessFunObj destroy_acl_process_fun_obj_;
202   ReleaseAclProcessFunObj release_acl_process_fun_obj_;
203   InitAclProcessFunObj init_acl_process_fun_obj_;
204   GetContextFromAclProcessFunObj get_context_from_acl_process_fun_obj_;
205   GetStreamFromAclProcessFunObj get_stream_from_acl_process_fun_obj_;
206   JPEG_DRC_WITH_DATAFunObj jpeg_drc_with_data_fun_obj_;
207   JPEG_DR_WITH_DATAFunObj jpeg_dr_with_data_fun_obj_;
208   JPEG_D_WITH_DATAFunObj jpeg_d_with_data_fun_obj_;
209   JPEG_R_WITH_DATAFunObj jpeg_r_with_data_fun_obj_;
210   JPEG_C_WITH_DATAFunObj jpeg_c_with_data_fun_obj_;
211   PNG_D_WITH_DATAFunObj png_d_with_data_fun_obj_;
212   JPEG_DRCFunObj jpeg_drc_fun_obj_;
213   JPEG_DRFunObj jpeg_dr_fun_obj_;
214   JPEG_DFunObj jpeg_d_fun_obj_;
215   JPEG_RFunObj jpeg_r_fun_obj_;
216   JPEG_CFunObj jpeg_c_fun_obj_;
217   PNG_DFunObj png_d_fun_obj_;
218   GetMemoryDataFunObj get_memory_data_fun_obj_;
219   GetCropedDeviceDataFunObj get_croped_device_data_fun_obj_;
220   GetResizedDeviceDataFunObj get_resized_device_data_fun_obj_;
221   GetDecodeDeviceDataFunObj get_decode_device_data_fun_obj_;
222   H2D_SinkFunObj h_2_d_sink_fun_obj_;
223   D2H_PopFunObj d_2_h_pop_fun_obj_;
224   DeviceMemoryReleaseFunObj device_memory_release_fun_obj_;
225   SetResizeParasFunObj set_resize_paras_fun_obj_;
226   SetCropParasFunObj set_crop_paras_fun_obj_;
227   aclrtMallocHostFunObj aclrt_malloc_host_fun_obj_;
228   aclrtFreeHostFunObj aclrt_free_host_fun_obj_;
229   aclrtMemcpyFunObj aclrt_memcpy_fun_obj_;
230 
231 #if !defined(BUILD_LITE) && defined(ENABLE_D)
232   // Ascend910B
233   DvppAdjustBrightnessFunObj dvpp_brightness_fun_obj_;
234   DvppAdjustContrastFunObj dvpp_contrast_fun_obj_;
235   DvppAdjustHueFunObj dvpp_hue_fun_obj_;
236   DvppAdjustSaturationFunObj dvpp_saturation_fun_obj_;
237   DvppAdjustSharpnessFunObj dvpp_sharpness_fun_obj_;
238   DvppAffineFunObj dvpp_affine_fun_obj_;
239   DvppAutoContrastFunObj dvpp_auto_contrast_fun_obj_;
240   DvppConvertColorFunObj dvpp_convert_color_fun_obj_;
241   DvppCropFunObj dvpp_crop_fun_obj_;
242   DvppDecodeFunObj dvpp_decode_fun_obj_;
243   DvppEqualizeFunObj dvpp_equalize_fun_obj_;
244   DvppEraseFunObj dvpp_erase_fun_obj_;
245   DvppGaussianBlurFunObj dvpp_gaussian_blur_fun_obj_;
246   DvppHorizontalFlipFunObj dvpp_horizontal_flip_fun_obj_;
247   DvppInvertFunObj dvpp_invert_fun_obj_;
248   DvppNormalizeFunObj dvpp_normalize_fun_obj_;
249   DvppPadFunObj dvpp_pad_fun_obj_;
250   DvppPerspectiveFunObj dvpp_perspective_fun_obj_;
251   DvppPosterizeFunObj dvpp_posterize_fun_obj_;
252   DvppResizeFunObj dvpp_resize_fun_obj_;
253   DvppResizedCropFunObj dvpp_resized_crop_fun_obj_;
254   DvppRotateFunObj dvpp_rotate_fun_obj_;
255   DvppSolarizeFunObj dvpp_solarize_fun_obj_;
256   DvppVerticalFlipFunObj dvpp_vertical_flip_fun_obj_;
257 
258   // acl interface
259   GetSocNameFunObj get_soc_name_fun_obj_;
260   CreateAclTensorFunObj create_acl_tensor_fun_obj_;
261   DestroyTensorFunObj destroy_tensor_fun_obj_;
262   DestroyFloatArrayFunObj destroy_float_array_fun_obj_;
263   DestroyIntArrayFunObj destroy_int_array_fun_obj_;
264 #endif
265 };
266 }  // namespace mindspore::dataset
267 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_DVPP_ACL_ADAPTER_H_
268