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.

acl_stub.h 27 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 MINDSPORE_ACL_STUB_H
  17. #define MINDSPORE_ACL_STUB_H
  18. #include "acl/acl_base.h"
  19. #include "acl/acl.h"
  20. #include "acl/acl_mdl.h"
  21. #include "acl/acl_rt.h"
  22. #include "acl/ops/acl_dvpp.h"
  23. #include <algorithm>
  24. #include <vector>
  25. #include <string>
  26. #include <unordered_map>
  27. #include <map>
  28. #include <functional>
  29. #include <cstring>
  30. #include "jpeglib.h"
  31. struct aclDataBuffer {
  32. void *data = nullptr;
  33. size_t size = 0;
  34. };
  35. struct aclmdlDataset {
  36. std::vector<aclDataBuffer *> data_buffers;
  37. };
  38. struct aclTensorDesc {};
  39. struct AclTensorDesc {
  40. std::vector<int64_t> dims;
  41. aclDataType data_type = ACL_DT_UNDEFINED;
  42. size_t size = 0;
  43. };
  44. struct aclmdlDesc {
  45. std::vector<AclTensorDesc> inputs;
  46. std::vector<AclTensorDesc> outputs;
  47. };
  48. struct acldvppPicDesc {
  49. uint32_t size = 0;
  50. acldvppPixelFormat format = PIXEL_FORMAT_YUV_400;
  51. uint32_t width = 0;
  52. uint32_t height = 0;
  53. void *dataDev = nullptr;
  54. uint32_t widthStride = 0;
  55. uint32_t heightStride = 0;
  56. };
  57. struct acldvppRoiConfig {
  58. uint32_t left = 0;
  59. uint32_t right = 0;
  60. uint32_t top = 0;
  61. uint32_t bottom = 0;
  62. };
  63. struct acldvppResizeConfig {
  64. uint32_t id;
  65. };
  66. struct acldvppChannelDesc {
  67. bool channel_valid_flag = false;
  68. };
  69. class AclModel;
  70. extern AclModel *g_acl_model;
  71. template <class Type>
  72. aclError AclItemOnDestroy(
  73. std::vector<Type> &live, std::vector<Type> &destroy, const Type *destroy_item,
  74. std::function<void(Type &list_item)> func_release = [](Type &list_item) {}) {
  75. for (auto it = live.begin(); it != live.end(); it++) {
  76. if (&(*it) == destroy_item) {
  77. func_release(*it);
  78. destroy.push_back(*it);
  79. live.erase(it);
  80. return ACL_ERROR_NONE;
  81. }
  82. }
  83. return 1;
  84. }
  85. template <class PtType, typename std::enable_if<std::is_pointer<PtType>::value, int>::type = 0>
  86. class ResourceBase {
  87. public:
  88. using Type = typename std::remove_pointer<PtType>::type;
  89. ResourceBase() = default;
  90. virtual ~ResourceBase() { Clear(); }
  91. void Clear() {
  92. for (auto item : resource_live_) {
  93. delete item;
  94. }
  95. resource_live_.clear();
  96. resource_destroy_.clear();
  97. }
  98. template <class... Args>
  99. Type *OnCreate(Args &&... args) {
  100. auto item = new Type(std::forward<Args>(args)...);
  101. resource_live_.push_back(item);
  102. return item;
  103. }
  104. aclError OnDestroy(
  105. const Type *item, std::function<void(Type &list_item)> func_release = [](Type &list_item) {}) {
  106. auto it = std::find(resource_live_.begin(), resource_live_.end(), item);
  107. if (it == resource_live_.end()) {
  108. return 1;
  109. }
  110. func_release(**it); // Type&
  111. resource_destroy_.push_back(*it); // Type*
  112. resource_live_.erase(it);
  113. delete item;
  114. return ACL_ERROR_NONE;
  115. }
  116. size_t LiveSize() const { return resource_live_.size(); }
  117. bool Check() const { return resource_live_.empty(); }
  118. std::vector<Type *> resource_live_;
  119. std::vector<Type *> resource_destroy_;
  120. };
  121. class AclDataBuffer {
  122. public:
  123. AclDataBuffer() {}
  124. virtual ~AclDataBuffer() { Clear(); }
  125. virtual void Clear() { data_buffer_.Clear(); }
  126. bool Check() { return data_buffer_.Check(); }
  127. virtual aclDataBuffer *aclCreateDataBuffer(void *data, size_t size) {
  128. aclDataBuffer data_buffer;
  129. data_buffer.data = data;
  130. data_buffer.size = size;
  131. return data_buffer_.OnCreate(data_buffer);
  132. }
  133. virtual aclError aclDestroyDataBuffer(const aclDataBuffer *dataBuffer) { return data_buffer_.OnDestroy(dataBuffer); }
  134. virtual void *aclGetDataBufferAddr(const aclDataBuffer *dataBuffer) {
  135. if (dataBuffer == nullptr) {
  136. return nullptr;
  137. }
  138. return dataBuffer->data;
  139. }
  140. virtual uint32_t aclGetDataBufferSize(const aclDataBuffer *dataBuffer) {
  141. if (dataBuffer == nullptr) {
  142. return 0;
  143. }
  144. return dataBuffer->size;
  145. }
  146. ResourceBase<aclDataBuffer *> data_buffer_;
  147. };
  148. class AclDataSet {
  149. public:
  150. AclDataSet() {}
  151. virtual ~AclDataSet() { Clear(); }
  152. virtual void Clear() { dataset_.Clear(); }
  153. bool Check() { return dataset_.Check(); }
  154. public:
  155. virtual aclmdlDataset *aclmdlCreateDataset() { return dataset_.OnCreate(); }
  156. virtual aclError aclmdlDestroyDataset(const aclmdlDataset *dataSet) { return dataset_.OnDestroy(dataSet); }
  157. virtual aclError aclmdlAddDatasetBuffer(aclmdlDataset *dataSet, aclDataBuffer *dataBuffer) {
  158. if (dataSet == nullptr) {
  159. return 1;
  160. }
  161. dataSet->data_buffers.push_back(dataBuffer);
  162. return ACL_ERROR_NONE;
  163. }
  164. virtual size_t aclmdlGetDatasetNumBuffers(const aclmdlDataset *dataSet) {
  165. if (dataSet == nullptr) {
  166. return 0;
  167. }
  168. return dataSet->data_buffers.size();
  169. }
  170. virtual aclDataBuffer *aclmdlGetDatasetBuffer(const aclmdlDataset *dataSet, size_t index) {
  171. if (dataSet == nullptr || index >= dataSet->data_buffers.size()) {
  172. return nullptr;
  173. }
  174. return dataSet->data_buffers[index];
  175. }
  176. ResourceBase<aclmdlDataset *> dataset_;
  177. };
  178. class AclEnv {
  179. public:
  180. virtual aclError aclInit(const char *configPath) {
  181. is_init = true;
  182. return ACL_ERROR_NONE;
  183. }
  184. virtual aclError aclFinalize() {
  185. is_init = false;
  186. return ACL_ERROR_NONE;
  187. }
  188. bool Check() { return is_init == false; }
  189. bool is_init = false;
  190. };
  191. class AclModel {
  192. public:
  193. bool Check() { return model_live_.empty(); }
  194. virtual aclError aclmdlLoadFromFile(const char *modelPath, uint32_t *modelId) {
  195. model_live_.push_back(cur_max_model_id_);
  196. *modelId = cur_max_model_id_;
  197. cur_max_model_id_++;
  198. return ACL_ERROR_NONE;
  199. }
  200. virtual aclError aclmdlLoadFromMem(const void *model, size_t modelSize, uint32_t *modelId) {
  201. return aclmdlLoadFromFile("fake_path", modelId);
  202. }
  203. virtual aclError aclmdlLoadFromFileWithMem(const char *modelPath, uint32_t *modelId, void *workPtr, size_t workSize,
  204. void *weightPtr, size_t weightSize) {
  205. return aclmdlLoadFromFile(modelPath, modelId);
  206. }
  207. virtual aclError aclmdlLoadFromMemWithMem(const void *model, size_t modelSize, uint32_t *modelId, void *workPtr,
  208. size_t workSize, void *weightPtr, size_t weightSize) {
  209. return aclmdlLoadFromMem(model, modelSize, modelId);
  210. }
  211. virtual aclError aclmdlExecute(uint32_t modelId, const aclmdlDataset *input, aclmdlDataset *output) {
  212. if (std::find(model_live_.begin(), model_live_.end(), modelId) == model_live_.end()) {
  213. return 1;
  214. }
  215. if (input == nullptr || output == nullptr) {
  216. return false;
  217. }
  218. // auto& model_desc = model_live_[modelId];
  219. return ACL_ERROR_NONE;
  220. }
  221. virtual aclError aclmdlExecuteAsync(uint32_t modelId, const aclmdlDataset *input, aclmdlDataset *output,
  222. aclrtStream stream) {
  223. return ACL_ERROR_NONE;
  224. }
  225. virtual aclError aclmdlUnload(uint32_t modelId) {
  226. auto it = std::find(model_live_.begin(), model_live_.end(), modelId);
  227. if (it == model_live_.end()) {
  228. return 1;
  229. }
  230. model_live_.erase(it);
  231. model_destroy_.push_back(modelId);
  232. return ACL_ERROR_NONE;
  233. }
  234. uint32_t cur_max_model_id_ = 0;
  235. std::vector<uint32_t> model_live_;
  236. std::vector<uint32_t> model_destroy_;
  237. };
  238. class AclModelDesc {
  239. public:
  240. AclModelDesc() {}
  241. virtual ~AclModelDesc() { Clear(); }
  242. virtual void Clear() { model_desc_.Clear(); }
  243. bool Check() { return model_desc_.Check(); }
  244. public:
  245. virtual aclmdlDesc *aclmdlCreateDesc() { return model_desc_.OnCreate(); }
  246. aclError aclmdlDestroyDesc(aclmdlDesc *modelDesc) { return model_desc_.OnDestroy(modelDesc); }
  247. aclError aclmdlGetDesc(aclmdlDesc *modelDesc, uint32_t modelId) {
  248. auto &model_live = g_acl_model->model_live_;
  249. auto it = std::find(model_live.begin(), model_live.end(), modelId);
  250. if (it == model_live.end()) {
  251. return 1;
  252. }
  253. return ACL_ERROR_NONE;
  254. }
  255. size_t aclmdlGetNumInputs(aclmdlDesc *modelDesc) { return modelDesc->inputs.size(); }
  256. size_t aclmdlGetNumOutputs(aclmdlDesc *modelDesc) { return modelDesc->outputs.size(); }
  257. size_t aclmdlGetInputSizeByIndex(aclmdlDesc *modelDesc, size_t index) { return modelDesc->inputs[index].size; }
  258. size_t aclmdlGetOutputSizeByIndex(aclmdlDesc *modelDesc, size_t index) { return modelDesc->outputs[index].size; }
  259. aclError aclmdlGetInputDims(const aclmdlDesc *modelDesc, size_t index, aclmdlIODims *dims) {
  260. auto &input = modelDesc->inputs[index];
  261. dims->dimCount = input.dims.size();
  262. for (size_t i = 0; i < dims->dimCount; i++) {
  263. dims->dims[i] = input.dims[i];
  264. }
  265. return ACL_ERROR_NONE;
  266. }
  267. aclError aclmdlGetOutputDims(const aclmdlDesc *modelDesc, size_t index, aclmdlIODims *dims) {
  268. auto &input = modelDesc->outputs[index];
  269. dims->dimCount = input.dims.size();
  270. for (size_t i = 0; i < dims->dimCount; i++) {
  271. dims->dims[i] = input.dims[i];
  272. }
  273. return ACL_ERROR_NONE;
  274. }
  275. aclError aclmdlGetCurOutputDims(const aclmdlDesc *modelDesc, size_t index, aclmdlIODims *dims) {
  276. return aclmdlGetOutputDims(modelDesc, index, dims);
  277. }
  278. aclFormat aclmdlGetInputFormat(const aclmdlDesc *modelDesc, size_t index) { return ACL_FORMAT_NCHW; }
  279. aclFormat aclmdlGetOutputFormat(const aclmdlDesc *modelDesc, size_t index) { return ACL_FORMAT_NCHW; }
  280. aclDataType aclmdlGetInputDataType(const aclmdlDesc *modelDesc, size_t index) {
  281. return modelDesc->inputs[index].data_type;
  282. }
  283. aclDataType aclmdlGetOutputDataType(const aclmdlDesc *modelDesc, size_t index) {
  284. return modelDesc->outputs[index].data_type;
  285. }
  286. ResourceBase<aclmdlDesc *> model_desc_;
  287. };
  288. class AclRunMode {
  289. public:
  290. virtual aclError aclrtGetRunMode(aclrtRunMode *runMode) {
  291. *runMode = aclrtRunMode::ACL_HOST;
  292. return ACL_ERROR_NONE;
  293. }
  294. };
  295. class AclDeviceContextStream {
  296. public:
  297. AclDeviceContextStream() {}
  298. ~AclDeviceContextStream() { Clear(); }
  299. virtual void Clear() {
  300. for (auto context : context_live_) {
  301. delete (int *)context;
  302. }
  303. context_live_.clear();
  304. context_destroy_.clear();
  305. device_id_live_.clear();
  306. device_id_destroy_.clear();
  307. for (auto item : stream_live_) {
  308. delete (int *)item;
  309. }
  310. stream_live_.clear();
  311. stream_destroy_.clear();
  312. }
  313. bool Check() { return context_live_.empty() && device_id_live_.empty() && stream_live_.empty(); }
  314. virtual aclError aclrtCreateContext(aclrtContext *context, int32_t deviceId) {
  315. context_live_.push_back(new int());
  316. *context = context_live_.back();
  317. return ACL_ERROR_NONE;
  318. }
  319. virtual aclError aclrtDestroyContext(aclrtContext context) {
  320. for (auto it = context_live_.begin(); it != context_live_.end(); ++it) {
  321. if (*it == context) {
  322. context_live_.erase(it);
  323. context_destroy_.push_back(context);
  324. delete (int *)context;
  325. return ACL_ERROR_NONE;
  326. }
  327. }
  328. return 1;
  329. }
  330. aclError aclrtSetCurrentContext(aclrtContext context) { return ACL_ERROR_NONE; }
  331. aclError aclrtGetCurrentContext(aclrtContext *context) { return ACL_ERROR_NONE; }
  332. virtual aclError aclrtSetDevice(int32_t deviceId) {
  333. device_id_live_.push_back(deviceId);
  334. return ACL_ERROR_NONE;
  335. }
  336. virtual aclError aclrtResetDevice(int32_t deviceId) {
  337. for (auto it = device_id_live_.begin(); it != device_id_live_.end(); ++it) {
  338. if (*it == deviceId) {
  339. device_id_live_.erase(it);
  340. device_id_destroy_.push_back(deviceId);
  341. return ACL_ERROR_NONE;
  342. }
  343. }
  344. return 1;
  345. }
  346. aclError aclrtGetDevice(int32_t *deviceId) {
  347. *deviceId = 0;
  348. return ACL_ERROR_NONE;
  349. }
  350. aclError aclrtSynchronizeDevice(void) { return ACL_ERROR_NONE; }
  351. aclError aclrtSetTsDevice(aclrtTsId tsId) { return ACL_ERROR_NONE; }
  352. aclError aclrtGetDeviceCount(uint32_t *count) {
  353. *count = 1;
  354. return ACL_ERROR_NONE;
  355. }
  356. virtual aclError aclrtCreateStream(aclrtStream *stream) {
  357. stream_live_.push_back(new int());
  358. *stream = stream_live_.back();
  359. return ACL_ERROR_NONE;
  360. }
  361. virtual aclError aclrtDestroyStream(aclrtStream stream) {
  362. for (auto it = stream_live_.begin(); it != context_live_.end(); ++it) {
  363. if (*it == stream) {
  364. stream_live_.erase(it);
  365. stream_destroy_.push_back(stream);
  366. delete (int *)stream;
  367. return ACL_ERROR_NONE;
  368. }
  369. }
  370. return 1;
  371. }
  372. aclError aclrtSynchronizeStream(aclrtStream stream) {
  373. for (auto it = stream_live_.begin(); it != context_live_.end(); ++it) {
  374. if (*it == stream) {
  375. return ACL_ERROR_NONE;
  376. }
  377. }
  378. return 1;
  379. }
  380. std::vector<int32_t> device_id_live_;
  381. std::vector<int32_t> device_id_destroy_;
  382. std::vector<aclrtContext> context_live_;
  383. std::vector<aclrtContext> context_destroy_;
  384. std::vector<aclrtStream> stream_live_;
  385. std::vector<aclrtStream> stream_destroy_;
  386. };
  387. class AclMemory {
  388. public:
  389. AclMemory() {}
  390. ~AclMemory() { Clear(); }
  391. void Clear() {
  392. for (auto item : device_buffer_live_) {
  393. delete[] item;
  394. }
  395. for (auto item : host_buffer_live_) {
  396. delete[] item;
  397. }
  398. for (auto item : dvpp_buffer_live_) {
  399. delete[] item;
  400. }
  401. device_buffer_live_.clear();
  402. device_buffer_destroy_.clear();
  403. host_buffer_live_.clear();
  404. host_buffer_destroy_.clear();
  405. dvpp_buffer_live_.clear();
  406. dvpp_buffer_destroy_.clear();
  407. }
  408. bool Check() { return device_buffer_live_.empty() && host_buffer_live_.empty() && dvpp_buffer_live_.empty(); }
  409. virtual aclError aclrtMalloc(void **devPtr, size_t size, aclrtMemMallocPolicy policy) {
  410. auto buffer = new uint8_t[size];
  411. *devPtr = buffer;
  412. device_buffer_live_.push_back(buffer);
  413. memory_len_[buffer] = size;
  414. return ACL_ERROR_NONE;
  415. }
  416. aclError aclrtFree(void *devPtr) {
  417. auto it = std::find(device_buffer_live_.begin(), device_buffer_live_.end(), devPtr);
  418. if (it != device_buffer_live_.end()) {
  419. delete[](*it);
  420. device_buffer_live_.erase(it);
  421. device_buffer_destroy_.push_back(*it);
  422. return ACL_ERROR_NONE;
  423. }
  424. return 1;
  425. }
  426. virtual aclError aclrtMallocHost(void **hostPtr, size_t size) {
  427. auto buffer = new uint8_t[size];
  428. *hostPtr = buffer;
  429. host_buffer_live_.push_back(buffer);
  430. memory_len_[buffer] = size;
  431. return ACL_ERROR_NONE;
  432. }
  433. aclError aclrtFreeHost(void *hostPtr) {
  434. auto it = std::find(host_buffer_live_.begin(), host_buffer_live_.end(), hostPtr);
  435. if (it != host_buffer_live_.end()) {
  436. delete[](*it);
  437. host_buffer_live_.erase(it);
  438. host_buffer_destroy_.push_back(*it);
  439. return ACL_ERROR_NONE;
  440. }
  441. return 1;
  442. }
  443. aclError aclrtMemcpy(void *dst, size_t destMax, const void *src, size_t count, aclrtMemcpyKind kind) {
  444. auto is_device_memory = [this](const void *memory, uint32_t use_size) {
  445. for (auto it = device_buffer_live_.begin(); it != device_buffer_live_.end(); it++) {
  446. auto size = memory_len_[*it];
  447. if (memory >= *it && static_cast<const uint8_t *>(memory) + use_size <= (*it) + size) {
  448. return true;
  449. }
  450. }
  451. for (auto it = dvpp_buffer_live_.begin(); it != dvpp_buffer_live_.end(); it++) {
  452. auto size = memory_len_[*it];
  453. if (memory >= *it && static_cast<const uint8_t *>(memory) + use_size <= (*it) + size) {
  454. return true;
  455. }
  456. }
  457. return false;
  458. };
  459. if (kind == ACL_MEMCPY_HOST_TO_HOST) {
  460. if (is_device_memory(dst, destMax) || is_device_memory(src, count)) {
  461. return 1;
  462. }
  463. } else if (kind == ACL_MEMCPY_HOST_TO_DEVICE) {
  464. if (!is_device_memory(dst, destMax) || is_device_memory(src, count)) {
  465. return 1;
  466. }
  467. } else if (kind == ACL_MEMCPY_DEVICE_TO_HOST) {
  468. if (is_device_memory(dst, destMax) || !is_device_memory(src, count)) {
  469. return 1;
  470. }
  471. } else if (kind == ACL_MEMCPY_DEVICE_TO_DEVICE) {
  472. if (!is_device_memory(dst, destMax) || !is_device_memory(src, count)) {
  473. return 1;
  474. }
  475. } else {
  476. return 1;
  477. }
  478. memcpy(dst, src, count);
  479. return ACL_ERROR_NONE;
  480. }
  481. virtual aclError acldvppMalloc(void **devPtr, size_t size) {
  482. auto buffer = new uint8_t[size];
  483. *devPtr = buffer;
  484. dvpp_buffer_live_.push_back(buffer);
  485. memory_len_[buffer] = size;
  486. return ACL_ERROR_NONE;
  487. }
  488. aclError acldvppFree(void *devPtr) {
  489. auto it = std::find(dvpp_buffer_live_.begin(), dvpp_buffer_live_.end(), devPtr);
  490. if (it != dvpp_buffer_live_.end()) {
  491. delete[](*it);
  492. dvpp_buffer_live_.erase(it);
  493. dvpp_buffer_destroy_.push_back(*it);
  494. return ACL_ERROR_NONE;
  495. }
  496. return 1;
  497. }
  498. std::vector<uint8_t *> device_buffer_live_;
  499. std::vector<uint8_t *> device_buffer_destroy_;
  500. std::vector<uint8_t *> host_buffer_live_;
  501. std::vector<uint8_t *> host_buffer_destroy_;
  502. std::vector<uint8_t *> dvpp_buffer_live_;
  503. std::vector<uint8_t *> dvpp_buffer_destroy_;
  504. std::map<uint8_t *, uint32_t> memory_len_;
  505. };
  506. class AclDvppPicDesc {
  507. public:
  508. bool Check() { return pic_desc_.Check(); }
  509. acldvppPicDesc *acldvppCreatePicDesc() { return pic_desc_.OnCreate(); }
  510. aclError acldvppDestroyPicDesc(acldvppPicDesc *picDesc) { return pic_desc_.OnDestroy(picDesc); }
  511. aclError acldvppSetPicDescSize(acldvppPicDesc *picDesc, uint32_t size) {
  512. picDesc->size = size;
  513. return ACL_ERROR_NONE;
  514. }
  515. aclError acldvppSetPicDescFormat(acldvppPicDesc *picDesc, acldvppPixelFormat format) {
  516. picDesc->format = format;
  517. return ACL_ERROR_NONE;
  518. }
  519. aclError acldvppSetPicDescWidth(acldvppPicDesc *picDesc, uint32_t width) {
  520. picDesc->width = width;
  521. return ACL_ERROR_NONE;
  522. }
  523. aclError acldvppSetPicDescHeight(acldvppPicDesc *picDesc, uint32_t height) {
  524. picDesc->height = height;
  525. return ACL_ERROR_NONE;
  526. }
  527. aclError acldvppSetPicDescData(acldvppPicDesc *picDesc, void *dataDev) {
  528. picDesc->dataDev = dataDev;
  529. return ACL_ERROR_NONE;
  530. }
  531. aclError acldvppSetPicDescWidthStride(acldvppPicDesc *picDesc, uint32_t widthStride) {
  532. picDesc->widthStride = widthStride;
  533. return ACL_ERROR_NONE;
  534. }
  535. aclError acldvppSetPicDescHeightStride(acldvppPicDesc *picDesc, uint32_t heightStride) {
  536. picDesc->heightStride = heightStride;
  537. return ACL_ERROR_NONE;
  538. }
  539. ResourceBase<acldvppPicDesc *> pic_desc_;
  540. };
  541. class AclDvppRoiConfig {
  542. public:
  543. bool Check() { return roi_config_.Check(); }
  544. acldvppRoiConfig *acldvppCreateRoiConfig(uint32_t left, uint32_t right, uint32_t top, uint32_t bottom) {
  545. return roi_config_.OnCreate(acldvppRoiConfig{.left = left, .right = right, .top = top, .bottom = bottom});
  546. }
  547. aclError acldvppDestroyRoiConfig(acldvppRoiConfig *roiConfig) { return roi_config_.OnDestroy(roiConfig); }
  548. aclError acldvppSetRoiConfig(acldvppRoiConfig *roiConfig, uint32_t left, uint32_t right, uint32_t top,
  549. uint32_t bottom) {
  550. roiConfig->left = left;
  551. roiConfig->right = right;
  552. roiConfig->top = top;
  553. roiConfig->bottom = bottom;
  554. return ACL_ERROR_NONE;
  555. }
  556. ResourceBase<acldvppRoiConfig *> roi_config_;
  557. };
  558. class AclDvppResizeConfig {
  559. public:
  560. bool Check() { return resize_config_.Check(); }
  561. acldvppResizeConfig *acldvppCreateResizeConfig() { return resize_config_.OnCreate(acldvppResizeConfig{}); }
  562. aclError acldvppDestroyResizeConfig(acldvppResizeConfig *resizeConfig) {
  563. return resize_config_.OnDestroy(resizeConfig);
  564. }
  565. ResourceBase<acldvppResizeConfig *> resize_config_;
  566. };
  567. class AclDvppChannelDesc {
  568. public:
  569. bool Check() { return channel_desc_.Check(); }
  570. aclError acldvppCreateChannel(acldvppChannelDesc *channelDesc) {
  571. channelDesc->channel_valid_flag = true;
  572. return ACL_ERROR_NONE;
  573. }
  574. aclError acldvppDestroyChannel(acldvppChannelDesc *channelDesc) {
  575. channelDesc->channel_valid_flag = false;
  576. return ACL_ERROR_NONE;
  577. }
  578. acldvppChannelDesc *acldvppCreateChannelDesc() { return channel_desc_.OnCreate(); }
  579. aclError acldvppDestroyChannelDesc(acldvppChannelDesc *channelDesc) {
  580. if (channelDesc->channel_valid_flag) {
  581. return 1;
  582. }
  583. return channel_desc_.OnDestroy(channelDesc);
  584. }
  585. ResourceBase<acldvppChannelDesc *> channel_desc_;
  586. };
  587. class AclDvppProcess {
  588. public:
  589. bool Check() { return true; }
  590. virtual aclError acldvppVpcResizeAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc,
  591. acldvppPicDesc *outputDesc, acldvppResizeConfig *resizeConfig,
  592. aclrtStream stream) {
  593. resize_call_times_++;
  594. if (channelDesc == nullptr || inputDesc == nullptr || outputDesc == nullptr || resizeConfig == nullptr ||
  595. stream == nullptr) {
  596. return 1;
  597. }
  598. if (CheckPicDesc(inputDesc) != ACL_ERROR_NONE) {
  599. return 1;
  600. }
  601. if (CheckPicDesc(outputDesc) != ACL_ERROR_NONE) {
  602. return 1;
  603. }
  604. return ACL_ERROR_NONE;
  605. }
  606. virtual aclError acldvppVpcCropAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc,
  607. acldvppPicDesc *outputDesc, acldvppRoiConfig *cropArea, aclrtStream stream) {
  608. crop_call_times_++;
  609. if (channelDesc == nullptr || inputDesc == nullptr || outputDesc == nullptr || cropArea == nullptr ||
  610. stream == nullptr) {
  611. return 1;
  612. }
  613. if (CheckPicDesc(inputDesc) != ACL_ERROR_NONE) {
  614. return 1;
  615. }
  616. if (CheckPicDesc(outputDesc) != ACL_ERROR_NONE) {
  617. return 1;
  618. }
  619. if (CheckCropArea(cropArea) != ACL_ERROR_NONE) {
  620. return 1;
  621. }
  622. return ACL_ERROR_NONE;
  623. }
  624. virtual aclError acldvppVpcCropAndPasteAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc,
  625. acldvppPicDesc *outputDesc, acldvppRoiConfig *cropArea,
  626. acldvppRoiConfig *pasteArea, aclrtStream stream) {
  627. crop_paste_call_times_++;
  628. if (channelDesc == nullptr || inputDesc == nullptr || outputDesc == nullptr || cropArea == nullptr ||
  629. pasteArea == nullptr || stream == nullptr) {
  630. return 1;
  631. }
  632. if (CheckPicDesc(inputDesc) != ACL_ERROR_NONE) {
  633. return 1;
  634. }
  635. if (CheckPicDesc(outputDesc) != ACL_ERROR_NONE) {
  636. return 1;
  637. }
  638. if (CheckCropArea(cropArea) != ACL_ERROR_NONE) {
  639. return 1;
  640. }
  641. if (CheckCropArea(pasteArea) != ACL_ERROR_NONE) {
  642. return 1;
  643. }
  644. return ACL_ERROR_NONE;
  645. }
  646. aclError acldvppVpcBatchCropAsync(acldvppChannelDesc *channelDesc, acldvppBatchPicDesc *srcBatchDesc,
  647. uint32_t *roiNums, uint32_t size, acldvppBatchPicDesc *dstBatchDesc,
  648. acldvppRoiConfig *cropAreas[], aclrtStream stream) {
  649. return ACL_ERROR_NONE;
  650. }
  651. virtual aclError acldvppJpegDecodeAsync(acldvppChannelDesc *channelDesc, const void *data, uint32_t size,
  652. acldvppPicDesc *outputDesc, aclrtStream stream) {
  653. decode_call_times_++;
  654. if (channelDesc == nullptr || data == nullptr || size == 0 || outputDesc == nullptr || stream == nullptr) {
  655. return 1;
  656. }
  657. if (outputDesc->widthStride % 128 != 0) {
  658. return 1;
  659. }
  660. if (outputDesc->heightStride % 16 != 0) {
  661. return 1;
  662. }
  663. if (outputDesc->widthStride < 32 || outputDesc->widthStride > 8192) {
  664. return 1;
  665. }
  666. if (outputDesc->heightStride < 32 || outputDesc->heightStride > 8192) {
  667. return 1;
  668. }
  669. if (CheckPicDesc(outputDesc) != ACL_ERROR_NONE) {
  670. return 1;
  671. }
  672. return ACL_ERROR_NONE;
  673. }
  674. aclError CheckCropArea(acldvppRoiConfig *crop_area) {
  675. if (crop_area->left % 2 != 0 || crop_area->top % 2 != 0) {
  676. return 1;
  677. }
  678. if (crop_area->right % 2 != 1 || crop_area->bottom % 2 != 1) {
  679. return 1;
  680. }
  681. auto crop_width = crop_area->right - crop_area->left + 1;
  682. if (crop_width < 10 || crop_width > 4096) {
  683. return 1;
  684. }
  685. auto crop_heigth = crop_area->bottom - crop_area->top + 1;
  686. if (crop_heigth < 6 || crop_heigth > 4096) {
  687. return 1;
  688. }
  689. return ACL_ERROR_NONE;
  690. }
  691. aclError CheckPicDesc(acldvppPicDesc *pic_desc) {
  692. if (pic_desc->width == 0 || pic_desc->height == 0) {
  693. return 1;
  694. }
  695. if (pic_desc->widthStride % 16 != 0 || pic_desc->widthStride < pic_desc->width) {
  696. return 1;
  697. }
  698. if (pic_desc->heightStride % 2 != 0 || pic_desc->heightStride < pic_desc->height) {
  699. return 1;
  700. }
  701. if (pic_desc->widthStride < 32 || pic_desc->widthStride > 4096) {
  702. return 1;
  703. }
  704. if (pic_desc->heightStride < 6 || pic_desc->heightStride > 4096) {
  705. return 1;
  706. }
  707. if (pic_desc->dataDev == nullptr) {
  708. return 1;
  709. }
  710. auto size = pic_desc->size;
  711. auto ele_cnt = pic_desc->widthStride * pic_desc->heightStride;
  712. switch (pic_desc->format) {
  713. case PIXEL_FORMAT_YUV_SEMIPLANAR_420:
  714. case PIXEL_FORMAT_YVU_SEMIPLANAR_420:
  715. if (ele_cnt * 3 / 2 != size) {
  716. return 1;
  717. }
  718. break;
  719. case PIXEL_FORMAT_YUV_SEMIPLANAR_422:
  720. case PIXEL_FORMAT_YVU_SEMIPLANAR_422:
  721. if (ele_cnt * 2 != size) {
  722. return 1;
  723. }
  724. break;
  725. case PIXEL_FORMAT_YUV_SEMIPLANAR_444:
  726. case PIXEL_FORMAT_YVU_SEMIPLANAR_444:
  727. if (ele_cnt * 3 != size) {
  728. return 1;
  729. }
  730. break;
  731. default:
  732. return 1;
  733. }
  734. return ACL_ERROR_NONE;
  735. }
  736. uint32_t decode_call_times_ = 0;
  737. uint32_t resize_call_times_ = 0;
  738. uint32_t crop_call_times_ = 0;
  739. uint32_t crop_paste_call_times_ = 0;
  740. };
  741. class AclJpegLib {
  742. public:
  743. bool Check() { return jpeg_live_.empty(); }
  744. AclJpegLib(uint32_t width, uint32_t height) : image_width_(width), image_height_(height) {}
  745. void jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize) { jpeg_live_.push_back(cinfo); }
  746. void jpeg_mem_src(j_decompress_ptr cinfo, const unsigned char *inbuffer, unsigned long insize) {}
  747. int jpeg_read_header(j_decompress_ptr cinfo, boolean require_image) {
  748. static JHUFF_TBL tal;
  749. cinfo->image_width = image_width_;
  750. cinfo->image_height = image_height_;
  751. cinfo->jpeg_color_space = color_space_;
  752. for (int i = 0; i < NUM_HUFF_TBLS; i++) {
  753. cinfo->ac_huff_tbl_ptrs[i] = &tal;
  754. cinfo->dc_huff_tbl_ptrs[i] = &tal;
  755. }
  756. return 0;
  757. }
  758. void jpeg_destroy_decompress(j_decompress_ptr cinfo) {
  759. auto it = std::find(jpeg_live_.begin(), jpeg_live_.end(), cinfo);
  760. if (it != jpeg_live_.end()) {
  761. jpeg_live_.erase(it);
  762. }
  763. }
  764. uint32_t image_width_;
  765. uint32_t image_height_;
  766. J_COLOR_SPACE color_space_ = JCS_YCbCr;
  767. std::vector<j_decompress_ptr> jpeg_live_;
  768. };
  769. extern AclDataBuffer *g_acl_data_buffer;
  770. extern AclEnv *g_acl_env;
  771. extern AclDataSet *g_acl_dataset;
  772. extern AclModelDesc *g_acl_model_desc;
  773. extern AclDeviceContextStream *g_acl_device_context_stream;
  774. extern AclMemory *g_acl_memory;
  775. extern AclDvppPicDesc *g_acl_dvpp_pic_desc;
  776. extern AclDvppRoiConfig *g_acl_dvpp_roi_config;
  777. extern AclDvppResizeConfig *g_acl_dvpp_resize_config;
  778. extern AclDvppChannelDesc *g_acl_dvpp_channel_desc;
  779. extern AclDvppProcess *g_acl_dvpp_process;
  780. extern AclRunMode *g_acl_run_mode;
  781. extern AclJpegLib *g_acl_jpeg_lib;
  782. #endif // MINDSPORE_ACL_STUB_H