Repository navigation
Conversation
…bileNetV2 on CIFAR-10) - Framework: recursive Module train()/eval(), NamedBuffers, layout_utils, KaimingNormal, SGD momentum/weight_decay/StateDict, generalized distributed Checkpoint - Vision ops (Autograd/Module/Functional/CPU/CUDA): Conv2d (cuDNN cached plan + custom im2col/1x1-GEMM/zero-atomic Depthwise + naive baseline), BatchNorm2d, MaxPool2d, AdaptiveAvgPool2d (GAP BlockReduce), ReLU, ReLU6, Flatten - CIFAR-10 ResNet-18 (92.31%) & MobileNetV2 (91.50%), PyTorch alignment & 2-GPU RTX 5090 DDP (1.95x) - Includes HONOR_CODE.md, REFERENCE.md, and unit tests (CPU 318/318, CUDA vision 8/8)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
2026 秋季启元人工智能大赛 — 训练系统赛道(T2-2-2)中期技术报告
赛题编号与名称:
T2-2-2— InfiniTrain 视觉模型训练支持小组名称 / 队长:
Jinkey/ 汪景琦(杭州电子科技大学)GitHub ID / 仓库分支:
wjq0825/https://github.com/wjq0825/InfiniTrain(分支:2026-fall-wjq0825-T2-2-2)测试平台:2× NVIDIA GeForce RTX 5090(32 GB × 2,
sm_120,Driver 610.43.02,CUDA 12.8.61,cuDNN 9.7.0,PyTorch 2.6.0,Ubuntu 24.04,GCC 13.3.0,CMake 3.31.4)报告日期:2026 年 12 月 5 日
1. 项目概述与核心成果摘要
本项目面向 InfiniTrain 框架扩展完整的小型视觉模型训练能力,覆盖 框架基础设施升级 → 视觉算子(Autograd / Module / Functional / CPU & CUDA Kernel)→ 双模型端到端 CIFAR-10 训练 → Checkpoint 断点续训 → 双卡 RTX 5090 DDP 分布式训练 → 与 PyTorch 2.6.0 的严格数值对齐与性能对比。
目前阶段性实现(共改动/新增 57 个文件,+7,970 / -23 行)已在官方目标硬件 2× NVIDIA RTX 5090 (
sm_120) 上跑通全部必做验收项及多项加分项,核心实测指标全面超越赛题目标:build_cpuRelease 编译通过,318 / 318 项通过sm_120编译与视觉单测build_cudaRelease (sm_120) 编译通过,test_vision_cuda8 / 8 通过max_abs = 0.000e+00;前向1e-7~1e-5;bs=1285 步 Loss 偏差3.26e-4(ResNet-18) /7.99e-3(MobileNetV2)939 MB)2,363 MB)epoch_010恢复后 Epoch 11/12 的 LR、Loss 与 Test Acc 无缝衔接bs=32: 95.9% PT (5,237 img/s);bs=64: 71.9% PT (8,147 img/s);bs=128: 70.1% PT (6,924 img/s);bs=256: 62.4% PT (6,098 img/s)bs=128)6,334 img/s(69.2% PT),峰值显存2,363 MB < PyTorch 2,400 MBim2col+GEMM+DWvsnaiveim2col:2,415 img/svsnaive:252.8 img/s(9.55× 加速)max_abs_err ≤ 1e-51.654e-06;MobileNetV23.576e-0725,000+ Rank 125,000=50,000,零重叠1.95×(13,491 img/s);MobileNetV21.94×(12,283 img/s)clang-format-16/Blackclang_format_diff.py --all --check&black --check100% PASS2. 系统架构设计
2.1 框架基础设施升级(向后兼容现有 LLM / MNIST 代码)
Module训练态与 Buffer 隔离(module.h/module.cc):train(bool = true)、eval()、is_training(),确保嵌套的Sequential/BasicBlock/InvertedResidual中的BatchNorm2d同步切换训练/推理态;NamedBuffers()、has_buffer()、buffer()、mutable_buffer(),使BatchNorm2d的running_mean/running_var严格注册为 Buffer(不进入Optimizer参数列表,同时被To(device)和DistributedDataParallel设备检查正确覆盖);并在To(DataType)中保护非浮点 Buffer。nn/layout_utils.h/.cc):EnsureContiguous()、ValidateNCHW()、ComputeConv2dOutputSize()、ComputePool2dOutputSize(),所有视觉算子在框架入口处自动连续化并校验张量布局。nn/init.*、optimizer.*、accumulate_grad.{cc,cu}):nn::init::KaimingNormal;optimizers::SGD,新增momentum、weight_decay(L2 权重衰减)及StateDict()/LoadStateDict()(键前缀sgd.buf.),并配套实现 CPU 与 CUDASgdAccumulateGrad融合更新 Kernel。checkpoint/checkpoint.*、save_planner.cc):TrainerState中向后兼容增加epoch与rng_state字段(支持数据增强 RNG 状态无损续训);SavePlanner对优化器状态的识别从硬编码adam.泛化为IsOptimizerStateKey(),原生支持SGD动量状态与BatchNorm2drunning stats 的保存与恢复。CMakeLists.txt):USE_CUDNN自动探测开关,在 CUDA ≥ 12.8 时自动纳入sm_120并尊重用户传入的-DCMAKE_CUDA_ARCHITECTURES;新增cifar10训练/验证可执行目标。2.2 视觉算子设计与 CUDA 性能优化
所有视觉算子均提供完整的 Autograd (
Forward/SetupContext/Backward) +nn::Module+nn::functional+ CPU (OpenMP/Eigen) + CUDA (FP32/BF16/FP16) 实现:Conv2d(三档运行时可切换 CUDA 后端:--conv_backend=cudnn|im2col|naive):kCudnn(默认高性能路径):在conv2d.cu中设计了 线程局部CudnnConvPlan缓存(以(dev_idx, N, C_in, H, W, C_out, kH, kW, stride, pad, groups, dtype)为键缓存cudnnTensorDescriptor/FilterDescriptor/ConvolutionDescriptor以及cudnnGetConvolution*Algorithm_v7最优算法与 workspace 大小)。该优化消除了每步 ~15–18 ms 的重复启发式搜索与描述符创建开销,使 ResNet-18bs=32吞吐提升 4.25×、MobileNetV2bs=128吞吐提升 2.56×。FP32 模式下显式采用CUDNN_FMA_MATH以保证与 PyTorch 精确对齐。kIm2Col(自研优化 CUDA 后端,加分项):内置三条专用快速路径:(a)groups == c_in && c_out == c_in时走零原子操作 Depthwise 专属 Kernel(权重梯度采用cub::BlockReduce块内规约);(b)1×1, stride=1, pad=0时走零拷贝 Strided-Batched cuBLAS GEMM;(c) 通用分组卷积采用显存有界分块im2col/col2im+ cuBLAS GEMM(复用线程局部 scratch workspace)。kNaive(消融基线):直接嵌套循环 CUDA Kernel,用于量化自研优化与 cuDNN 的加速收益。BatchNorm2d:采用两遍 FP32 精确规约(cub::BlockReduce),训练态使用有偏方差归一化并以无偏方差var * M / (M - 1)更新running_var,推理态使用running_mean/running_var,与 PyTorch 公式逐项一致。MaxPool2d/AdaptiveAvgPool2d/ReLU/ReLU6/Flatten:AdaptiveAvgPool2d针对全局平均池化(output_size = (1, 1))特化了cub::BlockReduce快速路径;MaxPool2d保存int64索引并在反向采用原子累加正确处理重叠窗口。2.3 CIFAR-10 双模型、离线数据管线与 DDP 同步
example/cifar10/net.{h,cc}):实现 CIFAR 版 ResNet-18 与 MobileNetV2(width_mult=1.0),所有参数与 Buffer 的StateDict命名与 PyTorch 参考模型(example/cifar10/scripts/pytorch_models.py)保持 1:1 严格一致,支持通过自定义二进制格式(ckpt_io.py)实现零转换权重互通。example/cifar10/dataset.{h,cc}):纯 C++ 离线加载 CIFAR-10 二进制文件,支持RandomCrop(32, padding=4)、RandomHorizontalFlip(0.5)与通道归一化;内置可序列化mt19937RNG 状态以及严格无重叠、全覆盖的 DDP 索引分片(配合verify_ddp_sharding.py自动验证)。example/cifar10/main.cc):在多卡 DDP 启动时通过ProcessGroup::Broadcast按NamedParameters()/NamedBuffers()确定性顺序从 Rank 0 广播初始参数与 BN 统计量,保证各卡起点完全一致。3. 实验验证与数据分析(2× NVIDIA RTX 5090)
3.1 单元测试与存量回归
OMP_NUM_THREADS=4 ctest --test-dir build_cpu -j8):318 / 318 通过(100%),含新增的VisionOpsTest(含Conv2d标准/1×1/Depthwise 有限差分梯度检验、BN train/eval 及 buffer 隔离、SGD 动量+WD+Checkpoint 往返)与VisionModelSmokeTest,存量 GPT-2 / LLaMA3 / Checkpoint / DataLoader 测试全部零回归。sm_120视觉单测(./build_cuda/tests/vision/test_vision_cuda):8 / 8 通过(100%,耗时 0.71 s)。3.2 与 PyTorch 2.6.0 的严格数值对齐
通过
example/cifar10/scripts/align_verify.py导出相同初始权重与输入,在关闭 TF32(allow_tf32=False)条件下对比 CPU 与 CUDA 双端前向、Loss、梯度、参数更新、BN 统计量及 5 步多步 Loss:max_absmax_absmax_absrel_devbs=8, 32×32)0.000e+000.000e+00bs=8, 32×32)0.000e+002.384e-07bs=4, 32×32)4.768e-072.384e-07bs=4, 32×32)1.332e-150.000e+00bs=1, 32×32, im2col)1.907e-061.192e-071.907e-063.131e-03bs=1, 64×64, im2col)5.960e-070.000e+001.073e-062.311e-03bs=1, 32×32)7.153e-071.192e-071.073e-062.526e-03bs=128, 32×32)7.898e-062.384e-072.384e-073.263e-04bs=128, 32×32)3.330e-062.384e-078.345e-077.991e-033.3 CIFAR-10 20-Epoch 端到端训练与断点续训
统一基准配置:
bs=128,epochs=20,FP32,SGD(lr=0.1, momentum=0.9, weight_decay=5e-4),Cosine LR,数据增强开启(RandomCrop(32, pad=4) + HFlip):Checkpoint 断点续训验证(从 ResNet-18
epoch_010恢复继续训练至 Epoch 12):lr = 0.04218 / 0.03455,train_loss = 0.3456 / 0.3121,test_acc = 84.06% / 85.28%;lr = 0.04218 / 0.03455,train_loss = 0.3463 / 0.3101,test_acc = 85.28% / 85.59%,参数、SGD 动量、BN 统计量、Cosine LR 步数及 RNG 状态全部精确衔接。3.4 RTX 5090 单卡吞吐/显存基准与 Kernel 消融实验
在单卡 RTX 5090 上对比 InfiniTrain(
cudnn计划缓存版 / 自研im2col/naive基线)与 PyTorch 2.6.0(warmup=10, steps=50):cudnn缓存版)bs=325,236.8 img/s(6.11 ms)5,458.8 img/s(5.86 ms)321 MBvs314 MBbs=648,146.5 img/s(7.86 ms)11,336.1 img/s(5.65 ms)526 MBvs579 MBbs=1286,923.5 img/s(18.49 ms)9,880.5 img/s(12.96 ms)939 MBvs916 MBbs=2566,097.7 img/s(41.98 ms)9,768.8 img/s(26.21 ms)1,785 MBvs1,339 MBbs=1286,334.1 img/s(20.21 ms)9,152.2 img/s(13.99 ms)2,363 MBvs2,400 MBConv2d 后端消融实验(ResNet-18, RTX 5090,
bs=128, FP32):step_ms)img/s)naive加速比naive(朴素 CUDA 基线)506.25 ms252.8 img/s1.00×im2col(自研分块 im2col + cuBLAS GEMM + 1×1 直连)53.01 ms2,414.6 img/s9.55×cudnn(未缓存描述符/算法搜索)23.38 ms5,474.4 img/s21.66×cudnn(线程局部计划缓存优化版)18.49 ms6,923.5 img/s27.39×3.5 双卡 RTX 5090 DDP 分布式验证与加速比
在 2× RTX 5090(
--nthread_per_process=2,local_bs=128 -> global_bs=256)上实测:1e-5):max_abs_grad_err = 1.654e-06(PASS)max_abs_grad_err = 3.576e-07(PASS)verify_ddp_sharding.py):Rank 025,000样本 + Rank 125,000样本 =50,000样本,零重叠、全覆盖(PASS)。1.50×):6,923.5 img/s→ 双卡13,490.9 img/s(1.95×,扩展效率 97.4%,每 epoch 仅3.71 s);6,334.1 img/s→ 双卡12,283.4 img/s(1.94×,扩展效率 97.0%,每 epoch 仅4.07 s)。NCCL 2.25.1在 Driver610.43.02+ RTX 5090 环境下会触发enqueue.cc:1500 Cuda failure 1 'invalid argument'(PyTorch 原生torchrun亦复现同一错误);通过升级nvidia-nccl-cu12(LD_PRELOAD加载新版libnccl.so.2)彻底解决。4. 代码规范状态与下一阶段计划(终期冲刺)
scripts/clang_format_diff.py --all --check --clang-format clang-format-16与python3 -m black --check example/cifar10/scripts均 100% PASS。tools/clang_tidy自定义插件InfiniTrainTidy.so,完成全量clang-tidy-18与IWYU静态检查;BF16 / FP16混合精度下AccumulateGrad梯度 dtype 匹配问题,补齐混合精度吞吐与精度对比加分项;InfiniTensor/InfiniTrain提交符合赛题分支与标题规范的 PR,并补充完整训练日志与算子级 Profiling 报告。HONOR_CODE.md与第三方库/参考文献说明REFERENCE.md。