Skip to content

pythonPackages.torch: cudnn runtime compilation fails - libnvrtc not in runpath #461334

Closed

Description

@ethernet8023

Nixpkgs version

  • Unstable (25.11) (c5ae371f1a6a7fd27823bc500d9390b38c05fa55)

Describe the bug

using pythonPackages.torch, cudnn can't load libnvrtc. this is supposed to work via this injected runpath

extraRunpaths = lib.optionals cudaSupport [ "${lib.getLib cudaPackages.cuda_nvrtc}/lib" ];

but this doesn't work with the source torch package. this does work perfectly with torch-bin.
this is related to #296179

Steps to reproduce

On a machine with an H100 (or similar GPU),
in a nix-shell --arg config '{ cudaSupport = true; allowUnfree = true; }' -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/c5ae371f1a6a7fd27823bc500d9390b38c05fa55.tar.gz -p 'python3.withPackages (ps: [ps.torch])'
run python test.py, where test.py is the below script:

import torch
import os
import ctypes

print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"cuDNN version: {torch.backends.cudnn.version()}")

device = torch.device("cuda:0")

# seems to trigger runtime compilation
batch, heads, seq_len, head_dim = 2, 32, 4096, 128

q = torch.randn(batch, heads, seq_len, head_dim, device=device, dtype=torch.bfloat16, requires_grad=True)
k = torch.randn(batch, heads, seq_len, head_dim, device=device, dtype=torch.bfloat16, requires_grad=True)
v = torch.randn(batch, heads, seq_len, head_dim, device=device, dtype=torch.bfloat16, requires_grad=True)

# causal mask + large seq len on an H100 triggers cudnn runtime compilation
try:
    output = torch.nn.functional.scaled_dot_product_attention(
        q, k, v,
        attn_mask=None,
        dropout_p=0.0,
        is_causal=True,  # triggers torch to compile a fused kernel
        scale=None
    )
    loss = output.sum()
    loss.backward()
    print("compiled fused kernel!")
except RuntimeError as e:
    print(f"failed to do sdpa: {e}")
    if "libnvrtc" in str(e):
        print("\n yayyyy we  reproduced the issue!")

Expected behaviour

we see "compiled fused kernel!" as in torch-bin

Screenshots

No response

Relevant log output

torch-bin

via nix-shell -p 'python3.withPackages (ps: [ps.torch-bin])' -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/c5ae371f1a6a7fd27823bc500d9390b38c05fa55.tar.gz --arg config '{ cudaSupport = true; allowUnfree = true; }'

(nix:nix-shell-env) ari@dell-h100-nj4-2:~/src/psyche$ python test.py
PyTorch version: 2.9.0+cu128
CUDA available: True
cuDNN version: 91300

compiled fused kernel!

torch

via nix-shell -p 'python3.withPackages (ps: [ps.torch])' -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/c5ae371f1a6a7fd27823bc500d9390b38c05fa55.tar.gz --arg config '{ cudaSupport = true; allowUnfree = true; }'

(nix:nix-shell-env) ari@dell-h100-nj4-2:~/src/psyche$ python test.py
PyTorch version: 2.9.0
CUDA available: True
cuDNN version: 91300

failed to do sdpa: cuDNN Frontend error: No valid engine configs for Matmul_MUL_GEN_INDEX_GEN_INDEX_CMP_GE_BINARY_SELECT_Reduction_SUB_EXP_Reduction_LOG_ADD_DIV_Matmul_
{"engineId":2,"smVersion":900,"knobChoices":{"CUDNN_KNOB_TYPE_KERNEL_CFG":7}}
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: Could not open libnvrtc at: this->libnvrtc == nullptr
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: compiler.load()
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: rtk(kernelNumRunning)->loadDLL()
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: status == CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: ptr->isSupported()
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: finalize_internal()

{"engineId":1,"smVersion":900,"knobChoices":{"CUDNN_KNOB_TYPE_KERNEL_CFG":3}}
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: Could not open libnvrtc at: this->libnvrtc == nullptr
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: compiler.load()
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: rtk(kernelNumRunning)->loadDLL()
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: status == CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: ptr->isSupported()
Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: finalize_internal()



 yayyyy we  reproduced the issue!

Additional context

No response

System metadata

  • system: "x86_64-linux"
  • host os: Linux 5.15.0-119-generic, Ubuntu, 22.04.4 LTS (Jammy Jellyfish), nobuild
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.24.6
  • nixpkgs: /nix/store/yff8cp4mrjrf71i1g9llxqpip8a6sfyj-source

Notify maintainers

@GaetanLepage @teh @thoughtpolice @tscholak

