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.

pipelinecache.cpp 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "pipelinecache.h"
  15. #include "gpu.h"
  16. namespace ncnn {
  17. #if NCNN_VULKAN
  18. // https://en.wikipedia.org/wiki/MurmurHash
  19. static uint32_t murmur3_32(const uint32_t* data, int size)
  20. {
  21. uint32_t h = 0;
  22. for (int i = 0; i < size; i++)
  23. {
  24. uint32_t k = *data++;
  25. k *= 0xcc9e2d51;
  26. k = (k << 15) | (k >> (32 - 15));
  27. k *= 0x1b873593;
  28. h ^= k;
  29. h = (h << 13) | (h >> (32 - 13));
  30. h = (h * 5) + 0xe6546b64;
  31. }
  32. h ^= size * 4;
  33. h ^= h >> 16;
  34. h *= 0x85ebca6b;
  35. h ^= h >> 13;
  36. h *= 0xc2b2ae35;
  37. h ^= h >> 16;
  38. return h;
  39. }
  40. // https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash
  41. static uint32_t fnv1a_32(const uint8_t* data, int size)
  42. {
  43. uint32_t h = 0x811c9dc5;
  44. for (int i = 0; i < size; i++)
  45. {
  46. h ^= (uint32_t)*data++;
  47. h *= 0x01000193;
  48. }
  49. return h;
  50. }
  51. class PipelineCachePrivate
  52. {
  53. public:
  54. // digest -> artifact
  55. struct pipeline_cache_digest
  56. {
  57. pipeline_cache_digest(const uint32_t* spv_data, size_t spv_data_size, const std::vector<vk_specialization_type>& specializations,
  58. uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z);
  59. pipeline_cache_digest(int shader_type_index, const Option& opt, const std::vector<vk_specialization_type>& specializations,
  60. uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z);
  61. bool operator==(const pipeline_cache_digest& rhs) const
  62. {
  63. return d0 == rhs.d0 && d1 == rhs.d1;
  64. }
  65. bool operator!=(const pipeline_cache_digest& rhs) const
  66. {
  67. return d0 != rhs.d0 || d1 != rhs.d1;
  68. }
  69. union
  70. {
  71. struct
  72. {
  73. union
  74. {
  75. uint32_t spv_data_murmur3;
  76. int shader_type_index;
  77. };
  78. unsigned char opt_local_size_bits[4];
  79. };
  80. uint64_t d0;
  81. };
  82. union
  83. {
  84. struct
  85. {
  86. uint32_t specializations_murmur3;
  87. uint32_t specializations_fnv1a;
  88. };
  89. uint64_t d1;
  90. };
  91. };
  92. struct pipeline_cache_artifact
  93. {
  94. VkShaderModule shader_module;
  95. VkDescriptorSetLayout descriptorset_layout;
  96. VkPipelineLayout pipeline_layout;
  97. VkPipeline pipeline;
  98. VkDescriptorUpdateTemplateKHR descriptor_update_template;
  99. ShaderInfo shader_info; // TODO use pointer ?
  100. };
  101. mutable std::vector<pipeline_cache_digest> cache_digests;
  102. mutable std::vector<pipeline_cache_artifact> cache_artifacts;
  103. mutable Mutex cache_lock;
  104. };
  105. PipelineCachePrivate::pipeline_cache_digest::pipeline_cache_digest(const uint32_t* spv_data, size_t spv_data_size, const std::vector<vk_specialization_type>& specializations,
  106. uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z)
  107. {
  108. spv_data_murmur3 = murmur3_32(spv_data, spv_data_size / 4);
  109. // encode opt
  110. opt_local_size_bits[0] = 0;
  111. // encode local_size
  112. opt_local_size_bits[1] = local_size_x;
  113. opt_local_size_bits[2] = local_size_y;
  114. opt_local_size_bits[3] = local_size_z;
  115. // encode specializations
  116. const int specialization_count = specializations.size();
  117. specializations_murmur3 = murmur3_32((const uint32_t*)specializations.data(), specialization_count);
  118. specializations_fnv1a = fnv1a_32((const uint8_t*)specializations.data(), specialization_count * sizeof(vk_specialization_type));
  119. }
  120. PipelineCachePrivate::pipeline_cache_digest::pipeline_cache_digest(int _shader_type_index, const Option& opt, const std::vector<vk_specialization_type>& specializations,
  121. uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z)
  122. {
  123. shader_type_index = _shader_type_index;
  124. // encode opt
  125. opt_local_size_bits[0] = opt.use_image_storage << 7
  126. | opt.use_fp16_packed << 6
  127. | opt.use_fp16_storage << 5
  128. | opt.use_fp16_arithmetic << 4
  129. | opt.use_int8_storage << 3
  130. | opt.use_int8_arithmetic << 2;
  131. // encode local_size
  132. opt_local_size_bits[1] = local_size_x;
  133. opt_local_size_bits[2] = local_size_y;
  134. opt_local_size_bits[3] = local_size_z;
  135. // encode specializations
  136. const int specialization_count = specializations.size();
  137. specializations_murmur3 = murmur3_32((const uint32_t*)specializations.data(), specialization_count);
  138. specializations_fnv1a = fnv1a_32((const uint8_t*)specializations.data(), specialization_count * sizeof(vk_specialization_type));
  139. }
  140. PipelineCache::PipelineCache(const VulkanDevice* _vkdev)
  141. : vkdev(_vkdev), d(new PipelineCachePrivate)
  142. {
  143. }
  144. PipelineCache::~PipelineCache()
  145. {
  146. clear();
  147. delete d;
  148. }
  149. PipelineCache::PipelineCache(const PipelineCache&)
  150. : d(0)
  151. {
  152. }
  153. PipelineCache& PipelineCache::operator=(const PipelineCache&)
  154. {
  155. return *this;
  156. }
  157. void PipelineCache::clear()
  158. {
  159. MutexLockGuard lock(d->cache_lock);
  160. for (size_t i = 0; i < d->cache_artifacts.size(); i++)
  161. {
  162. const PipelineCachePrivate::pipeline_cache_artifact& cc = d->cache_artifacts[i];
  163. if (vkdev->info.support_VK_KHR_descriptor_update_template())
  164. {
  165. if (cc.descriptor_update_template)
  166. {
  167. vkdev->vkDestroyDescriptorUpdateTemplateKHR(vkdev->vkdevice(), cc.descriptor_update_template, 0);
  168. }
  169. }
  170. if (cc.pipeline)
  171. {
  172. vkDestroyPipeline(vkdev->vkdevice(), cc.pipeline, 0);
  173. }
  174. if (cc.pipeline_layout)
  175. {
  176. vkDestroyPipelineLayout(vkdev->vkdevice(), cc.pipeline_layout, 0);
  177. }
  178. if (cc.descriptorset_layout)
  179. {
  180. vkDestroyDescriptorSetLayout(vkdev->vkdevice(), cc.descriptorset_layout, 0);
  181. }
  182. if (cc.shader_module)
  183. {
  184. vkDestroyShaderModule(vkdev->vkdevice(), cc.shader_module, 0);
  185. }
  186. }
  187. d->cache_digests.clear();
  188. d->cache_artifacts.clear();
  189. }
  190. int PipelineCache::get_pipeline(const uint32_t* spv_data, size_t spv_data_size, const std::vector<vk_specialization_type>& specializations,
  191. uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z,
  192. VkShaderModule* _shader_module,
  193. VkDescriptorSetLayout* descriptorset_layout,
  194. VkPipelineLayout* pipeline_layout,
  195. VkPipeline* pipeline,
  196. VkDescriptorUpdateTemplateKHR* descriptor_update_template,
  197. ShaderInfo& shader_info) const
  198. {
  199. MutexLockGuard lock(d->cache_lock);
  200. PipelineCachePrivate::pipeline_cache_digest key(spv_data, spv_data_size, specializations, local_size_x, local_size_y, local_size_z);
  201. if (!vkdev->info.bug_corrupted_online_pipeline_cache())
  202. {
  203. // find cache
  204. for (size_t i = 0; i < d->cache_digests.size(); i++)
  205. {
  206. if (d->cache_digests[i] != key)
  207. continue;
  208. // hit cache
  209. const PipelineCachePrivate::pipeline_cache_artifact& cc = d->cache_artifacts[i];
  210. *_shader_module = cc.shader_module;
  211. *descriptorset_layout = cc.descriptorset_layout;
  212. *pipeline_layout = cc.pipeline_layout;
  213. *pipeline = cc.pipeline;
  214. *descriptor_update_template = cc.descriptor_update_template;
  215. shader_info = cc.shader_info;
  216. // NCNN_LOGE("get_pipeline hit %d", last_digest_index);
  217. return 0;
  218. }
  219. }
  220. int ret = 0;
  221. ret = resolve_shader_info(spv_data, spv_data_size, shader_info);
  222. if (ret != 0)
  223. {
  224. NCNN_LOGE("resolve_shader_info failed %d", ret);
  225. return -1;
  226. }
  227. VkShaderModule shader_module = vkdev->compile_shader_module(spv_data, spv_data_size, local_size_x, local_size_y, local_size_z);
  228. if (!shader_module)
  229. {
  230. NCNN_LOGE("create_shader_module failed");
  231. return -1;
  232. }
  233. ret = new_pipeline(shader_module, shader_info, specializations, descriptorset_layout, pipeline_layout, pipeline, descriptor_update_template);
  234. if (ret != 0)
  235. {
  236. NCNN_LOGE("new_pipeline failed");
  237. vkDestroyShaderModule(vkdev->vkdevice(), shader_module, 0);
  238. return -1;
  239. }
  240. *_shader_module = shader_module;
  241. // save to cache
  242. {
  243. PipelineCachePrivate::pipeline_cache_artifact cc;
  244. cc.shader_module = *_shader_module;
  245. cc.descriptorset_layout = *descriptorset_layout;
  246. cc.pipeline_layout = *pipeline_layout;
  247. cc.pipeline = *pipeline;
  248. cc.descriptor_update_template = *descriptor_update_template;
  249. cc.shader_info = shader_info;
  250. d->cache_digests.push_back(key);
  251. d->cache_artifacts.push_back(cc);
  252. }
  253. // NCNN_LOGE("new_pipeline %d", last_digest_index);
  254. return 0;
  255. }
  256. int PipelineCache::get_pipeline(int shader_type_index, const Option& opt, const std::vector<vk_specialization_type>& specializations,
  257. uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z,
  258. VkShaderModule* _shader_module,
  259. VkDescriptorSetLayout* descriptorset_layout,
  260. VkPipelineLayout* pipeline_layout,
  261. VkPipeline* pipeline,
  262. VkDescriptorUpdateTemplateKHR* descriptor_update_template,
  263. ShaderInfo& shader_info) const
  264. {
  265. MutexLockGuard lock(d->cache_lock);
  266. PipelineCachePrivate::pipeline_cache_digest key(shader_type_index, opt, specializations, local_size_x, local_size_y, local_size_z);
  267. if (!vkdev->info.bug_corrupted_online_pipeline_cache())
  268. {
  269. // find cache
  270. for (size_t i = 0; i < d->cache_digests.size(); i++)
  271. {
  272. if (d->cache_digests[i] != key)
  273. continue;
  274. // hit cache
  275. const PipelineCachePrivate::pipeline_cache_artifact& cc = d->cache_artifacts[i];
  276. *_shader_module = cc.shader_module;
  277. *descriptorset_layout = cc.descriptorset_layout;
  278. *pipeline_layout = cc.pipeline_layout;
  279. *pipeline = cc.pipeline;
  280. *descriptor_update_template = cc.descriptor_update_template;
  281. shader_info = cc.shader_info;
  282. // NCNN_LOGE("get_pipeline hit %d", last_digest_index);
  283. return 0;
  284. }
  285. }
  286. int ret = 0;
  287. // create new pipeline
  288. VkShaderModule shader_module = 0;
  289. ret = create_shader_module(shader_type_index, opt, local_size_x, local_size_y, local_size_z, &shader_module, shader_info);
  290. if (ret != 0)
  291. {
  292. NCNN_LOGE("create_shader_module failed");
  293. return -1;
  294. }
  295. ret = new_pipeline(shader_module, shader_info, specializations, descriptorset_layout, pipeline_layout, pipeline, descriptor_update_template);
  296. if (ret != 0)
  297. {
  298. NCNN_LOGE("new_pipeline failed");
  299. vkDestroyShaderModule(vkdev->vkdevice(), shader_module, 0);
  300. return -1;
  301. }
  302. *_shader_module = shader_module;
  303. // save to cache
  304. {
  305. PipelineCachePrivate::pipeline_cache_artifact cc;
  306. cc.shader_module = *_shader_module;
  307. cc.descriptorset_layout = *descriptorset_layout;
  308. cc.pipeline_layout = *pipeline_layout;
  309. cc.pipeline = *pipeline;
  310. cc.descriptor_update_template = *descriptor_update_template;
  311. cc.shader_info = shader_info;
  312. d->cache_digests.push_back(key);
  313. d->cache_artifacts.push_back(cc);
  314. }
  315. // NCNN_LOGE("new_pipeline %d", last_digest_index);
  316. return 0;
  317. }
  318. int PipelineCache::create_shader_module(int shader_type_index, const Option& opt, uint32_t local_size_x, uint32_t local_size_y, uint32_t local_size_z,
  319. VkShaderModule* _shader_module, ShaderInfo& si) const
  320. {
  321. std::vector<uint32_t> spirv;
  322. int retc = compile_spirv_module(shader_type_index, opt, spirv);
  323. if (retc != 0)
  324. {
  325. NCNN_LOGE("compile_spirv_module failed %d", retc);
  326. return -1;
  327. }
  328. const uint32_t* spv_data = spirv.data();
  329. size_t spv_data_size = spirv.size() * 4;
  330. int ret = resolve_shader_info(spv_data, spv_data_size, si);
  331. if (ret != 0)
  332. {
  333. NCNN_LOGE("resolve_shader_info failed %d", ret);
  334. return -1;
  335. }
  336. VkShaderModule shader_module = vkdev->compile_shader_module(spv_data, spv_data_size, local_size_x, local_size_y, local_size_z);
  337. if (!shader_module)
  338. {
  339. NCNN_LOGE("create_shader_module failed");
  340. return -1;
  341. }
  342. *_shader_module = shader_module;
  343. return 0;
  344. }
  345. int PipelineCache::new_pipeline(VkShaderModule shader_module, const ShaderInfo& shader_info, const std::vector<vk_specialization_type>& specializations,
  346. VkDescriptorSetLayout* _descriptorset_layout,
  347. VkPipelineLayout* _pipeline_layout,
  348. VkPipeline* _pipeline,
  349. VkDescriptorUpdateTemplateKHR* _descriptor_update_template) const
  350. {
  351. int ret = 0;
  352. VkDescriptorSetLayout descriptorset_layout = 0;
  353. VkPipelineLayout pipeline_layout = 0;
  354. VkPipeline pipeline = 0;
  355. VkDescriptorUpdateTemplateKHR descriptor_update_template = 0;
  356. // create new pipeline
  357. if ((int)specializations.size() != shader_info.specialization_count)
  358. {
  359. NCNN_LOGE("pipeline specialization count mismatch, expect %d but got %d", shader_info.specialization_count, (int)specializations.size());
  360. goto ERROR_PipelineCache;
  361. }
  362. ret = vkdev->create_descriptorset_layout(shader_info.binding_count, shader_info.binding_types, &descriptorset_layout);
  363. if (ret != 0)
  364. goto ERROR_PipelineCache;
  365. ret = vkdev->create_pipeline_layout(shader_info.push_constant_count, descriptorset_layout, &pipeline_layout);
  366. if (ret != 0)
  367. goto ERROR_PipelineCache;
  368. ret = vkdev->create_pipeline(shader_module, pipeline_layout, specializations, &pipeline);
  369. if (ret != 0)
  370. goto ERROR_PipelineCache;
  371. if (vkdev->info.support_VK_KHR_descriptor_update_template())
  372. {
  373. ret = vkdev->create_descriptor_update_template(shader_info.binding_count, shader_info.binding_types, descriptorset_layout, pipeline_layout, &descriptor_update_template);
  374. if (ret != 0)
  375. goto ERROR_PipelineCache;
  376. }
  377. *_descriptorset_layout = descriptorset_layout;
  378. *_pipeline_layout = pipeline_layout;
  379. *_pipeline = pipeline;
  380. *_descriptor_update_template = descriptor_update_template;
  381. return 0;
  382. ERROR_PipelineCache:
  383. if (vkdev->info.support_VK_KHR_descriptor_update_template())
  384. {
  385. if (descriptor_update_template)
  386. {
  387. vkdev->vkDestroyDescriptorUpdateTemplateKHR(vkdev->vkdevice(), descriptor_update_template, 0);
  388. }
  389. }
  390. if (pipeline)
  391. {
  392. vkDestroyPipeline(vkdev->vkdevice(), pipeline, 0);
  393. }
  394. if (pipeline_layout)
  395. {
  396. vkDestroyPipelineLayout(vkdev->vkdevice(), pipeline_layout, 0);
  397. }
  398. if (descriptorset_layout)
  399. {
  400. vkDestroyDescriptorSetLayout(vkdev->vkdevice(), descriptorset_layout, 0);
  401. }
  402. return -1;
  403. }
  404. #endif // NCNN_VULKAN
  405. } // namespace ncnn