|
19 | 19 | #include "../common/moe_hybrid_types.h" |
20 | 20 | #include "../common/moe_hybrid_types_impl.h" |
21 | 21 | #include "../common/moe_hybrid_placement.h" |
| 22 | +#include "../common/kvflash_placement.h" |
22 | 23 | #include "../common/moe_hybrid_ffn_eval.h" |
23 | 24 | #include "../common/moe_hybrid_storage.h" |
24 | 25 | #include "../common/moe_hybrid_routing_stats.h" |
@@ -121,27 +122,42 @@ KvFlashConfig LagunaBackend::kvflash_config() const { |
121 | 122 | return pc; |
122 | 123 | } |
123 | 124 |
|
124 | | -void LagunaBackend::kvflash_read_config() { |
| 125 | +void LagunaBackend::kvflash_resolve_drafter() { |
125 | 126 | if (std::getenv("DFLASH_KVFLASH")) { |
126 | 127 | kvflash_drafter_path_ = kvflash_find_drafter(args_.target_path.c_str()); |
127 | 128 | } |
| 129 | +} |
| 130 | + |
| 131 | +KvFlashAutoBudget LagunaBackend::make_kvflash_budget(int64_t gpu_free) const { |
| 132 | + KvFlashAutoBudget b; |
| 133 | + b.free_bytes = gpu_free; |
| 134 | + b.bytes_per_token = (int64_t)w_.n_layer * w_.n_head_kv * 2 * |
| 135 | + (int64_t)ggml_row_size(args_.kv_type, w_.head_dim); |
| 136 | + b.reserve_bytes = (int64_t)(1.5 * 1073741824.0) + |
| 137 | + (kvflash_drafter_path_.empty() ? 0 : (int64_t)(1.7 * 1073741824.0)); |
| 138 | + return b; |
| 139 | +} |
| 140 | + |
| 141 | +void LagunaBackend::kvflash_read_config() { |
| 142 | + kvflash_resolve_drafter(); |
128 | 143 | // "auto" sizes from the GPU (weights resident, cache not yet allocated): |
129 | 144 | // laguna pools ALL n_layer layers at the configured KV quant. |
130 | | - KvFlashAutoBudget kvf_budget; |
131 | | - { |
132 | | - size_t gpu_free = 0, gpu_total = 0; |
133 | | - if (ggml_backend_dev_t dev = ggml_backend_get_device(backend_)) { |
134 | | - ggml_backend_dev_memory(dev, &gpu_free, &gpu_total); |
135 | | - } |
136 | | - kvf_budget.free_bytes = (int64_t)gpu_free; |
137 | | - kvf_budget.bytes_per_token = (int64_t)w_.n_layer * w_.n_head_kv * 2 * |
138 | | - (int64_t)ggml_row_size(args_.kv_type, w_.head_dim); |
139 | | - kvf_budget.reserve_bytes = (int64_t)(1.5 * 1073741824.0) + |
140 | | - (kvflash_drafter_path_.empty() ? 0 : (int64_t)(1.7 * 1073741824.0)); |
| 145 | + size_t gpu_free = 0, gpu_total = 0; |
| 146 | + if (ggml_backend_dev_t dev = ggml_backend_get_device(backend_)) { |
| 147 | + ggml_backend_dev_memory(dev, &gpu_free, &gpu_total); |
141 | 148 | } |
| 149 | + KvFlashAutoBudget kvf_budget = make_kvflash_budget((int64_t)gpu_free); |
142 | 150 | kvflash_tokens_ = kvflash_pool_from_env(args_.max_ctx, kvflash_config(), |
143 | | - !kvflash_drafter_path_.empty(), |
| 151 | + kvflash_scorer_expected(), |
144 | 152 | kvf_budget); |
| 153 | + if (kvflash_tokens_ > 0 && placement_all_hot_full_kv_) { |
| 154 | + std::printf("[laguna][kvflash] disabled: placement all-hot at max_ctx %d, " |
| 155 | + "pool not needed\n", args_.max_ctx); |
| 156 | + std::fflush(stdout); |
| 157 | + kvflash_tokens_ = 0; |
| 158 | + kvflash_tau_ = 64; |
| 159 | + kvflash_drafter_path_.clear(); |
| 160 | + } |
145 | 161 | if (kvflash_tokens_ > 0) { |
146 | 162 | const char * tau = std::getenv("DFLASH_KVFLASH_TAU"); |
147 | 163 | kvflash_tau_ = std::max(1, tau ? std::atoi(tau) : 64); |
@@ -266,6 +282,7 @@ bool LagunaBackend::unpark(const std::string & what) { |
266 | 282 | } |
267 | 283 | cache_.kv_k_type = args_.kv_type; |
268 | 284 | cache_.kv_v_type = args_.kv_type; |
| 285 | + kvflash_read_config(); |
269 | 286 | if (!create_laguna_target_cache(w_, args_.max_ctx, backend_, cache_, |
270 | 287 | kvflash_tokens_)) { |
271 | 288 | std::fprintf(stderr, "[unpark] cache: %s\n", dflash27b_last_error()); |
@@ -1382,6 +1399,7 @@ static inline uint64_t elapsed_us(HybridClock::time_point t0, HybridClock::time_ |
1382 | 1399 |
|
1383 | 1400 | bool LagunaBackend::init_hybrid_mode() { |
1384 | 1401 | const char * hotness_path = std::getenv("DFLASH_LAGUNA_HOTNESS"); |
| 1402 | + placement_all_hot_full_kv_ = false; |
1385 | 1403 |
|
1386 | 1404 | // Step 1: Load model WITHOUT expert data to GPU (partial load) |
1387 | 1405 | TargetLoadPlan _hybrid_plan; |
@@ -1454,13 +1472,29 @@ bool LagunaBackend::init_hybrid_mode() { |
1454 | 1472 |
|
1455 | 1473 | const uint64_t kv_bytes_per_tok = (uint64_t)w_.n_layer * 2 * |
1456 | 1474 | (uint64_t)w_.n_head_kv * (uint64_t)w_.head_dim * 2; |
1457 | | - const uint64_t kv_total = kv_bytes_per_tok * (uint64_t)max_context; |
1458 | 1475 |
|
1459 | 1476 | const uint64_t warm_cache_bytes = 200ULL * 1024 * 1024; |
1460 | 1477 | const uint64_t safety_bytes = 512ULL * 1024 * 1024; |
1461 | 1478 | const uint64_t core_bytes = |
1462 | 1479 | moe_hybrid_core_bytes_from_memory("laguna", gpu_free, gpu_total); |
1463 | 1480 |
|
| 1481 | + kvflash_resolve_drafter(); |
| 1482 | + const int kvf_pool = kvflash_pool_from_env( |
| 1483 | + max_context, kvflash_config(), kvflash_scorer_expected(), |
| 1484 | + make_kvflash_budget((int64_t)gpu_free)); |
| 1485 | + const auto kvf_dec = dflash::common::kvflash_placement_decision( |
| 1486 | + kv_bytes_per_tok, max_context, kvf_pool, |
| 1487 | + gpu_total, core_bytes, total_expert_bytes, |
| 1488 | + warm_cache_bytes, safety_bytes, /*draft_bytes=*/0); |
| 1489 | + const uint64_t kv_total = kvf_dec.kv_total; |
| 1490 | + const int kv_ctx_log = kvf_dec.kv_ctx; |
| 1491 | + placement_all_hot_full_kv_ = kvf_dec.all_hot_full_kv; |
| 1492 | + if (kvf_dec.pool_reduced) { |
| 1493 | + std::printf("[laguna][kvflash] placement reserves pool KV (%d tokens, " |
| 1494 | + "not max_ctx %d) -> experts stay hot\n", kvf_pool, max_context); |
| 1495 | + std::fflush(stdout); |
| 1496 | + } |
| 1497 | + |
1464 | 1498 | uint64_t expert_budget = 0; |
1465 | 1499 | if (gpu_total > core_bytes + kv_total + warm_cache_bytes + safety_bytes) { |
1466 | 1500 | expert_budget = gpu_total - core_bytes - kv_total - warm_cache_bytes - safety_bytes; |
@@ -1511,7 +1545,7 @@ bool LagunaBackend::init_hybrid_mode() { |
1511 | 1545 | gpu_total / 1024.0 / 1024.0 / 1024.0, |
1512 | 1546 | core_bytes / 1024.0 / 1024.0 / 1024.0, |
1513 | 1547 | kv_total / 1024.0 / 1024.0 / 1024.0, |
1514 | | - max_context, |
| 1548 | + kv_ctx_log, |
1515 | 1549 | warm_cache_bytes / 1024.0 / 1024.0, |
1516 | 1550 | safety_bytes / 1024.0 / 1024.0, |
1517 | 1551 | expert_budget / 1024.0 / 1024.0 / 1024.0, |
|
0 commit comments