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 17 kB

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