fix: index CUDA sources/headers when generating .pyi stubs#361
Open
Osamaali313 wants to merge 1 commit into
Open
fix: index CUDA sources/headers when generating .pyi stubs#361Osamaali313 wants to merge 1 commit into
Osamaali313 wants to merge 1 commit into
Conversation
`build_cpp_function_index` (and `extract_m_def_statements`) only scanned `.cpp/.cc/.cxx/.c/.hpp/.h`, omitting `.cu` and `.cuh`. As a result, any pybind-bound function whose C++ definition/declaration lives in a CUDA source or header is not found, and the generated stub falls back to a generic `(*args, **kwargs) -> Any` (the code even logs "... not found in any .cpp file"). The omission is also internally inconsistent: `build_cpp_function_index` already classifies `.cuh` as a header via its `is_header` check, but never reads `.cuh` files, so that branch is unreachable. Add `.cu`/`.cuh` to both extension allowlists so CUDA-located bindings get a typed stub. No behavior change for the current tree (all 38 resolvable signatures still resolve; the only `.cu`, `csrc/indexing/main.cu`, registers no bindings); this matters as more kernels move into `.cu`/`.cuh`.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates the Python stub generator to also scan CUDA source/header files so that pybind-bound functions defined or declared in .cu/.cuh don’t fall back to overly-generic stubs.
Changes:
- Expand the C/C++ file extension sets to include
.cuand.cuhwhen indexing functions. - Expand the scan for
m.def(...)statements to include.cuand.cuh.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
157
to
+160
| Scan all c files under root_path and extract all m.def(...) statements. | ||
| """ | ||
| results = [] | ||
| extensions = {'.hpp', '.cpp', '.h', '.cc'} | ||
| extensions = {'.hpp', '.cpp', '.h', '.cc', '.cu', '.cuh'} |
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.
Problem
scripts/generate_pyi.pybuilds the type stubs (stubs/_C.pyi) consumed at build time bysetup.py(generate_pyi_file(name='_C', root='./csrc', ...)). It locates each bound function's C++ signature by scanning source files, but the extension allowlists omit CUDA sources/headers:So any
m.def-bound function whose C++ definition/declaration lives in a.cuor.cuhis not found, and its stub falls back to a genericdef name(*args, **kwargs) -> Any: ...(the code even logsWarning: C++ function "<name>" not found in any .cpp file).This is also internally inconsistent:
build_cpp_function_indexalready classifies.cuhas a header in itsis_headercheck —— but never reads
.cuhfiles, so that branch is unreachable.Fix
Add
.cuand.cuhto both allowlists.Validation
Ran the generator's parser over
./csrcbefore and after:.cufile,csrc/indexing/main.cu, registers no bindings, so output is byte-identical today..hppwhose C++ function is declared in a sibling.cuh:my_kernelnot indexed →Warning: ... not found in any .cpp file→ generic*argsstubtorch::Tensor my_kernel(torch::Tensor x)(typed stub).cu-defined binding →int cu_fn(int a, int b)The change is forward-looking: as more kernels/bindings move into
.cu/.cuh, their public stubs stay typed instead of silently degrading toAny.