Skip to content

【训练营】小模型训练支持 - #225

Open
ShenYouSOTA wants to merge 5 commits into
InfiniTensor:masterfrom
ShenYouSOTA:work/small-model-cnn-impl
Open

ShenYouSOTA wants to merge 5 commits into
InfiniTensor:masterfrom
ShenYouSOTA:work/small-model-cnn-impl

Conversation

@ShenYouSOTA

@ShenYouSOTA ShenYouSOTA commented Sep 19, 2026

Copy link
Copy Markdown

Summary

MNIST CNN 两层卷积训练支持:Conv2d / ReLU(CPU direct 基准 + CUDA im2col+GEMM,双后端互为 cross-check)、--model cnn|mlp(默认 mlp 不变)、infini_run 多进程 DDP(Broadcast-before-wrap、分片 eval + epoch 级 AllReduce SUM)。

Scope

  • Conv2d v1:FP32/NCHW-OIHW,标量 kernel/stride/padding API,内部 Kh/Kw 独立;Conv2dMeta 集中 shape 算术;selective-save autograd(三个独立 backward op)
  • CPU:direct reference 实现;CUDA:im2col + 框架 GEMM(gather col2im,cub bias reduction)
  • DataLoader shape-preserving Stack([B,*sample_dims],MNIST 样本 [1,28,28]);MNIST stride bug 修复(FLOAT32 步长)
  • 训练循环接线:ZeroGrad → Forward(DDP bucket-view 要求,与 gpt2/mixtral/llama3 对齐)、Module::operator() 调用、eval NoGradGuard

Verification

  • CPU finite-difference gradcheck + torch golden parity;CUDA forward/dW/dX/db 网格 ≤1e-5;网络级 parity 140/140
  • DDP 等价(RTX 4090D ×2,CNN,SGD lr=0.01,3 epoch,1×128 vs 2×64):Step 后 cross-rank 权重一致(post-epoch w0 两 rank 均为 -0.001326);单卡基线 bit 无回归
  • 双卡机环境:CUDA 12.8、NCCL 2.25.1(系统)、cmake 3.31、gcc13,USE_NCCL=ON 一次编过;MNIST 官方数据
  • 单卡 smoke:CNN cuda acc 0.9028;全量 ctest 354/355(唯一失败为 HEAD 预存 glog stacktrace 环境项)

细节见分支内 docs/small_model_patch.md §7。

ShenYouSOTA and others added 5 commits September 18, 2026 15:39
…MNIST demo, DDP)

- Conv2d v1: FP32/NCHW-OIHW, scalar kernel/stride/padding API, Kh/Kw-independent
  internals; Conv2dMeta centralizes shape arithmetic; selective-save autograd
- Backends: CPU direct reference + CUDA im2col + framework GEMM
  (gather col2im, cub bias reduction)
- DataLoader shape-preserving Stack ([B,*sample_dims]); MNIST sample [1,28,28];
  stride bug fix; MLP Flatten(1) adaptation
- MNIST: MnistCnn + --model (default mlp unchanged); shared_ptr migration for
  enable_shared_from_this Modules
- DDP: infini_run multiprocess, Broadcast-before-wrap, sharded eval +
  epoch-level AllReduce(SUM), no per-step logging collectives
- Tests: kernel exact-value, gradcheck, torch golden parity, im2col/CUDA grids,
  dataset/dataloader, network parity dumper
Reducer::PrepareForBackward (inside DDP Forward, gradient_as_bucket_view=true)
binds param.grad to the bucket view; ZeroGrad(set_to_none=true) after
Forward reset that binding, so backward accumulated into a standalone grad
the reducer never all-reduced (silent no-sync, cross-rank weight fork).
Same order as gpt2/mixtral/llama3 demos.
…NoGradGuard

Call modules via operator() (the hook entry, same as gpt2/llama3 and PRs
219/220) instead of Forward() directly. Wrap evaluation in NoGradGuard so
forward-only graphs never prime grad accumulators (PR 220 acfbe87 hazard);
resolves TODO(dcj) no_grad().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant