[2026春季][T2-2-1] surprisely#189
Open
surprisely wants to merge 3 commits into
Open
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.
Author
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.
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;全部验收日志均来自目标环境真实执行。