Files
Strata/include/strata/core/device.hpp
T
Niko1221andtuandat3019 10552345e9 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>
2026-10-14 07:14:17 +02:00

104 lines
5.1 KiB
C++

// include/strata/core/device.hpp - P2.S1: the device arena and the runtime's device facts.
//
// `DeviceArena` is ONE cudaMalloc per planner region with bump sub-allocation below it and no frees. That is
// not a simplification for the first version: the memory plan from P1.S9 is fixed at startup, so the set of
// regions and their sizes is known before anything is allocated, and an allocator that can free would be
// solving a problem the engine does not have while adding fragmentation and failure modes it does.
//
// The reason to get this in early is that the VRAM budget is the binding constraint of the whole design
// (5.95 GB pooled between KV and the expert cache, 33.97 GB of experts in DRAM). A runtime that discovers at
// token 4000 that it has overcommitted has already lost; the plan is printed against `cudaMemGetInfo` at
// startup so the discrepancy is visible immediately.
#pragma once
#include <cstdint>
#include <stdexcept>
#include <string>
#include <vector>
namespace strata::core {
struct DeviceInfo {
int ordinal = -1;
std::string name;
int cc_major = 0, cc_minor = 0;
uint64_t total_bytes = 0; // as reported by cudaMemGetInfo at query time
uint64_t free_bytes = 0;
int driver_version = 0, runtime_version = 0;
int multi_processor_count = 0;
std::string arch; // HIP: gcnArchName without its feature suffix ("gfx1201"); empty on CUDA
};
// HIP builds: whether GPU `ordinal` can run this binary - its architecture must be one the binary was COMPILED
// for (STRATA_HIP_ARCHS, set by cmake/hip_backend.cmake) and it must run wave32. "" when it can (or when there
// is no such device: the caller's own device errors apply), else the reason in a sentence. A binary carried to
// another card would otherwise fail later with "invalid device function". CUDA builds: always "".
std::string gpu_arch_problem(int ordinal);
// The GPU architectures this binary was compiled for ("gfx1100,gfx1201"); "" on CUDA builds.
const char* compiled_gpu_archs();
// How many devices the runtime enumerates, numbered as HIP_VISIBLE_DEVICES / CUDA_VISIBLE_DEVICES number them; 0 when
// there is none (or no usable runtime). `device_info` throws on a card this binary cannot run, so a caller that LISTS
// every card - the ones it has no code for included (`strata-device --list-devices`) - counts them with this first.
int device_count();
// One line per device for `strata-device --list-devices`, without the arch check that `device_info` applies:
// "arch gfx1201, 15.9 GiB" (HIP) or "compute capability 12.0, 11.9 GiB" (CUDA), plus the device's name. false when
// the runtime cannot describe it.
bool device_summary(int ordinal, std::string& name, std::string& detail);
// STRATA_PRIMARY_DEVICE (generate.cpp): the ordinal the engine's own session runs on; 0 = the runtime's first
// visible device (upstream's behavior). The Windows HIP runtime enumerates the cards in a fixed order no
// HIP_VISIBLE_DEVICES list can change (it only filters), so a PC whose faster card comes second could not run
// the model on it while the first card served as a helper. The helper expert caches and the peer tier must
// live on another device. Set once, before any context exists.
void set_primary_device(int ordinal);
int primary_device();
// Throws when there is no CUDA device. The engine targets sm_120 specifically and must say so rather than
// run slowly on something else: `CMakeLists.txt` already refuses to COMPILE for another architecture, and
// this is the matching check at run time (a binary can be carried to a different machine).
DeviceInfo device_info(int ordinal = 0);
/// "" when this build has device code for the current device, else CUDA's error: a build for other GPUs would
/// otherwise fail at its first kernel launch, with nothing that names the cause.
std::string device_code_error();
class CudaError : public std::runtime_error {
public:
CudaError(const std::string& what, int code) : std::runtime_error(what), code_(code) {}
int code() const { return code_; }
private:
int code_;
};
// One cudaMalloc, bump-allocated below. `poison` fills new allocations with a NaN-ish pattern in a debug
// build so that reading uninitialised VRAM gives a NaN rather than a plausible number - the same reasoning as
// the harness work in Phase 1: a wrong value that looks right is the expensive kind.
class DeviceArena {
public:
explicit DeviceArena(uint64_t bytes, int ordinal = 0, bool poison = false);
~DeviceArena();
DeviceArena(const DeviceArena&) = delete;
DeviceArena& operator=(const DeviceArena&) = delete;
// `align` must be a power of two; 256 keeps every sub-allocation at a sector boundary.
void* alloc(uint64_t bytes, uint64_t align = 256);
uint64_t capacity() const { return capacity_; }
uint64_t used() const { return used_; }
uint64_t peak() const { return used_; } // no frees, so used IS the peak
int ordinal() const { return ordinal_; }
void* base() const { return base_; }
private:
void* base_ = nullptr;
uint64_t capacity_ = 0, used_ = 0;
int ordinal_ = 0;
bool poison_ = false;
};
} // namespace strata::core