Skip to content
Merged
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
2 changes: 2 additions & 0 deletions engine/cmake/strata_poly.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ endif()
if(STRATA_BUILD_TESTS)
add_executable(poly_hip_value_split_policy_test src/poly/hip_value_split_policy_test.cpp)
add_test(NAME poly_hip_value_split_policy_test COMMAND poly_hip_value_split_policy_test)
add_executable(poly_device_pick_test src/poly/device_pick_test.cpp)
add_test(NAME poly_device_pick_test COMMAND poly_device_pick_test)
add_executable(poly_gdn_mma_control_test src/poly/gdn_mma_control_test.cpp)
add_test(NAME poly_gdn_mma_control_test COMMAND poly_gdn_mma_control_test)
add_executable(poly_q35_scratch_arena_test src/poly/q35_scratch_arena_test.cpp)
Expand Down
71 changes: 71 additions & 0 deletions engine/src/poly/device_pick.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// src/poly/device_pick.hpp - Which GPUs strata-poly uses by default on AMD (HIP).
// Some setups list the Ryzen iGPU (gfx1036, gfx1103, ...) as device 0 in front of the real card (WSL with ROCm 10.2),
// and the engine's default device is 0. So by default an integrated device is left out when a discrete one exists.
// A choice made with HIP_VISIBLE_DEVICES / ROCR_VISIBLE_DEVICES / CUDA_VISIBLE_DEVICES is kept exactly as it is.
// Pure logic: no GPU needed (device_pick_test.cpp).
#pragma once

#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>

