Skip to content

[2026春季][T2-2-1] surprisely#189

Open
surprisely wants to merge 3 commits into
InfiniTensor:masterfrom
surprisely:2026-spring-surprisely-T2-2-1
Open

[2026春季][T2-2-1] surprisely#189
surprisely wants to merge 3 commits into
InfiniTensor:masterfrom
surprisely:2026-spring-surprisely-T2-2-1

Conversation

@surprisely

Copy link
Copy Markdown

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
  • 实现 CPUGeneratorImplCUDAGeneratorImpl
  • 支持 ManualSeedManualSeedAllSeedInitialSeedGetStateSetState 和设备查询。
  • CPU 保存完整 mt19937 engine state;CUDA state v3 保存 seed、来源设备和 semantic offset。
  • Generator 内部状态访问和 CUDA offset reservation 具备线程安全保护。

默认实例与算子接入

  • CPU 使用进程级默认 Generator;CUDA 按 device index 维护独立默认实例。
  • 未显式传入 Generator 时,按 tensor/device 自动选择默认实例。
  • 显式 Generator 路径不会推进默认 Generator 状态。
  • CUDA Uniform/Normal 使用项目内 Philox-style seed/offset kernel,并在调用前预留不重叠随机区间。

测试

  • 新增 CPU Generator 41 项测试和 CUDA Generator 16 项测试。
  • 覆盖同/异 seed、state 重放、默认/显式路径、损坏 state、线程安全、随机流连续性、空 tensor、参数边界和双物理 GPU 隔离。
  • 新增跨进程复现脚本,用于比较固定 seed 下的稳定 digest。

Compatibility and Scope

  • 不传随机源的既有调用方式保持不变。
  • 显式随机源参数由 std::optional<std::mt19937> 迁移为 std::shared_ptr<Generator>;外部显式调用方需迁移。
  • CPU/CUDA 公共 Uniform/Normal 当前明确限定为 float32。
  • 不要求与 PyTorch 逐 bit 一致,也不要求 CPU 与 CUDA 输出一致。
  • 当前框架没有成熟 Dropout 算子;本 PR 以 Rand/Randn 完成训练期随机调用接入,不提交缺少 training/eval、mask scaling 和 autograd 语义的半成品 Dropout。
  • 示例 checkpoint loader 的局部 mt19937 不在本次核心随机调用链范围内。

Tests

验证环境:2× NVIDIA GeForce RTX 2080 Ti、CUDA 12.4、GCC/G++ 13.4.0、CMake 3.28.4。

Test Result
CPU Generator 41/41 passed
CPU label regression 231 passed, 11 skipped, 0 failed
CUDA Generator 16/16 passed
CUDA label regression 9/9 passed
Dual physical GPU isolation 10/10 passed
Cross-process reproducibility digest ec0f626ebd167804 matched
ASan + UBSan 41/41 passed, no sanitizer error
Format / whitespace passed

Test Screenshots

  • 01_cpu_generator_tests.png
image
  • 02_cuda_generator_tests.png
image

Reproduction

cmake -S . -B build-cpu -G Ninja -DUSE_CUDA=OFF -DUSE_NCCL=OFF -DBUILD_TEST=ON
cmake --build build-cpu
build-cpu/tests/tensor/test_tensor_cpu_only --gtest_filter='CPUGeneratorTest.*'
scripts/check_generator_reproducibility.sh build-cpu/tests/tensor/test_tensor_cpu_only

cmake -S . -B build-cuda -G Ninja -DUSE_CUDA=ON -DUSE_NCCL=OFF -DBUILD_TEST=ON
cmake --build build-cuda
build-cuda/tests/tensor/test_tensor_cuda_only --gtest_filter='CUDAGeneratorTensorTest.*'

python3 scripts/format.py --path infini_train example --check

AI Assistance Disclosure

本项目由参赛者主导并实际参与,使用 OpenAI Codex 作为辅助开发工具。具体说明见随 PR 上传的 REFERENCE.md;全部验收日志均来自目标环境真实执行。

Copilot AI review requested due to automatic review settings July 12, 2026 15:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Generator handle + polymorphic backend implementations (CPU mt19937 state, 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}, and nn::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.

@surprisely

Copy link
Copy Markdown
Author

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.

2 participants