Merge upstream main (v1.15.1 -> v1.17.0, 152 commits) into the SYCL port

Conflicts, both sides kept:
- abi: RAD_ABI_VERSION 37 (upstream); the fork's RadBuildCtx.plain_norm after upstream's
  expert_file_tier, widened to int64_t so it starts past ABI 37's 208-byte struct instead of in
  its tail padding (abi_layout_test); RadSyclStream rows after RadShapeGen*.
- arch/common/rad_block_gdn_fp8.h: upstream's q8_rot guard and the fork's o_plain guard
  combined; the decode-row q_o.step only where q_o is declared (!o_plain).
- docs/OPS.md: upstream's optional params on sample_logprobs, the fork's q_temp on
  mtp_couple_propose.

Also ignores /baseline-models/ (local model copies).

Host tests: arch_test's MTP lint/graph-pin failures are unchanged from before the merge (the
fork's mtp.fc LinearFP8, 10a99a5); avx_check's hand-built moe_gemm_q w4a8 case also fails on
upstream's own files; images/videos need FFmpeg; three binaries over their CPU budget on this VM.
SYCL card tests 3/3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-11-07 22:13:08 +01:00
commit 45f3b510c7
365 files changed
+56942 -6331

No files matched your search

+61 -16
View File
@@ -589,7 +589,7 @@ KernelRec* kernel_for(const void* stub, int ord, std::string* why) {
struct Stream {
uint64_t magic = kStreamMagic;
int ord = 0;
uint32_t id = 0; /* index into g_queue_by_id, never reused */
uint32_t id = 0; /* queue_of's key, never reused (see there) */
hsa_agent_t agent{};
hsa_queue_t* q = nullptr;
uint64_t mask = 0;
@@ -623,12 +623,27 @@ struct Stream {
void unlock() { busy.store(false, std::memory_order_release); }
};
/* Every queue this process has made, by an id that is never reused, so "packet i of queue n"
* stays meaningful after the queue is destroyed. A destroyed queue's entry is null; everything
* it held has been consumed. */
/* Every live queue, by an id that is never reused, so "packet i of queue n" stays meaningful after
* the queue is destroyed: queue_of answers null for a destroyed queue's id, and everything it held
* has been consumed. At most kMaxQueues are live at once; a destroyed queue's slot is taken again
* under a new generation, id = generation * kMaxQueues + slot, so a stale id never names the
* slot's next queue. */
constexpr uint32_t kMaxQueues = 1024;
std::atomic<hsa_queue_t*> g_queue_by_id[kMaxQueues];
std::atomic<uint32_t> g_next_queue_id{0};
std::atomic<uint32_t> g_queue_id_at[kMaxQueues]; /* the id holding each slot now */
std::mutex g_queue_slot_mu;
std::vector<uint32_t> g_queue_free; /* destroyed queues' slots */
uint32_t g_queue_next_slot = 0;
uint32_t g_queue_gen[kMaxQueues];
/* The queue an id names, or null once it is destroyed. The slot's id is stored before its queue
* (release), so a queue read here that belongs to a later generation is seen with that
* generation's id and refused. */
inline hsa_queue_t* queue_of(uint32_t id) {
const uint32_t slot = id % kMaxQueues;
hsa_queue_t* q = g_queue_by_id[slot].load(std::memory_order_acquire);
return g_queue_id_at[slot].load(std::memory_order_acquire) == id ? q : nullptr;
}
inline Stream* ours(const void* s) {
if ((uintptr_t)s < 4096) return nullptr;
@@ -1127,6 +1142,9 @@ int dispatch(Stream* s, const KernelRec* k, dim3 g, dim3 b, void** args, size_t
if (!gx || !gy || !gz) return RAD_OK;
if (gx > UINT32_MAX || gy > UINT32_MAX || gz > UINT32_MAX) return RAD_E_INVAL;
if ((uint64_t)k->group_size + shm > 65536) return RAD_E_INVAL;
/* The packet's fields are 16 bits wide and the card runs at most 1024 work-items a group;
* HIP refuses a larger block, and so does this. */
if ((uint64_t)b.x * b.y * b.z > 1024) return RAD_E_INVAL;
alignas(64) unsigned char ka[kKargMax];
const uint32_t n = (k->kernarg_size + 15u) & ~15u;
@@ -1562,6 +1580,7 @@ int a_stream_create(RadStream* out, int high_priority) {
if (hsa_amd_memory_pool_allocate(kpool, (size_t)(s->mask + 2) * kKargSlot, 0, &ka) !=
HSA_STATUS_SUCCESS ||
hsa_amd_agents_allow_access(1, &kpeer, nullptr, ka) != HSA_STATUS_SUCCESS) {
if (ka) hsa_amd_memory_pool_free(ka); /* allocated, then refused access */
hsa_queue_destroy(s->q);
device_set_error("rad_stream_create: the kernel argument ring could not be placed in %s",
d.host_kargs ? "pinned host memory" : "VRAM the host can write");
@@ -1582,17 +1601,28 @@ int a_stream_create(RadStream* out, int high_priority) {
s->bounce = static_cast<char*>(bb);
note_alloc(ka, (int64_t)(s->mask + 2) * kKargSlot, -1);
note_alloc(bb, kBounceBytes, -1);
s->id = g_next_queue_id.fetch_add(1, std::memory_order_relaxed);
if (s->id >= kMaxQueues) {
uint32_t slot = kMaxQueues;
{
std::lock_guard<std::mutex> lk(g_queue_slot_mu);
if (!g_queue_free.empty()) {
slot = g_queue_free.back();
g_queue_free.pop_back();
} else if (g_queue_next_slot < kMaxQueues) {
slot = g_queue_next_slot++;
}
if (slot < kMaxQueues) s->id = g_queue_gen[slot]++ * kMaxQueues + slot;
}
if (slot >= kMaxQueues) {
note_free(bb);
note_free(ka);
hsa_amd_memory_pool_free(bb);
hsa_amd_memory_pool_free(ka);
hsa_queue_destroy(s->q);
device_set_error("rad_stream_create: more than %u streams in one process", kMaxQueues);
device_set_error("rad_stream_create: more than %u streams live at once", kMaxQueues);
return RAD_E_NOMEM;
}
g_queue_by_id[s->id].store(s->q, std::memory_order_release);
g_queue_id_at[slot].store(s->id, std::memory_order_release);
g_queue_by_id[slot].store(s->q, std::memory_order_release);
{
std::lock_guard<std::mutex> lk(G().streams_mu);
G().streams.push_back(s.get());
@@ -1604,14 +1634,24 @@ int a_stream_create(RadStream* out, int high_priority) {
void a_stream_destroy(RadStream rs) {
Stream* s = ours(rs);
if (!s) return;
(void)stream_drain(s);
/* Undrained (a closed gate, a fault), the queue may still be read by the card, and so may its
* kernel argument ring: both are left allocated rather than freed under it. */
if (const int rc = stream_drain(s); rc < 0) {
RAD_WARN("device: a stream was destroyed with work it could not drain (%s); its queue and "
"argument ring stay allocated", rad_strerror(rc));
return;
}
{
std::lock_guard<std::mutex> lk(G().streams_mu);
auto& v = G().streams;
for (size_t i = 0; i < v.size(); ++i) if (v[i] == s) { v.erase(v.begin() + (long)i); break; }
}
g_queue_by_id[s->id].store(nullptr, std::memory_order_release);
g_queue_by_id[s->id % kMaxQueues].store(nullptr, std::memory_order_release);
hsa_queue_destroy(s->q);
{
std::lock_guard<std::mutex> lk(g_queue_slot_mu);
g_queue_free.push_back(s->id % kMaxQueues);
}
note_free(s->karg);
note_free(s->bounce);
hsa_amd_memory_pool_free(s->karg);
@@ -1676,7 +1716,7 @@ bool xp_list_finished(int ord, uint32_t k);
bool reusable(const Sig* g) {
if (g->stamped || hsa_signal_load_relaxed(g->h) != 0) return false;
for (const auto& w : g->waiters) {
hsa_queue_t* q = g_queue_by_id[w.first].load(std::memory_order_acquire);
hsa_queue_t* q = queue_of(w.first);
if (q && hsa_queue_load_read_index_relaxed(q) <= w.second) return false;
}
const int32_t xd = g->xp_dev.load(std::memory_order_acquire);
@@ -1902,7 +1942,7 @@ std::vector<Tape*> g_tape_retired;
* kReserveMargin past it has launched, which proves it as in reserve(). */
bool tape_idle(const Tape* t) {
for (const auto& l : t->last) {
hsa_queue_t* q = g_queue_by_id[l.first].load(std::memory_order_acquire);
hsa_queue_t* q = queue_of(l.first);
if (q && hsa_queue_load_read_index_scacquire(q) < l.second + kReserveMargin) return false;
}
return true;
@@ -2574,6 +2614,7 @@ struct DmaQueue {
* a queue while the engine runs on, and a ring the host thinks full would wait forever. */
volatile uint32_t* fin = nullptr;
uint32_t lists = 0; /* lists handed over; list k ends at ends[k % kDmaEnds] */
uint64_t seen = 0; /* the most the room check has known read, under the lock */
std::unique_ptr<uint64_t[]> ends;
};
constexpr uint32_t kDmaEnds = 4096;
@@ -2818,22 +2859,26 @@ inline uint64_t dma_read(const DmaQueue* q) {
uint64_t r = *q->rptr;
if (q->fin) {
const uint32_t k = *q->fin;
if (k) r = std::max(r, q->ends[k % kDmaEnds]);
/* ends[k % kDmaEnds] is list k's only while fewer than kDmaEnds lists are behind it. */
if (k && q->lists - k < kDmaEnds) r = std::max(r, q->ends[k % kDmaEnds]);
}
return r;
return std::max(r, q->seen);
}
/* Writes `n` dwords to the ring; queue lock held. A packet never straddles the ring end: the tail
* is padded with single-dword NOPs. The ring holds a few hundred layers' lists, so waiting for
* room means the host is that far ahead of a stalled engine. */
int dma_put(DmaQueue* q, const uint32_t* dw, uint64_t n) {
/* What was once known read stays read: a list number whose ends slot a later list has taken
* cannot be used any more, and the read pointer alone may be behind it. */
q->seen = dma_read(q);
const uint64_t cap = kDmaRingBytes / 4;
uint64_t at = (q->w / 4) & (cap - 1);
const uint64_t pad = at + n > cap ? cap - at : 0;
const uint64_t need = (pad + n) * 4;
if (q->w + need - dma_read(q) > kDmaRingBytes) {
Stall st("a full DMA ring");
while (q->w + need - dma_read(q) > kDmaRingBytes) {
while (q->w + need - (q->seen = dma_read(q)) > kDmaRingBytes) {
if (G().faulted.load(std::memory_order_relaxed)) {
device_set_error("rad_memcpy_list_async: a queue faulted while the DMA ring was full");
return RAD_E_DEVICE;
+2 -1
View File
@@ -406,7 +406,8 @@ int64_t dev_stuck_ms();
constexpr int kTapeOverlapRun = 6;
/* ------------------------------------------------------------------ the allocation ledger
* What rad_dev_alloc has handed out since process start, by RAD_MEM_* kind. It does not see a
* By RAD_MEM_* kind: the bytes rad_dev_alloc has handed out and rad_dev_free has not taken back,
* and the number of allocations since process start. It does not see a
* kernel plugin that calls hipMalloc directly; see the ledger in dispatch.cpp. */
void rad_dev_alloc_totals(int kind, int64_t* bytes, int64_t* count);
+24 -4
View File
@@ -21,6 +21,7 @@
#include <iterator>
#include <map>
#include <mutex>
#include <unordered_map>
#include <string>
#include <utility>
#include <cstdint>
@@ -172,6 +173,26 @@ struct Ledger {
std::atomic<int64_t> count{0};
};
Ledger g_ledger[4]; /* indexed by RAD_MEM_*; rad_dev_alloc bounds the kind */
/* Each live allocation's size, so rad_dev_free (which has none) takes it back off the ledger's
* bytes: pinned ranges are freed and taken again during bring-up, and the pin-cap check reads the
* ledger as what the process holds. Allocations are rare, never per step. */
struct LiveAlloc { int64_t bytes; int kind; };
std::mutex g_ledger_mu;
std::unordered_map<void*, LiveAlloc> g_ledger_live;
void ledger_add(void* p, int64_t bytes, int kind) {
g_ledger[kind].bytes.fetch_add(bytes, std::memory_order_relaxed);
std::lock_guard<std::mutex> lk(g_ledger_mu);
g_ledger_live[p] = LiveAlloc{ bytes, kind };
}
void ledger_drop(void* p) {
if (!p) return;
std::lock_guard<std::mutex> lk(g_ledger_mu);
const auto it = g_ledger_live.find(p);
if (it == g_ledger_live.end()) return;
g_ledger[it->second.kind].bytes.fetch_sub(it->second.bytes, std::memory_order_relaxed);
g_ledger_live.erase(it);
}
/* Live event handles, counted here so the number is backend-independent; the only place an
* event leaked at teardown shows. */
@@ -450,7 +471,7 @@ void* rad_dev_alloc(int64_t bytes, int kind) {
thread_local int64_t t_device_ordinal = 0;
void* p = dev()->alloc(bytes, kind);
if (p && kind >= 0 && kind < (int)std::size(rad::g_ledger)) {
rad::g_ledger[kind].bytes.fetch_add(bytes, std::memory_order_relaxed);
rad::ledger_add(p, bytes, kind);
const int64_t total = rad::g_ledger[kind].count.fetch_add(1, std::memory_order_relaxed) + 1;
const int64_t ordinal = kind == RAD_MEM_DEVICE ? ++t_device_ordinal : total;
const int64_t floor_ = rad::trace_floor();
@@ -476,8 +497,7 @@ void* rad_dev_alloc(int64_t bytes, int kind) {
return p;
}
void rad_dev_free(void* p, int kind) {
/* The ledger counts allocations, not live bytes: rad_dev_free has no size. The engine frees
* only at teardown, so the two agree while running. */
rad::ledger_drop(p);
dev()->free(p, kind);
}
void* rad_dev_host_ptr(void* p) { return dev()->host_ptr(p); }
@@ -694,7 +714,7 @@ void* rad_dev_map_file(int fd, int64_t foff, int64_t bytes) {
}
void* p = f(fd, foff, bytes);
if (p) {
rad::g_ledger[RAD_MEM_HOST_MAPPED].bytes.fetch_add(bytes, std::memory_order_relaxed);
rad::ledger_add(p, bytes, RAD_MEM_HOST_MAPPED);
rad::g_ledger[RAD_MEM_HOST_MAPPED].count.fetch_add(1, std::memory_order_relaxed);
}
return p;
+28 -3
View File
@@ -20,6 +20,7 @@
#include <condition_variable>
#include <cstdlib>
#include <cstring>
#include <map>
#include <mutex>
#include <thread>
#include <unordered_map>
@@ -723,6 +724,21 @@ constexpr int64_t kVmemGranule = RAD_ALIGN_POOL;
int64_t h_vmem_granularity(void) { return kVmemGranule; }
/* Each reservation's card, recorded at reserve: the thread that later commits into a pool, or
* releases it at teardown, may have selected another card, and the pages count against the card
* the range was reserved for (as the AQL backend's VRes does). Keyed by base. */
struct VRange { char* end; int device; };
std::mutex g_vres_mu;
std::map<const char*, VRange> g_vres;
int vmem_device_of(const void* addr) {
std::lock_guard<std::mutex> lk(g_vres_mu);
auto it = g_vres.upper_bound((const char*)addr);
if (it == g_vres.begin()) return t_device;
--it;
return (const char*)addr < it->second.end ? it->second.device : t_device;
}
void* h_vmem_reserve(int64_t bytes, int64_t align) {
if (bytes <= 0) return nullptr;
if (align < kVmemGranule) align = kVmemGranule;
@@ -740,11 +756,20 @@ void* h_vmem_reserve(int64_t bytes, int64_t align) {
if (head) ::munmap(raw, head);
const size_t tail = raw_len - head - (size_t)bytes;
if (tail) ::munmap(base + bytes, tail);
{
std::lock_guard<std::mutex> lk(g_vres_mu);
g_vres[base] = VRange{ base + bytes, t_device };
}
return base;
}
void h_vmem_release(void* base, int64_t bytes) {
if (base && bytes > 0) ::munmap(base, (size_t)bytes);
if (!base || bytes <= 0) return;
{
std::lock_guard<std::mutex> lk(g_vres_mu);
g_vres.erase((const char*)base);
}
::munmap(base, (size_t)bytes);
}
int h_vmem_commit(void* addr, int64_t bytes) {
@@ -755,7 +780,7 @@ int h_vmem_commit(void* addr, int64_t bytes) {
device_set_error("vmem_commit(%p, %lld): mmap failed", addr, (long long)bytes);
return RAD_E_NOMEM;
}
g_used[t_device].fetch_add(bytes, std::memory_order_relaxed);
g_used[vmem_device_of(addr)].fetch_add(bytes, std::memory_order_relaxed);
return RAD_OK;
}
@@ -769,7 +794,7 @@ int h_vmem_decommit(void* addr, int64_t bytes) {
device_set_error("vmem_decommit(%p, %lld): mmap failed", addr, (long long)bytes);
return RAD_E_INVAL;
}
g_used[t_device].fetch_sub(bytes, std::memory_order_relaxed);
g_used[vmem_device_of(addr)].fetch_sub(bytes, std::memory_order_relaxed);
return RAD_OK;
}