Repository navigation
Fix runtime lib loading logic - #2297
Conversation
There was a problem hiding this comment.
Greptile Overview
Greptile Summary
This review covers only the changes made since the last review, not the entire PR. New critical issues identified: The refactored _load_cuda_library function has a duplicate @functools.lru_cache decorator on line 320 that will cause a syntax error. Additionally, lines 334–336 now return only the first handle from multi-handle returns (previously _load_nvidia_cuda_library returned all handles), breaking libraries that may provide multiple versions. Most critically, lines356–357 directly assign the tuple (bool, list) from _load_cuda_library_from_python to _CUBLAS_LIB_CTYPES and _CUDART_LIB_CTYPES instead of unpacking the handle, which will cause runtime failures when these variables are later used as ctypes handles. The previously reported if-elif-else logic error on lines 269–274 remains unfixed.
Important Files Changed
| Filename | Score | Overview |
|---|---|---|
| transformer_engine/common/init.py | 1/5 | Refactored library loading to prioritize system over Python packages, but introduces duplicate decorator, loses multi-handle semantics, and incorrectly assigns tuple return values to ctypes handle globals |
Confidence score: 0/5
- This PR will cause immediate failures in production due to multiple syntax and logic errors
- Score reflects four distinct critical issues: duplicate decorator causing syntax error, incorrect return-value unpacking causing type errors, lost multi-handle semantics breaking library loading, and unfixed if-elif-else fallthrough from previous review
- Pay close attention to lines 320(duplicate decorator), 334–336 (lost multi-handle return), 356–357 (incorrect tuple assignment), and 269–274 (unfixed if-elif-else logic)
1 file reviewed, 3 comments
There was a problem hiding this comment.
Greptile Overview
Greptile Summary
This review covers only the changes made since the last review, not the entire PR. The developer has updated transformer_engine/common/__init__.py to address multiple critical bugs identified in previous reviews. Specifically, the changes fix: (1) the if-elif-else fallthrough issue by converting line 271 to elif to prevent unintended overwrites, (2) the duplicate decorator on line 360 which is removed, (3) the cublas/cudart loading logic which is now wrapped in _load_cuda_library_from_system to properly search system paths and raise errors on failure rather than silently assigning (False, []), and (4) the single-handle return in _load_cuda_library which now returns all matched handles. The glob pattern on lines 270/272/274 still uses a wildcard (lib*{ext}.*[0-9]) that matches all versioned libraries in the directory instead of filtering specifically by the requested lib_name, which could lead to incorrect library selection when multiple CUDA libraries coexist, but this is a pre-existing pattern carried over from the original code and may be intentional to capture multi-part library names (e.g., libcudart.so.12.0).
Important Files Changed
| Filename | Score | Overview |
|---|---|---|
| transformer_engine/common/init.py | 4/5 | Fixed if-elif fallthrough, duplicate decorator, and cublas/cudart loading; all handles now returned; glob pattern still non-specific but matches prior behavior |
Confidence score: 4/5
- This PR is now safe to merge with minimal risk after addressing the previous critical issues
- Score reflects that all previously identified logic errors (fallthrough, duplicate decorator, silent failures, single-handle return) have been corrected; remaining uncertainty is the wildcard glob pattern which may be by design and does not introduce new risk
- No files require additional attention; all critical bugs have been resolved
1 file reviewed, 2 comments
| libs = glob.glob(f"{path}/**/lib{lib_name}{_get_sys_extension()}*", recursive=True) | ||
| libs.sort(reverse=True, key=os.path.basename) | ||
| if libs: | ||
| return True, ctypes.CDLL(libs[0], mode=ctypes.RTLD_GLOBAL) |
There was a problem hiding this comment.
style: glob uses lib{lib_name}* but does not filter out stub libraries; for nvrtc this previously filtered stubs explicitly (old code line ~336). Consider adding not ("stub" in x) filter.
|
/te-ci |
Greptile OverviewGreptile SummaryRefactors library loading logic to consolidate duplicate code and prioritize system installations over PyPI packages, addressing version mismatch bugs. Key changes:
Critical issues found:
Confidence Score: 1/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant Main as Main Code
participant LoadLib as _load_cuda_library()
participant LoadSys as _load_cuda_library_from_system()
participant LoadPy as _load_cuda_library_from_python()
participant FS as File System
Note over Main: Load cudnn, nvrtc, curand
Main->>LoadLib: _load_cuda_library("cudnn")
LoadLib->>LoadSys: Check system paths
LoadSys->>FS: Check CUDNN_HOME/PATH
LoadSys->>FS: Check CUDA_HOME/PATH
LoadSys->>FS: Check LD_LIBRARY_PATH
alt Found in system
LoadSys-->>LoadLib: (True, handle)
LoadLib-->>Main: (True, handle)
else Not found in system
LoadLib->>LoadPy: Check python packages
LoadPy->>FS: Check nvidia/cu13/lib_name
LoadPy->>FS: Check nvidia/cu13/lib
LoadPy->>FS: Check nvidia/lib_name
LoadPy->>FS: Check nvidia/cuda_lib_name
alt Found in python
LoadPy-->>LoadLib: (True, [handles])
LoadLib-->>Main: (False, [handles])
else Not found
LoadLib-->>Main: RuntimeError
end
end
Note over Main: Check if system libs loaded
alt system_nvrtc AND system_curand
Note over Main: All system - done
else NOT system_nvrtc AND NOT system_curand
Note over Main: Load cublas, cudart, cudnn from python
Main->>LoadPy: _load_cuda_library_from_python()
LoadPy-->>Main: (bool, [handles])
end
|
There was a problem hiding this comment.
Additional Comments (1)
-
transformer_engine/pytorch/1.txt, line 1-13 (link)style: build log accidentally committed - should be removed from repository
4 files reviewed, 4 comments
|
/te-ci |
| if not system_nvrtc and not system_curand: | ||
| _CUBLAS_LIB_CTYPES = _load_cuda_library_from_python("cublas", strict=True) | ||
| _CUDART_LIB_CTYPES = _load_cuda_library_from_python("cudart", strict=True) | ||
| _CUDNN_LIB_CTYPES = _load_cuda_library_from_python("cudnn", strict=True) |
There was a problem hiding this comment.
logic: logic error: condition requires BOTH nvrtc AND curand to come from python before loading cublas/cudart/cudnn from python. If only one came from system, the block won't execute, creating mixed system/python library state. Line 374 also re-assigns _CUDNN_LIB_CTYPES, overwriting the system version loaded on line 367. Should be or not and, or remove the cudnn re-assignment.
Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
347eed8 to
17e0bd0
Compare
There was a problem hiding this comment.
Additional Comments (3)
-
transformer_engine/common/__init__.py, line 321 (link)logic: missing
libnvrtc-builtinsfilter - old code filtered bothstubandlibnvrtc-builtins, but new code only filtersstub -
transformer_engine/common/__init__.py, line 377 (link)logic: incorrect library name: should be
cuda_runtimenotcudart- old code used_load_nvidia_cuda_library("cuda_runtime")on line 385 -
transformer_engine/common/__init__.py, line 374-378 (link)logic:
_CUBLAS_LIB_CTYPESand_CUDART_LIB_CTYPESundefined when CUDA toolkit present - ifload_libs_for_no_ctkis False (system has toolkit), these variables are never assigned, causingNameErrorat runtime
3 files reviewed, 3 comments
|
/te-ci |
|
/te-ci |
There was a problem hiding this comment.
Additional Comments (5)
-
transformer_engine/common/__init__.py, line 343-350 (link)logic: return type mismatch:
_load_cuda_library_from_systemreturns(bool, handle)where handle is a single ctypes object (line 324), but_load_cuda_library_from_pythonreturns(bool, list)where the second element is a list of handles (line 297). When system lookup fails and python lookup succeeds, this returns a list instead of a single handle, causing inconsistent return types. -
transformer_engine/common/__init__.py, line 376-378 (link)logic: these return lists (line 297 shows
_load_cuda_library_from_pythonreturns(bool, list)), but the variable names suggest single handles. This will cause runtime errors when these are used as ctypes objects later. -
transformer_engine/common/__init__.py, line 374 (link)logic: condition is backwards: should be
ornotand. Whennvrtccomes from system (system_nvrtc=True) butcurandcomes from python (system_curand=False), you want to load the remaining libs from python to keep consistency. Theandrequires BOTH to be from python, which misses the mixed case. -
transformer_engine/common/__init__.py, line 273 (link)logic: glob pattern
lib*{ext}.*[0-9]matches ALL .so files, not justlib_name. Should belib{lib_name}*{ext}.*[0-9]to filter correctly. Same issue on lines 281 and 285. -
transformer_engine/common/__init__.py, line 378 (link)logic: this overwrites
_CUDNN_LIB_CTYPESwhich was already loaded from system on line 367. If the condition on line 374 executes, you're replacing the system cudnn with python cudnn. Remove this line - cudnn is already loaded.
3 files reviewed, 5 comments
Fixes to runtime loading logic and add missing deps Signed-off-by: Kirthi Shankar Sivamani <ksivamani@nvidia.com>
Description
This is a small refactor of library loading logic during runtime to be more consistent and avoid duplication. The main point is to check python packages as a last ditch attempt to find the library and prioritize system installations.
Fixes a bug where the incorrect shared object is loaded (with mismatching versions) due to presence of PyPI packages that are installed by
pytorch/jaxetc.It also allows users to install and import TE wheels in an environment without the cuda toolkit.
Fixes #2373
Type of change
Changes
curand,cudnnetc.LD_LIBRARY_PATHbefore checking python packages.ldconfigas redundant and brute force.Checklist: