[2026春季][T2-2-1] surprisely - #189
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a unified random number generation infrastructure (Generator / GeneratorImpl) across CPU and CUDA backends, enabling reproducible seeding, per-device default generators, state save/restore, and consistent integration into tensor random APIs and initialization utilities.
Changes:
- Added
Generatorhandle + polymorphic backend implementations (CPUmt19937state, CUDA Philox-style seed/offset with thread-safe offset reservation) and default generator management. - Wired the generator flow into
Tensor::Uniform,nn::init::{Uniform, Normal, KaimingUniform}, andnn::function::{Rand, Randn}, including CUDA kernels for float32 uniform/normal. - Added extensive CPU/CUDA generator tests and a script to validate cross-process reproducibility digest.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/tensor/cuda_only/test_generator_cuda.cc | New CUDA-only tests validating default/explicit generator behavior, state replay, offset semantics, and kernel statistical sanity. |
| tests/tensor/cpu_only/test_generator.cc | New CPU-only tests covering generator API/state serialization, parameter validation, default-generator semantics, and CUDA-generator host-side offset logic. |
| scripts/check_generator_reproducibility.sh | New helper script to run a reproducibility digest test twice and compare outputs across processes. |
| infini_train/src/tensor.cc | Updates Tensor::Uniform to accept std::shared_ptr<Generator> and route through nn::init::Uniform. |
| infini_train/src/nn/parallel/ddp/param_and_grad_buffer.cc | Adds missing <numeric> include for existing std::accumulate usage. |
| infini_train/src/nn/init.cc | Refactors Uniform/Normal/KaimingUniform to use Generator, adds bounds/parameter validation, and dispatches CUDA float32 random kernels with reserved offsets. |
| infini_train/src/nn/functional.cc | Adds nn::function::Rand and Randn that create float32 tensors and delegate to init random ops with optional generator. |
| infini_train/src/kernels/cuda/reduction.cu | Adds missing <numeric> include for existing std::accumulate usage. |
| infini_train/src/kernels/cuda/random.cu | New CUDA Philox-style random uniform/normal float32 kernels and launcher functions. |
| infini_train/src/kernels/cuda/no_op.cu | Adds missing <numeric> include for existing std::accumulate usage. |
| infini_train/src/kernels/cuda/gather.cu | Adds missing <numeric> include for existing std::accumulate usage. |
| infini_train/src/kernels/cuda/elementwise.cu | Adds missing <numeric> include for existing std::accumulate usage. |
| infini_train/src/kernels/cpu/transform.cc | Adds missing <numeric> include for existing std::accumulate usage. |
| infini_train/src/kernels/cpu/cross_entropy.cc | Adds missing <numeric> include for existing std::accumulate usage. |
| infini_train/src/generator.cc | New core implementation of CPU/CUDA generators, default-generator management, seeding APIs, and state serialization/parsing. |
| infini_train/include/tensor.h | Updates Tensor::Uniform signature to take std::shared_ptr<Generator> (default nullptr) and forward-declares Generator. |
| infini_train/include/nn/init.h | Updates init APIs to accept std::shared_ptr<Generator> and includes generator definitions. |
| infini_train/include/nn/functional.h | Declares Rand/Randn APIs with device + optional generator parameters and necessary forward declarations/includes. |
| infini_train/include/generator.h | New public generator interface (impl vtable, handle, default-generator APIs, seeding/state APIs). |
| infini_train/include/common/cuda/kernel_helper.cuh | Adjusts Tanh implementation for nv_bfloat16 / half via float conversion. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
chen2021673
left a comment
There was a problem hiding this comment.
- FP16/BF16 Tanh 修改与 Generator 无关,应独立提交;
- CPU只创建了一个进程级 generator,在单进程多线程 DDP 场景下,各 rank 串行消费同一个进程级 RNG,拿到的随机数并不确定。建议在 DDP 模型 to device 后、第一次 forward 前,从 DP group rank 0 原地 broadcast 所有参数。另外,TP 各 rank 如何获得一致稳定的随机流也需要更严谨的设计,可以后续继续讨论;
- generator.h 同时暴露 backend 接口、 ReserveRandomOffset 和 FillUniform ,建议重新划分,后两者不应该暴露给用户。同理检索其他接口;
- 近 1000 行 Generator 测试放在 tests/tensor/{cpu_only,cuda_only} 不合理。建议建立独立的 tests/generator/ 并合理拆分文件,测试尽量能够各 device 通用;
- 删掉遗留复现脚本check_generator_reproducibility.sh。
- internalize random backend helpers and relocate generator tests - synchronize DDP parameters and buffers from DP rank 0 before first forward - harden state validation, RNG offset accounting, and concurrent collectives
|
感谢 review,已在
同时补充了与复现性直接相关的核心加固:完整 64-bit CPU seed、严格且事务式的 state 恢复、CUDA Philox 精确 word-offset 预留及溢出/并发保护,以及 ProcessGroup 并发访问保护,并补齐相应边界测试。 已在 CUDA 12.8、2×RTX 2080 Ti 环境验证 Generator CPU、CUDA 和双卡 DDP 测试,并通过 ASan+UBSan 与 TSan 检查。 本提交同时同步当前 upstream,以复用新增的原地 |
Summary
本 PR 为 InfiniTrain 增加统一的
Generator/GeneratorImpl随机数基础设施,补齐 CPU、CUDA、逐设备默认 Generator、全局 seed、state 保存恢复,以及显式/默认 Generator 两种调用路径。随机能力已接入初始化和框架随机张量入口:
Tensor::Uniform;nn::init::{Uniform, Normal, KaimingUniform};nn::function::{Rand, Randn}。Background
原框架随机初始化依赖局部
std::mt19937,缺少统一后端抽象、默认随机源、全局 seed 和可恢复 state,无法稳定支持显式独立随机流、跨运行复现和 CUDA kernel 间不重叠的随机区间。Main Changes
Generator 基础设施
Generator句柄和多态GeneratorImpl。CPUGeneratorImpl和CUDAGeneratorImpl。ManualSeed、ManualSeedAll、Seed、InitialSeed、GetState、SetState和设备查询。默认实例与算子接入
测试
Compatibility and Scope
std::optional<std::mt19937>迁移为std::shared_ptr<Generator>;外部显式调用方需迁移。Tests
验证环境:2× NVIDIA GeForce RTX 2080 Ti、CUDA 12.4、GCC/G++ 13.4.0、CMake 3.28.4。
ec0f626ebd167804matchedTest Screenshots
01_cpu_generator_tests.png02_cuda_generator_tests.pngReproduction
AI Assistance Disclosure
本项目由参赛者主导并实际参与,使用 OpenAI Codex 作为辅助开发工具。具体说明见随 PR 上传的
REFERENCE.md;全部验收日志均来自目标环境真实执行。