CUDA
Overview
CUDA(NVIDIA Compute Unified Device Architecture)는 NVIDIA GPU에서 일반 연산을 수행할 수 있는 병렬 컴퓨팅 플랫폼과 프로그래밍 모델입니다.
Summary
- 무엇인가? NVIDIA GPU 병렬 컴퓨팅 플랫폼 및 프로그래밍 모델
- 왜 필요한가? GPU의 대규모 병렬 처리 능력을 활용하여 AI/ML, 과학 계산, 영상 처리 가속
- 언제 사용하는가? 딥러닝 훈련/추론, 과학 시뮬레이션, 영상 처리, 암호화 등
---
Purpose
이 문서가 존재하는 이유
- Goal: CUDA 컴파일 방법, nvcc 플래그, PyTorch/TensorRT 설정, 호환성 정보 제공
- Scope: CUDA 컴파일 플래그, TORCH_CUDA_ARCH_LIST, CMake 설정, CUDA 호환성
- Non-goals: CUDA C++ 프로그래밍 상세, 커널 최적화 기법
---
Key Concepts
| Concept | Description | Related |
|---|---|---|
| CUDA | NVIDIA Compute Unified Device Architecture — GPU 병렬 컴퓨팅 플랫폼 | NVIDIA GPU |
| nvcc | NVIDIA CUDA 컴파일러 — CUDA 코드를 GPU 명령어로 컴파일 | CUDA |
| -arch | 컴파일 대상 GPU 아키텍처 지정 플래그 | CUDA |
| -gencode | 생성할 GPU 코드 지정 플래그 | CUDA |
| Compute Capability | GPU 아키텍처 버전 (sm_XX, compute_XX) | NVIDIA GPU |
| TORCH_CUDA_ARCH_LIST | PyTorch CUDA 아키텍처 환경 변수 | PyTorch |
| TensorRT | NVIDIA 추론 최적화 라이브러리 | TensorRT |
| CUTLASS | CUDA 템플릿 라이브러리 for Linear Algebra | CUDA |
---
Architecture
CUDA 컴파일 프로세스:
graph TD
A[CUDA 소스 코드] --> B[nvcc 컴파일러]
B --> C{arch 플래그}
C -->|지정됨| D[컴파일 시간 코드 생성]
C -->|미지정| E[JIT 컴파일러]
D --> F[최적화된 GPU 바이너리]
E --> F
F --> G[GPU 실행]---
Workflow
CUDA 컴파일 워크플로우:
- CUDA 코드 컴파일
- nvcc -arch=sm_XX -gencode=arch=compute_XX,code=sm_XX source.cu -o output
- PyTorch 설치 시 아키텍처 지정
- TORCH_CUDA_ARCH_LIST="7.0 7.5 8.0 8.6" python3 setup.py install
- TensorRT CMake 설정
- cmake <...> -DGPU_ARCHS="70"
---
Detailed Explanation
CUDA 컴파일 기본
CUDA 코드를 컴파일할 때 가장 많이 사용하는 GPU 카드와 일치하는 -arch 플래그 하나만 컴파일해야 합니다. 이렇게 하면 컴파일 시 코드 생성이 발생하여 런타임이 빨라집니다.
-gencode만 지정하고 -arch 플래그를 생략하면 CUDA 드라이버에 의해 JIT 컴파일러에서 GPU 코드 생성이 발생합니다.
CUDA 컴파일을 빠르게 하려면 관련 없는 -gencode 플래그의 양을 줄여야 합니다. 그러나 때로는 더 포괄적인 -gencode 플래그를 추가하여 더 나은 CUDA 후방 호환성을 원할 수 있습니다.
NVIDIA 설명: arch= 와 code=
NVIDIA에 따르면:
The
arch=clause of the-gencode=command-line option tonvccspecifies the front-end compilation target and must always be a PTX version. Thecode=clause specifies the back-end compilation target and can either be cubin or PTX or both. Only the back-end target version(s) specified by thecode=clause will be retained in the resulting binary; at least one must be PTX to provide Ampere compatibility.
nvcc gencode 및 arch 플래그 예제
CUDA 7.0 — 모든 카드 호환
-arch=sm_30 \
-gencode=arch=compute_20,code=sm_20 \
-gencode=arch=compute_30,code=sm_30 \
-gencode=arch=compute_50,code=sm_50 \
-gencode=arch=compute_52,code=sm_52 \
-gencode=arch=compute_52,code=compute_52
CUDA 8.1 — Volta 이전 카드 호환
-arch=sm_30 \
-gencode=arch=compute_20,code=sm_20 \
-gencode=arch=compute_30,code=sm_30 \
-gencode=arch=compute_50,code=sm_50 \
-gencode=arch=compute_52,code=sm_52 \
-gencode=arch=compute_60,code=sm_60 \
-gencode=arch=compute_61,code=sm_61 \
-gencode=arch=compute_61,code=compute_61
CUDA 9.2 — Volta 카드 호환
-arch=sm_50 \
-gencode=arch=compute_50,code=sm_50 \
-gencode=arch=compute_52,code=sm_52 \
-gencode=arch=compute_60,code=sm_60 \
-gencode=arch=compute_61,code=sm_61 \
-gencode=arch=compute_70,code=sm_70 \
-gencode=arch=compute_70,code=compute_70
CUDA 10.1 — V100 및 T4 Turing 카드 호환
-arch=sm_50 \
-gencode=arch=compute_50,code=sm_50 \
-gencode=arch=compute_52,code=sm_52 \
-gencode=arch=compute_60,code=sm_60 \
-gencode=arch=compute_61,code=sm_61 \
-gencode=arch=compute_70,code=sm_70 \
-gencode=arch=compute_75,code=sm_75 \
-gencode=arch=compute_75,code=compute_75
CUDA 11.0 — V100 및 T4 Turing 카드 호환
-arch=sm_52 \
-gencode=arch=compute_52,code=sm_52 \
-gencode=arch=compute_60,code=sm_60 \
-gencode=arch=compute_61,code=sm_61 \
-gencode=arch=compute_70,code=sm_70 \
-gencode=arch=compute_75,code=sm_75 \
-gencode=arch=compute_80,code=sm_80 \
-gencode=arch=compute_80,code=compute_80
CUDA 11.7 — V100, T4, RTX 3080, Drive AGX Orin 호환
-arch=sm_52 \
-gencode=arch=compute_52,code=sm_52 \
-gencode=arch=compute_60,code=sm_60 \
-gencode=arch=compute_61,code=sm_61 \
-gencode=arch=compute_70,code=sm_70 \
-gencode=arch=compute_75,code=sm_75 \
-gencode=arch=compute_80,code=sm_80 \
-gencode=arch=compute_86,code=sm_86 \
-gencode=arch=compute_87,code=sm_87 \
-gencode=arch=compute_86,code=compute_86
CUDA 11.4 — RTX 3080 최적 성능
-arch=sm_80 \
-gencode=arch=compute_80,code=sm_80 \
-gencode=arch=compute_86,code=sm_86 \
-gencode=arch=compute_87,code=sm_87 \
-gencode=arch=compute_86,code=compute_86
CUDA 12 — RTX 4080 최적 성능
-arch=sm_89 \
-gencode=arch=compute_89,code=sm_89 \
-gencode=arch=compute_89,code=compute_89
CUDA 12 (PTX ISA version 8.0) — H100 (Hopper) 최적 성능
-arch=sm_90 \
-gencode=arch=compute_90,code=sm_90 \
-gencode=arch=compute_90a,code=sm_90a \
-gencode=arch=compute_90a,code=compute_90a
Hopper GPU 후방 호환성 추가
-arch=sm_52 \
-gencode=arch=compute_52,code=sm_52 \
-gencode=arch=compute_60,code=sm_60 \
-gencode=arch=compute_61,code=sm_61 \
-gencode=arch=compute_70,code=sm_70 \
-gencode=arch=compute_75,code=sm_75 \
-gencode=arch=compute_80,code=sm_80 \
-gencode=arch=compute_86,code=sm_86 \
-gencode=arch=compute_87,code=sm_87 \
-gencode=arch=compute_90,code=sm_90 \
-gencode=arch=compute_90,code=compute_90
PyTorch에서 TORCH_CUDA_ARCH_LIST 사용
PyTorch를 사용하는 경우 설치 중 TORCH_CUDA_ARCH_LIST 환경 변수를 사용하여 아키텍처를 설정할 수 있습니다:
$ TORCH_CUDA_ARCH_LIST="7.0 7.5 8.0 8.6" python3 setup.py install
이 변수에 모든 아키텍처를 지정할 수 있지만, 각 아키텍처마다 커널을 컴파일해야 하므로 빌드 시간이 길어집니다.
가장 최근 아키텍처에 +PTX 접미사를 추가하여 새 카드와 전방 호환인 PTX 코드를 생성할 수도 있습니다:
$ TORCH_CUDA_ARCH_LIST="7.0 7.5 8.0 8.6+PTX" python3 build_my_extension.py
TensorRT CMake 사용
CMAKE로 TensorRT를 컴파일하는 경우 sm_ 및 compute_ 접두사를 제거하고 계산 능력만 참조합니다.
Tesla V100 및 Volta 카드:
cmake <...> -DGPU_ARCHS="70"
NVIDIA RTX 2070 및 Tesla T4:
cmake <...> -DGPU_ARCHS="75"
NVIDIA A100:
cmake <...> -DGPU_ARCHS="80"
NVIDIA RTX 3080 및 A100 함께:
cmake <...> -DGPU_ARCHS="80 86"
NVIDIA H100:
cmake <...> -DGPU_ARCHS="90"
CUTLASS with Hopper GH100 CMake
cmake .. -DCUTLASS_NVCC_ARCHS=90a
"Value 'sm_86' is not defined for option 'gpu-architecture'" 오류
이러한 오류가 발생하는 경우:
nvcc fatal : Value 'sm_86' is not defined for option 'gpu-architecture'
더 오래된 버전의 CUDA 및/또는 드라이버가 설치되어 있을 가능성이 높습니다. A100, RTX 3080과 같은 sm_8x 카드를 지원하려면 450.36.06 이상의 최신 드라이버로 업그레이드하세요.
CUDA 호환성
| CUDA Version | cuDNN Version | NCCL Version | NVIDIA GPU Driver Version | Compute Capability Support |
|---|---|---|---|---|
| CUDA 12.0 | — | — | — | — |
| CUDA 11.8 | — | — | — | — |
| CUDA 11.7 | — | — | — | — |
| CUDA 11.5 | 8.3.x | 2.10.x | 510.39 or later | Compute Capability 3.0 to 8.6 |
| CUDA 11.4 | 8.2.x | 2.10.x | 470.42.01 or later | Compute Capability 3.0 to 8.6 |
| CUDA 11.3 | 8.2.x | 2.10.x | 465.19.01 or later | Compute Capability 3.0 to 8.6 |
| CUDA 11.2 | 8.1.x | 2.9.x | 460.32.03 or later | Compute Capability 3.0 to 8.6 |
| CUDA 11.1 | 8.0.x | 2.9.x | 455.23.04 or later | Compute Capability 3.0 to 8.6 |
| CUDA 11.0 | 7.6.x | 2.8.x | 450.36.06 or later | Compute Capability 3.0 to 8.6 |
| CUDA 10.2 | 7.6.x | 2.7.x | 440.33 or later | Compute Capability 3.0 to 7.5 |
| CUDA 10.1 | 7.6.x | 2.4.x | 418.39 or later | Compute Capability 3.0 to 7.5 |
| CUDA 10.0 | 7.4.x | 2.2.x | 410.48 or later | Compute Capability 3.0 to 7.5 |
| CUDA 9.2 | 7.2.x | 2.1.x | 396.26 or later | Compute Capability 3.0 to 7.5 |
| CUDA 9.1 | 7.1.x | 2.0.x | 390.46 or later | Compute Capability 3.0 to 7.5 |
| CUDA 9.0 | 7.0.x | 1.3.x | 384.81 or later | Compute Capability 3.0 to 7.5 |
| CUDA 8.0 | 6.0.x | 1.3.x | 375.26 or later | Compute Capability 2.0 to 6.2 |
| CUDA 7.5 | 5.1.x | 1.3.x | 352.31 or later | Compute Capability 2.0 to 5.0 |
---
Configuration
nvcc 컴파일 플래그
# 기본 컴파일
nvcc -arch=sm_XX -gencode=arch=compute_XX,code=sm_XX source.cu -o output
# PyTorch 설치
TORCH_CUDA_ARCH_LIST="7.0 7.5 8.0 8.6" python3 setup.py install
# TensorRT CMake
cmake <...> -DGPU_ARCHS="70"
# CUTLASS CMake
cmake .. -DCUTLASS_NVCC_ARCHS=90a
---
Examples
Example 1: CUDA 11.7로 RTX 3080 컴파일
입력:
nvcc -arch=sm_80 \
-gencode=arch=compute_80,code=sm_80 \
-gencode=arch=compute_86,code=sm_86 \
-gencode=arch=compute_87,code=sm_87 \
-gencode=arch=compute_86,code=compute_86 \
source.cu -o output
Example 2: PyTorch 설치
입력:
TORCH_CUDA_ARCH_LIST="7.0 7.5 8.0 8.6+PTX" python3 setup.py install
---
Best Practices
- 가장 많이 사용하는 GPU 아키텍처만 -arch에 지정 — 불필요한 -gencode 플래그 제거로 컴파일 시간 단축
- PyTorch 설치 시 TORCH_CUDA_ARCH_LIST 사용 — 빌드 시간 단축
- +PTX 접미사 사용 — 새 카드와의 전방 호환성 확보
- CUDA 드라이버 최신 유지 — sm_8x 카드 지원에는 450.36.06 이상 필요
- TensorRT CMake 시 sm_/compute_ 접두사 제거 — 계산 능력만 지정
---
Limitations
- 각 아키텍처마다 커널을 컴파일하면 빌드 시간이 길어짐
- 구형 CUDA 버전은 새 GPU 아키텍처를 지원하지 않음
- CUDA 호환성은 드라이버 버전과 cuDNN/NCCL 버전과 밀접한 관계가 있음
- Hopper GPU(sm_90)는 CUDA 12 이상 필요
---
References
- NVIDIA nvcc 문서
---
Related Pages
---