Archived
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
389 lines
14 KiB
C++
389 lines
14 KiB
C++
/* hip.cpp -- the HIP half of the device backend: devices, memory and elastic memory.
|
|
*
|
|
* Streams, events, copies and fills are AQL packets on engine-owned queues (aql.cpp), which
|
|
* starts from this table and fills in the rest, pinned host memory included because the copies
|
|
* must recognise it.
|
|
*
|
|
* Thin by design: each entry point is one hip* call plus error capture, so it cannot diverge from
|
|
* the host backend, where the logic is tested (docs/IMPLEMENTATION.md). Policy belongs in the
|
|
* component that wants it.
|
|
*
|
|
* Compiles to nothing without RAD_HAVE_HIP, so the source list needs no backend split.
|
|
*/
|
|
#include "device.h"
|
|
|
|
#if defined(RAD_HAVE_HIP) && RAD_HAVE_HIP
|
|
|
|
#include "../rad_internal.h"
|
|
|
|
#include <hip/hip_runtime.h>
|
|
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <mutex>
|
|
#include <new>
|
|
#include <vector>
|
|
#include <cstring>
|
|
#include <cstdio>
|
|
#include <strings.h>
|
|
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
namespace rad {
|
|
namespace {
|
|
|
|
/* Maps a hipError_t to a coarse RAD_E_* class; the detail goes to rad_dev_last_error(). */
|
|
int hip_fail(hipError_t e, const char* who) {
|
|
device_set_error("%s: %s (%s)", who, hipGetErrorString(e), hipGetErrorName(e));
|
|
switch (e) {
|
|
case hipErrorOutOfMemory: return RAD_E_NOMEM;
|
|
case hipErrorInvalidValue:
|
|
case hipErrorInvalidDevice:
|
|
case hipErrorInvalidHandle: return RAD_E_INVAL;
|
|
case hipErrorNotSupported: return RAD_E_UNSUPPORTED;
|
|
default: return RAD_E_DEVICE;
|
|
}
|
|
}
|
|
|
|
#define HIP_TRY(call) \
|
|
do { hipError_t _e = (call); \
|
|
if (_e != hipSuccess) return hip_fail(_e, #call); } while (0)
|
|
|
|
/* For void callers (frees, destroys): a failing teardown is still logged, since it means the
|
|
* pointer was not what the caller thought. */
|
|
void hip_note(hipError_t e, const char* who) {
|
|
if (e != hipSuccess) RAD_WARN("device: %s: %s", who, hipGetErrorString(e));
|
|
}
|
|
#define HIP_NOTE(call) hip_note((call), #call)
|
|
|
|
/* ------------------------------------------------------------------ devices */
|
|
|
|
int d_count(void) {
|
|
int n = 0;
|
|
if (hipGetDeviceCount(&n) != hipSuccess) return 0;
|
|
return n;
|
|
}
|
|
|
|
int d_set(int device) {
|
|
HIP_TRY(hipSetDevice(device));
|
|
return RAD_OK;
|
|
}
|
|
|
|
|
|
/* ------------------------------------------------------------------ how much VRAM is really free
|
|
*
|
|
* On a card that also drives a display, hipMemGetInfo's "free" sits up to 0.7 GiB below the
|
|
* amdgpu driver's own per-card accounting, and that gap is allocatable. So read the driver's
|
|
* counters (bytes, per card, all processes) from the drm node matched by PCI slot. The node's
|
|
* total must equal HIP's total, so both describe the same memory; any failure returns -1 and the
|
|
* caller keeps hipMemGetInfo. -1 on every non-amdgpu platform.
|
|
*
|
|
* The node's `mem_info_vram_used` is also kept open per device for d_vram_used, so an observer
|
|
* reads the card through that descriptor and never through the runtime. */
|
|
constexpr int kMaxVramNodes = 64;
|
|
std::atomic<int> g_vram_used_fd[kMaxVramNodes] = {}; /* 0: not opened; else the fd plus one */
|
|
|
|
static int64_t sysfs_vram_free(int device, int64_t total) {
|
|
char bus[64] = {0};
|
|
if (hipDeviceGetPCIBusId(bus, (int)sizeof bus, device) != hipSuccess || !bus[0]) return -1;
|
|
const size_t buslen = std::strlen(bus);
|
|
|
|
for (int c = 0; c < 64; ++c) {
|
|
char path[160];
|
|
std::snprintf(path, sizeof path, "/sys/class/drm/card%d/device/uevent", c);
|
|
FILE* f = std::fopen(path, "r");
|
|
if (!f) continue;
|
|
bool match = false;
|
|
char line[256];
|
|
while (std::fgets(line, (int)sizeof line, f))
|
|
if (!std::strncmp(line, "PCI_SLOT_NAME=", 14) &&
|
|
!strncasecmp(line + 14, bus, buslen)) { match = true; break; }
|
|
std::fclose(f);
|
|
if (!match) continue;
|
|
|
|
auto read_u64 = [&](const char* leaf, long long* out_v) {
|
|
char p[160];
|
|
std::snprintf(p, sizeof p, "/sys/class/drm/card%d/device/%s", c, leaf);
|
|
FILE* g = std::fopen(p, "r");
|
|
if (!g) return false;
|
|
const bool ok = std::fscanf(g, "%lld", out_v) == 1 && *out_v >= 0;
|
|
std::fclose(g);
|
|
return ok;
|
|
};
|
|
long long tot = 0, used = 0;
|
|
if (!read_u64("mem_info_vram_total", &tot) || !read_u64("mem_info_vram_used", &used))
|
|
return -1;
|
|
if (tot != (long long)total) return -1; /* different accountings; do not mix them */
|
|
if (device >= 0 && device < kMaxVramNodes &&
|
|
g_vram_used_fd[device].load(std::memory_order_acquire) == 0) {
|
|
char p[160];
|
|
std::snprintf(p, sizeof p, "/sys/class/drm/card%d/device/mem_info_vram_used", c);
|
|
const int fd = ::open(p, O_RDONLY | O_CLOEXEC);
|
|
int none = 0;
|
|
if (fd >= 0 && !g_vram_used_fd[device].compare_exchange_strong(none, fd + 1))
|
|
::close(fd);
|
|
}
|
|
return total - (int64_t)used;
|
|
}
|
|
return -1;
|
|
}
|
|
/* A sysfs attribute is regenerated by a read at offset 0, so read with pread at 0. */
|
|
int d_vram_used(int device, int64_t* out) {
|
|
if (!out || device < 0 || device >= kMaxVramNodes) return RAD_E_INVAL;
|
|
const int fd = g_vram_used_fd[device].load(std::memory_order_acquire) - 1;
|
|
if (fd < 0) return RAD_E_UNSUPPORTED;
|
|
char buf[32];
|
|
const ssize_t n = ::pread(fd, buf, sizeof buf - 1, 0);
|
|
if (n <= 0) return RAD_E_IO;
|
|
buf[n] = '\0';
|
|
char* end = nullptr;
|
|
const long long v = std::strtoll(buf, &end, 10);
|
|
if (end == buf || v < 0) return RAD_E_IO;
|
|
*out = (int64_t)v;
|
|
return RAD_OK;
|
|
}
|
|
|
|
int d_integrated(int device) {
|
|
int v = 0;
|
|
return hipDeviceGetAttribute(&v, hipDeviceAttributeIntegrated, device) == hipSuccess ? v : 0;
|
|
}
|
|
|
|
/* /proc/meminfo's MemAvailable in bytes, or -1. */
|
|
static int64_t host_mem_available() {
|
|
FILE* f = std::fopen("/proc/meminfo", "r");
|
|
if (!f) return -1;
|
|
char line[256];
|
|
long long kb = -1;
|
|
while (std::fgets(line, (int)sizeof line, f))
|
|
if (std::sscanf(line, "MemAvailable: %lld kB", &kb) == 1) break;
|
|
std::fclose(f);
|
|
return kb >= 0 ? (int64_t)kb * 1024 : -1;
|
|
}
|
|
|
|
int d_props(int device, RadDeviceProps* out) {
|
|
if (!out) return RAD_E_INVAL;
|
|
hipDeviceProp_t p{};
|
|
HIP_TRY(hipGetDeviceProperties(&p, device));
|
|
std::memset(out, 0, sizeof *out);
|
|
|
|
/* Intended truncation: HIP's fields are 256 bytes, ours 128 and 32. The explicit precision
|
|
* silences gcc's truncation warning; real values are a few dozen characters. */
|
|
snprintf(out->name, sizeof out->name, "%.*s", (int)(sizeof out->name) - 1, p.name);
|
|
snprintf(out->arch, sizeof out->arch, "%.*s", (int)(sizeof out->arch) - 1, p.gcnArchName);
|
|
/* Strip feature suffixes ("gfx1201:sramecc-:xnack-"): plugin constraints match the bare
|
|
* target. */
|
|
if (char* c = std::strchr(out->arch, ':')) *c = '\0';
|
|
|
|
out->device_id = device;
|
|
out->n_cu = p.multiProcessorCount;
|
|
out->warp_size = p.warpSize;
|
|
out->vram_bytes = (int64_t)p.totalGlobalMem;
|
|
out->lds_bytes = (int64_t)p.sharedMemPerBlock;
|
|
out->is_host_backend = 0;
|
|
|
|
/* hipMemGetInfo reports the current device, so switch to `device` and back. props is not
|
|
* called per step. */
|
|
int prev = 0;
|
|
bool moved = false;
|
|
if (hipGetDevice(&prev) == hipSuccess && prev != device &&
|
|
hipSetDevice(device) == hipSuccess)
|
|
moved = true;
|
|
size_t freeb = 0, totalb = 0;
|
|
if (hipMemGetInfo(&freeb, &totalb) == hipSuccess) {
|
|
out->vram_free = (int64_t)freeb;
|
|
if (totalb) out->vram_bytes = (int64_t)totalb;
|
|
}
|
|
/* Prefer the driver's counter where readable (see sysfs_vram_free). */
|
|
{
|
|
const int64_t sf = sysfs_vram_free(device, out->vram_bytes);
|
|
if (sf >= 0) {
|
|
out->vram_free = sf;
|
|
} else if (d_integrated(device)) {
|
|
/* Integrated GPU whose reported memory exceeds the driver's carve-out: the rest is
|
|
* system RAM, and the runtime's free figure ignores other processes using it. Bound
|
|
* it by MemAvailable. (A carve-out reported as-is is answered by sysfs above.) */
|
|
const int64_t avail = host_mem_available();
|
|
if (avail >= 0 && avail < out->vram_free) out->vram_free = avail;
|
|
}
|
|
}
|
|
/* A failed restore leaves this thread on the wrong card; under TP that is a rank working on
|
|
* its neighbour's memory, so it is logged. */
|
|
if (moved) HIP_NOTE(hipSetDevice(prev));
|
|
|
|
/* Whether any peer is reachable, for the report; pairs go through rad_dev_enable_peer. */
|
|
int n = d_count();
|
|
for (int i = 0; i < n && !out->supports_p2p; ++i) {
|
|
if (i == device) continue;
|
|
int can = 0;
|
|
if (hipDeviceCanAccessPeer(&can, device, i) == hipSuccess && can) out->supports_p2p = 1;
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
int d_peer_access(int self, int peer) {
|
|
int can = 0;
|
|
HIP_TRY(hipDeviceCanAccessPeer(&can, self, peer));
|
|
return can ? 1 : 0;
|
|
}
|
|
|
|
/* The runtime's bus id in the kernel's spelling (lower case, four-digit domain), matching
|
|
* /sys/bus/pci/devices. */
|
|
int d_pci_address(int device, char* out, int cap) {
|
|
if (!out || cap <= 0) return RAD_E_INVAL;
|
|
char bus[64] = { 0 };
|
|
HIP_TRY(hipDeviceGetPCIBusId(bus, (int)sizeof bus, device));
|
|
unsigned dom = 0, b = 0, d = 0, f = 0;
|
|
if (std::sscanf(bus, "%x:%x:%x.%x", &dom, &b, &d, &f) != 4) {
|
|
device_set_error("hipDeviceGetPCIBusId(%d): '%s' is not a PCI address", device, bus);
|
|
return RAD_E_IO;
|
|
}
|
|
std::snprintf(out, (size_t)cap, "%04x:%02x:%02x.%x", dom, b, d, f);
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* HIP_VERSION's encoding: major * 10^7 + minor * 10^5 + patch. */
|
|
int d_runtime_version(char* out, int cap) {
|
|
if (!out || cap <= 0) return RAD_E_INVAL;
|
|
int v = 0;
|
|
HIP_TRY(hipRuntimeGetVersion(&v));
|
|
std::snprintf(out, (size_t)cap, "HIP %d.%d.%d", v / 10000000, (v / 100000) % 100, v % 100000);
|
|
return RAD_OK;
|
|
}
|
|
|
|
int d_enable_peer(int self, int peer) {
|
|
if (self == peer) return RAD_OK;
|
|
int can = 0;
|
|
HIP_TRY(hipDeviceCanAccessPeer(&can, self, peer));
|
|
if (!can) {
|
|
device_set_error("rad_dev_enable_peer(%d, %d): the driver reports no peer path; "
|
|
"a kernel library's peer-to-peer all-reduce needs one (spec §9)", self,
|
|
peer);
|
|
return RAD_E_UNSUPPORTED;
|
|
}
|
|
int prev = 0;
|
|
HIP_TRY(hipGetDevice(&prev));
|
|
HIP_TRY(hipSetDevice(self));
|
|
hipError_t e = hipDeviceEnablePeerAccess(peer, 0);
|
|
HIP_NOTE(hipSetDevice(prev));
|
|
/* Already enabled is success: init() runs per resolved instance (spec §2.1) and several may
|
|
* want the same pair. */
|
|
if (e != hipSuccess && e != hipErrorPeerAccessAlreadyEnabled)
|
|
return hip_fail(e, "hipDeviceEnablePeerAccess");
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ memory */
|
|
|
|
void* d_alloc(int64_t bytes, int kind) {
|
|
if (bytes <= 0) {
|
|
device_set_error("rad_dev_alloc(%lld): size must be positive", (long long)bytes);
|
|
return nullptr;
|
|
}
|
|
void* p = nullptr;
|
|
hipError_t e = hipSuccess;
|
|
switch (kind) {
|
|
case RAD_MEM_DEVICE:
|
|
e = hipMalloc(&p, (size_t)bytes);
|
|
break;
|
|
case RAD_MEM_HOST_PINNED:
|
|
case RAD_MEM_HOST_MAPPED:
|
|
/* Pinned by the AQL backend through ROCr (aql.cpp, "pinned host memory, in pieces"). */
|
|
device_set_error("rad_dev_alloc: pinned host memory is the AQL backend's");
|
|
return nullptr;
|
|
case RAD_MEM_HOST:
|
|
/* Pageable, page-aligned so it can be an O_DIRECT target. */
|
|
if (posix_memalign(&p, 4096, (size_t)((bytes + 4095) / 4096 * 4096)) != 0) p = nullptr;
|
|
if (!p) e = hipErrorOutOfMemory;
|
|
break;
|
|
default:
|
|
device_set_error("rad_dev_alloc: unknown memory kind %d", kind);
|
|
return nullptr;
|
|
}
|
|
if (e != hipSuccess || !p) {
|
|
hip_fail(e == hipSuccess ? hipErrorOutOfMemory : e, "rad_dev_alloc");
|
|
return nullptr;
|
|
}
|
|
return p;
|
|
}
|
|
|
|
void d_free(void* p, int kind) {
|
|
if (!p) return;
|
|
switch (kind) {
|
|
case RAD_MEM_DEVICE: HIP_NOTE(hipFree(p)); break;
|
|
case RAD_MEM_HOST: std::free(p); break;
|
|
case RAD_MEM_HOST_PINNED:
|
|
case RAD_MEM_HOST_MAPPED:
|
|
RAD_WARN("rad_dev_free(%p): pinned host memory is the AQL backend's; leaked", p);
|
|
break;
|
|
default:
|
|
RAD_WARN("rad_dev_free(%p): unknown memory kind %d; leaked rather than freed with the "
|
|
"wrong allocator", p, kind);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void* d_host_ptr(void* p) {
|
|
if (!p) return nullptr;
|
|
hipPointerAttribute_t a{};
|
|
if (hipPointerGetAttributes(&a, p) != hipSuccess) return nullptr;
|
|
return a.hostPointer;
|
|
}
|
|
|
|
void* d_device_ptr(void* p) {
|
|
if (!p) return nullptr;
|
|
hipPointerAttribute_t a{};
|
|
if (hipPointerGetAttributes(&a, p) != hipSuccess) return nullptr;
|
|
if (a.devicePointer) return a.devicePointer;
|
|
/* Mapped host allocation not resolved by the attribute query. */
|
|
void* d = nullptr;
|
|
if (hipHostGetDevicePointer(&d, p, 0) == hipSuccess) return d;
|
|
return nullptr;
|
|
}
|
|
|
|
const DeviceBackend g_hip = {
|
|
|
|
"hip",
|
|
d_count, d_set, d_props, d_enable_peer,
|
|
d_alloc, d_free, d_host_ptr, d_device_ptr,
|
|
/* Streams, events, copies and fills: filled in by the AQL backend (aql.cpp). */
|
|
nullptr, nullptr, nullptr,
|
|
nullptr, nullptr, nullptr, nullptr,
|
|
nullptr, nullptr, nullptr,
|
|
nullptr,
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
/* Recorded passes: AQL backend. */
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
/* Elastic memory: AQL backend, through HSA (aql.cpp, "elastic memory, through HSA"). */
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
|
|
|
|
d_vram_used,
|
|
/* Plugin device code: AQL backend, which owns the fat-binary registrations. */
|
|
nullptr, nullptr,
|
|
d_integrated,
|
|
d_peer_access,
|
|
d_pci_address,
|
|
d_runtime_version,
|
|
};
|
|
|
|
} /* namespace */
|
|
|
|
|
|
|
|
const DeviceBackend* hip_backend_if_present(void) {
|
|
int n = 0;
|
|
hipError_t e = hipGetDeviceCount(&n);
|
|
if (e != hipSuccess || n <= 0) {
|
|
/* Supported configuration (host backend); dispatch.cpp logs it at Info. */
|
|
RAD_DEBUG("device: hipGetDeviceCount -> %s, %d device(s)", hipGetErrorName(e), n);
|
|
return nullptr;
|
|
}
|
|
return &g_hip;
|
|
}
|
|
|
|
} /* namespace rad */
|
|
|
|
#endif /* RAD_HAVE_HIP */
|