利用 NVIDIA Transformer Engine、融合內核、BF16、FP8 與 GPU 基準測試加速 Transformer 訓練
重點摘要
在本教學中,我們探討 NVIDIA Transformer Engine 如何透過結合融合 GPU 內核、BF16 計算與硬體感知的 FP8 執行來加速 Transformer 工作負載。我們首先安裝 Transformer Engine 並檢測活躍的 GPU 架構,以判斷執行環境是否支援 TE 內核、FP8 張量核心,或僅能使用純 PyTorch 回退路徑。接著,我們檢視核心融合元件,如 te.Linear、te.LayerNorm、te.LayerNormLinear、te.LayerNormMLP 和 te.TransformerLayer,同時配置一個延遲縮放 FP8 配方,以管理張量縮放、amax 歷史記錄與混合 E4M3/E5M2 格式。利用這些元件,我們建構一個精簡的 GPT 風格因果語言模型,在確定性的合成序列上進行訓練,並比較高精度與 FP8 的效能差異。
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 PyTorch fallback for unsupported hardware. Copy CodeCopiedUse a different BrowserVOCAB, D_MODEL, N_HEADS, N_LAYERS, FFN, SEQ = 96, 768, 12, 4, 3072, 256 class MiniGPT_TE(nn.Module): """Causal LM where every block is a single fused te.TransformerLayer.""" def __init__(self): super().__init__() self.emb = nn.Embedding(VOCAB, D_MODEL) self.pos = nn.Embedding(SEQ, D_MODEL) self.blocks = nn.ModuleList([ te.TransformerLayer( hidden_size=D_MODEL, ffn_hidden_size=FFN, num_attention_heads=N_HEADS, self_attn_mask_type="causal", layer_number=i + 1, params_dtype=torch.bfloat16, hidden_dropout=0.0, attention_dropout=0.0, ) for i in range(N_LAYERS) ]) self.ln_f = nn.LayerNorm(D_MODEL) self.head = nn.Linear(D_MODEL, VOCAB, bias=False) def forward(self, idx): B, T = idx.shape h = self.emb(idx) + self.pos(torch.arange(T, device=idx.device)) h = h.to(torch.bfloat16) for blk in self.blocks: h = blk(h) h = self.ln_f(h.float()) return self.head(h) class Block_PT(nn.Module): """Plain-PyTorch transformer block, mirrors te.TransformerLayer.""" def __init__(self): super().__init__() self.ln1 = nn.LayerNorm(D_MODEL) self.attn = nn.MultiheadAttention(D_MODEL, N_HEADS, batch_first=True) self.ln2 = nn.LayerNorm(D_MODEL) self.mlp = nn.Sequential(nn.Linear(D_MODEL, FFN), nn.GELU(), nn.Linear(FFN, D_MODEL)) def forward(self, x, mask): a, _ = self.attn(self.ln1(x), self.ln1(x), self.ln1(x), attn_mask=mask, need_weights=False) x = x + a return x + self.mlp(self.ln2(x)) class MiniGPT_PT(nn.Module): def __init__(self): super().__init__() self.emb = nn.Embedding(VOCAB, D_MODEL) self.pos = nn.Embedding(SEQ, D_MODEL) self.blocks = nn.ModuleList([Block_PT() for _ in range(N_LAYERS)]) self.ln_f = nn.LayerNorm(D_MODEL) self.head = nn.Linear(D_MODEL, VOCAB, bias=False) def forward(self, idx): B, T = idx.shape mask = torch.triu(torch.full((T, T), float("-inf"), device=idx.device), diagonal=1) h = self.emb(idx) + self.pos(torch.arange(T, device=idx.device)) for blk in self.blocks: h = blk(h, mask) return self.head(self.ln_f(h)) model = (MiniGPT_TE() if TE_CAPABLE else MiniGPT_PT()).to(DEVICE) n_params = sum(p.numel() for p in model.parameters()) print(f"\n>> Model: {'TE fused' if TE_CAPABLE else 'pure PyTorch'} | " f"{n_params/1e6:.1f}M params | {N_LAYERS} layers x {D_MODEL}d") We define a compact causal language model using fused te.TransformerLayer blocks for Transformer Engine execution. We also implement an equivalent pure-PyTorch transformer architecture with multi-head attention, layer normalization, residual connections, and feed-forward networks. We select the appropriate model dynamically according to GPU support and report the final parameter count and architectural dimensions. Copy CodeCopiedUse a different Browserdef make_batch(bsz=16): phase = torch.randint(0, VOCAB, (bsz, 1)) stride = torch.randint(1, 7, (bsz, 1)) steps = torch.arange(SEQ + 1).unsqueeze(0) seq = (phase + stride * steps) % VOCAB return seq[:, :-1].to(DEVICE), seq[:, 1:].to(DEVICE) opt = torch.optim.AdamW(model.parameters(), lr=3e-4) def run_step(x, y, use_fp8): if TE_CAPABLE and use_fp8: with te.fp8_autocast(enabled=True, fp8_recipe=fp8_recipe): logits = model(x) else: logits = model(x) loss = F.cross_entropy(logits.float().reshape(-1, VOCAB), y.reshape(-1)) opt.zero_grad(set_to_none=True) loss.backward() opt.step() return loss.item() print(f"\n>> Training 60 steps ({'FP8' if FP8_CAPABLE else 'BF16/FP32'})...") t0 = time.time() for step in range(1, 61): x, y = make_batch() loss = run_step(x, y, use_fp8=FP8_CAPABLE) if step % 10 == 0: print(f" step {step:3d} | loss {loss:.4f} | " f"{(time.time()-t0)/step*1000:.0f} ms/step") print(f">> Final loss: {loss:.4f} (random guess would be ~{math.log(VOCAB):.2f})") We create deterministic arithmetic-pattern sequences that allow the model to learn predictable token transitions across the vocabulary. We configure the AdamW optimizer and implement a training step that conditionally wraps the forward pass in te.fp8_autocast when FP8 execution is supported. We train the model for multiple iterations, monitor the loss and step latency, and compare the final loss against the random-guess baseline. Copy CodeCopiedUse a different Browserdef bench(use_fp8, iters=30, warmup=10): x, y = make_batch(bsz=32) for _ in range(warmup): r
Related
相關文章
AMD Releases Instella-MoE-16B-A3B: A Fully Open Mixture-of-Experts LLM With 2.8B Active Parameters Trained On Instinct GPUs
AMD 發布 Instella-MoE-16B-A3B,一個完全開放的混合專家語言模型,總參數 160 億但每次僅啟用 28 億,在 Instinct MI300X/MI325X GPU 上從頭訓練。該模型基線平均分數 76.7,領先其他完全開源的同級模型,但權重採用 ResearchRAIL 授權僅限學術與研究用途,訓練程式碼則以 MIT 授權發布。
Supabase Releases Evals: an Open Source Benchmark That Scores Claude Code, Codex and OpenCode on Real Supabase Tasks
Supabase has open sourced Supabase Evals, its benchmark and framework for testing how well AI agents build using Supabase.
深度求索發佈閃電模型更新
Ad Skip to content All Topics AI and society AI in practice AI research Frontier Radar Short News Read full article about: Google Deepmind unveils Gemini Robotics 2 to power robots of all shapes from tabletop arms to humanoids Google Deepmind has introduced Gemini Robotics 2, which it calls its most advanced vision-language-action (VLA) model yet. VLA models combine image recognition, language processing, and action control to help robots operate in physical environments. Deepmind says the model can control systems ranging from tabletop arms to full-body humanoid robots. The company describes Gemini Robotics 2 as an "intelligence layer" for a new generation of adaptive robots. It can manage full-body movement, perform fine motor tasks, and coordinate multiple robots, according to Deepmind.
潛推理推薦模型實現大幅提速
研究團隊提出潛在推理推薦框架WhisperRec,將教師生成的鏈式思考壓縮為可學習的潛在推理token,避免顯式推理的延遲瓶頸。實驗顯示,WhisperRec在工業級快手資料集與公開基準上,其SID@64指標較顯式CoT方法提升17.44%,且線上推論吞吐量提升超過10倍。
新型控制器優化大模型搜索成本
大型語言模型的應用場景持續擴張,除了聊天與程式生成之外,愈來愈多科學與演算法研究開始借助模型在推論階段進行自動化搜索,從大量候選結果中尋找最佳解答。然而,這類搜索過程往往需要消耗大量運算資源,尤其在不同提示長度、重試次數與引導呼叫的組合下,每次行動所付出的 token 成本並不相同,讓成本控管成為實際部署時的重要挑戰。一篇近期發表於 arXiv 的研究論文,針對此問題提出了一套全新的控制器機制,試圖在有限預算下,讓模型搜索既能維持品質,又不讓成本失控。
前特斯拉大牛探討編程範式轉變
前特斯拉大牛探討編程範式轉變。 專家聲稱傳統編程正被大模型徹底顛覆。用戶在紅迪討論貼中可細讀現場觀點。他認為上下文窗口已成了新的控制槓桿。很多開發者仍用老標準衡量自身價值。這一趨勢讓不少資深程序員 ��� 感到焦慮。