From ce968a2fac1f0357f0d3481d99c846945cca0f1d Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sun, 18 Oct 2026 12:07:42 +0200 Subject: [PATCH] hip: integrated Radeons (gfx1103/gfx1150/gfx1151, UMA flag) take the matrix-core prompt set by default; STRATA_IGPU_DEFAULTS=0 and each switch =0 turn it off; unit test with mock devices --- include/strata/core/arch_defaults.hpp | 20 +++++++-- src/core/arch_defaults.cpp | 55 +++++++++++++++++++++--- src/core/arch_defaults_test.cpp | 60 +++++++++++++++++++++++++++ src/program/generate.cpp | 17 +++++++- 4 files changed, 140 insertions(+), 12 deletions(-) diff --git a/include/strata/core/arch_defaults.hpp b/include/strata/core/arch_defaults.hpp index cd11a0ed..359557fb 100644 --- a/include/strata/core/arch_defaults.hpp +++ b/include/strata/core/arch_defaults.hpp @@ -20,9 +20,21 @@ namespace strata::core { /// "gfx1151:sramecc-:xnack-"): empty for every other architecture. std::vector> arch_default_env(const char* gcn_arch); -/// Sets `arch_default_env(gcn_arch)` in this process's environment where the user has not set the variable, prints one -/// line to stderr when it set anything, and returns the names it set. Call once, before any engine code reads its -/// switches (they are read lazily, at first use). -std::vector apply_arch_defaults(const char* gcn_arch); +/// The integrated Radeons (Phoenix / Hawk Point 780M gfx1103, Strix Point 890M / 880M gfx1150, Strix Halo gfx1151) whose +/// matrix-core prompt set is on by default (153-igpu). Pure architecture-name test; the caller adds the UMA test. +bool igpu_prompt_defaults_arch(const char* gcn_arch); + +/// The matrix-core prompt set an integrated GPU takes by default: STRATA_HIP_WMMA, STRATA_PF_GEMM, STRATA_PA_FAST, +/// STRATA_SELECT_WMMA, STRATA_HC_UPMIX, all "1". Empty unless `integrated` (hipDeviceProp_t::integrated: the device +/// shares the host's memory) AND the architecture is one of `igpu_prompt_defaults_arch`: a discrete card of the same +/// architecture family (RX 7900 XTX gfx1100, R9700 gfx1201, ...) and every CUDA / SYCL build get nothing. These change +/// bits (KL-gated, notes-153-igpu.md); every switch keeps =0 as its off switch and STRATA_IGPU_DEFAULTS=0 turns the set off. +std::vector> igpu_default_env(const char* gcn_arch, bool integrated); + +/// Sets `arch_default_env(gcn_arch)` and, when `integrated`, `igpu_default_env(gcn_arch, true)` in this process's +/// environment where the user has not set the variable, prints one line to stderr per table when it set anything, and +/// returns the names it set. `integrated` is true when every visible GPU is an integrated one. Call once, before any +/// engine code reads its switches (they are read lazily, at first use). +std::vector apply_arch_defaults(const char* gcn_arch, bool integrated = false); } // namespace strata::core diff --git a/src/core/arch_defaults.cpp b/src/core/arch_defaults.cpp index 4cf36daf..7cdf4108 100644 --- a/src/core/arch_defaults.cpp +++ b/src/core/arch_defaults.cpp @@ -59,15 +59,41 @@ std::vector> arch_default_env(const char* gc return t; } -std::vector apply_arch_defaults(const char* gcn_arch) { +bool igpu_prompt_defaults_arch(const char* gcn_arch) { + return strata::kernels::gfx_arch_is(gcn_arch, "gfx1103") || strata::kernels::gfx_arch_is(gcn_arch, "gfx1150") || + strata::kernels::gfx_arch_is(gcn_arch, "gfx1151"); +} + +std::vector> igpu_default_env(const char* gcn_arch, bool integrated) { + std::vector> t; + if (!integrated || !igpu_prompt_defaults_arch(gcn_arch)) return t; + const char* off = std::getenv("STRATA_IGPU_DEFAULTS"); + if (off != nullptr && off[0] == '0') return t; + // Measured on a Radeon 780M (gfx1103, P2006, Q2_0, notes-153-igpu.md / notes-148-amd.md): prompts 109.5 -> 214.7 tok/s + // with the first four, 233 with HC_UPMIX, decode unchanged. Dense projections are 63% of a prompt's GPU time there. + t = { + {"STRATA_HIP_WMMA", "1"}, // the prompt attention on matrix cores + {"STRATA_PF_GEMM", "1"}, // the prompt's FP16 projections on the 128 x 256 WMMA GEMM + {"STRATA_PA_FAST", "1"}, // the prompt attention with single FP16 q and p + {"STRATA_SELECT_WMMA", "1"}, // the block scorer on matrix cores + {"STRATA_HC_UPMIX", "1"}, // the hyper-connection up projection with its epilogue + }; + return t; +} + +static void set_default(const std::pair& kv) { +#if defined(_WIN32) + _putenv_s(kv.first.c_str(), kv.second.c_str()); +#else + setenv(kv.first.c_str(), kv.second.c_str(), 0); +#endif +} + +std::vector apply_arch_defaults(const char* gcn_arch, bool integrated) { std::vector set; for (const auto& kv : arch_default_env(gcn_arch)) { if (std::getenv(kv.first.c_str()) != nullptr) continue; // the user's setting wins, whatever it is -#if defined(_WIN32) - _putenv_s(kv.first.c_str(), kv.second.c_str()); -#else - setenv(kv.first.c_str(), kv.second.c_str(), 0); -#endif + set_default(kv); set.push_back(kv.first); } if (!set.empty()) { @@ -79,6 +105,23 @@ std::vector apply_arch_defaults(const char* gcn_arch) { for (size_t i = 0; i < set.size(); ++i) std::fprintf(stderr, "%s%s", i ? " " : "", set[i].c_str() + 7); std::fprintf(stderr, "\n"); } + std::vector igpu; + for (const auto& kv : igpu_default_env(gcn_arch, integrated)) { + if (std::getenv(kv.first.c_str()) != nullptr) continue; + set_default(kv); + igpu.push_back(kv.first); + } + if (!igpu.empty()) { + std::fprintf(stderr, "strata: integrated GPU (%s): the matrix-core prompt set is on by default (about 2x faster prompts on a " + "Radeon 780M, the answers differ in the last bits): ", gcn_arch); + for (size_t i = 0; i < igpu.size(); ++i) std::fprintf(stderr, "%s%s", i ? " " : "", igpu[i].c_str() + 7); + std::fprintf(stderr, ". Turn one off with NAME=0 (for example STRATA_HC_UPMIX=0), all of them with STRATA_IGPU_DEFAULTS=0"); +#if defined(_WIN32) + std::fprintf(stderr, ". Not yet tested on Windows"); +#endif + std::fprintf(stderr, "\n"); + set.insert(set.end(), igpu.begin(), igpu.end()); + } return set; } diff --git a/src/core/arch_defaults_test.cpp b/src/core/arch_defaults_test.cpp index 87f6246b..8bf9adc9 100644 --- a/src/core/arch_defaults_test.cpp +++ b/src/core/arch_defaults_test.cpp @@ -79,6 +79,66 @@ int main() { #else CHECK(kGfx12PromptDefaultsOn); #endif + // 153-igpu: the matrix-core prompt set of the integrated Radeons, chosen by the UMA flag AND the architecture. + // Mock device properties: {gcnArchName, integrated}. + struct Mock { const char* name; const char* gcn; bool integrated; bool expect_on; }; + const Mock mocks[] = { + {"Radeon 780M (Phoenix, UMA)", "gfx1103", true, true}, + {"Radeon 780M with feature suffix", "gfx1103:sramecc-:xnack-", true, true}, + {"Radeon 890M (Strix Point, UMA)", "gfx1150", true, true}, + {"Radeon 8060S (Strix Halo, UMA)", "gfx1151", true, true}, + {"RX 7900 XTX (dGPU)", "gfx1100", false, false}, + {"RX 7800 XT (dGPU)", "gfx1101", false, false}, + {"R9700 (dGPU)", "gfx1201", false, false}, + {"RX 9060 (dGPU)", "gfx1200", false, false}, + {"RX 6800 (dGPU)", "gfx1030", false, false}, + {"a gfx1103 reported as a discrete card", "gfx1103", false, false}, + {"a gfx1151 reported as a discrete card", "gfx1151", false, false}, + {"Radeon 860M (Krackan, no kernels)", "gfx1152", true, false}, + {"older iGPU (gfx1036, no kernels)", "gfx1036", true, false}, + {"an integrated gfx1201 would not get it", "gfx1201", true, false}, + {"CUDA (no gcn name)", "", true, false}, + }; + unset("STRATA_IGPU_DEFAULTS"); + for (const Mock& m : mocks) { + const auto t = igpu_default_env(m.gcn, m.integrated); + CHECK(t.empty() == !m.expect_on); + if (m.expect_on) { + CHECK(t.size() == 5); + const char* want[] = {"STRATA_HIP_WMMA", "STRATA_PF_GEMM", "STRATA_PA_FAST", "STRATA_SELECT_WMMA", "STRATA_HC_UPMIX"}; + for (const char* w : want) { + bool found = false; + for (const auto& kv : t) found = found || (kv.first == w && kv.second == "1"); + CHECK(found); + } + for (const auto& kv : t) CHECK(kv.first != "STRATA_PF_FUSED" && kv.first != "STRATA_HC_Q8" && kv.first != "STRATA_PF_HCDOWN"); + } + if (!m.expect_on) std::printf(" (not on) %s\n", m.name); + } + CHECK(igpu_default_env(nullptr, true).empty()); + CHECK(igpu_prompt_defaults_arch("gfx1103") && igpu_prompt_defaults_arch("gfx1150") && igpu_prompt_defaults_arch("gfx1151")); + CHECK(!igpu_prompt_defaults_arch("gfx11030") && !igpu_prompt_defaults_arch("gfx1100") && !igpu_prompt_defaults_arch("gfx1201")); + // the exact tables are untouched: gfx1103 / gfx1150 stay out of arch_default_env, gfx1151 keeps no bit-changing switch + CHECK(arch_default_env("gfx1103").empty()); + // the opt-out of the whole set + put("STRATA_IGPU_DEFAULTS", "0"); + CHECK(igpu_default_env("gfx1103", true).empty()); + unset("STRATA_IGPU_DEFAULTS"); + // apply: a discrete card sets nothing, an iGPU sets the five, a switch the user set (even to 0) is kept + const char* five[] = {"STRATA_HIP_WMMA", "STRATA_PF_GEMM", "STRATA_PA_FAST", "STRATA_SELECT_WMMA", "STRATA_HC_UPMIX"}; + for (const char* k : five) unset(k); + CHECK(apply_arch_defaults("gfx1100", false).empty()); + CHECK(apply_arch_defaults("gfx1201", false).empty()); + CHECK(apply_arch_defaults("gfx1103", false).empty()); // integrated == false: a mixed box (iGPU + dGPU) keeps the old defaults + for (const char* k : five) CHECK(std::getenv(k) == nullptr); + put("STRATA_HC_UPMIX", "0"); + const auto on = apply_arch_defaults("gfx1103", true); + CHECK(on.size() == 4); + for (const char* k : {"STRATA_HIP_WMMA", "STRATA_PF_GEMM", "STRATA_PA_FAST", "STRATA_SELECT_WMMA"}) + CHECK(std::getenv(k) != nullptr && std::string(std::getenv(k)) == "1"); + CHECK(std::getenv("STRATA_HC_UPMIX") != nullptr && std::string(std::getenv("STRATA_HC_UPMIX")) == "0"); + for (const char* k : five) unset(k); + // both platforms: no Windows guard on the iGPU set (the owner wants it there too; untested on Windows is only said in the log) std::printf(fails ? "arch_defaults_test: %d FAILED\n" : "arch_defaults_test: OK\n", fails); return fails ? 1 : 0; } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 01b99f06..2c0589f4 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2671,8 +2671,21 @@ int main(int argc, char** argv) { { // gfx1151 (Strix Halo): the switches that are exact there are on by default (strata/core/arch_defaults.hpp); before // any engine code reads its switches (they are read at first use), and the user's own settings are kept cudaDeviceProp ad{}; - if (cudaGetDeviceProperties(&ad, 0) == cudaSuccess) strata::core::apply_arch_defaults(ad.gcnArchName); - else (void) cudaGetLastError(); + if (cudaGetDeviceProperties(&ad, 0) == cudaSuccess) { + // an integrated Radeon (UMA / APU: the device property, not the name): the matrix-core prompt set is on by + // default. Only when EVERY visible GPU is integrated and of the same architecture, so a box that also holds a + // discrete card (whichever the engine ends up using) keeps the old defaults. + bool all_igpu = ad.integrated != 0; + int ndev = 0; + if (all_igpu && cudaGetDeviceCount(&ndev) == cudaSuccess) { + for (int d = 1; d < ndev && d < 64; ++d) { + cudaDeviceProp od{}; + if (cudaGetDeviceProperties(&od, d) != cudaSuccess || od.integrated == 0 || + std::strncmp(od.gcnArchName, ad.gcnArchName, 7) != 0) { all_igpu = false; (void) cudaGetLastError(); break; } + } + } + strata::core::apply_arch_defaults(ad.gcnArchName, all_igpu); + } else (void) cudaGetLastError(); } #endif #if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL)