Loading...
Loading...
Compare original and translation side by side
references/architecture.mdreferences/optimizers.mdreferences/domain-specific.mdreferences/scaling-and-selection.mdreferences/biomedical.mdreferences/experiment-loop.mdreferences/architecture.mdreferences/optimizers.mdreferences/domain-specific.mdreferences/scaling-and-selection.mdreferences/biomedical.mdreferences/experiment-loop.md| Data Type | < 10K samples | 10K-100K | > 100K |
|---|---|---|---|
| Images | Pretrained CNN + fine-tune | Fine-tune ViT or CNN | ViT from scratch |
| Text (gen) | Few-shot prompting | Fine-tune GPT/LLaMA (LoRA) | Pretrain from scratch |
| Tabular | XGBoost/LightGBM | Still XGBoost | Neural viable |
| Audio | Pretrained Whisper | Fine-tune AST | Train from scratch |
| Molecules | Pretrained GNN | Fine-tune molecular LM | Train GNN from scratch |
| Proteins | ESM-2 embeddings + head | Fine-tune ESM-2 | Train protein LM |
| Medical img | Pretrained CNN | nnU-Net (auto-config) | Swin-UNETR / MedSAM |
references/biomedical.mdreferences/scaling-and-selection.md| 数据类型 | 少于10K样本 | 10K-100K样本 | 超过100K样本 |
|---|---|---|---|
| 图像 | 预训练CNN + 微调 | 微调ViT或CNN | 从头训练ViT |
| 生成式文本 | 少样本提示 | 微调GPT/LLaMA(LoRA) | 从头预训练 |
| 表格数据 | XGBoost/LightGBM | 仍使用XGBoost | 可尝试神经网络 |
| 音频 | 预训练Whisper | 微调AST | 从头训练 |
| 分子 | 预训练GNN | 微调分子语言模型 | 从头训练GNN |
| 蛋白质 | ESM-2嵌入 + 分类头 | 微调ESM-2 | 训练蛋白质语言模型 |
| 医学影像 | 预训练CNN | nnU-Net(自动配置) | Swin-UNETR / MedSAM |
references/biomedical.mdreferences/scaling-and-selection.md| Model Size | Compute-Optimal | Inference-Optimal (100×) |
|---|---|---|
| 125M | 2.5B tokens | 12.5B tokens |
| 1B | 20B tokens | 100B tokens |
| 7B | 140B tokens | 700B tokens |
| 模型规模 | 算力最优token量 | 推理最优token量(100×) |
|---|---|---|
| 125M | 25亿 | 125亿 |
| 1B | 200亿 | 1000亿 |
| 7B | 1400亿 | 7000亿 |
import gc, time, torch
torch.manual_seed(42)
torch.set_float32_matmul_precision("high") # TF32 on Ampere+
autocast_ctx = torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16)
grad_accum_steps = total_batch_size // (batch_size * seq_len)
step = 0
while not done:
t0 = time.time()
for micro_step in range(grad_accum_steps):
with autocast_ctx:
loss = model(x, y)
(loss / grad_accum_steps).backward()
x, y = next(train_loader)
update_lr(optimizer, progress)
optimizer.step()
model.zero_grad(set_to_none=True) # frees memory vs zeroing
if loss.item() > 100: # fast-fail on divergence
print("FAIL: loss exploded"); exit(1)
torch.cuda.synchronize()
if step == 0:
gc.collect(); gc.freeze(); gc.disable() # avoid ~500ms GC stalls
step += 1import gc, time, torch
torch.manual_seed(42)
torch.set_float32_matmul_precision("high") # TF32 on Ampere+
autocast_ctx = torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16)
grad_accum_steps = total_batch_size // (batch_size * seq_len)
step = 0
while not done:
t0 = time.time()
for micro_step in range(grad_accum_steps):
with autocast_ctx:
loss = model(x, y)
(loss / grad_accum_steps).backward()
x, y = next(train_loader)
update_lr(optimizer, progress)
optimizer.step()
model.zero_grad(set_to_none=True) # frees memory vs zeroing
if loss.item() > 100: # fast-fail on divergence
print("FAIL: loss exploded"); exit(1)
torch.cuda.synchronize()
if step == 0:
gc.collect(); gc.freeze(); gc.disable() # avoid ~500ms GC stalls
step += 1clip_grad_norm_(params, 1.0)cudnn.benchmark = Trueclip_grad_norm_(params, 1.0)cudnn.benchmark = True| Parameter Type | Optimizer | LR (base) | Weight Decay |
|---|---|---|---|
| 2D weight matrices | Muon | 0.04 | 0.2 |
| Token embeddings | AdamW | 0.6 × scale | 0.0 |
| Unembedding (lm_head) | AdamW | 0.004 × scale | 0.0 |
| Per-layer scalars | AdamW | 0.005 × scale | 0.0 |
lr * (d_model / 768)^(-0.5)| 参数类型 | 优化器 | 基础学习率 | 权重衰减 |
|---|---|---|---|
| 2D权重矩阵 | Muon | 0.04 | 0.2 |
| Token嵌入 | AdamW | 0.6 × 缩放系数 | 0.0 |
| 反嵌入层(lm_head) | AdamW | 0.004 × 缩放系数 | 0.0 |
| 每层标量参数 | AdamW | 0.005 × 缩放系数 | 0.0 |
lr * (d_model / 768)^(-0.5)references/optimizers.mdreferences/optimizers.mddef get_lr_multiplier(progress): # progress = elapsed_time / time_budget
if progress < warmup_ratio:
return progress / warmup_ratio
elif progress < 1.0 - warmdown_ratio:
return 1.0
else:
cooldown = (1.0 - progress) / warmdown_ratio
return cooldown + (1 - cooldown) * final_lr_fracdef get_lr_multiplier(progress): # progress = 已用时间 / 时间预算
if progress < warmup_ratio:
return progress / warmup_ratio
elif progress < 1.0 - warmdown_ratio:
return 1.0
else:
cooldown = (1.0 - progress) / warmdown_ratio
return cooldown + (1 - cooldown) * final_lr_fracdef get_lr(step, total_steps, max_lr, min_lr, warmup_steps):
if step < warmup_steps:
return max_lr * step / warmup_steps
progress = (step - warmup_steps) / (total_steps - warmup_steps)
return min_lr + 0.5 * (max_lr - min_lr) * (1 + math.cos(math.pi * progress))def get_lr(step, total_steps, max_lr, min_lr, warmup_steps):
if step < warmup_steps:
return max_lr * step / warmup_steps
progress = (step - warmup_steps) / (total_steps - warmup_steps)
return min_lr + 0.5 * (max_lr - min_lr) * (1 + math.cos(math.pi * progress))WARMUP_RATIO=0.0WARMUP_RATIO=0.0import os
os.environ["PYTORCH_ALLOC_CONF"] = "expandable_segments:True" # before torch import
import torch
torch.set_float32_matmul_precision("high")
autocast_ctx = torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16)
model = torch.compile(model, dynamic=False)dynamic=Falsefullgraph=Trueimport os
os.environ["PYTORCH_ALLOC_CONF"] = "expandable_segments:True" # before torch import
import torch
torch.set_float32_matmul_precision("high")
autocast_ctx = torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16)
model = torch.compile(model, dynamic=False)dynamic=Falsefullgraph=Truewith torch.device("meta"):
model = GPT(config) # zero memory
model.to_empty(device="cuda")
model.init_weights()with torch.device("meta"):
model = GPT(config) # 零内存占用
model.to_empty(device="cuda")
model.init_weights()achieved_flops = model_flops_per_token * batch_tokens / step_time
mfu = achieved_flops / gpu_peak_flopsachieved_flops = model_flops_per_token * batch_tokens / step_time
mfu = achieved_flops / gpu_peak_flops
Good targets: >30% decent, >40% good, >50% excellent (single-GPU).
优秀目标:单GPU场景下,>30%为良好,>40%为优秀,>50%为极佳。DEVICE_BATCH_SIZEgrad_accum_stepsPYTORCH_ALLOC_CONF=expandable_segments:Truemodel.zero_grad(set_to_none=True)to_emptytorch.utils.checkpoint.checkpoint()DEVICE_BATCH_SIZEgrad_accum_stepsPYTORCH_ALLOC_CONF=expandable_segments:Truemodel.zero_grad(set_to_none=True)to_emptytorch.utils.checkpoint.checkpoint()| Setting | Value |
|---|---|
| Optimizer | AdamW (β1=0.9, β2=0.95, eps=1e-10) |
| Weight decay | 0.1 |
| LR schedule | Cosine decay or WSD |
| Peak LR | 3e-4 (scale down for larger models) |
| Precision | bf16 |
| Grad clipping | max_norm=1.0 |
| Normalization | RMSNorm (pre-norm) |
| Activation | SwiGLU |
| Position encoding | RoPE |
| Attention | Flash Attention, optionally GQA |
| 设置项 | 值 |
|---|---|
| 优化器 | AdamW (β1=0.9, β2=0.95, eps=1e-10) |
| 权重衰减 | 0.1 |
| 学习率调度 | 余弦衰减或WSD |
| 峰值学习率 | 3e-4(模型规模越大,取值越小) |
| 精度 | bf16 |
| 梯度裁剪 | max_norm=1.0 |
| 归一化 | RMSNorm(前置归一化) |
| 激活函数 | SwiGLU |
| 位置编码 | RoPE |
| 注意力机制 | Flash Attention,可选GQA |
clip_grad_norm_(params, 1.0)softcap * tanh(logits / softcap)loss / grad_accum_stepsclip_grad_norm_(params, 1.0)softcap * tanh(logits / softcap)loss / grad_accum_stepstorch.compiletorch.set_float32_matmul_precision("high")torch.profilergc.freeze(); gc.disable()torch.compiletorch.set_float32_matmul_precision("high")torch.profilergc.freeze(); gc.disable()commit val_bpb memory_gb status description
a1b2c3d 0.9979 44.0 keep baseline
b2c3d4e 0.9932 44.2 keep increase matrix LR to 0.04
c3d4e5f 1.0050 44.0 discard switch to GeLU (worse)references/experiment-loop.mdcommit val_bpb memory_gb status description
a1b2c3d 0.9979 44.0 keep baseline
b2c3d4e 0.9932 44.2 keep increase matrix LR to 0.04
c3d4e5f 1.0050 44.0 discard switch to GeLU (worse)references/experiment-loop.md| Domain | Primary Metric | Notes |
|---|---|---|
| LLM | BPB (bits per byte) | Vocab-size-independent |
| Classification | Accuracy / F1 | Macro-F1 for imbalanced |
| Segmentation | mIoU / Dice | Per-class IoU reveals weak spots |
| Generation | FID | Needs >10k samples |
| Regression | RMSE / MAE | Log-transform skewed targets |
| 领域 | 主要指标 | 说明 |
|---|---|---|
| LLM | BPB(bits per byte) | 与词汇表大小无关 |
| 分类任务 | 准确率/F1 | 不平衡数据集使用Macro-F1 |
| 分割任务 | mIoU/Dice | 按类别IoU可发现薄弱环节 |
| 生成任务 | FID | 需要超过10K样本 |
| 回归任务 | RMSE/MAE | 对偏斜目标进行对数变换 |