-
Notifications
You must be signed in to change notification settings - Fork 69
[2026春季][T2-2-1]ChaoticLuna #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChaoticLuna
wants to merge
9
commits into
InfiniTensor:master
Choose a base branch
from
ChaoticLuna:2026-spring-ChaoticLuna-T2-2-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
df7308c
feat(generator): add CPU and CUDA generator infrastructure
5367598
feat(random): add distributions and generator-aware initialization
fece48d
feat(random): add rand randn and dropout operators
5551fe3
test(random): add generator and random operator coverage
0c5aa44
feat(ddp): synchronize module state at initialization
41cb406
chore(generator): add supplementary tests and IWYU fixes
383657e
fix(random): pass registered kernel handles by value
34cbc91
refactor(generator): address PR review feedback
f492cb2
Merge upstream master into Generator PR branch
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "infini_train/include/autograd/function.h" | ||
| #include "infini_train/include/generator.h" | ||
|
|
||
| namespace infini_train { | ||
| class Tensor; | ||
| } | ||
|
|
||
| namespace infini_train::autograd { | ||
|
|
||
| class Dropout final : public Function { | ||
| public: | ||
| static constexpr char kType[] = "DropoutFunction"; | ||
|
|
||
| Dropout(double p, std::optional<Generator> generator) : Function(kType), p_(p), generator_(std::move(generator)) {} | ||
|
|
||
| std::vector<std::shared_ptr<Tensor>> Forward(const std::vector<std::shared_ptr<Tensor>> &input_tensors) override; | ||
| void SetupContext(const std::vector<std::shared_ptr<Tensor>> &input_tensors, | ||
| const std::vector<std::shared_ptr<Tensor>> &output_tensors) override; | ||
| std::vector<std::shared_ptr<Tensor>> Backward(const std::vector<std::shared_ptr<Tensor>> &grad_outputs) override; | ||
|
|
||
| private: | ||
| double p_ = 0.0; | ||
| std::optional<Generator> generator_; | ||
| }; | ||
|
|
||
| } // namespace infini_train::autograd | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #pragma once | ||
|
|
||
| // Internal Generator backend registration interface. Application code should include generator.h. | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <unordered_map> | ||
|
|
||
| #include "infini_train/include/common/common.h" | ||
| #include "infini_train/include/device.h" | ||
| #include "infini_train/include/generator.h" | ||
|
|
||
| namespace infini_train { | ||
|
|
||
| class GeneratorBackend { | ||
| public: | ||
| virtual ~GeneratorBackend() = default; | ||
|
|
||
| virtual Device::DeviceType Type() const = 0; | ||
| virtual Generator Create(const Device &device, uint64_t seed) = 0; | ||
| virtual const Generator &GetDefault(const Device &device) = 0; | ||
| virtual void ManualSeedAll(uint64_t seed) = 0; | ||
| }; | ||
|
|
||
| // Registers one backend per device type during static initialization. | ||
| class GeneratorBackendRegistry { | ||
| public: | ||
| static GeneratorBackendRegistry &Instance(); | ||
|
|
||
| void Register(Device::DeviceType type, std::unique_ptr<GeneratorBackend> backend); | ||
| // Throws if no backend is registered for the requested device type. | ||
| GeneratorBackend &Get(Device::DeviceType type) const; | ||
| void ManualSeedAll(uint64_t seed) const; | ||
|
|
||
| private: | ||
| GeneratorBackendRegistry() = default; | ||
| GeneratorBackendRegistry(const GeneratorBackendRegistry &) = delete; | ||
| GeneratorBackendRegistry &operator=(const GeneratorBackendRegistry &) = delete; | ||
|
|
||
| std::unordered_map<Device::DeviceType, std::unique_ptr<GeneratorBackend>> backends_; | ||
| }; | ||
|
|
||
| } // namespace infini_train | ||
|
|
||
| // CAT expands __COUNTER__ before token pasting so each registration has a unique name. | ||
| #define INFINI_TRAIN_REGISTER_GENERATOR_BACKEND(device_type, class_impl) \ | ||
| [[maybe_unused]] static const bool CAT(infini_train_generator_backend_registered_, __COUNTER__) = []() { \ | ||
| infini_train::GeneratorBackendRegistry::Instance().Register(device_type, std::make_unique<class_impl>()); \ | ||
| return true; \ | ||
| }(); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #pragma once | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个头文件是公开的,建议区分 generator.h 和 generator_impl.h 文件,避免把 GeneratorImpl 暴露给外部。 |
||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
|
|
||
| #include "infini_train/include/device.h" | ||
|
|
||
| namespace infini_train { | ||
|
|
||
| class Tensor; | ||
| class GeneratorImpl; | ||
|
|
||
| // A lightweight handle with shared-copy semantics. Use Clone() for an independent state. | ||
| class Generator { | ||
| public: | ||
| static constexpr uint64_t kDefaultSeed = 67280421310721; | ||
|
|
||
| Generator() = delete; | ||
|
|
||
| Generator(const Generator &) = default; | ||
| Generator &operator=(const Generator &) = default; | ||
| Generator(Generator &&) = default; | ||
| Generator &operator=(Generator &&) = default; | ||
|
|
||
| ~Generator() = default; | ||
|
|
||
| void set_current_seed(uint64_t seed) const; | ||
| uint64_t current_seed() const; | ||
| uint64_t Seed(); | ||
| void set_state(const Tensor &state); | ||
| std::shared_ptr<Tensor> get_state() const; | ||
| Device device() const; | ||
| Generator Clone() const; | ||
|
|
||
| friend bool operator==(const Generator &a, const Generator &b) { return a.impl_ == b.impl_; } | ||
| friend bool operator!=(const Generator &a, const Generator &b) { return !(a == b); } | ||
|
|
||
| private: | ||
| friend class GeneratorAccessor; | ||
|
|
||
| explicit Generator(std::shared_ptr<GeneratorImpl> impl); | ||
| std::shared_ptr<GeneratorImpl> impl_; | ||
| }; | ||
|
|
||
| Generator CreateGenerator(const Device &device, uint64_t seed = Generator::kDefaultSeed); | ||
|
|
||
| // Returns the lazily initialized default generator for the requested device. | ||
| const Generator &GetDefaultGenerator(const Device &device); | ||
|
|
||
| // Initializes or resets the default generators for all enabled devices with the given seed. | ||
| void ManualSeed(uint64_t seed); | ||
|
|
||
| } // namespace infini_train | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| #pragma once | ||
|
|
||
| // Internal Generator implementation interface. Application code should include generator.h. | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <mutex> | ||
| #include <optional> | ||
| #include <stdexcept> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| #include "infini_train/include/device.h" | ||
| #include "infini_train/include/generator.h" | ||
| #include "infini_train/include/tensor.h" | ||
|
|
||
| namespace infini_train { | ||
|
|
||
| class Tensor; | ||
|
|
||
| class GeneratorImpl { | ||
| public: | ||
| explicit GeneratorImpl(Device device) : device_(device) {} | ||
| virtual ~GeneratorImpl() = default; | ||
|
|
||
| GeneratorImpl(const GeneratorImpl &other) = delete; | ||
| GeneratorImpl(GeneratorImpl &&other) = delete; | ||
| GeneratorImpl &operator=(const GeneratorImpl &other) = delete; | ||
| GeneratorImpl &operator=(GeneratorImpl &&other) = delete; | ||
|
|
||
| virtual void set_current_seed(uint64_t seed) = 0; | ||
| virtual uint64_t current_seed() const = 0; | ||
| virtual uint64_t Seed() = 0; | ||
| virtual void set_state(const Tensor &state) = 0; | ||
| virtual std::shared_ptr<Tensor> get_state() const = 0; | ||
|
|
||
| std::shared_ptr<GeneratorImpl> Clone() const { return std::shared_ptr<GeneratorImpl>(CloneImpl()); } | ||
| Device device() const { return device_; } | ||
|
|
||
| // Callers must hold this mutex when accessing shared RNG state concurrently. | ||
| std::mutex mutex_; | ||
|
|
||
| protected: | ||
| Device device_; | ||
| virtual GeneratorImpl *CloneImpl() const = 0; | ||
| }; | ||
|
|
||
| class GeneratorAccessor { | ||
| public: | ||
| static GeneratorImpl &Get(const Generator &generator) { | ||
| if (!generator.impl_) { | ||
| throw std::invalid_argument("Undefined Generator"); | ||
| } | ||
| return *generator.impl_; | ||
| } | ||
|
|
||
| static std::mutex &Mutex(const Generator &generator) { return Get(generator).mutex_; } | ||
|
|
||
| static Generator FromImpl(std::shared_ptr<GeneratorImpl> impl) { return Generator(std::move(impl)); } | ||
| }; | ||
|
|
||
| template <typename T> T &CheckedGeneratorImpl(const Generator &generator, const Device &expected_device) { | ||
| static_assert(std::is_base_of_v<GeneratorImpl, T>); | ||
| auto &base = GeneratorAccessor::Get(generator); | ||
| if (base.device() != expected_device) { | ||
| throw std::invalid_argument("Generator device mismatch"); | ||
| } | ||
| auto *typed = dynamic_cast<T *>(&base); | ||
| if (typed == nullptr) { | ||
| throw std::invalid_argument("Generator backend mismatch"); | ||
| } | ||
| return *typed; | ||
| } | ||
|
|
||
| template <class Impl, class... Args> Generator MakeGenerator(Args &&...args) { | ||
| return GeneratorAccessor::FromImpl(std::make_shared<Impl>(std::forward<Args>(args)...)); | ||
| } | ||
|
|
||
| template <typename T> | ||
| T &GetGeneratorOrDefault(const std::optional<Generator> &generator, const Generator &default_generator, | ||
| const Device &expected_device) { | ||
| const Generator &chosen = generator.has_value() ? *generator : default_generator; | ||
| return CheckedGeneratorImpl<T>(chosen, expected_device); | ||
| } | ||
|
|
||
| namespace detail { | ||
| void CheckRngState(const Tensor &state); | ||
| } // namespace detail | ||
|
|
||
| } // namespace infini_train |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
训练入口 examples 应该调用 ManualSeed() 。考虑到多线程并发,应该在 main() 中完成环境初始化后、创建训练线程之前调用一次(现在的 InitAllEnv 调用之后)同时删除 Train() 里的注释调用。