Skip to content

Support static linking of libcudnn#182

Open
take-cheeze wants to merge 9 commits into
NVIDIA:developfrom
take-cheeze:static-v9
Open

Support static linking of libcudnn#182
take-cheeze wants to merge 9 commits into
NVIDIA:developfrom
take-cheeze:static-v9

Conversation

@take-cheeze
Copy link
Copy Markdown
Contributor

@take-cheeze take-cheeze commented Nov 18, 2025

Since most FindCUDNN.cmake's static feature seems to be broken from cudnn v8 I've like to fix it.
libcudnn_static.a isn't included at least from v9 and there is more tricks needed to link static cudnn documented in:
https://docs.nvidia.com/deeplearning/cudnn/installation/latest/build-run-cudnn.html#running-a-cudnn-dependent-program

In this PR, I'll add 2 hint flags:

I've tried

cmake -GNinja -Bbuild -S. -DCUDNN_STATIC_LINK=ON -DCUDNN_SKIP_PRECOMPILED_LINK=ON
cmake --build build
./build/bin/samples

working and result of ldd seems to be minimum as I expect:

$ ldd ./build/bin/samples 
        linux-vdso.so.1 (0x0000789a47e99000)
        libcudart.so.12 => /usr/local/cuda-12.8/targets/x86_64-linux/lib/libcudart.so.12 (0x00007899f2c00000)
        libcuda.so.1 => /lib/x86_64-linux-gnu/libcuda.so.1 (0x00007899ece00000)
        libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007899eca00000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007899f2f17000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007899f2ee9000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007899ec600000)
        /lib64/ld-linux-x86-64.so.2 (0x0000789a47e9b000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x0000789a47e67000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x0000789a47e62000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007899f2ee4000)

Summary by CodeRabbit

  • Refactor
    • Improved cuDNN version detection by reading header details with robust fallbacks for missing fields.
    • More reliable selection and linking of static vs. shared cuDNN libraries, including safer validation of include/library presence.
    • Enhanced wiring for static builds and cuDNN v9 to ensure correct linker flags and conditional inclusion of precompiled components.

@hwanseoc hwanseoc changed the base branch from main to develop May 21, 2026 00:05
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: a995657b-1638-42e1-a38b-048f560a5ca9

📥 Commits

Reviewing files that changed from the base of the PR and between 0011303 and a143d19.

📒 Files selected for processing (1)
  • cmake/cuDNN.cmake
🚧 Files skipped from review as they are similar to previous changes (1)
  • cmake/cuDNN.cmake

📝 Walkthrough

Walkthrough

CMake cuDNN module refactored to support static and shared builds: version detection reads cudnn_version.h with fallback to cudnn.h; library discovery uses static vs shared naming based on CUDNN_STATIC; package validation is adjusted; targets are wired differently per build mode; cuDNN v9 gets whole-archive and conditional engine/linker handling.

Changes

cuDNN CMake Configuration for Static and Shared Builds

Layer / File(s) Summary
Version detection from headers
cmake/cuDNN.cmake
Version extraction updated to read cudnn_version.h when available, with fallback parsing of cudnn.h. CUDNN_VERSION is composed from major/minor/patch, using ? when major is unavailable.
Library discovery and validation
cmake/cuDNN.cmake
find_cudnn_library() conditionally selects static (_static, .a) or shared (.so.<major>) naming based on CUDNN_STATIC. Prebuilt cuDNN discovery runs only for non-static builds. Package validation updated to require CUDNN_INCLUDE_DIR and (CUDNN_STATIC OR cudnn_LIBRARY).
Target creation and conditional wiring
cmake/cuDNN.cmake
When CUDNN_STATIC is enabled, a new INTERFACE IMPORTED target CUDNN::cudnn is created and linked to CUDNN::cudnn_all. For non-static builds, CUDNN::cudnn_all links directly to CUDNN::cudnn.
cuDNN v9 linking with whole-archive and engines
cmake/cuDNN.cmake
CUDNN::cudnn_all interface-links updated to add generator-expression whole-archive/no-whole-archive flags for static builds, conditional inclusion of CUDNN::cudnn_engines_precompiled via CUDNN_SKIP_PRECOMPILED_LINK, and static-vs-dynamic selection for CUDA (cublasLt, nvrtc) and ZLIB::ZLIB.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

