cuda-extensions
Safely diagnose and plan use of this repository's legacy 2D/3D NMS and RoIAlign CUDA extensions without compiling or treating precompiled binaries as portable.
- License 许可
- Apache-2.0
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- cuda-extensions
- license
- Apache 2.0
- metadata
- { "disco-role": "operating" }
- description
- Safely diagnose and plan use of this repository's legacy 2D/3D NMS and RoIAlign CUDA extensions without compiling or treating precompiled binaries as portable.
- disable-model-invocation
- true
02 资源文件 4
- 参考文档references/compatibility.md6.6 KB
- 参考文档references/custom-ops.md7.7 KB
- 参考文档references/troubleshooting.md6.2 KB
- 脚本scripts/check_legacy_cuda_compat.py2.3 KB
03 SKILL.md 正文
Legacy CUDA extensions
Use this sub-skill when a model import, proposal stage, RoIAlign call, or
installation question reaches cuda_functions/. It is an operating guide for
compatibility diagnosis and source-backed interface selection, not a build
recipe to execute automatically.
Non-negotiable boundaries
- Do not run
build.pyfrom this skill. It invokes the removedtorch.utils.ffi.create_extensionAPI and can write native build output.scripts/check_legacy_cuda_compat.pyis the read-only diagnostic entry point. - Do not compile the checked-in CUDA sources as part of normal diagnosis.
The host has no
nvcc, and source modernization plus ABI validation is a separate engineering task. - Treat exact execution of the historical detector path as optional/unverified. This includes MRCNN/U-FRCNN/RetinaNet/Retina U-Net runs that import the custom operators. A framework-level CUDA tensor smoke is evidence only for PyTorch and the driver; it is not evidence that these extensions work.
- There is no CPU substitute for claiming exact legacy detector execution.
The source contains
nms_cpu, and RoIAlign has a CPU branch, but using those branches does not verify the model's importednms_gpu/legacy CUDA ABI path. Do not silently change a CUDA failure into a CPU result. - Do not load or inspect the checked-in
*.so,*.o, or swap files as runtime evidence. They are host- and ABI-specific artifacts; the source and wrappers are the portable evidence.
For a one-command, non-mutating report, run from the repository root:
python skills/disco/medical-detection-toolkit/sub-skills/cuda-extensions/scripts/check_legacy_cuda_compat.py \
--repo-root .
Add --framework-cuda-smoke only when a tiny PyTorch allocation is safe on the
selected GPU. Read compatibility before
interpreting the result, then use troubleshooting
for a stop/route decision.
Current verified classification
The prepared inspection stack reports a modern CUDA-capable PyTorch wheel,
CUDA available, and an A100-class (sm_80) device. The host driver reports CUDA 13.0. nvcc is absent and
torch.utils.ffi cannot be imported. The repository pins torch==0.4.1 and
its README describes precompiled TitanX-era operators. Therefore the current
host is useful for a safe PyTorch CUDA smoke and static inspection, but it is
not a verified environment for the legacy operators or exact detector path.
These facts are recorded with evidence in
compatibility.md; do not weaken this
classification because nvidia-smi reports a newer driver or because a .so
file exists.
Routing procedure
- Run the read-only checker. Record the Python interpreter, torch/driver
facts, presence of
torch.utils.ffi,nvcc, README architecture mappings, build inputs, and native artifacts. The checker never imports a custom extension and never callsbuild.py. - Separate framework CUDA from custom-op CUDA. If the optional framework
smoke succeeds, report only that
torch.cudacan allocate and execute a tiny tensor on the selected device. A failure due to device memory, device selection, or driver state is a host issue; it still does not justify trying the legacy extension. - Check dimensionality before reading signatures.
cf.dim == 2routes model proposal/detection NMS to 2D; a 3D configuration routes to 3D. The Mask/Faster R-CNN family additionally routes feature maps to the directra2Dorra3Dfunction wrapper. See custom-ops.md. - Classify compatibility blockers. Missing
torch.utils.ffi, missingnvcc, an unsupported architecture, or an unverified precompiled binary is a stop condition—not a prompt to patch imports, symlink libraries, or use CPU silently. - Route upward. For model selection, configuration, tensor shapes, and output contracts, continue with models-and-architectures. For prediction, postprocessing, 2D-to-3D merging, and saved-result behavior, continue with inference-and-evaluation. Return here only for custom-op import/build/ABI diagnosis.
Interfaces at a glance
NMS
cuda_functions/nms_2D/pth_nms.py:nms_gpu(dets, thresh)accepts a contiguous tensor with columns(y1, x1, y2, x2, score), sorts by column 4, calls the legacy_ext.nms.gpu_nms, and returns selected indices on CUDA. Its explicitnms_cpuuses the same five-column convention but is not a CUDA execution substitute.cuda_functions/nms_3D/pth_nms.py:nms_gpu(dets, thresh)accepts(y1, x1, y2, x2, z1, z2, score), sorts by the final column, calls the same legacy symbol, and returns selected CUDA indices. Itsnms_cpuuses the seven-column convention.- Both native implementations use inclusive
+1extents in IoU and suppress when overlap is>= threshon CPU. The CUDA kernels use a 64-bit suppression mask with 64 threads per block and perform the final keep scan on CPU. models/mrcnn.py,models/ufrcnn.py,models/retina_net.py, andmodels/retina_unet.pyimportnms_gpudirectly for RPN and/or final detection suppression. These imports make exact legacy detector execution depend on the custom extension even when ordinary PyTorch CUDA is healthy.
RoIAlign / crop-and-resize
- The 2D direct wrapper is
cuda_functions/roi_align_2D/roi_align/crop_and_resize.py:CropAndResizeFunction. Construct it as(crop_height, crop_width, extrapolation_value=0)and call it with(image, boxes, box_ind). The image isN,C,H,W; boxes areM,4normalized(y1,x1,y2,x2);box_indis lengthM; output isM,C,crop_height,crop_width. - The 3D direct wrapper is
cuda_functions/roi_align_3D/roi_align/crop_and_resize.py:CropAndResizeFunction. Construct it as(crop_height, crop_width, crop_zdepth, extrapolation_value=0). Its intended image isN,C,H,W,Z, boxes areM,6normalized(y1,x1,y2,x2,z1,z2), and output isM,C,crop_height,crop_width,crop_zdepth. models/mrcnn.pyandmodels/ufrcnn.pycall the direct wrappers asra2Dorra3Dinpyramid_roi_align; model ROIs are normalized and include a batch-index column before the wrapper separatesboxesandbatch_ixs.- The optional
RoIAlignclasses inroi_align.pyaccept pixel-coordinate boxes in an(x1,y1,x2,y2)docstring and normalize them whentransform_fpcoor=True. The 2D class is coherent with the 2D constructor. The checked-in 3DRoIAlignclass is not a reliable 3D interface: it has a 2D docstring/coordinate split and callsCropAndResizeFunctionwithout the requiredcrop_zdepth. Use the direct 3D wrapper facts above and treat this class as source evidence of drift, not as verified API.
For exact argument and dispatch details, follow the linked custom-ops reference. For model-level shape and configuration contracts, follow models-and-architectures.
Build prerequisites (read-only planning only)
The README's historical sequence manually compiles an architecture-specific
CUDA object with nvcc, then invokes the per-op build.py:
cuda_functions/nms_xD/src/cuda/
nvcc -c -o nms_kernel.cu.o nms_kernel.cu -x cu -Xcompiler -fPIC -arch=[arch]
python build.py
cuda_functions/roi_align_xD/roi_align/src/cuda/
nvcc -c -o crop_and_resize_kernel.cu.o crop_and_resize_kernel.cu -x cu -Xcompiler -fPIC -arch=[arch]
python build.py
The README examples map TitanX to sm_52, GTX 960M to sm_50, and GTX
1070/1080(Ti) to sm_61; they do not document A100 sm_80. Each build.py
uses torch.utils.ffi.create_extension, old TH/THC tensor APIs, C/CUDA
sources, and a precompiled .cu.o in extra_objects. This means all of the
following are prerequisites for a real legacy build: a matching historical
PyTorch/FFI ABI (the package pins 0.4.1), C/CUDA compiler headers and runtime,
nvcc, a target architecture flag, and compatible source/build tooling. A
CUDA-capable driver alone is insufficient. Never turn this planning section
into an unreviewed build command; use the checker and stop on the blockers in
troubleshooting.
Review checklist
Before handing a CUDA issue to another skill or user, record:
- the exact
cf.dim, model family, and whether the failure is at NMS, RoIAlign, import, or build planning; - the input shape/column convention and whether tensors are contiguous and on CUDA;
- checker output for torch,
torch.utils.ffi,nvcc, device capability, and README arch mapping; - whether a checked-in binary was merely present or actually verified (the default is not verified);
- a clear optional/unverified label for exact detector execution, with no CPU substitution; and
- the next route: models/configuration, inference/result handling, or a separate source-modernization project.
All bundled material is cross-linked here: custom-ops.md, compatibility.md, troubleshooting.md, and check_legacy_cuda_compat.py.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).