AMD HIP: the primary ordinal, and the helper caches beside the resident RAM mode
Three changes for two-card AMD PCs whose faster card does not come first in the
runtime's enumeration (Windows HIP orders the cards itself; HIP_VISIBLE_DEVICES
only filters):
- gfx1032 (RX 6600) joins the unvalidated build list - it shares the gfx1030 /
gfx1031 / gfx1034 dp4a path.
- STRATA_PRIMARY_DEVICE=N picks the visible ordinal the engine's own session
runs on; it is set before any context or allocation exists. The helper
caches take the other visible devices, the HIP arch check and the peer tier's
device queries follow it, and the primary's session is created on the chosen
device. A layer split still assumes device 0 and is refused with this set.
Unset or 0 is upstream's behavior unchanged.
- The helper-GPU caches are allowed beside the resident RAM mode: the experts a
helper holds are left out of the RAM copy as a split stage's are, and the two
adaptive tiers already keep each other's experts out. RemoteExperts::finish
spins on its stream like PeerExperts::finish - on HIP the CUDA branch's
cudaInitDevice(ScheduleSpin) does not exist.
Measured on an RX 6800 (16 GB, gfx1030) + RX 6600 (8 GB, gfx1032), Windows 11,
32 GB of RAM, Swift 1.5 IQ3_XXS at 160K context, the resident RAM mode with the
helper's cache left out of the copy: decode 37.5 -> 44.2 tok/s on 8K-token
requests with a 3800-slot helper; the CPU pool's share of the routed experts
falls from ~2.9 to ~0.4 entries per layer-window.
Rebased onto 296b59d2 (conflicts in generate.cpp, peer_experts.cpp, hip_backend.cmake, docs/AMD_HIP.md resolved by keeping main's code and adding the PR's). Changes: remote_caches is kept (still used by the --batch-mtp check); the spin in RemoteExperts::finish is HIP only (CUDA keeps its cudaInitDevice(ScheduleSpin) + blocking sync). NOT COMPILED YET, not run.
Co-authored-by: tuandat3019 <tuandat3019@users.noreply.github.com>
This commit is contained in:
1 parent
eb6e0f1e4d
commit
10552345e9
8 files changed
+164
-15
No files matched your search
@@ -2343,6 +2343,16 @@ int main(int argc, char** argv) {
|
||||
if (o.serve && o.conversation_cache_mib > 0 && (o.prompt_cache == 0 || o.conversation_cache_slots == 0))
|
||||
std::fprintf(stderr, "strata serve: warning: conversation caching is disabled by %s\n",
|
||||
o.prompt_cache == 0 ? "--prompt-cache 0" : "--conversation-cache-slots 0");
|
||||
// STRATA_PRIMARY_DEVICE: which VISIBLE device the engine's own session runs on. The Windows HIP runtime
|
||||
// enumerates the cards in a fixed order no HIP_VISIBLE_DEVICES list can change (it only filters), so with a
|
||||
// slower card first a run could only put the model on it (RX 6600 before RX 6800). 0 (unset) is upstream's
|
||||
// behavior; the helper caches and the peer tier follow it, a layer split still assumes device 0 (see below).
|
||||
const int primary_device = [] {
|
||||
const char* v = std::getenv("STRATA_PRIMARY_DEVICE");
|
||||
const int n = v != nullptr ? std::atoi(v) : 0;
|
||||
return n > 0 ? n : 0;
|
||||
}();
|
||||
strata::core::set_primary_device(primary_device);
|
||||
// parking with --layer-split saves every stage (SavedConversation::stage_images)
|
||||
// Layer split (multi-GPU): the later stages run layers [K_i, K_i+1) on their own GPUs (--split-device, default
|
||||
// the next visible ones); "auto" places the K from each GPU's free VRAM once the weights are in (below). KV
|
||||
@@ -2405,6 +2415,14 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
bool multi_gpu = !split_devs.empty() && !split_same; // cleared by --split-skip-if-fits before any stage loads
|
||||
// STRATA_PRIMARY_DEVICE: a later stage's list (`--split-device` values must be > 0) still assumes the
|
||||
// primary is device 0 - refuse the combination rather than mis-place a stage. The peer tier takes the
|
||||
// primary into account (peer_experts.cpp).
|
||||
if (primary_device != 0 && !o.layer_split.empty()) {
|
||||
std::fprintf(stderr, "strata generate: STRATA_PRIMARY_DEVICE %d is not supported with a layer split yet\n",
|
||||
primary_device);
|
||||
return 2;
|
||||
}
|
||||
bool split_own_auto = false; // #340: the split keeps own prompt buffers by its rule (not --no-prefill-borrow)
|
||||
if (o.mmap_experts && !o.shared_expert_arena.empty()) {
|
||||
std::fprintf(stderr, "strata generate: --shared-expert-arena backs the resident arena and cannot be used with --mmap-experts\n");
|
||||
@@ -2418,13 +2436,23 @@ int main(int argc, char** argv) {
|
||||
o.expert_cache_remote[2] > 0;
|
||||
// A layer split keeps the resident RAM mode: every stage's GPU cache is left out of the RAM copy, and an adaptive
|
||||
// swap copies an evicted expert back from the card that owns its layer (resident_stage_swaps).
|
||||
if (o.resident_cpu_experts && remote_caches) {
|
||||
std::fprintf(stderr, "strata generate: --resident-cpu-experts does not support remote expert caches\n");
|
||||
return 2;
|
||||
}
|
||||
// The helper-GPU caches are allowed beside the resident RAM mode too: their experts are left out of the RAM copy
|
||||
// like a stage's (below), the helpers' refills read through the source (the RAM copy when it holds the bytes),
|
||||
// and the two adaptive tiers already keep each other's experts out (helper_holds in the primary's candidates,
|
||||
// RemoteExpertOpt::adapt's resident table). Measured on RX 6800 + RX 6600, Windows (docs/SECOND_GPU.md).
|
||||
// the helper-GPU expert caches (--expert-cache-remote, docs/SECOND_GPU.md): CUDA1..3 on one GPU; with a layer
|
||||
// split, the visible GPUs no stage runs on, in order
|
||||
int remote_dev[3] = {1, 2, 3};
|
||||
if (primary_device != 0) {
|
||||
// STRATA_PRIMARY_DEVICE: the helpers are the visible devices except the primary, in order
|
||||
int n_vis = 1;
|
||||
if (cudaGetDeviceCount(&n_vis) != cudaSuccess || n_vis < 1) n_vis = 1;
|
||||
cudaGetLastError();
|
||||
int k = 0;
|
||||
for (int d = 0; d < n_vis && k < 3; ++d)
|
||||
if (d != primary_device) remote_dev[k++] = d;
|
||||
for (; k < 3; ++k) remote_dev[k] = -1;
|
||||
}
|
||||
if (multi_gpu) {
|
||||
if (o.expert_profile.empty()) {
|
||||
std::fprintf(stderr, "strata generate: a layer split across GPUs needs --expert-profile\n");
|
||||
@@ -2508,7 +2536,7 @@ int main(int argc, char** argv) {
|
||||
{
|
||||
// every GPU this run uses must be an architecture the binary has code for (a gfx1100 build on a gfx1201
|
||||
// card would otherwise fail later with "invalid device function")
|
||||
std::vector<int> used{0};
|
||||
std::vector<int> used{primary_device}; // STRATA_PRIMARY_DEVICE: the primary is not always 0
|
||||
if (multi_gpu) used.insert(used.end(), split_devs.begin(), split_devs.end());
|
||||
for (int r = 0; r < 3; ++r)
|
||||
if (o.expert_cache_remote[(size_t) r] > 0) used.push_back(remote_dev[r]);
|
||||
@@ -2831,6 +2859,25 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
const bool native_pack = strata::kernels::cpu::expert_layout().native;
|
||||
// STRATA_PRIMARY_DEVICE: switch the engine's own thread to the chosen card BEFORE any GPU
|
||||
// context or allocation exists below (the helper preflights, the PCIe probe, the weights and the
|
||||
// arena all follow it). Every other device switch in the engine saves and restores the current
|
||||
// device, so this single call is where the primary is picked.
|
||||
if (primary_device != 0) {
|
||||
int n_vis = 0;
|
||||
if (cudaGetDeviceCount(&n_vis) != cudaSuccess || primary_device >= n_vis) {
|
||||
cudaGetLastError();
|
||||
std::fprintf(stderr, "strata generate: STRATA_PRIMARY_DEVICE=%d: no such visible device (%d visible)\n",
|
||||
primary_device, n_vis);
|
||||
return 2;
|
||||
}
|
||||
if (cudaSetDevice(primary_device) != cudaSuccess) {
|
||||
std::fprintf(stderr, "strata generate: STRATA_PRIMARY_DEVICE=%d: cudaSetDevice failed: %s\n",
|
||||
primary_device, cudaGetErrorString(cudaGetLastError()));
|
||||
return 1;
|
||||
}
|
||||
std::fprintf(stderr, "strata generate: primary device from STRATA_PRIMARY_DEVICE: CUDA%d\n", primary_device);
|
||||
}
|
||||
// STRATA_EARLY_REMOTE_CONTEXTS=1: create EVERY secondary context here, like CUDA1's. Under WSL2 the driver's
|
||||
// pinned/mapped host budget (dxg gpadl, ~1 GiB) is spent by CUDA0's weights and MTP before the later loop runs,
|
||||
// and a new context then fails with cudaErrorMemoryAllocation (CUDA2: "cudaSetDevice(2) failed: out of memory").
|
||||
@@ -4386,7 +4433,7 @@ int main(int argc, char** argv) {
|
||||
// held all 48 layers' state whatever layers it ran, which is the same disease the chunked-QSA-prefill PR
|
||||
// fixed in llama.cpp: allocation sized by the whole model instead of the device's own work.
|
||||
{
|
||||
const strata::core::OnDevice on0(0);
|
||||
const strata::core::OnDevice on0(primary_device); // STRATA_PRIMARY_DEVICE (upstream: a literal 0)
|
||||
const int64_t hi0 = multi_gpu ? split_at[0] : -1;
|
||||
// the elastic K/V (--kv-grow, see kvg_ensure): one GPU, the whole K/V in VRAM (no streaming), a profiled cache
|
||||
// that can give slots up, and every expert in RAM for the CPU to compute the ones it gives up
|
||||
@@ -7026,6 +7073,14 @@ int main(int argc, char** argv) {
|
||||
for (int64_t l = st->lb; l < st->le; ++l)
|
||||
for (int64_t e = 0; e < g.n_expert; ++e)
|
||||
if (st->cache.slot_of(l, e) >= 0) stage_pairs.emplace_back((int32_t) l, (int32_t) e);
|
||||
// A helper GPU's experts are left out of the RAM copy as a stage's are - the RAM
|
||||
// that duplication would take goes to colder experts instead.
|
||||
for (int r = 0; r < 3; ++r) {
|
||||
if (o.expert_cache_remote[(size_t) r] <= 0) continue;
|
||||
for (int64_t l = 0; l < g.n_layers; ++l)
|
||||
for (int64_t e = 0; e < g.n_expert; ++e)
|
||||
if (remote_experts[(size_t) r].holds(l, (int32_t) e)) stage_pairs.emplace_back((int32_t) l, (int32_t) e);
|
||||
}
|
||||
// The split's lend regions. The serve scan lends the tail slots of EVERY part's cache - CUDA0's own
|
||||
// part first, then every stage (the `pf_parts` loop below) - and `lend_from` above is switched off in
|
||||
// pin_cache_complement the moment `additional_gpu_pairs` is non-empty, so until now none of those experts
|
||||
|
||||
Reference in new issue
Block a user