A rabbit peers at headers, sniffing the trail,
Major, minor, patch — it follows the grail.
Static or shared, it hops through each rule,
Linking flags and targets, tidy and cool.
🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Support static linking of libcudnn' directly and clearly summarizes the main change in the PR, which adds CUDNN_STATIC and related flags to enable static linking of cuDNN.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@take-cheeze
Copy link
Copy Markdown
Contributor Author

@hwanseoc
Thank you for maintaining this project.
I'd like to use this in pytorch build. Is there any blocking issue for this PR?

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
cmake/cuDNN.cmake (1)

87-91: 💤 Low value

Use "CUDNN" as the package name for clearer error messages.

The first argument should be the package name, not "LIBRARY". Currently, if discovery fails, users will see "Could NOT find LIBRARY" instead of "Could NOT find CUDNN", making it harder to diagnose configuration issues.

 include (FindPackageHandleStandardArgs)
 find_package_handle_standard_args(
-    LIBRARY REQUIRED_VARS
+    CUDNN REQUIRED_VARS
     CUDNN_INCLUDE_DIR ${CUDNN_LIBRARY_VAR}
 )
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmake/cuDNN.cmake` around lines 87 - 91, The
find_package_handle_standard_args invocation uses "LIBRARY" as the package name
which yields misleading messages; change the first argument to "CUDNN" so
failure messages read "Could NOT find CUDNN". Edit the call to
find_package_handle_standard_args (the function named
find_package_handle_standard_args) and pass CUDNN as the package name while
keeping the same REQUIRED_VARS list (CUDNN_INCLUDE_DIR and ${CUDNN_LIBRARY_VAR})
so error reporting correctly references CUDNN.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cmake/cuDNN.cmake`:
- Around line 161-171: The generator expression is using the literal token
CUDNN_STATIC instead of the variable value, causing the static-only clauses to
always evaluate true; update each $<BOOL:CUDNN_STATIC> occurrence to use
variable expansion (e.g., $<BOOL:${CUDNN_STATIC}>), and likewise ensure the
static-libraries clause uses $<BOOL:${CUDNN_STATIC}> so the
--whole-archive/--no-whole-archive and CUDA::cublasLt_static /
CUDA::nvrtc_static / ZLIB::ZLIB selection are only applied when the CUDNN_STATIC
variable is truthy.
- Line 121: The current find_package(ZLIB) call can leave ZLIB unresolved but
later references ZLIB::ZLIB for static builds; update the CMake logic so ZLIB is
required when performing static builds: detect the static-build condition used
in this CMake script (e.g., BUILD_SHARED_LIBS OFF or your BUILD_STATIC option)
and call find_package(ZLIB REQUIRED) in that branch (otherwise keep the
non-REQUIRED call for optional builds), so that missing ZLIB fails at configure
time instead of producing a confusing "target ZLIB::ZLIB not found" at link
time.

---

Nitpick comments:
In `@cmake/cuDNN.cmake`:
- Around line 87-91: The find_package_handle_standard_args invocation uses
"LIBRARY" as the package name which yields misleading messages; change the first
argument to "CUDNN" so failure messages read "Could NOT find CUDNN". Edit the
call to find_package_handle_standard_args (the function named
find_package_handle_standard_args) and pass CUDNN as the package name while
keeping the same REQUIRED_VARS list (CUDNN_INCLUDE_DIR and ${CUDNN_LIBRARY_VAR})
so error reporting correctly references CUDNN.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 241e37ae-8491-4d78-843d-2058cc2a2a49

📥 Commits

Reviewing files that changed from the base of the PR and between f06a076 and a4cce65.

📒 Files selected for processing (1)
  • cmake/cuDNN.cmake

Comment thread cmake/cuDNN.cmake Outdated
Comment thread cmake/cuDNN.cmake Outdated
take-cheeze and others added 2 commits June 3, 2026 14:00
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@Anerudhan
Copy link
Copy Markdown
Collaborator

@cudnn-ci-bot run

@cudnn-ci-bot
Copy link
Copy Markdown

🚀 Running mirror pipeline

Branch: cudnn-gh/pr-182-a143d19
Pipeline: 53495806

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants