Repository navigation
Capture PTX that programs load at run time - #174
Merged
Merged
Conversation
PTX was read only from the executable's fatbin with cuobjdump. cuda-oxide (Rust) kernels hand their PTX to cuModuleLoadData at run time, so their sessions had SASS but no PTX. A driver-API handler now copies PTX text passed to cuModuleLoadData, cuModuleLoadDataEx, cuLibraryLoadData and cuLinkAddData (binaries are ignored), and the PTX flush emits its kernels through the same per-kernel parser. Kernels cuobjdump already emitted are not repeated, and cuobjdump's own output is unchanged.
- Keep a module's PTX only when its load returned CUDA_SUCCESS (API exit), so a rejected PTX cannot shadow a fallback with the same kernel names. - reset() clears the queued hashes, the emitted kernel names and the executable-dump flag, so a later session in the same process gets PTX for the modules it loads and runs cuobjdump again. - The one-time cuobjdump dump skips kernels the run-time path already emitted in this session; its own per-arch copies are kept. - PTX may open with a /* */ comment; one never closed is not PTX. - Note that loads by file name (cuModuleLoad, cuLinkAddFile, cuLibraryLoadFromFile) are not captured.
cuLibraryLoadData returns success before the driver compiles PTX: on driver 610.62 an unsupported .version loads with CUDA_SUCCESS and fails only at cuLibraryGetKernel / cuLibraryGetModule, with CUDA_MODULE_LOADING lazy, eager or unset. Its success therefore does not prove the PTX is the code that runs, and the API mostly carries the CUDA runtime's own fatbins. cuModuleLoadData, cuModuleLoadDataEx and cuLinkAddData reject the same PTX with CUDA_ERROR_UNSUPPORTED_PTX_VERSION before they return in all three modes, so their success is final and they stay captured. cuda-oxide, NVRTC users and Numba hand PTX to those.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
PTX was read only from the executable's fatbin with
cuobjdump -ptx(DictionaryManager::flushPtx). cuda-oxide (Rust) kernels carry their PTX in an.oxartsection and hand the text tocuModuleLoadDataat run time (cuda-core 0.3.1load_module_from_image), socuobjdumpfinds nothing. Rust Performance Lab sessions in dev show SASS but every kernel's PTX request returns 204, while C++ sessions of the same labs have PTX (sm_89).What
ModuleLoadPtxHandlercopies the PTX passed tocuModuleLoadData,cuModuleLoadDataEx, andcuLinkAddData_v2withCU_JIT_INPUT_PTX. It copies at API exit and only when the call returnedCUDA_SUCCESS, so a rejected PTX cannot shadow a fallback with the same kernel names. It runs only where cubins are captured (NeedsCubinCapture).cuLibraryLoadData, which returns success before the driver compiles PTX (see Testing) and mostly carries the CUDA runtime's own fatbins. Loads by file name (cuModuleLoad,cuLinkAddFile,cuLibraryLoadFromFile) are not captured either. cuda-oxide, NVRTC users and Numba pass the image to the captured calls.DictionaryManager::enqueueLoadedPtxkeeps an image only when it is PTX text: optional whitespace,//lines and/* */comments, then.version. ELF cubins and fatbins are rejected at their first byte. Reads stop at the NUL or the size limit. Each module is capped at 64 MiB, the pending total at 256 MiB, and an identical module is kept once.emitPtxText. A kernel already emitted is not repeated. The one-timecuobjdumpdump skips kernels the run-time path already emitted in the session; otherwise its output is unchanged, per-arch copies included.flushDisassemblyalso drains loaded PTX when no cubins are pending, so a module loaded after the last flush is not lost.reset(), which runs at every session start, now clears the PTX state too: the queued hashes, the emitted kernel names and the executable-dump flag. A later session in the same process gets PTX for the modules it loads, and runscuobjdumpagain (before, a second session never ran it).No backend change is needed: PTX joins kernels and SASS by md5 of the function name, and Rust kernel names in the PTX (
layernorm_rows) match their SASS names.Testing
LoadedPtxTest(3 tests):ptx_disassemblyper kernel, with its arch and module header. The same module loaded again adds nothing.reset(), the same module is emitted again for the new session. This test fails without the fix.run_tests.py --cpp-onlyon Windows, CUDA 13.3: 644 tests passed.CUDA_MODULE_LOADINGlazy, eager and unset, each case in its own process:.versionfails withCUDA_ERROR_UNSUPPORTED_PTX_VERSIONat the call forcuModuleLoadData,cuModuleLoadDataExandcuLinkAddData, in every mode.cuLibraryLoadDatareturnsCUDA_SUCCESSfor the same PTX and fails only atcuLibraryGetKernel/cuLibraryGetModule.