Daily Tech Briefing
AI 科技速览
每天 5 分钟内学习 AI。获取最新的人工智能新闻,理解其重要性,并学习如何将其应用于您的工作。
MarkTechPost · 2026/8/1 18:31:15
Accelerating Transformer Training with NVIDIA Transformer Engine, Fused Kernels, BF16, FP8, and GPU Benchmarking
AI 中文解读
核心亮点:NVIDIA发布了一款“引擎”级加速工具,能让AI大模型的训练速度大幅提升,还支持最新的FP8低精度计算,堪称AI训练界的“涡轮增压器”。
通俗解读:训练像GPT这样的AI大模型,就像做一道超级复杂的菜,传统方法要一锅一锅慢慢炖。NVIDIA的Transformer Engine相当于给厨房装上了“高压锅”和“流水线”,把切菜、炒菜、调味等步骤合并起来同时完成,还改用了一种更省食材的“轻量调料”(FP8/BF16),让AI在几乎不损失“口味”的前提下,做菜更快、占用的灶台(显存)也更少。这套工具还特别“聪明”,会自动检查你的GPU型号,老显卡也能用,只是效果差一点。
实际影响:普通人用AI工具时,响应速度会更快,比如用ChatGPT写文章、生成图片,等待时间缩短。更重要的是,训练成本降低后,更多中小企业和开发者能“玩得起”大模型,未来催生更多实用的AI应用,比如手机上的AI助手、智能客服,都会变得更流畅、更便宜。
In this tutorial, we explore how NVIDIA Transformer Engine accelerates transformer workloads by combining fused GPU kernels, BF16 computation, and hardware-aware FP8 execution. We begin by installing Transformer Engine and detecting the active GPU architecture so that we can determine whether the runtime supports TE kernels, FP8 tensor cores, or only the pure-PyTorch fallback path. We then examine core fused components such as te.Linear, te.LayerNorm, te.LayerNormLinear, te.LayerNormMLP, and te.TransformerLayer, while also configuring a delayed-scaling FP8 recipe that manages tensor scaling, amax history, and hybrid E4M3/E5M2 formats. Using these components, we construct a compact GPT-style causal language model, train it on deterministic synthetic sequences, compare higher-precision and FP8 execution, measure runtime and peak GPU memory, inspect FP8 metadata, and validate the trained model through autoregressive generation.
Copy CodeCopiedUse a different Browserimport subprocess, sys, os
def pip_install(*pkgs):
subprocess.run([sys.executable, "-m", "pip", "install", "-q",
"--no-build-isolation", *pkgs], check=False)
print(">> Installing transformer_engine[pytorch] (this can take a few minutes)...")
pip_install("transformer_engine[pytorch]")
import time, math, gc
import torch
import torch.nn as nn
import torch.nn.functional as F
assert torch.cuda.is_available(), "Enable a GPU runtime in Colab first!"
DEVICE = "cuda"
props = torch.cuda.get_device_properties(0)
CC = (props.major, props.minor)
GPU_NAME = props.name
print(f">> GPU: {GPU_NAME} | compute capability {CC[0]}.{CC[1]} | "
f"{props.total_memory/1e9:.1f} GB")
TE_CAPABLE = CC >= (8, 0)
FP8_CAPABLE = CC >= (8, 9)
te = None
if TE_CAPABLE:
try:
import transformer_engine.pytorch as te
from transformer_engine.common import recipe
print(">> Transformer Engine imported OK:",
getattr(te, "__version__", "unknown version"))
except Exception as e:
print(f">> TE import failed ({e}); using pure-PyTorch fallback.")
TE_CAPABLE = FP8_CAPABLE = False
else:
print(">> GPU is pre-Ampere (e.g. T4): TE kernels unsupported -> fallback mode.")
if TE_CAPABLE and FP8_CAPABLE and te is not None:
try:
ok, reason = te.fp8.check_fp8_support()
FP8_CAPABLE = bool(ok)
if not ok:
print(">> TE reports FP8 unsupported:", reason)
except Exception:
pass
print(f">> Mode: TE={'ON' if TE_CAPABLE else 'OFF'} | "
f"FP8={'ON' if FP8_CAPABLE else 'OFF (will use BF16)'}")
torch.manual_seed(1234)
if TE_CAPABLE:
H = 768
x_demo = torch.randn(8, 32, H, device=DEVICE, dtype=torch.bfloat16)
lin = te.Linear(H, H, bias=True, params_dtype=torch.bfloat16).to(DEVICE)
ln = te.LayerNorm(H, params_dtype=torch.bfloat16).to(DEVICE)
ln_lin = te.LayerNormLinear(H, 3 * H, params_dtype=torch.bfloat16).to(DEVICE)
ln_mlp = te.LayerNormMLP(H, 4 * H, params_dtype=torch.bfloat16).to(DEVICE)
with torch.no_grad():
print("\n>> Module tour (shapes):")
print(" te.Linear ", tuple(lin(x_demo).shape))
print(" te.LayerNorm ", tuple(ln(x_demo).shape))
print(" te.LayerNormLinear", tuple(ln_lin(x_demo).shape))
print(" te.LayerNormMLP ", tuple(ln_mlp(x_demo).shape))
del lin, ln, ln_lin, ln_mlp, x_demo
gc.collect(); torch.cuda.empty_cache()
fp8_recipe = None
if FP8_CAPABLE:
fp8_recipe = recipe.DelayedScaling(
fp8_format=recipe.Format.HYBRID,
amax_history_len=16,
amax_compute_algo="max",
)
print("\n>> FP8 recipe:", fp8_recipe)
We install NVIDIA Transformer Engine and initialize the PyTorch environment required for GPU-accelerated execution. We inspect the active GPU, compute capability, and memory capacity to determine whether fused TE kernels and FP8 tensor cores are available. We also validate the core fused modules and configure a delayed-scaling FP8 recipe while preserving an automatic
分享
阅读原文 ↗