Skip to content
Draft
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
8 changes: 7 additions & 1 deletion docs/models/inkling.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ rows took 3.4 s.
| **Disk** | 9.1 to 17.6 GB for the model file. |
| **System** | Linux or Windows 10 / 11, and a current NVIDIA driver; or macOS 15 or newer. |

Not run yet: an AMD card, two cards (`--expert-gpu`, built but not run), UD-Q4_K_XL on the 8 GB card and UD-Q4_K_XL
**AMD (HIP), experimental, not ready:** the engine builds for RDNA3 and RDNA4 (gfx1100, gfx1200, gfx1201), and main's
HIP tests pass on an RX 7900 XT (15 of 16) and on an RX 9070 XT (11 of 16), both under ROCm 7.2.4 in a container that
reaches the card through librocdxg. Known problems there: a prompt with some experts in RAM can hang (the engine waits
forever, at full CPU load); on the RX 9070 XT the prompt products' library gave a NaN and results that differ run to run
in its test. Use an NVIDIA card or a Mac for now.

Not run yet: a desktop AMD card on native Linux or Windows, two cards (`--expert-gpu`, built but not run), UD-Q4_K_XL on the 8 GB card and UD-Q4_K_XL
on a Mac. Pictures go through Unsloth's mmproj-F16.gguf, read on the CPU (`--vision cpu`) or the card; on a Mac, on
the CPU. The draft file is an
option (`--draft-model`). The engine takes one request at a time
Expand Down
8 changes: 8 additions & 0 deletions engine/include/strata/hip_compat/cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
#define STRATA_VMM_MAP_SETTLES 1
#endif

// ROCm 7.2.4 under WSL2 (librocdxg 1.2.2, an RX 9070 XT) refuses hipMemSetAccess (hipErrorInvalidValue) on a piece
// mapped after a larger one in the same reservation - 64 MiB then 1 MiB, 2 then 1, 64 then 4 - while pieces of one
// size always pass (64 + 64, 8 + 8, 1 + 1 MiB): the expert cache lost its shorter last piece, and an attention cache
// grown to its reservation's end failed. Under WSL (Engine::vmm_map()) every piece of a reservation is one size.
#if !defined(_WIN32)
#define STRATA_VMM_EQUAL_PIECES 1
#endif

