mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 15:58:53 +00:00
Merge f-strix-p1a: P1A prompt experts for Windows gfx1151, on by default there
This commit is contained in:
commit
4786a2e7d8
13 files changed
+1011
-215
No files matched your search
@@ -299,6 +299,7 @@ macro(strata_cuda_pf_own)
|
||||
endif()
|
||||
if(STRATA_HIP_GFX906) # (strata_hip_gfx906_sources adds these two to strata_pf_own only with MMQ)
|
||||
target_sources(strata_pf_own PRIVATE src/prefill/moe_f16_kq.cu)
|
||||
target_sources(strata_pf_own PRIVATE src/prefill/moe_p1a.cu)
|
||||
target_compile_definitions(strata_pf_own PRIVATE GGML_USE_HIP)
|
||||
endif()
|
||||
target_link_libraries(strata_prefill PUBLIC strata_pf_own)
|
||||
|
||||
@@ -50,6 +50,7 @@ macro(strata_hip_mmq)
|
||||
src/prefill/moe_fused_iq.cu
|
||||
src/prefill/moe_fused_iq_q36.cu # the same kernels again for Qwen3.6's expert shape (gfx12: strata-q36's fused prompt experts)
|
||||
src/prefill/moe_f16_kq.cu
|
||||
src/prefill/moe_p1a.cu
|
||||
src/prefill/moe_helpers.cu) # the gathers, SwiGLU passes and row table (cut from moe_mmq.cu)
|
||||
if(STRATA_MMQ_KQUANTS)
|
||||
# the same K-quant instances as the CUDA branch: on HIP they serve the dense GGUF projections
|
||||
@@ -107,6 +108,7 @@ macro(strata_hip_pf_own)
|
||||
src/prefill/moe_fused_iq.cu
|
||||
src/prefill/moe_fused_iq_q36.cu
|
||||
src/prefill/moe_f16_kq.cu
|
||||
src/prefill/moe_p1a.cu
|
||||
src/prefill/moe_helpers.cu)
|
||||
set_source_files_properties(${_strata_pf_own_sources} PROPERTIES LANGUAGE HIP INCLUDE_DIRECTORIES "${STRATA_HIP_COMPAT_INCLUDE_DIR}")
|
||||
add_library(strata_pf_own STATIC ${_strata_pf_own_sources} src/prefill/mmq_absent.cpp)
|
||||
@@ -233,6 +235,7 @@ if(STRATA_HIP_GFX906)
|
||||
list(APPEND _strata_cu ${_strata_tq_hip})
|
||||
endif()
|
||||
target_sources(strata_pf_own PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/prefill/moe_f16_kq.cu) # (Strata's own: beside moe_fused_iq.cu)
|
||||
target_sources(strata_pf_own PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/prefill/moe_p1a.cu) # (the same: moe_fused_iq.cu calls it)
|
||||
endif()
|
||||
set_source_files_properties(${_strata_cu} PROPERTIES LANGUAGE HIP)
|
||||
endif()
|
||||
|
||||
+280
-207
File diff suppressed because it is too large.
Load diff
+2
-1
@@ -10,7 +10,7 @@ debugging and are kept in `data/settings-register.json`.
|
||||
A number or a word is set like any environment variable, for example `STRATA_KVA=1`. In the Terminal, `strata launch --set NAME=value`
|
||||
puts it in the model's run configuration.
|
||||
|
||||
## Switches you can turn on (271)
|
||||
## Switches you can turn on (272)
|
||||
|
||||
Off unless you set them. The last column says where each one is described.
|
||||
|
||||
@@ -184,6 +184,7 @@ Off unless you set them. The last column says where each one is described.
|
||||
| `STRATA_PF_GROUP_SCRATCH` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md) |
|
||||
| `STRATA_PF_HC_REGION` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md) |
|
||||
| `STRATA_PF_I8` | on / off | Intel Arc | [INTEL_INT8_EXPERTS.md](INTEL_INT8_EXPERTS.md) |
|
||||
| `STRATA_PF_P1A_L2` | on / off | AMD | - |
|
||||
| `STRATA_PF_PEER_CPU` | number | NVIDIA and AMD | - |
|
||||
| `STRATA_PF_SWITCH_MIN_T` | number | NVIDIA and AMD, Intel Arc | [AMD_HIP.md](AMD_HIP.md), [STRIX_HALO.md](STRIX_HALO.md) |
|
||||
| `STRATA_PF_WMMA` | on / off | NVIDIA and AMD | [NVIDIA_V100.md](NVIDIA_V100.md) |
|
||||
|
||||
@@ -46,6 +46,15 @@ void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n
|
||||
/// down runs on an F16 kernel; dm_f16_used() says which form the last call wrote (false on every other path), for moe_combine16.
|
||||
void want_dm_f16(bool on);
|
||||
bool dm_f16_used();
|
||||
/// STRATA_PF_P1A=1 on gfx1151 (HIP; a default on Windows gfx1151, false everywhere else): the K-quant layers may take moe_p1a.cu's kernels.
|
||||
bool p1a_wanted();
|
||||
/// For the next experts_native calls (when p1a_wanted()): the layer input as FP16 rows [token][n_embd] (nullptr: none) and the
|
||||
/// bytes of `ha`. A call takes the P1A kernels only with an input, want_dm_f16(true) and room for FP16 H; p1a_used() says whether
|
||||
/// the last call did (it wrote FP16 down rows then, dm_f16_used() is true).
|
||||
void p1a_input(const void* x16, size_t h_bytes);
|
||||
bool p1a_used();
|
||||
/// p1a_wanted() and a layer of this gate/up + down type pair would take the P1A kernels (the buffers grow only for such a pack).
|
||||
bool p1a_takes_pair(int gu_type, int d_type);
|
||||
/// (CUDA, STRATA_PF_FUSED_TQ=1) a Hadamard-folded down for the next experts_native calls of a trellis pair: the gate/up writes H as FP32
|
||||
/// into the first n_ff floats of each row's own down-output row in `dm` (no other buffer), which is rotated there (y = H_128 (signs * h) /
|
||||
/// sqrt(128) per 128 features; signs nullptr = identity, else n_ff values on the device) and quantized into `ha` before the down product
|
||||
|
||||
@@ -205,6 +205,11 @@ std::vector<std::pair<std::string, std::string>> igpu_default_env(const char* gc
|
||||
// interleaved 8K pairs, same engine: IQ3_S x1.022 (4 of 4 pairs), UD-IQ4_XS x1.026 (3 of 3), UD-Q4_K_XL x1.012 (3 of 4); the first-token KL
|
||||
// is 1e-4 at 8K and 0.02-0.04 on agent transcripts (information). STRATA_PF_DM16=0 turns it off.
|
||||
{"STRATA_PF_DM16", "1"},
|
||||
// The Q4_K gate/up + Q5_1 down layers (43 of 48 in UD-Q4_K_XL) on the F16 WMMA kernels of src/prefill/moe_p1a.cu; other layers
|
||||
// and packs keep the kernels above. Windows gfx1151, UD-Q4_K_XL, 5 interleaved pairs: prompt x1.046 (4K), x1.043 (8K),
|
||||
// x1.043 (32K), decode unchanged (job strix-20261114-1347-9a28). KL to the FP16 truth, 24 prompts a size: on 0.0112 / 0.0116
|
||||
// against off 0.0179 / 0.0118 at 4K / 8K (job strix-20261114-1347-238b). STRATA_PF_P1A=0 turns it off.
|
||||
{"STRATA_PF_P1A", "1"},
|
||||
};
|
||||
}
|
||||
return t;
|
||||
|
||||
@@ -268,7 +268,7 @@ int main() {
|
||||
// 0.2.8: Strix Halo (gfx1151, integrated) gets the matrix-core prompt set on Windows; the 780M and the 890M are
|
||||
// not measured there and stay off
|
||||
const bool win_on = m.expect_on && m.integrated && std::string(m.gcn).rfind("gfx1151", 0) == 0;
|
||||
CHECK(igpu_default_env(m.gcn, m.integrated).size() == (win_on ? 8u : 0u));
|
||||
CHECK(igpu_default_env(m.gcn, m.integrated).size() == (win_on ? 9u : 0u));
|
||||
#else
|
||||
CHECK(igpu_default_env(m.gcn, m.integrated).size() == (want ? 5u : 0u));
|
||||
#endif
|
||||
@@ -304,10 +304,10 @@ int main() {
|
||||
#if defined(_WIN32)
|
||||
halo_win = std::string(m.gcn).rfind("gfx1151", 0) == 0;
|
||||
#endif
|
||||
CHECK(t.size() == (halo_win ? 8u : 5u));
|
||||
CHECK(t.size() == (halo_win ? 9u : 5u));
|
||||
std::vector<const char*> want = {"STRATA_HIP_WMMA", "STRATA_PF_GEMM", "STRATA_PA_FAST", "STRATA_HC_UPMIX"};
|
||||
want.push_back("STRATA_SELECT_WMMA");
|
||||
if (halo_win) { want.push_back("STRATA_PF_FUSED"); want.push_back("STRATA_PF_F16"); want.push_back("STRATA_PF_DM16"); }
|
||||
if (halo_win) { want.push_back("STRATA_PF_FUSED"); want.push_back("STRATA_PF_F16"); want.push_back("STRATA_PF_DM16"); want.push_back("STRATA_PF_P1A"); }
|
||||
for (const char* w : want) {
|
||||
bool found = false;
|
||||
for (const auto& kv : t) found = found || (kv.first == w && kv.second == "1");
|
||||
@@ -373,20 +373,20 @@ int main() {
|
||||
CHECK(!igpu_prompt_set_enabled("gfx1103", true));
|
||||
{
|
||||
const auto t = igpu_default_env("gfx1151", true);
|
||||
CHECK(t.size() == 8);
|
||||
CHECK(t.size() == 9);
|
||||
auto has = [&t](const char* k) {
|
||||
for (const auto& kv : t) if (kv.first == k) return kv.second == "1";
|
||||
return false;
|
||||
};
|
||||
CHECK(has("STRATA_HIP_WMMA") && has("STRATA_PF_GEMM") && has("STRATA_PA_FAST") && has("STRATA_HC_UPMIX") &&
|
||||
has("STRATA_PF_FUSED") && has("STRATA_SELECT_WMMA") && has("STRATA_PF_F16") && has("STRATA_PF_DM16"));
|
||||
has("STRATA_PF_FUSED") && has("STRATA_SELECT_WMMA") && has("STRATA_PF_F16") && has("STRATA_PF_DM16") && has("STRATA_PF_P1A"));
|
||||
CHECK(igpu_default_env("gfx1151", false).empty()); // a gfx1151 reported as a discrete card
|
||||
CHECK(igpu_default_env("gfx1150", true).empty()); // 890M: unchanged
|
||||
CHECK(igpu_default_env("gfx1103", true).empty()); // 780M on Windows: unchanged
|
||||
}
|
||||
{
|
||||
const char* halo[] = {"STRATA_HIP_WMMA", "STRATA_PF_GEMM", "STRATA_PA_FAST", "STRATA_HC_UPMIX", "STRATA_PF_FUSED",
|
||||
"STRATA_SELECT_WMMA", "STRATA_PF_F16", "STRATA_PF_DM16"};
|
||||
"STRATA_SELECT_WMMA", "STRATA_PF_F16", "STRATA_PF_DM16", "STRATA_PF_P1A"};
|
||||
for (const char* k : halo) unset(k);
|
||||
const auto set = apply_arch_defaults("gfx1151", true);
|
||||
for (const char* k : halo) {
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#if defined(__HIPCC__)
|
||||
#include "moe_f16_common.cuh"
|
||||
#include "moe_f16_kq.hpp"
|
||||
#include "moe_p1a.hpp"
|
||||
#endif
|
||||
|
||||
#include <cuda_fp16.h>
|
||||
@@ -3024,6 +3025,49 @@ static bool g_dm16_want = false, g_dm16_used = false;
|
||||
void want_dm_f16(bool on) { g_dm16_want = on; }
|
||||
bool dm_f16_used() { return g_dm16_used; }
|
||||
|
||||
// STRATA_PF_P1A=1 (gfx1151, HIP; a default on Windows gfx1151, arch_defaults.cpp): the Q4_K gate/up + Q5_1 down layers on moe_p1a.cu's kernels (F16 activations from the
|
||||
// layer's FP16 input rows, F16 H, FP16 down rows); STRATA_PF_P1A_L2=1 adds the Q5_K gate/up and Q8_0 down layers. Taken per call only
|
||||
// when the prompt path handed over the FP16 input (p1a_input), the down rows go out as FP16 (want_dm_f16) and H has room for FP16 rows;
|
||||
// every other call, and every call with the switch off, runs as before.
|
||||
#if defined(__HIPCC__)
|
||||
static const void* g_p1a_x16 = nullptr;
|
||||
static size_t g_p1a_h_bytes = 0;
|
||||
#endif
|
||||
static bool g_p1a_used = false;
|
||||
bool p1a_wanted() {
|
||||
#if defined(__HIPCC__)
|
||||
static const bool on = [] {
|
||||
const char* v = std::getenv("STRATA_PF_P1A");
|
||||
return v != nullptr && v[0] == '1' && v[1] == '\0' && dev_info().g1151;
|
||||
}();
|
||||
return on;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
[[maybe_unused]] static bool p1a_l2() {
|
||||
#if defined(__HIPCC__)
|
||||
static const bool on = [] { const char* v = std::getenv("STRATA_PF_P1A_L2"); return v != nullptr && v[0] == '1' && v[1] == '\0'; }();
|
||||
return on;
|
||||
#else
|
||||
return false; // (the P1A kernels run on gfx1151 only: p1a_wanted)
|
||||
#endif
|
||||
}
|
||||
void p1a_input(const void* x16, size_t h_bytes) {
|
||||
#if defined(__HIPCC__)
|
||||
g_p1a_x16 = p1a_wanted() ? x16 : nullptr;
|
||||
g_p1a_h_bytes = g_p1a_x16 != nullptr ? h_bytes : 0;
|
||||
#else
|
||||
(void) x16; (void) h_bytes; // (g_p1a_x16 / g_p1a_h_bytes are read in the HIP-only P1A launch only)
|
||||
#endif
|
||||
}
|
||||
bool p1a_used() { return g_p1a_used; }
|
||||
#if defined(__HIPCC__)
|
||||
bool p1a_takes_pair(int gu_type, int d_type) { return p1a_wanted() && p1a::takes(gu_type, d_type, p1a_l2()); }
|
||||
#else
|
||||
bool p1a_takes_pair(int, int) { return false; } // (namespace p1a, moe_p1a.hpp, exists in HIP builds only)
|
||||
#endif
|
||||
|
||||
// (CUDA, STRATA_PF_FUSED_TQ=1) a Hadamard-folded down (set_down_rotation): on, and the down input's sign vector
|
||||
static const float* g_rot_signs = nullptr;
|
||||
static bool g_rot_on = false;
|
||||
@@ -3039,6 +3083,7 @@ void hadamard128_rows(float* x, int64_t rows, int64_t cols, const float* signs,
|
||||
void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n, const void* scratch, const void* xa,
|
||||
const int32_t* src, void* ha, float* dm, void* stream) {
|
||||
g_dm16_used = false;
|
||||
g_p1a_used = false;
|
||||
if (b.e1 <= b.e0 || n <= 0) return;
|
||||
const DevInfo& d = dev_info();
|
||||
#if !defined(__HIPCC__)
|
||||
@@ -3055,6 +3100,16 @@ void experts_native(const Batch& b, const NativeGeom& g, int n_expert, int64_t n
|
||||
#if defined(__HIPCC__)
|
||||
{
|
||||
const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0);
|
||||
// STRATA_PF_P1A=1 (gfx1151; p1a_input set only then): gate/up and down on moe_p1a.cu's kernels, H as FP16 in `ha`, the down rows as FP16 in `dm`
|
||||
if (g_p1a_x16 != nullptr && g_dm16_want && !g_rot_on && d.g1151 && p1a::takes(g.gu_type, g.d_type, p1a_l2()) && tiles <= 65535 &&
|
||||
(size_t) n * kMoeFF * 2 <= g_p1a_h_bytes) {
|
||||
p1a::gate_up(b, g, tb.ts, tb.tiles, tb.off, tiles, g_p1a_x16, src, ha, s);
|
||||
p1a::down(b, g, tb.ts, tb.tiles, tb.off, tiles, ha, dm, s);
|
||||
g_dm16_used = true;
|
||||
g_p1a_used = true;
|
||||
ck(cudaGetLastError(), "experts_native (P1A)");
|
||||
return;
|
||||
}
|
||||
// W (X1): LDS alias of the H tile + waves_per_eu(8) (192 VGPRs) + 4 blocks per WGP, only for the types whose
|
||||
// kernels do not spill under the cap (measured: gate/up IQ2_*, IQ3_*, IQ4_XS 0-5 regs; down IQ4_NL 11, Q8_0 / Q2_0 0;
|
||||
// Q4_K / Q5_K gate/up and Q5_1 down spill 100+ and keep the old launch).
|
||||
|
||||
@@ -58,6 +58,12 @@ bool covers_down(int) { return false; }
|
||||
void gate_up(const Batch&, const NativeGeom&, const int32_t*, const int2*, const int32_t*, int64_t, int, int64_t, const uint8_t*, const int32_t*, uint8_t*, cudaStream_t) {}
|
||||
void down(const Batch&, const NativeGeom&, const int32_t*, const int2*, const int32_t*, int64_t, int, int64_t, const uint8_t*, float*, cudaStream_t, bool) {}
|
||||
} // namespace strata::prefill::fused_q36::f16kq
|
||||
// moe_p1a.cu's kernels likewise: the original copy's only (this copy's p1a_input is never called, so its experts_native never reaches these)
|
||||
namespace strata::prefill::fused_q36::p1a {
|
||||
bool takes(int, int, bool) { return false; }
|
||||
void gate_up(const Batch&, const NativeGeom&, const int32_t*, const int2*, const int32_t*, int64_t, const void*, const int32_t*, void*, cudaStream_t) {}
|
||||
void down(const Batch&, const NativeGeom&, const int32_t*, const int2*, const int32_t*, int64_t, const void*, void*, cudaStream_t) {}
|
||||
} // namespace strata::prefill::fused_q36::p1a
|
||||
#endif
|
||||
|
||||
namespace strata::prefill::fused {
|
||||
|
||||
@@ -0,0 +1,531 @@
|
||||
// src/prefill/moe_p1a.cu - see moe_p1a.hpp. gfx11 device code; the engine takes it on gfx1151 only, behind STRATA_PF_P1A=1.
|
||||
//
|
||||
// The routed experts' prompt pass on the F16 matrix cores with F16 activations and H, the weight codes in registers
|
||||
// (gufo's RoutedPairF16Kernel / RoutedDownF16Kernel), on fused::group's tables and the native blobs:
|
||||
// gate/up (Q4_K, Q5_K) + SwiGLU -> H as F16 rows [routed row][n_ff]
|
||||
// down (Q5_1, Q8_0) of that H -> the routed rows' outputs as F16 [routed row][n_embd] (what moe_combine16 reads)
|
||||
// What differs from moe_f16_kq.cu (gufo's older RoutedF16GEMMKernel): the activations are the FP16 rows of the layer's input
|
||||
// (mixed_h), not int8 per 32; H is F16, not int8; each lane decodes only the K16 half of its row that the WMMA reads from it
|
||||
// and v_permlanex16 hands over the other half; the codes stay in registers and the activations are the only LDS stage;
|
||||
// a work item is 128 rows of one expert (two of fused::group's 64-row tiles). The kernels are the harness's
|
||||
// (tools/hip/strix_bench/k_moe_p1a.cu) line for line except the work-item decode and the blob / geometry arguments;
|
||||
// MD / WPE / FL are the harness's knobs, the engine instantiates MD 0, WPE 0.
|
||||
//
|
||||
// Adapted from gufo 07e1191f36885c144b34566aac8eaa9777d8fa78: src/models/qwen38_flash_next/kernels/rocm/kernels.hip.cpp
|
||||
// (RoutedPairF16Kernel, RoutedDownF16Kernel, CodesToHalves, SwapHalves, GatherBit, SpreadHighBits).
|
||||
// MIT License
|
||||
// Copyright (c) 2026 gufo contributors
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
#include "moe_p1a.hpp"
|
||||
|
||||
#include <hip/hip_fp16.h>
|
||||
|
||||
#include "ggml.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace strata::prefill::fused::p1a {
|
||||
namespace {
|
||||
|
||||
#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1150__) || defined(__gfx1151__)
|
||||
#define P1A_DEVICE 1
|
||||
#else
|
||||
#define P1A_DEVICE 0
|
||||
#endif
|
||||
|
||||
using v16h = __attribute__((__vector_size__(16 * sizeof(_Float16)))) _Float16;
|
||||
using v8f = __attribute__((__vector_size__(8 * sizeof(float)))) float;
|
||||
using v8u = __attribute__((__vector_size__(32))) uint32_t;
|
||||
|
||||
constexpr uint32_t kHalfMagic = 0x64646464U; // the high bytes of 1024.0 as F16
|
||||
constexpr int kQ4K = GGML_TYPE_Q4_K, kQ5K = GGML_TYPE_Q5_K, kQ51 = GGML_TYPE_Q5_1, kQ80 = GGML_TYPE_Q8_0;
|
||||
|
||||
|
||||
#if P1A_DEVICE
|
||||
__device__ __forceinline__ v8f wmma(v16h a, v16h b, v8f c) { return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, c); }
|
||||
__device__ __forceinline__ uint32_t gather_bit(uint32_t w, int s) { return (((w >> s) & 0x01010101U) * 0x01020408U) >> 24U; }
|
||||
__device__ __forceinline__ uint32_t spread_high_bits(uint32_t bits) { return (__umul24(bits, 0x00204081U) & 0x01010101U) << 4U; }
|
||||
// lane i receives lane i ^ 16's value
|
||||
__device__ __forceinline__ uint32_t swap_halves(uint32_t v) {
|
||||
uint32_t old;
|
||||
asm("" : "=v"(old));
|
||||
return (uint32_t) __builtin_amdgcn_permlanex16((int) old, (int) v, 0x76543210, 0xfedcba98, false, false);
|
||||
}
|
||||
__device__ __forceinline__ void codes_to_halves(uint32_t codes, __half2 magic, __half2 scale2, __half2 bias2, __half2& lo, __half2& hi) {
|
||||
const uint32_t p0 = __builtin_amdgcn_perm(codes, kHalfMagic, 0x01050004U);
|
||||
const uint32_t p1 = __builtin_amdgcn_perm(codes, kHalfMagic, 0x03070206U);
|
||||
lo = __hfma2(__hadd2(__builtin_bit_cast(__half2, p0), magic), scale2, bias2);
|
||||
hi = __hfma2(__hadd2(__builtin_bit_cast(__half2, p1), magic), scale2, bias2);
|
||||
}
|
||||
#endif
|
||||
|
||||
// rows of expert e: [off[e], off[e + 1]); token of row r: src[r]; the outputs go to row r.
|
||||
// MD (--set p1a_mode=N, timing only, the output is wrong): 0 the kernel; 1 no WMMA (a cheap op consumes A and B); 2 no dequant (A = the raw
|
||||
// codes); 3 no global activation loads (zeros staged); 5 no global weight loads (codes from registers).
|
||||
// WPE (--set p1a_wpe=N): the compiler's waves-per-SIMD floor (VGPR cap 1536 / N); 0 = the compiler's choice.
|
||||
// FL (bit 0 --set p1a_il=1, bit 1 --set p1a_db=1), both kernels, MD 0 only:
|
||||
// IL: token tiles in pairs j, j + 1: both B fragments read, then lo j, lo j+1, hi j, hi j+1 (two independent accumulators back to back
|
||||
// instead of the dependent lo -> hi pair on one); an odd last live tile also computes its (zero, unused) neighbour.
|
||||
// DB: two activation stage buffers in LDS, one barrier per stage instead of two (the stage written next was last read two stages ago,
|
||||
// behind the barrier after this stage's commit). No +1 padding: column XOR (row << 2) instead, so 2 x 16 KB = 32 KB a block.
|
||||
template <int KS, bool DB>
|
||||
__device__ __forceinline__ int act_slot(int r, int c) { return r * KS + (DB ? (c ^ ((r & 7) << 2)) : c); }
|
||||
template <int WT, int MD = 0, int WPE = 0, int FL = 0>
|
||||
__launch_bounds__(256) __attribute__((amdgpu_waves_per_eu(WPE > 0 ? WPE : 1))) __global__ void pair_kernel(Batch b, NativeGeom geo, const int32_t* __restrict__ ts, const int2* __restrict__ tiles, const __half* __restrict__ x,
|
||||
const int32_t* __restrict__ off, const int32_t* __restrict__ src, __half* __restrict__ out,
|
||||
int m, int k) {
|
||||
#if P1A_DEVICE
|
||||
constexpr int BN = 128, BK = 2, kTokTiles = BN / 16, kRows = 64;
|
||||
constexpr bool IL = (FL & 1) != 0 && MD == 0, DB = (FL & 2) != 0;
|
||||
constexpr bool Q5K = WT == kQ5K;
|
||||
constexpr int kBlockBytes = Q5K ? 176 : 144;
|
||||
constexpr int kActStride = DB ? BN : BN + 1, kActBuf = BK * 4 * kActStride, kActBytes = (DB ? 2 : 1) * kActBuf * 16;
|
||||
constexpr int kPairStride = kRows + 2, kEpilogueBytes = BN * kPairStride * 2;
|
||||
constexpr int kLdsBytes = kActBytes > kEpilogueBytes ? kActBytes : kEpilogueBytes;
|
||||
__shared__ __attribute__((aligned(16))) uint8_t lds[kLdsBytes];
|
||||
auto* s_act = reinterpret_cast<uint4*>(lds);
|
||||
|
||||
// the work item: fused::group's 64-row tile y of this batch and the next one of the same expert (a 128-row item);
|
||||
// the second tile of a pair has no block of its own
|
||||
const int t0 = ts[b.e0], w = (int) blockIdx.y;
|
||||
if (w >= ts[b.e1] - t0) return;
|
||||
const int2 tl = tiles[t0 + w];
|
||||
const int expert = tl.x;
|
||||
if ((t0 + w - ts[expert]) & 1) return;
|
||||
const int t_local = 0;
|
||||
const int bucket_begin = tl.y;
|
||||
const int bucket_rows = min(BN, off[expert + 1] - bucket_begin);
|
||||
const int live_tok_tiles = min(kTokTiles, (bucket_rows - t_local + 15) / 16);
|
||||
const int num_sb = k / 256;
|
||||
|
||||
const int tid = (int) threadIdx.x, wave_id = tid >> 5, lane_id = tid & 31, sub_lane = lane_id & 15, half_id = lane_id >> 4;
|
||||
const int r_block = (int) blockIdx.x * kRows;
|
||||
const int row = r_block + wave_id * 8 + (sub_lane % 8); // tile rows 0-7 gate, 8-15 the matching up rows
|
||||
const bool row_live = row < m;
|
||||
const int f_c = half_id ^ (sub_lane & 1);
|
||||
const uint8_t* w_row = b.blob[expert - b.e0] + (sub_lane < 8 ? 0 : geo.up_off) + (size_t) (row_live ? row : m - 1) * geo.gu_row;
|
||||
|
||||
constexpr int kActFetch = BN * BK * 4 / 256;
|
||||
const __half* a_src[kActFetch];
|
||||
int a_slot[kActFetch];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kActFetch; ++i) {
|
||||
const int chunk = tid + i * 256, t = chunk / (BK * 4), sub = chunk % (BK * 4), c_row = t_local + t;
|
||||
const int32_t s = c_row < bucket_rows ? src[bucket_begin + c_row] : -1;
|
||||
a_src[i] = s >= 0 && MD != 3 ? x + (size_t) s * k + sub * 8 : nullptr;
|
||||
a_slot[i] = act_slot<kActStride, DB>(sub, t);
|
||||
}
|
||||
uint4 raw_header, raw_codes[4], raw_qh[2];
|
||||
uint4 header = make_uint4(0u, 0u, 0u, 0u), codes[4], qh[2];
|
||||
uint4 a_data[kActFetch];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kActFetch; ++i) a_data[i] = make_uint4(0u, 0u, 0u, 0u);
|
||||
|
||||
const auto fetch_stage = [&]<int G>(int sb) {
|
||||
if constexpr (G == 0 && MD == 5) {
|
||||
raw_header = make_uint4(0x3C003C00U ^ (uint32_t) sb, (uint32_t) tid, (uint32_t) sb * 7U, (uint32_t) row);
|
||||
if constexpr (Q5K) { raw_qh[0] = make_uint4((uint32_t) sb, 1U, 2U, 3U); raw_qh[1] = make_uint4((uint32_t) tid, 5U, 6U, 7U); }
|
||||
#pragma unroll
|
||||
for (int g = 0; g < 4; ++g) raw_codes[g] = make_uint4((uint32_t) (sb * g), (uint32_t) tid, (uint32_t) g, (uint32_t) (sb ^ tid));
|
||||
} else if constexpr (G == 0) {
|
||||
const auto* chunks = reinterpret_cast<const uint4*>(w_row + sb * kBlockBytes);
|
||||
constexpr int kCodeChunk = Q5K ? 3 : 1;
|
||||
raw_header = chunks[0];
|
||||
if constexpr (Q5K) { raw_qh[0] = chunks[1]; raw_qh[1] = chunks[2]; }
|
||||
#pragma unroll
|
||||
for (int g = 0; g < 4; ++g) raw_codes[g] = chunks[kCodeChunk + g * 2 + f_c];
|
||||
}
|
||||
const int kb0 = sb * 8 + G * 2;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kActFetch; ++i)
|
||||
if (a_src[i] != nullptr) a_data[i] = *reinterpret_cast<const uint4*>(a_src[i] + kb0 * 32);
|
||||
};
|
||||
uint32_t own_scale = 0, own_high = 0;
|
||||
const auto commit_stage = [&]<int G>() {
|
||||
if constexpr (G == 0) {
|
||||
header = raw_header;
|
||||
#pragma unroll
|
||||
for (int g = 0; g < 4; ++g) codes[g] = raw_codes[g];
|
||||
if constexpr (Q5K) { qh[0] = raw_qh[0]; qh[1] = raw_qh[1]; }
|
||||
}
|
||||
const int sb32 = 2 * G + f_c;
|
||||
const unsigned shift = 8U * ((unsigned) sb32 & 3U);
|
||||
const uint32_t scale_byte = header.y >> shift, min_byte = header.z >> shift;
|
||||
uint32_t sc, mn;
|
||||
if (sb32 < 4) {
|
||||
sc = scale_byte & 0x3FU;
|
||||
mn = min_byte & 0x3FU;
|
||||
} else {
|
||||
const uint32_t packed = header.w >> shift;
|
||||
sc = (packed & 0x0FU) | ((scale_byte & 0xC0U) >> 2U);
|
||||
mn = ((packed >> 4U) & 0x0FU) | ((min_byte & 0xC0U) >> 2U);
|
||||
}
|
||||
const __half2 dm = __builtin_bit_cast(__half2, header.x);
|
||||
const float scale = row_live ? __low2float(dm) * (float) sc : 0.0F;
|
||||
const float offset = row_live ? __high2float(dm) * (float) mn : 0.0F;
|
||||
own_scale = __builtin_bit_cast(uint32_t, __floats2half2_rn(scale, -offset));
|
||||
if constexpr (Q5K) {
|
||||
const uint32_t q[8] = {qh[0].x, qh[0].y, qh[0].z, qh[0].w, qh[1].x, qh[1].y, qh[1].z, qh[1].w};
|
||||
uint32_t high = 0;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 8; ++i) high |= gather_bit(q[i], sb32) << (4 * i);
|
||||
own_high = high;
|
||||
}
|
||||
uint4* buf = s_act + (DB ? (G & 1) * kActBuf : 0);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kActFetch; ++i) buf[a_slot[i]] = a_data[i];
|
||||
};
|
||||
const auto load_b = [&](const uint4* buf, int kb, int j, v16h& b_lo, v16h& b_hi) {
|
||||
uint4 b[4];
|
||||
#pragma unroll
|
||||
for (int q = 0; q < 4; ++q) b[q] = buf[act_slot<kActStride, DB>(kb * 4 + q, j * 16 + sub_lane)];
|
||||
__builtin_memcpy(&b_lo, &b[0], 32);
|
||||
__builtin_memcpy(&b_hi, &b[2], 32);
|
||||
};
|
||||
v8f acc[kTokTiles];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < kTokTiles; ++j) acc[j] = v8f{0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F};
|
||||
const __half2 magic = __floats2half2_rn(-1024.0F, -1024.0F);
|
||||
const auto compute_stage = [&]<int G>() {
|
||||
const uint4 c = codes[G];
|
||||
const uint32_t words[4] = {c.x, c.y, c.z, c.w};
|
||||
const uint32_t peer_scale = swap_halves(own_scale);
|
||||
const uint32_t peer_high = Q5K ? swap_halves(own_high) : 0U;
|
||||
#pragma unroll
|
||||
for (int kb = 0; kb < BK; ++kb) {
|
||||
const __half2 sb = __builtin_bit_cast(__half2, f_c == kb ? own_scale : peer_scale);
|
||||
const __half2 scale2 = __low2half2(sb), bias2 = __high2half2(sb);
|
||||
const uint32_t high = Q5K ? ((f_c == kb ? own_high : peer_high) >> (16U * f_c)) : 0U;
|
||||
__half2 h[8];
|
||||
v8u own, peer;
|
||||
if constexpr (MD == 2) {
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
own[2 * i] = words[i] ^ (uint32_t) kb ^ __builtin_bit_cast(uint32_t, sb);
|
||||
own[2 * i + 1] = words[i] + high;
|
||||
}
|
||||
peer = own;
|
||||
} else {
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
uint32_t nib = (words[i] >> (4U * kb)) & 0x0F0F0F0FU;
|
||||
if constexpr (Q5K) nib |= spread_high_bits((high >> (4 * i)) & 0xFU);
|
||||
codes_to_halves(nib, magic, scale2, bias2, h[2 * i], h[2 * i + 1]);
|
||||
}
|
||||
__builtin_memcpy(&own, &h[0], 32);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 8; ++i) peer[i] = swap_halves(own[i]);
|
||||
}
|
||||
const v16h a_lo = __builtin_bit_cast(v16h, own), a_hi = __builtin_bit_cast(v16h, peer);
|
||||
const uint4* buf = s_act + (DB ? (G & 1) * kActBuf : 0);
|
||||
if constexpr (IL) {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < kTokTiles; j += 2) {
|
||||
asm volatile("" ::: "memory"); // two token tiles' fragments live at a time
|
||||
if (j >= live_tok_tiles) continue;
|
||||
v16h b0_lo, b0_hi, b1_lo, b1_hi;
|
||||
load_b(buf, kb, j, b0_lo, b0_hi);
|
||||
load_b(buf, kb, j + 1, b1_lo, b1_hi);
|
||||
acc[j] = wmma(a_lo, b0_lo, acc[j]);
|
||||
acc[j + 1] = wmma(a_lo, b1_lo, acc[j + 1]);
|
||||
acc[j] = wmma(a_hi, b0_hi, acc[j]);
|
||||
acc[j + 1] = wmma(a_hi, b1_hi, acc[j + 1]);
|
||||
}
|
||||
} else {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < kTokTiles; ++j) {
|
||||
asm volatile("" ::: "memory"); // one token tile's fragments live at a time (hoisting all eight spills)
|
||||
if (j >= live_tok_tiles) continue;
|
||||
v16h b_lo, b_hi;
|
||||
load_b(buf, kb, j, b_lo, b_hi);
|
||||
if constexpr (MD == 1) {
|
||||
const v8u bl = __builtin_bit_cast(v8u, b_lo), bh = __builtin_bit_cast(v8u, b_hi);
|
||||
acc[j][0] += __uint_as_float((own[j & 7] ^ peer[(j + 1) & 7] ^ bl[0] ^ bl[5] ^ bh[2] ^ bh[7]) & 0x3F7FFFFFU);
|
||||
} else {
|
||||
acc[j] = wmma(a_lo, b_lo, acc[j]);
|
||||
acc[j] = wmma(a_hi, b_hi, acc[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const auto run_stage = [&]<int G>(int sb) {
|
||||
commit_stage.template operator()<G>();
|
||||
__syncthreads();
|
||||
if constexpr (G < 3) fetch_stage.template operator()<G + 1>(sb);
|
||||
else if (sb + 1 < num_sb) fetch_stage.template operator()<0>(sb + 1);
|
||||
compute_stage.template operator()<G>();
|
||||
if constexpr (!DB) __syncthreads();
|
||||
};
|
||||
fetch_stage.template operator()<0>(0);
|
||||
for (int sb = 0; sb < num_sb; ++sb) {
|
||||
run_stage.template operator()<0>(sb);
|
||||
run_stage.template operator()<1>(sb);
|
||||
run_stage.template operator()<2>(sb);
|
||||
run_stage.template operator()<3>(sb);
|
||||
}
|
||||
if constexpr (DB) __syncthreads(); // the epilogue's scratch overlays the stage buffers
|
||||
// lane (token n, rows 2l + half) holds gate rows in acc[j][l] and the matching up rows in acc[j][l + 4]
|
||||
__half* scratch = reinterpret_cast<__half*>(lds);
|
||||
#pragma unroll
|
||||
for (int j = 0; j < kTokTiles; ++j) {
|
||||
if (j >= live_tok_tiles) continue;
|
||||
#pragma unroll
|
||||
for (int l = 0; l < 4; ++l) {
|
||||
float product = acc[j][l + 4] * acc[j][l];
|
||||
asm volatile("" : "+v"(product));
|
||||
float value = product * (1.0f / (1.0f + __expf(-acc[j][l])));
|
||||
asm volatile("" : "+v"(value));
|
||||
scratch[(j * 16 + sub_lane) * kPairStride + wave_id * 8 + 2 * l + half_id] = __float2half(value);
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
#pragma unroll
|
||||
for (int unit = 0; unit < BN * kRows / (2 * 256); ++unit) {
|
||||
const int flat = (unit * 256 + tid) * 2, t = t_local + flat / kRows, r = r_block + flat % kRows;
|
||||
if (t < bucket_rows && r < m) {
|
||||
const size_t o = (size_t) (bucket_begin + t) * m + r;
|
||||
const int s = (flat / kRows) * kPairStride + flat % kRows;
|
||||
*reinterpret_cast<__half2*>(out + o) = *reinterpret_cast<const __half2*>(scratch + s);
|
||||
}
|
||||
}
|
||||
#else
|
||||
(void) b; (void) geo; (void) ts; (void) x; (void) tiles; (void) off; (void) src; (void) out; (void) m; (void) k;
|
||||
__builtin_trap();
|
||||
#endif
|
||||
}
|
||||
|
||||
// down: x = H rows [routed row][k]; the outputs to the same rows, F16 [routed row][m]
|
||||
// MD, WPE, FL as for pair_kernel; BKT (--set p1a_dbk=4): K blocks of 32 a stage
|
||||
template <int WT, int MD = 0, int WPE = 0, int BKT = 2, int FL = 0>
|
||||
__launch_bounds__(256) __attribute__((amdgpu_waves_per_eu(WPE > 0 ? WPE : 1))) __global__ void down_kernel(Batch b, NativeGeom geo, const int32_t* __restrict__ ts, const int2* __restrict__ tiles, const __half* __restrict__ x,
|
||||
const int32_t* __restrict__ off, __half* __restrict__ out, int m, int k) {
|
||||
#if P1A_DEVICE
|
||||
constexpr int BN = 128, BK = BKT, kTokTiles = BN / 16;
|
||||
constexpr bool IL = (FL & 1) != 0 && MD == 0, DB = (FL & 2) != 0;
|
||||
constexpr bool Q5 = WT == kQ51;
|
||||
constexpr int kBlockBytes = Q5 ? 24 : 34;
|
||||
constexpr int kActStride = DB ? BN : BN + 1, kActBuf = BK * 4 * kActStride, kActBytes = (DB ? 2 : 1) * kActBuf * 16;
|
||||
constexpr int kOutStride = 128 + 8, kEpiTokens = 64, kEpiBytes = kEpiTokens * kOutStride * 2;
|
||||
constexpr int kLdsBytes = kActBytes > kEpiBytes ? kActBytes : kEpiBytes;
|
||||
__shared__ __attribute__((aligned(16))) uint8_t lds[kLdsBytes];
|
||||
auto* s_act = reinterpret_cast<uint4*>(lds);
|
||||
|
||||
// the work item: fused::group's 64-row tile y of this batch and the next one of the same expert (a 128-row item);
|
||||
// the second tile of a pair has no block of its own
|
||||
const int t0 = ts[b.e0], w = (int) blockIdx.y;
|
||||
if (w >= ts[b.e1] - t0) return;
|
||||
const int2 tl = tiles[t0 + w];
|
||||
const int expert = tl.x;
|
||||
if ((t0 + w - ts[expert]) & 1) return;
|
||||
const int t_local = 0;
|
||||
const int bucket_begin = tl.y;
|
||||
const int bucket_rows = min(BN, off[expert + 1] - bucket_begin);
|
||||
const int live_tok_tiles = min(kTokTiles, (bucket_rows - t_local + 15) / 16);
|
||||
const int num_kb = k / 32;
|
||||
|
||||
const int tid = (int) threadIdx.x, wave_id = tid >> 5, lane_id = tid & 31, sub_lane = lane_id & 15, half_id = lane_id >> 4;
|
||||
const int r_block = (int) blockIdx.x * 128;
|
||||
const int f_c = half_id ^ (sub_lane & 1);
|
||||
const int row = r_block + wave_id * 16 + sub_lane;
|
||||
const bool row_live = row < m;
|
||||
const uint8_t* w_row = b.blob[expert - b.e0] + geo.down_off + (size_t) (row_live ? row : m - 1) * geo.d_row;
|
||||
|
||||
constexpr int kActFetch = BN * BK * 4 / 256;
|
||||
const __half* a_src[kActFetch];
|
||||
int a_slot[kActFetch];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kActFetch; ++i) {
|
||||
const int chunk = tid + i * 256, t = chunk / (BK * 4), sub = chunk % (BK * 4), c_row = t_local + t;
|
||||
a_src[i] = c_row < bucket_rows && MD != 3 ? x + (size_t) (bucket_begin + c_row) * k + sub * 8 : nullptr;
|
||||
a_slot[i] = act_slot<kActStride, DB>(sub, t);
|
||||
}
|
||||
const auto load_b = [&](const uint4* buf, int kb, int j, v16h& b_lo, v16h& b_hi) {
|
||||
uint4 b[4];
|
||||
#pragma unroll
|
||||
for (int q = 0; q < 4; ++q) b[q] = buf[act_slot<kActStride, DB>(kb * 4 + q, j * 16 + sub_lane)];
|
||||
__builtin_memcpy(&b_lo, &b[0], 32);
|
||||
__builtin_memcpy(&b_hi, &b[2], 32);
|
||||
};
|
||||
uint4 w_codes[2][BK];
|
||||
uint32_t w_head[2][BK], w_high[2][BK];
|
||||
uint4 a_data[kActFetch];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kActFetch; ++i) a_data[i] = make_uint4(0u, 0u, 0u, 0u);
|
||||
const auto fetch_stage = [&]<int S>(int kb0) {
|
||||
#pragma unroll
|
||||
for (int b = 0; b < BK; ++b) {
|
||||
const uint8_t* blk = w_row + (kb0 + b) * kBlockBytes;
|
||||
if constexpr (MD == 5) {
|
||||
w_head[S][b] = 0x3C003C00U ^ (uint32_t) kb0;
|
||||
w_high[S][b] = (uint32_t) (tid + b);
|
||||
w_codes[S][b] = make_uint4((uint32_t) kb0, (uint32_t) tid, (uint32_t) b, (uint32_t) row);
|
||||
} else if constexpr (Q5) {
|
||||
const uint2 head = *reinterpret_cast<const uint2*>(blk);
|
||||
w_head[S][b] = head.x;
|
||||
w_high[S][b] = head.y;
|
||||
w_codes[S][b] = *reinterpret_cast<const uint4*>(blk + 8);
|
||||
} else {
|
||||
w_head[S][b] = *reinterpret_cast<const uint16_t*>(blk);
|
||||
__builtin_memcpy(&w_codes[S][b], blk + 2 + 16 * f_c, 16);
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kActFetch; ++i)
|
||||
if (a_src[i] != nullptr) a_data[i] = *reinterpret_cast<const uint4*>(a_src[i] + kb0 * 32);
|
||||
};
|
||||
v8f acc[kTokTiles];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < kTokTiles; ++j) acc[j] = v8f{0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F};
|
||||
constexpr float kMagic = Q5 ? -1024.0F : -1152.0F;
|
||||
const __half2 magic = __floats2half2_rn(kMagic, kMagic);
|
||||
const auto compute_stage = [&]<int S>() {
|
||||
#pragma unroll
|
||||
for (int kb = 0; kb < BK; ++kb) {
|
||||
const uint32_t scale_bias = Q5 ? w_head[S][kb] : (row_live ? w_head[S][kb] : 0U);
|
||||
const __half2 sb = __builtin_bit_cast(__half2, scale_bias);
|
||||
const __half2 scale2 = __low2half2(sb), bias2 = __high2half2(sb);
|
||||
const uint4 c = w_codes[S][kb];
|
||||
const uint32_t words[4] = {c.x, c.y, c.z, c.w};
|
||||
__half2 h[8];
|
||||
v8u own, peer;
|
||||
if constexpr (MD == 2) {
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
own[2 * i] = words[i] ^ scale_bias;
|
||||
own[2 * i + 1] = words[i] + w_high[S][kb];
|
||||
}
|
||||
peer = own;
|
||||
} else {
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
uint32_t nib;
|
||||
if constexpr (Q5) nib = ((words[i] >> (4U * f_c)) & 0x0F0F0F0FU) | spread_high_bits((w_high[S][kb] >> (16 * f_c + 4 * i)) & 0xFU);
|
||||
else nib = words[i] ^ 0x80808080U;
|
||||
codes_to_halves(nib, magic, scale2, bias2, h[2 * i], h[2 * i + 1]);
|
||||
}
|
||||
__builtin_memcpy(&own, &h[0], 32);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 8; ++i) peer[i] = swap_halves(own[i]);
|
||||
}
|
||||
const v16h a_lo = __builtin_bit_cast(v16h, own), a_hi = __builtin_bit_cast(v16h, peer);
|
||||
const uint4* buf = s_act + (DB ? S * kActBuf : 0);
|
||||
if constexpr (IL) {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < kTokTiles; j += 2) {
|
||||
asm volatile("" ::: "memory");
|
||||
if (j >= live_tok_tiles) continue;
|
||||
v16h b0_lo, b0_hi, b1_lo, b1_hi;
|
||||
load_b(buf, kb, j, b0_lo, b0_hi);
|
||||
load_b(buf, kb, j + 1, b1_lo, b1_hi);
|
||||
acc[j] = wmma(a_lo, b0_lo, acc[j]);
|
||||
acc[j + 1] = wmma(a_lo, b1_lo, acc[j + 1]);
|
||||
acc[j] = wmma(a_hi, b0_hi, acc[j]);
|
||||
acc[j + 1] = wmma(a_hi, b1_hi, acc[j + 1]);
|
||||
}
|
||||
} else {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < kTokTiles; ++j) {
|
||||
asm volatile("" ::: "memory");
|
||||
if (j >= live_tok_tiles) continue;
|
||||
v16h b_lo, b_hi;
|
||||
load_b(buf, kb, j, b_lo, b_hi);
|
||||
if constexpr (MD == 1) {
|
||||
const v8u bl = __builtin_bit_cast(v8u, b_lo), bh = __builtin_bit_cast(v8u, b_hi);
|
||||
acc[j][0] += __uint_as_float((own[j & 7] ^ peer[(j + 1) & 7] ^ bl[0] ^ bl[5] ^ bh[2] ^ bh[7]) & 0x3F7FFFFFU);
|
||||
} else {
|
||||
acc[j] = wmma(a_lo, b_lo, acc[j]);
|
||||
acc[j] = wmma(a_hi, b_hi, acc[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const auto run_stage = [&]<int S>(int kb0) {
|
||||
uint4* wbuf = s_act + (DB ? S * kActBuf : 0);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < kActFetch; ++i) wbuf[a_slot[i]] = a_data[i];
|
||||
__syncthreads();
|
||||
if (kb0 + BK < num_kb) fetch_stage.template operator()<1 - S>(kb0 + BK);
|
||||
compute_stage.template operator()<S>();
|
||||
if constexpr (!DB) __syncthreads();
|
||||
};
|
||||
fetch_stage.template operator()<0>(0);
|
||||
for (int kb0 = 0; kb0 < num_kb; kb0 += 2 * BK) {
|
||||
run_stage.template operator()<0>(kb0);
|
||||
if (kb0 + BK < num_kb) run_stage.template operator()<1>(kb0 + BK);
|
||||
}
|
||||
if constexpr (DB) __syncthreads(); // the epilogue's scratch overlays the stage buffers
|
||||
__half* scratch = reinterpret_cast<__half*>(lds);
|
||||
#pragma unroll
|
||||
for (int part = 0; part < BN / kEpiTokens; ++part) {
|
||||
if (part * (kEpiTokens / 16) >= live_tok_tiles) break;
|
||||
if (part > 0) __syncthreads();
|
||||
#pragma unroll
|
||||
for (int jj = 0; jj < kEpiTokens / 16; ++jj) {
|
||||
const int j = part * (kEpiTokens / 16) + jj;
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 8; ++i)
|
||||
scratch[(jj * 16 + sub_lane) * kOutStride + wave_id * 16 + 2 * i + half_id] =
|
||||
__float2half(fminf(fmaxf(acc[j][i], -65504.0f), 65504.0f));
|
||||
}
|
||||
__syncthreads();
|
||||
#pragma unroll
|
||||
for (int unit = 0; unit < kEpiTokens * 16 / 256; ++unit) {
|
||||
const int flat = unit * 256 + tid, tr = flat / 16, chunk = flat % 16;
|
||||
const int t = t_local + part * kEpiTokens + tr, r = r_block + chunk * 8;
|
||||
if (t >= bucket_rows || r >= m) continue;
|
||||
*reinterpret_cast<uint4*>(out + (size_t) (bucket_begin + t) * m + r) =
|
||||
*reinterpret_cast<const uint4*>(scratch + tr * kOutStride + chunk * 8);
|
||||
}
|
||||
}
|
||||
#else
|
||||
(void) b; (void) geo; (void) ts; (void) x; (void) tiles; (void) off; (void) out; (void) m; (void) k;
|
||||
__builtin_trap();
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
static_assert(kMoeFF % 64 == 0 && kMoeEmbd % 256 == 0 && kMoeEmbd % 128 == 0 && kMoeFF % 32 == 0, "Path A's tile and stage sizes");
|
||||
|
||||
bool takes(int gu_type, int d_type, bool l2) {
|
||||
if (gu_type == kQ4K && d_type == kQ51) return true;
|
||||
return l2 && (gu_type == kQ4K || gu_type == kQ5K) && (d_type == kQ51 || d_type == kQ80);
|
||||
}
|
||||
|
||||
void gate_up(const Batch& b, const NativeGeom& g, const int32_t* ts, const int2* tiles, const int32_t* off, int64_t work_bound,
|
||||
const void* x16, const int32_t* src, void* h16, cudaStream_t s) {
|
||||
const dim3 grid((unsigned) (kMoeFF / 64), (unsigned) std::max<int64_t>(1, work_bound));
|
||||
if (g.gu_type == kQ5K)
|
||||
pair_kernel<kQ5K><<<grid, 256, 0, s>>>(b, g, ts, tiles, (const __half*) x16, off, src, (__half*) h16, kMoeFF, kMoeEmbd);
|
||||
else
|
||||
pair_kernel<kQ4K><<<grid, 256, 0, s>>>(b, g, ts, tiles, (const __half*) x16, off, src, (__half*) h16, kMoeFF, kMoeEmbd);
|
||||
}
|
||||
|
||||
void down(const Batch& b, const NativeGeom& g, const int32_t* ts, const int2* tiles, const int32_t* off, int64_t work_bound,
|
||||
const void* h16, void* d16, cudaStream_t s) {
|
||||
const dim3 grid((unsigned) ((kMoeEmbd + 127) / 128), (unsigned) std::max<int64_t>(1, work_bound));
|
||||
if (g.d_type == kQ80)
|
||||
down_kernel<kQ80><<<grid, 256, 0, s>>>(b, g, ts, tiles, (const __half*) h16, off, (__half*) d16, kMoeEmbd, kMoeFF);
|
||||
else
|
||||
down_kernel<kQ51><<<grid, 256, 0, s>>>(b, g, ts, tiles, (const __half*) h16, off, (__half*) d16, kMoeEmbd, kMoeFF);
|
||||
}
|
||||
|
||||
} // namespace strata::prefill::fused::p1a
|
||||
@@ -0,0 +1,28 @@
|
||||
// src/prefill/moe_p1a.hpp - gfx11: the K-quant prompt experts with F16 activations and F16 H (moe_p1a.cu, gufo's
|
||||
// RoutedPairF16Kernel / RoutedDownF16Kernel). The engine takes them on gfx1151 only, behind STRATA_PF_P1A=1
|
||||
// (moe_fused_iq.cu: experts_native, p1a_input); off by default.
|
||||
// Internal to moe_fused_iq.cu, which owns the native experts' tables and launches.
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include "strata/prefill/moe_fused.hpp"
|
||||
#include "strata/prefill/moe_fused_iq.hpp"
|
||||
|
||||
namespace strata::prefill::fused::p1a {
|
||||
|
||||
/// The pair has kernels here: Q4_K gate/up with Q5_1 down; with `l2` (STRATA_PF_P1A_L2=1) also Q5_K gate/up and Q8_0 down.
|
||||
bool takes(int gu_type, int d_type, bool l2);
|
||||
|
||||
/// Gate/up + SwiGLU for experts [b.e0, b.e1): `x16` the layer input as FP16 rows [token][n_embd], `src` the routed rows'
|
||||
/// tokens, `ts`, `tiles`, `off` fused::group's tables (at most `work_bound` 64-row tiles in this batch); H as FP16 rows
|
||||
/// [routed row][n_ff] into `h16`.
|
||||
void gate_up(const Batch& b, const NativeGeom& g, const int32_t* ts, const int2* tiles, const int32_t* off, int64_t work_bound,
|
||||
const void* x16, const int32_t* src, void* h16, cudaStream_t s);
|
||||
/// Down of that H: the routed rows' outputs as FP16 [routed row][n_embd] into `d16` (saturated at +-65504; moe_combine16 reads them).
|
||||
void down(const Batch& b, const NativeGeom& g, const int32_t* ts, const int2* tiles, const int32_t* off, int64_t work_bound,
|
||||
const void* h16, void* d16, cudaStream_t s);
|
||||
|
||||
} // namespace strata::prefill::fused::p1a
|
||||
+74
-1
@@ -117,6 +117,7 @@ bool ple_batch_enabled() {
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
@@ -192,6 +193,10 @@ bool native_wide() { return false; }
|
||||
void experts_native(const WideBatch&, const NativeGeom&, int, int64_t, const void*, const void*, const int32_t*, void*,
|
||||
float*, void*) {}
|
||||
void set_down_rotation(bool, const float*) {}
|
||||
bool p1a_wanted() { return false; }
|
||||
void p1a_input(const void*, size_t) {}
|
||||
bool p1a_used() { return false; }
|
||||
bool p1a_takes_pair(int, int) { return false; }
|
||||
void hadamard128_rows(float*, int64_t, int64_t, const float*, void*) {}
|
||||
} // namespace strata::prefill::fused
|
||||
#endif
|
||||
@@ -1506,6 +1511,7 @@ struct Prefill::Impl {
|
||||
// row map, the group bounds, the group buffers of gathered experts
|
||||
void *Xq = nullptr, *Hq = nullptr;
|
||||
float* H = nullptr;
|
||||
size_t H_bytes = 0; // H's size (STRATA_PF_P1A: FP16 H rows must fit)
|
||||
int32_t *ids_identity = nullptr, *bounds_dev = nullptr;
|
||||
uint8_t *grp_gu = nullptr, *grp_d = nullptr;
|
||||
float *Xscale = nullptr, *Hscale = nullptr, *grp_tail = nullptr; // NVFP4: activation scales, expert tails
|
||||
@@ -2162,6 +2168,62 @@ bool fused_layout(size_t T, bool src) {
|
||||
}
|
||||
// The MoE buffers MMQ and the fused path share: GU and H in floats, Xq and Hq in bytes. Without `fused` (the
|
||||
// default): MMQ's, for T tokens.
|
||||
// STRATA_PF_P1A=1: once, at the last layer of the first fused prompt chunk, which layers' experts took the P1A kernels (by type pair)
|
||||
template <class F>
|
||||
void p1a_report(const std::vector<F>& fmt, size_t l, bool took) {
|
||||
static std::vector<int> seen; // per layer: -1 not on the fused path yet, 0 today's kernels, 1 P1A
|
||||
static bool said = false;
|
||||
if (said || fmt.empty()) return;
|
||||
if (seen.empty()) seen.assign(fmt.size(), -1);
|
||||
if (l < seen.size() && seen[l] < 0) seen[l] = took ? 1 : 0;
|
||||
if (l + 1 != fmt.size()) return;
|
||||
said = true;
|
||||
const auto name = [](int t) -> std::string {
|
||||
switch (t) {
|
||||
case 7: return "Q5_1";
|
||||
case 8: return "Q8_0";
|
||||
case 12: return "Q4_K";
|
||||
case 13: return "Q5_K";
|
||||
default: return "type " + std::to_string(t);
|
||||
}
|
||||
};
|
||||
std::map<std::string, int> on, off;
|
||||
int n_on = 0, n_off = 0, n_unseen = 0;
|
||||
for (size_t i = 0; i < fmt.size(); ++i) {
|
||||
const std::string pair = name(fmt[i].gu_type) + "/" + name(fmt[i].d_type);
|
||||
if (seen[i] == 1) { ++on[pair]; ++n_on; }
|
||||
else if (seen[i] == 0) { ++off[pair]; ++n_off; }
|
||||
else ++n_unseen;
|
||||
}
|
||||
const auto list = [](const std::map<std::string, int>& m) {
|
||||
std::string s;
|
||||
for (const auto& kv : m) s += (s.empty() ? "" : ", ") + kv.first + " " + std::to_string(kv.second);
|
||||
return s.empty() ? std::string("none") : s;
|
||||
};
|
||||
if (n_on == 0) return; // a pack or chunk with no P1A layer: no line
|
||||
std::fprintf(stderr, "strata: prompt experts P1A (STRATA_PF_P1A=1): gate/up + down on the P1A kernels in %d of %zu layers (%s); "
|
||||
"today's kernels in %d (%s); %d not on the fused path in this chunk\n",
|
||||
n_on, fmt.size(), list(on).c_str(), n_off, list(off).c_str(), n_unseen);
|
||||
}
|
||||
// STRATA_PF_P1A (gfx1151): some layer of the loaded native pack takes the P1A kernels; only then does H grow to FP16 rows, so a
|
||||
// pack with no such layer sizes exactly what it did before. The expert layout is loaded before the first plan (generate.cpp's
|
||||
// main: expert_layout_load ahead of every bytes_needed and Prefill::init); debug builds check that every sizing sees one answer.
|
||||
// A smaller H than the P1A kernels need is never written past: experts_native takes them only when the FP16 rows fit in the
|
||||
// H that Prefill::init carved (H_bytes), and runs today's kernels for that call otherwise.
|
||||
bool p1a_pack() {
|
||||
bool any = false;
|
||||
if (fused::p1a_wanted()) {
|
||||
const strata::kernels::cpu::ExpertLayout& lay = strata::kernels::cpu::expert_layout();
|
||||
for (size_t l = 0; lay.native && l < lay.fmt.size() && !any; ++l)
|
||||
any = fused::p1a_takes_pair(lay.fmt[l].gu_type, lay.fmt[l].d_type);
|
||||
}
|
||||
#ifndef NDEBUG
|
||||
static std::atomic<int> first{-1}; // the first sizing's answer; a later different one = a plan made before the layout was loaded
|
||||
int want = -1;
|
||||
if (!first.compare_exchange_strong(want, (int) any)) assert(want == (int) any);
|
||||
#endif
|
||||
return any;
|
||||
}
|
||||
struct MoeBufs { size_t gu, h, xq, hq; };
|
||||
// `tail` (>= 0, fused only): the MMQ rows a chunk below the floor may bring, in tokens (a lent layout sized for its
|
||||
// segment's own last chunk, loan_rules.hpp); -1: stream_all_min() - 1, as the start-up layout.
|
||||
@@ -2173,8 +2235,10 @@ MoeBufs moe_bufs(size_t T, int64_t n_expert, bool fused, int64_t tail = -1) {
|
||||
if (!fused) return {T * K * kFF2, T * K * kFF, mmq::q8_bytes((int64_t) (T * K), N), mmq::q8_bytes((int64_t) (T * K), kFF)};
|
||||
const int64_t tail_tokens = tail >= 0 ? std::min<int64_t>(tail, std::max<int64_t>(stream_all_min() - 1, 0)) : stream_all_min() - 1;
|
||||
const size_t ts = (size_t) std::min<int64_t>((int64_t) T, tail_tokens); // MMQ's last small chunk
|
||||
// STRATA_PF_P1A=1 (gfx1151 only; on by default on Windows gfx1151) and a pack with P1A layers: H holds FP16 rows (n_ff halves a routed row)
|
||||
const size_t h_p1a = p1a_pack() ? (T * K * kFF * 2 + 3) / 4 : 0;
|
||||
return {std::max<size_t>(ts * K * kFF2, (fused::group_bytes((int64_t) (T * K), (int) n_expert) + 3) / 4),
|
||||
std::max<size_t>(ts * K * kFF, (fused::act_bytes((int64_t) (T * K), kFF) + 3) / 4),
|
||||
std::max<size_t>(std::max<size_t>(ts * K * kFF, (fused::act_bytes((int64_t) (T * K), kFF) + 3) / 4), h_p1a),
|
||||
std::max(mmq::q8_bytes((int64_t) (ts * K), N), fused::act_bytes((int64_t) T, N)),
|
||||
mmq::q8_bytes((int64_t) (ts * K), kFF)};
|
||||
}
|
||||
@@ -2582,6 +2646,7 @@ bool Prefill::carve(size_t T, void* alloc) {
|
||||
if (mp.any) {
|
||||
m.Xq = c.take<uint8_t>(mb.xq, ok);
|
||||
m.H = c.take<float>(mb.h, ok);
|
||||
m.H_bytes = mb.h * sizeof(float);
|
||||
m.Hq = c.take<uint8_t>(mb.hq, ok);
|
||||
m.Xscale = c.take<float>(T * K, ok); m.Hscale = c.take<float>(T * K, ok);
|
||||
m.grp_tail = c.take<float>(MMQ_GROUP * 4, ok);
|
||||
@@ -5357,6 +5422,9 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
else fused::quantize_act(m.mixed, T, N, m.Xq, m.cs);
|
||||
// (a folded trellis down: H rotated inside experts_native; off again after the layer's launches)
|
||||
if (fused_nat && had_d) fused::set_down_rotation(true, had_sh);
|
||||
// STRATA_PF_P1A=1 (gfx1151): the layer input's FP16 rows for the P1A kernels (p1a_input keeps nullptr otherwise)
|
||||
if (fused_nat) fused::p1a_input(had_gu ? nullptr : m.mixed_h, m.H_bytes);
|
||||
bool p1a_layer = false;
|
||||
fused::group(m.ids, T * K, (int) K, (int) m.g->n_expert, m.GU, m.slot_dev, m.src_dev, m.cs);
|
||||
// launches over the experts in id order, each at most kMaxBatch experts (STRATA_PF_BATCH: below)
|
||||
// of which at most a third of the ring streamed: the next batch's blobs arrive while one computes.
|
||||
@@ -5416,6 +5484,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
m.Dm, m.cs);
|
||||
}
|
||||
dm16_used = fused::dm_f16_used();
|
||||
p1a_layer = p1a_layer || fused::p1a_used();
|
||||
} else {
|
||||
fused::experts(b, (int) m.g->n_expert, T * K, m.GU, m.Xq, m.src_dev, m.H, m.Dm, m.cs);
|
||||
}
|
||||
@@ -5429,6 +5498,10 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
}
|
||||
}
|
||||
if (fused_nat && had_d) fused::set_down_rotation(false, nullptr);
|
||||
if (fused_nat && fused::p1a_wanted()) {
|
||||
fused::p1a_input(nullptr, 0);
|
||||
p1a_report(lay.fmt, (size_t) l, p1a_layer);
|
||||
}
|
||||
} else {
|
||||
// group the (token, k) pairs by expert on the host
|
||||
pt.mark(kPfHostGroup, cs);
|
||||
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
# gufo (gufo contributors), MIT
|
||||
|
||||
Strata adapts routed-expert GEMM kernels for gfx11 from gufo (https://github.com/gufo-org/gufo), MIT License:
|
||||
|
||||
- `src/prefill/moe_f16_kq.cu` and `src/prefill/moe_f16_common.cuh`: gufo 97b8645f, `src/models/qwen38_flash_next/kernels/rocm/kernels.hip.cpp`
|
||||
(RoutedF16GEMMKernel and its helpers).
|
||||
- `src/prefill/moe_p1a.cu` (the prompt experts behind STRATA_PF_P1A, gfx1151): gufo 07e1191f36885c144b34566aac8eaa9777d8fa78, the same file
|
||||
(RoutedPairF16Kernel, RoutedDownF16Kernel and their helpers).
|
||||
|
||||
Copyright (c) 2026 gufo contributors. The full MIT licence text is in each adapted source file and in the upstream repository; this notice
|
||||
stays with the code that uses it.
|
||||
Reference in new issue
Block a user