-
Notifications
You must be signed in to change notification settings - Fork 249
feat(moe): add--freq and --collect-routing, train predictor of experts #386
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,59 @@ | ||
| #include "moe_routing_collector.h" | ||
|
|
||
| #include <cerrno> | ||
| #include <cstdio> | ||
| #include <cstring> | ||
|
|
||
| namespace dflash::common { | ||
|
|
||
| bool MoeRoutingCollector::open(const std::string & path) { | ||
| std::lock_guard<std::mutex> lock(mu_); | ||
| if (fd_) { | ||
| std::fclose(fd_); | ||
| fd_ = nullptr; | ||
| } | ||
| fd_ = std::fopen(path.c_str(), "wb"); | ||
| if (!fd_) { | ||
| std::fprintf(stderr, "[routing-collector] failed to open '%s': %s\n", | ||
| path.c_str(), std::strerror(errno)); | ||
| return false; | ||
| } | ||
| samples_ = 0; | ||
| std::fprintf(stderr, "[routing-collector] collecting routing data to '%s'\n", path.c_str()); | ||
| return true; | ||
| } | ||
|
|
||
| void MoeRoutingCollector::record(int layer_idx, const float * hidden, int n_embd, | ||
| const int32_t * expert_ids, int K) { | ||
| std::lock_guard<std::mutex> lock(mu_); | ||
| if (!fd_) return; | ||
|
|
||
| int32_t li = layer_idx; | ||
| int32_t ki = K; | ||
| // A short write desyncs the fixed-layout binary stream: every subsequent | ||
| // record would be misaligned and the file silently unparseable. Treat any | ||
| // partial write as fatal for this file — stop writing and don't count the | ||
| // sample, rather than emitting corrupt training data with an inflated count. | ||
| if (std::fwrite(&li, sizeof(int32_t), 1, fd_) != 1 || | ||
| std::fwrite(&ki, sizeof(int32_t), 1, fd_) != 1 || | ||
| std::fwrite(hidden, sizeof(float), (size_t)n_embd, fd_) != (size_t)n_embd || | ||
| std::fwrite(expert_ids, sizeof(int32_t), (size_t)K, fd_) != (size_t)K) { | ||
| std::fprintf(stderr, "[routing-collector] write error after %lld samples: %s\n", | ||
| (long long)samples_, std::strerror(errno)); | ||
| std::fclose(fd_); | ||
| fd_ = nullptr; | ||
| return; | ||
| } | ||
| samples_++; | ||
| } | ||
|
|
||
| void MoeRoutingCollector::close() { | ||
| std::lock_guard<std::mutex> lock(mu_); | ||
| if (!fd_) return; | ||
| std::fclose(fd_); | ||
| fd_ = nullptr; | ||
| std::fprintf(stderr, "[routing-collector] closed, %lld samples written\n", | ||
| (long long)samples_); | ||
| } | ||
|
|
||
| } // namespace dflash::common |
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 @@ | ||
| // MoE routing data collector — writes per-token binary routing data for | ||
| // predictor training (gate input hidden states + expert selections). | ||
| // | ||
| // Binary format per sample (matches StreamMoE train_predictor.py format): | ||
| // int32 layer_idx | ||
| // int32 K (number of selected experts) | ||
| // float32[n_embd] hidden_state (gate input / post-norm hidden for this layer) | ||
| // int32[K] expert_indices (actual routing result) | ||
| // | ||
| // Enable via --collect-routing <path> on the server CLI or via env var | ||
| // DFLASH_COLLECT_ROUTING=<path>. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <cstdio> | ||
| #include <mutex> | ||
| #include <string> | ||
|
|
||
| namespace dflash::common { | ||
|
|
||
| class MoeRoutingCollector { | ||
| public: | ||
| MoeRoutingCollector() = default; | ||
| ~MoeRoutingCollector() { close(); } | ||
|
|
||
| MoeRoutingCollector(const MoeRoutingCollector &) = delete; | ||
| MoeRoutingCollector & operator=(const MoeRoutingCollector &) = delete; | ||
|
|
||
| // Open the output file. Returns false on failure. | ||
| bool open(const std::string & path); | ||
|
|
||
| // Record one routing sample: gate input + expert selection for one token at one layer. | ||
| // hidden must point to n_embd floats; expert_ids to K int32s. | ||
| void record(int layer_idx, const float * hidden, int n_embd, | ||
| const int32_t * expert_ids, int K); | ||
|
|
||
| // Close and flush the output file. Prints summary to stderr. | ||
| void close(); | ||
|
|
||
| bool is_open() const { return fd_ != nullptr; } | ||
| int64_t sample_count() const { return samples_; } | ||
|
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. P2: Prompt for AI agents |
||
|
|
||
| private: | ||
| std::FILE * fd_ = nullptr; | ||
| int64_t samples_ = 0; // wide: collection runs can exceed 2^31 samples | ||
| std::mutex mu_; | ||
| }; | ||
|
|
||
| } // namespace dflash::common | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.