cudatraining · 학습 기록

LESSON 08 · 2026.04.19 · L4

Triton vs CUDA — 추상화의 비용은 어디서 나타나는가

레슨 1–6 의 네 커널 (reduction, softmax, matmul, flash attention) 을 Triton 40–130 줄짜리로 재작성. L4 (sm_89) 에서 측정한 네 구간.

GPU · L4 (sm_89) stack · torch 2.6 + triton 3.2 ported · 4 kernels

하드웨어 업그레이드 — 왜 L4 인가

T4 는 FP16 WMMA 한 종류, TF32 없음. Triton tl.dot 가 TF32 를 자동 쓰는 걸 측정하려면 TF32 TC 가 있는 GPU 필요. L4 (Ada) = TF32 121 · FP16 242 · FP8 485 TFLOPS, L2 48 MB (T4 의 8×).

메모리 바운드 — 3 접근이 10% 안에서 동률

taskCUDAtorchTriton
Reduction 67M fp32258 GB/s254 GB/s245 GB/s
Softmax 4096² fp32237 GB/s240 GB/s221 GB/s

Triton 이 손 CUDA 의 93–95%. HBM 이 bottleneck 이면 추상화 비용은 증발한다.

컴퓨트 바운드 — Triton 이 cuBLAS 를 미세하게 이김

taskCUDAtorchTriton
matmul 4096³ FP32 (TF32)3.9 TF25.828.9
matmul 4096³ FP1618.5 TF51.854.0

우리 WMMA 대비 Triton 2.9×. cuBLAS 대비 FP32 +12%, FP16 +4%. autotune 이 사람 손 튜닝을 근소하게 넘은 지점.

Flash Attention — Triton fp16 이 polyglot

NCUDA FA fp32Triton fp32Triton fp16SDPA fp16
10240.3240.1480.1220.076
20480.6380.1960.1380.076
40961.2560.3580.2070.127
81923.0451.1180.4960.394

N=8192: Triton fp16 이 우리 CUDA FP32 대비 6.14×, SDPA (cuDNN FA-2) 의 79%. 100 줄짜리 Triton 이 cuDNN 의 80% 에 도달. Tri Dao 가 FA-2 를 Triton 으로 쓴 이유.

한 줄 = 수십 줄 이 네 번 반복

추상화의 비용이 실존하는 구간

구간Triton vs CUDA원인
작은 N (< 4 MB)3–12× 뒤짐Launch floor 50–100 µs (Python → autotune 캐시 → JIT → cuLaunch)
HBM 바운드95%거의 없음
큰 matmul/FA이김autotune 이 사람보다 나은 config 선택

실무: Transformer layer 가 ≥1 ms 면 100 µs 오버헤드 10% 미만 — 인내 가능. element-wise 30 개를 각각 Triton 런치하면 망함.

두 개의 footgun

(1) TF32 벤치 거짓말. torch.matmul(fp32) 는 기본 TF32 미사용. tl.dot 는 사용. 그대로 비교하면 "Triton 이 torch 를 2× 이김" 처럼 보임. 공정 비교엔:

torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True

(2) Autotune stale write. @triton.autotune 이 trial 중 output 버퍼에 오염된 write. reset_to_zero=["partial_ptr"] + 호출 후 best_config.kwargs["BLOCK_SIZE"] 로 slicing.

CUDA 왜 계속 배우나

  1. Triton 이 막히는 순간 CUDA 로 떨어짐 (Blackwell mma, persistent kernel, async copy).
  2. Triton 이 낸 PTX 를 읽을 줄 알아야 perf bug 추적.
  3. vLLM, FA-3, Mamba 는 여전히 CUDA.
  4. "왜 느림" 의 답이 bank conflict, register spill, occupancy — CUDA 개념.

CUDA = 어셈블리, Triton = C. 대부분은 C 로 짜고, 핫패스만 어셈블리.