typedef hipError_t CUresult;
typedef int CUdevice;
// HIP's own CUdeviceptr/handle equivalents are pointer-typed; CUDA's are integers, and strata-poly's Mapped
Expand Down
5 changes: 3 additions & 2 deletions engine/src/kernels/cuda/native_mmvq.cu
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,9 @@ __device__ __align__(4) int8_t mxfp4_values[16] = {0, 1, 2, 3, 4, 6, 8, 12, 0, -

// Eight four-bit codes of q4 looked up in a codebook of 16: the even nibbles' values in .x, the odd ones' in .y.
__device__ __forceinline__ int2 table16_lookup(const int8_t* table, int q4) {
#if defined(STRATA_HIP_GFX906)
// AMD: llama.cpp's HIP lookup (see iq_kernels.cu get_int_from_table_16): 4 v_perm_b32 per 8 values
#if defined(__HIPCC__)
// AMD: llama.cpp's HIP lookup (see iq_kernels.cu get_int_from_table_16): 4 v_perm_b32 per 8 values, and 2 to pick
// the halves - the same bytes as the __byte_perm sequence below, which was slower on RDNA4
const uint32_t* v32 = reinterpret_cast<const uint32_t*>(table);
const uint32_t q_even = (uint32_t) q4, q_odd = (uint32_t) q4 >> 4;
const uint32_t el = __builtin_amdgcn_perm(v32[1], v32[0], q_even & 0x07070707u);
Expand Down
89 changes: 88 additions & 1 deletion engine/src/poly/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,9 +942,41 @@ void Engine::mind_memory() {
#endif
}

#ifdef STRATA_VMM_EQUAL_PIECES
// Whether a reservation's pieces must all be of one size (hip_compat/cuda.h): under WSL2, where ROCm reaches the card
// through librocdxg (/dev/dxg, and no /dev/kfd). STRATA_VMM_EQUAL_PIECES=1 or 0 says it for any Linux HIP build.
static bool vmm_equal_pieces() {
static const bool on = [] {
if (const char* v = std::getenv("STRATA_VMM_EQUAL_PIECES"); v && v[0]) return v[0] != '0';
std::error_code ec;
return std::filesystem::exists("/dev/dxg", ec) && !std::filesystem::exists("/dev/kfd", ec);
}();
return on;
}

// The unit such pieces are made of, 2 MiB like the pool's handles: vmm_reserve() reserves a whole number of them.
static size_t vmm_unit(size_t grain) { return up_to(size_t(2) << 20, grain); }

// The size of every piece of a reservation of `reserved` bytes: `first`, the size of the piece it has, or for its
// first map the most units that divide the reservation and are no more than the piece asked for and the map's bytes.
// A map then covers a whole number of pieces, and the last one is no shorter than the others.
static size_t vmm_piece(size_t reserved, size_t first, size_t bytes, size_t piece, size_t unit) {
if (first) return first;
const size_t most = std::min(up_to(std::min(piece, reserved), unit), up_to(bytes, unit)) / unit;
size_t k = std::max<size_t>(most, 1);
while (k > 1 && (reserved / unit) % k != 0) --k;
return k * unit;
}
#endif

void Engine::vmm_reserve(Mapped& m, size_t bytes) {
CUdeviceptr p = 0;
#ifdef STRATA_VMM_EQUAL_PIECES
// with equal pieces, a whole number of their units
m.reserved = up_to(std::max<size_t>(bytes, 1), pool_on_ ? pg_ : vmm_equal_pieces() ? vmm_unit(grain_) : grain_);
#else
m.reserved = up_to(std::max<size_t>(bytes, 1), pool_on_ ? pg_ : grain_); // a whole number of pool handles
#endif
cu_ck(cuMemAddressReserve(&p, m.reserved, 0, 0, 0), "reserving addresses for VRAM");
m.base = p;
}
Expand Down Expand Up @@ -1042,7 +1074,16 @@ void Engine::vmm_map(Mapped& m, size_t bytes, size_t piece, const char* what) {
// With the pool a piece is one of its handles and comes from it, never from the card; a map it cannot serve
// throws as a card that has no more VRAM does
if (pool_on_) piece = pg_;
#ifdef STRATA_VMM_EQUAL_PIECES
if (!pool_on_ && vmm_equal_pieces()) { // whole pieces of the reservation's one size
piece = vmm_piece(m.reserved, m.pieces.empty() ? 0 : m.pieces.front().second, bytes, piece, vmm_unit(grain_));
bytes = std::min(up_to(bytes, piece), m.reserved);
} else {
bytes = std::min(up_to(bytes, pool_on_ ? pg_ : grain_), m.reserved);
}
#else
bytes = std::min(up_to(bytes, pool_on_ ? pg_ : grain_), m.reserved);
#endif
CUmemAllocationProp prop = {};
prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
Expand Down Expand Up @@ -1367,7 +1408,19 @@ void* Engine::kv_alloc(size_t bytes, int per) {
Kv k;
k.bytes = bytes;
k.per = per;
#ifdef STRATA_VMM_EQUAL_PIECES
// with equal pieces, a whole number of cover()'s (addresses only; at most a piece more is mapped at the whole
// context): vmm_piece() takes the most units that divide the reservation, which for one of another size could
// be one unit, every piece 2 MiB and a file descriptor on ROCm
if (!pool_on_ && vmm_equal_pieces()) {
const size_t unit = vmm_unit(grain_), piece = (size_t) ((kv_grow() + per - 1) / per) * bytes;
vmm_reserve(k, up_to(all, up_to(std::min(piece, up_to(all, unit)), unit)));
} else {
vmm_reserve(k, all);
}
#else
vmm_reserve(k, all);
#endif
kv_.push_back(std::move(k));
p = (void*) kv_.back().base;
}
Expand Down Expand Up @@ -1502,7 +1555,17 @@ void Engine::cover(int64_t end) {
const auto size = [rows](const Kv& k) { return (size_t) ((rows + k.per - 1) / k.per) * k.bytes; };
size_t more = 0, held = arena_.mapped + other_rows();
for (const Kv& k : kv_) {
#ifdef STRATA_VMM_EQUAL_PIECES
// what vmm_map() below maps: with equal pieces, whole pieces
const size_t piece = pool_on_ ? pg_
: !vmm_equal_pieces()
? grain_
: vmm_piece(k.reserved, k.pieces.empty() ? 0 : k.pieces.front().second, size(k),
(size_t) ((kv_grow() + k.per - 1) / k.per) * k.bytes, vmm_unit(grain_));
const size_t want = std::min(up_to(size(k), piece), k.reserved);
#else
const size_t want = std::min(up_to(size(k), pool_on_ ? pg_ : grain_), k.reserved);
#endif
if (want > k.mapped) more += want - k.mapped; // trim_card_kv() leaves a piece it cuts through
held += k.mapped;
}
Expand Down Expand Up @@ -2211,7 +2274,13 @@ void Engine::fill_expert_cache() {
#endif
if (arena == 0) return;
if (vmm_) { // in pieces, so that it can give some up to the attention caches (shrink_cache()) and grow
#ifdef STRATA_VMM_EQUAL_PIECES
// with equal pieces, a whole number of kArenaPiece, so that its pieces can all be of that size (vmm_map())
if (!arena_.base)
vmm_reserve(arena_, pool_on_ || !vmm_equal_pieces() ? most + 256 : up_to(most + 256, kArenaPiece));
#else
if (!arena_.base) vmm_reserve(arena_, most + 256); // addresses only: grow_cache() maps more of them
#endif
try {
vmm_map(arena_, arena + 256, kArenaPiece, "the expert cache");
} catch (const std::runtime_error&) { // the card may not have it all (grow_cache() is just as tolerant)
Expand Down Expand Up @@ -3123,6 +3192,16 @@ void Engine::load(const Options& o) {
// allocation each (kv_alloc()) and the expert cache still gives VRAM to other programs and takes it back
// (mind_vram()): refilled in one allocation instead, it only ever got smaller.
ck(cudaGetDevice(&device_), "cudaGetDevice");
#if defined(STRATA_USE_HIP) && defined(__linux__)
// ROCm keeps a file descriptor open for each VMM handle (its dmabuf; librocdxg under WSL2): with --vram-pool's
// 2 MiB handles the usual soft limit of 1,024 descriptors held an RX 9070 XT's pool to 2 GiB ("fail to import fd",
// and the map fails as a card without VRAM does). The soft limit goes up to the hard one (at most 2^20), as a
// process may, and never down.
if (rlimit r{}; getrlimit(RLIMIT_NOFILE, &r) == 0 && r.rlim_cur < r.rlim_max) {
r.rlim_cur = std::max(r.rlim_cur, std::min<rlim_t>(r.rlim_max, rlim_t(1) << 20));
(void) setrlimit(RLIMIT_NOFILE, &r);
}
#endif
CUdevice dev = 0;
int vmm = 0;
if (cuDeviceGet(&dev, device_) == CUDA_SUCCESS &&
Expand Down Expand Up @@ -3671,8 +3750,16 @@ void Engine::alloc() {
work = floats_bytes + (need.halves * 2 + 255) / 256 * 256;
// The buffers only a call of kStageRows rows or more uses (moe_rows(), lin_raw()'s products), side by side:
// where they can be mapped apart (VMM) and the answer's calls never have that many rows, the expert cache
// has their VRAM between prompts (lend_prompt()), 300 MB of Qwen3.6's at a prompt batch of 2,048.
// has their VRAM between prompts (lend_prompt()), 300 MB of Qwen3.6's at a prompt batch of 2,048. Not on ROCm
// on Linux, which gives VRAM back only with its addresses (vmm_unmap()), as the staging buffer's widening is
// not: with the buffers lent, strata-poly crashed (SIGSEGV inside hipLaunchKernel, the dequantize launch of a
// prompt's dense product into the lent work memory) in 15 of 16 runs of Inkling 2 Mini IQ4_NL on an RX 9070 XT
// with 2,443 of its 3,072 experts in VRAM, ROCm 7.2.4.
#ifdef STRATA_VMM_RELEASE_WITH_ADDRESSES
prompt_lends_ = false;
#else
prompt_lends_ = vmm_ && !split_ && std::max(opt_.window, most_step) < kStageRows;
#endif
const size_t sizes[] = {rr * 2 * n_ff_exp_ * sizeof(float), rr * n_ff_exp_ * sizeof(float),
rr * n_embd_ * sizeof(float), rr * sizeof(float),
Mmq::q8_bytes((int64_t) rr, n_embd_), Mmq::q8_bytes((int64_t) rr, n_ff_exp_),
Expand Down
62 changes: 62 additions & 0 deletions engine/src/poly/inkling2.cu
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ void launched(const char* what) {
throw std::runtime_error(std::string(what) + ": " + cudaGetErrorString(e));
}

#if defined(__HIPCC__)
// HIP has __ldcs and __ldcg for halves only. The DSpark screen's are cache hints (a copy's words read once, the
// tiles' candidates from earlier launches), so on HIP they are plain loads.
template <typename T> __device__ __forceinline__ T __ldcs(const T* p) { return *p; }
template <typename T> __device__ __forceinline__ T __ldcg(const T* p) { return *p; }
#endif

constexpr int WARP = 32; // the lanes a shuffle spans: a warp, or half of a 64-lane wavefront

// The warp's sum of x, in every lane.
Expand Down Expand Up @@ -1631,9 +1638,61 @@ void ink_moe_conv_add(const float* x, const float* w, const float* scale, const
launched("ink_moe_conv_add");
}

#if defined(__HIPCC__)
// HIP: ink_clips() as the launches its contract names (ink_clip(), ink_head_rms() and fp16 stores of its jobs), and
// ink_qkv() as ink_conv() of K and V and then those. ink_clips_kernel and ink_qkv_kernel never finished on an
// RX 7900 XT (gfx1100) or an RX 9070 XT (gfx1201) under ROCm 7.2.4, the first prompt or token of Inkling 2 Mini
// with them, and the same code built with any change to ink_clips_kernel finished: the compiler's code for these
// two kernels, not their arithmetic. The separate kernels give the same bits (poly_inkling2_test's clips and qkv
// cases compare them).
static void clips_apart(const InkClips& c, void* stream) {
if (c.kv8) throw std::logic_error("ink_clips: no FP8 caches under HIP");
for (int j = 0; j < 4; ++j) {
if (c.heads[j] <= 0) continue;
if (c.rms[j])
ink_head_rms(c.x[j], c.dim[j], c.heads[j], c.rows, c.eps, stream);
else
ink_clip(c.x[j], c.scale[j], c.thr[j], c.dim[j], c.heads[j], c.rows, c.pos, c.alpha[j], c.floor[j], stream,
c.at);
if (!c.cache[j]) continue;
const int n = c.heads[j] * c.dim[j];
if (c.at) store_f16_at(c.x[j], c.cache[j], n, c.at, c.rows, c.ring, stream);
else if (c.ring > 0) ink_store_ring(c.x[j], c.cache[j], n, c.pos, c.rows, c.ring, stream);
else store_f16(c.x[j], c.cache[j], n, c.pos, c.rows, stream);
launched("ink_clips' stores");
}
}
#endif

void ink_qkv(const InkQkv& a, void* stream) {
if (a.dim > 128 || a.n_rel > 128 || a.taps < 2 || a.taps > 8 || a.n_kv < 1)
throw std::invalid_argument("ink_qkv: heads of up to 128 values, 2 to 8 taps");
#if defined(__HIPCC__)
const int kv = a.n_kv * a.dim;
ink_conv(a.kraw, a.kstate, a.kw, a.kdyn, a.k, kv, a.n_kv, a.taps, 1, stream);
ink_conv(a.vraw, a.vstate, a.vw, a.vdyn, a.v, kv, a.n_kv, a.taps, 1, stream);
InkClips c;
float* xs[4] = {a.q, a.k, a.v, a.r};
const float* sc[4] = {a.q_scale, nullptr, nullptr, a.r_scale};
const float thr[4] = {a.qk_thr, a.qk_thr, 0.f, a.rel_thr}, al[4] = {a.alpha, 0.f, 0.f, a.alpha};
const float fl[4] = {a.floor, 1.f, 1.f, a.floor};
const int dims[4] = {a.dim, a.dim, a.dim, a.n_rel}, hs[4] = {a.n_head, a.n_kv, a.n_kv, a.n_head};
for (int j = 0; j < 4; ++j) {
c.x[j] = xs[j], c.scale[j] = sc[j], c.thr[j] = thr[j], c.alpha[j] = al[j], c.floor[j] = fl[j];
c.dim[j] = dims[j], c.heads[j] = hs[j];
}
c.rms[2] = true;
c.eps = a.eps;
c.pos = a.pos;
c.at = a.at;
c.rows = 1;
c.cache[1] = a.keys;
c.cache[2] = a.vals;
c.ring = a.ring;
c.kv8 = a.kv8;
clips_apart(c, stream);
return;
#endif
QkvArgs g{a, 1.0f / std::sqrt((float) ((a.kdyn ? 2 : 1) * a.taps + 1)),
1.0f / std::sqrt((float) ((a.vdyn ? 2 : 1) * a.taps + 1))};
launch_pdl(a.kv8 ? ink_qkv_kernel<true> : ink_qkv_kernel<false>, dim3(2 * a.n_head + 2 * a.n_kv), dim3(128), 0,
Expand All @@ -1651,6 +1710,9 @@ void ink_clip(float* x, const float* scale, float thr, int dim, int heads, int r

void ink_clips(const InkClips& c, void* stream) {
if (c.rows <= 0) return;
#if defined(__HIPCC__)
return clips_apart(c, stream);
#endif
auto* k = c.kv8 ? ink_clips_kernel<true> : ink_clips_kernel<false>;
k<<<c.rows * (c.heads[0] + c.heads[1] + c.heads[2] + c.heads[3]), 128, 0, cs(stream)>>>(c);
launched("ink_clips");
Expand Down
3 changes: 3 additions & 0 deletions engine/src/poly/mmq_tiles_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ int run_case(const Quant& q, const std::vector<int>& rows, const std::vector<int
cudaMemset(dref, 0, (size_t) total * kRows * 4);
cudaMemset(dcls, 0, (size_t) total * kRows * 4);
const int maxrows = *std::max_element(rows.begin(), rows.end());
// the bounds mirrored on the host, as the engine has them (Mmq::mirror()): the BLAS path reads them there
mmq.mirror(d_rb, ref_bounds.data(), ref_bounds.size() * sizeof(int32_t));
mmq.quantize(dx, d_gref, xq, q.type, kCols, kCols, total, nullptr);
mmq.mul(dw, q.type, kRows, kCols, per, n, xq, d_rb, d_iota, total, maxrows, dref, kRows, nullptr);
mmq.mirror(d_cb, bounds.data(), bounds.size() * sizeof(int32_t));
mmq.quantize(dx, d_gcls, xq, q.type, kCols, kCols, total, nullptr);
for (int k = 0; k < nclass; ++k)
if (most[(size_t) k] > 0)
Expand Down
4 changes: 2 additions & 2 deletions terminal/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,8 @@ def ms_meta(repo: str, path: str):
# request at a time ("one_request": setup writes "parallel": 1). "vram_beside_gb" is the peak VRAM measured on an RTX 5090 with no expert on the card, a 10,081-token
# prompt read 1,024 tokens at a time at a 32K context (2,916 MiB), plus the rows of the context's other tokens.
"inkling": {"title": "Inkling 2 Mini", "by": "Thinking Machines; quantized by Unsloth",
"about": "experimental, on the strata-poly engine: one NVIDIA card, Linux or Windows, or a Mac (beta), "
"reads pictures; a 17.6 GB file, or 13.6 or 9.1 GB",
"about": "experimental, on the strata-poly engine: one NVIDIA card, Linux or Windows, or a Mac (beta); "
"AMD cards are not ready (docs/models/inkling.md); reads pictures; a 17.6 GB file, or 13.6 or 9.1 GB",
"hf": hf("unsloth/Inkling-2-Mini-GGUF") + "Mainline_Support/", "file": "Inkling-2-Mini-{q}.gguf",
"remote_suffix": "_file", "shards": 1,
"tag": "inkling-", "name": "inkling-2-mini", "engine": "poly", "vision": True, "experimental": True,
Expand Down
4 changes: 2 additions & 2 deletions terminal/tools/strata_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@
"Q8_0": {"about": "8-bit throughout: the closest to the model as trained",
"download_gb": 25.5, "ram_gb": 31, "arena_gb": 24.4}}},
"inkling": {"title": "Inkling 2 Mini",
"about": "experimental, on the strata-poly engine: one NVIDIA card, Linux or Windows, or a Mac (beta), "
"reads pictures; a 17.6 GB file, or 13.6 or 9.1 GB",
"about": "experimental, on the strata-poly engine: one NVIDIA card, Linux or Windows, or a Mac (beta); "
"AMD cards are not ready (docs/models/inkling.md); reads pictures; a 17.6 GB file, or 13.6 or 9.1 GB",
"tag": "inkling-", "engine": "poly", "vision": True, "experimental": True,
"models": {"UD-Q4_K_XL": {"about": "4-bit experts (NVFP4, Unsloth Dynamic), 8-bit where they write",
"download_gb": 17.6, "ram_gb": 21, "arena_gb": 15.797846016},
Expand Down
Loading