Skip to content
Merged
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
273 changes: 227 additions & 46 deletions helios/native/roce_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ _Static_assert(CTRL_CREDIT >= CTRL_READY_VL + ROCE_VARLEN_MAX_CHUNKS &&
#define RING2_MAX_OUTSTANDING 6u
_Static_assert((RING2_SIGNAL_STRIDE & (RING2_SIGNAL_STRIDE - 1u)) == 0,
"RING2_SIGNAL_STRIDE must be a power of two");
// Hardware-hairpin multi-hop transit pacing: keep per-(HCA, peer) in-flight
// bytes across all lanes strictly below ConnectX-7's 512 KiB internal hairpin
// RQ SRAM so multi-MB pipelined and varlen writes stay lossless on stock
// mode legacy + Global Pause without CPU mailbox relaying or PFC/switchdev.
#define HAIRPIN_SEG_BYTES (64u * 1024u)
#define HAIRPIN_MAX_INFLIGHT_SEGS 2u
_Static_assert(HAIRPIN_MAX_INFLIGHT_SEGS < ROCE_RESERVE_OPS,
"hairpin pacing cap must fit within local origin reserve limit");
// run_pipe_op phase mask: reduce-scatter chunks, all-gather chunks.
#define PIPE_PHASE_RS 1u
#define PIPE_PHASE_AG 2u
Expand Down Expand Up @@ -343,6 +351,10 @@ typedef struct {
int8_t relay_via[ROCE_MAX_PEERS];
int8_t relay_to[ROCE_MAX_PEERS];
uint64_t relay_forwarded;
// Hardware-hairpin transit peer mask (roce_set_hairpin_peers): non-zero for peers reached
// through intermediate NIC hairpinning rather than a direct cable.
int hairpin_pacing;
uint8_t hairpin_peer[ROCE_MAX_PEERS];
char err[512];
} roce_ctx_t;

Expand Down Expand Up @@ -855,6 +867,31 @@ int roce_set_relay(roce_ctx_t* c, const int32_t* plan, int n) {
return 0;
}

// Mark peers reached across intermediate NIC hardware hairpinning (non-zero in
// `peers[p]`) so multi-MB pipelined and variable-length writes to those peers
// are posted after 1-hop neighbors and paced to HAIRPIN_MAX_INFLIGHT_SEGS
// (128 KiB) per (HCA, peer). Call before roce_start on non-ring transports.
int roce_set_hairpin_peers(roce_ctx_t* c, const uint8_t* peers, int n) {
if (c == NULL) {
return -1;
}
if (c->started || c->ring != NULL || peers == NULL || n != c->world) {
snprintf(c->err, sizeof(c->err),
"hairpin peer mask requires a non-ring transport before start with %d entries, got %d",
c->world, n);
return -1;
}
int any = 0;
for (int p = 0; p < c->world; p++) {
c->hairpin_peer[p] = (p != c->rank && peers[p] != 0) ? 1u : 0u;
if (c->hairpin_peer[p]) {
any = 1;
}
}
c->hairpin_pacing = any;
return 0;
}

static int drain_cq(roce_ctx_t* c, int h) {
struct ibv_wc wc[32];
int n = ibv_poll_cq(c->hca[h].cq, 32, wc);
Expand Down Expand Up @@ -1259,6 +1296,131 @@ static int post_striped(roce_ctx_t* c, int p, const uint8_t* send, uint64_t remo
return 0;
}

static uint32_t peer_outstanding(const roce_ctx_t* c, int h, int p) {
uint32_t total = 0;
for (int l = 0; l < c->n_lane; l++) {
total += c->hca[h].outstanding[p][l];
}
return total;
}

// Write `packs` 16-byte packs to a multi-hop hardware-hairpin peer `p`,
// sub-segmenting each rail's stripe into <= HAIRPIN_SEG_BYTES signaled writes
// capped at HAIRPIN_MAX_INFLIGHT_SEGS (128 KiB) in flight per (HCA, peer)
// across all lanes. Rails interleave segment-by-segment so multi-rail arcs
// progress concurrently; each rail's 4-byte readiness seq flag chains inline
// onto its final segment on the same reliable QP.
static int post_striped_paced(roce_ctx_t* c, int p, const uint8_t* send, uint64_t remote_off,
uint32_t packs, uint64_t flag_off, uint32_t seq, int lane) {
volatile uint32_t* ctrl = (volatile uint32_t*)(c->region + c->ctrl_off);
uint32_t seq_copy = seq;
uint64_t remote = c->peer_addr[p];
uint32_t stripe_bytes[ROCE_MAX_RAILS];
uint64_t byte_offset[ROCE_MAX_RAILS];
uint32_t posted[ROCE_MAX_RAILS];
int done[ROCE_MAX_RAILS];
uint32_t pack_offset = 0;
int remaining = c->n_rail;
for (int k = 0; k < c->n_rail; k++) {
uint32_t stripe_packs = packs / (uint32_t)c->n_rail;
if ((uint32_t)k < packs % (uint32_t)c->n_rail) {
stripe_packs += 1;
}
stripe_bytes[k] = stripe_packs * 16;
byte_offset[k] = (uint64_t)pack_offset * 16;
posted[k] = 0;
done[k] = 0;
pack_offset += stripe_packs;
}
while (remaining > 0) {
int progressed = 0;
for (int k = 0; k < c->n_rail; k++) {
if (done[k]) {
continue;
}
int h = c->rail_hca[p][k];
roce_hca_t* hca = &c->hca[h];
if (hca->outstanding[p][lane] >= ROCE_RESERVE_OPS ||
peer_outstanding(c, h, p) >= HAIRPIN_MAX_INFLIGHT_SEGS) {
continue;
}
uint32_t rem = stripe_bytes[k] - posted[k];
uint32_t seg = rem > HAIRPIN_SEG_BYTES ? HAIRPIN_SEG_BYTES : rem;
int last = (posted[k] + seg == stripe_bytes[k]);
struct ibv_sge flag_sge = {
.addr = (uint64_t)(uintptr_t)&seq_copy,
.length = 4,
.lkey = 0,
};
struct ibv_send_wr flag_wr;
memset(&flag_wr, 0, sizeof(flag_wr));
flag_wr.wr_id = (uint64_t)p | ((uint64_t)lane << 8);
flag_wr.sg_list = &flag_sge;
flag_wr.num_sge = 1;
flag_wr.opcode = IBV_WR_RDMA_WRITE;
flag_wr.send_flags = IBV_SEND_SIGNALED | IBV_SEND_INLINE;
flag_wr.wr.rdma.remote_addr = remote + flag_off + (uint64_t)k * ROCE_FLAG_STRIDE;
flag_wr.wr.rdma.rkey = c->peer_rkey[k][p];

struct ibv_send_wr data_wr;
struct ibv_sge data_sge;
struct ibv_send_wr* first_wr = &flag_wr;
if (seg != 0) {
data_sge = (struct ibv_sge){
.addr = (uint64_t)(uintptr_t)(send + byte_offset[k] + posted[k]),
.length = seg,
.lkey = hca->mr->lkey,
};
memset(&data_wr, 0, sizeof(data_wr));
data_wr.wr_id = (uint64_t)p | ((uint64_t)lane << 8);
data_wr.sg_list = &data_sge;
data_wr.num_sge = 1;
data_wr.opcode = IBV_WR_RDMA_WRITE;
if (seg <= hca->max_inline) {
data_wr.send_flags = IBV_SEND_INLINE;
}
if (last) {
data_wr.next = &flag_wr;
} else {
data_wr.send_flags |= IBV_SEND_SIGNALED;
}
data_wr.wr.rdma.remote_addr = remote + remote_off + byte_offset[k] + posted[k];
data_wr.wr.rdma.rkey = c->peer_rkey[k][p];
first_wr = &data_wr;
}
struct ibv_send_wr* bad = NULL;
int rc = ibv_post_send(hca->qp[p][lane], first_wr, &bad);
if (rc != 0) {
set_err(c, "ibv_post_send(paced)", rc);
return -1;
}
hca->outstanding[p][lane] += 1;
hca->bytes_posted += seg;
posted[k] += seg;
if (last) {
done[k] = 1;
remaining -= 1;
}
progressed = 1;
}
if (!progressed) {
if (drain_all(c) != 0) {
return -1;
}
if (__atomic_load_n(&ctrl[CTRL_ERROR], __ATOMIC_ACQUIRE) != 0) {
snprintf(c->err, sizeof(c->err), "RoCE paced write to rank %d abandoned: kernel timed out",
p);
return -1;
}
if (!atomic_load_explicit(&c->running, memory_order_relaxed)) {
snprintf(c->err, sizeof(c->err), "RoCE proxy stopped during paced write to rank %d", p);
return -1;
}
}
}
return 0;
}

static int drain_cqs(roce_ctx_t* c) {
for (int h = 0; h < c->n_hca; h++) {
if (drain_cq(c, h) != 0) {
Expand Down Expand Up @@ -1293,10 +1455,10 @@ static int post_op(roce_ctx_t* c, uint32_t seq, uint32_t nbytes) {
int passes = c->ring != NULL ? 2 : 1;
for (int pass = 0; pass < passes; pass++) {
int want_relayed = (c->ring != NULL && pass == 0);
for (int p = 0; p < c->world; p++) {
if (p == c->rank) {
continue;
}
// Rotate destination order by rank so each step forms a 1:1 permutation across peers instead
// of synchronizing (world - 1):1 incast onto rank 0, 1, ...
for (int step = 1; step < c->world; step++) {
int p = (c->rank + step) % c->world;
if (c->ring != NULL && (ring_relayed(c, p) != 0) != want_relayed) {
continue;
}
Expand Down Expand Up @@ -1434,31 +1596,43 @@ static inline uint32_t shard_start(const roce_ctx_t* c, uint32_t total, int j) {
static int post_pipe_chunk(roce_ctx_t* c, uint32_t seq, int phase, uint32_t chunk, uint32_t total,
uint32_t chunk_packs) {
const uint8_t* send = c->region + c->pipe_send_off;
for (int p = 0; p < c->world; p++) {
if (p == c->rank || (phase == 1 && c->relay && c->relay_via[p] >= 0)) {
continue;
}
int shard = phase == 0 ? p : c->rank;
uint32_t first = shard_start(c, total, shard);
uint32_t end = shard_start(c, total, shard + 1);
uint64_t lo = (uint64_t)first + (uint64_t)chunk * chunk_packs;
uint64_t hi = lo + chunk_packs;
if (lo > end) {
lo = end;
}
if (hi > end) {
hi = end;
}
uint64_t remote_off = c->pipe_recv_off +
((uint64_t)c->rank * 2 + (uint64_t)phase) * c->shard_cap +
(lo - first) * 16;
uint64_t flag_off =
c->pipe_flag_off +
((((uint64_t)phase * c->world + c->rank) * ROCE_MAX_CHUNKS + chunk) * (uint64_t)c->n_rail) *
ROCE_FLAG_STRIDE;
if (post_striped(c, p, send + lo * 16, remote_off, (uint32_t)(hi - lo), flag_off, seq,
(int)(chunk % (uint32_t)c->n_lane)) != 0) {
return -1;
int passes = c->hairpin_pacing ? 2 : 1;
for (int pass = 0; pass < passes; pass++) {
int want_paced = (c->hairpin_pacing && pass == 1);
for (int step = 1; step < c->world; step++) {
int p = (c->rank + step) % c->world;
if (phase == 1 && c->relay && c->relay_via[p] >= 0) {
continue;
}
if (c->hairpin_pacing && (c->hairpin_peer[p] != 0) != want_paced) {
continue;
}
int shard = phase == 0 ? p : c->rank;
uint32_t first = shard_start(c, total, shard);
uint32_t end = shard_start(c, total, shard + 1);
uint64_t lo = (uint64_t)first + (uint64_t)chunk * chunk_packs;
uint64_t hi = lo + chunk_packs;
if (lo > end) {
lo = end;
}
if (hi > end) {
hi = end;
}
uint64_t remote_off = c->pipe_recv_off +
((uint64_t)c->rank * 2 + (uint64_t)phase) * c->shard_cap +
(lo - first) * 16;
uint64_t flag_off =
c->pipe_flag_off + ((((uint64_t)phase * c->world + c->rank) * ROCE_MAX_CHUNKS + chunk) *
(uint64_t)c->n_rail) *
ROCE_FLAG_STRIDE;
int rc = want_paced
? post_striped_paced(c, p, send + lo * 16, remote_off, (uint32_t)(hi - lo),
flag_off, seq, (int)(chunk % (uint32_t)c->n_lane))
: post_striped(c, p, send + lo * 16, remote_off, (uint32_t)(hi - lo), flag_off,
seq, (int)(chunk % (uint32_t)c->n_lane));
if (rc != 0) {
return -1;
}
}
}
return 0;
Expand Down Expand Up @@ -1552,10 +1726,8 @@ static uint32_t credit_phases(uint32_t prev, uint32_t kind) {
static int post_credits(roce_ctx_t* c, uint32_t seq) {
uint32_t value = seq;
uint64_t credit_off = c->ctrl_off + 4ull * (CTRL_CREDIT + c->rank);
for (int p = 0; p < c->world; p++) {
if (p == c->rank) {
continue;
}
for (int step = 1; step < c->world; step++) {
int p = (c->rank + step) % c->world;
if (ring_relayed(c, p)) {
if (ring_post(c, p, c->region, 0, 0, credit_off, seq) != 0) {
return -1;
Expand Down Expand Up @@ -1937,19 +2109,28 @@ static int run_varlen_op(roce_ctx_t* c, uint32_t seq, uint32_t nbytes, uint32_t
if (hi > total) {
hi = total;
}
for (int p = 0; p < c->world; p++) {
if (p == c->rank) {
continue;
}
uint64_t flag_off =
c->varlen_flag_off +
((((uint64_t)slot * c->world + c->rank) * ROCE_VARLEN_MAX_CHUNKS + posted) *
(uint64_t)c->n_rail) *
ROCE_FLAG_STRIDE;
if (post_striped(c, p, c->region + row_off + lo * 16, row_off + lo * 16,
(uint32_t)(hi - lo), flag_off, seq,
(int)(posted % (uint32_t)c->n_lane)) != 0) {
return -1;
int passes = c->hairpin_pacing ? 2 : 1;
for (int pass = 0; pass < passes; pass++) {
int want_paced = (c->hairpin_pacing && pass == 1);
for (int step = 1; step < c->world; step++) {
int p = (c->rank + step) % c->world;
if (c->hairpin_pacing && (c->hairpin_peer[p] != 0) != want_paced) {
continue;
}
uint64_t flag_off =
c->varlen_flag_off +
((((uint64_t)slot * c->world + c->rank) * ROCE_VARLEN_MAX_CHUNKS + posted) *
(uint64_t)c->n_rail) *
ROCE_FLAG_STRIDE;
int rc = want_paced ? post_striped_paced(c, p, c->region + row_off + lo * 16,
row_off + lo * 16, (uint32_t)(hi - lo), flag_off,
seq, (int)(posted % (uint32_t)c->n_lane))
: post_striped(c, p, c->region + row_off + lo * 16, row_off + lo * 16,
(uint32_t)(hi - lo), flag_off, seq,
(int)(posted % (uint32_t)c->n_lane));
if (rc != 0) {
return -1;
}
}
}
posted++;
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ explicit = true
torch = { index = "pytorch-cu132" }
torchvision = { index = "pytorch-cu132" }
flare-heliopause = { path = "heliopause" }
flashinfer-python = { git = "https://github.com/local-inference-lab/flashinfer.git", rev = "1441ce862ffbc9d8550fdda7ad90e97ca04f4b8e" }
flashinfer-python = { git = "https://github.com/local-inference-lab/flashinfer.git", rev = "3c5c7b14886a7b8904e2963f7d678dbf3a4a755e" }

[tool.setuptools.packages.find]
include = ["ignition*"]
Expand Down
Loading