namespace strata::poly::device_pick {

struct Dev {
std::string arch; // gcnArchName, "gfx1036:sramecc-:xnack-" or "gfx1036"
bool integrated = false; // hipDeviceProp_t.integrated
};

struct Pick {
std::vector<int> ids; // the devices to use, in order: ids[0] is the primary one
std::string why; // one line for the log
};

// The APUs (Ryzen iGPUs) whose flag may read 0 under some drivers.
inline bool apu_arch(const std::string& arch) {
for (const char* a : {"gfx90c", "gfx1035", "gfx1036", "gfx1103", "gfx1150", "gfx1151", "gfx1152", "gfx1153"})
if (arch.compare(0, std::strlen(a), a) == 0 && (arch.size() == std::strlen(a) || arch[std::strlen(a)] == ':'))
return true;
return false;
}

inline bool is_integrated(const Dev& d) { return d.integrated || apu_arch(d.arch); }

// `env_set`: one of the *_VISIBLE_DEVICES variables is set (non-empty).
inline Pick pick(const std::vector<Dev>& devs, bool env_set) {
Pick p;
const int n = (int) devs.size();
for (int i = 0; i < n; ++i) p.ids.push_back(i);
if (n == 0) {
p.why = "no device";
return p;
}
if (env_set) {
p.why = "the *_VISIBLE_DEVICES variable is set, so all " + std::to_string(n) + " visible device(s) are kept";
return p;
}
std::vector<int> discrete;
for (int i = 0; i < n; ++i)
if (!is_integrated(devs[(size_t) i])) discrete.push_back(i);
if (discrete.empty()) {
p.why = "only integrated device(s) found, using them";
} else if ((int) discrete.size() == n) {
p.why = "no integrated device, all " + std::to_string(n) + " device(s) are kept";
} else {
p.ids = discrete;
p.why = "skipped " + std::to_string(n - (int) discrete.size()) +
" integrated device(s); set HIP_VISIBLE_DEVICES to choose by hand";
}
return p;
}

inline bool env_selects_devices() {
for (const char* v : {"HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "CUDA_VISIBLE_DEVICES", "GPU_DEVICE_ORDINAL"}) {
const char* s = std::getenv(v);
if (s && *s) return true;
}
return false;
}

} // namespace strata::poly::device_pick
34 changes: 34 additions & 0 deletions engine/src/poly/device_pick_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// No GPU needed: the default device choice over fake device lists.
#include "device_pick.hpp"
#include <cstdio>

using namespace strata::poly::device_pick;
int main() {
int bad = 0;
const auto check = [&](bool ok, const char* why) {
if (!ok) { std::fprintf(stderr, "FAIL %s\n", why); ++bad; }
};
const Dev igpu{"gfx1036", false}, igpu_flag{"gfx9999", true}, d9070{"gfx1201:sramecc-:xnack-", false},
d7900{"gfx1100", false};
Pick p = pick({igpu, d9070}, false);
check(p.ids == std::vector<int>{1}, "iGPU first, discrete second: the discrete one");
p = pick({igpu_flag, d9070}, false);
check(p.ids == std::vector<int>{1}, "the integrated flag alone skips a device");
p = pick({{"gfx1036:xnack-", false}, d9070}, false);
check(p.ids == std::vector<int>{1}, "arch with features suffix is an APU");
p = pick({d9070, igpu}, false);
check(p.ids == std::vector<int>{0}, "discrete first, iGPU second");
p = pick({igpu}, false);
check(p.ids == std::vector<int>{0}, "only integrated: used");
p = pick({d9070, d7900}, false);
check(p.ids == std::vector<int>{0, 1}, "two discrete: both, in order");
p = pick({igpu, d9070, d7900}, false);
check(p.ids == std::vector<int>{1, 2}, "iGPU and two discrete");
p = pick({igpu, d9070}, true);
check(p.ids == std::vector<int>{0, 1}, "env set: nothing is skipped");
check(!apu_arch("gfx10360") && !apu_arch("gfx11") && apu_arch("gfx1151"), "arch match is exact");
check(pick({}, false).ids.empty(), "no devices");
check(!p.why.empty(), "a reason is given");
if (!bad) std::puts("poly_device_pick_test: ok");
return bad ? 1 : 0;
}
58 changes: 51 additions & 7 deletions engine/src/poly/engine.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// src/poly/engine.cpp - see the header. What every architecture shares: the files, the weights' way to VRAM, the
// routed experts (the cache, the CPU, a prompt batch), the sequence's state, and the calls that feed tokens.
#include "engine.hpp"
#include "device_pick.hpp"
#include "stage_copy.hpp"

#include "deepseek4.hpp"
Expand Down Expand Up @@ -606,6 +607,44 @@ class Behind {
std::exception_ptr err_;
};

// The devices this process uses, in order; empty where every device is used as it is (CUDA, Metal). On HIP an
// integrated device (a Ryzen iGPU that some setups list as device 0) is left out when a discrete one exists, unless HIP_VISIBLE_DEVICES
// or ROCR_VISIBLE_DEVICES chooses (device_pick.hpp). Prints one line, once, saying which and why.
const std::vector<int>& device_ids() {
static const std::vector<int> ids = [] {
#if defined(STRATA_USE_HIP)
int n = 0;
ck(cudaGetDeviceCount(&n), "cudaGetDeviceCount");
std::vector<device_pick::Dev> devs((size_t) std::max(n, 0));
for (int i = 0; i < n; ++i) {
cudaDeviceProp p{};
if (cudaGetDeviceProperties(&p, i) != cudaSuccess) {
(void) cudaGetLastError();
continue;
}
devs[(size_t) i].arch = p.gcnArchName;
devs[(size_t) i].integrated = p.integrated != 0;
}
const device_pick::Pick pk = device_pick::pick(devs, device_pick::env_selects_devices());
if (n > 0) {
std::string list;
for (int i : pk.ids) list += (list.empty() ? "" : ",") + std::to_string(i);
std::fprintf(stderr, "strata-poly: GPU device %s of %d: %s\n", list.c_str(), n, pk.why.c_str());
}
return pk.ids;
#else
return std::vector<int>();
#endif
}();
return ids;
}

// The device for the i-th part of a split over `devices` devices, in turn.
int device_at(int i, int devices) {
const std::vector<int>& ids = device_ids();
return ids.empty() ? i % devices : ids[(size_t) (i % devices)];
}

// A card's memory bandwidth in GB/s for split_plan.hpp, from its peak memory clock and bus width; 0 where it is not
// known (Metal; HIP, whose clocks are not read this way yet). STRATA_POLY_CARD_GBS=a,b,... gives each card's.
double card_gbs(int d) {
Expand Down Expand Up @@ -685,11 +724,12 @@ std::vector<int> layer_starts(const std::string& split, const Shards& file, int
std::vector<double> room((size_t) cards);
SplitCard two[2];
double all_room = 0;
int devices = 1;
ck(cudaGetDeviceCount(&devices), "cudaGetDeviceCount");
const std::vector<int>& dev_ids = device_ids();
int devices = (int) dev_ids.size();
if (dev_ids.empty()) ck(cudaGetDeviceCount(&devices), "cudaGetDeviceCount");
for (int d = 0; d < cards; ++d) { // (STRATA_POLY_CARDS: parts on the devices there are, in turn)
size_t free_b = 0, total_b = 0;
ck(cudaSetDevice(d % devices), "cudaSetDevice");
ck(cudaSetDevice(device_at(d, devices)), "cudaSetDevice");
ck(cudaMemGetInfo(&free_b, &total_b), "cudaMemGetInfo");
room[(size_t) d] = std::max((double) free_b - kBeside - (d == cards - 1 ? head : 0.0), double(1ull << 28));
all_room += room[(size_t) d];
Expand Down Expand Up @@ -2058,8 +2098,12 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
throw std::runtime_error(o.model + " is a '" + name + "' model, which this engine does not run");
};
int cards = 1, devices = 1;
if (!o.layer_split.empty()) ck(cudaGetDeviceCount(&devices), "cudaGetDeviceCount");
cards = devices;
const std::vector<int>& dev_ids = device_ids();
devices = (int) dev_ids.size();
if (dev_ids.empty() && !o.layer_split.empty()) ck(cudaGetDeviceCount(&devices), "cudaGetDeviceCount");
devices = std::max(devices, 1);
if (!dev_ids.empty()) ck(cudaSetDevice(dev_ids[0]), "cudaSetDevice"); // the engine's first device
cards = o.layer_split.empty() ? 1 : devices;
// STRATA_POLY_CARDS=N: a split's N parts on the devices there are, in turn, so that a split is tried on one card
if (const char* v = std::getenv("STRATA_POLY_CARDS"); v && !o.layer_split.empty()) cards = std::max(1, std::atoi(v));
std::vector<int> starts;
Expand Down Expand Up @@ -2109,7 +2153,7 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
e->lo_ = starts[k];
e->end_ = last ? -1 : starts[k + 1];
e->split_ = true;
ck(cudaSetDevice((swap ? 1 - (int) k : (int) k) % devices), "cudaSetDevice");
ck(cudaSetDevice(device_at(swap ? 1 - (int) k : (int) k, devices)), "cudaSetDevice");
try {
e->load(so);
} catch (const std::runtime_error& err) {
Expand All @@ -2120,7 +2164,7 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
else first = std::move(e);
prev = at;
}
ck(cudaSetDevice(0), "cudaSetDevice");
ck(cudaSetDevice(device_at(0, devices)), "cudaSetDevice");
int k = 0;
for (const Engine* e = first.get(); e; e = e->next_.get(), ++k)
std::fprintf(stderr, "strata-poly: layers %d to %d on card %d: %d of %d routed experts in VRAM (%.1f GiB)\n",
Expand Down
Loading