Repository navigation
Expand file tree
/
Copy pathbackend_plan.cpp
More file actions
190 lines (166 loc) · 7.35 KB
/
Copy pathbackend_plan.cpp
File metadata and controls
190 lines (166 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "backend_plan_internal.h"
#include "feature_gate.h"
#include "model_capabilities.h"
#include "kv_quant.h"
#include <limits>
#include <utility>
namespace luce::common::detail {
namespace {
BackendPreparationFailure failure(
BackendPreparationError error,
std::string message,
std::vector<std::string> warnings = {}) {
return {error, std::move(message), std::move(warnings)};
}
PlacementBackend resolve_target_backend(
const BackendArgs & args,
PlacementBackend compiled_backend) {
return args.device.backend == PlacementBackend::Auto
? compiled_backend
: args.device.backend;
}
} // namespace
BackendPreparation BackendPlanBuilder::resolve(
BackendArgs args,
BackendAdmissionContext admission,
GgufModelInfo model,
PlacementBackend compiled_backend) {
if (args.model_path.empty()) {
return failure(
BackendPreparationError::InvalidRequest,
"model_path is empty");
}
if (model.arch.empty()) {
return failure(
BackendPreparationError::ModelInspection,
"failed to detect architecture from " + args.model_path);
}
if (!arch_is_supported(model.arch)) {
return failure(
BackendPreparationError::ModelInspection,
"unsupported model architecture '" + model.arch + "' in " +
args.model_path);
}
const PlacementBackend target_backend =
resolve_target_backend(args, compiled_backend);
// Monolithic qwen35 resolves KV cache element types as part of planning:
// explicit --cache-type overrides first, then env, then the family
// default. Other families keep their own env-driven resolution.
if (model.arch == "qwen35" && !args.device.is_multi_device()) {
luce::resolve_kv_types(args.cache_type_k, args.cache_type_v,
args.cache_type_k, args.cache_type_v);
}
if (args.specla_mode && !args.ddtree_tau_explicit) {
args.ddtree_tau = 6.0f;
}
// Preserve the original admission order. Warnings describe what the
// operator requested, before model-specific SpecLA normalization changes
// the effective construction request.
std::string incompatible = check_feature_compatibility(
args, admission, model.arch, target_backend, compiled_backend);
if (!incompatible.empty()) {
return failure(
BackendPreparationError::FeatureCompatibility,
std::move(incompatible));
}
std::vector<std::string> warnings =
collect_feature_warnings(args, model.arch);
if (args.specla_mode) {
const bool supported =
model.arch == "qwen35" && !args.device.is_multi_device();
if (!supported) {
warnings.push_back(
"--specla is unavailable for architecture '" + model.arch +
"' with placement " + placement_device_name(args.device) +
"; using the architecture's normal decode path");
args.specla_mode = false;
} else if (!args.fast_rollback) {
// The CLI rejects --specla with --no-fast-rollback before
// admission; this guard covers callers that build BackendArgs
// directly.
warnings.push_back(
"--specla requires fast rollback; using the normal decode "
"path");
args.specla_mode = false;
} else if (!args.draft_path.has_value()) {
return failure(
BackendPreparationError::FeatureCompatibility,
"Qwen3.6 SpecLA requires --draft <path>",
std::move(warnings));
} else {
args.ddtree_mode = true;
if (admission.kvflash_requested()) {
warnings.push_back(
"--specla is unavailable with KVFlash; using ordinary "
"DDTree verification");
args.specla_mode = false;
}
}
if (!args.specla_mode && !args.ddtree_tau_explicit) {
args.ddtree_tau = std::numeric_limits<float>::infinity();
}
}
// Validate the exact snapshot construction will consume. This replaces
// the old factory recheck against a second mutable BackendArgs object.
incompatible = check_feature_compatibility(
args, admission, model.arch, target_backend, compiled_backend);
if (!incompatible.empty()) {
return failure(
BackendPreparationError::FeatureCompatibility,
std::move(incompatible),
std::move(warnings));
}
BackendPlan plan;
plan.model_.path = std::move(args.model_path);
plan.model_.mmproj_path = std::move(args.mmproj_path);
plan.model_.mmproj_device = std::move(args.mmproj_device);
plan.model_.metadata = std::move(model);
plan.placement_.target = std::move(args.device);
plan.placement_.draft = std::move(args.draft_device);
plan.placement_.remote_draft = std::move(args.remote_draft);
plan.placement_.remote_target_shard =
std::move(args.remote_target_shard);
plan.placement_.expert = std::move(args.expert_device);
plan.cache_.fa_window = args.fa_window;
plan.cache_.paged_attention = args.paged_attention;
plan.cache_.kv_pool_tokens = args.kv_pool_tokens;
plan.cache_.kv_pool_reserve_bytes = args.kv_pool_reserve_bytes;
plan.cache_.pflash_reserve_gpu = args.pflash_reserve_gpu;
plan.cache_.pflash_reserve_bytes = args.pflash_reserve_bytes;
plan.cache_.pflash_reserve_bytes_per_token = args.pflash_reserve_bytes_per_token;
plan.cache_.cache_type_k = args.cache_type_k;
plan.cache_.cache_type_v = args.cache_type_v;
plan.cache_.kq_stride_pad = args.kq_stride_pad;
plan.cache_.draft_swa_window = args.draft_swa_window;
plan.cache_.draft_ctx_max = args.draft_ctx_max;
plan.speculation_.draft_path = std::move(args.draft_path);
plan.speculation_.draft_block_size = args.draft_block_size;
plan.speculation_.fast_rollback = args.fast_rollback;
plan.speculation_.seq_verify = args.seq_verify;
plan.speculation_.specla_mode = args.specla_mode;
plan.speculation_.specla_top_k = args.specla_top_k;
plan.speculation_.ddtree_mode = args.ddtree_mode;
plan.speculation_.ddtree_budget = args.ddtree_budget;
plan.speculation_.ddtree_temp = args.ddtree_temp;
plan.speculation_.ddtree_chain_seed = args.ddtree_chain_seed;
plan.speculation_.ddtree_tau = args.ddtree_tau;
plan.speculation_.verify_width = args.verify_width;
plan.speculation_.use_feature_mirror = args.use_feature_mirror;
plan.execution_.stream_fd = args.stream_fd;
// Resolve qwen4exp auto sizing after weights and the configured cache load.
plan.execution_.chunk =
plan.model_.metadata.arch == "qwen4exp" && !args.chunk_set ? 0 : args.chunk;
plan.execution_.max_concurrency = args.max_concurrency;
plan.execution_.prefill_mode = args.ds4_prefill_mode;
plan.execution_.expert_top_k = args.ds4_expert_top_k;
plan.execution_.fused_decode = args.ds4_fused_decode;
plan.execution_.fused_verify_f16_kv =
args.ds4_fused_verify_f16_kv;
plan.execution_.expert_placement = args.expert_placement;
plan.execution_.router_bias = args.ds4_router_bias;
plan.execution_.protected_experts = args.ds4_protected_experts;
plan.execution_.cluster = args.cluster;
plan.warnings_ = std::move(warnings);
return plan;
}
} // namespace luce::common::detail