本文是「大模型开发」赛道 L1 阶段的深挖文。同阶段:
- 开发环境搭建:从零到跑通第一次模型调用
- 本地推理与自托管:选型、部署与硬件账
- 本地部署的硬件基线:显存、内存与系统门槛
- 操作系统选型:从 Windows 20H2 到 Linux 的迁移决策
- 本地存储的部署清单:从采购到装后验证
- 本地 GPU 栈落地:驱动、CUDA、vLLM 容器与工具链
- 大模型开发的工具地图:哪些要读源码,哪些只要会用
跨阶段:
本地 GPU 栈落地那篇把软件栈装起来了,最后停在「CUDA Toolkit 这一层」。
但那一层里还有一个东西始终只是被提到名字:nvcc。
「要不要装 CUDA Toolkit」这个判断,标准是「你要 nvcc 吗」。于是问题就来了——nvcc 是什么?
官方文档第一句话就给了答案,而且答案跟大多数人的预期不一样:
「It is the purpose of
nvcc, the CUDA compiler driver, to hide the intricate details of CUDA compilation from developers.」—— NVIDIA CUDA Compiler Driver 用户指南,§1.1.3 Purpose of NVCC
nvcc 是 CUDA 编译器驱动(compiler driver),不是编译器本身。
这个区别不是抠字眼。它决定了三件很实用的事:你该去哪里找报错、你能单独抽出哪一段产物来看、
以及为什么「我明明写了 -arch=sm_86,为什么报 unsupported」。
这篇把一条 .cu 文件从源代码到能上卡的机器码走一遍。
一、地图:谁干什么
nvcc 自己不做编译,它拆源、调度、拼装。真正的活分给几个可执行文件:
| 角色 | 干什么 | 谁调用它 |
|---|---|---|
nvcc |
编译器驱动:拆 host/device 两段、调度各阶段、把产物拼起来 | 你 |
device 前端(cicc / NVVM) |
device 代码 → PTX | nvcc |
ptxas |
PTX → SASS,装进 cubin | nvcc |
nvlink |
分离编译时,把可重定位的 device 代码链接起来 | nvcc |
宿主编译器(Linux 上是 gcc / g++,Windows 上是 cl.exe) |
host 代码编译、最终链接 | nvcc 调用 |
最后一行的官方原文是:
「All non-CUDA compilation steps are forwarded to a C++ host compiler that is supported by
nvcc, andnvcctranslates its options to appropriate host compiler command line options.」
这句话解释了一件很常见的事:你在 CUDA 项目里看到一堆 -I、-D、-L 参数,它们最终是喂给 gcc 的——
nvcc 只是把它们翻译过去。宿主的 include 路径、宏定义、静态库路径,全走这条路。
官方还专门说明了宿主的默认查找规则:
「On all platforms, the default host compiler executable (
gccandg++on Linux andcl.exeon Windows) found in the current execution search path will be used, unless specified otherwise with appropriate options.」
「在当前执行搜索路径里找到的那个」 —— 这就是为什么同一台机器上,/usr/bin/nvcc 编译的包和/usr/local/cuda/bin/nvcc 编译的包,行为可能不一样:它们找到的 gcc 不是同一个。
要指定宿主编译器,官方给的是 --compiler-bindir(-ccbin)。
1.1 什么时候你会直接碰上 nvcc
- 编译自定义 CUDA 扩展——PyTorch 的
torch.utils.cpp_extension、vLLM 的某些算子、FlashAttention; - 编译 llama.cpp / vLLM 的 CUDA 后端(见 本地 GPU 栈落地 第五节);
- 读 kernel 源码:想知道
.cu→.ptx→.cubin这一串到底是什么关系; - 排查报错:
unsupported gpu architecture、no kernel image is available这类消息到底是谁发出的。
前两种是「必须会」,第三种是「读得懂」,第四种是「不抓瞎」。
二、编译流水线:一条 .cu 文件的旅程
2.1 官方口径
官方对整条轨迹的描述(nvcc 用户指南 §3 The CUDA Compilation Trajectory):
「CUDA compilation works as follows: the input program is preprocessed for device compilation and is compiled to CUDA binary (
cubin) and/or PTX orcuda_tileintermediate code, which are placed in a fatbinary. The input program is preprocessed once again for host compilation and is synthesized to embed the fatbinary and transform CUDA specific C++ extensions into standard C++ constructs. Then the C++ host compiler compiles the synthesized host code with the embedded fatbinary into a host object.」
拆开就是五步:
foo.cu
│
├─ ① 设备侧预处理 ─→ 编译成 cubin 和/或 PTX ─→ 塞进 fatbinary
│ (cuda_tile 中间码同理)
│
└─ ② host 侧再预处理一次
│
└─ ③ 合成:把 fatbinary 嵌进去,
把 CUDA 专有的 C++ 扩展改写成标准 C++ 构造
│
└─ ④ 宿主编译器(gcc/g++/cl.exe)编译合成后的 host 代码
│
└─ ⑤ host 目标文件(内嵌 fatbinary)
│
程序启动、host 调用 kernel ────────────┘
└─ CUDA runtime 检查内嵌的 fatbinary,为当前这块卡挑一份合适的镜像
这份图里最容易被忽略的是第 ③ 步。 CUDA 的 __global__、<<<>>> 这些语法,在最终的目标文件里
已经不存在了——它们被改写成了标准 C++ 的调用 stub,kernel 的机器码则以 fatbinary 的形式内嵌在
目标文件里。
所以「为什么 .so 里有 kernel 却没有 kernel 的源码」这个问题的答案就在这里:源码在 fatbinary 里,不在 .so 的符号表里。
2.2 阶段是可以单独选的
因为 nvcc 是驱动,它支持「只跑到某一步」。官方文档把这叫 compilation phase,并且给了一张完整的表
(§2.4 Supported Phases)。这张表是本文最该被引用的一张:
| 阶段 | nvcc 选项 | 默认产物 |
|---|---|---|
| CUDA 源码编译成 C/C++ 源码 | --cuda / -cuda |
源文件名后缀加 .cpp.ii,如 x.cu.cpp.ii |
| C/C++ 预处理 | --preprocess / -E |
标准输出 |
| C/C++ 编译成目标文件 | --compile / -c |
源文件名后缀换成 .o(Windows 是 .obj) |
| 从 CUDA 源码生成 SIMT cubin | --cubin / -cubin |
后缀换成 .cubin |
| 从 PTX 中间文件生成 cubin | --cubin / -cubin |
后缀换成 .cubin |
| 从 CUDA 源码生成 PTX | --ptx / -ptx |
后缀换成 .ptx |
| 从源码 / PTX / cubin 生成 fatbinary | --fatbin / -fatbin |
后缀换成 .fatbin |
| 链接可重定位 device 代码 | --device-link / -dlink |
a_dlink.o(Windows 是 a_dlink.obj) |
| 链接可执行文件 | 无阶段选项 | a.out(Windows 是 a.exe) |
| 构造目标文件归档或库 | --lib / -lib |
a.a(Windows 是 a.lib) |
| 跑一个可执行文件 | --run / -run |
—— |
这张表是实战里最常用的。三个立刻能用的例子:
# 只看 CUDA 代码被改写成什么(host 编译器最终看到的东西)
nvcc --cuda x.cu -o x.cu.cpp.ii
# 只生成 PTX,不生成机器码
nvcc --ptx x.cu -o x.ptx
# 只生成某一架构的机器码
nvcc --cubin x.cu -arch sm_86 -o x.cubin
官方文档在这张表下面还留了一句警告,值得原样抄下来:
「only compilation phases are stable across releases, and although
nvccprovides options to display the compilation steps that it executes, these are for debugging purposes only and must not be copied and used in build scripts.」
翻译:阶段是稳定的,阶段内部的步骤不是。 nvcc 打印出来的内部执行序列(cudafe++、cicc、ptxas 怎么调)随版本变,
不要抄进构建脚本。这解释了为什么老博客里那些「照抄 nvcc -v 输出就能复现」的教程会失效。
2.3 官方口径的一个补充:默认是「整程序编译」
「CUDA programs are compiled in the whole program compilation mode by default, i.e., the device code cannot reference an entity from a separate file. In the whole program compilation mode, device link steps have no effect.」
默认模式下,device 代码不能跨文件引用。 这不是限制,是设计:整程序编译让编译器能做全局优化。
需要跨文件引用时,才切到第六节说的分离编译。
三、两段式编译:虚拟架构与真实架构
3.1 compute_XX 与 sm_XX
CUDA 不用「二进制兼容」来保证应用能跑在新卡上——官方说得很直白:
「This situation is different for GPUs, because NVIDIA cannot guarantee binary compatibility without sacrificing regular opportunities for GPU improvements. Rather,
nvccrelies on a two stage compilation model for ensuring application compatibility with future GPU generations.」
取而代之的办法是两段式编译,中间夹一层 PTX。官方对虚拟架构的定义:
「GPU compilation is performed via an intermediate representation, PTX, which can be considered as assembly for a virtual GPU architecture.」
于是一条 nvcc 命令必然同时指定两个架构:
| 是什么 | 官方例子 | |
|---|---|---|
虚拟架构 compute_XX |
一个虚拟 GPU 的指令集;PTX 就是它的汇编 | compute_86 |
真实架构 sm_XX |
某个具体 GPU 世代的机器码目标 | sm_86 |
官方给的硬约束是:
「For such an
nvcccommand to be valid, the real architecture must be an implementation of the virtual architecture.」
真实架构必须是虚拟架构的实现——sm_89(Ada)是 compute_89 的实现;反过来,compute_86 的 PTX
在 sm_89 上是能跑的(往上兼容),但 compute_89 的 PTX 不能在 sm_86 上跑。
3.2 官方给的选型原则
「the virtual architecture should always be chosen as low as possible, thereby maximizing the actual GPUs to run on. The real architecture should be chosen as high as possible (assuming that this always generates better code), but this is only possible with knowledge of the actual GPUs on which the application is expected to run on.」
虚拟架构定低,真实架构定高。 虚拟架构定低了,PTX 的适用面就宽(能 JIT 到更多代);
定高了,那些用不到的特性和把可跑的卡排除掉了。真实架构定高了,生成的机器码更贴合那张卡。
3.3 三个选项,三种写法
官方在 §5.7 给了这些选项:
| 选项 | 形式 | 语义 |
|---|---|---|
--gpu-architecture |
单值,虚拟架构 | 第一段(编到 PTX)的目标 |
--gpu-code |
列表,真实架构 | 第二段(编到机器码)的目标,可给多个 |
--generate-code |
可重复,arch=...,code=... |
把上面两个合起来写,可以重复多次 |
官方的例子:
# 虚拟架构 compute_80,真实架构两个:sm_80 和 sm_86
nvcc x.cu --gpu-architecture=compute_80 --gpu-code=sm_80,sm_86
# 只给 compute_80 和 compute_90,机器码留给驱动 JIT
nvcc x.cu \
--generate-code arch=compute_80,code=sm_80 \
--generate-code arch=compute_80,code=sm_86 \
--generate-code arch=compute_90,code=sm_90
# 同一份虚拟架构,真实架构交给 JIT
nvcc x.cu \
--generate-code arch=compute_80,code=compute_80 \
--generate-code arch=compute_90,code=compute_90
code=compute_XX 这一行是「向前兼容」开关——它的意思是「这一档我只要 PTX,不要机器码」。
还有第三种更常见的简写形式 -arch。官方编程指南把它的几种写法并排列了出来,
注释里的那两行值得逐字读:
# PTX-only,GPU forward compatible
nvcc example.cu -arch=compute_<XY>
# PTX 和 Cubin 都要,GPU forward compatible
nvcc example.cu -arch=sm_<XY>
# 自动探测当前 GPU,只生成 Cubin
nvcc example.cu -arch=native
# 所有支持架构的 Cubin,外加最新的一份 PTX
nvcc example.cu -arch=all
nvcc example.cu -arch=all-major
nvcc 用户指南里对应的说明是:
「Specify name of NVIDIA GPU to generate code for. This option also takes virtual compute architectures, in which case code generation is suppressed.」
合起来读:--gpu-name(-arch)接受虚拟架构时,代码生成被抑制——-arch=compute_86 只产出 PTX,不产出任何机器码;-arch=sm_86 产出 sm_86 的 cubin,
并且顺带留一份 PTX(官方注释里的 “GPU forward compatible” 就是指这份 PTX)。
这是一条极易踩的线:
-arch=compute_XX不是「更通用的 sm_XX」,它是「只有 PTX」,
跑起来全靠驱动 JIT,而 JIT 在老驱动上是不工作的(见第 4.4 节)。
官方还给了两个查清单的命令,写脚本时别写死架构列表:
nvcc --list-gpu-arch # 所有支持的虚拟架构
nvcc --list-gpu-code # 所有支持的真实架构
另外注意官方这句默认行为:
「By default,
nvccgenerates PTX and Cubin for the earliest GPU architecture (lowestcompute_XYandsm_XYversion) supported by the CUDA Toolkit to maximize compatibility.」
不写 -arch 时,nvcc 编译的是「CUDA Toolkit 支持的最老那一代架构」。
这既不慢也不快,但它是兼容优先的选择,而不是性能优先的选择。
3.4 __CUDA_ARCH__:一段代码写两种架构
官方对架构识别宏的说明:
「The architecture identification macro
__CUDA_ARCH__is assigned a three-digit value stringxy0(ending in a literal0) for each stage 1nvcccompilation that compiles forcompute_xy.」
多架构编译时,同一个 .cu 会被第一段编译多次,每次的 __CUDA_ARCH__ 不同。
代码里常用它做条件编译:
__global__ void k() {
#if __CUDA_ARCH__ >= 800
// sm_80 及以上走这条路
#else
// 旧架构的兜底
#endif
}
官方也提醒了它的约束:重复指定架构时,device 代码必须用 __CUDA_ARCH__ 做条件编译。
四、PTX 与 JIT:向前兼容是怎么来的
4.1 两种机制
官方明确说,需要另外两个机制才能真正做到面向未来 GPU 的兼容:
「Clearly, compilation staging in itself does not help towards the goal of application compatibility with future GPUs. For this we need two other mechanisms: just-in-time compilation (JIT) and fatbinaries.」
① JIT(just-in-time compilation)
「By specifying a virtual code architecture instead of a real GPU,
nvccpostpones the assembly of PTX code until application runtime, at which time the target GPU is exactly known.」
官方例子:
nvcc x.cu --gpu-architecture=compute_90 --gpu-code=compute_90
这条命令生成的只有 PTX。程序启动时,驱动才知道要跑在哪张卡上,这时候才把 PTX 汇编成那张卡的机器码。
② Fatbinary(胖二进制)
「
nvccorganizes its device code in fatbinaries, which are able to hold multiple translations of the same GPU source code. At runtime, the CUDA driver will select the most appropriate translation when the device function is launched.」
nvcc x.cu --gpu-architecture=compute_80 --gpu-code=compute_80,sm_86,sm_89
这一条生成两份机器码(sm_86、sm_89)加一份 PTX。运行时驱动挑最合适的那份。
4.2 代价:启动延迟
官方对 JIT 的缺口的原文:
「The disadvantage of just-in-time compilation is increased application startup delay, but this can be alleviated by letting the CUDA driver use a compilation cache, which is persistent over multiple runs of the applications.」
JIT 第一次跑要等编译,第二次跑就走缓存。 这就是「fatbinary 里塞多份机器码」这条路的价值:
它绕开了启动延迟,同时保留了「万一来了张新卡还能 JIT」的能力。
而驱动挑镜像的那一刻,在官方轨迹里写得很清楚:
「The embedded fatbinary is inspected by the CUDA runtime system whenever the device code is launched by the host program to obtain an appropriate fatbinary image for the current GPU.」
每次 kernel 启动,runtime 都会看一眼内嵌的 fatbinary——这就是「同一份二进制在不同卡上跑出不同机器码」的原因。
4.3 虚拟架构清单(官方当前定义)
| 虚拟架构 | 对应 GPU 架构 |
|---|---|
compute_75 |
Turing |
compute_80、compute_86、compute_87、compute_88 |
Ampere |
compute_89 |
Ada |
compute_90、compute_90a |
Hopper |
compute_100、compute_100f、compute_100a、compute_103、compute_103f、compute_103a、compute_110、compute_110f、compute_110a、compute_120、compute_120f、compute_120a、compute_121、compute_121f、compute_121a |
Blackwell |
compute_107、compute_107f、compute_107a |
Rubin |
带 f 后缀的是「family-specific」变体,带 a 的是「architecture-specific」变体——
它们提供该架构专属的特性,代价是不能跨架构复用。
这张表会随 CUDA 版本变,落地前以你手上那份 nvcc 用户指南为准。
4.4 驱动兼容的两套机制
「PTX 由驱动 JIT」这件事有一个直接后果:老驱动可能 JIT 不了新 PTX。
NVIDIA 为此定义了两套兼容机制,在《CUDA Compatibility》文档里写得很清楚。
① Minor Version Compatibility(次版本兼容,CUDA 11 起默认)
「From CUDA 11 onwards, applications compiled with a CUDA Toolkit release from within a CUDA major release family can run, with limited feature-set, on systems having at least the minimum required driver version as indicated below.」
官方给的驱动版本区间表:
| CUDA Toolkit | 最低驱动版本 | 次版本兼容的上界 |
|---|---|---|
| CUDA 13.x | ≥ 580 | N/A(更新的驱动靠向后兼容) |
| CUDA 12.x | ≥ 525 | < 580 |
| CUDA 11.x | ≥ 450 | < 525 |
这张表就是「CUDA 13 要 R580+ 驱动」那条结论的官方出处(本地 GPU 栈落地
的兼容矩阵里引的是同一个口径)。
官方还列了三条使用限制,其中两条和本文直接相关:
「Applications using PTX will see runtime issues — Applications that compile device code to PTX will not work on older drivers.」
「Requires target architecture argument to NVCC — Minor Compatibility requires passing the target architecture argument to NVCC, for example
nvcc -arch=sm_xx.」
这两条合起来是一条硬规则:只编出 PTX 的程序,在次版本兼容的框架下跑不了老驱动;
要让次版本兼容生效,必须显式给出 -arch=sm_xx。
另一条限制是特性集:某些跨 Toolkit 与驱动的特性在老驱动上会报 cudaErrorCallRequiresNewerDriver。
② Forward Compatibility(向前兼容,跨大版本)
「CUDA introduced a Forward Compatibility Upgrade path in CUDA 10.0.」
它靠一个特殊的包 cuda-compat-<major>-<minor>(例如 cuda-compat-13-5),
把与新 Toolkit 配套的驱动库装进版本化的 Toolkit 目录,再靠 LD_LIBRARY_PATH 指过去。
官方限定了适用对象:数据中心 GPU、部分 NGC Server Ready 的 RTX SKU、Jetson 板子。
这个包里有一个文件直接印证了本文的主题:
「
libnvidia-ptxjitcompiler.so.*— JIT compiler for PTX files」
PTX 的 JIT 编译器在驱动这一侧,不在 Toolkit 这一侧。所以「PTX 能不能 JIT」这件事,
最终由你装的是哪个驱动决定。
五、CUDA 13 的架构门槛(这条最实用)
上一节的清单里,没有 compute_50(Maxwell)、compute_60(Pascal)、compute_70(Volta)。
这不是漏抄,是 CUDA 13.0 官方明确移除的:
「Architecture support for Maxwell, Pascal, and Volta is considered feature-complete. Offline compilation and library support for these architectures have been removed in CUDA Toolkit 13.0 major version release. The use of CUDA Toolkits through the 12.x series to build applications for these architectures will continue to be supported, but newer toolkits will be unable to target these architectures.」
—— CUDA Toolkit 13.0 Release Notes,§2.6.2 Deprecated Architectures
CUDA 13.0 发行说明的 Deprecations 一节也有一条更短的写法:
「Removed support for Maxwell, Pascal, and Volta GPUs, corresponding to compute capabilities earlier than Turing.」
翻译成人话:
- CUDA 13 工具链最低支持 Turing(sm_75)。 更老的卡要用 CUDA 12.x 工具链编。
- 这条与 vLLM 的口径是一致的——vLLM 官方要求 compute capability 7.5 及以上(见
本地 GPU 栈落地 的兼容矩阵)。 - 「编译得过」不等于「跑得动」:老卡上驱动版本、CUDA 运行时、框架 wheel 三者还要各自对得上。
同一条发行说明里还有一条容易被忽略的:CUDA 13.0 起,Ubuntu 20.04 的支持被移除,
官方建议迁移到 Ubuntu 22.04 LTS 或更新版本。
六、分离编译:为什么要多一步
默认的整程序编译(whole program compilation)里,device 代码不能跨文件引用。
CUDA 5.0 起支持了 device 代码的分离编译,但默认仍然是整程序模式。
官方对分离编译的说法:
「In separate compilation, we embed relocatable device code into the host object, and run
nvlink, the device linker, to link all the device code together. The output ofnvlinkis then linked together with all the host objects by the host linker to form the final executable.」
于是流程从「一步」变成「两步」:
a.cu ──nvcc -dc──> a.o(含可重定位 device 代码)
b.cu ──nvcc -dc──> b.o(含可重定位 device 代码)
│
└─ nvcc -dlink ──> a_dlink.o(device 代码链接完成)
│
a.o b.o a_dlink.o ──宿主链接器──> 可执行文件
对应的选项:
| 选项 | 作用 |
|---|---|
--relocatable-device-code=true |
生成可重定位的 device 代码(默认 false) |
--device-c / -dc |
上面那个选项 + --compile 的简写 |
--device-link / -dlink |
只跑 device 链接器,产物是 a_dlink.o |
官方还给了个偷懒写法:nvcc <objects> 会隐式调用 device 链接器和宿主链接器,
因为 device 链接器看不到可重定位代码时什么也不做。
6.1 代码层面要改什么
官方写得很直接:
「The code changes required for separate compilation of device code are the same as what you already do for host code, namely using
externandstaticto control the visibility of symbols. Note that previouslyexternwas ignored in CUDA code; now it will be honored.」
以前 CUDA 代码里的 extern 是被忽略的,现在会被尊重。 这意味着老的代码里那些
「靠 extern 被忽略才没出事」的地方,切到分离编译后会真的开始链接。
还有一个连带的变化:
「the CUDA API calls that referred to symbols by their string name are deprecated; instead the symbol should be referenced by its address.」
按字符串名查符号的 CUDA API 被弃用,要改成按地址引用。动态加载 device 函数的场景要注意这条。
6.2 分离编译的坑
官方专门有一节列了潜在问题(§6.6 Potential Separate Compilation Issues),其中两条值得记住:
- 对象兼容性:
__CUDA_ARCH__在不同编译单元取不同值时,链接期只会用一份实现,
行为取决于「挑中了哪一份」。官方给的规避办法是:要么所有.cu用同一个 compute 架构编译,
要么共享头文件里的函数不要用__CUDA_ARCH__。 - 库里的 device 代码:如果一个库和一个非库目标文件里定义了同名、非 weak 的外部链接 device 函数,
device 链接器会报多重定义——这和传统宿主链接器的行为不一样(宿主链接器可能直接忽略库里的定义)。
七、NVRTC:运行期编译的那条路
官方编程指南对 nvcc 的定位里,有一句是专门用来对比 NVRTC 的:
「
nvccdrives offline compilation of CUDA code, in contrast to online or Just-in-Time (JIT) compilation driven by the CUDA runtime compilernvrtc.」
同一个「JIT」字眼,在两处指的是两件事:
| nvcc | NVRTC | |
|---|---|---|
| 什么时候编 | 离线(构建时) | 在线(程序运行时) |
| 产物 | 目标文件 / 库 | 内存里的 PTX 或 cubin |
| 谁调用 | 你在命令行里 | 程序通过 API 调用 |
| 典型用途 | 编译整个 CUDA 程序 | 运行时按输入动态生成 kernel |
一句话区分:nvcc 是「编译 CUDA 程序的工具」,NVRTC 是「在程序里现场编 CUDA 的库」。
NVRTC 的官方定位文档在 NVRTC 用户指南。
它最常见的使用场景是把 CUDA 当成运行时后端:程序收到一段字符串形式的 kernel,
现场编译成 PTX 再交给驱动——这类需求是离线编译满足不了的。
本文不展开 NVRTC 的 API 细节(它的限制、头文件支持、编译选项与 nvcc 的差异,官方文档另有一册)。
这里只把它放进地图里:nvcc 与 NVRTC 是两个东西,一个离线一个在线。
看到「JIT」这个词时,先确认说的是哪一个。
八、动手看产物:三个命令
8.1 --resource-usage:看寄存器用了多少
官方文档给的例子:
$ nvcc --resource-usage acos.cu -arch sm_80
ptxas info : 1536 bytes gmem
ptxas info : Compiling entry function 'acos_main' for 'sm_80'
ptxas info : Function properties for acos_main
0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads
ptxas info : Used 6 registers, 1536 bytes smem, 32 bytes cmem[0]
「spill stores / spill loads」这两个数是排查性能问题的第一现场:一旦不为 0,
说明寄存器不够用,编译器把数据挪到了本地内存——在 GPU 上这非常慢。
8.2 --keep / --keep-dir / --clean-targets:留住中间产物
「nvcc stores intermediate results by default into temporary files that are deleted immediately before it completes.」
「Option
--keepmakes nvcc store these intermediate files in the current directory or in the directory specified by--keep-dirinstead.」
默认中间产物在 TMPDIR(没设就是 /tmp),编译完就删。想看 PTX 就得用 --keep 留住。
清理也很方便:把原命令加上 --clean-targets 再跑一遍就行。
8.3 cuobjdump 与 nvdisasm:读已经编好的二进制
看一个 .so 或 .o 里到底带了哪些架构的机器码、PTX 长什么样:
# 列出里面所有的 ELF(cubin)
cuobjdump --list-elf x.so
# 列出所有 PTX
cuobjdump --list-ptx x.so
# 把某个架构的 PTX 打出来
cuobjdump --dump-ptx x.so
# 反汇编 SASS
nvdisasm -c x.so
排查「no kernel image is available」这类问题时,第一步就是 cuobjdump --list-elf 看一眼
这个二进制里到底有没有你这张卡的架构——这是本文第七节表格里最常用的入口。
九、报错对照表:先看是谁在说话
CUDA 的报错前缀能直接告诉你是哪个阶段出的问题,这一点是「nvcc 是驱动」带来的最大好处。
运行期错误还有官方的一组错误码可以直接对上(CUDA Runtime API 的 cudaError_t 枚举):
| 你看到的 | 官方错误码 | 出错阶段 | 官方含义与处置 |
|---|---|---|---|
no kernel image is available for execution on the device |
cudaErrorNoKernelImageForDevice (209) |
运行期 | 官方原话:「there is no kernel image available that is suitable for the device. This can occur when a user specifies code generation options for a particular CUDA source file that do not include the corresponding device configuration.」 |
CUDA driver version is insufficient for CUDA runtime version |
cudaErrorInsufficientDriver (35) |
运行期 | 官方原话:「the installed NVIDIA CUDA driver is older than the CUDA runtime library. This is not a supported configuration.」 |
| 某个 API 报「需要更新的驱动」 | cudaErrorCallRequiresNewerDriver (36) |
运行期 | 该 API 用到的新特性在当前驱动上没有。升级驱动,或避开这个特性 |
invalid device function |
cudaErrorInvalidDeviceFunction (98) |
运行期 | 官方原话:「The requested device function does not exist or is not compiled for the proper device architecture.」 |
unsupported gpu architecture 'sm_XX' |
—— | 编译期 | 你的 nvcc 不认识这个架构。查 nvcc --list-gpu-arch 与第四节清单 |
ptxas fatal : ... |
—— | 汇编期(PTX → SASS) | PTX 合法但汇编不过,多半是寄存器压力或用了该架构没有的指令。看 --resource-usage |
undefined reference to ...(device 函数) |
—— | 链接期 | 分离编译漏了 -dlink 一步 |
其中第一行最值得单拎出来。 它的误导性在于:报错发生在运行期、报错信息里没有「架构」两个字,
但根因在编译期——你编出来的 fatbinary 里没有这张卡的机器码,或者只有 PTX 而驱动没能 JIT。
官方在错误码说明里已经把因果写明了:「code generation options … do not include the corresponding device configuration」——
代码生成选项没覆盖这台设备。所以排查从编译期开始,而不是从运行期开始:
# 1) 这块卡到底是什么架构
nvidia-smi --query-gpu=name,compute_cap
# 2) 二进制里到底有什么
cuobjdump --list-elf 你的.so
cuobjdump --list-ptx 你的.so
# 3) 编译时是不是只出了 PTX
# 回忆一下 -arch 写的是 compute_XX 还是 sm_XX
nvidia-smi --query-gpu=name,compute_cap是官方编程指南给出的查法,
它比「记住哪张卡是 8.6」可靠——架构规格会随产品线变化,记名字不如现场查。
十、PyTorch 里的 nvcc
10.1 扩展是怎么被编译的
大模型开发里最常见的「直接用 nvcc」的场景,是给 PyTorch 写自定义算子。官方入口是torch.utils.cpp_extension:
from torch.utils.cpp_extension import load
mod = load(
name="my_ext",
sources=["my_ext.cu"],
extra_cuda_cflags=["-O3", "--use_fast_math"],
)
load 会在构建目录里拼出一条 nvcc 命令,把 .cu 编成 .so,再由 Python 侧 dlopen 加载。
你传的 extra_cuda_cflags 是直接透传给 nvcc 的——这就是上面那些选项在实战里的入口。
10.2 TORCH_CUDA_ARCH_LIST:替 nvcc 选架构
官方文档在这一节讲得很细:
「By default the extension will be compiled to run on all archs of the cards visible during the building process of the extension, plus PTX. If down the road a new card is installed the extension may need to be recompiled.」
默认行为是「编译时看得见什么卡,就编什么卡的架构,外加一份 PTX」。
这意味着:同一份代码在插着 A 卡的机器上编出来的扩展,和插着 B 卡的机器上编出来的,不是同一个东西。
要覆盖这个行为,就用 TORCH_CUDA_ARCH_LIST:
TORCH_CUDA_ARCH_LIST="6.1 8.6" python build_my_extension.py
TORCH_CUDA_ARCH_LIST="5.2 6.0 6.1 7.0 7.5 8.0 8.6+PTX" python build_my_extension.py
+PTX 后缀是本文第 3.3 节那条规则的另一种写法。官方解释:
「The
+PTXoption causes extension kernel binaries to include PTX instructions for the specified CC. PTX is an intermediate representation that allows kernels to runtime-compile for any CC >= the specified CC (for example,8.6+PTXgenerates PTX that can runtime-compile for any GPU with CC >= 8.6). This improves your binary’s forward compatibility. However, relying on older PTX to provide forward compat by runtime-compiling for newer CCs can modestly reduce performance on those newer CCs. If you know exact CC(s) of the GPUs you want to target, you’re always better off specifying them individually.」
记住这句权衡:靠 PTX 换来的向前兼容,代价是在新卡上性能会略降。
知道要跑哪几张卡,就把它们逐个写出来("8.0 8.6" 好过 "8.0+PTX")。
官方还提醒了构建时间:
「the more archs get included the slower the building process will be, as it will build a separate kernel image for each arch.」
给做扩展的人的实用建议:明确写出
TORCH_CUDA_ARCH_LIST,别依赖自动探测。
探测会随机器上插了哪张卡而变——这类「本地好好的,CI 上no kernel image」的故障,多数就是这么来的。
10.3 分离编译在扩展里怎么体现
官方对扩展里的可重定位 device 代码也写了:
「If you want to reference device symbols across compilation units (across object files), the object files need to be built with relocatable device code (
-rdc=trueor-dc).」「If you have rdc objects you need to have an extra
-dlink(device linking) step before the CPU symbol linking step.」
并且提醒 -rdc 的代价:
「Relocatable device code is less optimized so it needs to be used only on object files that need it. Using
-dlto(Device Link Time Optimization) at the device code compilation step and dlink step helps reduce the protentional perf degradation of-rdc.」
这就是第六节那套两步流程在 PyTorch 扩展里的落法:
只有真正需要跨文件引用 device 符号的那几个 .cu 才加 -rdc=true,
其余照常整程序编译——-rdc 是有性能代价的,不该无差别地开。
10.4 宿主编译器那半边
第 1 节说过,host 代码是转发给宿主编译器的。官方给了三个控制它的选项:
nvcc example.cu -ccbin=clang++ # 指定用哪个宿主编译器
export NVCC_CCBIN='gcc' # 用环境变量指定
nvcc example.cu -Xcompiler=-O3 # 把参数透传给宿主编译器
还有一条容易忽略的默认:
「By default,
nvcclinks against the static CUDA runtime library. To use the shared library version of the CUDA runtime, pass the flag--cudart=sharedtonvccon the compile or link command.」
默认链的是静态 CUDA 运行时。 想知道当前这份二进制到底链了哪一份,ldd 一下就清楚。
十一、边界与小结
七条带走的:
nvcc是编译器驱动,不是编译器。 它拆源、调度、拼装;真正的编译在cicc、ptxas、
宿主编译器里。看到报错前缀就知道是谁在说话。- 一条 nvcc 命令必然指定两个架构:虚拟
compute_XX与真实sm_XX。
虚拟定低、真实定高;-arch=compute_XX的含义是「只出 PTX,机器码交给驱动 JIT」,-arch=sm_XX是「PTX + 该架构的机器码」。 - PTX + JIT + fatbinary 三件套才是向前兼容的来源。 NVIDIA 不保证 GPU 二进制兼容,
官方明确写了这一点。而 JIT 由驱动执行,所以「PTX 能不能 JIT」最终由驱动版本决定。 - CUDA 13.0 起,Maxwell / Pascal / Volta 不再支持(低于 Turing 的 compute capability 一律不认),
与 vLLM 的 sm_75+ 门槛是同一条线。 - 次版本兼容有前提:CUDA 13.x 要驱动 ≥ 580、12.x 要 ≥ 525、11.x 要 ≥ 450;
而且只编出 PTX 的程序在老驱动上不工作,要让次版本兼容生效必须显式-arch=sm_xx。 - 默认是整程序编译;跨文件引用 device 符号要切分离编译,多一步
nvlink,
而且extern从此开始被尊重。 --resource-usage、--keep、cuobjdump --list-elf这三个工具,能把「猜」变成「看」。
本文没讲、留给后面的:
- 驱动、CUDA Toolkit、容器、工具链四层怎么装 → 本地 GPU 栈落地
- kernel 怎么写、怎么调优 → 不在本站这条线的范围内
- 推理服务的旋钮(并发、上下文、指标)→ Agent、工具调用与推理服务
边界与未核实项:
- 本文所有阶段名、选项名、虚拟架构清单与官方引述核对时间 2026-12-05,
取自 NVIDIA 官方文档站当时的 nvcc 用户指南、CUDA C++ 编程指南、CUDA Compatibility 文档、
CUDA Toolkit 13.0 发行说明与 CUDA 安装指南。
虚拟架构清单与报错文本会随 CUDA 版本变,落地前请以你手上那份文档当时的状态为准。 - 本文没有在装有 GPU 的机器上实跑过编译。 命令写法与官方文档一致,
但具体输出(ptxas info的寄存器数、报错文本的确切拼写)取决于你的代码与 CUDA 版本。 - 第三节的「虚拟架构定低、真实架构定高」是官方的建议,不是无条件的定理。
如果你的程序用了某代 GPU 专属特性(a/f后缀的架构),真实架构就定不下去。 - 本文只覆盖 SMIT 路径。 CUDA 13 的
cuda_tile路径(--tilecubin、--tilefatbin、--tilebc这几个阶段)在官方文档里是并列的另一条轨迹,本文没有展开。 - NVRTC 只给了定位,没有展开 API。 它的头文件支持、选项差异与限制见官方 NVRTC 用户指南。
- 宿主编译器的版本区间以 CUDA 安装指南为准(本地 GPU 栈落地
的兼容矩阵里记的是 GCC 6.x – 16.x),本文不重复。
引用
本文涉及的阶段名、选项名、虚拟架构清单、官方引述与版本口径,取自以下官方来源(核对时间:2026-12-05):
| 事实 | 出处 |
|---|---|
nvcc 是 CUDA 编译器驱动、其目的是隐藏 CUDA 编译的复杂细节;非 CUDA 阶段转发给宿主编译器;默认宿主编译器是搜索路径里的 gcc/g++(Linux)或 cl.exe(Windows) |
CUDA Compiler Driver (nvcc) 用户指南 §1.1.3、§1.2 |
| 输入文件后缀表、支持的编译阶段与各自选项、默认产物名;「只有编译阶段在版本间稳定,内部步骤不可写进构建脚本」 | 同上,§2.3、§2.4 |
| CUDA 编译轨迹(设备侧预处理 → cubin/PTX → fatbinary → host 侧再预处理与合成 → 宿主编译器 → 内嵌 fatbinary 的目标文件);启动时 runtime 检查 fatbinary 挑镜像;默认整程序编译 | 同上,§3 |
两段式编译模型;虚拟架构定义;真实架构必须是虚拟架构的实现;虚拟架构定低、真实架构定高;--gpu-architecture / --gpu-code / --generate-code 的用法示例;__CUDA_ARCH__ 的取值规则 |
同上,§5.3、§5.4、§5.6、§5.7 |
| 当前虚拟架构清单(compute_75 至 compute_121,含 Blackwell 与 Rubin) | 同上,§5.5 Virtual Architecture Feature List |
| JIT 的定义与启动延迟代价、驱动编译缓存;fatbinary 的多份翻译与运行期选择 | 同上,§5.6.1、§5.6.2 |
--gpu-name(-arch)接受虚拟架构时抑制代码生成 |
同上,§4.2.9.1.15 |
分离编译流程、nvlink 的角色、--relocatable-device-code / --device-c / --device-link 选项;extern 现在被尊重、按字符串名查符号的 API 被弃用;对象兼容性与库中 device 代码的多重定义 |
同上,§6.1、§6.2、§6.6 |
--resource-usage 的官方输出示例(寄存器、spill、smem、cmem);中间文件默认落在 TMPDIR、--keep / --keep-dir / --clean-targets 的行为 |
同上,§8.2、§8.3、§8.4 |
nvcc 的工具链定位(顶层工具协调各阶段、nvcc 离线编译 vs nvrtc 在线编译);-arch=compute_XX / -arch=sm_XX / -arch=native / -arch=all 的官方注释;默认编译最老架构;--list-gpu-arch / --list-gpu-code;-ccbin / NVCC_CCBIN / -Xcompiler;默认链静态 CUDA 运行时、--cudart=shared |
CUDA C++ Programming Guide §2.7 NVCC |
nvidia-smi --query-gpu=name,compute_cap 查 compute capability 的官方方法 |
同上,§5.1.1 Obtain the GPU Compute Capability |
cudaErrorNoKernelImageForDevice (209)、cudaErrorInsufficientDriver (35)、cudaErrorCallRequiresNewerDriver (36)、cudaErrorInvalidDeviceFunction (98) 的官方含义 |
CUDA Runtime API — 数据类型与枚举 |
Minor Version Compatibility 的定义与驱动版本区间表(13.x ≥ 580、12.x ≥ 525、11.x ≥ 450);「只编 PTX 的应用在老驱动上不工作」;「次版本兼容要求给 nvcc 传目标架构」;Forward Compatibility 的 cuda-compat-<major>-<minor> 包、适用对象与 libnvidia-ptxjitcompiler.so.* |
CUDA Compatibility — Minor Version Compatibility 与 Forward Compatibility |
| CUDA 13.0 移除 Maxwell / Pascal / Volta 的离线编译与库支持;CUDA 13.0 移除 Ubuntu 20.04 支持 | CUDA Toolkit 13.0 Release Notes §2.6.2、§2.6.3 |
| CUDA 13.0 新增宿主编译器支持(Clang 20、GCC 15)、libNVVM 拆分 | 同上,§2.3.2 CUDA Compiler |
| 宿主编译器支持区间(Linux 下 GCC 6.x – 16.x 等)、CUDA 13.4 起 Toolkit 与驱动解耦 | CUDA Installation Guide for Linux |
| vLLM 要求 compute capability 7.5+、CUDA 13 需 R580+ 驱动等版本口径 | vLLM — GPU Installation |
PyTorch 扩展的默认编译行为(按可见卡的架构编译 + PTX)、TORCH_CUDA_ARCH_LIST 与 +PTX 后缀的语义与性能权衡、架构越多构建越慢、扩展里的 -rdc=true / -dc 与额外的 -dlink 步骤、-dlto 的作用 |
torch.utils.cpp_extension 官方文档 |