Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/content/docs/en/cli_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Current support covers ordinary non-PD text inference with prefix caching enable
| `prefetch_batch_size` | `uint32` | `2` | Copy batch size for prefetching from KV Cache Store. |
| `layers_wise_copy_batchs` | `uint32` | `4` | Number of batches for layer-wise H2D copy. |
| `host_blocks_factor` | `double` | `0.0` | Host block factor, for example `host block num = host_blocks_factor * hbm block num`. |
| `enable_fabric_host_kv` | `bool` | `false` | Use Ascend Fabric H2D/D2H for Host KV. Requires an Ascend910_9382, Ascend910_9392 or Ascend910B2, the matching `xllm-kernel` package, reserved 2 MiB HugeTLB pages for payloads and controls, ND device storage with contiguous block rows and 32-byte-aligned copy units (one history row for Conv checkpoints), and `host_blocks_factor > 1`. |
| `enable_fabric_host_kv` | `bool` | `false` | Use Ascend Fabric H2D/D2H for Host KV. Requires an Ascend910_9382, Ascend910_9392 or Ascend910B2, the matching `xllm-kernel` package, reserved 2 MiB HugeTLB pages for payloads and controls, ND device storage with contiguous block rows and 32-byte-aligned copy units (one history row for Conv checkpoints), and `host_blocks_factor > 1`. With Store disabled, Workers on one host in the same IPC namespace share one Host block allocator and one payload per distinct KV shard. Control slots and device mappings remain per Worker. |
| `enable_kvcache_store` | `bool` | `false` | Whether to enable KV Cache Store. |
| `store_protocol` | `string` | `"tcp"` | KV Cache Store protocol, for example `tcp` or `rdma`. |
| `store_rdma_devices` | `string` | `""` | Comma-separated RDMA HCAs for the embedded xLLM Store client. Empty means Mooncake auto-discovery. |
Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/zh/cli_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ xLLM 使用 gflags 管理服务启动参数。`--model <PATH>` 是唯一必填
| `prefetch_batch_size` | `uint32` | `2` | 从 KV Cache Store 预取并拷贝的 batch size。 |
| `layers_wise_copy_batchs` | `uint32` | `4` | 按层执行 H2D 拷贝的 batch 数。 |
| `host_blocks_factor` | `double` | `0.0` | host block 系数,例如 `host block num = host_blocks_factor * hbm block num`。 |
| `enable_fabric_host_kv` | `bool` | `false` | 使用昇腾 Fabric H2D/D2H 恢复和写回 Host KV。支持 Ascend910_9382、Ascend910_9392 和 Ascend910B2。需要安装配套的 `xllm-kernel`,为数据和控制区预留 2 MiB HugeTLB 页,设备存储须为 ND 格式,块内数据连续,拷贝单元的字节数须为 32 的倍数(Conv checkpoint 以一行历史状态为单位),且 `host_blocks_factor > 1`。 |
| `enable_fabric_host_kv` | `bool` | `false` | 使用昇腾 Fabric H2D/D2H 恢复和写回 Host KV。支持 Ascend910_9382、Ascend910_9392 和 Ascend910B2。需要安装配套的 `xllm-kernel`,为数据和控制区预留 2 MiB HugeTLB 页,设备存储须为 ND 格式,块内数据连续,拷贝单元的字节数须为 32 的倍数(Conv checkpoint 以一行历史状态为单位),且 `host_blocks_factor > 1`。关闭 Store 时,同机同一 IPC 命名空间内的 Worker 共用一套 Host 块号,每个不同的 KV 分片只存一份。各 Worker 持有自己的控制槽和设备映射。 |
| `enable_kvcache_store` | `bool` | `false` | 是否启用 KV Cache Store。 |
| `store_protocol` | `string` | `"tcp"` | KV Cache Store 协议,例如 `tcp`、`rdma`。 |
| `store_rdma_devices` | `string` | `""` | xLLM Worker 内嵌 Store client 使用的 RDMA HCA 列表,多个设备以逗号分隔;空值表示由 Mooncake 自动发现。 |
Expand Down
17 changes: 16 additions & 1 deletion tests/core/distributed_runtime/collective_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ namespace {

void register_worker(CollectiveService& service,
int32_t rank,
const std::string& address) {
const std::string& address,
const std::string& ipc_namespace = "") {
proto::AddressInfo worker;
proto::CommUniqueIdList response;
worker.set_global_rank(rank);
worker.set_address(address);
worker.set_ipc_namespace(ipc_namespace);
service.Sync(nullptr, &worker, &response, nullptr);
}

Expand All @@ -55,6 +57,19 @@ TEST(CollectiveServiceTest, RendezvousNeedsNoDeviceOrCommunicator) {
EXPECT_EQ(addresses.at(1), "127.0.0.1:9001");
}

TEST(CollectiveServiceTest, SharesHostStorageOnlyWithinOneIpcNamespace) {
CollectiveService service(2);
register_worker(service, 0, "127.0.0.1:9000", "node-a/ipc-1");
register_worker(service, 1, "127.0.0.1:9001");
EXPECT_FALSE(service.workers_share_ipc_namespace());
register_worker(service, 1, "127.0.0.1:9001", "node-b/ipc-1");
EXPECT_FALSE(service.workers_share_ipc_namespace());
register_worker(service, 1, "127.0.0.1:9001", "node-a/ipc-2");
EXPECT_FALSE(service.workers_share_ipc_namespace());
register_worker(service, 1, "127.0.0.1:9001", "node-a/ipc-1");
EXPECT_TRUE(service.workers_share_ipc_namespace());
}

TEST(CollectiveServiceTest, MissingWorkersTimeOutWithoutPartialAddresses) {
CollectiveService service(/*total_num=*/2);
register_worker(service, /*rank=*/0, "127.0.0.1:9000");
Expand Down
81 changes: 78 additions & 3 deletions tests/core/framework/block/cache_tier_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CacheTierTestPeer final {
static const CompositeBlockManager::LegacyLeafMap& host_leaves(
const BlockManagerPool& pool,
int32_t dp_rank = 0) {
return pool.host_prefix_cache_->host_managers_[dp_rank]->leaf_entries();
return pool.host_prefix_cache_->host_manager(dp_rank)->leaf_entries();
}

static std::vector<BlockTransferInfo> pending_load_infos(
Expand Down Expand Up @@ -324,18 +324,31 @@ class RecordingHostRestoreCoordinator final : public KVTransferCoordinatorBase {

class PrefixCopyRecorder final : public KVTransferCoordinatorBase {
public:
explicit PrefixCopyRecorder(bool delay_load = false)
: delay_load_(delay_load) {}
void finish_load() {
pending_load_.setValue(static_cast<uint32_t>(loads_.size()));
}

std::vector<folly::SemiFuture<uint32_t>> transfer_kv_blocks(
uint32_t /*dp_rank*/,
uint32_t dp_rank,
const std::vector<BlockTransferInfo>& infos) override {
write_dp = dp_rank;
writes_ = infos;
return completed(infos.size());
}

std::vector<folly::SemiFuture<uint32_t>> transfer_kv_blocks(
uint32_t /*dp_rank*/,
uint32_t dp_rank,
uint64_t /*batch_id*/,
const std::vector<BlockTransferInfo>& infos) override {
load_dp = dp_rank;
loads_ = infos;
if (delay_load_) {
std::vector<folly::SemiFuture<uint32_t>> results;
results.emplace_back(pending_load_.getSemiFuture());
return results;
}
return completed(infos.size());
}

Expand All @@ -350,7 +363,12 @@ class PrefixCopyRecorder final : public KVTransferCoordinatorBase {
const std::vector<BlockTransferInfo>& writes() const { return writes_; }
const std::vector<BlockTransferInfo>& loads() const { return loads_; }

int32_t write_dp = -1;
int32_t load_dp = -1;

private:
bool delay_load_;
folly::Promise<uint32_t> pending_load_;
static std::vector<folly::SemiFuture<uint32_t>> completed(size_t count) {
std::vector<folly::SemiFuture<uint32_t>> results;
results.reserve(1);
Expand Down Expand Up @@ -2783,6 +2801,63 @@ TEST_F(CacheTierManagerTest, HostPrefixCacheSavesAndLoadsAcrossDeviceReuse) {
EXPECT_EQ(CacheTierTestPeer::pending_offload_pair_count(pool), 0u);
}

TEST_F(CacheTierManagerTest,
SharedHostSlotSurvivesCrossDpRestoreUntilLastReader) {
auto transfer = std::make_shared<PrefixCopyRecorder>(/*delay_load=*/true);
auto options = make_flat_kv_options();
auto journal = std::make_shared<KVCacheEventJournal>();
// Slot zero is reserved, so the shared Host pool has one usable slot.
options.share_fabric_host_storage(true).host_num_blocks(2).event_journal(
journal);
BlockManagerPool pool(
options, 2, nullptr, std::nullopt, std::nullopt, transfer);
ASSERT_EQ(CacheTierTestPeer::host_block_managers(pool).size(), 1u);
auto* host = CacheTierTestPeer::host_leaves(pool).at(BlockType::KV).leaf;
ASSERT_EQ(host->num_total_blocks(), 1u);
const std::vector<int32_t> tokens(129, 71);
Sequence source = make_test_sequence(0, tokens);
source.set_dp_rank(1);
ASSERT_TRUE(pool.allocate(&source, 128));
source.kv_state().set_kv_cache_tokens_num(128);
pool.deallocate(&source);
pool.transfer_blocks();
ASSERT_EQ(transfer->writes().size(), 1u);
EXPECT_EQ(transfer->write_dp, 1);
EXPECT_EQ(host->num_blocks_in_prefix_cache(), 0u);
pool.drain_prefetch_completions();

std::vector<int32_t> advertised;
const auto snapshot = journal->read("", 0, 8, "cpu");
for (const auto& event : snapshot.at("events")) {
advertised.push_back(event.at("dp_rank").get<int32_t>());
}
std::sort(advertised.begin(), advertised.end());
EXPECT_EQ(advertised, (std::vector<int32_t>{0, 1}));
Sequence consumer = make_test_sequence(1, tokens);
consumer.set_dp_rank(0);
pool.allocate_shared(&consumer);
EXPECT_EQ(consumer.kv_state().kv_cache_tokens_num(), 0u);
EXPECT_EQ(consumer.host_kv_state().kv_cache_tokens_num(), 128u);
ASSERT_TRUE(pool.allocate(&consumer, 128));
BatchGroup batches(2);
pool.transfer_blocks(batches);
pool.deallocate_without_cache(&consumer);
EXPECT_EQ(transfer->load_dp, 0);
EXPECT_EQ(host->evict_prefix_cache(1), 0u);
EXPECT_TRUE(host->allocate(1).empty());
transfer->finish_load();
pool.drain_prefetch_completions();
ASSERT_EQ(transfer->loads().size(), 1u);
EXPECT_EQ(transfer->loads()[0].src_block_id,
transfer->writes()[0].dst_block_id);
EXPECT_EQ(host->evict_prefix_cache(1), 1u);
EXPECT_TRUE(journal->read("", 0, 8, "cpu").at("events").empty());
auto next = host->allocate(1);
ASSERT_EQ(next.size(), 1u);
EXPECT_EQ(next[0].id(), transfer->loads()[0].src_block_id);
Block::release(std::move(next));
}

TEST_F(CacheTierManagerTest,
HostPrefixCacheShutdownDiscardsLateLoadsForRetiredSequences) {
auto transfer = std::make_shared<RecordingHostRestoreCoordinator>(
Expand Down
111 changes: 111 additions & 0 deletions tests/core/kv_cache/transfer/hierarchy_kv_transfer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ limitations under the License.

#if defined(USE_NPU)
#include <acl/acl.h>

#include "core/framework/parallel_state/process_group.h"
#endif

#include <array>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <memory>
#include <string_view>
Expand Down Expand Up @@ -346,6 +350,113 @@ TEST(HierarchyKVTransferTest, FabricRestoresValuesAfterSourceReuse) {
check_host_restore_after_source_reuse(/*fabric=*/true);
}

// Launch with torchrun --standalone --nproc-per-node=4 --no-python and this
// test's gtest filter. A second rendezvous port is supplied for the C++ group.
TEST(HierarchyKVTransferTest, FabricRestoresSharedShardsAcrossTpLayouts) {
const char* port = std::getenv("XLLM_TEST_PROCESS_GROUP_PORT");
if (port == nullptr || std::getenv("LOCAL_WORLD_SIZE") == nullptr ||
std::string_view(std::getenv("LOCAL_WORLD_SIZE")) != "4") {
GTEST_SKIP() << "Requires four local ranks and a C++ rendezvous port.";
}
const auto available = fabric_available(16);
if (!available) GTEST_SKIP() << available.message();

const int rank = std::stoi(std::getenv("LOCAL_RANK"));
Device device(rank);
device.set_device();
device.init_device_context();
auto group = create_process_group(rank,
4,
4,
std::stoi(port),
false,
"127.0.0.1",
"fabric_host_test",
device.unwrap());
auto stream = device.current_stream();
struct Layout {
int tp_size;
int kv_heads;
std::array<int, 4> expected_writer_tp;
};
const Layout layouts[] = {{2, 1, {0, 0, 0, 0}},
{2, 4, {0, 1, 0, 1}},
{4, 2, {0, 0, 2, 2}},
{4, 4, {0, 1, 2, 3}}};
for (const auto& [tp_size, kv_heads, expected_writer_tp] : layouts) {
SCOPED_TRACE(tp_size);
SCOPED_TRACE(kv_heads);
const int tp_rank = rank % tp_size;
const int source_dp_start = 4 - tp_size;
ModelArgs model;
model.model_type("test_model")
.n_layers(1)
.n_heads(24)
.n_kv_heads(kv_heads)
.head_dim(256);
KVCacheCapacity capacity;
capacity.n_blocks(2).block_size(128);
KVCacheShape shape(capacity, model, tp_size);
shape.set_shared_fabric_host_storage(true);
KVCacheCreateOptions create;
create.device(device.unwrap())
.dtype(torch::kBFloat16)
.num_layers(1)
.model_type("test_model");
std::vector<KVCache> caches;
allocate_kv_caches(caches, shape, create);

HierarchyKVTransfer::Options options;
options.world_group = group.get();
options.workers_per_dp(tp_size)
.layers(1)
.enable_fabric_host_kv(true)
.host_blocks_factor(2.0);
HierarchyKVTransfer transfer(options, device.unwrap());
HierarchyKVTransfer::CacheRegistration registration;
registration.tensor_layout.tp_size = tp_size;
registration.tensor_layout.tp_rank = tp_rank;
registration.tensor_layout.kv_head_count = kv_heads;
registration.kv_cache_shape = shape;
registration.device_kv_caches = &caches;
registration.create_options = create;
registration.producer_stream = stream.get();
transfer.register_cache(std::move(registration));
ASSERT_TRUE(transfer.finalize_registration());

auto key = caches.front().get_k_cache();
auto value = caches.front().get_v_cache();
key.zero_();
value.zero_();
if (rank >= source_dp_start) {
key[0].fill_(17 + tp_rank);
value[0].fill_(53 + tp_rank);
}
BlockTransferInfo save(/*src=*/0, /*dst=*/0);
save.transfer_type = TransferType::D2H2G;
// Later replicas carry distinct poison values. They must leave the owner's
// Host row intact, while genuine shards must each write their own bytes.
for (int writer = source_dp_start; writer < 4; ++writer) {
if (rank == writer) EXPECT_EQ(transfer.transfer_kv_blocks(0, {save}), 1u);
group->barrier();
}
key[0].zero_();
value[0].zero_();
if (rank < tp_size) {
BlockTransferInfo load(/*src=*/0, /*dst=*/1);
load.transfer_type = TransferType::H2D;
EXPECT_EQ(transfer.transfer_kv_blocks(1, {load}), 1u);
EXPECT_TRUE(transfer.wait_for_load(1));
const int shard = expected_writer_tp[rank];
EXPECT_TRUE(key[1].cpu().eq(17 + shard).all().item<bool>());
EXPECT_TRUE(value[1].cpu().eq(53 + shard).all().item<bool>());
EXPECT_TRUE(key[0].cpu().eq(0).all().item<bool>());
}
group->barrier();
transfer.shutdown();
}
}

TEST(HierarchyKVTransferTest, FabricRestoresHybridTargetAndDraftCheckpoints) {
const auto available = fabric_available(12);
if (!available) {
Expand Down
Loading
Loading