-
Notifications
You must be signed in to change notification settings - Fork 221
feat(server): add cross-backend MoE expert compute foundation #375
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
Draft
weicj
wants to merge
2
commits into
Luce-Org:main
Choose a base branch
from
weicj:feat-cross-backend-moe-expert-compute
base: main
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.
Draft
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
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 was deleted.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| #include "moe_expert_compute.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cerrno> | ||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <limits> | ||
|
|
||
| namespace dflash::common { | ||
|
|
||
| namespace { | ||
|
|
||
| uint64_t hash_u64(uint64_t h, uint64_t v) { | ||
| h ^= v + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2); | ||
| return h; | ||
| } | ||
|
|
||
| const char * nonempty_env(const char * name) { | ||
| const char * raw = std::getenv(name); | ||
| return raw && *raw ? raw : nullptr; | ||
| } | ||
|
|
||
| int parse_nonnegative_env(const char * name, int fallback) { | ||
| const char * raw = nonempty_env(name); | ||
| if (!raw) return fallback; | ||
| errno = 0; | ||
| char * end = nullptr; | ||
| const long value = std::strtol(raw, &end, 10); | ||
| if (errno == ERANGE || end == raw || *end != '\0' || | ||
| value < 0 || value > std::numeric_limits<int>::max()) { | ||
| return fallback; | ||
| } | ||
| return (int)value; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| uint64_t moe_expert_placement_fingerprint(const MoeHybridStorage & hybrid, | ||
| int n_layer, | ||
| int n_expert, | ||
| int n_expert_used) { | ||
| uint64_t h = 1469598103934665603ULL; | ||
| h = hash_u64(h, (uint64_t)n_layer); | ||
| h = hash_u64(h, (uint64_t)n_expert); | ||
| h = hash_u64(h, (uint64_t)n_expert_used); | ||
| h = hash_u64(h, (uint64_t)hybrid.placement.total_hot); | ||
| for (size_t il = 0; il < hybrid.placement.hot_expert_ids.size(); ++il) { | ||
| h = hash_u64(h, (uint64_t)il); | ||
| for (int32_t expert : hybrid.placement.hot_expert_ids[il]) { | ||
| h = hash_u64(h, (uint64_t)(uint32_t)expert); | ||
| } | ||
| } | ||
| return h; | ||
| } | ||
|
|
||
| std::vector<MoeExpertLayer> make_moe_expert_layers( | ||
| const MoeHybridStorage & hybrid, | ||
| const std::vector<MoeLayerDesc> & layer_descs) { | ||
| std::vector<MoeExpertLayer> layers(hybrid.layers.size()); | ||
| for (size_t il = 0; il < hybrid.layers.size(); ++il) { | ||
| const auto & storage = hybrid.layers[il]; | ||
| const MoeLayerDesc * desc = | ||
| il < layer_descs.size() ? &layer_descs[il] : nullptr; | ||
| auto & cl = layers[il]; | ||
| cl.layer_idx = (int)il; | ||
| cl.cold_global_by_local = storage.cold_expert_ids; | ||
| cl.fused_gate_up = (storage.gate_up_cold != nullptr); | ||
| if (cl.fused_gate_up) { | ||
| cl.gate_up_data = | ||
| storage.gate_up_cold ? storage.gate_up_cold->data : nullptr; | ||
| cl.gate_up_stride = | ||
| storage.gate_up_cold ? storage.gate_up_cold->nb[2] : 0; | ||
| cl.gate_up_type = | ||
| storage.gate_up_cold ? storage.gate_up_cold->type : GGML_TYPE_Q4_K; | ||
| cl.gate_up_scale = desc ? desc->ffn_gate_up_exps_s : 1.0f; | ||
| } else { | ||
| cl.gate_data = storage.gate_cold ? storage.gate_cold->data : nullptr; | ||
| cl.up_data = storage.up_cold ? storage.up_cold->data : nullptr; | ||
| cl.gate_stride = storage.gate_cold ? storage.gate_cold->nb[2] : 0; | ||
| cl.up_stride = storage.up_cold ? storage.up_cold->nb[2] : 0; | ||
| cl.gate_type = | ||
| storage.gate_cold ? storage.gate_cold->type : GGML_TYPE_Q4_K; | ||
| cl.up_type = | ||
| storage.up_cold ? storage.up_cold->type : GGML_TYPE_Q4_K; | ||
| cl.gate_scale = desc ? desc->ffn_gate_exps_s : 1.0f; | ||
| cl.up_scale = desc ? desc->ffn_up_exps_s : 1.0f; | ||
| } | ||
| cl.down_data = storage.down_cold ? storage.down_cold->data : nullptr; | ||
| cl.down_stride = storage.down_cold ? storage.down_cold->nb[2] : 0; | ||
| cl.down_type = | ||
| storage.down_cold ? storage.down_cold->type : GGML_TYPE_Q4_K; | ||
| cl.down_scale = desc ? desc->ffn_down_exps_s : 1.0f; | ||
| } | ||
| return layers; | ||
| } | ||
|
|
||
| void MoeExpertComputeRuntime::reset() { | ||
| compute.reset(); | ||
| layers.clear(); | ||
| target_path.clear(); | ||
| placement_fingerprint = 0; | ||
| } | ||
|
|
||
| bool ensure_moe_expert_compute_runtime( | ||
| MoeExpertComputeRuntime & runtime, | ||
| const MoeExpertComputeRuntimeConfig & cfg, | ||
| const MoeHybridStorage & hybrid, | ||
| const std::vector<MoeLayerDesc> & layer_descs, | ||
| std::string * err) { | ||
| if (!cfg.enabled) { | ||
| runtime.reset(); | ||
| return true; | ||
| } | ||
| if (cfg.n_layer <= 0 || cfg.n_expert <= 0 || cfg.n_expert_used <= 0 || | ||
| cfg.n_embd <= 0 || cfg.n_ff_exp <= 0) { | ||
| if (err) *err = "invalid MoE expert compute runtime config"; | ||
| runtime.reset(); | ||
| return false; | ||
| } | ||
|
|
||
| const uint64_t fingerprint = | ||
| moe_expert_placement_fingerprint(hybrid, cfg.n_layer, cfg.n_expert, | ||
| cfg.n_expert_used); | ||
| const bool can_reuse = | ||
| runtime.compute && | ||
| runtime.compute->healthy() && | ||
| runtime.target_path == cfg.target_path && | ||
| runtime.placement_fingerprint == fingerprint; | ||
| if (!can_reuse) { | ||
| runtime.compute.reset(); | ||
| runtime.target_path.clear(); | ||
| runtime.placement_fingerprint = 0; | ||
| } | ||
|
|
||
| if (!runtime.compute) { | ||
| if (const char * ipc_bin = nonempty_env("DFLASH_MOE_EXPERT_COMPUTE_IPC_BIN")) { | ||
| const char * work_dir = nonempty_env("DFLASH_MOE_EXPERT_COMPUTE_IPC_WORK_DIR"); | ||
| const int remote_gpu = | ||
| parse_nonnegative_env("DFLASH_MOE_EXPERT_COMPUTE_IPC_GPU", 0); | ||
| const bool required = | ||
| parse_nonnegative_env("DFLASH_MOE_EXPERT_COMPUTE_IPC_REQUIRED", 0) != 0; | ||
| MoeExpertComputeIpcStartResult remote = make_moe_expert_compute_ipc( | ||
| ipc_bin, cfg.target_path, remote_gpu, hybrid.placement, | ||
| cfg.n_embd, cfg.n_ff_exp, cfg.n_expert_used, | ||
| work_dir ? work_dir : "", required); | ||
| if (required && !remote.started_remote) { | ||
| if (err) *err = "remote MoE expert compute IPC is required but did not start"; | ||
| std::fprintf(stderr, "%s %s\n", cfg.log_prefix ? cfg.log_prefix : "[moe-expert-compute]", | ||
| err ? err->c_str() : "remote IPC did not start"); | ||
| runtime.reset(); | ||
| return false; | ||
| } | ||
| runtime.compute = std::move(remote.compute); | ||
| } | ||
| if (!runtime.compute) { | ||
| runtime.compute = make_cpu_moe_expert_compute(cfg.n_ff_exp); | ||
| } | ||
| } | ||
|
|
||
| runtime.layers = make_moe_expert_layers(hybrid, layer_descs); | ||
| runtime.target_path = cfg.target_path; | ||
| runtime.placement_fingerprint = fingerprint; | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace dflash::common | ||
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.