Skip to content

fix(#222): load all PTX bundles merged for binaries with generic kernels - #247

Merged
nihalpasham merged 4 commits into
mainfrom
fix/issue-222-cross-crate-generic
Jun 18, 2026
Merged

nihalpasham merged 4 commits into
mainfrom
fix/issue-222-cross-crate-generic

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

What

Closes #222.

Generic kernels (e.g. fn add<T>(...)) are monomorphized in the
consuming crate, not the crate that defines them. Their PTX lands in
the consuming crate's artifact bundle. Calling load_embedded_module
with the defining crate's name failed at runtime with:

DriverError(500, "named symbol not found")

Fix

cuda-host: add load_all_ptx_bundles_merged. It iterates every
PTX bundle embedded in the current binary, strips the per-file header
directives (.version, .target, .address_size) from all but the
first bundle, and concatenates the bodies into one PTX string for the
CUDA driver. All kernel symbols are then visible regardless of which
bundle they were compiled into.

cuda-macros: during #[kernel] macro expansion, check whether any
kernel in the crate is generic. If so, emit load_all_ptx_bundles_merged
in the generated load_named function instead of load_embedded_module.
Non-generic crates continue to use the existing named-bundle path.

Test plan

  • Binary that calls a generic kernel defined in a separate crate loads without DriverError(500)
  • cargo test -p cuda-host
  • smoketest passes (non-generic examples unchanged)

…ernels

Generic kernels are monomorphized in the consuming crate, so their PTX
ends up in that crate's artifact bundle rather than the defining crate's.
Calling `load_embedded_module` with the defining crate's name failed with
`named symbol not found` because the kernel symbol lived in a different bundle.

Add `load_all_ptx_bundles_merged` to cuda-host: it iterates every PTX
bundle in the current binary, strips duplicate header directives
(`.version`, `.target`, `.address_size`) from all but the first bundle,
and concatenates the bodies into one PTX string for the CUDA driver.
The generated `load_named` function in cuda-macros uses this path
automatically when any `#[kernel]` in the crate is generic.
@nihalpasham nihalpasham added bug Something isn't working host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) labels Jun 18, 2026
Exercises the embedded bundle loading path that issue #222 fixed.

The existing cross_crate_kernel example always passed because it loads
PTX from a file on disk (load_module_from_file), which bypasses the
embedded bundle machinery entirely.

This example calls kernel_lib::kernels::load(&ctx) directly, which uses
the macro-generated embedded loader. Because the module contains a
generic kernel (scale<T>), the macro emits load_all_ptx_bundles_merged
instead of load_embedded_module. Without the fix, calling load() would
panic with "named symbol not found" because the monomorphized PTX for
scale::<f32> lives in the binary crate's bundle, not the library's.
@nihalpasham
nihalpasham merged commit 19a0aed into main Jun 18, 2026
24 checks passed
@nihalpasham
nihalpasham deleted the fix/issue-222-cross-crate-generic branch June 19, 2026 11:30
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
…VIDIA#247)

The Sphinx author field listed only the first-draft author and is not
rendered anywhere visible. List both authors there, add an About This
Book page under Reference that credits the first draft explicitly, and
extend the copyright year.

Signed-off-by: Melih Elibol <[email protected]>
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
…NVIDIA#225, NVIDIA#185) (NVIDIA#256)

Three squash merges landed from branches that had resolved a merge against
main by keeping pre-merge copies of files, so each one quietly reverted an
earlier, unrelated merge:

- NVIDIA#239 (2482cb9) reverted NVIDIA#240 (1216aef): the const raw-pointer surface
  (as_ptr, program_id/num_programs, the cast_* and get_tensor_base helpers,
  unsafe on load_ptr_tko/store_ptr_tko), the compiler's const-pointer
  typing, the two GPU test files and the matching book and tutorial text.
- NVIDIA#247 (5f5f4c5) reverted NVIDIA#225 (a2843e7): integer division and remainder
  serialization (rounding<zero> and inferred signedness on divi/remi) and
  its test.
- NVIDIA#187 (35a9cc2) reverted NVIDIA#185 (cbf52c0): the flake.nix toolkit-assembly
  hardening and host-arch driver directory derivation.

This re-applies all three verbatim as cherry-picks. The only overlapping
file, cutile-book/guide/performance.md, merges cleanly with NVIDIA#239's own
edits. A per-file scan of the last 60 merges finds no other reversal still
missing at HEAD: NVIDIA#249's removal of the About page was deliberate, and the
two setup-python flips in .github/workflows/pages.yml were re-applied by
the later dependabot bump.

Verified: cargo fmt, cargo build, and the restored GPU tests
(const_pointers, program_id) plus integer_ops, memory_and_atomic_ops and
optimization_hints pass on an RTX 5090 with CUDA 13.3.

Signed-off-by: Melih Elibol <[email protected]>
Co-authored-by: Gabriel Wu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic DriverError(500, "named symbol not found") when calling generic kernels from a separate crate

1 participant