Skip to content

Conversation

@CLV-Iclucia
Copy link

Add Persistent Vulkan Pipeline Cache and Persistent Shader SPIR-V Cache

Changes

  • Implemented persistent save/load APIs for PipelineCache, enabling serialization of VkPipelineCache data to disk and reuse across application runs.
  • Introduced strict validation for cache consistency, including:
    • data hash verification
    • GPU info
    • ABI (32 bit or 64 bit)
  • Developed a separate on-disk cache mechanism for compiled shader SPIR-V binaries to avoid redundant shader compilation and improve pipeline build efficiency across runs.

Design Considerations

  • Save VkPipeline cache to the disk to accelerate pipeline creation.
  • Most of the time, compiling source code to spirv code is the bottleneck so I also designed a persistent spirv code cache to accelerate compiling across runs.

Testing

  • Verified robustness for pipeline cache in various situations like file corruption and multi-threading.
  • Passed all the current tests on Windows.

Performance

In a single pipeline creation test, the time taken for creating a pipeline is reduced from 90ms to 0.4ms using the two caches across runs (mocked by creating and destroying GPU repeatedly) on my PC and this is mainly contributed by spirv code cache.

The CPU is AMD Ryzen 7 5800H and the GPU is Nvidia RTX 3060.

pipeline cache test creation time (without cache): 90.87 ms
pipeline cache test creation time (with cache): 0.37 ms

Impact

  • Now ncnn users might need to manually load/save the pipeline cache, or customize spirv code cache saving directories.
  • Updating ncnn might need to change the ncnn_version in spirv cache header.

Questions for Review

1. Cross-platform file operations

The project currently lacks a unified cross-platform file API due to using C++11 (like renaming file). I implemented platform-specific file handling within the relevant pipelinecache.cpp files.
Would it be better to extract these into a cross-platform file module? Any recommendations or existing patterns in the project?

2. SPIR-V cache invalidation strategy

Multiple factors affect the generated SPIR-V, such as shader source, glslang version, and how ncnn handles device extensions and options.
I combined these into a single ncnn_version field in the spirv code cache header to invalidate caches on changes.
Is this approach sufficient? Are there better versioning or change-tracking strategies you recommend?

3. Testing and API exposure for SPIR-V cache

Exposing many interfaces to manipulate the SPIR-V cache risks complexity, yet hiding them makes thorough testing difficult.
What is the best practice here? Controlled internal APIs for testing, or relying on integration tests?

4. Security of SPIR-V cache files

If a cached SPIR-V file is maliciously altered (content and hash), there's a risk of running compromised shaders when loading the cache.
Do you have suggestions on mitigating this risk?

@tencent-adm
Copy link
Member

tencent-adm commented Aug 11, 2025

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link

github-actions bot commented Aug 11, 2025

The binary size change of libncnn.so (bytes)

architecture base size pr size difference
x86_64 15155464 15148064 -7400 😘
armhf 6182208 6170080 -12128 😘
aarch64 9520608 9520968 +360 ⚠️

@nihui
Copy link
Member

nihui commented Aug 21, 2025

感谢你的工作,请将你在实现中的笔记和心得,遇到的困难和解决方法等,记录成文章,发表在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

@codecov-commenter
Copy link

codecov-commenter commented Sep 10, 2025

Codecov Report

❌ Patch coverage is 69.25373% with 103 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.85%. Comparing base (a514cf5) to head (34c9d85).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
src/pipelinecache.cpp 68.28% 98 Missing ⚠️
src/gpu.cpp 80.76% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6248      +/-   ##
==========================================
- Coverage   95.89%   95.85%   -0.04%     
==========================================
  Files         837      836       -1     
  Lines      264994   265053      +59     
==========================================
- Hits       254105   254076      -29     
- Misses      10889    10977      +88     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nihui nihui closed this Sep 11, 2025
@nihui nihui reopened this Sep 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants