Skip to content

Commit f7c7566

Browse files
committed
feat: restore D2H offload collection.
1 parent fd46ee1 commit f7c7566

24 files changed

Lines changed: 925 additions & 714 deletions

xllm/core/distributed_runtime/llm_engine.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ limitations under the License.
4040
#include "core/framework/config/parallel_config.h"
4141
#include "core/framework/config/service_config.h"
4242
#include "framework/block/block_utils.h"
43-
// hierarchy temporarily disabled during the block-manager refactor
44-
// #include "framework/block/hierarchy_block_manager_pool.h"
43+
#include "framework/block/hierarchy_block_manager_pool.h"
4544
#include "framework/kv_cache/kv_cache_estimation.h"
4645
#include "framework/kv_cache/kv_cache_shape.h"
4746
#include "framework/model/model_args.h"
@@ -566,19 +565,12 @@ bool LLMEngine::allocate_kv_cache(const KVCacheCapacity& kv_cache_cap) {
566565
kv_cache_cap.swa_count(), std::numeric_limits<uint32_t>::max())));
567566
}
568567

569-
if (options_.host_blocks_factor() > 1.0 || options_.enable_kvcache_store()) {
570-
// hierarchy temporarily disabled during the block-manager refactor.
571-
// host-offload / kvcache-store routes the device + host dual
572-
// KVCacheState through HierarchyBlockManagerPool, which is parked while
573-
// the composite block-manager refactor lands in smaller pieces. Until
574-
// then this path fails loudly rather than silently degrading to a
575-
// device-only pool.
576-
LOG(FATAL) << "host-offload / kvcache-store is temporarily disabled during "
577-
"the block-manager refactor (hierarchy rebuild in progress). "
578-
"Please disable --host_blocks_factor and "
579-
"--enable_kvcache_store for now.";
568+
if (options_.host_blocks_factor() > 1.0) {
569+
kv_cache_manager_ =
570+
std::make_unique<HierarchyBlockManagerPool>(options, this, dp_size_);
571+
} else {
572+
kv_cache_manager_ = std::make_unique<BlockManagerPool>(options, dp_size_);
580573
}
581-
kv_cache_manager_ = std::make_unique<BlockManagerPool>(options, dp_size_);
582574

583575
// init kv cache for each worker in parallel
584576
std::vector<folly::SemiFuture<bool>> futures;

xllm/core/framework/block/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ cc_library(
2323
linear_state_block_manager.h
2424
block_manager_impl.h
2525
concurrent_block_manager_impl.h
26-
# hierarchy temporarily disabled during the block-manager refactor
27-
# hierarchy_block_manager_pool.h
26+
hierarchy_block_manager_pool.h
2827
sliding_window_block_manager.h
2928
composite_block_manager.h
3029
SRCS
@@ -34,8 +33,7 @@ cc_library(
3433
linear_state_block_manager.cpp
3534
concurrent_block_manager_impl.cpp
3635
block_manager_impl.cpp
37-
# hierarchy temporarily disabled during the block-manager refactor
38-
# hierarchy_block_manager_pool.cpp
36+
hierarchy_block_manager_pool.cpp
3937
sliding_window_block_manager.cpp
4038
composite_block_manager.cpp
4139
DEPS

xllm/core/framework/block/hierarchy_block_manager_pool.cpp

Lines changed: 206 additions & 88 deletions
Large diffs are not rendered by default.

xllm/core/framework/block/hierarchy_block_manager_pool.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class HierarchyBlockManagerPool : public BlockManagerPool {
6868

6969
private:
7070
void allocate_host_shared(Sequence* sequence);
71+
// Move offload-eligible device/host KV block pairs out of the sequence into
72+
// offload_block_pair_queues_[dp_rank] for the next D2H transfer.
73+
void collect_offload_pairs(Sequence* sequence, int32_t dp_rank);
7174

7275
private:
7376
Engine* engine_;

xllm/core/framework/kv_cache/kv_cache_tensor_role.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class KVCacheTensorRole {
2929
CONV = 3,
3030
SSM = 4,
3131
INDEX_SCALE = 5,
32+
SWA = 6,
3233
INVALID = -1,
3334
};
3435

@@ -46,6 +47,8 @@ class KVCacheTensorRole {
4647
value_ = CONV;
4748
} else if (str == "SSM" || str == "ssm") {
4849
value_ = SSM;
50+
} else if (str == "SWA" || str == "swa") {
51+
value_ = SWA;
4952
} else {
5053
value_ = INVALID;
5154
}
@@ -74,6 +77,8 @@ class KVCacheTensorRole {
7477
return "conv";
7578
} else if (this->value_ == SSM) {
7679
return "ssm";
80+
} else if (this->value_ == SWA) {
81+
return "swa";
7782
} else {
7883
return "invalid";
7984
}

xllm/core/framework/kv_cache/kv_cache_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ HostPageAlignedRegion::HostPageAlignedRegion(size_t bytes) {
393393
CHECK(base_ptr != MAP_FAILED)
394394
<< "Failed to mmap host memory, size=" << total_bytes;
395395
if (mlock(base_ptr, total_bytes) != 0) {
396-
const int err = errno;
396+
const int32_t err = errno;
397397
munmap(base_ptr, total_bytes);
398398
base_ptr = nullptr;
399399
total_bytes = 0;

xllm/core/framework/kv_cache/kv_cache_utils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
#include <torch/torch.h>
2020

2121
#include <cstdint>
22+
#include <map>
2223
#include <memory>
2324
#include <optional>
2425
#include <string>
@@ -50,6 +51,7 @@ struct KVCacheCreateOptions {
5051
PROPERTY(torch::ScalarType, dtype) = torch::kBFloat16;
5152
// ssm dtype for linear attention layers
5253
PROPERTY(torch::ScalarType, ssm_dtype) = torch::kBFloat16;
54+
PROPERTY(double, host_blocks_factor) = 0.0;
5355
PROPERTY(int64_t, num_layers) = 0;
5456
// full attention interval for linear attention layers
5557
PROPERTY(int64_t, full_attention_interval) = 1;
@@ -106,6 +108,21 @@ struct KVCacheTensor {
106108
torch::Tensor tensor;
107109
};
108110

111+
using BlockTypeTensorMap = std::map<KVCacheTensorRole::Value, torch::Tensor>;
112+
113+
struct HostPageAlignedRegion {
114+
void* base_ptr = nullptr;
115+
size_t total_bytes = 0;
116+
117+
HostPageAlignedRegion() = default;
118+
explicit HostPageAlignedRegion(size_t bytes);
119+
HostPageAlignedRegion(const HostPageAlignedRegion&) = delete;
120+
HostPageAlignedRegion& operator=(const HostPageAlignedRegion&) = delete;
121+
HostPageAlignedRegion(HostPageAlignedRegion&& other) noexcept;
122+
HostPageAlignedRegion& operator=(HostPageAlignedRegion&& other) noexcept;
123+
~HostPageAlignedRegion();
124+
};
125+
109126
struct DeepSeekV4KVCacheTensors {
110127
torch::Tensor key_cache;
111128
torch::Tensor index_cache;

xllm/core/framework/kv_cache_transfer/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ cc_library(
4141
$<$<BOOL:${USE_NPU}>:llm_data_dist_transfer.h>
4242
$<$<BOOL:${USE_NPU}>:spec_kv_cache_transfer.h>
4343
kv_cache_store.h
44-
# hierarchy temporarily disabled during the block-manager refactor
45-
# hierarchy_kv_cache_transfer.h
44+
hierarchy_kv_cache_transfer.h
4645
$<$<OR:$<BOOL:${USE_NPU}>,$<BOOL:${USE_MLU}>,$<BOOL:${USE_DCU}>>:mooncake_transfer_engine.h>
4746
$<$<OR:$<BOOL:${USE_NPU}>,$<BOOL:${USE_MLU}>,$<BOOL:${USE_DCU}>>:mooncake_kv_cache_transfer.h>
4847
$<$<BOOL:${USE_NPU}>:mooncake_weight_transfer.h>
@@ -51,8 +50,7 @@ cc_library(
5150
$<$<BOOL:${USE_NPU}>:llm_data_dist_transfer.cpp>
5251
$<$<BOOL:${USE_NPU}>:spec_kv_cache_transfer.cpp>
5352
kv_cache_store.cpp
54-
# hierarchy temporarily disabled during the block-manager refactor
55-
# hierarchy_kv_cache_transfer.cpp
53+
hierarchy_kv_cache_transfer.cpp
5654
$<$<OR:$<BOOL:${USE_NPU}>,$<BOOL:${USE_MLU}>,$<BOOL:${USE_DCU}>>:mooncake_transfer_engine.cpp>
5755
$<$<OR:$<BOOL:${USE_NPU}>,$<BOOL:${USE_MLU}>,$<BOOL:${USE_DCU}>>:mooncake_kv_cache_transfer.cpp>
5856
$<$<BOOL:${USE_NPU}>:mooncake_weight_transfer.cpp>
@@ -61,6 +59,7 @@ cc_library(
6159
:kv_cache
6260
:push_route
6361
:xtensor
62+
:platform
6463
$<$<BOOL:${USE_NPU}>:graph>
6564
glog::glog
6665
$<$<BOOL:${USE_NPU}>:llm_datadist>

0 commit comments

Comments
 (0)