-
Notifications
You must be signed in to change notification settings - Fork 4.3k
persistent vulkan pipeline cache #6221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
The binary size change of libncnn.so (bytes)
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #6221 +/- ##
==========================================
+ Coverage 95.72% 95.95% +0.23%
==========================================
Files 747 836 +89
Lines 249693 265392 +15699
==========================================
+ Hits 239014 254662 +15648
- Misses 10679 10730 +51 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
remove chrono
remove param file in cpp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements a persistent Vulkan pipeline cache feature to optimize pipeline compilation and loading times. The cache stores compiled SPIR-V modules and Vulkan pipeline cache data to disk, providing significant performance improvements for subsequent application runs.
- Added file-level pipeline cache persistence with platform validation
- Implemented SPIR-V module caching to avoid recompilation
- Added performance benchmarking test showing ~76% speedup improvement
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_pipecache.cpp | Comprehensive test suite for pipeline cache functionality and performance validation |
| tests/CMakeLists.txt | Added pipeline cache test to CMake build configuration |
| src/pipelinecache.h | Extended PipelineCache API with save/load methods for file and buffer operations |
| src/pipelinecache.cpp | Core implementation of cache serialization, platform validation, and SPIR-V caching |
| src/gpu.h | Added pipeline cache parameter to create_pipeline method |
| src/gpu.cpp | Updated pipeline creation to use Vulkan pipeline cache |
| return ret; | ||
| } | ||
|
|
||
| int PipelineCache::create_shader_module(int shader_type_index, const Option& opt, |
Copilot
AI
Aug 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goto statement on line 702 creates non-linear control flow that reduces code readability. Consider restructuring this logic using an if-else statement or extracting the cache lookup into a separate function.
| | opt.use_fp16_arithmetic << 4 | ||
| | opt.use_int8_storage << 3 | ||
| | opt.use_int8_arithmetic << 2; | ||
|
|
Copilot
AI
Aug 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bit manipulation for opt_bits uses magic numbers and could be error-prone. Consider defining named constants for bit positions or using an enum to make the code more maintainable and self-documenting.
| // Define bit positions for opt_bits | |
| enum OptBits { | |
| OPT_BIT_UNUSED = 7, | |
| OPT_BIT_FP16_PACKED = 6, | |
| OPT_BIT_FP16_STORAGE = 5, | |
| OPT_BIT_FP16_ARITHMETIC = 4, | |
| OPT_BIT_INT8_STORAGE = 3, | |
| OPT_BIT_INT8_ARITHMETIC = 2 | |
| }; | |
| uint32_t opt_bits = (0 << OPT_BIT_UNUSED) | |
| | (opt.use_fp16_packed << OPT_BIT_FP16_PACKED) | |
| | (opt.use_fp16_storage << OPT_BIT_FP16_STORAGE) | |
| | (opt.use_fp16_arithmetic << OPT_BIT_FP16_ARITHMETIC) | |
| | (opt.use_int8_storage << OPT_BIT_INT8_STORAGE) | |
| | (opt.use_int8_arithmetic << OPT_BIT_INT8_ARITHMETIC); |
| int retc = compile_spirv_module(shader_type_index, opt, spirv); | ||
| int retc = 0; | ||
|
|
||
| for (int i = 0; i < d->cache_spirv_module.size(); i++) |
Copilot
AI
Aug 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linear search through cache_spirv_module could become inefficient with large numbers of cached modules. Consider using a hash map or other efficient lookup structure for better performance.
| fprintf(stderr, "Test failed: extraction with cache failed.\n"); | ||
| return -1; | ||
| } | ||
| delete net_with_cache.opt.pipeline_cache; // clean up pipeline cache by hand |
Copilot
AI
Aug 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Manual memory management with delete is error-prone. Consider using smart pointers (std::unique_ptr) or RAII patterns to ensure automatic cleanup and prevent potential memory leaks.
| delete net_with_cache.opt.pipeline_cache; // clean up pipeline cache by hand | |
| // pipeline_cache_ptr will automatically clean up the pipeline cache here |
| } | ||
|
|
||
| // Validate platform information for compatibility | ||
| if (header.vendorID != vkdev->info.vendor_id() || header.deviceID != vkdev->info.device_id() || header.driverVersion != vkdev->info.driver_version() || memcmp(header.uuid, vkdev->info.pipeline_cache_uuid(), VK_UUID_SIZE) != 0) |
Copilot
AI
Aug 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This long conditional statement with multiple OR operations is difficult to read and maintain. Consider breaking it into separate boolean variables or a helper validation function for better readability.
| if (header.vendorID != vkdev->info.vendor_id() || header.deviceID != vkdev->info.device_id() || header.driverVersion != vkdev->info.driver_version() || memcmp(header.uuid, vkdev->info.pipeline_cache_uuid(), VK_UUID_SIZE) != 0) | |
| if (!is_cache_platform_compatible(header, vkdev)) |
|
感谢你的工作,请将你在实现中的笔记和心得,遇到的困难和解决方法等,记录成文章,发表在discussion分区,这将作为知识总结 https://github.com/Tencent/ncnn/discussions Thank you for your work. Please record your notes and experience in the implementation, difficulties encountered and solutions, etc. into an article and publish it in the discussion section. This will serve as a knowledge summary. https://github.com/Tencent/ncnn/discussions |
性能表现:
CPU: 13900HX
GPU: 对应核心显卡