Skip to content

Commit 33ed5b2

Browse files
datenglincopybara-github
authored andcommitted
Supported heterogeneous block sizes and block merging in Raiden.
PiperOrigin-RevId: 945789358
1 parent e586814 commit 33ed5b2

7 files changed

Lines changed: 246 additions & 167 deletions

File tree

tpu_raiden/core/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ cc_library(
254254
"//tpu_raiden/kv_cache:kv_cache_manager_base",
255255
"//tpu_raiden/transport:block_transport",
256256
"@com_google_absl//absl/container:flat_hash_map",
257+
"@com_google_absl//absl/container:flat_hash_set",
257258
"@com_google_absl//absl/log",
258259
"@com_google_absl//absl/status",
259260
"@com_google_absl//absl/status:statusor",

tpu_raiden/core/kv_cache_manager_with_transfer.cc

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include <vector>
6666

6767
#include "absl/container/flat_hash_map.h"
68+
#include "absl/container/flat_hash_set.h"
6869
#include "absl/log/log.h"
6970
#include "absl/status/status.h"
7071
#include "absl/status/statusor.h"
@@ -379,8 +380,18 @@ static CopyPlan BuildLoadCopyPlan(
379380
plan.h2d_host_block_ids.reserve(local_order.size());
380381
for (size_t i = 0; i < local_order.size(); ++i) {
381382
const size_t original_idx = local_order[i];
382-
plan.h2d_local_block_ids.push_back(local_block_ids[original_idx]);
383-
plan.h2d_host_block_ids.push_back(local_host_block_ids[original_idx]);
383+
int64_t local_bid = local_block_ids[original_idx];
384+
int64_t host_bid = local_host_block_ids[original_idx];
385+
if (plan.h2d_local_block_ids.empty() ||
386+
plan.h2d_local_block_ids.back() != local_bid) {
387+
plan.h2d_local_block_ids.push_back(local_bid);
388+
plan.h2d_host_block_ids.push_back(host_bid);
389+
} else {
390+
if (plan.h2d_host_block_ids.back() != host_bid) {
391+
throw std::invalid_argument(
392+
"Duplicate local block IDs must map to the same host block ID");
393+
}
394+
}
384395
}
385396

386397
plan.h2d_copy =
@@ -633,19 +644,31 @@ absl::Status KVCacheManagerWithTransfer::RegisterActivePlan(
633644
recv_entry.req_id = req_id;
634645

635646
int64_t total_blocks = 0;
647+
absl::flat_hash_set<int> unique_dst_blocks;
636648
for (const auto& [src_replica_idx, schedule] :
637649
request.shard_push_schedules()) {
638-
std::set<int> unique_blocks_from_this_source;
650+
absl::flat_hash_set<std::pair<int, int>> unique_transfers_from_this_source;
639651
for (const auto& push_entry : schedule.entries()) {
640652
recv_entry.host_to_chip[push_entry.dst_block_id()] =
641653
push_entry.dst_block_id();
642-
unique_blocks_from_this_source.insert(push_entry.dst_block_id());
654+
unique_transfers_from_this_source.insert(
655+
{push_entry.src_block_id(), push_entry.dst_block_id()});
656+
unique_dst_blocks.insert(push_entry.dst_block_id());
643657
}
644-
total_blocks += unique_blocks_from_this_source.size();
658+
total_blocks += unique_transfers_from_this_source.size();
645659
}
646660
recv_entry.total_blocks = total_blocks;
647661
recv_entry.num_completed_blocks = 0;
648662
recv_entry.deadline = DeadlineFromNow();
663+
664+
// Populate h2d_copy spec for unique destination blocks
665+
std::vector<int64_t> h2d_host_block_ids(unique_dst_blocks.begin(),
666+
unique_dst_blocks.end());
667+
std::vector<int64_t> h2d_local_block_ids =
668+
h2d_host_block_ids; // 1-to-1 mapping
669+
recv_entry.h2d_copy =
670+
BuildCoalescedCopySpec(h2d_host_block_ids, h2d_local_block_ids);
671+
649672
if (total_blocks > 0) {
650673
active_recv_entries_[uuid] = std::move(recv_entry);
651674
LOG(INFO) << "RegisterActivePlan (Receiver): Populated "
@@ -765,7 +788,9 @@ void KVCacheManagerWithTransfer::StartRead(
765788
host_block_ids = *local_host_block_ids;
766789
} else if (!local_block_ids.empty()) {
767790
std::lock_guard<std::mutex> lock(mu_);
768-
if (static_cast<int64_t>(local_block_ids.size()) > max_blocks_ ||
791+
absl::flat_hash_set<int64_t> unique_local_bids(local_block_ids.begin(),
792+
local_block_ids.end());
793+
if (static_cast<int64_t>(unique_local_bids.size()) > max_blocks_ ||
769794
free_slots_.empty()) {
770795
// Request larger than a slot, or staging pool exhausted: surface as a
771796
// recv failure (the connector can recompute) rather than throwing.
@@ -774,9 +799,19 @@ void KVCacheManagerWithTransfer::StartRead(
774799
}
775800
Slot slot = AcquireSlotLocked();
776801
recv_slot = slot.slot_idx;
802+
absl::flat_hash_map<int64_t, int64_t> local_to_host;
803+
size_t host_block_idx = 0;
777804
host_block_ids.reserve(local_block_ids.size());
778805
for (size_t k = 0; k < local_block_ids.size(); ++k) {
779-
host_block_ids.push_back(slot.block_ids[k]);
806+
int64_t local_bid = local_block_ids[k];
807+
auto it = local_to_host.find(local_bid);
808+
if (it == local_to_host.end()) {
809+
int64_t host_bid = slot.block_ids[host_block_idx++];
810+
local_to_host[local_bid] = host_bid;
811+
host_block_ids.push_back(host_bid);
812+
} else {
813+
host_block_ids.push_back(it->second);
814+
}
780815
}
781816
}
782817
CopyPlan load_plan =

tpu_raiden/kv_cache/kv_cache_manager_base.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,8 @@ KVCacheManagerBase::GetBlockChunks(size_t layer_idx, size_t shard_idx,
12671267
absl::Span<const int64_t> block_ids,
12681268
size_t total_bytes, uint64_t uuid,
12691269
int64_t sender_node_id,
1270-
absl::string_view peer) {
1270+
absl::string_view peer,
1271+
int64_t src_block_id) {
12711272
RegisteredPlan plan;
12721273
bool has_plan = false;
12731274
{
@@ -1337,10 +1338,16 @@ KVCacheManagerBase::GetBlockChunks(size_t layer_idx, size_t shard_idx,
13371338
if (sender_node_id != -1) {
13381339
found_src_shard = static_cast<int>(sender_node_id);
13391340
} else {
1341+
// If src_block_id is provided, we use it to disambiguate which source
1342+
// block we are receiving. This is crucial for heterogeneous block sizes
1343+
// (merging) where multiple source blocks target the same dst_block_id.
1344+
// If src_block_id is -1, we fall back to matching only on dst_block_id.
13401345
for (const auto& [src_shard, src_schedule] : schedules) {
13411346
for (const auto& entry : src_schedule.entries()) {
13421347
if (static_cast<size_t>(entry.dst_shard_idx()) == shard_idx &&
1343-
static_cast<size_t>(entry.dst_block_id()) == block_id) {
1348+
static_cast<size_t>(entry.dst_block_id()) == block_id &&
1349+
(src_block_id == -1 ||
1350+
static_cast<size_t>(entry.src_block_id()) == src_block_id)) {
13441351
found_src_shard = src_shard;
13451352
break;
13461353
}
@@ -1355,7 +1362,9 @@ KVCacheManagerBase::GetBlockChunks(size_t layer_idx, size_t shard_idx,
13551362
const auto& schedule = schedule_found_it->second;
13561363
for (const auto& entry : schedule.entries()) {
13571364
if (static_cast<size_t>(entry.dst_shard_idx()) == shard_idx &&
1358-
static_cast<size_t>(entry.dst_block_id()) == block_id) {
1365+
static_cast<size_t>(entry.dst_block_id()) == block_id &&
1366+
(src_block_id == -1 ||
1367+
static_cast<size_t>(entry.src_block_id()) == src_block_id)) {
13591368
if (!peer.empty() && entry.dst_peer() != peer) {
13601369
continue;
13611370
}

tpu_raiden/kv_cache/kv_cache_manager_base.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,14 @@ class KVCacheManagerBase : public tpu_raiden::RaidenManagerBase {
219219
"RegisterRecv not implemented in base class");
220220
}
221221

222+
// Resolves the host memory pointers (BlockChunks) for the given block_ids.
223+
// If `src_block_id` is provided (not -1), it is used to filter the active plan
224+
// to resolve the correct chunk offset, which is necessary when multiple
225+
// source blocks merge into a single destination block (heterogeneous block sizes).
222226
std::vector<tpu_raiden::transport::BlockChunk> GetBlockChunks(
223227
size_t layer_idx, size_t shard_idx, absl::Span<const int64_t> block_ids,
224228
size_t total_bytes, uint64_t uuid, int64_t sender_node_id = -1,
225-
absl::string_view peer = "") override;
229+
absl::string_view peer = "", int64_t src_block_id = -1) override;
226230

227231
bool IsDramDestination(uint64_t uuid) const;
228232

0 commit comments

Comments
 (0)