Win11+RTX3090 亲测 · ComfyUI Hunyuan3D 全程实录 ②:nvdiffrast 源码编译实战(CUDA 13.1 零降级)

Win11+RTX3090 亲测 · ComfyUI Hunyuan3D 全程实录 ②:nvdiffrast 源码编译实战(CUDA 13.1 零降级)

环境:Windows 11 23H2 | Intel Ultra 9 285K | RTX 3090 24 GB | CUDA 13.1 | Python 3.12.11 | PyTorch 2.7.1+cu126 | VS2022 17.12

系列:全程实录第 ② 篇(第 ① 篇见依赖安装指南


一、前言:为什么必须本地编译

nvdiffrast

nvdiffrast 是 NVIDIA 官方的高性能可微渲染库,PyPI 仅提供 Linux wheel,Windows 用户必须:

  1. 克隆源码
  2. 本地编译 CUDA 扩展 _nvdiffrast_c.cp312-win_amd64.pyd

本文带你 零降级 CUDA 驱动,不改 PyTorch 版本,在 CUDA 13.1 + PyTorch 12.6 环境下一次编译通过。


二、环境 checklist

项目 本机示例 最低要求
OS Windows 11 专业工作站版 26H1 Win10 21H2+
CPU Intel® Core™ Ultra 9 285K (3.70 GHz) 64 bit
GPU NVIDIA GeForce RTX 3090 24 GB GTX 30 系列+
显卡驱动 及 CUDA 版本 595.02 / CUDA 13.1 ≥ 12.6
Python 3.12.11 64-bit 3.10-3.12
PyTorch 2.7.1+cu126 2.5.0+cu118+
VS Build Tools 17.12 / MSVC 14.44 2019/2022 任意

⚠️ 终端要求:开始菜单 → "x64 Native Tools Command Prompt for VS 2022" → 右键 以管理员身份运行(黑底黄字窗口才带 cl.exe/nvcc)


三、直接安装 vs 直接编译 vs 修改编译 对比

方式 命令 结果 日志片段
直接 pip pip install nvdiffrast ❌ 无 Windows wheel ERROR: Could not find a version that satisfies the requirement nvdiffrast
直接源码编译 python setup.py bdist_wheel ❌ CUDA 版本检查失败 RuntimeError: CUDA version (13.1) mismatches PyTorch (12.6)
修改后编译 python setup_patch.py bdist_wheel ✅ 生成可用 wheel creating 'dist\nvdiffrast-0.4.0-cp312-cp312-win_amd64.whl'

下文全程基于 第三种。



四、setup.py 修改细节(核心)

在 文件最顶部插入两行即可绕过版本检查:

python 复制代码
# 跳过 CUDA 驱动版本检查(必须放最前)
import torch.utils.cpp_extension as _ext
_ext._check_cuda_version = lambda *args, **kwargs: None

完整 setup_patch.py(已含 RTX30 架构优化):

python 复制代码
# Copyright (c) 2020, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.

import setuptools
import os

# 1. 强制跳过 Torch 内部 CUDA 驱动版本检查
import torch.utils.cpp_extension as _ext
_ext._check_cuda_version = lambda *args, **kwargs: None

# Print an error message if there's no PyTorch installed.
try:
    from torch.utils.cpp_extension import BuildExtension, CUDAExtension
except ImportError:
    print("\n\n" + "*" * 70)
    print("ERROR! Cannot compile nvdiffrast CUDA extension. Please ensure that:\n")
    print("1. You have PyTorch installed")
    print("2. You run 'pip install' with --no-build-isolation flag")
    print("*" * 70 + "\n\n")
    exit(1)

setuptools.setup(
    ext_modules=[
        CUDAExtension(
            "_nvdiffrast_c",
            sources=[
                "csrc/common/antialias.cu",
                "csrc/common/common.cpp",
                "csrc/common/cudaraster/impl/Buffer.cpp",
                "csrc/common/cudaraster/impl/CudaRaster.cpp",
                "csrc/common/cudaraster/impl/RasterImpl.cpp",
                "csrc/common/cudaraster/impl/RasterImpl_kernel.cu",
                "csrc/common/interpolate.cu",
                "csrc/common/rasterize.cu",
                "csrc/common/texture.cpp",
                "csrc/common/texture_kernel.cu",
                "csrc/torch/torch_antialias.cpp",
                "csrc/torch/torch_bindings.cpp",
                "csrc/torch/torch_interpolate.cpp",
                "csrc/torch/torch_rasterize.cpp",
                "csrc/torch/torch_texture.cpp",
            ],
            extra_compile_args={
                "cxx": ["-DNVDR_TORCH"]
                # Disable warnings in torch headers.
                + (["/wd4067", "/wd4624", "/wd4996"] if os.name == "nt" else []),
                "nvcc": ["-DNVDR_TORCH", "-lineinfo"],
            },
        )
    ],
    cmdclass={"build_ext": BuildExtension},
)

五、编译成功现场(日志截图)

复制代码
# 运行编译命令
python setup.py bdist_wheel
log 复制代码
[91/91] Linking build\lib.win-amd64-cpython-312\_nvdiffrast_c.cp312-win_amd64.pyd
creating 'dist\nvdiffrast-0.4.0-cp312-cp312-win_amd64.whl'
Successfully installed nvdiffrast-0.4.0

六、安装与验证

bash 复制代码
pip install dist\nvdiffrast-0.4.0-cp312-cp312-win_amd64.whl
python -c "import nvdiffrast.torch as nvr; print('✅', nvr.__file__)"

输出示例:

复制代码
✅ H:\YourComfyUI\.venv\Lib\site-packages\nvdiffrast\torch\__init__.py

七、常见报错对照表(收藏级)

报错关键词 原因 一键修复
-CUDA version (13.1) mismatches PyTorch (12.6)- -驱动 vs 编译版本检查- -本文 MonkeyPatch -
-cl.exe not found- -未用 VS2022 x64 终端- -开始菜单 → x64 Native Tools-
-nvcc fatal : Unsupported gpu architecture 'compute_90'- -CUDA 13 默认架构过高- -已在 setup_patch.py 指定 compute_86-
-MSVC/cl.exe with traditional preprocessor is used- -仅警告,可忽略- -加 /WX- 不视为错误-


八、一键脚本(失败→成功全流程)

保存为 build_nvdiffrast.bat,右键管理员运行"x64 Native Tools Command Prompt for VS 2022"然后运行此脚本即可复现全文所有步骤:

bat 复制代码
@echo off
title nvdiffrast-Windows-Build
cd /d "%~dp0"
call .venv\Scripts\activate
echo [1/4] 克隆源码...
git clone https://github.com/NVlabs/nvdiffrast.git
cd nvdiffrast
echo [2/4] 应用 MonkeyPatch...
copy setup.py setup.py.bak
python -c "import torch.utils.cpp_extension as _ext;_ext._check_cuda_version=lambda *a,**k:None" setup.py bdist_wheel
echo [3/4] 安装 wheel...
pip install dist\nvdiffrast-0.4.0-cp312-cp312-win_amd64.whl
echo [4/4] 验证...
python -c "import nvdiffrast.torch as nvr;print('✅',nvr.__file__)"
pause

九、系列交叉引用


转载注明出处 → 博客标题 + 链接即可。

ComfyUI, Hunyuan3D, nvdiffrast, CUDA13.1, PyTorch12.6, 源码编译, setup.py, MonkeyPatch, RTX3090, Windows11

相关推荐
小雨下雨的雨1 小时前
井字棋AI机器人实现详解 - Minimax算法实战-鸿蒙PC Electron框架完成
前端·人工智能·算法·华为·electron·鸿蒙
我没胡说八道3 小时前
高校论文AI检测优化工具对比研究与实测分析(2026)
人工智能·深度学习·机器学习·计算机视觉·aigc·论文
秦亚伟4 小时前
AI浪潮重塑融资租赁行业新格局
人工智能
love530love4 小时前
LiveTalking 数字人项目 Windows 部署完全指南(EPGF 架构)
人工智能·windows·python·架构·livetalking·epgf
元启数宇4 小时前
喷淋AI布点实战:8小时人工布点→20分钟自动出图
人工智能
哈哈,柳暗花明4 小时前
人工智能专业术语详解(H)
人工智能·专业术语
圣殿骑士-Khtangc4 小时前
AI 编程工具 2026 实战横评:Cursor 3 vs Claude Code vs Copilot,开发者选型完全指南
人工智能·copilot
云器科技4 小时前
云器Lakehouse 2026年5月版本发布:拥抱 AI Agent,重塑数据智能开发新范式
人工智能
小鹰-上海鹰谷-电子实验记录本4 小时前
第六届党建引领科创生态座谈会 | 邓光辉博士出席分享AI赋能创新药科研新范式
人工智能·ai·电子实验记录本·药企合规
遇事不決洛必達4 小时前
【Python基础】GIL 锁是什么及其对爬虫的影响
爬虫·python·线程·进程·gil锁