expert_offload.h

src/inference/expert_offload.h

@file expert_offload.h @brief EXPERIMENTAL (v2.13.0): put a MoE layer's routed-expert tensors on the host while its attention, KV and dense weights stay on the card.

Owners: @tvanfossen

expert_block_pattern

std::string entropic::expert_block_pattern(int layer)

The override pattern for ONE layer's expert tensors.

src/inference/expert_offload.h:93

Show on the map →

Called by SCENARIO, expert_offload_patterns

Calls none

Source · 3 lines
inline std::string expert_block_pattern(int layer) {
    return "blk\\." + std::to_string(layer) + kExpertTensorRegex;
}
Activity · 1 steps
lines 94–94return "blk\\." + std::to_string(layer) + kExpertTensorRegex;

Requirements

REQ-INFER-027

expert_offload_conflict_reason

std::string entropic::expert_offload_conflict_reason(const ModelConfig &cfg)

Configuration-time refusal for a combination that cannot mean what it says.

src/inference/expert_offload.h:155

Show on the map →

Called by SCENARIO, validate

Calls none

Source · 27 lines
inline std::string expert_offload_conflict_reason(const ModelConfig& cfg) {
    const int n = cfg.cpu_moe_layers;
    std::string reason;
    if (n < 0) {
        reason = "cpu_moe_layers must be >= 0 (0 = off), got "
               + std::to_string(n)
               + ". There is no 'all layers' sentinel — name the layer "
                 "count you want host-side.";
    } else if (n > 0 && cfg.gpu_layers == 0) {
        reason = "cpu_moe_layers=" + std::to_string(n)
               + " with gpu_layers: 0 — the whole model is already on the "
                 "host, so there are no expert tensors on the card to move "
                 "off it. Raise gpu_layers, or drop cpu_moe_layers.";
    } else if (n > 0 && cfg.gpu_layers_auto) {
        // v2.13.1: auto now derives the expert split ITSELF, from the GGUF's
        // real expert-tensor bytes, and prefers moving experts host-side
        // over dropping a layer. So this is no longer "auto cannot model
        // this" — it is "you have asked for both, and only one can win".
        reason = "cpu_moe_layers=" + std::to_string(n)
               + " with gpu_layers: auto — auto derives the expert split "
                 "itself from the model's shape, so setting both asks two "
                 "things to decide one placement. Drop cpu_moe_layers and "
                 "let auto choose, or set gpu_layers to an explicit count "
                 "and keep your own split.";
    }
    return reason;
}
Activity · 14 steps
lines 156–156const int n = cfg.cpu_moe_layers;lines 157–157std::string reason;lines 158–158◇ if (n < 0)lines 159–160reason = "cpu_moe_layers must be >= 0 (0 = off), got " + std::lines 160–162to_string(n) + ". There is no 'all layers' sentinel — name the layer " "count you want host-side.";lines 163–163elselines 163–163◇ if (n > 0 && cfg.gpu_layers == 0)lines 164–164reason = "cpu_moe_layers=" + std::lines 164–167to_string(n) + " with gpu_layers: 0 — the whole model is already on the " "host, so there are no expert tensors on the card to move " "off it. Raise gpu_layers, or drop cpu_moe_layers.";lines 168–168elselines 168–168◇ if (n > 0 && cfg.gpu_layers_auto)lines 173–173reason = "cpu_moe_layers=" + std::lines 173–178to_string(n) + " with gpu_layers: auto — auto derives the expert split " "itself from the model's shape, so setting both asks two " "things to decide one placement. Drop cpu_moe_layers and " "let auto choose, or set gpu_layers to an explicit count " "and keep your own split.";lines 180–180return reason;

Requirements

REQ-CFG-006 REQ-INFER-027

expert_offload_load_refusal

std::string entropic::expert_offload_load_refusal(int cpu_moe_layers, int n_layer, int n_expert)

Post-load refusal, against the numbers only the loaded model knows.

src/inference/expert_offload.h:207

Show on the map →

Called by SCENARIO, expert_offload_admits

Calls none

Source · 19 lines
inline std::string expert_offload_load_refusal(int cpu_moe_layers,
                                               int n_layer, int n_expert) {
    std::string reason;
    if (cpu_moe_layers <= 0) { return reason; }
    if (n_expert <= 0) {
        reason = "cpu_moe_layers=" + std::to_string(cpu_moe_layers)
               + " was requested, but this model declares no experts "
                 "(<arch>.expert_count is absent or 0). Expert-tensor "
                 "offload has nothing to place; use gpu_layers to move "
                 "whole layers on a dense model.";
    } else if (cpu_moe_layers > n_layer) {
        reason = "cpu_moe_layers=" + std::to_string(cpu_moe_layers)
               + " exceeds the model's " + std::to_string(n_layer)
               + " layers. Lower it to at most " + std::to_string(n_layer)
               + "; it is not clamped, because a count that cannot be "
                 "honoured is a configuration error, not a preference.";
    }
    return reason;
}
Activity · 11 steps
lines 209–209std::string reason;lines 210–210◇ if (cpu_moe_layers <= 0)lines 210–210return reason;lines 211–211◇ if (n_expert <= 0)lines 212–212reason = "cpu_moe_layers=" + std::lines 212–216to_string(cpu_moe_layers) + " was requested, but this model declares no experts " "(<arch>.expert_count is absent or 0). Expert-tensor " "offload has nothing to place; use gpu_layers to move " "whole layers on a dense model.";lines 217–217elselines 217–217◇ if (cpu_moe_layers > n_layer)lines 218–220reason = "cpu_moe_layers=" + std::to_string(cpu_moe_layers) + " exceeds the model's " + std::to_string(n_layer) + " layers. Lower it to at most " + std::lines 220–222to_string(n_layer) + "; it is not clamped, because a count that cannot be " "honoured is a configuration error, not a preference.";lines 224–224return reason;

Requirements

REQ-INFER-027

expert_offload_patterns

std::vector< std::string > entropic::expert_offload_patterns(int cpu_moe_layers)

Patterns for the FIRST cpu_moe_layers layers, in layer order.

src/inference/expert_offload.h:119

Show on the map →

Called by ExpertOffloadOverrides, SCENARIO

Calls expert_block_pattern

Source · 9 lines
inline std::vector<std::string> expert_offload_patterns(int cpu_moe_layers) {
    std::vector<std::string> patterns;
    if (cpu_moe_layers <= 0) { return patterns; }
    patterns.reserve(static_cast<std::size_t>(cpu_moe_layers));
    for (int i = 0; i < cpu_moe_layers; ++i) {
        patterns.push_back(expert_block_pattern(i));
    }
    return patterns;
}
Activity · 7 steps
lines 120–120std::vector<std::string> patterns;lines 121–121◇ if (cpu_moe_layers <= 0)lines 121–121return patterns;lines 122–122patterns.reserve(static_cast<std::size_t>(cpu_moe_layers));lines 123–123◇ for (int i = 0; i < cpu_moe_layers; ++i)lines 124–124patterns.push_back(expert_block_pattern(i));lines 126–126return patterns;

Requirements

REQ-INFER-027