Skip to content
Open
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
7 changes: 6 additions & 1 deletion docs/AMD_HIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,12 @@ before it installs a model.
- Under WSL2 (ROCm 7.2.4 with librocdxg: `/dev/dxg` and no `/dev/kfd`) ROCm refuses `hipMemSetAccess` on a piece
mapped after a larger one in the same reservation, so every piece of a reservation is made one size there (the
expert cache had lost its last 64 MiB piece, and an attention cache grown to its end failed).
`STRATA_VMM_EQUAL_PIECES=1` or `=0` turns that on or off on any Linux HIP build.
`STRATA_VMM_EQUAL_PIECES=all` (or `1`) or `=0` turns that on or off on any Linux HIP build.
- On native Linux ROCm 7.2.4 (`/dev/kfd`, RX 9070 XT) the expert cache's shorter last piece is refused the same way, so
`STRATA_GPU_EXPERTS=N` was refused for every N. The expert cache's pieces are now all one size on every Linux HIP
build (64, 32 or 16 MiB, whichever holds the most under free VRAM minus the reserve); the other reservations map as
before there. `STRATA_VMM_EQUAL_PIECES=0` turns that off on native Linux (WSL2 is as above);
`=arena` asks for only that on any Linux HIP build.
- hipBLASLt loads a product type's kernels at its first call. On Windows (RX 9070 XT, ROCm 10.2.0a20260930) the
first single-precision product of a prompt took 4 s on one CPU thread with the card idle, inside the first prompt
of every strata-poly process. strata-poly now makes one small product of each kind the prompt uses (halves into
Expand Down
4 changes: 3 additions & 1 deletion engine/include/strata/hip_compat/cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
// 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.
// grown to its reservation's end failed. Native ROCm 7.2.4 (/dev/kfd) refuses the shorter last piece of the expert cache
// the same way. Under WSL (Engine::vmm_map()) every piece of every reservation is one size; on native Linux only the
// expert cache's.
#if !defined(_WIN32)
#define STRATA_VMM_EQUAL_PIECES 1
#endif
Expand Down
71 changes: 58 additions & 13 deletions engine/src/poly/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,16 +1016,22 @@ void Engine::mind_memory() {
}

#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';
// Which reservations' pieces must all be of one size (hip_compat/cuda.h): 2 every one, under WSL2, where ROCm reaches the
// card through librocdxg (/dev/dxg, and no /dev/kfd); 1 the expert cache's only, on native ROCm 7.2.4 (/dev/kfd, an
// RX 9070 XT), which refuses its shorter last piece as WSL2 does, while the other reservations (attention caches, the
// staging buffer, a prompt's work memory) map as before there; 0 none. STRATA_VMM_EQUAL_PIECES=all (or 1), =arena
// and =0 say it for any Linux HIP build.
static int vmm_equal_mode() {
static const int mode = [] {
if (const char* v = std::getenv("STRATA_VMM_EQUAL_PIECES"); v && v[0])
return v[0] == '0' ? 0 : std::strcmp(v, "arena") == 0 ? 1 : 2;
std::error_code ec;
return std::filesystem::exists("/dev/dxg", ec) && !std::filesystem::exists("/dev/kfd", ec);
return std::filesystem::exists("/dev/dxg", ec) && !std::filesystem::exists("/dev/kfd", ec) ? 2 : 1;
}();
return on;
return mode;
}
static bool vmm_equal_pieces() { return vmm_equal_mode() == 2; }
static bool arena_equal_pieces() { return vmm_equal_mode() >= 1; }

// 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); }
Expand Down Expand Up @@ -1061,7 +1067,7 @@ 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_);
m.reserved = up_to(std::max<size_t>(bytes, 1), pool_on_ ? pg_ : (m.equal || 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
Expand Down Expand Up @@ -1163,8 +1169,13 @@ void Engine::vmm_map(Mapped& m, size_t bytes, size_t piece, const char* what) {
// 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_));
if (!pool_on_ && (m.equal || vmm_equal_pieces())) { // whole pieces of the reservation's one size
// an expert cache that was cut to nothing keeps its piece size for the next map (native only: under WSL
// vmm_equal_pieces() chooses it again, as before)
const bool keep = m.equal && !vmm_equal_pieces();
piece = vmm_piece(m.reserved, m.pieces.empty() ? (keep ? m.piece0 : 0) : m.pieces.front().second, bytes, piece,
vmm_unit(grain_));
if (keep && m.pieces.empty()) m.piece0 = piece;
bytes = std::min(up_to(bytes, piece), m.reserved);
} else {
bytes = std::min(up_to(bytes, pool_on_ ? pg_ : grain_), m.reserved);
Expand Down Expand Up @@ -2416,13 +2427,47 @@ void Engine::fill_expert_cache() {
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));
if (!arena_.base) {
arena_.equal = arena_equal_pieces();
vmm_reserve(arena_, pool_on_ || !(arena_.equal || 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
size_t arena_piece = kArenaPiece;
#ifdef STRATA_VMM_EQUAL_PIECES
if (!pool_on_ && arena_.equal && !vmm_equal_pieces() && free_b > reserve) { // native; WSL sizes as before
arena_.piece0 = 0; // chosen again for this load
// With equal pieces a map covers whole pieces, so it must be sized to the pieces that fit under free
// minus the reserve, never past it: a cap that needs the room rounded up to a piece is tried with 32 and
// 16 MiB pieces too, and a cache the room limits is rounded down to whole pieces of the size that holds the
// most. A piece the cache already has stays the size.
const size_t unit = vmm_unit(grain_), room = free_b - reserve;
const size_t front = arena_.pieces.empty() ? 0 : arena_.pieces.front().second;
const auto mapped = [&](size_t want, size_t p, bool down) {
const size_t pc = vmm_piece(arena_.reserved, front, want, p, unit);
return std::min(down ? want / pc * pc : up_to(want, pc), arena_.reserved);
};
const size_t sizes[3] = {kArenaPiece, kArenaPiece / 2, kArenaPiece / 4};
const size_t n = front ? 1 : 3, want = arena + 256;
bool fits = false;
if (arena >= all)
for (size_t i = 0; i < n && !fits; ++i)
if (mapped(want, front ? kArenaPiece : sizes[i], false) <= room) fits = true, arena_piece = sizes[i];
if (!fits) {
size_t best = 0;
for (size_t i = 0; i < n; ++i) {
const size_t b = mapped(std::min(want, room), front ? kArenaPiece : sizes[i], true);
if (b > best) best = b, arena_piece = sizes[i];
}
arena = best > 256 ? best - 256 : 0;
if (expert_cap_ > 0 && arena < all) short_of(arena, all);
if (arena == 0) return;
}
}
#endif
try {
vmm_map(arena_, arena + 256, kArenaPiece, "the expert cache");
vmm_map(arena_, arena + 256, arena_piece, "the expert cache");
} catch (const std::runtime_error&) { // the card may not have it all (grow_cache() is just as tolerant)
arena = arena_.mapped > 256 ? arena_.mapped - 256 : 0;
if (expert_cap_ > 0 && arena < all) short_of(arena, all);
Expand Down
2 changes: 2 additions & 0 deletions engine/src/poly/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,8 @@ class Engine {
struct Mapped {
unsigned long long base = 0; ///< a CUdeviceptr
size_t reserved = 0, mapped = 0;
size_t piece0 = 0; ///< the piece size an equal-piece cache kept when it was cut to nothing
bool equal = false; ///< its pieces are all of one size even where the other reservations' are not (the expert cache)
std::vector<std::pair<unsigned long long, size_t>> pieces; ///< allocation handles and their bytes, in order
};
struct Kv : Mapped {
Expand Down
2 changes: 1 addition & 1 deletion terminal/data/settings-register.json
Original file line number Diff line number Diff line change
Expand Up @@ -51808,7 +51808,7 @@
"last_date": null,
"status": "research",
"owner": "poly",
"note": "[reviewed] HIP on Linux (STRATA_VMM_EQUAL_PIECES builds): every piece of a VRAM reservation is one size; on by default under WSL2 (/dev/dxg without /dev/kfd), where ROCm 7.2.4 with librocdxg refuses hipMemSetAccess on a piece mapped after a larger one; =1 or =0 forces it on or off (engine/src/poly/engine.cpp:948, docs/AMD_HIP.md:518)",
"note": "HIP on Linux (STRATA_VMM_EQUAL_PIECES builds): which VRAM reservations map in pieces of one size. WSL2 (/dev/dxg without /dev/kfd): every one, where ROCm 7.2.4 with librocdxg refuses hipMemSetAccess on a piece mapped after a larger one. Native Linux: the expert cache's only (=arena), which native ROCm 7.2.4 refuses a shorter last piece of. =all (or =1) every reservation, =arena the expert cache's only, =0 none, on any Linux HIP build.",
"retire_after": "",
"retired_in": ""
},
Expand Down
Loading