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

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