Note for maintainers: Please tag this issue in your pull request description. (i.e. Resolves #ISSUE.)

I assert that this issue is relevant for Nixpkgs

Is this issue important to you?

Add a 👍 reaction to issues you find important.

Activity

  1. added
    0.kind: bug
    on Nov 13, 2025
  2. added
    6.topic: cuda
    on Nov 13, 2025
  3. ethernet8023 commented on Nov 24, 2025

    @ethernet8023
    ContributorAuthor

    This is very related to, if not the exact same issue, as #296179
    #315694 was designed to fix it, but somehow seems like it didn't.

  4. SomeoneSerge commented on Nov 24, 2025

    @SomeoneSerge
    Contributor

    Warning: CUDNN_STATUS_NOT_SUPPORTED_RUNTIME_PREREQUISITE_MISSING; Reason: Could not open libnvrtc at: this->libnvrtc == nullptr

    Could it be it's libcudnn trying to load libnvrtc, and doing that before torch (which does know the location) has

    Could you re-run your test with LD_DEBUG=libs, and locate the "using DT_RUNPATH from ..." bit relating to the libnvrtc look-up? Ty

  5. ethernet8023 commented on Dec 10, 2025

    @ethernet8023
    ContributorAuthor

    Could you re-run your test with LD_DEBUG=libs, and locate the "using DT_RUNPATH from ..." bit relating to the libnvrtc look-up? Ty

    I don't see any "using DT_RUNPATH from ..." in my output.

    FWIW, this is running under nix-gl-host on ubuntu.
    Here's all the logs from the torch package: https://gist.github.com/arilotter/0993568341df8df1cc9aa422b6735cfd

    What seems to be of note is

       3043400:     calling init: /nix/store/sqdpvh996bxbpg93pjp4zbaw3a8j6lza-cuda12.8-cudnn-9.13.0.50-lib/lib/libcudnn_engines_precompiled.so.9.13.0
       3043400:
       3043400:     python: error: symbol lookup error: undefined symbol: InitializeInjectionCaskEnhanced (fatal)
    

    surprisingly though, the torch-bin package does have the same error.
    here's the output from the working torch-bin one: https://gist.github.com/arilotter/e10d3224745ebcc6ed8ceb27e7111b4d

  6. SomeoneSerge commented on Dec 10, 2025

    @SomeoneSerge
    Contributor
    3043400:	find library=libnvrtc.so [0]; searching
    ...
    3043400:	 search path=/nix/store/rdj62gf4zjpwj5ffxdq5ql0gk3piq09w-zlib-1.3.1/lib:/nix/store/fly29nz0gwiww48y76myy0mimv12wz5w-gcc-14.3.0-lib/lib:/nix/store/l1q4cdis5blgij8ac1cwwmxgm4kl62ab-gcc-14.3.0-libgcc/lib:/nix/store/sqdpvh996bxbpg93pjp4zbaw3a8j6lza-cuda12.8-cudnn-9.13.0.50-lib/lib		(RUNPATH from file /nix/store/40cy5bxldrc86hr493j1na3vpg5qyj44-python3-3.13.9-env/lib/python3.13/site-packages/torch/lib/libtorch_global_deps.so)
    

    The bit I was looking for is RUNPATH from file /nix/store/.../lib/python3.13/site-packages/torch/lib/libtorch_global_deps.so. This should have contained the nvrtc directory and it doesn't

  7. ethernet8023 commented on Dec 15, 2025

    @ethernet8023
    ContributorAuthor

    my understanding is that dlopen does not use the runpath of the caller library. so, when libcudnn.so calls dlopen("libnvrtc.so.12"),
    it does not look at the runpath of libtorch_cuda.so nor libcudnn.so - so adding nvrtc to torch's runpath is a no-op and it only would work accidentally.

    doing a shell with
    export LD_LIBRARY_PATH=${pkgs.lib.getLib pkgs.cudaPackages.cuda_nvrtc}/lib:$LD_LIBRARY_PATH would work because dlopen always checks LD_LIBRARY_PATH.

    so, when python imports torch,

    python
      └── load libtorch_cuda.so
            └── load libcudnn.so
                  └── dlopen("libnvrtc.so.12")
    

    what we'd have to do somehow is modify python's runpath, not torch's.
    i'm not sure if there's a nixpkgs mechanism to patch runpaths for dlopens like this - such that if we build a python.withPackages that depends on a torch we'd automatically propagate the runpath to the python binary? it seems.. very sketchy.

  8. SomeoneSerge commented on Dec 16, 2025

    @SomeoneSerge
    Contributor

    it does not look at the runpath of libtorch_cuda.so nor libcudnn.so - so adding nvrtc to torch's runpath is a no-op and it only would work accidentally.

    No, cf. "RUNPATH from ..."

  9. moved this from New to ✅ Done in CUDA Teamon Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bug6.topic: cuda

    Projects

    • Status
      ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions