Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
HIP: routed MMQ prefill MoE from Gufo / llama.cpp (default on with ROCm) (#209)
Yaniss Amazouzb55e3e5cfee4
diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.mdindex 3fdc11a..489f7bf 100644--- a/THIRD_PARTY_NOTICES.md+++ b/THIRD_PARTY_NOTICES.md@@ -2,6 +2,11 @@## llama.cpp / ggml (MIT License)+exllamav3/exllamav3_ext/rocm/gmmq/ (routed MMQ prefill MoE for HIP) is adapted from Gufo (https://github.com/gufo-org/gufo,+commit 07e1191, path src/models/qwen38_flash_next/kernels/rocm/mmq/, MIT License, Copyright (c) 2026 Gufo contributors), itself+adapted from llama.cpp (commit 5c0e946, ggml/src/ggml-cuda mmq / mmid / quantize, MIT License, Copyright (c) 2023-2026 The ggml+authors). Each file keeps its licence header and lists its changes. Gufo's MIT licence text is the same as the one below.+Parts of exllamav3/exllamav3_ext/foreign_quant/ggml_mmvq/ and foreign_quant/fq_mmvq.cu are derived fromllama.cpp (https://github.com/ggml-org/llama.cpp, commit 7182ef7):diff --git a/exllamav3/exllamav3_ext/bindings.cpp b/exllamav3/exllamav3_ext/bindings.cppindex 67596cd..658bd4a 100644--- a/exllamav3/exllamav3_ext/bindings.cpp+++ b/exllamav3/exllamav3_ext/bindings.cpp@@ -118,6 +118,7 @@#include "generator/dry.cuh"#include "ngram.cuh"#include "sam.h"+#include "rocm/gmmq/gmmq.h"#endif@@ -476,6 +477,10 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)#if defined(USE_ROCM)m.def("mla_absorb_dec", &mla_absorb_dec, "mla_absorb_dec");m.def("mla_unfold_dec", &mla_unfold_dec, "mla_unfold_dec");+ // routed (MoE) llama.cpp MMQ via Gufo (rocm/gmmq): out [T * n_used, M] fp32, row t * n_used + slot+ m.def("fq_mmq_moe", &fq_mmq_moe, "fq_mmq_moe",+ py::arg("x"), py::arg("ids"), py::arg("w_ptrs"), py::arg("fmt"), py::arg("M"), py::arg("out"),+ py::arg("tile_cols") = 0, py::arg("max_expert_rows") = 0);#endifm.def("kda_gb_norm", &kda_gb_norm, "kda_gb_norm");m.def("kda_inter_off", &kda_inter_off, "kda_inter_off");diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/common.hpp b/exllamav3/exllamav3_ext/rocm/gmmq/common.hppnew file mode 100644index 0000000..58b2674--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/common.hpp@@ -0,0 +1,1205 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/common.hpp, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+#pragma once++#include "ggml.h"+#include "ggml-impl.h"+#include "ggml-hip.h"++#include <cstdint>+#include <memory>++#define GGML_COMMON_DECL_HIP+#define GGML_COMMON_IMPL_HIP+#include "ggml-common.h"++#include <array>+#include <algorithm>+#include <cassert>+#include <cfloat>+#include <cstdio>+#include <string>+#include <unordered_map>+#include <vector>++#include "vendors/hip.h"++#define STRINGIZE_IMPL(...) #__VA_ARGS__+#define STRINGIZE(...) STRINGIZE_IMPL(__VA_ARGS__)++#define WARP_SIZE 32++#define GGML_HIP_CC_OFFSET_AMD 0x1000000++// AMD+// GCN/CDNA, wave size is 64+#define GGML_HIP_CC_GCN4 (GGML_HIP_CC_OFFSET_AMD + 0x803) // Tonga, Fiji, Polaris, minimum for fast fp16+#define GGML_HIP_CC_VEGA (GGML_HIP_CC_OFFSET_AMD + 0x900) // Vega56/64, minimum for fp16 dual issue+#define GGML_HIP_CC_VEGA20 (GGML_HIP_CC_OFFSET_AMD + 0x906) // MI50/Radeon VII, minimum for dp4a+#define GGML_HIP_CC_CDNA1 (GGML_HIP_CC_OFFSET_AMD + 0x908) // MI100, minimum for MFMA, acc registers+#define GGML_HIP_CC_CDNA2 (GGML_HIP_CC_OFFSET_AMD + 0x90a) // MI210 (gfx90a), minimum acc register renaming+#define GGML_HIP_CC_CDNA3 (GGML_HIP_CC_OFFSET_AMD + 0x942) // MI300+#define GGML_HIP_CC_CDNA4 (GGML_HIP_CC_OFFSET_AMD + 0x950) // MI350X/MI355X++// RDNA removes MFMA, dp4a, xnack, acc registers, wave size is 32+#define GGML_HIP_CC_RDNA1 (GGML_HIP_CC_OFFSET_AMD + 0x1010) // RX 5000+#define GGML_HIP_CC_RDNA2 (GGML_HIP_CC_OFFSET_AMD + 0x1030) // RX 6000, minimum for dp4a+#define GGML_HIP_CC_RDNA3 (GGML_HIP_CC_OFFSET_AMD + 0x1100) // RX 7000, minimum for WMMA+#define GGML_HIP_CC_RDNA3_5 (GGML_HIP_CC_OFFSET_AMD + 0x1150) // AI 370, AI Max 395 laptops.+#define GGML_HIP_CC_RDNA4 (GGML_HIP_CC_OFFSET_AMD + 0x1200) // RX 9000++#define GGML_HIP_CC_IS_AMD(cc) (cc >= GGML_HIP_CC_OFFSET_AMD)+#define GGML_HIP_CC_IS_RDNA(cc) (cc >= GGML_HIP_CC_RDNA1)+#define GGML_HIP_CC_IS_RDNA1(cc) (cc >= GGML_HIP_CC_RDNA1 && cc < GGML_HIP_CC_RDNA2)+#define GGML_HIP_CC_IS_RDNA2(cc) (cc >= GGML_HIP_CC_RDNA2 && cc < GGML_HIP_CC_RDNA3)+#define GGML_HIP_CC_IS_RDNA3_0(cc) (cc >= GGML_HIP_CC_RDNA3 && cc < GGML_HIP_CC_RDNA3_5)+#define GGML_HIP_CC_IS_RDNA3_5(cc) (cc >= GGML_HIP_CC_RDNA3_5 && cc < GGML_HIP_CC_RDNA4)+#define GGML_HIP_CC_IS_RDNA3(cc) (GGML_HIP_CC_IS_RDNA3_0(cc) || GGML_HIP_CC_IS_RDNA3_5(cc))+#define GGML_HIP_CC_IS_RDNA4(cc) (cc >= GGML_HIP_CC_RDNA4)+#define GGML_HIP_CC_IS_GCN(cc) (cc > GGML_HIP_CC_OFFSET_AMD && cc < GGML_HIP_CC_CDNA1)+#define GGML_HIP_CC_IS_CDNA(cc) (cc >= GGML_HIP_CC_CDNA1 && cc < GGML_HIP_CC_RDNA1)+#define GGML_HIP_CC_IS_CDNA1(cc) (cc >= GGML_HIP_CC_CDNA1 && cc < GGML_HIP_CC_CDNA2)+#define GGML_HIP_CC_IS_CDNA2(cc) (cc >= GGML_HIP_CC_CDNA2 && cc < GGML_HIP_CC_CDNA3)+#define GGML_HIP_CC_IS_CDNA3(cc) (cc >= GGML_HIP_CC_CDNA3 && cc < GGML_HIP_CC_CDNA4)+#define GGML_HIP_CC_IS_CDNA4(cc) (cc >= GGML_HIP_CC_CDNA4 && cc < GGML_HIP_CC_RDNA1)++static int ggml_hip_highest_compiled_arch(const int arch) {+ return arch;+}++// ---------------------------------------------------------------------------------------------------------++#define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses++#define GGML_HIP_MAX_STREAMS 8++[[noreturn]]+void ggml_hip_error(const char * stmt, const char * func, const char * file, int line, const char * msg);++#define HIP_CHECK_GEN(err, success, error_fn) \+ do { \+ auto err_ = (err); \+ if (err_ != (success)) { \+ ggml_hip_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \+ } \+ } while (0)++#define HIP_CHECK(err) HIP_CHECK_GEN(err, hipSuccess, hipGetErrorString)++ static const char * hipblas_get_error_str(const hipblasStatus_t err) {+ switch (err) {+ case HIPBLAS_STATUS_SUCCESS: return "HIPBLAS_STATUS_SUCCESS";+ case HIPBLAS_STATUS_NOT_INITIALIZED: return "HIPBLAS_STATUS_NOT_INITIALIZED";+ case HIPBLAS_STATUS_ALLOC_FAILED: return "HIPBLAS_STATUS_ALLOC_FAILED";+ case HIPBLAS_STATUS_INVALID_VALUE: return "HIPBLAS_STATUS_INVALID_VALUE";+ case HIPBLAS_STATUS_ARCH_MISMATCH: return "HIPBLAS_STATUS_ARCH_MISMATCH";+ case HIPBLAS_STATUS_MAPPING_ERROR: return "HIPBLAS_STATUS_MAPPING_ERROR";+ case HIPBLAS_STATUS_EXECUTION_FAILED: return "HIPBLAS_STATUS_EXECUTION_FAILED";+ case HIPBLAS_STATUS_INTERNAL_ERROR: return "HIPBLAS_STATUS_INTERNAL_ERROR";+ case HIPBLAS_STATUS_NOT_SUPPORTED: return "HIPBLAS_STATUS_NOT_SUPPORTED";+ default: return "unknown error";+ }+ }++#define HIPBLAS_CHECK(err) HIP_CHECK_GEN(err, HIPBLAS_STATUS_SUCCESS, hipblas_get_error_str)++# define HIP_SET_SHARED_MEMORY_LIMIT(kernel, nbytes) \+ do { \+ GGML_UNUSED(nbytes); \+ } while (0)++#define GGML_HIP_ASSUME(x)++#define FP16_AVAILABLE++#define FAST_FP16_AVAILABLE++#if defined(CDNA) && !defined(GGML_HIP_NO_MMQ_MFMA)+#define AMD_MFMA_AVAILABLE+#endif // defined(CDNA) && !defined(GGML_HIP_NO_MMQ_MFMA)++#if (defined(RDNA4) || defined(RDNA3))+#define AMD_WMMA_AVAILABLE+#endif // defined(RDNA4)++static bool amd_mfma_available(const int cc) {+#if !defined(GGML_HIP_NO_MMQ_MFMA)+ return GGML_HIP_CC_IS_CDNA(cc);+#else+ return false;+#endif //!defined(GGML_HIP_NO_MMQ_MFMA)+}++static bool amd_wmma_available(const int cc) {+ return (GGML_HIP_CC_IS_RDNA4(cc) || GGML_HIP_CC_IS_RDNA3(cc));+}++static constexpr __device__ int ggml_hip_get_physical_warp_size() {+#if (defined(__GFX9__) || defined(__GFX8__))+ return 64;+#else+ return 32;+#endif // (defined(__GFX9__) || defined(__GFX8__))+}++// Maximum number of bytes that can be copied in a single instruction.+static constexpr __device__ int ggml_hip_get_max_cpy_bytes() {+ return 16;+}+++[[noreturn]]+static __device__ void no_device_code(+ const char * file_name, const int line, const char * function_name, const char * arch) {++ printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %s.\n",+ file_name, line, function_name, arch);+ __trap();++ GGML_UNUSED(no_device_code); // suppress unused function warning++}++#define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, "gfx1151")++// The compiler is always able to unroll loops if they contain continue expressions.+// In such cases loop unrolling can still be achieved via recursion:+template <int n>+struct ggml_hip_unroll {+ template <typename Func, typename... Args>+ __device__ void operator()(const Func & f, Args... args) const {+ f(n - 1, args...);+ ggml_hip_unroll<n - 1>{}(f, args...);+ }+};++template <>+struct ggml_hip_unroll<1> {+ template <typename Func, typename... Args>+ __device__ void operator()(const Func & f, Args... args) const {+ f(0, args...);+ }+};++// Lane i receives lane (i ^ offset)'s value, as __shfl_xor does for a full+// wave. On gfx1151 this is a DPP row_xmask (offsets below 16) or+// v_permlanex16 (16): a register move, not a round trip through the LDS+// crossbar (ds_bpermute). Only the transport differs, so sums stay bitwise.+template<int offset>+static __device__ __forceinline__ int xor_lane(int x) {+#if defined(__gfx1151__)+ if constexpr (offset < 16) {+ return __builtin_amdgcn_update_dpp(0, x, 0x160 | offset, 0xF, 0xF, false);+ } else if constexpr (offset == 16) {+ return __builtin_amdgcn_permlanex16(x, x, 0x76543210, 0xfedcba98, false, false);+ }+#endif+ return __shfl_xor(x, offset, WARP_SIZE);+}++template<int offset>+static __device__ __forceinline__ float xor_lane(float x) {+ return __builtin_bit_cast(float, xor_lane<offset>(__builtin_bit_cast(int, x)));+}++// The butterfly in the shuffle loop's order (width/2 down to 1).+template<int width, typename T>+static __device__ __forceinline__ T xor_reduce_sum(T x) {+ static_assert(width <= 32, "xor_lane covers offsets up to 16");+ if constexpr (width >= 32) x += xor_lane<16>(x);+ if constexpr (width >= 16) x += xor_lane<8>(x);+ if constexpr (width >= 8) x += xor_lane<4>(x);+ if constexpr (width >= 4) x += xor_lane<2>(x);+ if constexpr (width >= 2) x += xor_lane<1>(x);+ return x;+}++template<int width = WARP_SIZE>+static __device__ __forceinline__ int warp_reduce_sum(int x) {+ return xor_reduce_sum<width>(x);+}++template<int width = WARP_SIZE>+static __device__ __forceinline__ float warp_reduce_sum(float x) {+ return xor_reduce_sum<width>(x);+}++// Reduce-scatter levels for n sums: the first levels of the wave butterfly+// each halve the sums a lane carries.+constexpr int ScatterLevels(int n) { return n <= 1 ? 0 : n <= 2 ? 1 : n <= 4 ? 2 : 3; }++// One butterfly level that keeps half of `count` sums: the lane on the+// `offset` side keeps the upper half and sends the lower, its partner the+// reverse, so each kept sum adds the partner's copy of the same sum.+template<int offset, int count>+static __device__ __forceinline__ void scatter_level(float* s, int lane) {+ const bool upper = (lane & offset) != 0;+#pragma unroll+ for (int i = 0; i < count / 2; ++i) {+ const float keep = upper ? s[count / 2 + i] : s[i];+ const float send = upper ? s[i] : s[count / 2 + i];+ s[i] = keep + xor_lane<offset>(send);+ }+}++// warp_reduce_sum<32> over n sums at once: the same pairs (16, 8, 4, 2, 1)+// and operands as the full butterfly, which leaves every lane with identical+// bits, but lane l only finishes sum l >> (5 - ScatterLevels(n)).+template<int n>+static __device__ __forceinline__ float scatter_reduce_sum(const float (&v)[n], int lane) {+ constexpr int levels = ScatterLevels(n);+ float s[1 << levels];+#pragma unroll+ for (int j = 0; j < (1 << levels); ++j)+ s[j] = j < n ? v[j] : 0.0f;+ if constexpr (levels == 3) {+ scatter_level<16, 8>(s, lane);+ scatter_level<8, 4>(s, lane);+ scatter_level<4, 2>(s, lane);+ } else if constexpr (levels == 2) {+ scatter_level<16, 4>(s, lane);+ scatter_level<8, 2>(s, lane);+ s[0] += xor_lane<4>(s[0]);+ } else if constexpr (levels == 1) {+ scatter_level<16, 2>(s, lane);+ s[0] += xor_lane<8>(s[0]);+ s[0] += xor_lane<4>(s[0]);+ } else {+ s[0] += xor_lane<16>(s[0]);+ s[0] += xor_lane<8>(s[0]);+ s[0] += xor_lane<4>(s[0]);+ }+ s[0] += xor_lane<2>(s[0]);+ s[0] += xor_lane<1>(s[0]);+ return s[0];+}++template<int width = WARP_SIZE>+static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {+#pragma unroll+ for (int offset = width/2; offset > 0; offset >>= 1) {+ a.x += __shfl_xor(a.x, offset, width);+ a.y += __shfl_xor(a.y, offset, width);+ }+ return a;+}++template<int width = WARP_SIZE>+static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {+#ifdef FP16_AVAILABLE+#pragma unroll+ for (int offset = width/2; offset > 0; offset >>= 1) {+ a = __hadd2(a, __shfl_xor(a, offset, width));+ }+ return a;++#else+ NO_DEVICE_CODE;+ return a;+#endif // FP16_AVAILABLE+}++template<int width = WARP_SIZE>+static __device__ __forceinline__ int warp_reduce_all(int x) {+ if (width == ggml_hip_get_physical_warp_size()) {+ return __all(x);+ } else {+#pragma unroll+ for (int offset = width/2; offset > 0; offset >>= 1) {+ x = __shfl_xor(x, offset, width) && x;+ }+ return x;+ }+}++template<int width = WARP_SIZE>+static __device__ __forceinline__ int warp_reduce_any(int x) {+ if (width == ggml_hip_get_physical_warp_size()) {+ return __any(x);+ } else {+#pragma unroll+ for (int offset = width/2; offset > 0; offset >>= 1) {+ x = __shfl_xor(x, offset, width) || x;+ }+ return x;+ }+}++template<int width = WARP_SIZE>+static __device__ __forceinline__ float warp_reduce_max(float x) {+#pragma unroll+ for (int offset = width/2; offset > 0; offset >>= 1) {+ x = fmaxf(x, __shfl_xor(x, offset, width));+ }+ return x;+}++template<typename T, int width = WARP_SIZE>+static __device__ __forceinline__ T warp_prefix_inclusive_sum(T x) {+ const int lane_id = threadIdx.x % width;+#pragma unroll+ for (int offset = 1; offset < width; offset <<= 1) {+ const T t = __shfl_up(x, offset, width);+ if (lane_id >= offset) {+ x += t;+ }+ }+ return x;+}++template<int width = WARP_SIZE>+static __device__ __forceinline__ float2 warp_prefix_inclusive_sum(float2 a) {+ const int lane_id = threadIdx.x % width;+#pragma unroll+ for (int offset = 1; offset < width; offset <<= 1) {+ const float t_x = __shfl_up(a.x, offset, width);+ const float t_y = __shfl_up(a.y, offset, width);+ if (lane_id >= offset) {+ a.x += t_x;+ a.y += t_y;+ }+ }+ return a;+}++template<int width = WARP_SIZE>+static __device__ __forceinline__ half2 warp_prefix_inclusive_sum(half2 a) {+#ifdef FP16_AVAILABLE+ const int lane_id = threadIdx.x % width;+#pragma unroll+ for (int offset = 1; offset < width; offset <<= 1) {+ const half2 t = __shfl_up(a, offset, width);+ if (lane_id >= offset) {+ a = __hadd2(a, t);+ }+ }+ return a;++#else+ NO_DEVICE_CODE;+ return a;+#endif // FP16_AVAILABLE+}++enum class block_reduce_method {+ MAX,+ SUM,+};++template<block_reduce_method method_t, typename T>+struct block_reduce_policy;++template <typename T, typename... Ts>+inline constexpr bool is_any = (std::is_same_v<T, Ts> || ...);++template<typename...>+inline constexpr bool ggml_hip_dependent_false_v = false;++template <typename T> struct block_reduce_policy<block_reduce_method::SUM, T> {+ static __device__ T reduce(T val) {+ if constexpr(is_any<T, float, float2, half2, int>) {+ return warp_reduce_sum(val);+ } else {+ static_assert(ggml_hip_dependent_false_v<T>, "Unsupported type for block reduce sum");+ }+ }++ static __device__ T sentinel() {+ if constexpr (std::is_same_v<T, float>) {+ return 0.0f;+ } else if constexpr (std::is_same_v<T, float2>) {+ return make_float2(0.0f, 0.0f);+ } else if constexpr (std::is_same_v<T, half2>) {+ return make_half2(0.0f, 0.0f);+ } else if constexpr (std::is_same_v<T, int>) {+ return 0;+ } else {+ static_assert(ggml_hip_dependent_false_v<T>, "Unsupported type for block reduce sum");+ }+ }+};++template <typename T> struct block_reduce_policy<block_reduce_method::MAX, T> {+ static __device__ T reduce(T val) {+ if constexpr (is_any<T, float, half2>) {+ return warp_reduce_max(val);+ } else {+ static_assert(ggml_hip_dependent_false_v<T>, "Unsupported type for block reduce max");+ }+ }++ static __device__ T sentinel() {+ if constexpr (std::is_same_v<T, float>) {+ return -INFINITY;+ } else if constexpr (std::is_same_v<T, half2>) {+ return make_half2(-INFINITY, -INFINITY);+ } else {+ static_assert(ggml_hip_dependent_false_v<T>, "Unsupported type for block reduce max");+ }+ }+};++template <block_reduce_method reduce_method_t, const unsigned int block_size_template = 0, typename T>+static __device__ T block_reduce(T val, T * shared_vals) {+ val = block_reduce_policy<reduce_method_t, T>::reduce(val);+ const unsigned int block_size = block_size_template == 0 ? blockDim.x : block_size_template;+ if (block_size > WARP_SIZE) {+ assert((block_size <= 1024) && (block_size % WARP_SIZE) == 0);+ const int warp_id = threadIdx.x / WARP_SIZE;+ const int lane_id = threadIdx.x % WARP_SIZE;+ if (lane_id == 0) {+ shared_vals[warp_id] = val;+ }+ __syncthreads();+ val = block_reduce_policy<reduce_method_t, T>::sentinel();+ if (lane_id < (static_cast<int>(block_size) / WARP_SIZE)) {+ val = shared_vals[lane_id];+ }+ return block_reduce_policy<reduce_method_t, T>::reduce(val);+ }++ return val;+}++static __device__ __forceinline__ half ggml_hip_hmax(const half a, const half b) {+#ifdef FP16_AVAILABLE++ return __hmax(a, b);++#else+ NO_DEVICE_CODE;+ GGML_UNUSED(b);+ return a;+#endif // FP16_AVAILABLE+}++static __device__ __forceinline__ half2 ggml_hip_hmax2(const half2 a, const half2 b) {+ return half2(__hmax(a.x, b.x), __hmax(a.y, b.y));+}++template<int width = WARP_SIZE>+static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {+#pragma unroll+ for (int offset = width/2; offset > 0; offset >>= 1) {+ x = ggml_hip_hmax2(x, __shfl_xor(x, offset, width));+ }+ return x;+}++static __device__ __forceinline__ uint32_t __hgt2_mask(const half2 a, const half2 b) {+ const uint32_t mask_low = 0x0000FFFF * (float( __low2half(a)) > float( __low2half(b)));+ const uint32_t mask_high = 0xFFFF0000 * (float(__high2half(a)) > float(__high2half(b)));+ return mask_low | mask_high;+}++static __device__ __forceinline__ int ggml_hip_dp4a(const int a, const int b, int c) {+#if defined(CDNA) || defined(RDNA2) || defined(__gfx906__)+ c = __builtin_amdgcn_sdot4(a, b, c, false);+#elif defined(RDNA3) || defined(RDNA4)+ c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);+#elif defined(RDNA1) || defined(__gfx900__)+ int tmp1;+ int tmp2;+ asm("\n \+ v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0 src1_sel:BYTE_0 \n \+ v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_1 src1_sel:BYTE_1 \n \+ v_add3_u32 %0, %1, %2, %0 \n \+ v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_2 src1_sel:BYTE_2 \n \+ v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_3 src1_sel:BYTE_3 \n \+ v_add3_u32 %0, %1, %2, %0 \n \+ "+ : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)+ : "v"(a), "v"(b)+ );+#else+ const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);+ const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);+ c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];+#endif+ return c;++}++static __device__ __forceinline__ void ggml_hip_mad(float & acc, const float v, const float u) {+ acc += v*u;+}++static __device__ __forceinline__ void ggml_hip_mad(float & acc, const float2 v, const float2 u) {+ acc += v.x*u.x;+ acc += v.y*u.y;+}++#if (defined(RDNA2) || defined(RDNA3) || defined(RDNA4) || defined(__gfx906__) || defined(CDNA))+#define V_DOT2_F32_F16_AVAILABLE+#endif // (defined(RDNA2) || defined(RDNA3) || defined(RDNA4) || defined(__gfx906__) || defined(CDNA))++static __device__ __forceinline__ void ggml_hip_mad(float & acc, const half2 v, const half2 u) {+#ifdef V_DOT2_F32_F16_AVAILABLE+ asm volatile("v_dot2_f32_f16 %0, %1, %2, %0" : "+v"(acc) : "v"(v), "v"(u));+#else+#ifdef FAST_FP16_AVAILABLE+ const float2 tmp = __half22float2(v*u);+ acc += tmp.x + tmp.y;+#else+ const float2 tmpv = __half22float2(v);+ const float2 tmpu = __half22float2(u);+ acc += tmpv.x * tmpu.x;+ acc += tmpv.y * tmpu.y;+#endif // FAST_FP16_AVAILABLE+#endif // V_DOT2_F32_F16_AVAILABLE+}++static __device__ __forceinline__ void ggml_hip_mad(half2 & acc, const half2 v, const half2 u) {+#ifdef FAST_FP16_AVAILABLE+ acc += v*u;+#else+ const float2 tmpv = __half22float2(v);+ const float2 tmpu = __half22float2(u);+ float2 tmpacc = __half22float2(acc);+ tmpacc.x += tmpv.x * tmpu.x;+ tmpacc.y += tmpv.y * tmpu.y;+ acc = make_half2(tmpacc.x, tmpacc.y);+#endif // FAST_FP16_AVAILABLE+}++// Aligned memory transfers of 8/16 bytes can be faster than 2 transfers with 4 bytes, especially on AMD.+// Important: do not use this function if dst and src both point at registers.+// Due to the strict aliasing rule the compiler can do incorrect optimizations if src and dst have different types.+// The function is intended for copies between registers and SRAM/VRAM to make the compiler emit the right instructions.+// If dst and src point at different address spaces then they are guaranteed to not be aliased.+template <int nbytes, int alignment = 0>+static __device__ __forceinline__ void ggml_hip_memcpy_1(void * __restrict__ dst, const void * __restrict__ src) {+ static_assert(+ nbytes <= ggml_hip_get_max_cpy_bytes() || alignment == 0,+ "You are misusing the alignment parameter for ggml_hip_memcpy_1. "+ "The intent is for the parameter is only as a workaround if either one of the pointers is not properly aligned. "+ "If you use it to do more bytes per copy than ggml_hip_max_cpy_bytes() the reads and writes may not be coalesced. "+ "Call ggml_hip_memcpy_1 in a loop instead.");+ if constexpr (alignment != 0) {+ static_assert(nbytes % alignment == 0, "bad alignment");+ }+ constexpr int nb_per_cpy = alignment == 0 ? nbytes : alignment;++#pragma unroll+ for (int i = 0; i < nbytes/nb_per_cpy; ++i) {+ if constexpr (nb_per_cpy == 1) {+ ((char *) dst)[i] = ((const char *) src)[i];+ } else if constexpr (nb_per_cpy == 2) {+ ((short *) dst)[i] = ((const short *) src)[i];+ } else if constexpr (nb_per_cpy == 4) {+ ((int *) dst)[i] = ((const int *) src)[i];+ } else if constexpr (nb_per_cpy == 8) {+ ((int2 *) dst)[i] = ((const int2 *) src)[i];+ } else if constexpr (nb_per_cpy == 16) {+ ((int4 *) dst)[i] = ((const int4 *) src)[i];+ } else {+ static_assert(nbytes == 0 && nbytes == -1, "bad nbytes");+ }+ }+}++static __device__ __forceinline__ float ggml_hip_e8m0_to_fp32(uint8_t x) {+ uint32_t bits;+ if (x == 0) {+ bits = 0x00400000;+ } else {+ bits = (uint32_t) x << 23;+ }++ float result;+ memcpy(&result, &bits, sizeof(float));+ return result;+}++static __device__ __forceinline__ float ggml_hip_ue4m3_to_fp32(uint8_t x) {+#if defined(CDNA3) && defined(FP8_AVAILABLE) && HIP_VERSION >= 60200000+ // ROCm does not support fp8 in software on devices with fp8 hardware,+ // but CDNA3 supports only e4m3_fnuz (no inf).+ const uint32_t bits = x * (x != 0x7F && x != 0xFF); // Convert NaN to 0.0f to match CPU implementation.+ const __hip_fp8_e4m3_fnuz xf = *reinterpret_cast<const __hip_fp8_e4m3_fnuz *>(&bits);+ return static_cast<float>(xf) / 2;+#else+ if (x == 0 || (x == 0x7F && x != 0xFF)) { // Convert NaN to 0.0f+ return 0.0f;+ }+ const int exp = (x >> 3) & 0xF;+ const int man = x & 0x7;+ float raw;+ if (exp == 0) {+ raw = ldexpf((float) man, -9);+ } else {+ raw = ldexpf(1.0f + (float) man / 8.0f, exp - 7);+ }+ return static_cast<float>(raw / 2);+#endif // defined(CDNA3) && defined(FP8_AVAILABLE) && HIP_VERSION >= 60200000+}++static __device__ __forceinline__ uint8_t ggml_hip_fp32_to_ue4m3(float x) {+ NO_DEVICE_CODE; // Used only for NVFP4 Scales for Activations, only for Blackwell+}++__device__ __forceinline__ uint8_t ggml_hip_float_to_fp4_e2m1(float x, float e) {+ const uint8_t sign_bit = (x < 0.0f) << 3;+ float ax = fabsf(x) * e;++ // Positive LUT+ static constexpr float pos_lut[8] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f };++ int best_i = 0;+ float best_err = fabsf(ax - pos_lut[0]);++#pragma unroll+ for (int i = 1; i < 8; ++i) {+ const float err = fabsf(ax - pos_lut[i]);+ if (err < best_err) {+ best_err = err;+ best_i = i;+ }+ }++ return static_cast<uint8_t>(best_i | sign_bit);+}++// See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1.+// Precompute mp (m' in the paper) and L such that division+// can be computed using a multiply (high 32b of 64b result)+// and a shift:+//+// n/d = (mulhi(n, mp) + n) >> L;+static const uint3 init_fastdiv_values(uint64_t d_64) {+ GGML_ASSERT(d_64 != 0);+ GGML_ASSERT(d_64 <= std::numeric_limits<uint32_t>::max());++ uint32_t d = (uint32_t)d_64;++ // compute L = ceil(log2(d));+ uint32_t L = 0;+ while (L < 32 && (uint32_t{ 1 } << L) < d) {+ L++;+ }++ uint32_t mp = (uint32_t) ((uint64_t{ 1 } << 32) * ((uint64_t{ 1 } << L) - d) / d + 1);+ // pack divisor as well to reduce error surface+ return make_uint3(mp, L, d);+}++static __device__ __forceinline__ uint32_t fastdiv(uint32_t n, const uint3 fastdiv_values) {+ // expects fastdiv_values to contain <mp, L, divisor> in <x, y, z>+ // fastdiv_values.z is unused and optimized away by the compiler.+ // Compute high 32 bits of n * mp+ const uint32_t hi = __umulhi(n, fastdiv_values.x);+ // add n, apply bit shift+ return (hi + n) >> fastdiv_values.y;+}++static __device__ __forceinline__ uint32_t fastmodulo(uint32_t n, const uint3 fastdiv_values) {+ // expects fastdiv_values to contain <mp, L, divisor> in <x, y, z> (see init_fastdiv_values)+ return n - fastdiv(n, fastdiv_values) * fastdiv_values.z;+}++// Calculate both division and modulo at once, returns <n/divisor, n%divisor>+static __device__ __forceinline__ uint2 fast_div_modulo(uint32_t n, const uint3 fastdiv_values) {+ // expects fastdiv_values to contain <mp, L, divisor> in <x, y, z> (see init_fastdiv_values)+ const uint32_t div_val = fastdiv(n, fastdiv_values);+ const uint32_t mod_val = n - div_val * fastdiv_values.z;+ return make_uint2(div_val, mod_val);+}++typedef void (*dequantize_kernel_t)(const void * vx, const int64_t ib, const int iqs, float2 & v);++static __device__ __forceinline__ float get_alibi_slope(+ const float max_bias, const uint32_t h, const uint32_t n_head_log2, const float m0, const float m1+) {+ if (max_bias <= 0.0f) {+ return 1.0f;+ }+ const float base = h < n_head_log2 ? m0 : m1;+ const int exph = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;++ return powf(base, exph);+}++template <ggml_type type>+struct ggml_hip_type_traits;++template<>+struct ggml_hip_type_traits<GGML_TYPE_F16> {+ static constexpr int qk = 1;+ static constexpr int qr = 1;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q1_0> {+ static constexpr int qk = QK1_0;+ static constexpr int qr = QR1_0;+ static constexpr int qi = QI1_0;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q4_0> {+ static constexpr int qk = QK4_0;+ static constexpr int qr = QR4_0;+ static constexpr int qi = QI4_0;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q4_1> {+ static constexpr int qk = QK4_1;+ static constexpr int qr = QR4_1;+ static constexpr int qi = QI4_1;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q5_0> {+ static constexpr int qk = QK5_0;+ static constexpr int qr = QR5_0;+ static constexpr int qi = QI5_0;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q5_1> {+ static constexpr int qk = QK5_1;+ static constexpr int qr = QR5_1;+ static constexpr int qi = QI5_1;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q8_0> {+ static constexpr int qk = QK8_0;+ static constexpr int qr = QR8_0;+ static constexpr int qi = QI8_0;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_MXFP4> {+ static constexpr int qk = QK_MXFP4;+ static constexpr int qr = QR_MXFP4;+ static constexpr int qi = QI_MXFP4;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_NVFP4> {+ static constexpr int qk = QK_NVFP4;+ static constexpr int qr = QR_NVFP4;+ static constexpr int qi = QI_NVFP4;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q2_K> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR2_K;+ static constexpr int qi = QI2_K;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q3_K> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR3_K;+ static constexpr int qi = QI3_K;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q4_K> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR4_K;+ static constexpr int qi = QI4_K;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q5_K> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR5_K;+ static constexpr int qi = QI5_K;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_Q6_K> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR6_K;+ static constexpr int qi = QI6_K;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_IQ2_XXS> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR2_XXS;+ static constexpr int qi = QI2_XXS;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_IQ2_XS> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR2_XS;+ static constexpr int qi = QI2_XS;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_IQ2_S> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR2_S;+ static constexpr int qi = QI2_S;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_IQ3_XXS> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR3_XXS;+ static constexpr int qi = QI3_XXS;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_IQ1_S> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR1_S;+ static constexpr int qi = QI1_S;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_IQ1_M> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR1_M;+ static constexpr int qi = QI1_M;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_IQ4_NL> {+ static constexpr int qk = QK4_NL;+ static constexpr int qr = QR4_NL;+ static constexpr int qi = QI4_NL;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_IQ4_XS> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR4_XS;+ static constexpr int qi = QI4_XS;+};++template<>+struct ggml_hip_type_traits<GGML_TYPE_IQ3_S> {+ static constexpr int qk = QK_K;+ static constexpr int qr = QR3_S;+ static constexpr int qi = QI3_S;+};++//////////////////////++struct ggml_hip_device_info {+ int device_count;++ struct hip_device_info {+ int cc; // compute capability+ int nsm; // number of streaming multiprocessors+ size_t smpb; // max. shared memory per block+ size_t smpbo; // max. shared memory per block (with opt-in)+ bool integrated; // Device is integrated as opposed to discrete+ bool vmm; // virtual memory support+ size_t vmm_granularity; // granularity of virtual memory+ size_t total_vram;+ int warp_size; // Number of threads in a dispatch+ bool supports_cooperative_launch; // whether cooperative launch is supported+ };++ hip_device_info devices[GGML_HIP_MAX_DEVICES] = {};++ std::array<float, GGML_HIP_MAX_DEVICES> default_tensor_split = {};+};++const ggml_hip_device_info & ggml_hip_info();++void ggml_hip_set_device(int device);+int ggml_hip_get_device();++struct ggml_hip_pool {+ virtual ~ggml_hip_pool() = default;++ virtual void * alloc(size_t size, size_t * actual_size) = 0;+ virtual void free(void * ptr, size_t size) = 0;+};++template<typename T>+struct ggml_hip_pool_alloc {+ ggml_hip_pool * pool = nullptr;+ T * ptr = nullptr;+ size_t actual_size = 0;++ ggml_hip_pool_alloc() = default;++ explicit ggml_hip_pool_alloc(ggml_hip_pool & pool) : pool(&pool) {+ }++ ggml_hip_pool_alloc(ggml_hip_pool & pool, size_t size) : pool(&pool) {+ alloc(size);+ }++ ~ggml_hip_pool_alloc() {+ if (ptr != nullptr) {+ pool->free(ptr, actual_size);+ }+ }++ // size is in number of elements+ T * alloc(size_t size) {+ GGML_ASSERT(pool != nullptr);+ GGML_ASSERT(ptr == nullptr);+ ptr = (T *) pool->alloc(size * sizeof(T), &this->actual_size);+ return ptr;+ }++ T * alloc(ggml_hip_pool & pool, size_t size) {+ this->pool = &pool;+ return alloc(size);+ }++ T * get() {+ return ptr;+ }++ ggml_hip_pool_alloc(const ggml_hip_pool_alloc &) = delete;+ ggml_hip_pool_alloc(ggml_hip_pool_alloc &&) = delete;+ ggml_hip_pool_alloc& operator=(const ggml_hip_pool_alloc &) = delete;+ ggml_hip_pool_alloc& operator=(ggml_hip_pool_alloc &&) = delete;+};+++// backend interface++struct ggml_tensor_extra_gpu {+ void * data_device[GGML_HIP_MAX_DEVICES]; // 1 pointer for each device for split tensors+ hipEvent_t events[GGML_HIP_MAX_DEVICES][GGML_HIP_MAX_STREAMS]; // events for synchronizing multiple GPUs+};+++struct ggml_hip_graph {+};++struct ggml_hip_concurrent_event {+ std::vector<hipEvent_t> join_events;+ hipEvent_t fork_event = nullptr;++ int n_streams = 0;+ std::unordered_map<const ggml_tensor *, int> stream_mapping;++ // Original order of nodes in this concurrent region (before interleaving)+ // Used to restore grouping for fusion within streams+ std::vector<const ggml_tensor *> original_order;++ const ggml_tensor * join_node;++ ggml_hip_concurrent_event() = default;++ ggml_hip_concurrent_event(const ggml_hip_concurrent_event &) = delete;+ ggml_hip_concurrent_event & operator=(const ggml_hip_concurrent_event &) = delete;++ explicit ggml_hip_concurrent_event(int n_streams) : n_streams(n_streams) {+ join_events.resize(n_streams);++ for (size_t i = 0; i < join_events.size(); ++i) {+ HIP_CHECK(hipEventCreateWithFlags(&join_events[i], hipEventDisableTiming));+ }++ HIP_CHECK(hipEventCreateWithFlags(&fork_event, hipEventDisableTiming));+ }++ ggml_hip_concurrent_event(ggml_hip_concurrent_event && other) noexcept+ : join_events(std::move(other.join_events))+ , fork_event(other.fork_event)+ , n_streams(other.n_streams)+ , stream_mapping(std::move(other.stream_mapping))+ , original_order(std::move(other.original_order))+ , join_node(other.join_node) {+ other.fork_event = nullptr;+ }++ // 1. check if any branches write to overlapping memory ranges (except the join node)+ // 2. check whether all srcs are either within the branch or outside the nodes covered by ggml_hip_concurrent_event+ // we assume all nodes have the same buffer+ bool is_valid() const {+ std::vector<std::vector<std::pair<int64_t, int64_t>>> write_ranges;+ write_ranges.resize(n_streams);++ // get join_node's memory range to exclude from overlap checking.+ // multiple nodes can use join_node's buffer; we synchronize on the join node.+ const ggml_tensor * join_t = join_node->view_src ? join_node->view_src : join_node;+ const int64_t join_start = (int64_t) join_t->data;+ const int64_t join_end = join_start + ggml_nbytes(join_t);++ for (const auto & [tensor, stream] : stream_mapping) {+ const ggml_tensor * t = tensor->view_src ? tensor->view_src : tensor;+ const int64_t t_start = (int64_t) t->data;+ const int64_t t_end = t_start + ggml_nbytes(t);++ // skip tensors that overlap with join_node's buffer.+ if ((t_start <= join_start && join_start < t_end) || (join_start <= t_start && t_start < join_end)) {+ continue;+ }++ // concurrent streams begin from 1+ write_ranges[stream - 1].emplace_back(t_start, t_end);+ }++ for (int i = 0; i < n_streams; ++i) {+ // sorts first by start then by end of write range+ std::sort(write_ranges[i].begin(), write_ranges[i].end());+ }++ bool writes_overlap = false;+ bool dependent_srcs = false;+ for (const auto & [tensor, stream] : stream_mapping) {+ const ggml_tensor * t = tensor->view_src ? tensor->view_src : tensor;+ const int64_t t_start = (int64_t) t->data;+ const int64_t t_end = t_start + ggml_nbytes(t);++ // skip tensors that overlap with join_node's buffer+ if ((t_start <= join_start && join_start < t_end) || (join_start <= t_start && t_start < join_end)) {+ continue;+ }++ // check if this buffer's write data overlaps with another stream's+ std::pair<int64_t, int64_t> data_range = std::make_pair(t_start, t_end);+ for (int i = 0; i < n_streams; ++i) {+ if (i == stream - 1) {+ continue;+ }+ auto it = std::lower_bound(write_ranges[i].begin(), write_ranges[i].end(), data_range);++ if (it != write_ranges[i].end()) {+ const std::pair<int64_t, int64_t> & other = *it;++ // std::lower_bound returns the first element where other >= data_range (lexicographically).+ // This guarantees other.first >= data_range.first.+ // Therefore, overlap occurs iff other.first < data_range.second+ // (i.e., the other range starts before this range ends).+ if (other.first < data_range.second) {+ GGML_LOG_DEBUG("Writes overlap for %s", tensor->name);+ writes_overlap = true;+ break;+ }+ }+ }++ //check if all srcs are either in branch or don't have a branch+ for (int i = 0; i < GGML_MAX_SRC; ++i) {+ if (!tensor->src[i]) {+ continue;+ }++ auto it = stream_mapping.find(tensor->src[i]);++ if (it == stream_mapping.end()) {+ continue;+ }++ if (it->second != stream) {+ dependent_srcs = true;+ break;+ }+ }++ if (dependent_srcs || writes_overlap) {+ break;+ }+ }++ return !writes_overlap && !dependent_srcs;+ }++ ~ggml_hip_concurrent_event() {+ if (fork_event != nullptr) {+ HIP_CHECK(hipEventDestroy(fork_event));+ }+ for (hipEvent_t e : join_events) {+ if (e != nullptr) {+ HIP_CHECK(hipEventDestroy(e));+ }+ }+ }+};++struct ggml_hip_stream_context {+ std::unordered_map<const ggml_tensor *, ggml_hip_concurrent_event> concurrent_events;++ void reset() {+ concurrent_events.clear();+ }+};++struct ggml_backend_hip_context {+ int device;+ std::string name;+ hipEvent_t copy_event = nullptr;++ hipStream_t streams[GGML_HIP_MAX_DEVICES][GGML_HIP_MAX_STREAMS] = { { nullptr } };+ hipblasHandle_t hipblas_handles[GGML_HIP_MAX_DEVICES] = {nullptr};++ int curr_stream_no = 0;++ explicit ggml_backend_hip_context(int device) :+ device(device),+ name(GGML_HIP_NAME + std::to_string(device)) {+ }++ ggml_hip_stream_context concurrent_stream_context;++ ~ggml_backend_hip_context();++ hipStream_t stream(int device, int stream) {+ if (streams[device][stream] == nullptr) {+ ggml_hip_set_device(device);+ HIP_CHECK(hipStreamCreateWithFlags(&streams[device][stream], hipStreamNonBlocking));+ }+ return streams[device][stream];+ }++ hipStream_t stream() { return stream(device, curr_stream_no); }++ ggml_hip_stream_context & stream_context() { return concurrent_stream_context; }++ hipblasHandle_t hipblas_handle(int device) {+ if (hipblas_handles[device] == nullptr) {+ ggml_hip_set_device(device);+ HIPBLAS_CHECK(hipblasCreate(&hipblas_handles[device]));+ HIPBLAS_CHECK(hipblasSetMathMode(hipblas_handles[device], HIPBLAS_TF32_TENSOR_OP_MATH));+ }+ return hipblas_handles[device];+ }++ hipblasHandle_t hipblas_handle() {+ return hipblas_handle(device);+ }++ // pool+ std::unique_ptr<ggml_hip_pool> pools[GGML_HIP_MAX_DEVICES][GGML_HIP_MAX_STREAMS];++ static std::unique_ptr<ggml_hip_pool> new_pool_for_device(int device, int stream_no);++ ggml_hip_pool & pool(int device) {+ if (pools[device][curr_stream_no] == nullptr) {+ pools[device][curr_stream_no] = new_pool_for_device(device, curr_stream_no);+ }+ return *pools[device][curr_stream_no];+ }++ ggml_hip_pool & pool() {+ return pool(device);+ }+};++// The scratch pool for kernels queued on `stream` (one per device and stream).+ggml_hip_pool & qfn_pool_for_stream(int device, hipStream_t stream);++struct ggml_hip_mm_fusion_args_host {+ const ggml_tensor * x_bias = nullptr;+ const ggml_tensor * gate = nullptr;+ const ggml_tensor * gate_bias = nullptr;+ ggml_glu_op glu_op;+};+struct ggml_hip_mm_fusion_args_device {+ const void * x_bias = nullptr;+ const void * gate = nullptr;+ const void * gate_bias = nullptr;+ ggml_glu_op glu_op;+};diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/ggml-common.h b/exllamav3/exllamav3_ext/rocm/gmmq/ggml-common.hnew file mode 100644index 0000000..82bb02e--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/ggml-common.h@@ -0,0 +1,1294 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/ggml-common.h, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+#ifndef GGML_COMMON_DECL++#include <hip/hip_fp16.h>+#include <cstdint>++typedef half ggml_half;+typedef half2 ggml_half2;++#define GGML_COMMON_AGGR_U+#define GGML_COMMON_AGGR_S data++#define GGML_COMMON_DECL++#if defined(GGML_COMMON_DECL)++#ifndef __cplusplus+#ifndef static_assert+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201100L)+#define static_assert(cond, msg) _Static_assert(cond, msg)+#else+#define static_assert(cond, msg) struct global_scope_noop_trick+#endif+#endif+#endif // __cplusplus++// QK = number of values after dequantization+// QK_K = super-block size++#define QK_K 256+#define K_SCALE_SIZE 12++// QR = QK / number of values before dequantization+// QI = number of 32 bit integers before dequantization++#define QI1_0 (QK1_0 / 32)+#define QR1_0 1+++#define QI4_0 (QK4_0 / (4 * QR4_0))+#define QR4_0 2++#define QI4_1 (QK4_1 / (4 * QR4_1))+#define QR4_1 2++#define QI_MXFP4 (QK_MXFP4 / (4 * QR_MXFP4))+#define QR_MXFP4 2++#define QI_NVFP4 (QK_NVFP4 / (4 * QR_NVFP4))+#define QR_NVFP4 2++#define QI5_0 (QK5_0 / (4 * QR5_0))+#define QR5_0 2++#define QI5_1 (QK5_1 / (4 * QR5_1))+#define QR5_1 2++#define QI8_0 (QK8_0 / (4 * QR8_0))+#define QR8_0 1++#define QI8_1 (QK8_1 / (4 * QR8_1))+#define QR8_1 1++#define QI2_K (QK_K / (4*QR2_K))+#define QR2_K 4++#define QI3_K (QK_K / (4*QR3_K))+#define QR3_K 4++#define QI4_K (QK_K / (4*QR4_K))+#define QR4_K 2++#define QI5_K (QK_K / (4*QR5_K))+#define QR5_K 2++#define QI6_K (QK_K / (4*QR6_K))+#define QR6_K 2++#define QI2_XXS (QK_K / (4*QR2_XXS))+#define QR2_XXS 4++#define QI2_XS (QK_K / (4*QR2_XS))+#define QR2_XS 4++#define QI2_S (QK_K / (4*QR2_S))+#define QR2_S 4++#define QI3_XXS (QK_K / (4*QR3_XXS))+#define QR3_XXS 4++#define QI3_XS (QK_K / (4*QR3_XS))+#define QR3_XS 4++#define QI1_S (QK_K / (4*QR1_S))+#define QR1_S 8++#define QI1_M (QK_K / (4*QR1_M))+#define QR1_M 8++#define QI4_NL (QK4_NL / (4*QR4_NL))+#define QR4_NL 2++#define QI4_XS (QK_K / (4*QR4_XS))+#define QR4_XS 2++#define QI3_S (QK_K / (4*QR3_S))+#define QR3_S 4++#ifdef _MSC_VER+#define GGML_EXTENSION+#else // _MSC_VER+#define GGML_EXTENSION __extension__+#endif // _MSC_VER++#define QK1_0 128+typedef struct {+ ggml_half d; // delta+ uint8_t qs[QK1_0 / 8]; // bits / quants+} block_q1_0;+static_assert(sizeof(block_q1_0) == sizeof(ggml_half) + QK1_0 / 8, "wrong q1_0 block size/padding");++#define QK4_0 32+typedef struct {+ ggml_half d; // delta+ uint8_t qs[QK4_0 / 2]; // nibbles / quants+} block_q4_0;+static_assert(sizeof(block_q4_0) == sizeof(ggml_half) + QK4_0 / 2, "wrong q4_0 block size/padding");++#define QK4_1 32+typedef struct {+ GGML_EXTENSION union {+ struct {+ ggml_half d; // delta+ ggml_half m; // min+ } GGML_COMMON_AGGR_S;+ ggml_half2 dm;+ } GGML_COMMON_AGGR_U;+ uint8_t qs[QK4_1 / 2]; // nibbles / quants+} block_q4_1;+static_assert(sizeof(block_q4_1) == 2 * sizeof(ggml_half) + QK4_1 / 2, "wrong q4_1 block size/padding");++#define QK_MXFP4 32+typedef struct {+ uint8_t e; // E8M0+ uint8_t qs[QK_MXFP4/2];+} block_mxfp4;+static_assert(sizeof(block_mxfp4) == sizeof(uint8_t) + QK_MXFP4/2, "wrong mxfp4 block size/padding");++#define QK_NVFP4 64+#define QK_NVFP4_SUB 16 // sub-block size for per-group scales+typedef struct {+ uint8_t d[QK_NVFP4/QK_NVFP4_SUB]; // UE4M3 scales (4 bytes, one per 16-element sub-block)+ uint8_t qs[QK_NVFP4/2]; // packed 4-bit E2M1 values (32 bytes)+} block_nvfp4;+static_assert(sizeof(block_nvfp4) == sizeof(uint8_t)*(QK_NVFP4/QK_NVFP4_SUB) + QK_NVFP4/2, "wrong nvfp4 block size/padding");++#define QK5_0 32+typedef struct {+ ggml_half d; // delta+ uint8_t qh[4]; // 5-th bit of quants+ uint8_t qs[QK5_0 / 2]; // nibbles / quants+} block_q5_0;+static_assert(sizeof(block_q5_0) == sizeof(ggml_half) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");++#define QK5_1 32+typedef struct {+ GGML_EXTENSION union {+ struct {+ ggml_half d; // delta+ ggml_half m; // min+ } GGML_COMMON_AGGR_S;+ ggml_half2 dm;+ } GGML_COMMON_AGGR_U;+ uint8_t qh[4]; // 5-th bit of quants+ uint8_t qs[QK5_1 / 2]; // nibbles / quants+} block_q5_1;+static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_half) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");++#define QK8_0 32+typedef struct {+ ggml_half d; // delta+ int8_t qs[QK8_0]; // quants+} block_q8_0;+static_assert(sizeof(block_q8_0) == sizeof(ggml_half) + QK8_0, "wrong q8_0 block size/padding");++#define QK8_1 32+typedef struct {+ GGML_EXTENSION union {+ struct {+ ggml_half d; // delta+ ggml_half s; // d * sum(qs[i])+ } GGML_COMMON_AGGR_S;+ ggml_half2 ds;+ } GGML_COMMON_AGGR_U;+ int8_t qs[QK8_1]; // quants+} block_q8_1;+static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_half) + QK8_1, "wrong q8_1 block size/padding");++//+// Ternary quantization+//++// 1.6875 bpw+typedef struct {+ uint8_t qs[(QK_K - 4 * QK_K / 64) / 5]; // 5 elements per byte (3^5 = 243 < 256)+ uint8_t qh[QK_K/64]; // 4 elements per byte+ ggml_half d;+} block_tq1_0;+static_assert(sizeof(block_tq1_0) == sizeof(ggml_half) + QK_K / 64 + (QK_K - 4 * QK_K / 64) / 5, "wrong tq1_0 block size/padding");++// 2.0625 bpw+typedef struct {+ uint8_t qs[QK_K/4]; // 2 bits per element+ ggml_half d;+} block_tq2_0;+static_assert(sizeof(block_tq2_0) == sizeof(ggml_half) + QK_K / 4, "wrong tq2_0 block size/padding");++//+// Super-block quantization structures+//++// 2-bit quantization+// weight is represented as x = a * q + b+// 16 blocks of 16 elements each+// Effectively 2.625 bits per weight+typedef struct {+ uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits+ uint8_t qs[QK_K/4]; // quants+ GGML_EXTENSION union {+ struct {+ ggml_half d; // super-block scale for quantized scales+ ggml_half dmin; // super-block scale for quantized mins+ } GGML_COMMON_AGGR_S;+ ggml_half2 dm;+ } GGML_COMMON_AGGR_U;+} block_q2_K;+static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_half) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");++// 3-bit quantization+// weight is represented as x = a * q+// 16 blocks of 16 elements each+// Effectively 3.4375 bits per weight+typedef struct {+ uint8_t hmask[QK_K/8]; // quants - high bit+ uint8_t qs[QK_K/4]; // quants - low 2 bits+ uint8_t scales[12]; // scales, quantized with 6 bits+ ggml_half d; // super-block scale+} block_q3_K;+static_assert(sizeof(block_q3_K) == sizeof(ggml_half) + QK_K / 4 + QK_K / 8 + 12, "wrong q3_K block size/padding");++// 4-bit quantization+// 8 blocks of 32 elements each+// weight is represented as x = a * q + b+// Effectively 4.5 bits per weight+typedef struct {+ GGML_EXTENSION union {+ struct {+ ggml_half d; // super-block scale for quantized scales+ ggml_half dmin; // super-block scale for quantized mins+ } GGML_COMMON_AGGR_S;+ ggml_half2 dm;+ } GGML_COMMON_AGGR_U;+ uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits+ uint8_t qs[QK_K/2]; // 4--bit quants+} block_q4_K;+static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_half) + K_SCALE_SIZE + QK_K/2, "wrong q4_K block size/padding");++// 5-bit quantization+// 8 blocks of 32 elements each+// weight is represented as x = a * q + b+// Effectively 5.5 bits per weight+typedef struct {+ GGML_EXTENSION union {+ struct {+ ggml_half d; // super-block scale for quantized scales+ ggml_half dmin; // super-block scale for quantized mins+ } GGML_COMMON_AGGR_S;+ ggml_half2 dm;+ } GGML_COMMON_AGGR_U;+ uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits+ uint8_t qh[QK_K/8]; // quants, high bit+ uint8_t qs[QK_K/2]; // quants, low 4 bits+} block_q5_K;+static_assert(sizeof(block_q5_K) == 2*sizeof(ggml_half) + K_SCALE_SIZE + QK_K/2 + QK_K/8, "wrong q5_K block size/padding");++// 6-bit quantization+// weight is represented as x = a * q+// 16 blocks of 16 elements each+// Effectively 6.5625 bits per weight+typedef struct {+ uint8_t ql[QK_K/2]; // quants, lower 4 bits+ uint8_t qh[QK_K/4]; // quants, upper 2 bits+ int8_t scales[QK_K/16]; // scales, quantized with 8 bits+ ggml_half d; // super-block scale+} block_q6_K;+static_assert(sizeof(block_q6_K) == sizeof(ggml_half) + QK_K / 16 + 3*QK_K/4, "wrong q6_K block size/padding");++// This is only used for intermediate quantization and dot products+typedef struct {+ float d; // delta+ int8_t qs[QK_K]; // quants+ int16_t bsums[QK_K/16]; // sum of quants in groups of 16+} block_q8_K;+static_assert(sizeof(block_q8_K) == sizeof(float) + QK_K + QK_K/16*sizeof(int16_t), "wrong q8_K block size/padding");++// (Almost) "true" 2-bit quantization.+// Due to the need to use blocks as per ggml design, it ends up using+// 2.0625 bpw because of the 16-bit scale for each block of 256.+typedef struct {+ ggml_half d;+ uint16_t qs[QK_K/8];+} block_iq2_xxs;+static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_half) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");++// 2.3125 bpw quants+typedef struct {+ ggml_half d;+ uint16_t qs[QK_K/8];+ uint8_t scales[QK_K/32];+} block_iq2_xs;+static_assert(sizeof(block_iq2_xs) == sizeof(ggml_half) + QK_K/8*sizeof(uint16_t) + QK_K/32, "wrong iq2_xs block size/padding");++// 2.5625 bpw quants+typedef struct {+ ggml_half d;+ uint8_t qs[QK_K/4];+ uint8_t qh[QK_K/32];+ uint8_t scales[QK_K/32];+} block_iq2_s;+static_assert(sizeof(block_iq2_s) == sizeof(ggml_half) + QK_K/4 + QK_K/16, "wrong iq2_s block size/padding");++// (Almost) "true" 3-bit quantization.+// Due to the need to use blocks as per ggml design, it ends up using+// 3.0625 bpw because of the 16-bit scale for each block of 256.+typedef struct {+ ggml_half d;+ uint8_t qs[3*QK_K/8];+} block_iq3_xxs;+static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_half) + 3*(QK_K/8), "wrong iq3_xxs block size/padding");++// 3.4375 bpw+#define IQ3S_N_SCALE QK_K/64+typedef struct {+ ggml_half d;+ uint8_t qs[QK_K/4];+ uint8_t qh[QK_K/32];+ uint8_t signs[QK_K/8];+ uint8_t scales[IQ3S_N_SCALE];+} block_iq3_s;+static_assert(sizeof(block_iq3_s) == sizeof(ggml_half) + 13*(QK_K/32) + IQ3S_N_SCALE, "wrong iq3_s block size/padding");++// 1.5625 bpw+typedef struct {+ ggml_half d;+ uint8_t qs[QK_K/8];+ uint16_t qh[QK_K/32];+} block_iq1_s;+static_assert(sizeof(block_iq1_s) == sizeof(ggml_half) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");++// 1.75 bpw+typedef struct {+ uint8_t qs[QK_K/8]; // grid index, low 8 bits+ uint8_t qh[QK_K/16]; // grid index, high 3 bits + grid shift bit (for two groups of 8)+ uint8_t scales[QK_K/32]; // 3-bit block scales (4-bit if QK_K == 64)+} block_iq1_m;+static_assert(sizeof(block_iq1_m) == QK_K/8 + QK_K/16 + QK_K/32, "wrong iq1_m block size/padding");++// Used by IQ1_M quants+typedef union {+ ggml_half f16;+ uint16_t u16;+} iq1m_scale_t;++// Non-linear quants+#define QK4_NL 32+typedef struct {+ ggml_half d;+ uint8_t qs[QK4_NL/2];+} block_iq4_nl;+static_assert(sizeof(block_iq4_nl) == sizeof(ggml_half) + QK4_NL/2, "wrong iq4_nl block size/padding");++typedef struct {+ ggml_half d;+ uint16_t scales_h;+ uint8_t scales_l[QK_K/64];+ uint8_t qs[QK_K/2];+} block_iq4_xs;+static_assert(sizeof(block_iq4_xs) == sizeof(ggml_half) + sizeof(uint16_t) + QK_K/64 + QK_K/2, "wrong iq4_xs block size/padding");++#endif // GGML_COMMON_DECL+#endif // GGML_COMMON_DECL++////////////////////////////////////////////////////////////////////////////////++#ifndef GGML_COMMON_IMPL++#include <cstdint>++#define GGML_TABLE_BEGIN(type, name, size) static const __device__ type name[size] = {+#define GGML_TABLE_END() };++#define GGML_COMMON_IMPL++#if defined(GGML_COMMON_IMPL)++GGML_TABLE_BEGIN(uint8_t, kmask_iq2xs, 8)+ 1, 2, 4, 8, 16, 32, 64, 128+GGML_TABLE_END()++GGML_TABLE_BEGIN(uint8_t, ksigns_iq2xs, 128)+ 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,+ 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,+ 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,+ 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,+ 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,+ 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,+ 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,+ 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,+GGML_TABLE_END()++GGML_TABLE_BEGIN(uint64_t, ksigns64, 128)+ 0x0000000000000000, 0xff000000000000ff, 0xff0000000000ff00, 0x000000000000ffff,+ 0xff00000000ff0000, 0x0000000000ff00ff, 0x0000000000ffff00, 0xff00000000ffffff,+ 0xff000000ff000000, 0x00000000ff0000ff, 0x00000000ff00ff00, 0xff000000ff00ffff,+ 0x00000000ffff0000, 0xff000000ffff00ff, 0xff000000ffffff00, 0x00000000ffffffff,+ 0xff0000ff00000000, 0x000000ff000000ff, 0x000000ff0000ff00, 0xff0000ff0000ffff,+ 0x000000ff00ff0000, 0xff0000ff00ff00ff, 0xff0000ff00ffff00, 0x000000ff00ffffff,+ 0x000000ffff000000, 0xff0000ffff0000ff, 0xff0000ffff00ff00, 0x000000ffff00ffff,+ 0xff0000ffffff0000, 0x000000ffffff00ff, 0x000000ffffffff00, 0xff0000ffffffffff,+ 0xff00ff0000000000, 0x0000ff00000000ff, 0x0000ff000000ff00, 0xff00ff000000ffff,+ 0x0000ff0000ff0000, 0xff00ff0000ff00ff, 0xff00ff0000ffff00, 0x0000ff0000ffffff,+ 0x0000ff00ff000000, 0xff00ff00ff0000ff, 0xff00ff00ff00ff00, 0x0000ff00ff00ffff,+ 0xff00ff00ffff0000, 0x0000ff00ffff00ff, 0x0000ff00ffffff00, 0xff00ff00ffffffff,+ 0x0000ffff00000000, 0xff00ffff000000ff, 0xff00ffff0000ff00, 0x0000ffff0000ffff,+ 0xff00ffff00ff0000, 0x0000ffff00ff00ff, 0x0000ffff00ffff00, 0xff00ffff00ffffff,+ 0xff00ffffff000000, 0x0000ffffff0000ff, 0x0000ffffff00ff00, 0xff00ffffff00ffff,+ 0x0000ffffffff0000, 0xff00ffffffff00ff, 0xff00ffffffffff00, 0x0000ffffffffffff,+ 0xffff000000000000, 0x00ff0000000000ff, 0x00ff00000000ff00, 0xffff00000000ffff,+ 0x00ff000000ff0000, 0xffff000000ff00ff, 0xffff000000ffff00, 0x00ff000000ffffff,+ 0x00ff0000ff000000, 0xffff0000ff0000ff, 0xffff0000ff00ff00, 0x00ff0000ff00ffff,+ 0xffff0000ffff0000, 0x00ff0000ffff00ff, 0x00ff0000ffffff00, 0xffff0000ffffffff,+ 0x00ff00ff00000000, 0xffff00ff000000ff, 0xffff00ff0000ff00, 0x00ff00ff0000ffff,+ 0xffff00ff00ff0000, 0x00ff00ff00ff00ff, 0x00ff00ff00ffff00, 0xffff00ff00ffffff,+ 0xffff00ffff000000, 0x00ff00ffff0000ff, 0x00ff00ffff00ff00, 0xffff00ffff00ffff,+ 0x00ff00ffffff0000, 0xffff00ffffff00ff, 0xffff00ffffffff00, 0x00ff00ffffffffff,+ 0x00ffff0000000000, 0xffffff00000000ff, 0xffffff000000ff00, 0x00ffff000000ffff,+ 0xffffff0000ff0000, 0x00ffff0000ff00ff, 0x00ffff0000ffff00, 0xffffff0000ffffff,+ 0xffffff00ff000000, 0x00ffff00ff0000ff, 0x00ffff00ff00ff00, 0xffffff00ff00ffff,+ 0x00ffff00ffff0000, 0xffffff00ffff00ff, 0xffffff00ffffff00, 0x00ffff00ffffffff,+ 0xffffffff00000000, 0x00ffffff000000ff, 0x00ffffff0000ff00, 0xffffffff0000ffff,+ 0x00ffffff00ff0000, 0xffffffff00ff00ff, 0xffffffff00ffff00, 0x00ffffff00ffffff,+ 0x00ffffffff000000, 0xffffffffff0000ff, 0xffffffffff00ff00, 0x00ffffffff00ffff,+ 0xffffffffffff0000, 0x00ffffffffff00ff, 0x00ffffffffffff00, 0xffffffffffffffff,+GGML_TABLE_END()+++GGML_TABLE_BEGIN(uint64_t, iq2xxs_grid, 256)+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,+ 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,+ 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,+ 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,+ 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,+ 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,+ 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,+ 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,+ 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,+ 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,+ 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,+ 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,+ 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,+ 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,+ 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,+ 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,+ 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,+ 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,+ 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,+ 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,+ 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,+ 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,+ 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,+ 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,+ 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,+ 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,+ 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,+ 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,+ 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,+ 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,+ 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,+ 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,+ 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,+ 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,+ 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,+ 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,+ 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,+ 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,+ 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,+ 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,+ 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,+ 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,+ 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,+ 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,+ 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,+ 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,+ 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,+ 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,+ 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,+ 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,+ 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,+ 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,+ 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,+ 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,+ 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,+ 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,+ 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,+ 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,+ 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,+ 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,+ 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,+ 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,+ 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,+GGML_TABLE_END()++GGML_TABLE_BEGIN(uint64_t, iq2xs_grid, 512)+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,+ 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,+ 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,+ 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,+ 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,+ 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,+ 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,+ 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,+ 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,+ 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,+ 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,+ 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,+ 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,+ 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,+ 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,+ 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,+ 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,+ 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,+ 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,+ 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,+ 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,+ 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,+ 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,+ 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,+ 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,+ 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,+ 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,+ 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,+ 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,+ 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,+ 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,+ 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,+ 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,+ 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,+ 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,+ 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,+ 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,+ 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,+ 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,+ 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,+ 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,+ 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,+ 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,+ 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,+ 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,+ 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,+ 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,+ 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,+ 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,+ 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,+ 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,+ 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,+ 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,+ 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,+ 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,+ 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,+ 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,+ 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,+ 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,+ 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,+ 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,+ 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,+ 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,+ 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,+ 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,+ 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,+ 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,+ 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,+ 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,+ 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,+ 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,+ 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,+ 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,+ 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,+ 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,+ 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,+ 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,+ 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,+ 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,+ 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,+ 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,+ 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,+ 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,+ 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,+ 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,+ 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,+ 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,+ 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,+ 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,+ 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,+ 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,+ 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,+ 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,+ 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,+ 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,+ 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,+ 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,+ 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,+ 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,+ 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,+ 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,+ 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,+ 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,+ 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,+ 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,+ 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,+ 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,+ 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,+ 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,+ 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,+ 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,+ 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,+ 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,+ 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,+ 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,+ 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,+ 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,+ 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,+ 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,+ 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,+ 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,+ 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,+ 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,+ 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,+ 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,+ 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,+ 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,+GGML_TABLE_END()++GGML_TABLE_BEGIN(uint64_t, iq2s_grid, 1024)+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,+ 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,+ 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,+ 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,+ 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x08080808192b192b,+ 0x08080808192b2b19, 0x080808082b080808, 0x080808082b08082b, 0x080808082b081919,+ 0x080808082b082b08, 0x080808082b190819, 0x080808082b191908, 0x080808082b2b0808,+ 0x080808082b2b1919, 0x080808082b2b2b2b, 0x0808081908080819, 0x0808081908081908,+ 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, 0x080808190819082b,+ 0x0808081908191919, 0x0808081908192b08, 0x08080819082b0819, 0x08080819082b1908,+ 0x0808081919080808, 0x080808191908082b, 0x0808081919081919, 0x0808081919082b08,+ 0x0808081919190819, 0x0808081919191908, 0x080808191919192b, 0x0808081919192b19,+ 0x08080819192b0808, 0x08080819192b1919, 0x08080819192b2b08, 0x080808192b080819,+ 0x080808192b081908, 0x080808192b190808, 0x080808192b19082b, 0x080808192b191919,+ 0x080808192b2b0819, 0x080808192b2b1908, 0x0808082b08080808, 0x0808082b0808082b,+ 0x0808082b08081919, 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908,+ 0x0808082b082b0808, 0x0808082b082b2b2b, 0x0808082b19080819, 0x0808082b19081908,+ 0x0808082b1908192b, 0x0808082b19082b19, 0x0808082b19190808, 0x0808082b19191919,+ 0x0808082b2b080808, 0x0808082b2b081919, 0x0808082b2b082b2b, 0x0808082b2b191908,+ 0x0808082b2b2b082b, 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,+ 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, 0x0808190808191919,+ 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, 0x08081908082b192b,+ 0x08081908082b2b19, 0x0808190819080808, 0x080819081908082b, 0x0808190819081919,+ 0x0808190819082b08, 0x0808190819082b2b, 0x0808190819190819, 0x0808190819191908,+ 0x080819081919192b, 0x0808190819192b19, 0x08081908192b0808, 0x08081908192b082b,+ 0x08081908192b1919, 0x080819082b080819, 0x080819082b081908, 0x080819082b08192b,+ 0x080819082b082b19, 0x080819082b190808, 0x080819082b191919, 0x080819082b192b08,+ 0x080819082b2b0819, 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b,+ 0x0808191908081919, 0x0808191908082b08, 0x0808191908082b2b, 0x0808191908190819,+ 0x0808191908191908, 0x080819190819192b, 0x0808191908192b19, 0x08081919082b0808,+ 0x08081919082b1919, 0x08081919082b2b08, 0x0808191919080819, 0x0808191919081908,+ 0x080819191908192b, 0x0808191919082b19, 0x0808191919190808, 0x080819191919082b,+ 0x0808191919191919, 0x0808191919192b08, 0x08081919192b0819, 0x08081919192b1908,+ 0x080819192b080808, 0x080819192b08082b, 0x080819192b081919, 0x080819192b082b08,+ 0x080819192b190819, 0x080819192b191908, 0x080819192b2b0808, 0x0808192b08080819,+ 0x0808192b08081908, 0x0808192b0808192b, 0x0808192b08082b19, 0x0808192b08190808,+ 0x0808192b08191919, 0x0808192b19080808, 0x0808192b19081919, 0x0808192b19082b08,+ 0x0808192b19190819, 0x0808192b19191908, 0x0808192b192b0808, 0x0808192b2b080819,+ 0x0808192b2b081908, 0x0808192b2b190808, 0x08082b0808080808, 0x08082b080808082b,+ 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808190819, 0x08082b0808191908,+ 0x08082b080819192b, 0x08082b0808192b19, 0x08082b08082b0808, 0x08082b08082b1919,+ 0x08082b08082b2b2b, 0x08082b0819080819, 0x08082b0819081908, 0x08082b081908192b,+ 0x08082b0819082b19, 0x08082b0819190808, 0x08082b081919082b, 0x08082b0819191919,+ 0x08082b0819192b08, 0x08082b08192b0819, 0x08082b08192b1908, 0x08082b082b080808,+ 0x08082b082b081919, 0x08082b082b191908, 0x08082b082b2b2b2b, 0x08082b1908080819,+ 0x08082b1908081908, 0x08082b1908190808, 0x08082b190819082b, 0x08082b1908191919,+ 0x08082b1908192b08, 0x08082b19082b0819, 0x08082b1919080808, 0x08082b1919081919,+ 0x08082b1919082b08, 0x08082b1919190819, 0x08082b1919191908, 0x08082b19192b0808,+ 0x08082b192b080819, 0x08082b192b190808, 0x08082b2b08080808, 0x08082b2b08190819,+ 0x08082b2b08191908, 0x08082b2b082b082b, 0x08082b2b082b2b08, 0x08082b2b082b2b2b,+ 0x08082b2b19190808, 0x08082b2b2b192b19, 0x0819080808080819, 0x0819080808081908,+ 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b,+ 0x0819080808191919, 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908,+ 0x08190808082b192b, 0x0819080819080808, 0x081908081908082b, 0x0819080819081919,+ 0x0819080819082b08, 0x0819080819190819, 0x0819080819191908, 0x081908081919192b,+ 0x0819080819192b19, 0x08190808192b0808, 0x08190808192b082b, 0x08190808192b1919,+ 0x08190808192b2b08, 0x081908082b080819, 0x081908082b081908, 0x081908082b08192b,+ 0x081908082b190808, 0x081908082b191919, 0x081908082b192b08, 0x081908082b2b0819,+ 0x081908082b2b1908, 0x0819081908080808, 0x081908190808082b, 0x0819081908081919,+ 0x0819081908082b08, 0x0819081908082b2b, 0x0819081908190819, 0x0819081908191908,+ 0x081908190819192b, 0x0819081908192b19, 0x08190819082b0808, 0x08190819082b082b,+ 0x08190819082b1919, 0x08190819082b2b08, 0x0819081919080819, 0x0819081919081908,+ 0x081908191908192b, 0x0819081919082b19, 0x0819081919190808, 0x081908191919082b,+ 0x0819081919191919, 0x0819081919192b08, 0x08190819192b0819, 0x08190819192b1908,+ 0x081908192b080808, 0x081908192b08082b, 0x081908192b081919, 0x081908192b082b08,+ 0x081908192b190819, 0x081908192b191908, 0x0819082b08080819, 0x0819082b08081908,+ 0x0819082b08082b19, 0x0819082b08190808, 0x0819082b08191919, 0x0819082b082b0819,+ 0x0819082b082b1908, 0x0819082b19080808, 0x0819082b19081919, 0x0819082b19190819,+ 0x0819082b19191908, 0x0819082b2b080819, 0x0819082b2b081908, 0x0819082b2b190808,+ 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,+ 0x0819190808190819, 0x0819190808191908, 0x081919080819192b, 0x0819190808192b19,+ 0x08191908082b0808, 0x08191908082b1919, 0x08191908082b2b08, 0x0819190819080819,+ 0x0819190819081908, 0x081919081908192b, 0x0819190819082b19, 0x0819190819190808,+ 0x081919081919082b, 0x0819190819191919, 0x0819190819192b08, 0x08191908192b0819,+ 0x08191908192b1908, 0x081919082b080808, 0x081919082b08082b, 0x081919082b081919,+ 0x081919082b082b08, 0x081919082b190819, 0x081919082b191908, 0x081919082b2b0808,+ 0x0819191908080819, 0x0819191908081908, 0x081919190808192b, 0x0819191908082b19,+ 0x0819191908190808, 0x081919190819082b, 0x0819191908191919, 0x0819191908192b08,+ 0x08191919082b0819, 0x08191919082b1908, 0x0819191919080808, 0x081919191908082b,+ 0x0819191919081919, 0x0819191919082b08, 0x0819191919190819, 0x0819191919191908,+ 0x08191919192b0808, 0x081919192b080819, 0x081919192b081908, 0x081919192b190808,+ 0x0819192b08080808, 0x0819192b08081919, 0x0819192b08082b08, 0x0819192b08190819,+ 0x0819192b08191908, 0x0819192b082b0808, 0x0819192b19080819, 0x0819192b19081908,+ 0x0819192b19190808, 0x0819192b2b080808, 0x0819192b2b2b2b2b, 0x08192b0808080819,+ 0x08192b0808081908, 0x08192b080808192b, 0x08192b0808082b19, 0x08192b0808190808,+ 0x08192b0808191919, 0x08192b0808192b08, 0x08192b08082b0819, 0x08192b0819080808,+ 0x08192b081908082b, 0x08192b0819081919, 0x08192b0819082b08, 0x08192b0819190819,+ 0x08192b0819191908, 0x08192b08192b0808, 0x08192b082b080819, 0x08192b082b081908,+ 0x08192b1908080808, 0x08192b190808082b, 0x08192b1908081919, 0x08192b1908082b08,+ 0x08192b1908190819, 0x08192b1908191908, 0x08192b19082b0808, 0x08192b1919080819,+ 0x08192b1919081908, 0x08192b1919190808, 0x08192b19192b2b19, 0x08192b192b2b082b,+ 0x08192b2b08081908, 0x08192b2b08190808, 0x08192b2b19080808, 0x08192b2b1919192b,+ 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, 0x082b080808082b08,+ 0x082b080808190819, 0x082b080808191908, 0x082b08080819192b, 0x082b080808192b19,+ 0x082b0808082b0808, 0x082b0808082b1919, 0x082b0808082b2b2b, 0x082b080819080819,+ 0x082b080819081908, 0x082b080819190808, 0x082b08081919082b, 0x082b080819191919,+ 0x082b0808192b1908, 0x082b08082b080808, 0x082b08082b082b2b, 0x082b08082b191908,+ 0x082b08082b2b2b2b, 0x082b081908080819, 0x082b081908081908, 0x082b081908190808,+ 0x082b08190819082b, 0x082b081908191919, 0x082b0819082b0819, 0x082b081919080808,+ 0x082b08191908082b, 0x082b081919081919, 0x082b081919190819, 0x082b081919191908,+ 0x082b0819192b0808, 0x082b08192b080819, 0x082b08192b081908, 0x082b08192b190808,+ 0x082b082b08080808, 0x082b082b08082b2b, 0x082b082b082b082b, 0x082b082b082b2b08,+ 0x082b082b082b2b2b, 0x082b082b19081908, 0x082b082b19190808, 0x082b082b2b082b08,+ 0x082b082b2b082b2b, 0x082b082b2b2b2b08, 0x082b190808080819, 0x082b190808081908,+ 0x082b19080808192b, 0x082b190808082b19, 0x082b190808190808, 0x082b190808191919,+ 0x082b190808192b08, 0x082b1908082b0819, 0x082b1908082b1908, 0x082b190819080808,+ 0x082b19081908082b, 0x082b190819081919, 0x082b190819082b08, 0x082b190819190819,+ 0x082b190819191908, 0x082b1908192b0808, 0x082b19082b080819, 0x082b19082b081908,+ 0x082b19082b190808, 0x082b191908080808, 0x082b191908081919, 0x082b191908082b08,+ 0x082b191908190819, 0x082b191908191908, 0x082b1919082b0808, 0x082b191919080819,+ 0x082b191919081908, 0x082b191919190808, 0x082b1919192b192b, 0x082b19192b080808,+ 0x082b192b08080819, 0x082b192b08081908, 0x082b192b08190808, 0x082b192b19080808,+ 0x082b192b19192b19, 0x082b2b0808080808, 0x082b2b0808081919, 0x082b2b0808190819,+ 0x082b2b0808191908, 0x082b2b0819080819, 0x082b2b0819081908, 0x082b2b0819190808,+ 0x082b2b082b082b2b, 0x082b2b082b2b2b2b, 0x082b2b1908080819, 0x082b2b1908081908,+ 0x082b2b1908190808, 0x082b2b192b191919, 0x082b2b2b08082b2b, 0x082b2b2b082b082b,+ 0x082b2b2b192b1908, 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,+ 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808,+ 0x190808080819082b, 0x1908080808191919, 0x1908080808192b08, 0x1908080808192b2b,+ 0x19080808082b0819, 0x19080808082b1908, 0x19080808082b192b, 0x1908080819080808,+ 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, 0x1908080819082b2b,+ 0x1908080819190819, 0x1908080819191908, 0x190808081919192b, 0x1908080819192b19,+ 0x19080808192b0808, 0x19080808192b082b, 0x19080808192b1919, 0x190808082b080819,+ 0x190808082b081908, 0x190808082b190808, 0x190808082b191919, 0x190808082b192b08,+ 0x190808082b2b0819, 0x190808082b2b1908, 0x1908081908080808, 0x190808190808082b,+ 0x1908081908081919, 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,+ 0x190808190819192b, 0x1908081908192b19, 0x19080819082b0808, 0x19080819082b082b,+ 0x19080819082b1919, 0x1908081919080819, 0x1908081919081908, 0x190808191908192b,+ 0x1908081919082b19, 0x1908081919190808, 0x190808191919082b, 0x1908081919191919,+ 0x1908081919192b08, 0x19080819192b0819, 0x19080819192b1908, 0x190808192b080808,+ 0x190808192b08082b, 0x190808192b081919, 0x190808192b082b08, 0x190808192b190819,+ 0x190808192b191908, 0x190808192b2b0808, 0x1908082b08080819, 0x1908082b08081908,+ 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b08191919, 0x1908082b08192b08,+ 0x1908082b082b1908, 0x1908082b19080808, 0x1908082b19081919, 0x1908082b19082b08,+ 0x1908082b19190819, 0x1908082b19191908, 0x1908082b192b0808, 0x1908082b2b080819,+ 0x1908082b2b081908, 0x1908190808080808, 0x190819080808082b, 0x1908190808081919,+ 0x1908190808082b08, 0x1908190808082b2b, 0x1908190808190819, 0x1908190808191908,+ 0x190819080819192b, 0x1908190808192b19, 0x19081908082b0808, 0x19081908082b082b,+ 0x19081908082b1919, 0x19081908082b2b08, 0x1908190819080819, 0x1908190819081908,+ 0x190819081908192b, 0x1908190819082b19, 0x1908190819190808, 0x190819081919082b,+ 0x1908190819191919, 0x1908190819192b08, 0x19081908192b0819, 0x19081908192b1908,+ 0x190819082b080808, 0x190819082b08082b, 0x190819082b081919, 0x190819082b082b08,+ 0x190819082b190819, 0x190819082b191908, 0x190819082b2b0808, 0x1908191908080819,+ 0x1908191908081908, 0x190819190808192b, 0x1908191908082b19, 0x1908191908190808,+ 0x190819190819082b, 0x1908191908191919, 0x1908191908192b08, 0x19081919082b0819,+ 0x19081919082b1908, 0x1908191919080808, 0x190819191908082b, 0x1908191919081919,+ 0x1908191919082b08, 0x1908191919190819, 0x1908191919191908, 0x19081919192b0808,+ 0x19081919192b2b2b, 0x190819192b080819, 0x190819192b081908, 0x190819192b190808,+ 0x1908192b08080808, 0x1908192b0808082b, 0x1908192b08081919, 0x1908192b08082b08,+ 0x1908192b08190819, 0x1908192b08191908, 0x1908192b082b0808, 0x1908192b19080819,+ 0x1908192b19081908, 0x1908192b19190808, 0x1908192b2b080808, 0x1908192b2b2b1919,+ 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808082b19, 0x19082b0808190808,+ 0x19082b080819082b, 0x19082b0808191919, 0x19082b0808192b08, 0x19082b08082b0819,+ 0x19082b08082b1908, 0x19082b0819080808, 0x19082b081908082b, 0x19082b0819081919,+ 0x19082b0819082b08, 0x19082b0819190819, 0x19082b0819191908, 0x19082b08192b0808,+ 0x19082b082b081908, 0x19082b082b190808, 0x19082b1908080808, 0x19082b190808082b,+ 0x19082b1908081919, 0x19082b1908082b08, 0x19082b1908190819, 0x19082b1908191908,+ 0x19082b19082b0808, 0x19082b1919080819, 0x19082b1919081908, 0x19082b1919190808,+ 0x19082b192b080808, 0x19082b192b19192b, 0x19082b2b08080819, 0x19082b2b08081908,+ 0x19082b2b08190808, 0x19082b2b19080808, 0x1919080808080808, 0x191908080808082b,+ 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, 0x1919080808191908,+ 0x191908080819192b, 0x1919080808192b19, 0x19190808082b0808, 0x19190808082b082b,+ 0x19190808082b1919, 0x19190808082b2b08, 0x1919080819080819, 0x1919080819081908,+ 0x191908081908192b, 0x1919080819082b19, 0x1919080819190808, 0x191908081919082b,+ 0x1919080819191919, 0x1919080819192b08, 0x19190808192b0819, 0x19190808192b1908,+ 0x191908082b080808, 0x191908082b08082b, 0x191908082b081919, 0x191908082b082b08,+ 0x191908082b190819, 0x191908082b191908, 0x1919081908080819, 0x1919081908081908,+ 0x191908190808192b, 0x1919081908082b19, 0x1919081908190808, 0x191908190819082b,+ 0x1919081908191919, 0x1919081908192b08, 0x19190819082b0819, 0x19190819082b1908,+ 0x1919081919080808, 0x191908191908082b, 0x1919081919081919, 0x1919081919082b08,+ 0x1919081919190819, 0x1919081919191908, 0x19190819192b0808, 0x191908192b080819,+ 0x191908192b081908, 0x191908192b190808, 0x1919082b08080808, 0x1919082b08081919,+ 0x1919082b08082b08, 0x1919082b08190819, 0x1919082b08191908, 0x1919082b082b0808,+ 0x1919082b19080819, 0x1919082b19081908, 0x1919082b19190808, 0x1919082b192b2b19,+ 0x1919082b2b080808, 0x1919190808080819, 0x1919190808081908, 0x191919080808192b,+ 0x1919190808082b19, 0x1919190808190808, 0x191919080819082b, 0x1919190808191919,+ 0x1919190808192b08, 0x19191908082b0819, 0x19191908082b1908, 0x1919190819080808,+ 0x191919081908082b, 0x1919190819081919, 0x1919190819082b08, 0x1919190819190819,+ 0x1919190819191908, 0x19191908192b0808, 0x191919082b080819, 0x191919082b081908,+ 0x191919082b190808, 0x1919191908080808, 0x191919190808082b, 0x1919191908081919,+ 0x1919191908082b08, 0x1919191908190819, 0x1919191908191908, 0x19191919082b0808,+ 0x1919191919080819, 0x1919191919081908, 0x1919191919190808, 0x191919192b080808,+ 0x1919192b08080819, 0x1919192b08081908, 0x1919192b08190808, 0x1919192b082b192b,+ 0x1919192b19080808, 0x19192b0808080808, 0x19192b080808082b, 0x19192b0808081919,+ 0x19192b0808082b08, 0x19192b0808190819, 0x19192b0808191908, 0x19192b08082b0808,+ 0x19192b0819080819, 0x19192b0819081908, 0x19192b0819190808, 0x19192b0819192b2b,+ 0x19192b082b080808, 0x19192b1908080819, 0x19192b1908081908, 0x19192b1908190808,+ 0x19192b1919080808, 0x19192b2b08080808, 0x19192b2b08192b19, 0x19192b2b2b081919,+ 0x19192b2b2b2b2b08, 0x192b080808080819, 0x192b080808081908, 0x192b08080808192b,+ 0x192b080808190808, 0x192b08080819082b, 0x192b080808191919, 0x192b080808192b08,+ 0x192b0808082b0819, 0x192b0808082b1908, 0x192b080819080808, 0x192b080819081919,+ 0x192b080819082b08, 0x192b080819190819, 0x192b080819191908, 0x192b0808192b0808,+ 0x192b08082b081908, 0x192b08082b190808, 0x192b081908080808, 0x192b08190808082b,+ 0x192b081908081919, 0x192b081908082b08, 0x192b081908190819, 0x192b081908191908,+ 0x192b0819082b0808, 0x192b081919080819, 0x192b081919081908, 0x192b081919190808,+ 0x192b08192b080808, 0x192b08192b192b19, 0x192b082b08081908, 0x192b082b08190808,+ 0x192b082b19080808, 0x192b082b1919192b, 0x192b082b2b2b0819, 0x192b190808080808,+ 0x192b190808081919, 0x192b190808082b08, 0x192b190808190819, 0x192b190808191908,+ 0x192b1908082b0808, 0x192b190819080819, 0x192b190819081908, 0x192b190819190808,+ 0x192b19082b080808, 0x192b191908080819, 0x192b191908081908, 0x192b191908190808,+ 0x192b191919080808, 0x192b191919082b2b, 0x192b1919192b2b08, 0x192b19192b19082b,+ 0x192b192b08080808, 0x192b192b2b191908, 0x192b2b0808080819, 0x192b2b0808081908,+ 0x192b2b0808190808, 0x192b2b08192b1919, 0x192b2b082b192b08, 0x192b2b1908080808,+ 0x192b2b19082b2b2b, 0x192b2b2b1908082b, 0x192b2b2b2b2b0819, 0x2b08080808080808,+ 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,+ 0x2b08080808191908, 0x2b08080808192b19, 0x2b080808082b0808, 0x2b080808082b1919,+ 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808081919082b,+ 0x2b08080819191919, 0x2b08080819192b08, 0x2b080808192b0819, 0x2b0808082b080808,+ 0x2b0808082b081919, 0x2b0808082b190819, 0x2b0808082b191908, 0x2b08081908080819,+ 0x2b08081908081908, 0x2b08081908082b19, 0x2b08081908190808, 0x2b0808190819082b,+ 0x2b08081908191919, 0x2b08081908192b08, 0x2b080819082b0819, 0x2b080819082b1908,+ 0x2b08081919080808, 0x2b0808191908082b, 0x2b08081919081919, 0x2b08081919082b08,+ 0x2b08081919190819, 0x2b08081919191908, 0x2b0808192b080819, 0x2b0808192b081908,+ 0x2b0808192b190808, 0x2b0808192b2b2b19, 0x2b08082b08080808, 0x2b08082b08081919,+ 0x2b08082b08082b2b, 0x2b08082b08190819, 0x2b08082b08191908, 0x2b08082b19080819,+ 0x2b08082b19081908, 0x2b08082b19190808, 0x2b08190808080819, 0x2b08190808081908,+ 0x2b0819080808192b, 0x2b08190808082b19, 0x2b08190808190808, 0x2b0819080819082b,+ 0x2b08190808191919, 0x2b08190808192b08, 0x2b081908082b0819, 0x2b08190819080808,+ 0x2b0819081908082b, 0x2b08190819081919, 0x2b08190819082b08, 0x2b08190819190819,+ 0x2b08190819191908, 0x2b081908192b0808, 0x2b0819082b080819, 0x2b0819082b081908,+ 0x2b0819082b190808, 0x2b08191908080808, 0x2b0819190808082b, 0x2b08191908081919,+ 0x2b08191908082b08, 0x2b08191908190819, 0x2b08191908191908, 0x2b081919082b0808,+ 0x2b08191919080819, 0x2b08191919081908, 0x2b08191919190808, 0x2b0819192b080808,+ 0x2b0819192b082b2b, 0x2b08192b08080819, 0x2b08192b08081908, 0x2b08192b08190808,+ 0x2b08192b082b2b19, 0x2b08192b19080808, 0x2b082b0808080808, 0x2b082b0808081919,+ 0x2b082b0808190819, 0x2b082b0808191908, 0x2b082b0819080819, 0x2b082b0819081908,+ 0x2b082b0819190808, 0x2b082b082b2b082b, 0x2b082b1908080819, 0x2b082b1908081908,+ 0x2b082b1919080808, 0x2b082b19192b1919, 0x2b082b2b082b082b, 0x2b082b2b19192b08,+ 0x2b082b2b19192b2b, 0x2b082b2b2b08082b, 0x2b082b2b2b2b082b, 0x2b19080808080819,+ 0x2b19080808081908, 0x2b19080808082b19, 0x2b19080808190808, 0x2b1908080819082b,+ 0x2b19080808191919, 0x2b19080808192b08, 0x2b190808082b1908, 0x2b19080819080808,+ 0x2b1908081908082b, 0x2b19080819081919, 0x2b19080819082b08, 0x2b19080819190819,+ 0x2b19080819191908, 0x2b190808192b0808, 0x2b1908082b080819, 0x2b1908082b081908,+ 0x2b1908082b190808, 0x2b19081908080808, 0x2b19081908081919, 0x2b19081908190819,+ 0x2b19081908191908, 0x2b19081919080819, 0x2b19081919081908, 0x2b19081919190808,+ 0x2b19081919192b2b, 0x2b19082b08080819, 0x2b19082b08081908, 0x2b19082b08190808,+ 0x2b19082b19080808, 0x2b19082b2b2b192b, 0x2b19190808080808, 0x2b1919080808082b,+ 0x2b19190808081919, 0x2b19190808082b08, 0x2b19190808190819, 0x2b19190808191908,+ 0x2b191908082b0808, 0x2b19190819080819, 0x2b19190819081908, 0x2b19190819190808,+ 0x2b1919082b080808, 0x2b1919082b19192b, 0x2b19191908080819, 0x2b19191908081908,+ 0x2b19191908190808, 0x2b19191919080808, 0x2b1919192b192b08, 0x2b1919192b2b0819,+ 0x2b19192b08080808, 0x2b19192b1908192b, 0x2b19192b192b1908, 0x2b192b0808080819,+ 0x2b192b0808081908, 0x2b192b0808190808, 0x2b192b08082b192b, 0x2b192b0819080808,+ 0x2b192b082b2b2b19, 0x2b192b1908080808, 0x2b192b1919082b19, 0x2b192b191919082b,+ 0x2b192b2b2b190808, 0x2b2b080808080808, 0x2b2b080808081919, 0x2b2b080808082b2b,+ 0x2b2b080808191908, 0x2b2b0808082b082b, 0x2b2b0808082b2b2b, 0x2b2b080819080819,+ 0x2b2b080819081908, 0x2b2b080819190808, 0x2b2b08082b2b082b, 0x2b2b08082b2b2b2b,+ 0x2b2b081919080808, 0x2b2b0819192b1919, 0x2b2b082b0808082b, 0x2b2b082b08082b2b,+ 0x2b2b082b082b082b, 0x2b2b082b082b2b08, 0x2b2b082b082b2b2b, 0x2b2b082b2b08082b,+ 0x2b2b082b2b082b08, 0x2b2b082b2b082b2b, 0x2b2b082b2b2b2b08, 0x2b2b190808080819,+ 0x2b2b190808081908, 0x2b2b190808190808, 0x2b2b190819080808, 0x2b2b19082b082b19,+ 0x2b2b19082b2b1908, 0x2b2b191908080808, 0x2b2b191908192b19, 0x2b2b192b19190819,+ 0x2b2b2b0808082b2b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b082b, 0x2b2b2b1919191908,+ 0x2b2b2b192b08192b, 0x2b2b2b2b08082b08, 0x2b2b2b2b08082b2b, 0x2b2b2b2b082b0808,+ 0x2b2b2b2b082b082b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b2b082b08, 0x2b2b2b2b2b2b2b2b,+GGML_TABLE_END()++GGML_TABLE_BEGIN(uint32_t, iq3xxs_grid, 256)+ 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,+ 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,+ 0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,+ 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,+ 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,+ 0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,+ 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,+ 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,+ 0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,+ 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,+ 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,+ 0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,+ 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,+ 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,+ 0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,+ 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,+ 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,+ 0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,+ 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,+ 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,+ 0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,+ 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,+ 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,+ 0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,+ 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,+ 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,+ 0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,+ 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,+ 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,+ 0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,+ 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,+ 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,+GGML_TABLE_END()++GGML_TABLE_BEGIN(uint32_t, iq3s_grid, 512)+ 0x01010101, 0x01010103, 0x01010105, 0x0101010b, 0x0101010f, 0x01010301, 0x01010303, 0x01010305,+ 0x01010309, 0x0101030d, 0x01010501, 0x01010503, 0x0101050b, 0x01010707, 0x01010901, 0x01010905,+ 0x0101090b, 0x0101090f, 0x01010b03, 0x01010b07, 0x01010d01, 0x01010d05, 0x01010f03, 0x01010f09,+ 0x01010f0f, 0x01030101, 0x01030103, 0x01030105, 0x01030109, 0x01030301, 0x01030303, 0x0103030b,+ 0x01030501, 0x01030507, 0x0103050f, 0x01030703, 0x0103070b, 0x01030909, 0x01030d03, 0x01030d0b,+ 0x01030f05, 0x01050101, 0x01050103, 0x0105010b, 0x0105010f, 0x01050301, 0x01050307, 0x0105030d,+ 0x01050503, 0x0105050b, 0x01050701, 0x01050709, 0x01050905, 0x0105090b, 0x0105090f, 0x01050b03,+ 0x01050b07, 0x01050f01, 0x01050f07, 0x01070107, 0x01070303, 0x0107030b, 0x01070501, 0x01070505,+ 0x01070703, 0x01070707, 0x0107070d, 0x01070909, 0x01070b01, 0x01070b05, 0x01070d0f, 0x01070f03,+ 0x01070f0b, 0x01090101, 0x01090307, 0x0109030f, 0x01090503, 0x01090509, 0x01090705, 0x01090901,+ 0x01090907, 0x01090b03, 0x01090f01, 0x010b0105, 0x010b0109, 0x010b0501, 0x010b0505, 0x010b050d,+ 0x010b0707, 0x010b0903, 0x010b090b, 0x010b090f, 0x010b0d0d, 0x010b0f07, 0x010d010d, 0x010d0303,+ 0x010d0307, 0x010d0703, 0x010d0b05, 0x010d0f03, 0x010f0101, 0x010f0105, 0x010f0109, 0x010f0501,+ 0x010f0505, 0x010f050d, 0x010f0707, 0x010f0b01, 0x010f0b09, 0x03010101, 0x03010103, 0x03010105,+ 0x03010109, 0x03010301, 0x03010303, 0x03010307, 0x0301030b, 0x0301030f, 0x03010501, 0x03010505,+ 0x03010703, 0x03010709, 0x0301070d, 0x03010b09, 0x03010b0d, 0x03010d03, 0x03010f05, 0x03030101,+ 0x03030103, 0x03030107, 0x0303010d, 0x03030301, 0x03030309, 0x03030503, 0x03030701, 0x03030707,+ 0x03030903, 0x03030b01, 0x03030b05, 0x03030f01, 0x03030f0d, 0x03050101, 0x03050305, 0x0305030b,+ 0x0305030f, 0x03050501, 0x03050509, 0x03050705, 0x03050901, 0x03050907, 0x03050b0b, 0x03050d01,+ 0x03050f05, 0x03070103, 0x03070109, 0x0307010f, 0x03070301, 0x03070307, 0x03070503, 0x0307050f,+ 0x03070701, 0x03070709, 0x03070903, 0x03070d05, 0x03070f01, 0x03090107, 0x0309010b, 0x03090305,+ 0x03090309, 0x03090703, 0x03090707, 0x03090905, 0x0309090d, 0x03090b01, 0x03090b09, 0x030b0103,+ 0x030b0301, 0x030b0307, 0x030b0503, 0x030b0701, 0x030b0705, 0x030b0b03, 0x030d0501, 0x030d0509,+ 0x030d050f, 0x030d0909, 0x030d090d, 0x030f0103, 0x030f0107, 0x030f0301, 0x030f0305, 0x030f0503,+ 0x030f070b, 0x030f0903, 0x030f0d05, 0x030f0f01, 0x05010101, 0x05010103, 0x05010107, 0x0501010b,+ 0x0501010f, 0x05010301, 0x05010305, 0x05010309, 0x0501030d, 0x05010503, 0x05010507, 0x0501050f,+ 0x05010701, 0x05010705, 0x05010903, 0x05010907, 0x0501090b, 0x05010b01, 0x05010b05, 0x05010d0f,+ 0x05010f01, 0x05010f07, 0x05010f0b, 0x05030101, 0x05030105, 0x05030301, 0x05030307, 0x0503030f,+ 0x05030505, 0x0503050b, 0x05030703, 0x05030709, 0x05030905, 0x05030b03, 0x05050103, 0x05050109,+ 0x0505010f, 0x05050503, 0x05050507, 0x05050701, 0x0505070f, 0x05050903, 0x05050b07, 0x05050b0f,+ 0x05050f03, 0x05050f09, 0x05070101, 0x05070105, 0x0507010b, 0x05070303, 0x05070505, 0x05070509,+ 0x05070703, 0x05070707, 0x05070905, 0x05070b01, 0x05070d0d, 0x05090103, 0x0509010f, 0x05090501,+ 0x05090507, 0x05090705, 0x0509070b, 0x05090903, 0x05090f05, 0x05090f0b, 0x050b0109, 0x050b0303,+ 0x050b0505, 0x050b070f, 0x050b0901, 0x050b0b07, 0x050b0f01, 0x050d0101, 0x050d0105, 0x050d010f,+ 0x050d0503, 0x050d0b0b, 0x050d0d03, 0x050f010b, 0x050f0303, 0x050f050d, 0x050f0701, 0x050f0907,+ 0x050f0b01, 0x07010105, 0x07010303, 0x07010307, 0x0701030b, 0x0701030f, 0x07010505, 0x07010703,+ 0x07010707, 0x0701070b, 0x07010905, 0x07010909, 0x0701090f, 0x07010b03, 0x07010d07, 0x07010f03,+ 0x07030103, 0x07030107, 0x0703010b, 0x07030309, 0x07030503, 0x07030507, 0x07030901, 0x07030d01,+ 0x07030f05, 0x07030f0d, 0x07050101, 0x07050305, 0x07050501, 0x07050705, 0x07050709, 0x07050b01,+ 0x07070103, 0x07070301, 0x07070309, 0x07070503, 0x07070507, 0x0707050f, 0x07070701, 0x07070903,+ 0x07070907, 0x0707090f, 0x07070b0b, 0x07070f07, 0x07090107, 0x07090303, 0x0709030d, 0x07090505,+ 0x07090703, 0x07090b05, 0x07090d01, 0x07090d09, 0x070b0103, 0x070b0301, 0x070b0305, 0x070b050b,+ 0x070b0705, 0x070b0909, 0x070b0b0d, 0x070b0f07, 0x070d030d, 0x070d0903, 0x070f0103, 0x070f0107,+ 0x070f0501, 0x070f0505, 0x070f070b, 0x09010101, 0x09010109, 0x09010305, 0x09010501, 0x09010509,+ 0x0901050f, 0x09010705, 0x09010903, 0x09010b01, 0x09010f01, 0x09030105, 0x0903010f, 0x09030303,+ 0x09030307, 0x09030505, 0x09030701, 0x0903070b, 0x09030907, 0x09030b03, 0x09030b0b, 0x09050103,+ 0x09050107, 0x09050301, 0x0905030b, 0x09050503, 0x09050707, 0x09050901, 0x09050b0f, 0x09050d05,+ 0x09050f01, 0x09070109, 0x09070303, 0x09070307, 0x09070501, 0x09070505, 0x09070703, 0x0907070b,+ 0x09090101, 0x09090105, 0x09090509, 0x0909070f, 0x09090901, 0x09090f03, 0x090b010b, 0x090b010f,+ 0x090b0503, 0x090b0d05, 0x090d0307, 0x090d0709, 0x090d0d01, 0x090f0301, 0x090f030b, 0x090f0701,+ 0x090f0907, 0x090f0b03, 0x0b010105, 0x0b010301, 0x0b010309, 0x0b010505, 0x0b010901, 0x0b010909,+ 0x0b01090f, 0x0b010b05, 0x0b010d0d, 0x0b010f09, 0x0b030103, 0x0b030107, 0x0b03010b, 0x0b030305,+ 0x0b030503, 0x0b030705, 0x0b030f05, 0x0b050101, 0x0b050303, 0x0b050507, 0x0b050701, 0x0b05070d,+ 0x0b050b07, 0x0b070105, 0x0b07010f, 0x0b070301, 0x0b07050f, 0x0b070909, 0x0b070b03, 0x0b070d0b,+ 0x0b070f07, 0x0b090103, 0x0b090109, 0x0b090501, 0x0b090705, 0x0b09090d, 0x0b0b0305, 0x0b0b050d,+ 0x0b0b0b03, 0x0b0b0b07, 0x0b0d0905, 0x0b0f0105, 0x0b0f0109, 0x0b0f0505, 0x0d010303, 0x0d010307,+ 0x0d01030b, 0x0d010703, 0x0d010707, 0x0d010d01, 0x0d030101, 0x0d030501, 0x0d03050f, 0x0d030d09,+ 0x0d050305, 0x0d050709, 0x0d050905, 0x0d050b0b, 0x0d050d05, 0x0d050f01, 0x0d070101, 0x0d070309,+ 0x0d070503, 0x0d070901, 0x0d09050b, 0x0d090907, 0x0d090d05, 0x0d0b0101, 0x0d0b0107, 0x0d0b0709,+ 0x0d0b0d01, 0x0d0d010b, 0x0d0d0901, 0x0d0f0303, 0x0d0f0307, 0x0f010101, 0x0f010109, 0x0f01010f,+ 0x0f010501, 0x0f010505, 0x0f01070d, 0x0f010901, 0x0f010b09, 0x0f010d05, 0x0f030105, 0x0f030303,+ 0x0f030509, 0x0f030907, 0x0f03090b, 0x0f050103, 0x0f050109, 0x0f050301, 0x0f05030d, 0x0f050503,+ 0x0f050701, 0x0f050b03, 0x0f070105, 0x0f070705, 0x0f07070b, 0x0f070b07, 0x0f090103, 0x0f09010b,+ 0x0f090307, 0x0f090501, 0x0f090b01, 0x0f0b0505, 0x0f0b0905, 0x0f0d0105, 0x0f0d0703, 0x0f0f0101,+GGML_TABLE_END()++// TODO: fix name to kvalues_iq4_nl+GGML_TABLE_BEGIN(int8_t, kvalues_iq4nl, 16)+ -127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113,+GGML_TABLE_END()++// e2m1 values (doubled)+// ref: https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf+GGML_TABLE_BEGIN(int8_t, kvalues_mxfp4, 16)+ 0, 1, 2, 3, 4, 6, 8, 12, 0, -1, -2, -3, -4, -6, -8, -12,+GGML_TABLE_END()++#define NGRID_IQ1S 2048+#define IQ1S_DELTA 0.125f+#define IQ1M_DELTA 0.125f+GGML_TABLE_BEGIN(uint32_t, iq1s_grid_gpu, NGRID_IQ1S)+ 0x00000000, 0x00000002, 0x00000101, 0x00000200, 0x00000202, 0x00010001, 0x00010101, 0x00020000,+ 0x00020002, 0x00020200, 0x00020202, 0x01000101, 0x01010001, 0x01010100, 0x01010102, 0x01020101,+ 0x02000000, 0x02000002, 0x02000200, 0x02000202, 0x02010101, 0x02020000, 0x02020002, 0x02020200,+ 0x02020202, 0x00000110, 0x00000111, 0x00010011, 0x00010110, 0x00010112, 0x00010211, 0x00010212,+ 0x00020111, 0x01000011, 0x01000112, 0x01000211, 0x01010012, 0x01010111, 0x01010212, 0x01020011,+ 0x01020110, 0x01020112, 0x01020210, 0x02000111, 0x02010011, 0x02010110, 0x02010112, 0x02020111,+ 0x00000020, 0x00000022, 0x00000220, 0x00000222, 0x00010121, 0x00020020, 0x00020022, 0x00020220,+ 0x00020222, 0x01000121, 0x01010021, 0x01010221, 0x01020120, 0x01020221, 0x02000020, 0x02000022,+ 0x02000220, 0x02000222, 0x02010021, 0x02010121, 0x02010221, 0x02020020, 0x02020022, 0x02020220,+ 0x02020222, 0x00011001, 0x00011100, 0x00011102, 0x00021101, 0x01001001, 0x01001201, 0x01011101,+ 0x01011202, 0x01021100, 0x01021101, 0x02011001, 0x02011201, 0x02021101, 0x00001011, 0x00001110,+ 0x00001111, 0x00001112, 0x00011111, 0x00011210, 0x00011212, 0x00021211, 0x01001010, 0x01001111,+ 0x01001212, 0x01011010, 0x01011011, 0x01011110, 0x01011111, 0x01011112, 0x01011211, 0x01021010,+ 0x01021012, 0x01021111, 0x01021210, 0x01021212, 0x02001011, 0x02011011, 0x02011111, 0x02011210,+ 0x02011212, 0x02021011, 0x02021110, 0x02021111, 0x02021112, 0x02021211, 0x00011120, 0x00011221,+ 0x01001021, 0x01001120, 0x01011020, 0x01011022, 0x01011121, 0x01011220, 0x01021020, 0x01021021,+ 0x01021122, 0x01021221, 0x02001121, 0x02011021, 0x02011120, 0x02011221, 0x00002000, 0x00002002,+ 0x00002200, 0x00002202, 0x00012101, 0x00022000, 0x00022002, 0x00022200, 0x00022202, 0x01002101,+ 0x01012001, 0x01012102, 0x01022101, 0x02002000, 0x02002002, 0x02002200, 0x02002202, 0x02012101,+ 0x02022000, 0x02022002, 0x02022200, 0x02022202, 0x00002111, 0x00012011, 0x00012110, 0x00012211,+ 0x00022110, 0x00022111, 0x01002011, 0x01012010, 0x01012011, 0x01012111, 0x01022011, 0x01022110,+ 0x01022211, 0x02012011, 0x02012110, 0x02012112, 0x02012211, 0x02022111, 0x00002020, 0x00002022,+ 0x00002220, 0x00002222, 0x00012121, 0x00022020, 0x00022022, 0x00022220, 0x00022222, 0x01002121,+ 0x01012021, 0x01012221, 0x01022021, 0x01022121, 0x02002020, 0x02002022, 0x02002121, 0x02002220,+ 0x02002222, 0x02012121, 0x02022020, 0x02022022, 0x02022220, 0x02022222, 0x00110000, 0x00110001,+ 0x00110100, 0x00110201, 0x00120100, 0x00120101, 0x01100001, 0x01100100, 0x01110000, 0x01110101,+ 0x01110200, 0x01120001, 0x01120100, 0x01120101, 0x01120201, 0x02110001, 0x02110100, 0x02110102,+ 0x02120001, 0x02120101, 0x00100011, 0x00100110, 0x00100112, 0x00100211, 0x00110010, 0x00110012,+ 0x00110111, 0x00110210, 0x00120011, 0x00120110, 0x00120211, 0x01100111, 0x01100212, 0x01110010,+ 0x01110011, 0x01110012, 0x01110110, 0x01110111, 0x01110112, 0x01110211, 0x01120010, 0x01120111,+ 0x02100110, 0x02110012, 0x02110111, 0x02120011, 0x02120110, 0x00110021, 0x00110120, 0x00110122,+ 0x00120121, 0x01100020, 0x01100122, 0x01100221, 0x01110022, 0x01110121, 0x01110220, 0x01110222,+ 0x01120120, 0x01120122, 0x02100121, 0x02110021, 0x02110120, 0x02110122, 0x02120121, 0x00101001,+ 0x00101102, 0x00101201, 0x00111100, 0x00111101, 0x00111200, 0x00111201, 0x00121001, 0x00121102,+ 0x01101001, 0x01101101, 0x01101102, 0x01101200, 0x01101202, 0x01111001, 0x01111100, 0x01111101,+ 0x01111102, 0x01111201, 0x01121002, 0x01121101, 0x01121200, 0x02101100, 0x02101201, 0x02111000,+ 0x02111100, 0x02111101, 0x02111200, 0x02111201, 0x02111202, 0x02121001, 0x02121100, 0x02121101,+ 0x02121201, 0x00101012, 0x00101111, 0x00101212, 0x00111011, 0x00111110, 0x00111111, 0x00111112,+ 0x00111211, 0x00121010, 0x00121012, 0x00121111, 0x00121210, 0x00121212, 0x01101011, 0x01101110,+ 0x01101111, 0x01101112, 0x01111011, 0x01111012, 0x01111110, 0x01111111, 0x01111112, 0x01111211,+ 0x01111212, 0x01121011, 0x01121110, 0x01121111, 0x01121112, 0x01121211, 0x02101010, 0x02101012,+ 0x02101110, 0x02101111, 0x02101210, 0x02101212, 0x02111010, 0x02111011, 0x02111110, 0x02111111,+ 0x02111112, 0x02111211, 0x02111212, 0x02121010, 0x02121012, 0x02121111, 0x00101021, 0x00101120,+ 0x00101121, 0x00101122, 0x00111121, 0x00111122, 0x00111220, 0x00111222, 0x00121021, 0x00121122,+ 0x01101020, 0x01101022, 0x01101120, 0x01101121, 0x01101220, 0x01101222, 0x01111021, 0x01111121,+ 0x01111122, 0x01111220, 0x01111221, 0x01121021, 0x01121120, 0x01121121, 0x01121220, 0x01121221,+ 0x01121222, 0x02101122, 0x02101222, 0x02111022, 0x02111121, 0x02121120, 0x02121221, 0x00112001,+ 0x00112102, 0x00122101, 0x01102001, 0x01102100, 0x01102102, 0x01102201, 0x01112000, 0x01112101,+ 0x01112200, 0x01112202, 0x01122000, 0x01122001, 0x01122100, 0x01122102, 0x01122201, 0x02102101,+ 0x02112001, 0x02112100, 0x02122101, 0x00112010, 0x00112012, 0x00112111, 0x00112212, 0x00122011,+ 0x00122111, 0x01102012, 0x01102110, 0x01102111, 0x01102210, 0x01112011, 0x01112110, 0x01112111,+ 0x01112112, 0x01112211, 0x01112212, 0x01122010, 0x01122111, 0x01122212, 0x02102211, 0x02112011,+ 0x02112012, 0x02112111, 0x02112210, 0x02122011, 0x02122112, 0x02122211, 0x00102221, 0x00112122,+ 0x00122120, 0x00122122, 0x01102120, 0x01102122, 0x01102221, 0x01112020, 0x01112022, 0x01112121,+ 0x01112220, 0x01122021, 0x01122122, 0x01122221, 0x02102121, 0x02112021, 0x02112122, 0x02112222,+ 0x00200000, 0x00200002, 0x00200200, 0x00200202, 0x00210101, 0x00220000, 0x00220002, 0x00220101,+ 0x00220200, 0x00220202, 0x01200101, 0x01210001, 0x01210201, 0x01220001, 0x01220101, 0x02200000,+ 0x02200002, 0x02200200, 0x02200202, 0x02210101, 0x02220000, 0x02220002, 0x02220101, 0x02220200,+ 0x02220202, 0x00200111, 0x00210011, 0x00210110, 0x00210211, 0x00220111, 0x01200012, 0x01200110,+ 0x01200211, 0x01210111, 0x01210210, 0x01210212, 0x01220011, 0x01220110, 0x01220111, 0x01220112,+ 0x02200111, 0x02210010, 0x02210112, 0x02210211, 0x02220111, 0x00200021, 0x00200220, 0x00200222,+ 0x00210021, 0x00210121, 0x00220020, 0x00220022, 0x00220220, 0x00220222, 0x01200121, 0x01210021,+ 0x01210122, 0x01210221, 0x01220121, 0x02200021, 0x02200220, 0x02200222, 0x02210021, 0x02210121,+ 0x02220020, 0x02220022, 0x02220220, 0x02220222, 0x00201101, 0x00211100, 0x00211102, 0x00211201,+ 0x00221101, 0x01201100, 0x01201101, 0x01201102, 0x01201201, 0x01211002, 0x01211101, 0x01211200,+ 0x01211202, 0x01221102, 0x02201101, 0x02211001, 0x02211100, 0x02211201, 0x02221001, 0x02221101,+ 0x00201211, 0x00211111, 0x00221011, 0x00221211, 0x01201010, 0x01201111, 0x01201210, 0x01211011,+ 0x01211110, 0x01211111, 0x01211211, 0x01221012, 0x01221111, 0x01221210, 0x02201211, 0x02211010,+ 0x02211110, 0x02211111, 0x02211210, 0x02211212, 0x02221011, 0x02221110, 0x02221112, 0x02221211,+ 0x00201121, 0x00211020, 0x00211022, 0x00211221, 0x00221121, 0x01201021, 0x01201221, 0x01211121,+ 0x01221020, 0x01221021, 0x01221221, 0x02201120, 0x02201122, 0x02211020, 0x02211222, 0x00202000,+ 0x00202002, 0x00202200, 0x00202202, 0x00212101, 0x00222000, 0x00222002, 0x00222200, 0x00222202,+ 0x01202101, 0x01212001, 0x01212100, 0x01222101, 0x02202000, 0x02202002, 0x02202200, 0x02202202,+ 0x02222000, 0x02222002, 0x02222200, 0x02222202, 0x00202211, 0x00212011, 0x00212110, 0x00212211,+ 0x00222111, 0x01202112, 0x01202211, 0x01212012, 0x01212111, 0x01222011, 0x01222110, 0x01222112,+ 0x01222211, 0x02202111, 0x02212010, 0x02212112, 0x02212211, 0x02222110, 0x02222111, 0x00202020,+ 0x00202022, 0x00202220, 0x00202222, 0x00222020, 0x00222022, 0x00222220, 0x00222222, 0x01202121,+ 0x01212021, 0x01212122, 0x01212221, 0x01222121, 0x02202020, 0x02202022, 0x02202220, 0x02202222,+ 0x02212121, 0x02222020, 0x02222022, 0x02222220, 0x02222222, 0x10000101, 0x10010001, 0x10010102,+ 0x10020101, 0x11000201, 0x11010002, 0x11010101, 0x11010200, 0x11010202, 0x11020001, 0x11020100,+ 0x11020102, 0x12010100, 0x12010201, 0x12020001, 0x12020102, 0x10000010, 0x10000011, 0x10000110,+ 0x10000112, 0x10000211, 0x10010012, 0x10010111, 0x10010112, 0x10010210, 0x10010212, 0x10020011,+ 0x10020112, 0x10020211, 0x11000111, 0x11000210, 0x11000212, 0x11010011, 0x11010110, 0x11010111,+ 0x11010112, 0x11010211, 0x11010212, 0x11020111, 0x11020210, 0x11020212, 0x12000011, 0x12000110,+ 0x12000112, 0x12010010, 0x12010012, 0x12010111, 0x12020010, 0x12020011, 0x12020012, 0x10000121,+ 0x10010021, 0x10010120, 0x10010122, 0x10020121, 0x11000021, 0x11010022, 0x11010121, 0x11010222,+ 0x11020120, 0x11020221, 0x12000221, 0x12010120, 0x12020121, 0x10001001, 0x10011101, 0x10011201,+ 0x10021201, 0x11001101, 0x11001200, 0x11001202, 0x11011001, 0x11011100, 0x11011101, 0x11011102,+ 0x11021001, 0x11021002, 0x11021101, 0x11021200, 0x11021202, 0x12001001, 0x12001102, 0x12001201,+ 0x12011000, 0x12011002, 0x12011101, 0x12021000, 0x12021001, 0x12021201, 0x10001011, 0x10001012,+ 0x10001111, 0x10001212, 0x10011011, 0x10011110, 0x10011111, 0x10011112, 0x10011211, 0x10021010,+ 0x10021111, 0x10021212, 0x11001011, 0x11001110, 0x11001111, 0x11001112, 0x11001211, 0x11011010,+ 0x11011011, 0x11011110, 0x11011111, 0x11011112, 0x11011210, 0x11011211, 0x11021011, 0x11021110,+ 0x11021111, 0x11021112, 0x11021211, 0x12001012, 0x12001110, 0x12001111, 0x12001210, 0x12011011,+ 0x12011110, 0x12011111, 0x12011112, 0x12011211, 0x12011212, 0x12021111, 0x12021210, 0x12021212,+ 0x10001021, 0x10001121, 0x10001221, 0x10011120, 0x10011121, 0x10011220, 0x10011222, 0x10021021,+ 0x10021120, 0x10021221, 0x11001020, 0x11001022, 0x11001121, 0x11001220, 0x11011020, 0x11011021,+ 0x11011022, 0x11011121, 0x11011122, 0x11011221, 0x11021022, 0x11021121, 0x11021220, 0x12001021,+ 0x12001121, 0x12001222, 0x12011120, 0x12011121, 0x12021021, 0x12021120, 0x12021122, 0x10002101,+ 0x10012001, 0x10012101, 0x10012202, 0x10022101, 0x11002002, 0x11002201, 0x11012000, 0x11012101,+ 0x11012200, 0x11022001, 0x11022100, 0x11022102, 0x11022201, 0x12002101, 0x12012001, 0x12012100,+ 0x12012102, 0x12012201, 0x12022101, 0x10002011, 0x10002111, 0x10002112, 0x10002212, 0x10012010,+ 0x10012110, 0x10012111, 0x10012210, 0x10022011, 0x10022110, 0x10022112, 0x11002010, 0x11002111,+ 0x11002212, 0x11012011, 0x11012012, 0x11012110, 0x11012111, 0x11012112, 0x11012211, 0x11022010,+ 0x11022012, 0x11022111, 0x11022112, 0x11022212, 0x12002112, 0x12002211, 0x12012012, 0x12012111,+ 0x12012112, 0x12012210, 0x12022011, 0x12022110, 0x12022112, 0x12022211, 0x10012122, 0x11002120,+ 0x11002122, 0x11002221, 0x11012121, 0x11012220, 0x11012222, 0x11022120, 0x11022221, 0x12012120,+ 0x12022121, 0x10100001, 0x10100100, 0x10100101, 0x10100102, 0x10100201, 0x10110002, 0x10110101,+ 0x10110202, 0x10120001, 0x10120100, 0x10120201, 0x11100000, 0x11100101, 0x11100200, 0x11110001,+ 0x11110100, 0x11110101, 0x11110102, 0x11110201, 0x11120101, 0x11120200, 0x12100102, 0x12100201,+ 0x12110101, 0x12110200, 0x12120000, 0x12120001, 0x12120102, 0x12120201, 0x10100111, 0x10100210,+ 0x10100211, 0x10100212, 0x10110011, 0x10110110, 0x10110111, 0x10110112, 0x10110210, 0x10110211,+ 0x10120010, 0x10120111, 0x10120112, 0x10120210, 0x10120212, 0x11100011, 0x11100110, 0x11100111,+ 0x11100112, 0x11100211, 0x11110010, 0x11110011, 0x11110012, 0x11110110, 0x11110111, 0x11110112,+ 0x11110210, 0x11110211, 0x11110212, 0x11120011, 0x11120110, 0x11120111, 0x11120112, 0x11120211,+ 0x12100012, 0x12100111, 0x12110011, 0x12110110, 0x12110111, 0x12110112, 0x12110211, 0x12120010,+ 0x12120111, 0x12120212, 0x10100021, 0x10100122, 0x10110022, 0x10110121, 0x10110222, 0x10120021,+ 0x10120120, 0x11100022, 0x11100121, 0x11100222, 0x11110021, 0x11110120, 0x11110121, 0x11110122,+ 0x11110221, 0x11120022, 0x11120121, 0x12100121, 0x12110020, 0x12110022, 0x12110121, 0x12110221,+ 0x12110222, 0x12120120, 0x10101100, 0x10101101, 0x10111001, 0x10111100, 0x10111101, 0x10111102,+ 0x10111200, 0x10111201, 0x10121001, 0x10121101, 0x10121200, 0x10121202, 0x11101001, 0x11101100,+ 0x11101101, 0x11101102, 0x11101201, 0x11101202, 0x11111000, 0x11111001, 0x11111100, 0x11111101,+ 0x11111102, 0x11111200, 0x11111201, 0x11111202, 0x11121001, 0x11121002, 0x11121100, 0x11121101,+ 0x11121102, 0x11121201, 0x12101000, 0x12101200, 0x12101202, 0x12111001, 0x12111100, 0x12111101,+ 0x12111102, 0x12111201, 0x12121001, 0x12121100, 0x12121101, 0x12121202, 0x10101011, 0x10101012,+ 0x10101110, 0x10101111, 0x10101112, 0x10101211, 0x10111010, 0x10111011, 0x10111012, 0x10111110,+ 0x10111111, 0x10111112, 0x10111211, 0x10111212, 0x10121011, 0x10121110, 0x10121111, 0x10121112,+ 0x10121211, 0x11101010, 0x11101011, 0x11101012, 0x11101110, 0x11101111, 0x11101112, 0x11101210,+ 0x11101211, 0x11111010, 0x11111011, 0x11111012, 0x11111110, 0x11111111, 0x11111112, 0x11111210,+ 0x11111211, 0x11111212, 0x11121010, 0x11121011, 0x11121110, 0x11121111, 0x11121112, 0x11121210,+ 0x11121211, 0x11121212, 0x12101011, 0x12101110, 0x12101111, 0x12101211, 0x12101212, 0x12111010,+ 0x12111011, 0x12111110, 0x12111111, 0x12111112, 0x12111210, 0x12111211, 0x12121011, 0x12121110,+ 0x12121111, 0x12121112, 0x12121211, 0x10101020, 0x10101021, 0x10101022, 0x10101120, 0x10101122,+ 0x10101220, 0x10101221, 0x10111021, 0x10111120, 0x10111121, 0x10111220, 0x10111221, 0x10121020,+ 0x10121021, 0x10121022, 0x10121120, 0x10121121, 0x10121122, 0x10121220, 0x10121221, 0x11101021,+ 0x11101121, 0x11101122, 0x11101220, 0x11101221, 0x11101222, 0x11111020, 0x11111021, 0x11111022,+ 0x11111120, 0x11111121, 0x11111122, 0x11111220, 0x11111221, 0x11111222, 0x11121021, 0x11121120,+ 0x11121121, 0x11121221, 0x12101022, 0x12101121, 0x12101122, 0x12101220, 0x12101221, 0x12101222,+ 0x12111021, 0x12111121, 0x12111222, 0x12121022, 0x12121121, 0x12121122, 0x12121220, 0x12121221,+ 0x10102100, 0x10102101, 0x10102102, 0x10102201, 0x10112000, 0x10112101, 0x10112200, 0x10122001,+ 0x10122202, 0x11102101, 0x11102200, 0x11102202, 0x11112001, 0x11112100, 0x11112101, 0x11112102,+ 0x11112200, 0x11112201, 0x11122000, 0x11122002, 0x11122100, 0x11122101, 0x12102002, 0x12102201,+ 0x12112000, 0x12112002, 0x12112101, 0x12112200, 0x12122001, 0x12122201, 0x10102011, 0x10102012,+ 0x10102111, 0x10102212, 0x10112011, 0x10112110, 0x10112111, 0x10112112, 0x10112211, 0x10122111,+ 0x11102011, 0x11102110, 0x11102111, 0x11102112, 0x11102211, 0x11112010, 0x11112011, 0x11112012,+ 0x11112110, 0x11112111, 0x11112112, 0x11112210, 0x11112211, 0x11112212, 0x11122011, 0x11122110,+ 0x11122111, 0x11122112, 0x11122211, 0x12102011, 0x12102111, 0x12102211, 0x12112011, 0x12112110,+ 0x12112111, 0x12112112, 0x12112210, 0x12112211, 0x12122111, 0x10102120, 0x10102220, 0x10112121,+ 0x10112222, 0x10122020, 0x10122121, 0x10122122, 0x10122221, 0x11102121, 0x11102220, 0x11102221,+ 0x11112021, 0x11112121, 0x11112122, 0x11112220, 0x11112221, 0x11122022, 0x11122121, 0x11122220,+ 0x11122222, 0x12102021, 0x12102222, 0x12112022, 0x12112121, 0x12112122, 0x12112220, 0x12112222,+ 0x12122021, 0x10200101, 0x10210100, 0x10210102, 0x10210201, 0x10220101, 0x11200100, 0x11210000,+ 0x11210101, 0x11210102, 0x11210200, 0x11210202, 0x11220001, 0x11220100, 0x11220102, 0x11220201,+ 0x12200001, 0x12210102, 0x12220101, 0x10200011, 0x10200110, 0x10200112, 0x10200211, 0x10210012,+ 0x10210111, 0x10220011, 0x10220012, 0x10220112, 0x10220211, 0x11200111, 0x11200211, 0x11210011,+ 0x11210111, 0x11210112, 0x11210211, 0x11220111, 0x11220112, 0x11220212, 0x12200110, 0x12200212,+ 0x12210012, 0x12210111, 0x12220011, 0x12220112, 0x12220211, 0x10210021, 0x10210122, 0x10210221,+ 0x11200020, 0x11200021, 0x11200122, 0x11210121, 0x11210122, 0x11210220, 0x11220020, 0x12200121,+ 0x12210021, 0x12210122, 0x12220121, 0x10211001, 0x10211002, 0x10211101, 0x10211102, 0x10211202,+ 0x10221001, 0x10221102, 0x10221201, 0x11201000, 0x11201002, 0x11201101, 0x11201200, 0x11201202,+ 0x11211001, 0x11211100, 0x11211101, 0x11211102, 0x11211201, 0x11211202, 0x11221000, 0x11221002,+ 0x11221101, 0x12201100, 0x12201101, 0x12201201, 0x12211000, 0x12211002, 0x12211100, 0x12211101,+ 0x12211102, 0x12211200, 0x12211202, 0x12221001, 0x12221100, 0x12221201, 0x10201111, 0x10201210,+ 0x10201212, 0x10211011, 0x10211111, 0x10211112, 0x10211211, 0x11201110, 0x11201111, 0x11201112,+ 0x11201211, 0x11211010, 0x11211011, 0x11211110, 0x11211111, 0x11211112, 0x11211211, 0x11221011,+ 0x11221110, 0x11221111, 0x11221112, 0x11221211, 0x12201112, 0x12201211, 0x12201212, 0x12211011,+ 0x12211111, 0x12211112, 0x12211211, 0x12211212, 0x12221012, 0x12221111, 0x12221112, 0x12221210,+ 0x10201022, 0x10201221, 0x10211121, 0x10221020, 0x10221122, 0x10221220, 0x10221221, 0x11201020,+ 0x11201121, 0x11201220, 0x11201222, 0x11211021, 0x11211120, 0x11211121, 0x11211122, 0x11211220,+ 0x11211222, 0x11221020, 0x11221121, 0x11221220, 0x12201020, 0x12201022, 0x12201121, 0x12201222,+ 0x12211120, 0x12211122, 0x12211220, 0x12211221, 0x12221020, 0x12221120, 0x12221122, 0x12221222,+ 0x10212102, 0x10212201, 0x10222101, 0x11202001, 0x11212002, 0x11212101, 0x11212202, 0x11222001,+ 0x11222201, 0x12202101, 0x12212001, 0x12212200, 0x12222102, 0x10202011, 0x10202110, 0x10212010,+ 0x10212111, 0x10222011, 0x10222110, 0x10222112, 0x10222211, 0x11202010, 0x11202011, 0x11202111,+ 0x11202112, 0x11202210, 0x11212011, 0x11212110, 0x11212111, 0x11212112, 0x11212211, 0x11222010,+ 0x11222111, 0x11222212, 0x12202012, 0x12202110, 0x12202212, 0x12212111, 0x12222011, 0x12222110,+ 0x12222111, 0x12222211, 0x10212021, 0x10212122, 0x10212220, 0x11202021, 0x11202120, 0x11202221,+ 0x11212020, 0x11212121, 0x11212220, 0x11212222, 0x11222120, 0x11222121, 0x11222221, 0x12202122,+ 0x12212120, 0x12212220, 0x12212222, 0x12222122, 0x20000000, 0x20000002, 0x20000200, 0x20000202,+ 0x20020000, 0x20020002, 0x20020200, 0x20020202, 0x21000101, 0x21010000, 0x21010001, 0x21010100,+ 0x21010102, 0x21010201, 0x21020101, 0x22000000, 0x22000002, 0x22000200, 0x22000202, 0x22010101,+ 0x22020000, 0x22020002, 0x22020200, 0x22020202, 0x20000111, 0x20010011, 0x20010110, 0x20010112,+ 0x20010211, 0x20020111, 0x21000011, 0x21000110, 0x21000211, 0x21010010, 0x21010012, 0x21010111,+ 0x21010112, 0x21010210, 0x21010211, 0x21020110, 0x21020112, 0x21020211, 0x22000111, 0x22000211,+ 0x22010110, 0x22010112, 0x22010211, 0x22020111, 0x20000020, 0x20000022, 0x20000220, 0x20000222,+ 0x20010121, 0x20020020, 0x20020022, 0x20020220, 0x20020222, 0x21010021, 0x21010120, 0x21010221,+ 0x21020121, 0x22000020, 0x22000022, 0x22000220, 0x22000222, 0x22010121, 0x22020020, 0x22020022,+ 0x22020220, 0x22020222, 0x20011100, 0x20011201, 0x21001001, 0x21001100, 0x21011001, 0x21011101,+ 0x21011202, 0x21021001, 0x21021100, 0x21021201, 0x22011100, 0x22011201, 0x20001011, 0x20001211,+ 0x20011012, 0x20011111, 0x20011212, 0x20021112, 0x20021211, 0x21001010, 0x21001011, 0x21001111,+ 0x21001210, 0x21011011, 0x21011110, 0x21011111, 0x21011112, 0x21011211, 0x21011212, 0x21021111,+ 0x21021112, 0x21021210, 0x21021212, 0x22001011, 0x22001110, 0x22001112, 0x22001211, 0x22011010,+ 0x22011012, 0x22011111, 0x22011210, 0x22021112, 0x20011021, 0x20011122, 0x20011221, 0x20021121,+ 0x21001021, 0x21001120, 0x21001221, 0x21001222, 0x21011020, 0x21011121, 0x21011221, 0x21011222,+ 0x21021021, 0x21021122, 0x21021222, 0x22001121, 0x22011021, 0x22011222, 0x22021120, 0x20002000,+ 0x20002002, 0x20002200, 0x20002202, 0x20012101, 0x20022000, 0x20022002, 0x20022200, 0x20022202,+ 0x21002001, 0x21002101, 0x21012001, 0x21012100, 0x21012201, 0x21022101, 0x21022201, 0x22002000,+ 0x22002002, 0x22002200, 0x22002202, 0x22012101, 0x22022000, 0x22022002, 0x22022200, 0x22022202,+ 0x20002111, 0x20002112, 0x20012011, 0x20012110, 0x20012112, 0x20022111, 0x21002011, 0x21002110,+ 0x21002112, 0x21002211, 0x21012010, 0x21012012, 0x21012111, 0x21012212, 0x21022011, 0x21022110,+ 0x22002111, 0x22012112, 0x22012211, 0x22022111, 0x20002020, 0x20002022, 0x20002220, 0x20002222,+ 0x20012121, 0x20022020, 0x20022022, 0x20022220, 0x20022222, 0x21002121, 0x21012021, 0x21012120,+ 0x21012122, 0x22002020, 0x22002022, 0x22002220, 0x22002222, 0x22012121, 0x22022020, 0x22022022,+ 0x22022220, 0x22022222, 0x20100101, 0x20110001, 0x20110102, 0x20110200, 0x20110201, 0x20120101,+ 0x21100001, 0x21100102, 0x21100201, 0x21110101, 0x21110200, 0x21110202, 0x21120201, 0x21120202,+ 0x22100101, 0x22110001, 0x22110100, 0x22110102, 0x22110201, 0x22120101, 0x20100011, 0x20100110,+ 0x20100112, 0x20100211, 0x20110010, 0x20110111, 0x20110210, 0x20110212, 0x20120011, 0x20120110,+ 0x20120112, 0x20120211, 0x21100010, 0x21100111, 0x21110010, 0x21110011, 0x21110110, 0x21110111,+ 0x21110112, 0x21110211, 0x21120012, 0x21120111, 0x22100110, 0x22100112, 0x22110012, 0x22110111,+ 0x22110210, 0x22120011, 0x22120110, 0x22120112, 0x22120211, 0x20100121, 0x20110021, 0x20110120,+ 0x20110221, 0x20120121, 0x21100120, 0x21100122, 0x21100221, 0x21110020, 0x21110022, 0x21110121,+ 0x21110220, 0x21120122, 0x21120221, 0x22100121, 0x22110120, 0x22110122, 0x22120221, 0x20101001,+ 0x20101100, 0x20101102, 0x20111000, 0x20111101, 0x20111200, 0x20121102, 0x21101000, 0x21101202,+ 0x21111001, 0x21111100, 0x21111101, 0x21111102, 0x21111200, 0x21111201, 0x21121000, 0x21121001,+ 0x21121002, 0x21121101, 0x22101100, 0x22101102, 0x22111002, 0x22111100, 0x22111101, 0x22111200,+ 0x22121001, 0x22121201, 0x20101010, 0x20101111, 0x20101210, 0x20101212, 0x20111010, 0x20111011,+ 0x20111110, 0x20111111, 0x20111112, 0x20111211, 0x20121011, 0x20121111, 0x20121211, 0x20121212,+ 0x21101011, 0x21101110, 0x21101111, 0x21101112, 0x21101211, 0x21111010, 0x21111011, 0x21111012,+ 0x21111110, 0x21111111, 0x21111112, 0x21111210, 0x21111211, 0x21111212, 0x21121011, 0x21121110,+ 0x21121111, 0x21121112, 0x21121211, 0x22101011, 0x22101111, 0x22101210, 0x22111011, 0x22111012,+ 0x22111110, 0x22111111, 0x22111112, 0x22111211, 0x22111212, 0x22121010, 0x22121012, 0x22121111,+ 0x22121210, 0x22121212, 0x20101021, 0x20101120, 0x20111020, 0x20111121, 0x20111221, 0x20121020,+ 0x20121122, 0x20121221, 0x21101121, 0x21101220, 0x21101221, 0x21111021, 0x21111022, 0x21111121,+ 0x21111122, 0x21111221, 0x21121121, 0x21121220, 0x22101022, 0x22101120, 0x22101221, 0x22101222,+ 0x22111022, 0x22111120, 0x22111121, 0x22121120, 0x22121122, 0x22121221, 0x20102101, 0x20112102,+ 0x20112201, 0x20122101, 0x21102001, 0x21102102, 0x21112000, 0x21112002, 0x21112101, 0x21112102,+ 0x21112202, 0x21122100, 0x21122101, 0x22102101, 0x22112001, 0x22112102, 0x22112201, 0x22122101,+ 0x20102110, 0x20102112, 0x20102211, 0x20112010, 0x20112012, 0x20112111, 0x20112210, 0x20112212,+ 0x20122010, 0x20122011, 0x20122110, 0x20122112, 0x21102010, 0x21102012, 0x21102111, 0x21102210,+ 0x21102212, 0x21112011, 0x21112110, 0x21112111, 0x21112112, 0x21112211, 0x21122012, 0x21122111,+ 0x21122112, 0x21122212, 0x22102011, 0x22102110, 0x22112010, 0x22112012, 0x22112111, 0x22112212,+ 0x22122011, 0x22122112, 0x20102121, 0x20112121, 0x20122121, 0x21102120, 0x21102122, 0x21102221,+ 0x21112020, 0x21112121, 0x21112220, 0x21122021, 0x22102121, 0x22112021, 0x22112120, 0x22112121,+ 0x22112122, 0x20200000, 0x20200002, 0x20200200, 0x20200202, 0x20210101, 0x20220000, 0x20220002,+ 0x20220200, 0x20220202, 0x21200101, 0x21210001, 0x21210100, 0x21210102, 0x21210201, 0x22200000,+ 0x22200002, 0x22200200, 0x22200202, 0x22210101, 0x22220000, 0x22220002, 0x22220200, 0x22220202,+ 0x20200111, 0x20200211, 0x20210011, 0x20210110, 0x20210112, 0x20210211, 0x20210212, 0x21200112,+ 0x21200211, 0x21210011, 0x21210111, 0x21210210, 0x21210212, 0x21220011, 0x21220110, 0x22200111,+ 0x22210010, 0x22210012, 0x22210112, 0x22210211, 0x20200022, 0x20200220, 0x20200222, 0x20210020,+ 0x20210221, 0x20220022, 0x20220220, 0x20220222, 0x21200121, 0x21210021, 0x21210122, 0x21210221,+ 0x21220121, 0x22200020, 0x22200022, 0x22200220, 0x22200222, 0x22210121, 0x22220020, 0x22220022,+ 0x22220220, 0x22220222, 0x20211201, 0x20221101, 0x21201001, 0x21201100, 0x21211000, 0x21211100,+ 0x21211101, 0x21211200, 0x21211202, 0x21221001, 0x21221101, 0x21221102, 0x21221200, 0x21221201,+ 0x22201101, 0x20201112, 0x20201211, 0x20211010, 0x20211012, 0x20211111, 0x20211210, 0x20221112,+ 0x20221211, 0x21201012, 0x21201111, 0x21211011, 0x21211110, 0x21211111, 0x21211112, 0x21211211,+ 0x21221111, 0x21221212, 0x22201011, 0x22201110, 0x22201111, 0x22201112, 0x22201211, 0x22211012,+ 0x22211111, 0x22211210, 0x20201121, 0x20211021, 0x20211122, 0x20211222, 0x20221021, 0x20221121,+ 0x21201120, 0x21201122, 0x21201222, 0x21211022, 0x21211121, 0x21211122, 0x21211220, 0x21221020,+ 0x21221022, 0x22201122, 0x22211020, 0x22211121, 0x22211122, 0x22211221, 0x22221021, 0x22221120,+ 0x22221122, 0x20202000, 0x20202002, 0x20202200, 0x20202202, 0x20222000, 0x20222002, 0x20222200,+ 0x20222202, 0x21212001, 0x21212100, 0x21212102, 0x21212201, 0x22202000, 0x22202002, 0x22202200,+ 0x22202202, 0x22212101, 0x22222000, 0x22222002, 0x22222200, 0x22222202, 0x20202111, 0x20212110,+ 0x20212211, 0x20222011, 0x20222111, 0x21202011, 0x21212010, 0x21212111, 0x21212212, 0x21222011,+ 0x21222112, 0x21222211, 0x22212010, 0x22212112, 0x20202020, 0x20202022, 0x20202220, 0x20202222,+ 0x20222020, 0x20222022, 0x20222220, 0x20222222, 0x21212021, 0x21212120, 0x21212122, 0x22202020,+ 0x22202022, 0x22202220, 0x22202222, 0x22212121, 0x22222020, 0x22222022, 0x22222220, 0x22222222,+GGML_TABLE_END()++#endif // GGML_COMMON_IMPL+#endif // GGML_COMMON_IMPLdiff --git a/exllamav3/exllamav3_ext/rocm/gmmq/ggml-hip.h b/exllamav3/exllamav3_ext/rocm/gmmq/ggml-hip.hnew file mode 100644index 0000000..722b0e8--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/ggml-hip.h@@ -0,0 +1,8 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/ggml-hip.h, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+// Thin redirect - see ggml.h.+#pragma once+#include "qfn_ggml_stubs.h"diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/ggml-impl.h b/exllamav3/exllamav3_ext/rocm/gmmq/ggml-impl.hnew file mode 100644index 0000000..ec452ea--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/ggml-impl.h@@ -0,0 +1,8 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/ggml-impl.h, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+// Thin redirect - see ggml.h.+#pragma once+#include "qfn_ggml_stubs.h"diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/ggml.h b/exllamav3/exllamav3_ext/rocm/gmmq/ggml.hnew file mode 100644index 0000000..2a2bd67--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/ggml.h@@ -0,0 +1,9 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/ggml.h, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+// Thin redirect so the vendored common.hpp / vecdotq.hpp / mmid.cu / etc.+// can write #include "ggml.h" unchanged.+#pragma once+#include "qfn_ggml_stubs.h"diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/gmmq.h b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq.hnew file mode 100644index 0000000..e4a86e0--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq.h@@ -0,0 +1,11 @@+// SPDX-License-Identifier: MIT+// Kyojin: routed (MoE) HIP MMQ torch op (rocm/gmmq/gmmq_torch.cu). HIP only.+#pragma once+#include <torch/extension.h>++// out[t * n_used + s, :] = dequant(W[ids[t, s]]) @ x[t, :] in fp32, activations quantized to q8_1 (llama.cpp MMQ).+// x: fp16/fp32 [T, K]; ids: int32 [T, n_used]; w_ptrs: int64 [E] device pointers to each expert's raw GGUF blocks+// (M rows of K / blck blocks); fmt: ggml type id (Q4_K 12, Q5_K 13, Q5_1 7, Q8_0 8); out: fp32 [T * n_used, M].+// When K % 256 != 0 the last row of each expert is read up to 256 values past its end; those blocks get zero scales (see gmmq_api.h).+void fq_mmq_moe(torch::Tensor x, torch::Tensor ids, torch::Tensor w_ptrs, int64_t fmt, int64_t M, torch::Tensor out,+ int64_t tile_cols, int64_t max_expert_rows);diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_api.h b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_api.hnew file mode 100644index 0000000..da7f193--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_api.h@@ -0,0 +1,25 @@+// SPDX-License-Identifier: MIT+// Kyojin: host entry points of the routed (MoE) HIP MMQ (rocm/gmmq). Plain header: no vendored ggml names, so the+// torch translation unit (gmmq_torch.cu) can include it next to the torch headers.+#pragma once+#include <hip/hip_runtime.h>+#include <cstddef>+#include <cstdint>++// Bytes of device scratch gmmq_moe() needs for these shapes (id maps + q8_1 activations).+size_t gmmq_moe_scratch_bytes(int type, int K, int n_tokens, int n_experts, int n_expert_used);++// out[t * n_expert_used + s, :M] = W[ids[t, s]] (M x K, raw GGUF blocks of `type`) @ x[t, :K], fp32.+// type: ggml type id (Q4_K 12, Q5_K 13, Q5_1 7, Q8_0 8).+// w_ptrs: device array of n_experts pointers, each to one expert's M rows of K / blck raw blocks (row-major).+// MMQ loads K in steps of 256 values: when K % 256 != 0, the tail of each row reads the next row and the last row+// of an expert reads up to (256 - K % 256) / blck blocks past its end. That memory must be readable; the Q8_0 / Q5_1+// tile loads zero the scales of blocks past the row end, so their contents never reach the result.+// x: device fp32 [n_tokens, K]; ids: device int32 [n_tokens, n_expert_used] (ids outside [0, n_experts) are skipped+// and leave their output row untouched).+// tile_cols / max_expert_rows: 0 = llama.cpp's default column tile (by n_tokens * n_expert_used); otherwise the+// MMQ column tile width / the largest per-expert row count when the caller knows it (Gufo's routed knobs).+// Returns 0, or < 0 on a bad argument / launch error (message on stderr).+int gmmq_moe(int type, const void * const * w_ptrs, const float * x, const int32_t * ids, float * out,+ int M, int K, int n_tokens, int n_experts, int n_expert_used,+ void * scratch, size_t scratch_bytes, int tile_cols, int max_expert_rows, hipStream_t stream);diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case.cuh b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case.cuhnew file mode 100644index 0000000..e79a2e7--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case.cuh@@ -0,0 +1,16 @@+// SPDX-License-Identifier: MIT+// Kyojin: one explicit mul_mat_q_case<type> instantiation per translation unit (gmmq_case_*.cu), like llama.cpp's+// ggml-cuda/template-instances/mmq-instance-*.cu, so the four routed formats compile in parallel.+// The kernels come from rocm/gmmq/mmq.hpp: adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT), itself adapted+// from llama.cpp 5c0e946 (MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+#pragma once+#include "qfn_mmq_prelude.h"+namespace qfn_mmq {+#include "common.hpp"+#include "mmq.hpp"+} // namespace qfn_mmq++#define GMMQ_DECL_CASE(T) \+ namespace qfn_mmq { extern template void mul_mat_q_case<T>(ggml_backend_hip_context &, const mmq_args &, hipStream_t); }+#define GMMQ_INST_CASE(T) \+ namespace qfn_mmq { template void mul_mat_q_case<T>(ggml_backend_hip_context &, const mmq_args &, hipStream_t); }diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q4_k.cu b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q4_k.cunew file mode 100644index 0000000..70e8d06--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q4_k.cu@@ -0,0 +1,6 @@+// SPDX-License-Identifier: MIT+// Kyojin: mul_mat_q_case<GGML_TYPE_Q4_K> instance for the routed (MoE) HIP MMQ. See gmmq_case.cuh.+#if defined(USE_ROCM)+#include "gmmq_case.cuh"+GMMQ_INST_CASE(GGML_TYPE_Q4_K)+#endifdiff --git a/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q5_1.cu b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q5_1.cunew file mode 100644index 0000000..0678fbc--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q5_1.cu@@ -0,0 +1,6 @@+// SPDX-License-Identifier: MIT+// Kyojin: mul_mat_q_case<GGML_TYPE_Q5_1> instance for the routed (MoE) HIP MMQ. See gmmq_case.cuh.+#if defined(USE_ROCM)+#include "gmmq_case.cuh"+GMMQ_INST_CASE(GGML_TYPE_Q5_1)+#endifdiff --git a/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q5_k.cu b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q5_k.cunew file mode 100644index 0000000..540b105--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q5_k.cu@@ -0,0 +1,6 @@+// SPDX-License-Identifier: MIT+// Kyojin: mul_mat_q_case<GGML_TYPE_Q5_K> instance for the routed (MoE) HIP MMQ. See gmmq_case.cuh.+#if defined(USE_ROCM)+#include "gmmq_case.cuh"+GMMQ_INST_CASE(GGML_TYPE_Q5_K)+#endifdiff --git a/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q8_0.cu b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q8_0.cunew file mode 100644index 0000000..acba9f1--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_case_q8_0.cu@@ -0,0 +1,6 @@+// SPDX-License-Identifier: MIT+// Kyojin: mul_mat_q_case<GGML_TYPE_Q8_0> instance for the routed (MoE) HIP MMQ. See gmmq_case.cuh.+#if defined(USE_ROCM)+#include "gmmq_case.cuh"+GMMQ_INST_CASE(GGML_TYPE_Q8_0)+#endifdiff --git a/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_moe.cu b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_moe.cunew file mode 100644index 0000000..93d7e25--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_moe.cu@@ -0,0 +1,157 @@+// SPDX-License-Identifier: MIT+// Kyojin: routed (MoE) HIP MMQ adapter. Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT,+// Copyright (c) 2026 Gufo contributors) src/models/qwen38_flash_next/kernels/rocm/mmq/qfn_mmq.hip.cpp+// (qfn_mmq_moe_impl), itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda/mmq.cu (MIT, Copyright (c) 2023-2026+// The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: routed path only (dense / vector / paired / decode adapters dropped); weights through a per-expert pointer+// table (mmq_args::x_ptrs) instead of one contiguous [E, M, K] tensor; scratch (id maps, q8_1 activations) supplied by+// the caller instead of the ggml pool; no output sanitize pass.+#include "qfn_mmq_prelude.h"+#include "gmmq_api.h"+#include "gmmq_case.cuh"+namespace qfn_mmq {+#include "quantize.hpp"+#include "mmid.hpp"+} // namespace qfn_mmq++GMMQ_DECL_CASE(GGML_TYPE_Q4_K)+GMMQ_DECL_CASE(GGML_TYPE_Q5_K)+GMMQ_DECL_CASE(GGML_TYPE_Q5_1)+GMMQ_DECL_CASE(GGML_TYPE_Q8_0)++namespace qfn_mmq {++static ggml_backend_hip_context * gmmq_ctx_for_device(int device) {+ static std::unique_ptr<ggml_backend_hip_context> cached[GGML_HIP_MAX_DEVICES];+ static std::mutex mutex;+ if (device < 0 || device >= GGML_HIP_MAX_DEVICES) return nullptr;+ const std::lock_guard<std::mutex> lock(mutex);+ if (!cached[device]) cached[device] = std::make_unique<ggml_backend_hip_context>(device);+ return cached[device].get();+}++static bool gmmq_type_ok(int type) {+ return type == GGML_TYPE_Q4_K || type == GGML_TYPE_Q5_K || type == GGML_TYPE_Q5_1 || type == GGML_TYPE_Q8_0;+}++struct gmmq_layout {+ size_t off_src1, off_dst, off_bounds, off_scan, off_q8, total;+};++static size_t gmmq_align(size_t n) { return (n + 255) & ~(size_t) 255; }++static gmmq_layout gmmq_get_layout(int K, int n_tokens, int n_experts, int n_expert_used, int cc) {+ const int64_t rows = (int64_t) n_tokens * n_expert_used;+ const int64_t ne10_padded = GGML_PAD((int64_t) K, MATRIX_ROW_PADDING);+ gmmq_layout l;+ l.off_src1 = 0;+ l.off_dst = l.off_src1 + gmmq_align(rows * sizeof(int32_t));+ l.off_bounds = l.off_dst + gmmq_align(rows * sizeof(int32_t));+ l.off_scan = l.off_bounds + gmmq_align((size_t) (n_experts + 1) * sizeof(int32_t));+ l.off_q8 = l.off_scan + gmmq_align(gmmq_ids_scratch_elems(n_experts, n_tokens, n_expert_used) * sizeof(int32_t));+ const size_t q8 = rows * ne10_padded * sizeof(block_q8_1_mmq) / (4 * QK8_1) ++ get_mmq_x_max_host(cc) * sizeof(block_q8_1_mmq);+ l.total = l.off_q8 + gmmq_align(q8);+ return l;+}++template <ggml_type type>+static int gmmq_moe_impl(const void * const * w_ptrs, const float * X_f32, const int32_t * ids, float * out_f32,+ int M, int K, int n_tokens, int n_experts, int n_expert_used,+ char * scratch, const gmmq_layout & lay, int tile_cols, int max_expert_rows,+ ggml_backend_hip_context & ctx, hipStream_t stream) {+ const int64_t ne_get_rows = (int64_t) n_tokens * n_expert_used;+ const int64_t ne00 = K;+ const int64_t ne10_padded = GGML_PAD((int64_t) K, MATRIX_ROW_PADDING);+ const int64_t ne11 = 1;+ const int64_t ne12 = n_tokens;+ const int64_t s01 = (int64_t) K / ggml_blck_size(type);++ int32_t * ids_src1 = (int32_t *) (scratch + lay.off_src1);+ int32_t * ids_dst = (int32_t *) (scratch + lay.off_dst);+ int32_t * expert_bounds = (int32_t *) (scratch + lay.off_bounds);+ int32_t * scan = (int32_t *) (scratch + lay.off_scan);+ char * src1_q8_1 = scratch + lay.off_q8;++ hipMemsetAsync(ids_src1, 0, ne_get_rows * sizeof(int32_t), stream);+ hipMemsetAsync(ids_dst, 0, ne_get_rows * sizeof(int32_t), stream);++ const int si1 = n_expert_used;+ const int sis1 = 1;+ ggml_hip_launch_mm_ids_helper(ids, ids_src1, ids_dst, expert_bounds, n_experts, n_tokens, n_expert_used,+ (int) ne11, si1, sis1, scan, stream);++ const size_t y_block_size = sizeof(block_q8_1_mmq);+ const size_t y_values_per_block = 4 * QK8_1;+ quantize_mmq_q8_1_hip(X_f32, ids_src1, (void *) src1_q8_1, type, K, (int64_t) K, (int64_t) K * ne11,+ (int64_t) K * ne11 * ne12, ne10_padded, ne_get_rows, 1, 1, stream);++ const int64_t s12_mmq = ne11 * ne10_padded * y_block_size / (y_values_per_block * sizeof(int));+ const int64_t s13_mmq = ne12 * s12_mmq;++ mmq_args args = {+ nullptr, type, (const int *) src1_q8_1, ids_dst, expert_bounds, out_f32,+ ne00, (int64_t) M, ne_get_rows, s01, ne_get_rows, (int64_t) M,+ (int64_t) n_experts, (int64_t) n_experts, (int64_t) M * s01, s12_mmq, (int64_t) 0,+ 1, 1, 0, s13_mmq, 0,+ false, ne_get_rows,+ nullptr, 0,+ };+ args.ncols_grid_max = max_expert_rows > 0 && (int64_t) max_expert_rows < ne_get_rows ? max_expert_rows : 0;+ args.mmq_x_request = tile_cols > 0 ? tile_cols : 0;+ args.x_ptrs = (const char * const *) w_ptrs;+ mul_mat_q_case<type>(ctx, args, stream);+ return 0;+}++} // namespace qfn_mmq++size_t gmmq_moe_scratch_bytes(int type, int K, int n_tokens, int n_experts, int n_expert_used) {+ using namespace qfn_mmq;+ (void) type;+ const int dev = ggml_hip_get_device();+ return gmmq_get_layout(K, n_tokens, n_experts, n_expert_used, ggml_hip_info().devices[dev].cc).total;+}++int gmmq_moe(int type, const void * const * w_ptrs, const float * x, const int32_t * ids, float * out,+ int M, int K, int n_tokens, int n_experts, int n_expert_used,+ void * scratch, size_t scratch_bytes, int tile_cols, int max_expert_rows, hipStream_t stream) {+ using namespace qfn_mmq;+ constexpr const char * tag = "gmmq_moe";+ if (!gmmq_type_ok(type)) {+ fprintf(stderr, "%s: unsupported ggml type %d\n", tag, type);+ return -1;+ }+ if (!w_ptrs || !x || !ids || !out || !scratch) {+ fprintf(stderr, "%s: null pointer\n", tag);+ return -1;+ }+ if (M <= 0 || K <= 0 || n_tokens <= 0 || n_experts <= 0 || n_expert_used <= 0 || n_expert_used > n_experts ||+ K % ggml_blck_size((ggml_type) type) != 0) {+ fprintf(stderr, "%s: bad shape M=%d K=%d ntok=%d nexp=%d nused=%d\n", tag, M, K, n_tokens, n_experts,+ n_expert_used);+ return -1;+ }+ const int dev = ggml_hip_get_device();+ ggml_backend_hip_context * ctx = gmmq_ctx_for_device(dev);+ if (!ctx) return -1;+ const gmmq_layout lay = gmmq_get_layout(K, n_tokens, n_experts, n_expert_used, ggml_hip_info().devices[dev].cc);+ if (scratch_bytes < lay.total) {+ fprintf(stderr, "%s: scratch %zu < %zu bytes\n", tag, scratch_bytes, lay.total);+ return -1;+ }+ char * s = (char *) scratch;+ int rc = -1;+ switch (type) {+ case GGML_TYPE_Q4_K: rc = gmmq_moe_impl<GGML_TYPE_Q4_K>(w_ptrs, x, ids, out, M, K, n_tokens, n_experts, n_expert_used, s, lay, tile_cols, max_expert_rows, *ctx, stream); break;+ case GGML_TYPE_Q5_K: rc = gmmq_moe_impl<GGML_TYPE_Q5_K>(w_ptrs, x, ids, out, M, K, n_tokens, n_experts, n_expert_used, s, lay, tile_cols, max_expert_rows, *ctx, stream); break;+ case GGML_TYPE_Q5_1: rc = gmmq_moe_impl<GGML_TYPE_Q5_1>(w_ptrs, x, ids, out, M, K, n_tokens, n_experts, n_expert_used, s, lay, tile_cols, max_expert_rows, *ctx, stream); break;+ case GGML_TYPE_Q8_0: rc = gmmq_moe_impl<GGML_TYPE_Q8_0>(w_ptrs, x, ids, out, M, K, n_tokens, n_experts, n_expert_used, s, lay, tile_cols, max_expert_rows, *ctx, stream); break;+ }+ const hipError_t err = hipGetLastError();+ if (err != hipSuccess) {+ fprintf(stderr, "%s: launch failed: %s\n", tag, hipGetErrorString(err));+ return -4;+ }+ return rc;+}diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_torch.cu b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_torch.cunew file mode 100644index 0000000..c2a193b--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/gmmq_torch.cu@@ -0,0 +1,40 @@+// SPDX-License-Identifier: MIT+// Kyojin: torch binding of the routed (MoE) HIP MMQ (rocm/gmmq, adapted from Gufo / llama.cpp, MIT; see+// THIRD_PARTY_NOTICES.md). HIP only.+#if defined(USE_ROCM)++#include <ATen/cuda/CUDAContext.h>+#include <c10/cuda/CUDAGuard.h>+#include <torch/extension.h>++#include "gmmq.h"+#include "gmmq_api.h"++void fq_mmq_moe(torch::Tensor x, torch::Tensor ids, torch::Tensor w_ptrs, int64_t fmt, int64_t M, torch::Tensor out,+ int64_t tile_cols, int64_t max_expert_rows)+{+ const at::cuda::OptionalCUDAGuard device_guard(x.device());+ TORCH_CHECK(x.is_cuda() && x.dim() == 2 && x.is_contiguous() &&+ (x.dtype() == torch::kFloat || x.dtype() == torch::kHalf), "fq_mmq_moe: x must be fp32/fp16 [T, K] contiguous");+ TORCH_CHECK(ids.is_cuda() && ids.dim() == 2 && ids.is_contiguous() && ids.dtype() == torch::kInt &&+ ids.size(0) == x.size(0), "fq_mmq_moe: ids must be int32 [T, n_used] contiguous");+ TORCH_CHECK(w_ptrs.is_cuda() && w_ptrs.dim() == 1 && w_ptrs.is_contiguous() && w_ptrs.dtype() == torch::kLong,+ "fq_mmq_moe: w_ptrs must be an int64 device tensor [E]");+ const int64_t T = x.size(0), K = x.size(1), n_used = ids.size(1), E = w_ptrs.size(0);+ TORCH_CHECK(out.is_cuda() && out.dtype() == torch::kFloat && out.is_contiguous() && out.dim() == 2 &&+ out.size(0) == T * n_used && out.size(1) == M, "fq_mmq_moe: out must be fp32 [T * n_used, M] contiguous");+ TORCH_CHECK(T * n_used < (int64_t) 1 << 31 && M * T * n_used < ((int64_t) 1 << 31) && E < 65536,+ "fq_mmq_moe: shape too large");+ if (T == 0) return;+ auto stream = at::cuda::getCurrentCUDAStream().stream();+ torch::Tensor xf = x.dtype() == torch::kFloat ? x : x.to(torch::kFloat);+ const size_t nbytes = gmmq_moe_scratch_bytes((int) fmt, (int) K, (int) T, (int) E, (int) n_used);+ torch::Tensor scratch = torch::empty({(int64_t) nbytes}, x.options().dtype(torch::kUInt8));+ const int rc = gmmq_moe((int) fmt, (const void * const *) w_ptrs.data_ptr(), (const float *) xf.data_ptr(),+ (const int32_t *) ids.data_ptr(), (float *) out.data_ptr(), (int) M, (int) K, (int) T,+ (int) E, (int) n_used, scratch.data_ptr(), nbytes, (int) tile_cols, (int) max_expert_rows,+ stream);+ TORCH_CHECK(rc == 0, "fq_mmq_moe: gmmq_moe failed (rc ", rc, ", fmt ", fmt, ")");+}++#endifdiff --git a/exllamav3/exllamav3_ext/rocm/gmmq/mma.hpp b/exllamav3/exllamav3_ext/rocm/gmmq/mma.hppnew file mode 100644index 0000000..8008e72--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/mma.hpp@@ -0,0 +1,1115 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/mma.hpp, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+#pragma once+// HIP matrix fragments and AMD WMMA/MFMA operations.+// Fragment coordinates are measured in physical 32-bit elements.+// Shared-memory and generic loads require 16-byte aligned pointers.++#include "common.hpp"+++static __device__ __forceinline__ int ggml_hip_movmatrix(const int x) {+ // Imagine transposing row-major matrix to column-major matrix.+ const int src_i_low = 2 * (threadIdx.x % 4);+ const int src_i_high = src_i_low + 1;+ const int src_j = threadIdx.x / 4;++ const int src_laneid_low = src_i_low * 4 + src_j / 2;+ const int src_laneid_high = src_i_high * 4 + src_j / 2;++ const int shift_low = ((src_j + 0) % 2) * 16;+ const int shift_high = ((src_j + 1) % 2) * 16;++ const int ret_low = (__shfl(x, src_laneid_low, WARP_SIZE) >> shift_low) & 0x0000FFFF;+ const int ret_high = (__shfl(x, src_laneid_high, WARP_SIZE) << shift_high) & 0xFFFF0000;++ return ret_low | ret_high;+}++static __device__ __forceinline__ half2 ggml_hip_movmatrix(const half2 x) {+ half2 ret;+ *((int *) &ret) = ggml_hip_movmatrix(*((const int *) &x));+ return ret;+}++namespace ggml_hip_mma {++ // Some architectures like Volta or CDNA3 perform multiple matrix multiplications per warp in parallel,+ // effectively the warp is being split into subgroups of threads that each perform a single mma instruction.+ // In those cases the data can be split in different ways across the warp.+ enum data_layout {+ // By default the data uses the I direction as its major dimension and the J direction as its minor dimension.+ // For the A/C matrices this means I major == row major, J major == column major.+ // For the B matrix this means I major == column major, J major == row major.+ // MIRRORED == Each data value is held exactly once per thread subgroup.+ DATA_LAYOUT_I_MAJOR = 0, // Always used for Turing, Ampere, Ada Lovelace, consumer Blackwell, matrix A&B for RDNA4 and CDNA.+ DATA_LAYOUT_J_MAJOR = 10, // Matrix C for CDNA and RDNA4, int and float matrix C for RDNA3.+ DATA_LAYOUT_I_MAJOR_MIRRORED = 20, // Volta, matrix A&B for RDNA3.+ DATA_LAYOUT_J_MAJOR_MIRRORED = 30,+ DATA_LAYOUT_I_MAJOR_SCRAMBLED = 40, // Scrambled matrix C for faster transposition (RDNA4/CDNA), convert to float to unscramble.+ };+ // Implemented mma combinations are:+ // - (I_MAJOR, I_MAJOR) -> I_MAJOR+ // - (I_MAJOR, I_MAJOR_MIRRORED) -> I_MAJOR+ // - (I_MAJOR, J_MAJOR_MIRRORED) -> I_MAJOR++ static constexpr __device__ data_layout get_input_data_layout() {+#if defined(RDNA3)+ return DATA_LAYOUT_I_MAJOR_MIRRORED;+#else+ return DATA_LAYOUT_I_MAJOR;+#endif // defined(RDNA3)+ }++ template <int I_, int J_, typename T, data_layout ds_=DATA_LAYOUT_I_MAJOR>+ struct tile {};++ template <int I_, int J_, typename T>+ struct tile<I_, J_, T, DATA_LAYOUT_I_MAJOR> {+ static constexpr int I = I_;+ static constexpr int J = J_;+ static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR;++#if defined(AMD_MFMA_AVAILABLE)+ static constexpr int ne = I * J / 64;+ T x[ne] = {0};++ static constexpr __device__ bool supported() {+ if (I == 16 && J == 8) return true;+ if (I == 32 && J == 4) return true;+ if (I == 16 && J == 16) return true;+ if (I == 32 && J == 32) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int l) {+ if constexpr (I == 16 && J == 4) {+ return threadIdx.x % 16;+ } else if constexpr (I == 16 && J == 8) {+ return threadIdx.x % 16;+ } else if constexpr (I == 32 && J == 4) {+ return threadIdx.x % 32;+ } else if constexpr (I == 16 && J == 16) {+ return threadIdx.x % 16;+ } else if constexpr (I == 32 && J == 32) {+ return threadIdx.x % 32;+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }++ static __device__ __forceinline__ int get_j(const int l) {+ if constexpr (I == 16 && J == 4) {+ return threadIdx.x / 16;+ } else if constexpr (I == 16 && J == 8) {+ return 2 * (threadIdx.x / 16) + l;+ } else if constexpr (I == 32 && J == 4) {+ return 2 * (threadIdx.x / 32) + l;+ } else if constexpr (I == 16 && J == 16) {+ return 4 * (threadIdx.x / 16) + l;+ } else if constexpr (I == 32 && J == 32) {+ return 4 * (threadIdx.x / 32) + 8 * (l / 4) + (l % 4);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }+#elif defined(AMD_WMMA_AVAILABLE)+ static constexpr int ne = I * J / 32;+ T x[ne] = {0};++ static constexpr __device__ bool supported() {+ if (I == 16 && J == 16) return true;+ if (I == 16 && J == 8) return true;+ if (I == 16 && J == 4) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int l) {+ if constexpr (supported()) {+ return threadIdx.x % 16;+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }++ static __device__ __forceinline__ int get_j(const int l) {+ if constexpr (I == 16 && J == 16) {+#if defined(RDNA3)+ if constexpr (std::is_same_v<T, float> || std::is_same_v<T, int>) {+ // matrix C+ return 2 * l + (threadIdx.x / 16);+ } else {+ // matrix A&B+ return l;+ }+#else+ // matrix C is the transposed matrix A&B on RDNA4+ return ne * (threadIdx.x / 16) + l;+#endif // defined(RDNA3)+ } else if constexpr (I == 16 && J == 8) {+ // mmq input for RDNA4+ return ne * (threadIdx.x / 16) + l;+ } else if constexpr (I == 16 && J == 4) {+ return ne * (threadIdx.x / 16) + l;+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }+#else+ static constexpr int ne = I * J / 32;+ T x[ne] = {0};++ static constexpr __device__ bool supported() {+ if (I == 8 && J == 4) return true;+ if (I == 8 && J == 8) return true;+ if (I == 16 && J == 8) return true;+ if (I == 16 && J == 16) return true;+ if (I == 32 && J == 8) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int l) {+ if constexpr (I == 8 && J == 4) {+ return threadIdx.x / 4;+ } else if constexpr (I == 8 && J == 8) {+ return threadIdx.x / 4;+ } else if constexpr (I == 16 && J == 8) {+ return ((l / 2) * 8) + (threadIdx.x / 4);+ } else if constexpr (I == 16 && J == 16) {+ return (((l / 2) % 2) * 8) + (threadIdx.x / 4);+ } else if constexpr (I == 32 && J == 8) {+ return tile<16, 8, T>::get_i(l); // Memory layout simply repeated with same pattern in i direction.+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }++ static __device__ __forceinline__ int get_j(const int l) {+ if constexpr (I == 8 && J == 4) {+ return threadIdx.x % 4;+ } else if constexpr (I == 8 && J == 8) {+ return (l * 4) + (threadIdx.x % 4);+ } else if constexpr (I == 16 && J == 8) {+ return ((threadIdx.x % 4) * 2) + (l % 2);+ } else if constexpr (I == 16 && J == 16) {+ return ((l / 4) * 8) + ((threadIdx.x % 4) * 2) + (l % 2);+ } else if constexpr (I == 32 && J == 8) {+ return tile<16, 8, T>::get_j(l); // Memory layout simply repeated with same pattern in i direction.+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }+#endif // 1+ };++ template <int I_, int J_>+ struct tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR> {+ static constexpr int I = I_;+ static constexpr int J = J_;+ static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR;++#if defined(AMD_WMMA_AVAILABLE)+ static constexpr int ne = I * J / 32;+ half2 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ if (I == 16 && J == 8) return true;+ if (I == 16 && J == 16) return true;+ if (I == 32 && J == 8) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int l) {+ if constexpr (I == 16 && J == 8) {+ return threadIdx.x % 16;+ } else if constexpr (I == 16 && J == 16) {+ return threadIdx.x % 16;+ } else if constexpr (I == 32 && J == 8) {+ return (threadIdx.x % 16) * 2 + l / (ne/2);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }++ static __device__ __forceinline__ int get_j(const int l) {+ if constexpr (I == 16 && J == 8) {+ return (threadIdx.x / 16) * ne + l;+ } else if constexpr (I == 16 && J == 16) {+#ifdef RDNA3+ return l*2 + (threadIdx.x / 16);+#else+ return (threadIdx.x / 16) * ne + l;+#endif // RDNA3+ } else if constexpr (I == 32 && J == 8) {+ return (threadIdx.x / 16) * (ne/2) + l % (ne/2);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }+#elif defined(AMD_MFMA_AVAILABLE)+ static constexpr int ne = I * J / 64;+ half2 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ if (I == 16 && J == 8) return true;+ if (I == 16 && J == 16) return true;+ if (I == 32 && J == 8) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int l) {+ if constexpr (I == 16 && J == 8) {+ return threadIdx.x % 16;+ } else if constexpr (I == 16 && J == 16) {+ return threadIdx.x % 16;+ } else if constexpr (I == 32 && J == 8) {+ return (threadIdx.x % 16) * 2 + l / (ne/2);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }++ static __device__ __forceinline__ int get_j(const int l) {+ if constexpr (I == 16 && J == 8) {+ return (threadIdx.x / 16) * ne + l;+ } else if constexpr (I == 16 && J == 16) {+ return (threadIdx.x / 16) * ne + l;+ } else if constexpr (I == 32 && J == 8) {+ return (threadIdx.x / 16) * (ne/2) + l % (ne/2);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }+#else+ static constexpr int ne = I * J / WARP_SIZE;+ half2 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ if (I == 8 && J == 4) return true;+ if (I == 8 && J == 8) return true;+ if (I == 16 && J == 8) return true;+ if (I == 16 && J == 16) return true;+ if (I == 32 && J == 8) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int l) {+ if constexpr (I == 8 && J == 8) {+ return threadIdx.x / 4;+ } else if constexpr (I == 16 && J == 4) {+ return (l * 8) + (threadIdx.x / 4);+ } else if constexpr (I == 16 && J == 8) {+ return ((l % 2) * 8) + (threadIdx.x / 4);+ } else if constexpr (I == 32 && J == 8) {+ return ((l / 4) * 16) + ((l % 2) * 8) + (threadIdx.x / 4);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }++ static __device__ __forceinline__ int get_j(const int l) {+ if constexpr (I == 8 && J == 8) {+ return (l * 4) + (threadIdx.x % 4);+ } else if constexpr (I == 16 && J == 4) {+ return threadIdx.x % 4;+ } else if constexpr (I == 16 && J == 8) {+ return ((l / 2) * 4) + (threadIdx.x % 4);+ } else if constexpr (I == 32 && J == 8) {+ return ((l & 2) * 2) + (threadIdx.x % 4);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }+#endif // defined(VOLTA_MMA_AVAILABLE)+ };++ template <int I_, int J_>+ struct tile<I_, J_, __hip_bfloat162, DATA_LAYOUT_I_MAJOR> {+ static constexpr int I = I_;+ static constexpr int J = J_;+ static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR;++#if defined(AMD_WMMA_AVAILABLE)+ static constexpr int ne = tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR>::ne;+ __hip_bfloat162 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ return tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR>::supported();+ }++ static __device__ __forceinline__ int get_i(const int l) {+ return tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR>::get_i(l);+ }++ static __device__ __forceinline__ int get_j(const int l) {+ return tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR>::get_j(l);+ }+#elif defined(AMD_MFMA_AVAILABLE)+ static constexpr int ne = tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR>::ne;+ __hip_bfloat162 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ return tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR>::supported();+ }++ static __device__ __forceinline__ int get_i(const int l) {+ return tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR>::get_i(l);+ }++ static __device__ __forceinline__ int get_j(const int l) {+ return tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR>::get_j(l);+ }+#else+ static constexpr int ne = I * J / WARP_SIZE;+ __hip_bfloat162 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ if (I == 8 && J == 8) return true;+ if (I == 16 && J == 4) return true;+ if (I == 16 && J == 8) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int l) {+ if constexpr (I == 8 && J == 8) {+ return threadIdx.x / 4;+ } else if constexpr (I == 16 && J == 4) {+ return (l * 8) + (threadIdx.x / 4);+ } else if constexpr (I == 16 && J == 8) {+ return ((l % 2) * 8) + (threadIdx.x / 4);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }++ static __device__ __forceinline__ int get_j(const int l) {+ if constexpr (I == 8 && J == 8) {+ return (l * 4) + (threadIdx.x % 4);+ } else if constexpr (I == 16 && J == 4) {+ return threadIdx.x % 4;+ } else if constexpr (I == 16 && J == 8) {+ return ((l / 2) * 4) + (threadIdx.x % 4);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }+#endif // defined(AMD_WMMA_AVAILABLE)+ };++ template <int I_, int J_, typename T>+ struct tile<I_, J_, T, DATA_LAYOUT_J_MAJOR> {+ static constexpr int I = I_;+ static constexpr int J = J_;+ static constexpr data_layout dl = DATA_LAYOUT_J_MAJOR;++ static constexpr int ne = tile<I_, J_, T, DATA_LAYOUT_I_MAJOR>::ne;+ T x[ne] = {0};++ static constexpr __device__ bool supported() {+ return tile<I_, J_, T, DATA_LAYOUT_I_MAJOR>::supported();+ }++ static __device__ __forceinline__ int get_i(const int l) {+ return tile<I_, J_, T, DATA_LAYOUT_I_MAJOR>::get_j(l);+ }++ static __device__ __forceinline__ int get_j(const int l) {+ return tile<I_, J_, T, DATA_LAYOUT_I_MAJOR>::get_i(l);+ }+ };++ template <int I_, int J_, typename T>+ struct tile<I_, J_, T, DATA_LAYOUT_I_MAJOR_MIRRORED> {+ static constexpr int I = I_;+ static constexpr int J = J_;+ static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR_MIRRORED;++ // RDNA3+ static constexpr int ne = I * J / 32 * 2;++ T x[ne] = {0};++ static constexpr __device__ bool supported() {+ if (I == 16 && J == 16) return true;+ if (I == 16 && J == 8) return true;+ if (I == 16 && J == 4) return true;+ if (I == 32 && J == 8) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int l) {+ if constexpr (I == 16) {+ return threadIdx.x % 16;+ } else if constexpr (I == 32) {+ return (threadIdx.x % 16) * 2 + l / (ne/2);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }++ static __device__ __forceinline__ int get_j(const int l) {+ if constexpr (I == 16) {+ return l;+ } else if constexpr (I == 32) {+ return l % (ne/2);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }+ };++ template <int I_, int J_>+ struct tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> {+ static constexpr int I = I_;+ static constexpr int J = J_;+ static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR_MIRRORED;+#if defined(RDNA3)+ static constexpr int ne = tile<I_, J_, float, DATA_LAYOUT_I_MAJOR_MIRRORED>::ne;++ half2 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ return tile<I_, J_, float, DATA_LAYOUT_I_MAJOR_MIRRORED>::supported();+ }++ static __device__ __forceinline__ int get_i(const int l) {+ return tile<I_, J_, float, DATA_LAYOUT_I_MAJOR_MIRRORED>::get_i(l);+ }++ static __device__ __forceinline__ int get_j(const int l) {+ return tile<I_, J_, float, DATA_LAYOUT_I_MAJOR_MIRRORED>::get_j(l);+ }+#else // Volta+ static constexpr int ne = I * J / (WARP_SIZE/4);++ half2 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ if (I == 8 && J == 4) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int /*l*/) {+ if constexpr (I == 8 && J == 4) {+ return ((threadIdx.x / 16) * 4) + (threadIdx.x % 4);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }++ static __device__ __forceinline__ int get_j(const int l) {+ if constexpr (I == 8 && J == 4) {+ return l;+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }+#endif // defined(RDNA3)+ };++ template <int I_, int J_>+ struct tile<I_, J_, __hip_bfloat162, DATA_LAYOUT_I_MAJOR_MIRRORED> {+ static constexpr int I = I_;+ static constexpr int J = J_;+ static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR_MIRRORED;+ static constexpr int ne = tile<I_, J_, float, DATA_LAYOUT_I_MAJOR_MIRRORED>::ne;++ __hip_bfloat162 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ return tile<I_, J_, float, DATA_LAYOUT_I_MAJOR_MIRRORED>::supported();+ }++ static __device__ __forceinline__ int get_i(const int l) {+ return tile<I_, J_, float, DATA_LAYOUT_I_MAJOR_MIRRORED>::get_i(l);+ }++ static __device__ __forceinline__ int get_j(const int l) {+ return tile<I_, J_, float, DATA_LAYOUT_I_MAJOR_MIRRORED>::get_j(l);+ }+ };++ template <int I_, int J_>+ struct tile<I_, J_, half2, DATA_LAYOUT_J_MAJOR_MIRRORED> {+ static constexpr int I = I_;+ static constexpr int J = J_;+ static constexpr data_layout dl = DATA_LAYOUT_J_MAJOR_MIRRORED;+ static constexpr int ne = I * J / (WARP_SIZE/4);++ half2 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ if (I == 8 && J == 4) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int l) {+ if constexpr (I == 8 && J == 4) {+ return ((l / 2) * 4) + (threadIdx.x % 4);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }++ static __device__ __forceinline__ int get_j(const int l) {+ if constexpr (I == 8 && J == 4) {+ return ((threadIdx.x / 16) * 2) + (l % 2);+ } else {+ NO_DEVICE_CODE;+ return -1;+ }+ }+ };++ template <int I_, int J_>+ struct tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR_SCRAMBLED> {+ static constexpr int I = I_;+ static constexpr int J = J_;+ static constexpr data_layout dl = DATA_LAYOUT_I_MAJOR_SCRAMBLED;++ static constexpr int ne = I * J / ggml_hip_get_physical_warp_size();+ half2 x[ne] = {{0.0f, 0.0f}};++ static constexpr __device__ bool supported() {+ if (I == 16 && J == 16) return true;+ return false;+ }++ static __device__ __forceinline__ int get_i(const int l) {+ return tile<I_, J_, half2, DATA_LAYOUT_I_MAJOR>::get_i(l);+ }+ };++ static __device__ __forceinline__ tile<16, 16, half2, DATA_LAYOUT_I_MAJOR> unscramble(const tile<16, 16, half2, DATA_LAYOUT_I_MAJOR_SCRAMBLED> & t) {+#if defined(AMD_MFMA_AVAILABLE) || (defined(AMD_WMMA_AVAILABLE) && defined(RDNA4))+ tile<16, 16, half2, DATA_LAYOUT_I_MAJOR> ret;+#pragma unroll+ for (int l0 = 0; l0 < t.ne/2; ++l0) {+ ret.x[2*l0 + 0] = __lows2half2(t.x[l0], t.x[l0 + t.ne/2]);+ ret.x[2*l0 + 1] = __highs2half2(t.x[l0], t.x[l0 + t.ne/2]);+ }+ return ret;+#else+ NO_DEVICE_CODE;+ GGML_UNUSED(t);+#endif // defined(AMD_MFMA_AVAILABLE) || (defined(AMD_WMMA_AVAILABLE) && defined(RDNA4))+ }++#if defined(AMD_WMMA_AVAILABLE) && defined(RDNA3)+ static __device__ __forceinline__ tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> get_half2(+ const tile<16, 16, float, DATA_LAYOUT_I_MAJOR> & tile_float) {+ tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> ret;+#pragma unroll+ for (int l = 0; l < tile_float.ne; ++l) {+ float tmp[2];+ int i = threadIdx.x / 16;+ tmp[i] = tile_float.x[l];+ i ^= 1;+ tmp[i] = __shfl_xor(tile_float.x[l], 16, WARP_SIZE);+ ret.x[l] = make_half2(tmp[0], tmp[1]);+ }+ return ret;+ }+#elif defined(AMD_WMMA_AVAILABLE) || defined(AMD_MFMA_AVAILABLE)+ template <int I, int J>+ static __device__ __forceinline__ tile<I, J/2, half2> get_half2(const tile<I, J, float> & tile_float) {+ tile<I, J/2, half2> ret;+#pragma unroll+ for (int l0 = 0; l0 < tile_float.ne; l0 += 2) {+ ret.x[l0/2] = make_half2(tile_float.x[l0 + 0], tile_float.x[l0 + 1]);+ }+ return ret;+ }++ static __device__ __forceinline__ tile<8, 8, half2> get_transposed(const tile<16, 4, half2> & t) {+ NO_DEVICE_CODE;+ return tile<8, 8, half2>{};+ }+#else // Volta+ template <int I, int J>+ static __device__ __forceinline__ tile<I, J/2, half2> get_half2(const tile<I, J, float> & tile_float) {+ tile<I, J/2, half2> ret;+#pragma unroll+ for (int l0 = 0; l0 < tile_float.ne; l0 += 4) {+ ret.x[l0/2 + 0] = make_half2(tile_float.x[l0 + 0], tile_float.x[l0 + 1]);+ ret.x[l0/2 + 1] = make_half2(tile_float.x[l0 + 2], tile_float.x[l0 + 3]);++ // On Volta FP16 and FP32 tiles have a different memory layout,+ // for the conversion threads with an offset of 2 need to exchange half their values:+ ret.x[l0/2 + (((threadIdx.x % 4) / 2) ^ 1)] = __shfl_xor(ret.x[l0/2 + (((threadIdx.x % 4) / 2) ^ 1)], 2, WARP_SIZE);+ }+ return ret;+ }+#endif // defined(TURING_MMA_AVAILABLE)++ template <int I, int J, typename T, data_layout dl>+ static __device__ __forceinline__ void load_generic(tile<I, J, T, dl> & t, const T * __restrict__ xs0, const int stride) {+#pragma unroll+ for (int l = 0; l < t.ne; ++l) {+ t.x[l] = xs0[t.get_i(l)*stride + t.get_j(l)];+ }+ }++ template <typename T>+ static __device__ __forceinline__ void load_ldmatrix(+ tile<8, 8, T> & t, const T * __restrict__ xs0, const int stride) {+ GGML_UNUSED_VARS(t, xs0, stride);+ NO_DEVICE_CODE;+ }++ template <typename T, data_layout dl>+ static __device__ __forceinline__ void load_ldmatrix(+ tile<16, 4, T, dl> & t, const T * __restrict__ xs0, const int stride) {+#if defined(AMD_WMMA_AVAILABLE)+#ifdef RDNA3+ static_assert(dl == DATA_LAYOUT_I_MAJOR_MIRRORED, "bad data layout");+ static_assert(sizeof(t.x) == 16, "bad ne");+ ggml_hip_memcpy_1<8>(t.x + 0, xs0 + t.get_i(0)*stride + 0);+ ggml_hip_memcpy_1<8>(t.x + 2, xs0 + t.get_i(0)*stride + 2);+#else+ static_assert(dl == DATA_LAYOUT_I_MAJOR, "bad data layout");+ static_assert(sizeof(t.x) == 8, "bad ne");+ ggml_hip_memcpy_1<8>(t.x, xs0 + t.get_i(0)*stride + t.get_j(0));+#endif // RDNA3+#elif defined(AMD_MFMA_AVAILABLE)+ static_assert(sizeof(t.x) == 4, "bad ne");+ ggml_hip_memcpy_1<4>(t.x, xs0 + t.get_i(0)*stride + t.get_j(0));+#else+ GGML_UNUSED_VARS(t, xs0, stride);+ NO_DEVICE_CODE;+#endif // TURING_MMA_AVAILABLE+ }++ template <typename T, data_layout dl>+ static __device__ __forceinline__ void load_ldmatrix(+ tile<16, 8, T, dl> & t, const T * __restrict__ xs0, const int stride) {+#if defined(AMD_WMMA_AVAILABLE)+#ifdef RDNA3+ static_assert(dl == DATA_LAYOUT_I_MAJOR_MIRRORED, "bad data layout");+ static_assert(sizeof(t.x) == 32, "bad ne");+ ggml_hip_memcpy_1<16>(t.x + 0, xs0 + t.get_i(0)*stride + 0);+ ggml_hip_memcpy_1<16>(t.x + 4, xs0 + t.get_i(0)*stride + 4);+#else+ static_assert(dl == DATA_LAYOUT_I_MAJOR, "bad data layout");+ static_assert(sizeof(t.x) == 16, "bad ne");+ ggml_hip_memcpy_1<16>(t.x, xs0 + t.get_i(0)*stride + t.get_j(0));+#endif // RDNA3+#elif defined(AMD_MFMA_AVAILABLE)+ static_assert(sizeof(t.x) == 8, "bad ne");+ ggml_hip_memcpy_1<8>(t.x, xs0 + t.get_i(0)*stride + t.get_j(0));+#else+ GGML_UNUSED_VARS(t, xs0, stride);+ NO_DEVICE_CODE;+#endif // TURING_MMA_AVAILABLE+ }++ static __device__ __forceinline__ void load_ldmatrix(+ tile<8, 4, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> & t, const half2 * __restrict__ xs0, const int stride) {+ ggml_hip_memcpy_1<4*sizeof(half2)>(t.x, xs0 + t.get_i(0)*stride);+ }++ static __device__ __forceinline__ void load_ldmatrix(+ tile<8, 4, half2, DATA_LAYOUT_J_MAJOR_MIRRORED> & t, const half2 * __restrict__ xs0, const int stride) {+#pragma unroll+ for (int l0 = 0; l0 < t.ne; l0 += 2) {+ ggml_hip_memcpy_1<2*sizeof(half2)>(t.x + l0, xs0 + t.get_i(l0)*stride + t.get_j(l0));+ }+ }++ static __device__ __forceinline__ void load_ldmatrix(+ tile<32, 4, half2> & t, const half2 * __restrict__ xs0, const int stride) {+ GGML_UNUSED_VARS(t, xs0, stride);+ NO_DEVICE_CODE;+ }++ template <int I, typename T, data_layout dl>+ static __device__ __forceinline__ void load_ldmatrix_trans(+ tile<I, 8, T, dl> & t, const T * __restrict__ xs0, const int stride) {+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ static_assert(dl == DATA_LAYOUT_I_MAJOR || dl == DATA_LAYOUT_I_MAJOR_MIRRORED, "bad data layout");+ if constexpr (I == 32) {+#pragma unroll+ for (int l0 = 0; l0 < t.ne/2; ++l0) {+ const half2 tmp0 = xs0[(2*t.get_j(l0) + 0)*stride + t.get_i(l0)/2];+ const half2 tmp1 = xs0[(2*t.get_j(l0) + 1)*stride + t.get_i(l0)/2];++ t.x[l0] = __lows2half2(tmp0, tmp1);+ t.x[l0 + t.ne/2] = __highs2half2(tmp0, tmp1);+ }+ } else {+ half * xh = (half *) t.x;+#pragma unroll+ for (int l = 0; l < t.ne; ++l) {+ xh[2*l + 0] = ((const half *) xs0)[(2*t.get_j(l) + 0)*(2*stride) + t.get_i(l)];+ xh[2*l + 1] = ((const half *) xs0)[(2*t.get_j(l) + 1)*(2*stride) + t.get_i(l)];+ }+ }+#else+ GGML_UNUSED_VARS(t, xs0, stride);+ NO_DEVICE_CODE;+#endif // TURING_MMA_AVAILABLE+ }++ static __device__ __forceinline__ void mma(+ tile<16, 8, int> & D, const tile<16, 4, int> & A, const tile<8, 4, int> & B) {+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+ }++ static __device__ __forceinline__ void mma(+ tile<16, 8, int> & D, const tile<16, 8, int> & A, const tile<8, 8, int> & B) {+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+ }++ static __device__ __forceinline__ void mma(+ tile<16, 4, half2> & D, const tile<16, 8, half2> & A, const tile<8, 8, half2> & B) {+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+ }++ static __device__ __forceinline__ void mma(+ tile<16, 8, half2> & D, const tile<16, 8, half2> & A, const tile<16, 8, half2> & B) {+#if defined(AMD_WMMA_AVAILABLE)+#if defined(RDNA4)+ using halfx8_t = __attribute__((ext_vector_type(8))) _Float16;+ halfx8_t& acc_frag = reinterpret_cast<halfx8_t&>(D.x[0]);+ const halfx8_t& a_frag = reinterpret_cast<const halfx8_t&>(A.x[0]);+ const halfx8_t& b_frag = reinterpret_cast<const halfx8_t&>(B.x[0]);+ acc_frag = __builtin_amdgcn_wmma_f16_16x16x16_f16_w32_gfx12(a_frag, b_frag, acc_frag);+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // defined(RDNA4)+#elif defined(AMD_MFMA_AVAILABLE)+ // MFMA: FP16 input, FP32 accumulate, convert back to half2.+ using halfx4_t = __attribute__((ext_vector_type(4))) _Float16;+ using floatx4_t = __attribute__((ext_vector_type(4))) float;++ // Convert existing half2 accumulator to float for MFMA:+ floatx4_t acc_f32;+ {+ const halfx4_t acc_h = reinterpret_cast<const halfx4_t&>(D.x[0]);+#pragma unroll+ for (int i = 0; i < 4; ++i) {+ acc_f32[i] = (float)acc_h[i];+ }+ }++ const halfx4_t& a_frag = reinterpret_cast<const halfx4_t&>(A.x[0]);+ const halfx4_t& b_frag = reinterpret_cast<const halfx4_t&>(B.x[0]);+ acc_f32 = __builtin_amdgcn_mfma_f32_16x16x16f16(a_frag, b_frag, acc_f32, 0, 0, 0);++ // Convert back to half2:+ {+ halfx4_t result_h;+#pragma unroll+ for (int i = 0; i < 4; ++i) {+ result_h[i] = (_Float16)acc_f32[i];+ }+ reinterpret_cast<halfx4_t&>(D.x[0]) = result_h;+ }+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // TURING_MMA_AVAILABLE+ }++ static __device__ __forceinline__ void mma(+ tile<16, 16, half2, DATA_LAYOUT_I_MAJOR_SCRAMBLED> & D, const tile<32, 8, half2, DATA_LAYOUT_I_MAJOR> & A,+ const tile<16, 8, half2, DATA_LAYOUT_I_MAJOR> & B) {+#if defined(AMD_MFMA_AVAILABLE) || (defined(AMD_WMMA_AVAILABLE) && defined(RDNA4))+ tile<16, 8, half2> * D16 = (tile<16, 8, half2> *) &D;+ const tile<16, 8, half2> * A16 = (const tile<16, 8, half2> *) &A;+ mma(D16[0], A16[0], B);+ mma(D16[1], A16[1], B);+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE) && defined(RDNA4)+ }++ template <data_layout dl_ab, data_layout dl_d>+ static __device__ __forceinline__ void mma(+ tile<16, 8, float, dl_d> & D, const tile<16, 8, float, dl_ab> & A, const tile<8, 8, float, dl_ab> & B) {+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+ }++ template <data_layout dl_ab, data_layout dl_d>+ static __device__ __forceinline__ void mma(+ tile<16, 16, float, dl_d> & D, const tile<16, 8, float, dl_ab> & A, const tile<16, 8, float, dl_ab> & B) {+#ifdef AMD_MFMA_AVAILABLE+ using floatx4_t = __attribute__((ext_vector_type(4))) float;+ floatx4_t& acc_frag = reinterpret_cast<floatx4_t&>(D.x[0]);+#if defined(CDNA3)+ using floatx2_t = __attribute__((ext_vector_type(2))) float;+ const floatx2_t& a_frag = reinterpret_cast<const floatx2_t&>(A.x[0]);+ const floatx2_t& b_frag = reinterpret_cast<const floatx2_t&>(B.x[0]);+ acc_frag = __builtin_amdgcn_mfma_f32_16x16x8_xf32(a_frag, b_frag, acc_frag, 0, 0, 0);+#elif defined(CDNA4) || defined(CDNA2) || defined(CDNA1)+ // CDNA4 (gfx950) does not support xf32 MFMA, use f32 path like CDNA2/CDNA1+#pragma unroll+ for (int i = 0; i < 2; ++i) {+ acc_frag = __builtin_amdgcn_mfma_f32_16x16x4f32(A.x[i], B.x[i], acc_frag, 0, 0, 0);+ }+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // defined(CDNA3)+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // AMD_MFMA_AVAILABLE+ }++ template <ggml_type type>+ static __device__ __forceinline__ void mma_block_scaled_fp4(tile<16, 8, float> & D,+ const tile<16, 8, int> & A,+ const tile<8, 8, int> & B,+ uint32_t a_scale,+ uint32_t b_scale) {+ GGML_UNUSED_VARS(D, A, B, a_scale, b_scale);+ }++ static __device__ __forceinline__ void mma(+ tile<16, 8, float> & D, const tile<16, 8, half2> & A, const tile<8, 8, half2> & B) {+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+ }++ static __device__ __forceinline__ void mma(+ tile<16, 8, float> & D, const tile<16, 8, __hip_bfloat162> & A, const tile<8, 8, __hip_bfloat162> & B) {+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+ }++ template <data_layout dl_ab, data_layout dl_d>+ static __device__ __forceinline__ void mma(+ tile<16, 16, float, dl_d> & D, const tile<16, 8, half2, dl_ab> & A, const tile<16, 8, half2, dl_ab> & B) {+#if defined(AMD_WMMA_AVAILABLE)+#if defined(RDNA4)+ using halfx8_t = __attribute__((ext_vector_type(8))) _Float16;+ using floatx8_t = __attribute__((ext_vector_type(8))) float;+ floatx8_t& acc_frag = reinterpret_cast<floatx8_t&>(D.x[0]);+ const halfx8_t& a_frag = reinterpret_cast<const halfx8_t&>(A.x[0]);+ const halfx8_t& b_frag = reinterpret_cast<const halfx8_t&>(B.x[0]);+ acc_frag = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(a_frag, b_frag, acc_frag);+#elif defined(RDNA3)+ using halfx16_t = __attribute__((ext_vector_type(16))) _Float16;+ using floatx8_t = __attribute__((ext_vector_type(8))) float;+ floatx8_t& acc_frag = reinterpret_cast<floatx8_t&>(D.x[0]);+ const halfx16_t& a_frag = reinterpret_cast<const halfx16_t&>(A.x[0]);+ const halfx16_t& b_frag = reinterpret_cast<const halfx16_t&>(B.x[0]);+ acc_frag = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a_frag, b_frag, acc_frag);+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // RDNA4+#elif defined(AMD_MFMA_AVAILABLE)+ using halfx4_t = __attribute__((ext_vector_type(4))) _Float16;+ using floatx4_t = __attribute__((ext_vector_type(4))) float;+ floatx4_t& acc_frag = reinterpret_cast<floatx4_t&>(D.x[0]);+ const halfx4_t& a_frag = reinterpret_cast<const halfx4_t&>(A.x[0]);+ const halfx4_t& b_frag = reinterpret_cast<const halfx4_t&>(B.x[0]);+ acc_frag = __builtin_amdgcn_mfma_f32_16x16x16f16(a_frag, b_frag, acc_frag, 0, 0, 0);+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // TURING_MMA_AVAILABLE+ }++ template <data_layout dl_ab, data_layout dl_d>+ static __device__ __forceinline__ void mma(+ tile<16, 16, float, dl_d> & D, const tile<16, 8, __hip_bfloat162, dl_ab> & A, const tile<16, 8, __hip_bfloat162, dl_ab> & B) {+#if defined(AMD_WMMA_AVAILABLE)+#if defined(RDNA4)+ using bf16x8_t = __attribute__((ext_vector_type(8))) __bf16;+ using floatx8_t = __attribute__((ext_vector_type(8))) float;+ floatx8_t& acc_frag = reinterpret_cast<floatx8_t&>(D.x[0]);+ const bf16x8_t& a_frag = reinterpret_cast<const bf16x8_t&>(A.x[0]);+ const bf16x8_t& b_frag = reinterpret_cast<const bf16x8_t&>(B.x[0]);+ acc_frag = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12(a_frag, b_frag, acc_frag);+#elif defined(RDNA3)+ using bf16x16_t = __attribute__((ext_vector_type(16))) short;+ using floatx8_t = __attribute__((ext_vector_type(8))) float;+ floatx8_t& acc_frag = reinterpret_cast<floatx8_t&>(D.x[0]);+ const bf16x16_t& a_frag = reinterpret_cast<const bf16x16_t&>(A.x[0]);+ const bf16x16_t& b_frag = reinterpret_cast<const bf16x16_t&>(B.x[0]);+ acc_frag = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a_frag, b_frag, acc_frag);+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // defined(RDNA4)+#elif defined(AMD_MFMA_AVAILABLE)+ using floatx4_t = __attribute__((ext_vector_type(4))) float;+ floatx4_t& acc_frag = reinterpret_cast<floatx4_t&>(D.x[0]);+#if defined(CDNA4) || defined(CDNA3) || defined(CDNA2)+ using bf16x4_t = __attribute__((ext_vector_type(4))) __bf16;+ const bf16x4_t& a_frag = reinterpret_cast<const bf16x4_t&>(A.x[0]);+ const bf16x4_t& b_frag = reinterpret_cast<const bf16x4_t&>(B.x[0]);+ acc_frag = __builtin_amdgcn_mfma_f32_16x16x16bf16_1k(a_frag, b_frag, acc_frag, 0, 0, 0);+#elif defined(CDNA1)+#pragma unroll+ for (int i = 0; i < 2; ++i) {+ using bf16x2_t = __attribute__((ext_vector_type(2))) __bf16;+ const bf16x2_t& a_frag = reinterpret_cast<const bf16x2_t&>(A.x[i]);+ const bf16x2_t& b_frag = reinterpret_cast<const bf16x2_t&>(B.x[i]);+ acc_frag = __builtin_amdgcn_mfma_f32_16x16x8bf16(a_frag, b_frag, acc_frag, 0, 0, 0);+ }+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // defined(CDNA3) || defined(CDNA2)+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // defined(AMD_WMMA_AVAILABLE)+ }++ template <data_layout dl_d, data_layout dl_ab>+ static __device__ __forceinline__ void mma(+ tile<16, 16, int, dl_d> & D, const tile<16, 8, int, dl_ab> & A, const tile<16, 8, int, dl_ab> & B) {+#if defined(AMD_MFMA_AVAILABLE)+ using int32x4_t = __attribute__((__vector_size__(4 * sizeof(int)))) int;+ int32x4_t * acc = (int32x4_t *) D.x;+#if defined(CDNA4) || defined(CDNA3)+ acc[0] = __builtin_amdgcn_mfma_i32_16x16x32_i8(((int64_t *) A.x)[0], ((int64_t *) B.x)[0], acc[0], 0, 0, 0);+#elif defined(CDNA2) || defined(CDNA1)+ acc[0] = __builtin_amdgcn_mfma_i32_16x16x16i8(A.x[0], B.x[0], acc[0], 0, 0, 0);+ acc[0] = __builtin_amdgcn_mfma_i32_16x16x16i8(A.x[1], B.x[1], acc[0], 0, 0, 0);+#endif // defined(CDNA4) || defined(CDNA3)+#elif defined(AMD_WMMA_AVAILABLE)+ using int32x8_t = __attribute__((__vector_size__(8 * sizeof(int)))) int;+ int32x8_t * acc = (int32x8_t *) D.x;+#if defined(RDNA4)+ using int32x2_t = __attribute__((__vector_size__(2 * sizeof(int)))) int;+ int32x2_t * a_vec = (int32x2_t *) A.x;+ int32x2_t * b_vec = (int32x2_t *) B.x;+ acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a_vec[0], true, b_vec[0], acc[0], true);+ acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a_vec[1], true, b_vec[1], acc[0], true);+#elif defined(RDNA3)+ using int32x4_t = __attribute__((__vector_size__(4 * sizeof(int)))) int;+ int32x4_t * a_vec = (int32x4_t *) A.x;+ int32x4_t * b_vec = (int32x4_t *) B.x;+ acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a_vec[0], true, b_vec[0], acc[0], true);+ acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a_vec[1], true, b_vec[1], acc[0], true);+#endif // RDNA4+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // AMD_MFMA_AVAILABLE+ }++ static __device__ __forceinline__ void mma(+ tile<32, 32, int> & D, const tile<32, 4, int> & A, const tile<32, 4, int> & B) {+#if defined(AMD_MFMA_AVAILABLE)+ using int32x16_t = __attribute__((__vector_size__(16 * sizeof(int)))) int;+ int32x16_t * acc = (int32x16_t *) D.x;+#if defined(CDNA4) || defined(CDNA3)+ acc[0] = __builtin_amdgcn_mfma_i32_32x32x16_i8(((int64_t *) A.x)[0], ((int64_t *) B.x)[0], acc[0], 0, 0, 0);+#elif defined(CDNA2) || defined(CDNA1)+ acc[0] = __builtin_amdgcn_mfma_i32_32x32x8i8(A.x[0], B.x[0], acc[0], 0, 0, 0);+ acc[0] = __builtin_amdgcn_mfma_i32_32x32x8i8(A.x[1], B.x[1], acc[0], 0, 0, 0);+#endif // defined(CDNA4) || defined(CDNA3)++#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // AMD_MFMA_AVAILABLE+ }++ template <typename T1, typename T2, int J, int K>+ static __device__ __forceinline__ void mma(+ tile<32, J, T1> & D, const tile<32, K, T2> & A, const tile<J, K, T2> & B) {+ tile <16, J, T1> * D16 = reinterpret_cast< tile<16, J, T1> *>(&D);+ const tile<16, K, T2> * A16 = reinterpret_cast<const tile<16, K, T2> *>(&A);+ mma(D16[0], A16[0], B);+ mma(D16[1], A16[1], B);+ }++ static __device__ __forceinline__ void mma(+ tile<32, 8, float> & D, const tile<32, 4, half2> & A, const tile<8, 4, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> & B) {+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+ }++ static __device__ __forceinline__ void mma(+ tile<32, 4, half2> & D, const tile<32, 4, half2> & A, const tile<8, 4, half2, DATA_LAYOUT_J_MAJOR_MIRRORED> & B) {+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+ }++ static __device__ __forceinline__ void mma(+ tile<16, 16, half2, DATA_LAYOUT_I_MAJOR> & D, const tile<32, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> & A,+ const tile<16, 8, half2, DATA_LAYOUT_I_MAJOR_MIRRORED> & B) {+#if defined(AMD_WMMA_AVAILABLE) && defined(RDNA3)+ using halfx16_t = __attribute__((ext_vector_type(16))) _Float16;+ halfx16_t * xD = (halfx16_t *) D.x;+ const halfx16_t * xA = (const halfx16_t *) A.x;+ const halfx16_t * xB = (const halfx16_t *) B.x;+ xD[0] = __builtin_amdgcn_wmma_f16_16x16x16_f16_w32(xA[0], xB[0], xD[0], /*opsel =*/ 0);+ xD[0] = __builtin_amdgcn_wmma_f16_16x16x16_f16_w32(xA[1], xB[0], xD[0], /*opsel =*/ 1);+#else+ GGML_UNUSED_VARS(D, A, B);+ NO_DEVICE_CODE;+#endif // TURING_MMA_AVAILABLE+ }++ template <data_layout dl_d, data_layout dl_ab>+ static __device__ __forceinline__ void mma(+ tile<16, 16, int, dl_d> & D, const tile<16, 4, int, dl_ab> & A, const tile<16, 4, int, dl_ab> & B) {+#if defined(AMD_MFMA_AVAILABLE)+ using int32x4_t = __attribute__((__vector_size__(4 * sizeof(int)))) int;+ int32x4_t * acc = (int32x4_t *) D.x;+#if defined(CDNA4) || defined(CDNA3)+ const int64_t xA = uint32_t(A.x[0]);+ const int64_t xB = uint32_t(B.x[0]);+ acc[0] = __builtin_amdgcn_mfma_i32_16x16x32_i8(xA, xB, acc[0], 0, 0, 0);+#elif defined(CDNA2) || defined(CDNA1)+ acc[0] = __builtin_amdgcn_mfma_i32_16x16x16i8(A.x[0], B.x[0], acc[0], 0, 0, 0);+#endif // defined(CDNA4) || defined(CDNA3)+#elif defined(AMD_WMMA_AVAILABLE)+ using int32x8_t = __attribute__((__vector_size__(8 * sizeof(int)))) int;+ int32x8_t * acc = (int32x8_t *) D.x;+#if defined(RDNA4)+ using int32x2_t = __attribute__((__vector_size__(2 * sizeof(int)))) int;+ int32x2_t * a_vec = (int32x2_t *) A.x;+ int32x2_t * b_vec = (int32x2_t *) B.x;+ acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(true, a_vec[0], true, b_vec[0], acc[0], false);+#elif defined(RDNA3)+ using int32x4_t = __attribute__((__vector_size__(4 * sizeof(int)))) int;+ int32x4_t * a_vec = (int32x4_t *) A.x;+ int32x4_t * b_vec = (int32x4_t *) B.x;+ acc[0] = __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a_vec[0], true, b_vec[0], acc[0], false);+#endif // RDNA4+#else+ GGML_UNUSED(D);+ GGML_UNUSED(A);+ GGML_UNUSED(B);+ NO_DEVICE_CODE;+#endif // AMD_WMMA_AVAILABLE+ }+}diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/mmid.cu b/exllamav3/exllamav3_ext/rocm/gmmq/mmid.cunew file mode 100644index 0000000..fe8772c--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/mmid.cu@@ -0,0 +1,167 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/mmid.hip.cpp (renamed .cu), itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: scan scratch supplied by the caller instead of a global buffer grown with gufo::hip::AllocateDevice;+// qfn_mmq_build_ids_maps C entry dropped.+#include "qfn_mmq_prelude.h"+namespace qfn_mmq {+#include "common.hpp"+#include "mmid.hpp"++// qwen38 local: parallel, deterministic id-map builder, replacing llama.cpp's+// mm_ids_helper. That helper ran one warp per expert over every assignment+// row (512 warps x 20,480 rows at a 2,048-token chunk), so its serial token+// loop cost 0.6-1.0 ms per launch. This one keeps its output contract (rows+// of one expert in ascending assignment order, so ids_src1/ids_dst/+// expert_bounds are bit-identical) with three parallel passes:+// 1. per block of kIdsRowsPerBlock assignment rows: each lane's rank among+// the same expert inside its wave (a 32-step shuffle loop), wave counts+// per expert in LDS, then per-expert prefix over the block's waves; the+// in-block rank goes to scratch and the block's per-expert counts to+// global memory.+// 2. one thread per expert: prefix over the blocks' counts (block bases)+// and the expert bounds.+// 3. scatter: position = bound[e] + block_base[b][e] + in-block rank.+// Like llama.cpp's helper, a negative id is counted below every expert (it+// shifts all bounds by one and leaves its slot unwritten) and an id at or+// past n_experts is dropped; block_counts carries the negatives in column+// n_experts.+constexpr int kIdsRowsPerBlock = 256;+constexpr int kIdsWaves = kIdsRowsPerBlock / 32;++static __global__ void mm_ids_rank_kernel(+ const int32_t * __restrict__ ids, int32_t * __restrict__ local_rank,+ int32_t * __restrict__ block_counts, const int n_rows,+ const int n_experts, const int n_expert_used, const int si1) {+ extern __shared__ int32_t wave_counts[]; // [n_experts][kIdsWaves]+ const int tid = threadIdx.x;+ const int lane = tid & 31;+ const int wave = tid >> 5;+ for (int i = tid; i < (n_experts + 1) * kIdsWaves; i += blockDim.x) {+ wave_counts[i] = 0;+ }+ __syncthreads();+ const int row = blockIdx.x * kIdsRowsPerBlock + tid;+ int expert = -2; // rows past the end are dropped+ if (row < n_rows) {+ const int it = row / n_expert_used;+ const int iex = row - it * n_expert_used;+ expert = ids[it * si1 + iex];+ if (expert >= n_experts) {+ expert = -2; // dropped+ } else if (expert < 0) {+ expert = -1; // counted below expert 0, never written+ }+ }+ // Rank among the same expert at lower lanes of this wave.+ int rank = 0;+#pragma unroll+ for (int l = 0; l < 32; ++l) {+ const int other = __shfl(expert, l, 32);+ rank += (l < lane && other == expert) ? 1 : 0;+ }+ if (expert >= 0) {+ atomicAdd(&wave_counts[expert * kIdsWaves + wave], 1);+ } else if (expert == -1) {+ atomicAdd(&wave_counts[n_experts * kIdsWaves + wave], 1);+ }+ __syncthreads();+ // Exclusive prefix over the waves of each expert; the total is the+ // block's count.+ for (int e = tid; e <= n_experts; e += blockDim.x) {+ int32_t * counts = wave_counts + e * kIdsWaves;+ int running = 0;+#pragma unroll+ for (int w = 0; w < kIdsWaves; ++w) {+ const int c = counts[w];+ counts[w] = running;+ running += c;+ }+ block_counts[blockIdx.x * (n_experts + 1) + e] = running;+ }+ __syncthreads();+ if (row < n_rows) {+ local_rank[row] = expert >= 0+ ? wave_counts[expert * kIdsWaves + wave] + rank : -1;+ }+}++static __global__ void mm_ids_bounds_kernel(+ int32_t * __restrict__ block_counts, int32_t * __restrict__ expert_bounds,+ const int n_blocks, const int n_experts) {+ // One block: per-expert totals, then a block-wide exclusive scan over+ // experts for the bounds. Block counts become block bases in place.+ extern __shared__ int32_t totals[]; // [n_experts + 1]+ for (int e = threadIdx.x; e <= n_experts; e += blockDim.x) {+ int running = 0;+ for (int b = 0; b < n_blocks; ++b) {+ const int c = block_counts[b * (n_experts + 1) + e];+ block_counts[b * (n_experts + 1) + e] = running;+ running += c;+ }+ totals[e] = running;+ }+ __syncthreads();+ if (threadIdx.x == 0) {+ int running = totals[n_experts]; // negative ids sit below expert 0+ for (int e = 0; e < n_experts; ++e) {+ expert_bounds[e] = running;+ running += totals[e];+ }+ expert_bounds[n_experts] = running;+ }+}++static __global__ void mm_ids_scatter_kernel(+ const int32_t * __restrict__ ids, const int32_t * __restrict__ local_rank,+ const int32_t * __restrict__ block_counts,+ const int32_t * __restrict__ expert_bounds,+ int32_t * __restrict__ ids_src1, int32_t * __restrict__ ids_dst,+ const int n_rows, const int n_experts, const int n_expert_used,+ const int nchannels_y, const int si1, const int sis1) {+ const int row = blockIdx.x * kIdsRowsPerBlock + threadIdx.x;+ if (row >= n_rows) {+ return;+ }+ const int rank = local_rank[row];+ if (rank < 0) {+ return;+ }+ const int it = row / n_expert_used;+ const int iex = row - it * n_expert_used;+ const int expert = ids[it * si1 + iex];+ const int pos = expert_bounds[expert] ++ block_counts[blockIdx.x * (n_experts + 1) + expert] + rank;+ ids_src1[pos] = it * sis1 + iex % nchannels_y;+ ids_dst[pos] = it * n_expert_used + iex;+}++// kyojin: the scan scratch (gmmq_ids_scratch_elems int32) is supplied by the caller (a torch allocation on the+// launch stream) instead of a process-global buffer grown with gufo::hip::AllocateDevice.+size_t gmmq_ids_scratch_elems(const int n_experts, const int n_tokens, const int n_expert_used) {+ const int n_rows = n_tokens * n_expert_used;+ const int n_blocks = (n_rows + kIdsRowsPerBlock - 1) / kIdsRowsPerBlock;+ return (size_t)n_rows + (size_t)n_blocks * (n_experts + 1);+}++void ggml_hip_launch_mm_ids_helper(+ const int32_t * __restrict__ ids, int32_t * __restrict__ ids_src1, int32_t * __restrict__ ids_dst, int32_t * __restrict__ expert_bounds,+ const int n_experts, const int n_tokens, const int n_expert_used, const int nchannels_y, const int si1, const int sis1,+ int32_t * __restrict__ scratch, hipStream_t stream) {+ const int n_rows = n_tokens * n_expert_used;+ const int n_blocks = (n_rows + kIdsRowsPerBlock - 1) / kIdsRowsPerBlock;+ const size_t lds = (size_t)(n_experts + 1) * kIdsWaves * sizeof(int32_t);+ GGML_ASSERT(n_rows > 0 && lds <= 48 * 1024 && "routed id maps: shape");+ int32_t * local_rank = scratch;+ int32_t * block_counts = scratch + n_rows;+ mm_ids_rank_kernel<<<n_blocks, kIdsRowsPerBlock, lds, stream>>>(+ ids, local_rank, block_counts, n_rows, n_experts, n_expert_used, si1);+ mm_ids_bounds_kernel<<<1, 1024, (size_t)(n_experts + 1) * sizeof(int32_t), stream>>>(+ block_counts, expert_bounds, n_blocks, n_experts);+ mm_ids_scatter_kernel<<<n_blocks, kIdsRowsPerBlock, 0, stream>>>(+ ids, local_rank, block_counts, expert_bounds, ids_src1, ids_dst, n_rows,+ n_experts, n_expert_used, nchannels_y, si1, sis1);+}++} // namespace qfn_mmqdiff --git a/exllamav3/exllamav3_ext/rocm/gmmq/mmid.hpp b/exllamav3/exllamav3_ext/rocm/gmmq/mmid.hppnew file mode 100644index 0000000..da48743--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/mmid.hpp@@ -0,0 +1,13 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/mmid.hpp, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: caller-supplied scan scratch (gmmq_ids_scratch_elems).+#pragma once++size_t gmmq_ids_scratch_elems(int n_experts, int n_tokens, int n_expert_used);++void ggml_hip_launch_mm_ids_helper(+ const int32_t * ids, int32_t * ids_src1, int32_t * ids_dst, int32_t * expert_bounds,+ int n_experts, int n_tokens, int n_expert_used, int nchannels_y, int si1, int sis1,+ int32_t * scratch, hipStream_t stream);diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/mmq.hpp b/exllamav3/exllamav3_ext/rocm/gmmq/mmq.hppnew file mode 100644index 0000000..f2165e8--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/mmq.hpp@@ -0,0 +1,4320 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/mmq.hpp, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: Q8_0 / Q5_1 tile loads zero the scales of blocks past the row end (rows not a multiple of 256, unpadded experts);+// optional per-expert weight pointer table (mmq_args::x_ptrs, last mul_mat_q argument): expert z reads x_ptrs[z]+// instead of x + z * stride_channel_x; null keeps the original path. extern DECL_MMQ_CASE declarations dropped (gmmq_case_*.cu+// instantiate the four routed formats).+#pragma once++#include "common.hpp"+#include "vecdotq.hpp"+#include "mma.hpp"++#include <climits>+#include <cstdint>+#include <type_traits>++using namespace ggml_hip_mma;++#define MMQ_DP4A_MAX_BATCH_SIZE 64 // Max. batch size to use for dp4a MMQ kernels when FP16 tensor cores are available.+#define MMQ_ITER_K 256+#define MMQ_ITER_K_FP4 512+#define MMQ_NWARPS 8++static constexpr int QK_FP4_MMQ = 8 * QK_MXFP4;++typedef void (*load_tiles_mmq_t)(const char * __restrict__ x, int * x_tile, const int kbx0, const int i_max, const int stride);+typedef void (*vec_dot_mmq_t)(const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00);+typedef void (*mmq_write_back_t)(const float * __restrict__ sum, const int32_t * __restrict__ get_rows_to_sorted,+ float * __restrict__ dst, const int stride, const int i_max, const int j_max,+ const float * __restrict__ epilogue_gate, const float * __restrict__ epilogue_weights,+ float * __restrict__ epilogue_mid, half * __restrict__ epilogue_mid_h,+ const float epilogue_clamp);++enum mmq_q8_1_ds_layout {+ MMQ_Q8_1_DS_LAYOUT_D4,+ MMQ_Q8_1_DS_LAYOUT_DS4,+ MMQ_Q8_1_DS_LAYOUT_D2S6,+};++struct block_q8_1_mmq {+ // The y float data is converted to a data layout that can simply be copied to shared memory as a contiguous block.+ // The y float data is first grouped as blocks of 128 values.+ // These blocks are then treated as individual data values and transposed.+ //+ // To avoid shared memory bank conflicts each block is padded with 16 bytes.+ // This padding is also used to store block scales/partial sums.+ // The scales multiplied with the quantized data are equal to the unquantized values.+ // The partial sums are obtained by summing up a subgroup of the contained values (prior to quantization)+ // and are only needed for performance reasons.+ //+ // The exact data stored depends on the x data type.+ union {+ float d4[4]; // 1 32 bit scale per 32 values, stored as d0,d1,d2,d3+ half2 ds4[4]; // 1 16 bit scale + 1 16 bit partial sum per 32 values, stored as d0,s0,d1,s1,d2,s2,d3,s3+ half d2s6[8]; // 1 16 bit scale per 64 values + 1 16 bit partial sum per 16 values for the first 96 values,+ // stored as d0,d1,s1,s2,s3,s4,s5+ };+ int8_t qs[4*QK8_1]; // 128 values quantized to 8 bit each+};++// this struct is used for fp4 data types (currently only used for Blackwell)+// mxfp4 has block size 32, each int32 of d4 contains 2 e8m0 scales in the lower 16 bits+// nvfp4 has block size 16, each int32 of d4 contains 4 ue4m3 scales+struct block_fp4_mmq {+ uint32_t d4[4];+ int8_t qs[4 * 32]; // 256 FP4 values packed as 4-bit pairs (2 per byte)+};++static_assert(sizeof(block_q8_1_mmq) == 4*QK8_1 + 4*sizeof(half2), "Unexpected block_q8_1_mmq size");+static_assert(sizeof(block_q8_1_mmq) == 4*sizeof(block_q8_1), "Unexpected block_q8_1_mmq size");+static_assert(sizeof(block_fp4_mmq) == sizeof(block_q8_1_mmq), "Unexpected block_fp4_mmq size");++static mmq_q8_1_ds_layout mmq_get_q8_1_ds_layout(const ggml_type type_x) {+ switch (type_x) {+ case GGML_TYPE_Q1_0:+ return MMQ_Q8_1_DS_LAYOUT_D4;+ case GGML_TYPE_Q4_0:+ case GGML_TYPE_Q4_1:+ return MMQ_Q8_1_DS_LAYOUT_DS4;+ case GGML_TYPE_Q5_0:+ return MMQ_Q8_1_DS_LAYOUT_D4;+ case GGML_TYPE_Q5_1:+ return MMQ_Q8_1_DS_LAYOUT_DS4;+ case GGML_TYPE_Q8_0:+ return MMQ_Q8_1_DS_LAYOUT_D4;+ case GGML_TYPE_MXFP4:+ return MMQ_Q8_1_DS_LAYOUT_D4;+ case GGML_TYPE_NVFP4:+ return MMQ_Q8_1_DS_LAYOUT_D4;+ case GGML_TYPE_Q2_K:+ return MMQ_Q8_1_DS_LAYOUT_D2S6;+ case GGML_TYPE_Q3_K:+ return MMQ_Q8_1_DS_LAYOUT_D4;+ case GGML_TYPE_Q4_K:+ case GGML_TYPE_Q5_K:+ return MMQ_Q8_1_DS_LAYOUT_DS4;+ case GGML_TYPE_Q6_K:+ case GGML_TYPE_IQ2_XXS:+ case GGML_TYPE_IQ2_XS:+ case GGML_TYPE_IQ2_S:+ case GGML_TYPE_IQ3_XXS:+ case GGML_TYPE_IQ3_S:+ return MMQ_Q8_1_DS_LAYOUT_D4;+ case GGML_TYPE_IQ1_S:+ return MMQ_Q8_1_DS_LAYOUT_DS4;+ case GGML_TYPE_IQ4_XS:+ case GGML_TYPE_IQ4_NL:+ return MMQ_Q8_1_DS_LAYOUT_D4;+ default:+ GGML_ABORT("fatal error");+ break;+ }+}++struct tile_x_sizes {+ int qs;+ int dm;+ int sc;+};++static int get_mmq_x_max_host(const int cc) {+ int base = amd_wmma_available(cc) ? 128 : 64;+ // RDNA 3.5 peaks at x=80 for the IQ2_XXS routed-MoE workload.+ if (base > 80) base = 80;+ // The width was swept against a real 4,096-token chunk: 24, 32 and 40 are+ // monotonically worse (397-412 tok/s), and 64, 96, 112 and 128 all tie 80+ // within noise, so the cap above is the whole story and needs no knob.+ return base;+}++static constexpr __device__ int get_mmq_x_max_device() {+#if defined(AMD_WMMA_AVAILABLE)+ return 128;+#else // defined(AMD_WMMA_AVAILABLE)++ return 64;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+}++inline constexpr int QFN_WMMA_MMQ_Y = 64;+static_assert(QFN_WMMA_MMQ_Y == 32 ||+ QFN_WMMA_MMQ_Y == 64 ||+ QFN_WMMA_MMQ_Y == 128,+ "RDNA WMMA MMQ Y must be 32, 64, or 128");++// Four-wave RDNA WMMA tiles cut the decoded-weight LDS footprint enough to+// admit more workgroups; MFMA devices retain their eight-wave tile. The+// compile-time override is only for controlled geometry sweeps.+static int get_mmq_y_host(const int cc) {+ return GGML_HIP_CC_IS_RDNA1(cc) ? 64 :+ (amd_wmma_available(cc) ? QFN_WMMA_MMQ_Y : 128);+}++static constexpr __device__ int get_iter_k([[maybe_unused]] const ggml_type type) {+ return MMQ_ITER_K;+}++static constexpr __device__ int get_mmq_y_device() {+#if defined(AMD_WMMA_AVAILABLE) || defined(RDNA1)+ return QFN_WMMA_MMQ_Y;+#else+ return 128;+#endif // defined(AMD_WMMA_AVAILABLE) || defined(RDNA1)+}++// Decouple shared memory tile sizes from WARP_SIZE to allow for different warp sizes.+// The K dimension of the tiles has either,+// 1*MMQ_TILE_NE_K==32 (always for TILE_Y_K) or 2*MMQ_TILE_NE_K==64 (typically for TILE_X_K),+// 32 bit elements for the quantized data (does not include scales).+// In other words, the size of the quantized data in the K dimension is a multiple of MMQ_TILE_NE_K.+// The final tile size in K direction is padded to avoid shared memory bank conflicts,+// in terms of 32 bit elements that means K % 2 == 1 for dp4a or K % 8 == 4 for mma.+#define MMQ_TILE_NE_K 32++#define MMQ_DP4A_TXS_Q4_0 tile_x_sizes{mmq_y*MMQ_TILE_NE_K + mmq_y, mmq_y*MMQ_TILE_NE_K/QI4_0 + mmq_y/QI4_0, 0}+#define MMQ_DP4A_TXS_Q4_1 tile_x_sizes{mmq_y*MMQ_TILE_NE_K + mmq_y, mmq_y*MMQ_TILE_NE_K/QI4_1 + mmq_y/QI4_1, 0}+#define MMQ_DP4A_TXS_Q8_0 tile_x_sizes{mmq_y*MMQ_TILE_NE_K*2 + mmq_y, mmq_y*MMQ_TILE_NE_K*2/QI8_0 + mmq_y/(QI8_0/2), 0}+#define MMQ_DP4A_TXS_Q8_0_16 tile_x_sizes{mmq_y*MMQ_TILE_NE_K*2 + mmq_y, mmq_y*MMQ_TILE_NE_K*4/QI8_0 + mmq_y/(QI8_0/4), 0}+#define MMQ_DP4A_TXS_Q8_1 tile_x_sizes{mmq_y*MMQ_TILE_NE_K*2 + mmq_y, mmq_y*MMQ_TILE_NE_K*2/QI8_1 + mmq_y/(QI8_1/2), 0}+#define MMQ_DP4A_TXS_Q2_K tile_x_sizes{mmq_y*MMQ_TILE_NE_K*2 + mmq_y, mmq_y*MMQ_TILE_NE_K + mmq_y, 0}+#define MMQ_DP4A_TXS_Q3_K tile_x_sizes{mmq_y*MMQ_TILE_NE_K*2 + mmq_y, mmq_y, mmq_y*MMQ_TILE_NE_K/8 + mmq_y/8}+#define MMQ_DP4A_TXS_Q4_K tile_x_sizes{mmq_y*MMQ_TILE_NE_K + mmq_y, mmq_y*MMQ_TILE_NE_K/QI4_K, mmq_y*MMQ_TILE_NE_K/8 + mmq_y/8}+#define MMQ_DP4A_TXS_Q5_K tile_x_sizes{mmq_y*MMQ_TILE_NE_K*2 + mmq_y, mmq_y*MMQ_TILE_NE_K/QI5_K + mmq_y/QI5_K, mmq_y*MMQ_TILE_NE_K/8 + mmq_y/8}+#define MMQ_DP4A_TXS_Q6_K tile_x_sizes{mmq_y*MMQ_TILE_NE_K*2 + mmq_y, mmq_y*MMQ_TILE_NE_K/QI6_K + mmq_y/QI6_K, mmq_y*MMQ_TILE_NE_K/8 + mmq_y/8}++static constexpr __host__ __device__ tile_x_sizes mmq_get_dp4a_tile_x_sizes(ggml_type type, int mmq_y) {+ switch (type) {+ case GGML_TYPE_Q1_0: return MMQ_DP4A_TXS_Q8_0;+ case GGML_TYPE_Q4_0: return MMQ_DP4A_TXS_Q4_0;+ case GGML_TYPE_Q4_1: return MMQ_DP4A_TXS_Q4_1;+ case GGML_TYPE_Q5_0: return MMQ_DP4A_TXS_Q8_0;+ case GGML_TYPE_Q5_1: return MMQ_DP4A_TXS_Q8_1;+ case GGML_TYPE_Q8_0: return MMQ_DP4A_TXS_Q8_0;+ case GGML_TYPE_MXFP4: return MMQ_DP4A_TXS_Q8_1;+ case GGML_TYPE_NVFP4: return MMQ_DP4A_TXS_Q8_0_16;+ case GGML_TYPE_Q2_K: return MMQ_DP4A_TXS_Q2_K;+ case GGML_TYPE_Q3_K: return MMQ_DP4A_TXS_Q3_K;+ case GGML_TYPE_Q4_K: return MMQ_DP4A_TXS_Q4_K;+ case GGML_TYPE_Q5_K: return MMQ_DP4A_TXS_Q5_K;+ case GGML_TYPE_Q6_K: return MMQ_DP4A_TXS_Q6_K;+ case GGML_TYPE_IQ2_XXS: return MMQ_DP4A_TXS_Q8_0;+ case GGML_TYPE_IQ2_XS: return MMQ_DP4A_TXS_Q8_0_16;+ case GGML_TYPE_IQ2_S: return MMQ_DP4A_TXS_Q8_0_16;+ case GGML_TYPE_IQ3_XXS: return MMQ_DP4A_TXS_Q8_0;+ case GGML_TYPE_IQ3_S: return MMQ_DP4A_TXS_Q8_0;+ case GGML_TYPE_IQ1_S: return MMQ_DP4A_TXS_Q8_0;+ case GGML_TYPE_IQ4_XS: return MMQ_DP4A_TXS_Q8_0;+ case GGML_TYPE_IQ4_NL: return MMQ_DP4A_TXS_Q8_0;+ default: return tile_x_sizes{0, 0, 0};+ }+}++#define MMQ_MMA_TILE_X_K_Q8_0 (2*MMQ_TILE_NE_K + 2*MMQ_TILE_NE_K/QI8_0 + 4)+#define MMQ_MMA_TILE_X_K_FP4 (2*MMQ_TILE_NE_K + 8 + 4) // MXFP4 and NVFP4 Blackwell+#define MMQ_MMA_TILE_X_K_NVFP4 (2*MMQ_TILE_NE_K + MMQ_TILE_NE_K/2 + 4) // NVFP4 Generic+#define MMQ_MMA_TILE_X_K_Q8_1 (2*MMQ_TILE_NE_K + 2*MMQ_TILE_NE_K/QI8_0 + 4)+#define MMQ_MMA_TILE_X_K_Q2_K (2*MMQ_TILE_NE_K + MMQ_TILE_NE_K + 4)+#define MMQ_MMA_TILE_X_K_Q3_K (2*MMQ_TILE_NE_K + MMQ_TILE_NE_K/2 + 4)+#define MMQ_MMA_TILE_X_K_Q6_K (2*MMQ_TILE_NE_K + MMQ_TILE_NE_K/QI6_K + MMQ_TILE_NE_K/8 + 7)++static_assert(MMQ_MMA_TILE_X_K_Q8_0 % 8 == 4, "Wrong padding.");+static_assert(MMQ_MMA_TILE_X_K_Q8_1 % 8 == 4, "Wrong padding.");+static_assert(MMQ_MMA_TILE_X_K_Q2_K % 8 == 4, "Wrong padding.");+static_assert(MMQ_MMA_TILE_X_K_Q3_K % 8 == 4, "Wrong padding.");+static_assert(MMQ_MMA_TILE_X_K_Q6_K % 8 == 4, "Wrong padding.");+static_assert(MMQ_MMA_TILE_X_K_FP4 % 8 == 4, "Wrong padding.");+static_assert(MMQ_MMA_TILE_X_K_FP4 == MMQ_MMA_TILE_X_K_Q8_1, "Wrong tile size for MXFP4");+static_assert(MMQ_MMA_TILE_X_K_NVFP4 % 8 == 4, "Wrong padding.");+++static constexpr __host__ __device__ int mmq_get_mma_tile_x_k(ggml_type type) {+ switch (type) {+ case GGML_TYPE_Q1_0: return MMQ_MMA_TILE_X_K_Q8_0;+ case GGML_TYPE_Q4_0: return MMQ_MMA_TILE_X_K_Q8_0;+ case GGML_TYPE_Q4_1: return MMQ_MMA_TILE_X_K_Q8_1;+ case GGML_TYPE_Q5_0: return MMQ_MMA_TILE_X_K_Q8_0;+ case GGML_TYPE_Q5_1: return MMQ_MMA_TILE_X_K_Q8_1;+ case GGML_TYPE_Q8_0: return MMQ_MMA_TILE_X_K_Q8_0;+ // tile sizes are the same for Q8_1 and FP4 for blackwell+ case GGML_TYPE_MXFP4: return MMQ_MMA_TILE_X_K_Q8_1;+ case GGML_TYPE_NVFP4: return MMQ_MMA_TILE_X_K_NVFP4;+ case GGML_TYPE_Q2_K: return MMQ_MMA_TILE_X_K_Q2_K;+ case GGML_TYPE_Q3_K: return MMQ_MMA_TILE_X_K_Q3_K;+ case GGML_TYPE_Q4_K: return MMQ_MMA_TILE_X_K_Q8_1;+ case GGML_TYPE_Q5_K: return MMQ_MMA_TILE_X_K_Q8_1;+ case GGML_TYPE_Q6_K: return MMQ_MMA_TILE_X_K_Q6_K;+ case GGML_TYPE_IQ2_XXS: return MMQ_MMA_TILE_X_K_Q8_0;+ case GGML_TYPE_IQ2_XS: return MMQ_MMA_TILE_X_K_Q3_K;+ case GGML_TYPE_IQ2_S: return MMQ_MMA_TILE_X_K_Q3_K;+ case GGML_TYPE_IQ3_XXS: return MMQ_MMA_TILE_X_K_Q8_0;+ case GGML_TYPE_IQ3_S: return MMQ_MMA_TILE_X_K_Q8_0;+ case GGML_TYPE_IQ1_S: return MMQ_MMA_TILE_X_K_Q8_0;+ case GGML_TYPE_IQ4_XS: return MMQ_MMA_TILE_X_K_Q8_0;+ case GGML_TYPE_IQ4_NL: return MMQ_MMA_TILE_X_K_Q8_0;+ default: return 0;+ }+}++// block_q8_1_mmq has (128 8-bit ints == 32 32-bit ints + 4 32-bit scales)+#define MMQ_TILE_Y_K (MMQ_TILE_NE_K + MMQ_TILE_NE_K / QI8_1)+#define MMQ_TILE_Y_FP4_K MMQ_TILE_Y_K++static int mmq_get_granularity_host(const int mmq_x, const int cc) {+ if (amd_mfma_available(cc) || amd_wmma_available(cc)) {+ return mmq_x >= 128 ? 32 : 16;+ } else if (false) {+ return 16;+ } else {+ return 8;+ }+}++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+static constexpr __device__ int mmq_get_granularity_device(const int mmq_x) {+ return mmq_x >= 128 ? 32 : 16;+}+#else+static constexpr __device__ int mmq_get_granularity_device(const int /*mmq_x*/) {+ return 8;+}+#endif // AMD_MFMA_AVAILABLE++/* ds4: warps per workgroup along the column axis.+ *+ * Upstream decomposes the workgroup purely by rows: a warp owns+ * `mmq_y / nwarps` rows and must own at least the 16-row accumulator tile, so+ * `nwarps == mmq_y / 16`. Shared-memory use also scales with `mmq_y`, which+ * makes resident waves per CU -- `(65536 / LDS) * nwarps` -- invariant at eight+ * of the 32 the hardware offers, at every `mmq_y`. Counters put the routed+ * gate/up at 11% VALU and 5% LDS issue with a 31x stall factor, so what it is+ * short of is exactly those waves.+ *+ * This adds a second axis: `NCW` warps share a row group and split the column+ * tiles between them, so `nwarps == (mmq_y / 16) * NCW` at unchanged shared+ * memory. Each warp then accumulates `mmq_x / NCW` columns, so the register+ * footprint drops with it. The column tiles a warp owns are strided by NCW,+ * which requires mmq_x to be a multiple of `NCW * ntx * tile_C::J`; the host+ * selector below enforces that.+ *+ * NCW == 1 reproduces upstream exactly, and that is the default: the split+ * works and is gate-clean, but it **loses**. Measured 2026-09-03 at a+ * 4,096-token chunk, three interleaved rounds each: NCW 1 gives 406.94 / 429.48+ * / 422.49 tok/s and NCW 2 gives 367.93 / 350.56 / 346.34, an 18% regression+ * that a forced 96-column width does not recover (348.26 / 354.55). Doubling+ * resident waves per CU from eight to sixteen therefore does not help, which+ * falsifies wave starvation as the explanation for the stall: halving the+ * columns per warp also halves the matrix ops each A-fragment load feeds, and+ * that costs more than the extra waves recover. Kept because it is the one+ * structural lever left and this records that it was tried. */+inline constexpr int QFN_WMMA_MMQ_NCW = 1;+static_assert(QFN_WMMA_MMQ_NCW == 1 || QFN_WMMA_MMQ_NCW == 2 ||+ QFN_WMMA_MMQ_NCW == 4,+ "RDNA WMMA MMQ column-warp factor must be 1, 2, or 4");++static int mmq_get_nwarps_host(const int cc, const int warp_size) {+ return amd_mfma_available(cc) ? 8 :+ (amd_wmma_available(cc) ? (QFN_WMMA_MMQ_Y/16)*QFN_WMMA_MMQ_NCW+ : 256/warp_size);+}++/* Non-1 only where mmq_get_nwarps_device() is boosted to match. */+static constexpr __device__ int mmq_get_ncw_device() {+#if defined(AMD_WMMA_AVAILABLE) && !defined(AMD_MFMA_AVAILABLE)+ return QFN_WMMA_MMQ_NCW;+#else+ return 1;+#endif+}++static constexpr __device__ int mmq_get_nwarps_device() {+#if defined(AMD_MFMA_AVAILABLE)+ return 8;+#elif defined(AMD_WMMA_AVAILABLE)+ return (QFN_WMMA_MMQ_Y/16)*QFN_WMMA_MMQ_NCW;+#else+ return 256/ggml_hip_get_physical_warp_size();+#endif // defined(AMD_MFMA_AVAILABLE)+}++// ------------------------------------------------------------++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q1_0(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + 2*MMQ_TILE_NE_K);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q8_0, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int blocks_per_iter = MMQ_ITER_K / QK1_0;+ constexpr int threads_per_row = blocks_per_iter * QI1_0;+ constexpr int nrows = warp_size / threads_per_row;+ constexpr int scale_entries_per_block = QK1_0 / QK8_1;+ constexpr int scale_entries_per_row = blocks_per_iter * scale_entries_per_block;++ const int txi = threadIdx.x % threads_per_row;+ const int kbx = txi / QI1_0;+ const int kqsx = txi % QI1_0;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q1_0 * bxi = (const block_q1_0 *) x + kbx0 + i*stride + kbx;+ const int qs_offset = 4*kqsx;+ const int qs0 = bxi->qs[qs_offset + 0] | (bxi->qs[qs_offset + 1] << 8) |+ (bxi->qs[qs_offset + 2] << 16) | (bxi->qs[qs_offset + 3] << 24);++ int unpacked_bytes[8];+#pragma unroll+ for (int j = 0; j < 8; ++j) {+ const int shift = j * 4;+ const int bits4 = (qs0 >> shift) & 0x0F;+ const int b0 = (bits4 & 0x01) ? 1 : -1;+ const int b1 = (bits4 & 0x02) ? 1 : -1;+ const int b2 = (bits4 & 0x04) ? 1 : -1;+ const int b3 = (bits4 & 0x08) ? 1 : -1;+ unpacked_bytes[j] = (b0 & 0xFF) | ((b1 & 0xFF) << 8) | ((b2 & 0xFF) << 16) | ((b3 & 0xFF) << 24);+ }++ const int dst_offset = kbx*(scale_entries_per_block*QI8_0) + kqsx*QI8_0;+#pragma unroll+ for (int j = 0; j < 8; ++j) {+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + dst_offset + j] = unpacked_bytes[j];+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + dst_offset + j] = unpacked_bytes[j];+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+ }++ const int ksx = threadIdx.x % scale_entries_per_row;+ const int scale_block = ksx / scale_entries_per_block;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {+ int i = i0 + threadIdx.y;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q1_0 * bxi = (const block_q1_0 *) x + kbx0 + i*stride + scale_block;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + ksx] = bxi->d;+#else+ x_df[i*(2*MMQ_TILE_NE_K/QI8_0) + i/(QI8_0/2) + ksx] = bxi->d;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q4_0(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + 2*MMQ_TILE_NE_K);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q4_0, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR4_0);+ constexpr int nrows = warp_size / threads_per_row;+ const int txi = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;+ const int kbx = txi / QI4_0;+ const int kqsx = txi % QI4_0;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q4_0 * bxi = (const block_q4_0 *) x + kbx0 + i*stride + kbx;+ const int qs0 = get_int_b2(bxi->qs, kqsx);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + kbx*(2*QI4_0) + kqsx + 0] = __vsubss4((qs0 >> 0) & 0x0F0F0F0F, 0x08080808);+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + kbx*(2*QI4_0) + kqsx + QI4_0] = __vsubss4((qs0 >> 4) & 0x0F0F0F0F, 0x08080808);+#else+ x_qs[i*(MMQ_TILE_NE_K + 1) + txi] = qs0;+#endif // defined(AMD_MFMA_AVAILABLE)+ }++ constexpr int blocks_per_tile_x_row = MMQ_TILE_NE_K / QI4_0;+ constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row;+ const int kbxd = threadIdx.x % blocks_per_tile_x_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * rows_per_warp) {+ int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q4_0 * bxi = (const block_q4_0 *) x + kbx0 + i*stride + kbxd;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + kbxd] = bxi->d;+#else+ x_df[i*(MMQ_TILE_NE_K/QI4_0) + i/QI4_0 + kbxd] = bxi->d;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q4_0_q8_1_dp4a(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q4_0, mmq_y);+ const int * x_qs = (const int *) x;+ const float * x_df = (const float *) x_qs + txs.qs;+ const int * y_qs = (const int *) y + 4;+ const half2 * y_ds = (const half2 *) y;++// #pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QR4_0*VDR_Q4_0_Q8_1_MMQ) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;+ const int kyqs = QI8_1 * ((k01/2) / (QI8_1/2)) + (k01/2) % (QI8_1/2);++ int u[2*VDR_Q4_0_Q8_1_MMQ];++ constexpr int max_cpy = ggml_hip_get_max_cpy_bytes();+ constexpr int mcpy_int = max_cpy / sizeof(int);+ static_assert(VDR_Q4_0_Q8_1_MMQ == 4, "bad VDR_Q4_0_Q8_1_MMQ");++ int tmp0[4], tmp1[4];++ #pragma unroll+ for (int l0 = 0; l0 < 4 / mcpy_int; ++l0) {+ ggml_hip_memcpy_1<max_cpy>(tmp0 + l0 * mcpy_int, &y_qs[j*MMQ_TILE_Y_K + kyqs + l0 * mcpy_int] );+ ggml_hip_memcpy_1<max_cpy>(tmp1 + l0 * mcpy_int, &y_qs[j*MMQ_TILE_Y_K + kyqs + QI4_0 + l0 * mcpy_int]);+ }++ u[0]=tmp0[0]; u[2]=tmp0[1]; u[4]=tmp0[2]; u[6]=tmp0[3];+ u[1]=tmp1[0]; u[3]=tmp1[1]; u[5]=tmp1[2]; u[7]=tmp1[3];++ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>+ (&x_qs[i*(MMQ_TILE_NE_K + 1) + k0/QR4_0], u,+ x_df[i*(MMQ_TILE_NE_K/QI4_0) + i/QI4_0 + k0/(QR4_0*QI4_0)], y_ds[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }+ }+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q4_1(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + 2*MMQ_TILE_NE_K);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q4_1, mmq_y);+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR4_1);+ constexpr int nrows = warp_size / threads_per_row;+ const int txi = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;+ const int kbx = txi / QI4_1;+ const int kqsx = txi % QI4_1;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q4_1 * bxi = (const block_q4_1 *) x + kbx0 + i*stride + kbx;+ const int qs0 = get_int_b4(bxi->qs, kqsx);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + kbx*(2*QI4_1) + kqsx + 0] = (qs0 >> 0) & 0x0F0F0F0F;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + kbx*(2*QI4_1) + kqsx + QI4_1] = (qs0 >> 4) & 0x0F0F0F0F;+#else+ x_qs[i*(MMQ_TILE_NE_K + 1) + txi] = qs0;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ constexpr int blocks_per_tile_x_row = MMQ_TILE_NE_K / QI4_1;+ constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row;+ const int kbxd = threadIdx.x % blocks_per_tile_x_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * rows_per_warp) {+ int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q4_1 * bxi = (const block_q4_1 *) x + kbx0 + i*stride + kbxd;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_dm[i*MMQ_MMA_TILE_X_K_Q8_1 + kbxd] = bxi->dm;+#else+ x_dm[i*(MMQ_TILE_NE_K/QI4_1) + i/QI4_1 + kbxd] = bxi->dm;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q4_1_q8_1_dp4a(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q4_1, mmq_y);+ const int * x_qs = (const int *) x;+ const half2 * x_dm = (const half2 *) x_qs + txs.qs;+ const int * y_qs = (const int *) y + 4;+ const half2 * y_ds = (const half2 *) y;++// #pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QR4_1*VDR_Q4_1_Q8_1_MMQ) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;+ const int kyqs = QI8_1 * ((k01/2) / (QI8_1/2)) + (k01/2) % (QI8_1/2);++ int u[2*VDR_Q4_1_Q8_1_MMQ];++ constexpr int max_cpy = ggml_hip_get_max_cpy_bytes();+ constexpr int mcpy_int = max_cpy / sizeof(int);+ static_assert(VDR_Q4_0_Q8_1_MMQ == 4, "bad VDR_Q4_0_Q8_1_MMQ");++ int tmp0[4], tmp1[4];++ #pragma unroll+ for (int l0 = 0; l0 < 4 / mcpy_int; ++l0) {+ ggml_hip_memcpy_1<max_cpy>(tmp0 + l0 * mcpy_int, &y_qs[j*MMQ_TILE_Y_K + kyqs + l0 * mcpy_int] );+ ggml_hip_memcpy_1<max_cpy>(tmp1 + l0 * mcpy_int, &y_qs[j*MMQ_TILE_Y_K + kyqs + QI4_1 + l0 * mcpy_int]);+ }++ u[0]=tmp0[0]; u[2]=tmp0[1]; u[4]=tmp0[2]; u[6]=tmp0[3];+ u[1]=tmp1[0]; u[3]=tmp1[1]; u[5]=tmp1[2]; u[7]=tmp1[3];++ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>+ (&x_qs[i*(MMQ_TILE_NE_K + 1) + k0/QR4_1], u,+ x_dm[i*(MMQ_TILE_NE_K/QI4_1) + i/QI4_1 + k0/(QR4_1*QI4_1)], y_ds[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }+ }+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q5_0(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q5_0, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR5_0);+ constexpr int nrows = warp_size / threads_per_row;+ const int txi = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;+ const int kbx = txi / QI5_0;+ const int kqsx = txi % QI5_0;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q5_0 * bxi = (const block_q5_0 *) x + kbx0 + i*stride + kbx;++ const int ql = get_int_b2(bxi->qs, kqsx);+ const int qh = get_int_b2(bxi->qh, 0) >> (4 * kqsx);++ int qs0 = (ql >> 0) & 0x0F0F0F0F;+ qs0 |= (qh << 4) & 0x00000010; // 0 -> 4+ qs0 |= (qh << 11) & 0x00001000; // 1 -> 12+ qs0 |= (qh << 18) & 0x00100000; // 2 -> 20+ qs0 |= (qh << 25) & 0x10000000; // 3 -> 28+ qs0 = __vsubss4(qs0, 0x10101010); // subtract 16++ int qs1 = (ql >> 4) & 0x0F0F0F0F;+ qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4+ qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12+ qs1 |= (qh << 2) & 0x00100000; // 18 -> 20+ qs1 |= (qh << 9) & 0x10000000; // 19 -> 28+ qs1 = __vsubss4(qs1, 0x10101010); // subtract 16++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + kbx*(2*QI5_0) + kqsx + 0] = qs0;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + kbx*(2*QI5_0) + kqsx + QI5_0] = qs1;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + kbx*(2*QI5_0) + kqsx + 0] = qs0;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + kbx*(2*QI5_0) + kqsx + QI5_0] = qs1;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ constexpr int blocks_per_tile_x_row = MMQ_TILE_NE_K / QI5_0;+ constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row;+ const int kbxd = threadIdx.x % blocks_per_tile_x_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * rows_per_warp) {+ int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q5_0 * bxi = (const block_q5_0 *) x + kbx0 + i*stride + kbxd;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + kbxd] = bxi->d;+#else+ x_df[i*(MMQ_TILE_NE_K/QI5_0) + i/QI5_0 + kbxd] = bxi->d;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q5_1(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + 2*MMQ_TILE_NE_K);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q5_1, mmq_y);+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR5_1);+ constexpr int nrows = warp_size / threads_per_row;+ const int txi = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;+ const int kbx = txi / QI5_1;+ const int kqsx = txi % QI5_1;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q5_1 * bxi = (const block_q5_1 *) x + kbx0 + i*stride + kbx;++ const int ql = get_int_b4(bxi->qs, kqsx);+ const int qh = get_int_b4(bxi->qh, 0) >> (4 * kqsx);++ int qs0 = (ql >> 0) & 0x0F0F0F0F;+ qs0 |= (qh << 4) & 0x00000010; // 0 -> 4+ qs0 |= (qh << 11) & 0x00001000; // 1 -> 12+ qs0 |= (qh << 18) & 0x00100000; // 2 -> 20+ qs0 |= (qh << 25) & 0x10000000; // 3 -> 28++ int qs1 = (ql >> 4) & 0x0F0F0F0F;+ qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4+ qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12+ qs1 |= (qh << 2) & 0x00100000; // 18 -> 20+ qs1 |= (qh << 9) & 0x10000000; // 19 -> 28++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + kbx*(2*QI5_1) + kqsx + 0] = qs0;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + kbx*(2*QI5_1) + kqsx + QI5_1] = qs1;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + kbx*(2*QI5_1) + kqsx + 0] = qs0;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + kbx*(2*QI5_1) + kqsx + QI5_1] = qs1;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ constexpr int blocks_per_tile_x_row = MMQ_TILE_NE_K / QI5_1;+ constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row;+ const int kbxd = threadIdx.x % blocks_per_tile_x_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * rows_per_warp) {+ int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q5_1 * bxi = (const block_q5_1 *) x + kbx0 + i*stride + kbxd;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_dm[i*MMQ_MMA_TILE_X_K_Q8_1 + kbxd] = kbx0 % stride + kbxd < stride ? bxi->dm : make_half2(0.0f, 0.0f); // kyojin: no scale past the row end+#else+ x_dm[i*(MMQ_TILE_NE_K/QI5_1) + i/QI5_1 + kbxd] = kbx0 % stride + kbxd < stride ? bxi->dm : make_half2(0.0f, 0.0f); // kyojin: no scale past the row end+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q8_0(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_tile + 2*MMQ_TILE_NE_K);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q8_0, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ // MMQ_ITER_K / (4 * QR8_0) == 64 required. but NV has only 32 threads per warp+ constexpr int threads_per_row = 32;+ constexpr int nrows = warp_size / threads_per_row;+ const int txi = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;+ const int kbx = txi / QI8_0;+ const int kqsx = txi % QI8_0;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q8_0 * bxi = (const block_q8_0 *) x + kbx0 + i*stride + kbx;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + 0 + txi] = get_int_b2(bxi[0].qs, kqsx);+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + MMQ_TILE_NE_K + txi] = get_int_b2(bxi[MMQ_TILE_NE_K/QI8_0].qs, kqsx);+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 0 + txi] = get_int_b2(bxi[0].qs, kqsx);+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + MMQ_TILE_NE_K + txi] = get_int_b2(bxi[MMQ_TILE_NE_K/QI8_0].qs, kqsx);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ constexpr int blocks_per_tile_x_row = 2*MMQ_TILE_NE_K / QI8_0;+ constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row;+ const int kbxd = threadIdx.x % blocks_per_tile_x_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * rows_per_warp) {+ int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q8_0 * bxi = (const block_q8_0 *) x + kbx0 + i*stride + kbxd;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + kbxd] = kbx0 % stride + kbxd < stride ? (float) bxi->d : 0.0f; // kyojin: no scale past the row end (unpadded experts)+#else+ x_df[i*(2*MMQ_TILE_NE_K/QI8_0) + i/(QI8_0/2) + kbxd] = kbx0 % stride + kbxd < stride ? (float) bxi->d : 0.0f; // kyojin: no scale past the row end (unpadded experts)+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_mxfp4(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_MXFP4, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR_MXFP4);+ constexpr int nrows = warp_size / threads_per_row;+ const int txi = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;+ const int kbx = txi / QI_MXFP4;+ const int kqsx = txi % QI_MXFP4;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_mxfp4 * bxi = (const block_mxfp4 *) x + kbx0 + i*stride + kbx;++ const int aux_q4 = get_int_b1(bxi->qs, kqsx);+ const int2 v = get_int_from_table_16(aux_q4, kvalues_mxfp4);+ const int k0 = kbx * (2 * QI_MXFP4) + kqsx;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + k0 + 0] = v.x;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + k0 + QI_MXFP4] = v.y;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0 + 0] = v.x;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0 + QI_MXFP4] = v.y;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ constexpr int blocks_per_tile_x_row = MMQ_TILE_NE_K / QI_MXFP4;+ constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row;+ const int kbxd = threadIdx.x % blocks_per_tile_x_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * rows_per_warp) {+ int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_mxfp4 * bxi = (const block_mxfp4 *) x + kbx0 + i*stride + kbxd;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_1 + kbxd] = ggml_hip_e8m0_to_fp32(bxi->e)*0.5f;+#else+ x_df[i*(MMQ_TILE_NE_K/QI_MXFP4) + i/QI_MXFP4 + kbxd] = ggml_hip_e8m0_to_fp32(bxi->e)*0.5f;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check>+static __device__ __forceinline__ void load_tiles_mxfp4_fp4(const char * __restrict__ x,+ int * __restrict__ x_tile,+ const int kbx0,+ const int i_max,+ const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ int * x_qs = (int *) x_tile;+ uint32_t * x_sc = (uint32_t *) (x_qs + 2 * MMQ_TILE_NE_K);++ const int txi = threadIdx.x;++ constexpr int iter_k = get_iter_k(GGML_TYPE_MXFP4);++ constexpr int threads_per_row = iter_k / QK_MXFP4; // each thread processes 1 block+ constexpr int rows_per_warp = warp_size / threads_per_row;+ const int kbx = txi % threads_per_row;+ const int row_in_warp = txi / threads_per_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += rows_per_warp * nwarps) {+ int i = i0 + threadIdx.y * rows_per_warp + row_in_warp;++ if constexpr (need_check) {+ i = min(i, i_max);+ }++ const block_mxfp4 * bxi = (const block_mxfp4 *) x + kbx0 + i * stride + kbx;++ // quantize_mxfp4_mmq permutes nibbles to match the quantized format+ const int k0 = kbx * 4;+ memcpy(x_qs + i * MMQ_MMA_TILE_X_K_FP4 + k0, bxi->qs, 16);++ // Load E8M0 scales: pack 2 consecutive scales into one uint32+ if (kbx % 2 == 0) {+ uint32_t e = bxi->e;+ e |= ((bxi + 1)->e << 8);+ x_sc[i * MMQ_MMA_TILE_X_K_FP4 + kbx / 2] = e;+ }+ }+}+++template <int mmq_y, bool need_check>+static __device__ __forceinline__ void load_tiles_nvfp4(const char * __restrict__ x,+ int * __restrict__ x_tile,+ const int kb0,+ const int i_max,+ const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_NVFP4, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / QK_NVFP4;+ constexpr int rows_per_warp = warp_size / threads_per_row;+ const int kbx = threadIdx.x % threads_per_row;+ const int row_in_warp = threadIdx.x / threads_per_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += rows_per_warp * nwarps) {+ int i = i0 + threadIdx.y * rows_per_warp + row_in_warp;++ if constexpr (need_check) {+ i = min(i, i_max);+ }++ const block_nvfp4 * bxi = (const block_nvfp4 *) x + kb0 + i * stride + kbx;+ const uint32_t * __restrict__ src_qs = reinterpret_cast<const uint32_t *>(bxi->qs);+ const int kqs = 16 * kbx;+ const int ksc = 4 * kbx;++#pragma unroll+ for (int sub = 0; sub < QK_NVFP4 / QK_NVFP4_SUB; ++sub) {+ const int2 q0 = get_int_from_table_16(src_qs[2 * sub + 0], kvalues_mxfp4);+ const int2 q1 = get_int_from_table_16(src_qs[2 * sub + 1], kvalues_mxfp4);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i * MMQ_MMA_TILE_X_K_NVFP4 + kqs + 4 * sub + 0] = q0.x;+ x_qs[i * MMQ_MMA_TILE_X_K_NVFP4 + kqs + 4 * sub + 1] = q1.x;+ x_qs[i * MMQ_MMA_TILE_X_K_NVFP4 + kqs + 4 * sub + 2] = q0.y;+ x_qs[i * MMQ_MMA_TILE_X_K_NVFP4 + kqs + 4 * sub + 3] = q1.y;+ x_df[i * MMQ_MMA_TILE_X_K_NVFP4 + ksc + sub] = ggml_hip_ue4m3_to_fp32(bxi->d[sub]);+#else+ x_qs[i * (2 * MMQ_TILE_NE_K + 1) + kqs + 4 * sub + 0] = q0.x;+ x_qs[i * (2 * MMQ_TILE_NE_K + 1) + kqs + 4 * sub + 1] = q1.x;+ x_qs[i * (2 * MMQ_TILE_NE_K + 1) + kqs + 4 * sub + 2] = q0.y;+ x_qs[i * (2 * MMQ_TILE_NE_K + 1) + kqs + 4 * sub + 3] = q1.y;+ x_df[i * (2 * MMQ_TILE_NE_K * 2 / QI_NVFP4) + i / (QK_NVFP4_SUB / QI_NVFP4) + ksc + sub] = ggml_hip_ue4m3_to_fp32(bxi->d[sub]);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+ }+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q8_0_q8_1_dp4a(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q8_0, mmq_y);+ const int * x_qs = (const int *) x;+ const float * x_df = (const float *) x_qs + txs.qs;+ const int * y_qs = (const int *) y + 4;+ const float * y_df = (const float *) y;++// #pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += VDR_Q8_0_Q8_1_MMQ) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;++ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q8_0_q8_1_impl<float, VDR_Q8_0_Q8_1_MMQ>+ (&x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0], &y_qs[j*MMQ_TILE_Y_K + k0 % MMQ_TILE_NE_K],+ x_df[i*(2*MMQ_TILE_NE_K/QI8_0) + i/(QI8_0/2) + k0/QI8_0], y_df[j*MMQ_TILE_Y_K + (k0/QI8_1) % (MMQ_TILE_NE_K/QI8_1)]);+ }+ }+ }+}++template <int mmq_x, int mmq_y, mmq_q8_1_ds_layout ds_layout>+static __device__ __forceinline__ void vec_dot_q8_0_q8_1_mma(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ constexpr data_layout input_layout = get_input_data_layout();+ typedef tile<16, 8, int, input_layout> tile_A;+ typedef tile<16, 8, int, input_layout> tile_B;+ typedef tile<16, 16, int, DATA_LAYOUT_J_MAJOR> tile_C;++ constexpr int granularity = mmq_get_granularity_device(mmq_x);+ constexpr int rows_per_warp = granularity;+ constexpr int ntx = rows_per_warp/tile_C::I; // Number of x minitiles per warp.+ /* ds4: see QFN_WMMA_MMQ_NCW. `ncw` warps share this row group and+ * take every ncw-th column tile; `wrow` is the warp's index within the+ * row decomposition upstream assumes. */+ constexpr int ncw = mmq_get_ncw_device();+ /* mmq_x is a multiple of j_stride for every width the host selector offers;+ * unreachable specializations are still instantiated, so this cannot assert. */+ constexpr int j_stride = ncw*ntx*tile_C::J;+ const int cgroup = threadIdx.y % ncw;+ const int wrow = threadIdx.y / ncw;++ y += (wrow % ntx) * (tile_C::J*MMQ_TILE_Y_K);++ const int * x_qs = (const int *) x;+ const float * x_df = (const float *) x_qs + 2*MMQ_TILE_NE_K;+ const int * y_qs = (const int *) y + 4;+ const float * y_df = (const float *) y;+ const half2 * y_ds = (const half2 *) y;++ const int i0 = (wrow / ntx) * rows_per_warp;++ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QI8_0) {+ const int k0 = k00 + k01;++ tile_A A[ntx];+#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ load_ldmatrix(A[n], x_qs + (i0 + n*tile_A::I)*MMQ_MMA_TILE_X_K_Q8_0 + k0, MMQ_MMA_TILE_X_K_Q8_0);+ }++#pragma unroll+ for (int j0 = cgroup*ntx*tile_C::J; j0 < mmq_x; j0 += j_stride) {+ tile_B B;+ load_ldmatrix(B, y_qs + j0*MMQ_TILE_Y_K + k01, MMQ_TILE_Y_K);++ float dB;+ const int j = j0 + tile_C::get_j(0);+ if (ds_layout == MMQ_Q8_1_DS_LAYOUT_D4) {+ dB = y_df[j*MMQ_TILE_Y_K + k01/QI8_1];+ } else {+ dB = __low2float(y_ds[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }++#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ tile_C C;+ mma(C, A[n], B);++#pragma unroll+ for (int l = 0; l < tile_C::ne; ++l) {+ const int i = i0 + n*tile_A::I + tile_C::get_i(l);+ const float dA = x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + k0/QI8_0];+ // Keep row placement from changing contraction. The two+ // column minitiles otherwise use different FP32 products+ // under fast-math, changing identical routed activations.+ float& acc = sum[((j0 - cgroup*ntx*tile_C::J)/j_stride*ntx + n)*tile_C::ne + l];+ acc = __fmaf_rn(__fmul_rn(float(C.x[l]), dA), dB, acc);+ }+ }+ }+ }+#else+ typedef tile<16, 8, int> tile_A;+ typedef tile< 8, 8, int> tile_B;+ typedef tile<16, 8, int> tile_C;++ constexpr int granularity = mmq_get_granularity_device(mmq_x);+ constexpr int rows_per_warp = 2 * granularity;+ constexpr int ntx = rows_per_warp/tile_C::I; // Number of x minitiles per warp.++ y += (threadIdx.y % ntx) * (tile_C::J*MMQ_TILE_Y_K);++ const int * x_qs = (const int *) x;+ const float * x_df = (const float *) x_qs + 2*MMQ_TILE_NE_K;+ const int * y_qs = (const int *) y + 4;+ const float * y_df = (const float *) y;+ const half2 * y_ds = (const half2 *) y;++ tile_A A[ntx][MMQ_TILE_NE_K/QI8_0];+ float dA[ntx][tile_C::ne/2][MMQ_TILE_NE_K/QI8_0];++ const int i0 = (threadIdx.y/ntx)*rows_per_warp;++#pragma unroll+ for (int n = 0; n < ntx; ++n) {+#pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QI8_0) {+ const int k0 = k00 + k01;++ load_ldmatrix(A[n][k01/QI8_0], x_qs + (i0 + n*tile_A::I)*MMQ_MMA_TILE_X_K_Q8_0 + k0, MMQ_MMA_TILE_X_K_Q8_0);+ }++#pragma unroll+ for (int l = 0; l < tile_C::ne/2; ++l) {+ const int i = i0 + n*tile_A::I + tile_C::get_i(2*l);++#pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QI8_0) {+ const int k0 = k00 + k01;++ dA[n][l][k01/QI8_0] = x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + k0/QI8_0];+ }+ }+ }++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += ntx*tile_C::J) {+#pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QI8_0) {+ tile_B B;+ float dB[tile_C::ne/2];++ load_generic(B, y_qs + j0*MMQ_TILE_Y_K + k01, MMQ_TILE_Y_K); // faster than load_ldmatrix++#pragma unroll+ for (int l = 0; l < tile_C::ne/2; ++l) {+ const int j = j0 + tile_C::get_j(l);++ if (ds_layout == MMQ_Q8_1_DS_LAYOUT_D4) {+ dB[l] = y_df[j*MMQ_TILE_Y_K + k01/QI8_1];+ } else {+ dB[l] = __low2float(y_ds[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }+ }++#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ tile_C C;+ mma(C, A[n][k01/QI8_0], B);++#pragma unroll+ for (int l = 0; l < tile_C::ne; ++l) {+ sum[(j0/tile_C::J + n)*tile_C::ne + l] += C.x[l]*dA[n][l/2][k01/QI8_0]*dB[l%2];+ }+ }+ }+ }+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+}+++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q8_1_q8_1_dp4a(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q5_1, mmq_y);+ const int * x_qs = (const int *) x;+ const half2 * x_dm = (const half2 *) x_qs + txs.qs;+ const int * y_qs = (const int *) y + 4;+ const half2 * y_ds = (const half2 *) y;++// #pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += VDR_Q8_0_Q8_1_MMQ) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;++ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>+ (&x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0], &y_qs[j*MMQ_TILE_Y_K + k01],+ x_dm[i*(MMQ_TILE_NE_K/QI5_1) + i/QI5_1 + k0/QI8_1], y_ds[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }+ }+ }+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q8_1_q8_1_mma(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ constexpr data_layout input_layout = get_input_data_layout();+ typedef tile<16, 8, int, input_layout> tile_A;+ typedef tile<16, 8, int, input_layout> tile_B;+ typedef tile<16, 16, int, DATA_LAYOUT_J_MAJOR> tile_C;++ constexpr int granularity = mmq_get_granularity_device(mmq_x);+ constexpr int rows_per_warp = granularity;+ constexpr int ntx = rows_per_warp/tile_C::I; // Number of x minitiles per warp.+ /* ds4: see QFN_WMMA_MMQ_NCW. `ncw` warps share this row group and+ * take every ncw-th column tile; `wrow` is the warp's index within the+ * row decomposition upstream assumes. */+ constexpr int ncw = mmq_get_ncw_device();+ /* mmq_x is a multiple of j_stride for every width the host selector offers;+ * unreachable specializations are still instantiated, so this cannot assert. */+ constexpr int j_stride = ncw*ntx*tile_C::J;+ const int cgroup = threadIdx.y % ncw;+ const int wrow = threadIdx.y / ncw;++ y += (wrow % ntx) * (tile_C::J*MMQ_TILE_Y_K);++ const int * x_qs = (const int *) x;+ const half2 * x_dm = (const half2 *) x_qs + 2*MMQ_TILE_NE_K;+ const int * y_qs = (const int *) y + 4;+ const half2 * y_dm = (const half2 *) y;++ const int i0 = (wrow / ntx) * rows_per_warp;++ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QI8_1) {+ const int k0 = k00 + k01;++ tile_A A[ntx];+#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ load_ldmatrix(A[n], x_qs + (i0 + n*tile_A::I)*MMQ_MMA_TILE_X_K_Q8_1 + k0, MMQ_MMA_TILE_X_K_Q8_1);+ }++#pragma unroll+ for (int j0 = cgroup*ntx*tile_C::J; j0 < mmq_x; j0 += j_stride) {+ tile_B B;+ load_ldmatrix(B, y_qs + j0*MMQ_TILE_Y_K + k01, MMQ_TILE_Y_K);++ const int j = j0 + tile_C::get_j(0);+ const float2 dsB = __half22float2(y_dm[j*MMQ_TILE_Y_K + k01/QI8_1]);++#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ tile_C C;+ mma(C, A[n], B);++#pragma unroll+ for (int l = 0; l < tile_C::ne; ++l) {+ const int i = i0 + n*tile_A::I + tile_C::get_i(l);+ float2 dmA = __half22float2(x_dm[i*MMQ_MMA_TILE_X_K_Q8_1 + k0/QI8_1]);+ sum[((j0 - cgroup*ntx*tile_C::J)/j_stride*ntx + n)*tile_C::ne + l] += dmA.x*dsB.x*C.x[l];+ sum[((j0 - cgroup*ntx*tile_C::J)/j_stride*ntx + n)*tile_C::ne + l] += dmA.y*dsB.y;+ }+ }+ }+ }+#else+ typedef tile<16, 8, int> tile_A;+ typedef tile< 8, 8, int> tile_B;+ typedef tile<16, 8, int> tile_C;++ constexpr int granularity = mmq_get_granularity_device(mmq_x);+ constexpr int rows_per_warp = 2 * granularity;+ constexpr int ntx = rows_per_warp/tile_C::I; // Number of x minitiles per warp.++ y += (threadIdx.y % ntx) * (tile_C::J*MMQ_TILE_Y_K);++ const int * x_qs = (const int *) x;+ const half2 * x_dm = (const half2 *) x_qs + 2*MMQ_TILE_NE_K;+ const int * y_qs = (const int *) y + 4;+ const half2 * y_dm = (const half2 *) y;++ tile_A A[ntx][MMQ_TILE_NE_K/QI8_1];+ float2 dmA[ntx][tile_C::ne/2][MMQ_TILE_NE_K/QI8_1];++ const int i0 = (threadIdx.y/ntx)*rows_per_warp;++#pragma unroll+ for (int n = 0; n < ntx; ++n) {+#pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QI8_1) {+ const int k0 = k00 + k01;++ load_ldmatrix(A[n][k01/QI8_1], x_qs + (i0 + n*tile_A::I)*MMQ_MMA_TILE_X_K_Q8_1 + k0, MMQ_MMA_TILE_X_K_Q8_1);+ }++#pragma unroll+ for (int l = 0; l < tile_C::ne/2; ++l) {+ const int i = i0 + n*tile_A::I + tile_C::get_i(2*l);++#pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QI8_1) {+ const int k0 = k00 + k01;++ dmA[n][l][k01/QI8_1] = __half22float2(x_dm[i*MMQ_MMA_TILE_X_K_Q8_1 + k0/QI8_1]);+ }+ }+ }++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += ntx*tile_C::J) {+#pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QI8_1) {+ tile_B B;+ float2 dsB[tile_C::ne/2];++ load_generic(B, y_qs + j0*MMQ_TILE_Y_K + k01, MMQ_TILE_Y_K); // faster than load_ldmatrix++#pragma unroll+ for (int l = 0; l < tile_C::ne/2; ++l) {+ const int j = j0 + tile_C::get_j(l);++ dsB[l] = __half22float2(y_dm[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }++#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ tile_C C;+ mma(C, A[n][k01/QI8_1], B);++#pragma unroll+ for (int l = 0; l < tile_C::ne; ++l) {+ sum[(j0/tile_C::J + n)*tile_C::ne + l] += dmA[n][l/2][k01/QI8_1].x*dsB[l%2].x*C.x[l];+ sum[(j0/tile_C::J + n)*tile_C::ne + l] += dmA[n][l/2][k01/QI8_1].y*dsB[l%2].y;+ }+ }+ }+ }+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+}++// Used for NVFP4, Q3_K, IQ2_S, and IQ2_XS+template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q8_0_16_q8_1_dp4a(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr tile_x_sizes txs = MMQ_DP4A_TXS_Q8_0_16;+ const int * x_qs = (const int *) x;+ const float * x_df = (const float *) x_qs + txs.qs;+ const int * y_qs = (const int *) y + 4;+ const float * y_df = (const float *) y;++// #pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QI8_0) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;++ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q8_0_16_q8_1_impl<QI8_0>(+ &x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0],+ &y_qs[j*MMQ_TILE_Y_K + k01],+ &x_df[i*(2*MMQ_TILE_NE_K*2/QI8_0) + i/(QI8_0/4) + k0/(QI8_0/2)],+ y_df[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }+ }+ }+}++// Used for Q3_K, IQ2_S, and IQ2_XS:+template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q8_0_16_q8_1_mma(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ constexpr data_layout input_layout = get_input_data_layout();+ typedef tile<16, 4, int, input_layout> tile_A;+ typedef tile<16, 4, int, input_layout> tile_B;+ typedef tile<16, 16, int, DATA_LAYOUT_J_MAJOR> tile_C;++ constexpr int granularity = mmq_get_granularity_device(mmq_x);+ constexpr int rows_per_warp = granularity;+ constexpr int ntx = rows_per_warp/tile_C::I; // Number of x minitiles per warp.+ /* ds4: see QFN_WMMA_MMQ_NCW. `ncw` warps share this row group and+ * take every ncw-th column tile; `wrow` is the warp's index within the+ * row decomposition upstream assumes. */+ constexpr int ncw = mmq_get_ncw_device();+ /* mmq_x is a multiple of j_stride for every width the host selector offers;+ * unreachable specializations are still instantiated, so this cannot assert. */+ constexpr int j_stride = ncw*ntx*tile_C::J;+ const int cgroup = threadIdx.y % ncw;+ const int wrow = threadIdx.y / ncw;++ y += (wrow % ntx) * (tile_C::J*MMQ_TILE_Y_K);++ const int * x_qs = (const int *) x;+ const float * x_df = (const float *) x_qs + MMQ_TILE_NE_K*2;+ const int * y_qs = (const int *) y + 4;+ const float * y_df = (const float *) y;++ const int i0 = (wrow / ntx) * rows_per_warp;++ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += 4) {+ const int k0 = k00 + k01;++ tile_A A[ntx];+#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ load_ldmatrix(A[n], x_qs + (i0 + n*tile_A::I)*MMQ_MMA_TILE_X_K_Q3_K + k0, MMQ_MMA_TILE_X_K_Q3_K);+ }++#pragma unroll+ for (int j0 = cgroup*ntx*tile_C::J; j0 < mmq_x; j0 += j_stride) {+ tile_B B;+ load_ldmatrix(B, y_qs + j0*MMQ_TILE_Y_K + k01, MMQ_TILE_Y_K);++ const int j = j0 + tile_C::get_j(0);+ const float dB = y_df[j*MMQ_TILE_Y_K + k01/QI8_1];++#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ tile_C C;+ mma(C, A[n], B);++#pragma unroll+ for (int l = 0; l < tile_C::ne; ++l) {+ const int i = i0 + n*tile_C::I + tile_C::get_i(l);+ sum[((j0 - cgroup*ntx*tile_C::J)/j_stride*ntx + n)*tile_C::ne + l] += C.x[l] * x_df[i*MMQ_MMA_TILE_X_K_Q3_K + k0/4] * dB;+ }+ }+ }+ }+#else+ GGML_UNUSED_VARS(x, y, sum, k00);+ NO_DEVICE_CODE;+#endif // AMD_MFMA_AVAILABLE || AMD_WMMA_AVAILABLE+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + 2*MMQ_TILE_NE_K);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q2_K, mmq_y);+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR2_K);+ constexpr int nrows = ggml_hip_get_physical_warp_size() / threads_per_row;+ const int kqsx = threadIdx.x % threads_per_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q2_K * bxi = (const block_q2_K *) x + kbx0 + i*stride;++ const int x_ql_0 = get_int_b2(bxi->qs, kqsx);++#pragma unroll+ for (int l = 0; l < QR2_K; ++l) {+ const int k = (kqsx/8)*32 + l*8 + kqsx % 8;++ const int x_qs_k = (x_ql_0 >> (2*l)) & 0x03030303;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q2_K + k] = x_qs_k;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + k] = x_qs_k;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ const int sc_m = bxi->scales[kqsx];+#ifdef FAST_FP16_AVAILABLE+ const half2 x_dm_ik = __hmul2(bxi->dm, make_half2(sc_m & 0x0F, sc_m >> 4));+#else+ const float2 bxi_dmf = __half22float2(bxi->dm);+ const half2 x_dm_ik = make_half2(bxi_dmf.x*(sc_m & 0x0F), bxi_dmf.y*(sc_m >> 4));+#endif // FAST_FP16_AVAILABLE++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_dm[i*MMQ_MMA_TILE_X_K_Q2_K + kqsx] = x_dm_ik;+#else+ x_dm[i*(MMQ_TILE_NE_K + 1) + kqsx] = x_dm_ik;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++// ds4 (P4 Inc3): aligned row-pair-SoA twin of load_tiles_q2_K. x points at+// the weight-server artifact [uint2 dm2 | int4 sc4 | uint2 qs2] (row pairs+// interleaved; q2_k_aligned_derepack_kernel in qfn_mmq.cu is the byte-level+// spec this inverts) and soa_npair is the total pair count = nblk/2, from+// which the section offsets derive. Every tile_x element is the same value+// the standard loader reads from the raw 84-byte block, moved through+// integer selects only, so the output is bit-identical and vec_dot is+// untouched. No expert geometry is needed: rows per expert is even, so the+// artifact's pair index ((e*(M/2))+r/2)*nb_row+b equals+// (r_global/2)*nb_row+b for the flat row r_global = e*M+r.+template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q2_K_soa(+ const char * __restrict__ x, const int64_t soa_npair, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + 2*MMQ_TILE_NE_K);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q2_K, mmq_y);+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR2_K);+ constexpr int nrows = ggml_hip_get_physical_warp_size() / threads_per_row;+ const int kqsx = threadIdx.x % threads_per_row;++ const uint64_t dm_bytes = ((uint64_t)soa_npair * 8u + 63u) & ~63ull;+ const uint64_t sc_bytes = ((uint64_t)soa_npair * 32u + 63u) & ~63ull;+ const uint2 * dm2 = (const uint2 *) x;+ const int4 * sc4 = (const int4 *) (x + dm_bytes);+ const uint2 * qs2 = (const uint2 *) (x + dm_bytes + sc_bytes);++ // kbx0 = offset_x + kb0 is a multiple of stride (the row part; experts+ // and row tiles are whole-row offsets) plus the block column kb0 <+ // stride, so the (row, block-column) split hoists out of the row loop.+ const int r0 = kbx0 / stride;+ const int b = kbx0 % stride;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const int rg = r0 + i;+ // Element indices stay 32-bit: the largest artifact (V4-Pro down)+ // has ~16.5M pairs -> qs2 index < 2^28.+ const uint32_t pblk = (uint32_t)(rg >> 1)*(uint32_t)stride + (uint32_t)b;+ const uint32_t par = (uint32_t)rg & 1u;++ const uint2 q = qs2[pblk*16u + kqsx];+ const int x_ql_0 = (int)(par ? q.y : q.x);++#pragma unroll+ for (int l = 0; l < QR2_K; ++l) {+ const int k = (kqsx/8)*32 + l*8 + kqsx % 8;++ const int x_qs_k = (x_ql_0 >> (2*l)) & 0x03030303;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q2_K + k] = x_qs_k;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + k] = x_qs_k;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ // scales byte kqsx of the raw block = word w = kqsx/4; the artifact+ // packs scale words {row0_w0, row0_w1, row1_w0, row1_w1} as two int4+ // per pair, so (w>>1) picks the int4 and (par, w&1) the component.+ const int w = kqsx >> 2;+ const int4 s = sc4[pblk*2u + (uint32_t)(w >> 1)];+ const uint32_t sw = par ? ((w & 1) ? (uint32_t)s.w : (uint32_t)s.z)+ : ((w & 1) ? (uint32_t)s.y : (uint32_t)s.x);+ const int sc_m = (int)((sw >> (8*(kqsx & 3))) & 0xFFu);++ const uint2 dmp = dm2[pblk];+ const uint32_t dm_bits = par ? dmp.y : dmp.x;+ half2 dm;+ *reinterpret_cast<uint32_t *>(&dm) = dm_bits;+#ifdef FAST_FP16_AVAILABLE+ const half2 x_dm_ik = __hmul2(dm, make_half2(sc_m & 0x0F, sc_m >> 4));+#else+ const float2 bxi_dmf = __half22float2(dm);+ const half2 x_dm_ik = make_half2(bxi_dmf.x*(sc_m & 0x0F), bxi_dmf.y*(sc_m >> 4));+#endif // FAST_FP16_AVAILABLE++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_dm[i*MMQ_MMA_TILE_X_K_Q2_K + kqsx] = x_dm_ik;+#else+ x_dm[i*(MMQ_TILE_NE_K + 1) + kqsx] = x_dm_ik;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q2_K_q8_1_dp4a(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q2_K, mmq_y);+ const int * x_qs = (const int *) x;+ const half2 * x_dm = (const half2 *) x_qs + txs.qs;+ const int * y_qs = (const int *) y + 4;+ const half2 * y_ds = (const half2 *) y;++ float2 y_df[mmq_x/nwarps];+#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++ y_df[j0/nwarps] = __half22float2(y_ds[j*MMQ_TILE_Y_K]);+ }++#pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K/2; k01 += QR2_K*VDR_Q2_K_Q8_1_MMQ) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;++ constexpr int ns = 2;+ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q2_K_q8_1_impl_mmq<ns>(+ &x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0], &y_qs[j*MMQ_TILE_Y_K + k01],+ &x_dm[i*(MMQ_TILE_NE_K + 1) + k0/4], k01 < MMQ_TILE_NE_K/2 ? y_df[j0/nwarps].x : y_df[j0/nwarps].y,+ &y_ds[j*MMQ_TILE_Y_K + (1 + k01/QI8_1)]);+ }+ }+ }++ // Some compilers fail to unroll the loop over k01 if there is a conditional statement for ns in the inner loop.+ // As a workaround 2 separate loops are used instead.+#pragma unroll+ for (int k01 = MMQ_TILE_NE_K/2; k01 < MMQ_TILE_NE_K; k01 += QR2_K*VDR_Q2_K_Q8_1_MMQ) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;++ constexpr int ns = 1;+ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q2_K_q8_1_impl_mmq<ns>(+ &x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0], &y_qs[j*MMQ_TILE_Y_K + k01],+ &x_dm[i*(MMQ_TILE_NE_K + 1) + k0/4], k01 < MMQ_TILE_NE_K/2 ? y_df[j0/nwarps].x : y_df[j0/nwarps].y,+ &y_ds[j*MMQ_TILE_Y_K + (1 + k01/QI8_1)]);+ }+ }+ }+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q2_K_q8_1_mma(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ constexpr data_layout input_layout = get_input_data_layout();+ typedef tile<16, 4, int, input_layout> tile_A;+ typedef tile<16, 4, int, input_layout> tile_B;+ typedef tile<16, 16, int, DATA_LAYOUT_J_MAJOR> tile_C;++ constexpr int granularity = mmq_get_granularity_device(mmq_x);+ constexpr int rows_per_warp = granularity;+ constexpr int ntx = rows_per_warp/tile_C::I; // Number of x minitiles per warp.+ /* ds4: see QFN_WMMA_MMQ_NCW. `ncw` warps share this row group and+ * take every ncw-th column tile; `wrow` is the warp's index within the+ * row decomposition upstream assumes. */+ constexpr int ncw = mmq_get_ncw_device();+ /* mmq_x is a multiple of j_stride for every width the host selector offers;+ * unreachable specializations are still instantiated, so this cannot assert. */+ constexpr int j_stride = ncw*ntx*tile_C::J;+ const int cgroup = threadIdx.y % ncw;+ const int wrow = threadIdx.y / ncw;++ y += (wrow % ntx) * (tile_C::J*MMQ_TILE_Y_K);++ const int * x_qs = (const int *) x;+ const half2 * x_dm = (const half2 *) x_qs + MMQ_TILE_NE_K*2;+ const int * y_qs = (const int *) y + 4;+ const half2 * y_ds = (const half2 *) y;++ const int i0 = (wrow / ntx) * rows_per_warp;++ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += 4) {+ const int k0 = k00 + k01;++ tile_A A[ntx];+#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ load_ldmatrix(A[n], x_qs + (i0 + n*tile_A::I)*MMQ_MMA_TILE_X_K_Q2_K + k0, MMQ_MMA_TILE_X_K_Q2_K);+ }++#pragma unroll+ for (int j0 = cgroup*ntx*tile_C::J; j0 < mmq_x; j0 += j_stride) {+ tile_B B;+ load_ldmatrix(B, y_qs + j0*MMQ_TILE_Y_K + k01, MMQ_TILE_Y_K);++ const int j = j0 + tile_C::get_j(0);+ const float dB = (k01 < MMQ_TILE_NE_K/2) ? __half22float2(y_ds[j*MMQ_TILE_Y_K]).x : __half22float2(y_ds[j*MMQ_TILE_Y_K]).y;+ const float sB = (k01 >= MMQ_TILE_NE_K * 3/4) ? 0+ : (((k01/4)%2) ? __half22float2(y_ds[j*MMQ_TILE_Y_K + (1 + k01/QI8_1)]).y+ : __half22float2(y_ds[j*MMQ_TILE_Y_K + (1 + k01/QI8_1)]).x);++ tile_C Cm;+ if (k01 >= MMQ_TILE_NE_K * 3/4) {+ tile_A A1;+#pragma unroll+ for (int l = 0; l < tile_A::ne; ++l) {+ A1.x[l] = 0x01010101;+ }+ mma(Cm, A1, B);+ }++#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ tile_C Cd;+ mma(Cd, A[n], B);++#pragma unroll+ for (int l = 0; l < tile_C::ne; ++l) {+ const int i = i0 + n*tile_C::I + tile_C::get_i(l);+ const float2 dm = __half22float2(x_dm[i*MMQ_MMA_TILE_X_K_Q2_K + k0/4]);+ float tmp = Cd.x[l]*dm.x;+ if (k01 >= MMQ_TILE_NE_K * 3/4) {+ tmp -= Cm.x[l]*dm.y;+ }+ sum[((j0 - cgroup*ntx*tile_C::J)/j_stride*ntx + n)*tile_C::ne + l] += tmp*dB;+ sum[((j0 - cgroup*ntx*tile_C::J)/j_stride*ntx + n)*tile_C::ne + l] -= dm.y*sB;+ }+ }+ }+ }+#else+ GGML_UNUSED_VARS(x, y, sum, k00);+ NO_DEVICE_CODE;+#endif // AMD_MFMA_AVAILABLE || AMD_WMMA_AVAILABLE+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q3_K(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q3_K, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+ int * x_sc = (int *) (x_df + txs.dm);+#endif // defined(AMD_MFMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR3_K);+ constexpr int nrows = warp_size / threads_per_row;+ const int kqsx = threadIdx.x % threads_per_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q3_K * bxi = (const block_q3_K *) x + kbx0 + i*stride;++ const int x_ql_0 = get_int_b2(bxi->qs, kqsx);+ const int x_qh_0 = get_int_b2(bxi->hmask, kqsx % (QI3_K/2)) >> (4 * (kqsx / (QI3_K/2)));++#pragma unroll+ for (int l = 0; l < QR3_K; ++l) {+ const int k = (kqsx/8)*32 + l*8 + kqsx % 8;++ const int x_ql_k = (x_ql_0 >> (2*l)) & 0x03030303;+ const int x_qh_k = ((x_qh_0 >> l) << 2) & 0x04040404;++ const int x_qs_k = __vsubss4(x_ql_k | x_qh_k, 0x04040404);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q3_K + k] = x_qs_k;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + k] = x_qs_k;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+ }++ constexpr int rows_per_warp = warp_size / 4;+#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps*rows_per_warp) {+ int i = i0 + threadIdx.y*rows_per_warp + threadIdx.x/4;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q3_K * bxi = (const block_q3_K *) x + kbx0 + i*stride;++ const int ksc = threadIdx.x % 4;++ const int ksc_low = ksc % (QI3_K/8);+ const int shift_low = 4 * (ksc / (QI3_K/8));+ const int sc_low = (get_int_b2(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;++ const int ksc_high = QI3_K/8;+ const int shift_high = 2 * ksc;+ const int sc_high = ((get_int_b2(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;++ const int sc = __vsubss4(sc_low | sc_high, 0x20202020);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ const int8_t * sc8 = (const int8_t *) ≻+ const float d = bxi->d;++#pragma unroll+ for (int l = 0; l < int(sizeof(int)); ++l) {+ x_df[i*MMQ_MMA_TILE_X_K_Q3_K + sizeof(int)*ksc + l] = d*sc8[l];+ }+#else+ x_sc[i*(MMQ_TILE_NE_K/8) + i/8 + ksc] = sc;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++#if !(defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE))+#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps*warp_size) {+ int i = (i0 + threadIdx.y*warp_size + threadIdx.x) % mmq_y;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q3_K * bxi = (const block_q3_K *) x + kbx0 + i*stride;++ x_df[i] = bxi->d;+ }+#endif // !(defined(AMD_MFMA_AVAILABLE)) || defined(AMD_WMMA_AVAILABLE)+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q3_K_q8_1_dp4a(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q3_K, mmq_y);+ const int * x_qs = (const int *) x;+ const float * x_df = (const float *) x_qs + txs.qs;+ const int * x_sc = (const int *) x_df + txs.dm;+ const int * y_qs = (const int *) y + 4;+ const float * y_df = (const float *) y;++// #pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QR3_K*VDR_Q3_K_Q8_1_MMQ) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;++ const int8_t * scales = ((const int8_t *) (x_sc + i*(MMQ_TILE_NE_K/8) + i/8)) + k0/4;++ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q3_K_q8_1_impl_mmq(+ &x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0], &y_qs[j*MMQ_TILE_Y_K + k01], scales,+ x_df[i], y_df[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }+ }+ }+}++static __device__ __forceinline__ int unpack_scales_q45_K(const int * scales, const int ksc) {+ // scale arrangement after the following two lines:+ // - ksc == 0: sc0, sc1, sc2, sc3+ // - ksc == 1: sc4, sc5, sc6, sc7+ // - ksc == 2: m0, m1, m2, m3+ // - ksc == 3: m4, m5, m6, m7+ return ((scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F) | // lower 4 bits+ ((scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030); // upper 2 bits+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q4_K(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + 2*MMQ_TILE_NE_K);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q4_K, mmq_y);+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + txs.qs);+ int * x_sc = (int *) (x_dm + txs.dm);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR4_K);+ constexpr int nrows = warp_size / threads_per_row;+ const int txi = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q4_K * bxi = (const block_q4_K *) x + kbx0 + i*stride;+ const int qs0 = get_int_b4(bxi->qs, txi);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + 16*(txi/8) + txi % 8 + 0] = (qs0 >> 0) & 0x0F0F0F0F;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + 16*(txi/8) + txi % 8 + 8] = (qs0 >> 4) & 0x0F0F0F0F;+#else+ x_qs[i*(MMQ_TILE_NE_K + 1) + txi] = qs0;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ constexpr int rows_per_warp = warp_size / 2;+#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps*rows_per_warp) {+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ // Need if on AMD instead of % because warp_size == 64+ // This causes double work and throughput loss (MI300X)+ // H100 loses about 100 t/s with 'if' condition over '%'+ int i = i0 + threadIdx.y*rows_per_warp + threadIdx.x/2;+ if (i < mmq_y) {+#else+ int i = (i0 + threadIdx.y*rows_per_warp + threadIdx.x/2) % mmq_y;+ {+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ if (need_check) {+ i = min(i, i_max);+ }++ const block_q4_K * bxi = (const block_q4_K *) x + kbx0 + i*stride;++ const int * scales = (const int *) bxi->scales;+ const int ksc = threadIdx.x % 2;++ const int sc32 = unpack_scales_q45_K(scales, ksc + 0);+ const int m32 = unpack_scales_q45_K(scales, ksc + 2);++ const uint8_t * sc8 = (const uint8_t *) &sc32;+ const uint8_t * m8 = (const uint8_t *) &m32;++ const half2 dm = bxi->dm * make_half2(1.0f, -1.0f);++ #pragma unroll+ for (int l = 0; l < sizeof(int); ++l) {+ x_dm[i*MMQ_MMA_TILE_X_K_Q8_1 + sizeof(int)*ksc + l] = dm*make_half2(sc8[l], m8[l]);+ }+ }+ }+#else+#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps*warp_size) {+ int i = (i0 + threadIdx.y*warp_size + threadIdx.x) % mmq_y;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q4_K * bxi = (const block_q4_K *) x + kbx0 + i*stride;++ x_dm[i] = bxi->dm;+ }+ constexpr int rows_per_warp = warp_size / 4;+#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps*rows_per_warp) {+ int i = (i0 + threadIdx.y*rows_per_warp + threadIdx.x/(MMQ_TILE_NE_K/8)) % mmq_y;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q4_K * bxi = (const block_q4_K *) x + kbx0 + i*stride + (threadIdx.x % (MMQ_TILE_NE_K/8)) / (QI4_K/8);++ const int * scales = (const int *) bxi->scales;++ const int ksc = threadIdx.x % (MMQ_TILE_NE_K/8);+ const int scales8 = unpack_scales_q45_K(scales, ksc);++ x_sc[i*(MMQ_TILE_NE_K/8) + i/8 + ksc] = scales8;+ }+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q4_K_q8_1_dp4a(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q4_K, mmq_y);+ const int * x_qs = (const int *) x;+ const half2 * x_dm = (const half2 *) x_qs + txs.qs;+ const int * x_sc = (const int *) x_dm + txs.dm;+ const int * y_qs = (const int *) y + 4;+ const half2 * y_ds = (const half2 *) y;++// #pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QR4_K*VDR_Q4_K_Q8_1_MMQ) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;++ const uint8_t * sc = (const uint8_t *) &x_sc[i * (MMQ_TILE_NE_K/8) + i/8 + k0/32] + 2*(k01/16);++ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q4_K_q8_1_impl_mmq(+ &x_qs[i*(MMQ_TILE_NE_K + 1) + k0/2], &y_qs[j*MMQ_TILE_Y_K + k01], sc, sc+8,+ x_dm[i], &y_ds[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }+ }+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q5_K(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q5_K, mmq_y);+ int * x_qs = (int *) x_tile;+ half2 * x_dm = (half2 *) (x_qs + txs.qs);+ int * x_sc = (int *) (x_dm + txs.dm);+#endif // defined(AMD_MFMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR5_K);+ constexpr int nrows = warp_size / threads_per_row;+ const int txi = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q5_K * bxi = (const block_q5_K *) x + kbx0 + i*stride;+ const int ky = QR5_K*txi;++ const int ql = get_int_b4(bxi->qs, txi);+ const int ql0 = (ql >> 0) & 0x0F0F0F0F;+ const int ql1 = (ql >> 4) & 0x0F0F0F0F;++ const int qh = get_int_b4(bxi->qh, txi % (QI5_K/4));+ const int qh0 = ((qh >> (2 * (txi / (QI5_K/4)) + 0)) << 4) & 0x10101010;+ const int qh1 = ((qh >> (2 * (txi / (QI5_K/4)) + 1)) << 4) & 0x10101010;++ const int kq0 = ky - ky % (QI5_K/2) + txi % (QI5_K/4) + 0;+ const int kq1 = ky - ky % (QI5_K/2) + txi % (QI5_K/4) + QI5_K/4;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + kq0] = ql0 | qh0;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + kq1] = ql1 | qh1;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + kq0] = ql0 | qh0;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + kq1] = ql1 | qh1;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ constexpr int rows_per_warp = warp_size / 2;+#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps*rows_per_warp) {+#if defined(AMD_MFMA_AVAILABLE)+ // Need if on AMD instead of % because warp_size == 64+ // This causes double work and throughput loss (MI300X)+ // H100 loses about 100 t/s with 'if' condition over '%'+ int i = i0 + threadIdx.y*rows_per_warp + threadIdx.x/2;+ if (i < mmq_y) {+#else+ int i = (i0 + threadIdx.y*rows_per_warp + threadIdx.x/2) % mmq_y;+ {+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ if (need_check) {+ i = min(i, i_max);+ }++ const block_q5_K * bxi = (const block_q5_K *) x + kbx0 + i*stride;++ const int * scales = (const int *) bxi->scales;+ const int ksc = threadIdx.x % 2;++ const int sc32 = unpack_scales_q45_K(scales, ksc + 0);+ const int m32 = unpack_scales_q45_K(scales, ksc + 2);++ const uint8_t * sc8 = (const uint8_t *) &sc32;+ const uint8_t * m8 = (const uint8_t *) &m32;++ const half2 dm = bxi->dm * make_half2(1.0f, -1.0f);++#pragma unroll+ for (int l = 0; l < int(sizeof(int)); ++l) {+ x_dm[i*MMQ_MMA_TILE_X_K_Q8_1 + sizeof(int)*ksc + l] = dm*make_half2(sc8[l], m8[l]);+ }+ }+ }+#else+#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps*warp_size) {+ int i = (i0 + threadIdx.y*warp_size + threadIdx.x) % mmq_y;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q5_K * bxi = (const block_q5_K *) x + kbx0 + i*stride;++ x_dm[i] = bxi->dm;+ }++ constexpr int rows_per_warp = warp_size / 4;+#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps*rows_per_warp) {+ int i = (i0 + threadIdx.y*rows_per_warp + threadIdx.x/(MMQ_TILE_NE_K/8)) % mmq_y;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q5_K * bxi = (const block_q5_K *) x + kbx0 + i*stride;++ const int * scales = (const int *) bxi->scales;++ const int ksc = threadIdx.x % (MMQ_TILE_NE_K/8);+ const int scales8 = unpack_scales_q45_K(scales, ksc);++ x_sc[i*(MMQ_TILE_NE_K/8) + i/8 + ksc] = scales8;+ }+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q5_K_q8_1_dp4a(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q5_K, mmq_y);+ const int * x_qs = (const int *) x;+ const half2 * x_dm = (const half2 *) x_qs + txs.qs;+ const int * x_sc = (const int *) x_dm + txs.dm;+ const int * y_qs = (const int *) y + 4;+ const half2 * y_ds = (const half2 *) y;++// #pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QR5_K*VDR_Q5_K_Q8_1_MMQ) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;++ const uint8_t * sc = ((const uint8_t *) &x_sc[i * (MMQ_TILE_NE_K/8) + i/8 + k00/32]) + 2*(k01/16);++ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q5_K_q8_1_impl_mmq(+ &x_qs[i*(QR5_K*MMQ_TILE_NE_K + 1) + k0], &y_qs[j*MMQ_TILE_Y_K + k01], sc, sc+8,+ x_dm[i], &y_ds[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }+ }+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_q6_K(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+ int * x_sc = (int *) (x_df + MMQ_TILE_NE_K/QI6_K);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q6_K, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+ int * x_sc = (int *) (x_df + txs.dm);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR6_K);+ constexpr int nrows = warp_size / threads_per_row;+ const int txi = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q6_K * bxi = (const block_q6_K *) x + kbx0 + i*stride;++ const int ql = get_int_b2(bxi->ql, txi);+ const int ql0 = (ql >> 0) & 0x0F0F0F0F;+ const int ql1 = (ql >> 4) & 0x0F0F0F0F;++ const int qh = get_int_b2(bxi->qh, (QI6_K/4) * (txi / (QI6_K/2)) + txi % (QI6_K/4));+ const int qh0 = ((qh >> ((txi & 0x08) >> 2)) << 4) & 0x30303030;+ const int qh1 = (qh >> ((txi & 0x08) >> 2)) & 0x30303030;++ const int kq0 = 2*txi - txi % (QI6_K/2) + 0;+ const int kq1 = 2*txi - txi % (QI6_K/2) + QI6_K/2;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q6_K + kq0] = __vsubss4(ql0 | qh0, 0x20202020);+ x_qs[i*MMQ_MMA_TILE_X_K_Q6_K + kq1] = __vsubss4(ql1 | qh1, 0x20202020);+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + kq0] = __vsubss4(ql0 | qh0, 0x20202020);+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + kq1] = __vsubss4(ql1 | qh1, 0x20202020);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps*warp_size) {+ int i = (i0 + threadIdx.y*warp_size + threadIdx.x) % mmq_y;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q6_K * bxi = (const block_q6_K *) x + kbx0 + i*stride;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q6_K] = bxi->d;+#else+ x_df[i*(MMQ_TILE_NE_K/QI6_K) + i/QI6_K] = bxi->d;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ constexpr int rows_per_warp = warp_size / 4;+#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps*rows_per_warp) {+ int i = (i0 + threadIdx.y*rows_per_warp + threadIdx.x/(MMQ_TILE_NE_K/8)) % mmq_y;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_q6_K * bxi = (const block_q6_K *) x + kbx0 + i*stride + (threadIdx.x % (MMQ_TILE_NE_K/8)) / 4;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_sc[i*MMQ_MMA_TILE_X_K_Q6_K + threadIdx.x%4] = get_int_b2(bxi->scales, threadIdx.x % (MMQ_TILE_NE_K/8));+#else+ x_sc[i*(MMQ_TILE_NE_K/8) + i/8 + threadIdx.x%(MMQ_TILE_NE_K/8)] = get_int_b2(bxi->scales, threadIdx.x%(QI6_K/8));+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q6_K_q8_1_dp4a(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_Q6_K, mmq_y);+ const int * x_qs = (const int *) x;+ const float * x_df = (const float *) x_qs + txs.qs;+ const int * x_sc = (const int *) x_df + txs.dm;+ const int * y_qs = (const int *) y + 4;+ const float * y_df = (const float *) y;++// #pragma unroll+ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += QR6_K*VDR_Q6_K_Q8_1_MMQ) {+ const int k0 = k00 + k01;++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;++ const int8_t * sc = ((const int8_t *) &x_sc[i * (MMQ_TILE_NE_K/8) + i/8 + k0/16]);++ sum[j0/nwarps*mmq_y/warp_size + i0/warp_size] += vec_dot_q6_K_q8_1_impl_mmq(+ &x_qs[i*(QR6_K*MMQ_TILE_NE_K + 1) + k0], &y_qs[j*MMQ_TILE_Y_K + k01], sc,+ x_df[i*(MMQ_TILE_NE_K/QI6_K) + i/QI6_K], &y_df[j*MMQ_TILE_Y_K + k01/QI8_1]);+ }+ }+ }+}++template <int mmq_x, int mmq_y>+static __device__ __forceinline__ void vec_dot_q6_K_q8_1_mma(+ const int * __restrict__ x, const int * __restrict__ y, float * __restrict__ sum, const int k00) {+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ constexpr data_layout input_layout = get_input_data_layout();+ typedef tile<16, 4, int, input_layout> tile_A;+ typedef tile<16, 4, int, input_layout> tile_B;+ typedef tile<16, 16, int, DATA_LAYOUT_J_MAJOR> tile_C;++ constexpr int granularity = mmq_get_granularity_device(mmq_x);+ constexpr int rows_per_warp = granularity;+ constexpr int ntx = rows_per_warp/tile_C::I; // Number of x minitiles per warp.+ /* ds4: see QFN_WMMA_MMQ_NCW. `ncw` warps share this row group and+ * take every ncw-th column tile; `wrow` is the warp's index within the+ * row decomposition upstream assumes. */+ constexpr int ncw = mmq_get_ncw_device();+ /* mmq_x is a multiple of j_stride for every width the host selector offers;+ * unreachable specializations are still instantiated, so this cannot assert. */+ constexpr int j_stride = ncw*ntx*tile_C::J;+ const int cgroup = threadIdx.y % ncw;+ const int wrow = threadIdx.y / ncw;++ y += (wrow % ntx) * (tile_C::J*MMQ_TILE_Y_K);++ const int * x_qs = (const int *) x;+ const float * x_df = (const float *) x_qs + MMQ_TILE_NE_K*2;+ const int * x_sc = (const int *) x_df + MMQ_TILE_NE_K/QI6_K;+ const int * y_qs = (const int *) y + 4;+ const float * y_df = (const float *) y;++ const int i0 = (wrow / ntx) * rows_per_warp;++ for (int k01 = 0; k01 < MMQ_TILE_NE_K; k01 += 4) {+ const int k0 = k00 + k01;++ tile_A A[ntx];+#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ load_ldmatrix(A[n], x_qs + (i0 + n*tile_A::I)*MMQ_MMA_TILE_X_K_Q6_K + k0, MMQ_MMA_TILE_X_K_Q6_K);+ }++#pragma unroll+ for (int j0 = cgroup*ntx*tile_C::J; j0 < mmq_x; j0 += j_stride) {+ tile_B B;+ load_ldmatrix(B, y_qs + j0*MMQ_TILE_Y_K + k01, MMQ_TILE_Y_K);++ const int j = j0 + tile_C::get_j(0);+ const float dB = y_df[j*MMQ_TILE_Y_K + k01/QI8_1];++#pragma unroll+ for (int n = 0; n < ntx; ++n) {+ tile_C C;+ mma(C, A[n], B);++#pragma unroll+ for (int l = 0; l < tile_C::ne; ++l) {+ const int i = i0 + n*tile_C::I + tile_C::get_i(l);+ const int8_t * sc = (const int8_t *) (x_sc + i*MMQ_MMA_TILE_X_K_Q6_K + k00/16);+ sum[((j0 - cgroup*ntx*tile_C::J)/j_stride*ntx + n)*tile_C::ne + l] += C.x[l] * sc[k01/4] * x_df[i*MMQ_MMA_TILE_X_K_Q6_K] * dB;+ }+ }+ }+ }+#else+ GGML_UNUSED_VARS(x, y, sum, k00);+ NO_DEVICE_CODE;+#endif // AMD_MFMA_AVAILABLE || AMD_WMMA_AVAILABLE+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_iq4_nl(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_IQ4_NL, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR4_NL);+ constexpr int nrows = warp_size / threads_per_row;+ const int txi = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;+ const int kbx = txi / QI4_NL;+ const int kqsx = txi % QI4_NL;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_iq4_nl * bxi = (const block_iq4_nl *) x + kbx0 + i*stride + kbx;++ const int aux_q4 = get_int_b2(bxi->qs, kqsx);+ const int2 v = get_int_from_table_16(aux_q4, kvalues_iq4nl);+ const int k0 = kbx * (2 * QI4_NL) + kqsx;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + k0 + 0] = v.x;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + k0 + QI4_NL] = v.y;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0 + 0] = v.x;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0 + QI4_NL] = v.y;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ constexpr int blocks_per_tile_x_row = MMQ_TILE_NE_K / QI4_NL;+ constexpr int rows_per_warp = warp_size / blocks_per_tile_x_row;+ const int kbxd = threadIdx.x % blocks_per_tile_x_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * rows_per_warp) {+ int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / blocks_per_tile_x_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_iq4_nl * bxi = (const block_iq4_nl *) x + kbx0 + i*stride + kbxd;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + kbxd] = __half2float(bxi->d);+#else+ x_df[i*(MMQ_TILE_NE_K/QI4_NL) + i/QI4_NL + kbxd] = __half2float(bxi->d);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++#if defined(__gfx1151__)+static __device__ __forceinline__ uint32_t iq2_xxs_apply_sign4(+ const uint32_t grid, const uint32_t signs) {+ const uint32_t bits = signs & 0x0fu;+ const uint32_t add = (bits * 0x00204081u) & 0x01010101u;+ // IQ2 grid bytes are nonzero, so per-byte two's-complement negation+ // cannot carry into an adjacent byte.+ return (grid ^ (add * 0xffu)) + add;+}+#endif++template <int mmq_y, bool need_check, int fixed_stride = 0> static __device__ __forceinline__ void load_tiles_iq2_xxs(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_IQ2_XXS, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = (MMQ_ITER_K / (4 * QR2_XXS)) / 2;+ constexpr int nrows = warp_size / threads_per_row;+ const int kqsx = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;++#if defined(__gfx1151__)+// Limit concurrent row decompression while retaining two-row ILP.+#pragma unroll 2+#else+#pragma unroll+#endif+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * nrows) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_iq2_xxs * bxi;+ if constexpr (fixed_stride != 0) {+ bxi = (const block_iq2_xxs *) x + kbx0 + i*fixed_stride;+ } else {+ bxi = (const block_iq2_xxs *) x + kbx0 + i*stride;+ }++ const int q2 = get_int_b2(bxi->qs, 2*kqsx+0);+ const uint8_t * aux8 = (const uint8_t *) &q2;+ const uint32_t aux32 = get_int_b2(bxi->qs, 2*kqsx+1);++#pragma unroll+ for (int l = 0; l < QR2_XXS; ++l) {+#if defined(__gfx1151__)+ const uint32_t sign_bits = (aux32 >> (7 * l)) & 0x7fu;+ int grid0;+ int grid1;+ /* ds4: two rewrites of this expansion were measured and rejected,+ * both bit-identical. Deriving the sign masks arithmetically instead+ * of loading ksigns64 was neutral (435.1 against 439.6 tok/s), and+ * precomputing the whole sign-applied codebook into a 2^15-entry+ * table -- one 8-byte load replacing two loads and six operations --+ * was slightly worse (412.1 against 418.7). The loader's time is not+ * sensitive to its own instruction mix; see the counter table in+ * docs/internal/models/deepseek-v4-flash/BENCHMARKS.md. */+ if constexpr (fixed_stride == 16) {+ const uint2 grid_pos = ((const uint2*)iq2xxs_grid)[aux8[l]];+ const uint2 sign_mask = ((const uint2 *) ksigns64)[sign_bits];+ const uint32_t add0 = sign_mask.x & 0x01010101u;+ const uint32_t add1 = sign_mask.y & 0x01010101u;+ grid0 = (grid_pos.x ^ sign_mask.x) + add0;+ grid1 = (grid_pos.y ^ sign_mask.y) + add1;+ } else {+ const uint2 grid_pos = ((const uint2*)iq2xxs_grid)[aux8[l]];+ const uint32_t signs = sign_bits | ((__popc(sign_bits) & 1u) << 7);+ grid0 = iq2_xxs_apply_sign4(grid_pos.x, signs);+ grid1 = iq2_xxs_apply_sign4(grid_pos.y, signs >> 4);+ }+#else+ const uint2 grid_pos = ((const uint2*)iq2xxs_grid)[aux8[l]];+ const uint32_t signs = unpack_ksigns(aux32 >> (7 * l));++ const int signs0 = __vcmpne4(signs & 0x08040201, 0);+ const int grid0 = __vsub4(grid_pos.x ^ signs0, signs0);++ const int signs1 = __vcmpne4(signs & 0x80402010, 0);+ const int grid1 = __vsub4(grid_pos.y ^ signs1, signs1);+#endif++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ /* ds4: one 8-byte store, not two 4-byte ones.+ *+ * Left as two scalar stores the compiler pairs them into+ * `ds_store_2addr_stride64_b32`, whose two addresses are 64 dwords+ * apart -- exactly 32 banks, hence the same bank -- so every such+ * instruction self-conflicts before cross-lane conflicts are counted.+ * Counters put this loader at 19.7% LDS bank-conflict cycles against+ * the dense Q8 path's 7.1%. The pair is always 8-byte aligned here+ * (the row stride is even, and so are `8*kqsx` and `2*l`), so writing+ * it as one `int2` gives `ds_store_b64` across banks b and b+1. */+ *reinterpret_cast<int2 *>(+ &x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + 8*kqsx + 2*l]) =+ make_int2(grid0, grid1);+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l + 0)] = grid0;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l + 1)] = grid1;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ const int ls = aux32 >> 27 | 1; // (scale * 2 + 1)+ const float d = bxi->d;+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + kqsx] = d * ls / 8; // (d * scale + d / 2) / 4+#else+ x_df[i*(MMQ_TILE_NE_K/4) + i/4 + kqsx] = d * ls / 8; // (d * scale + d / 2) / 4+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++// ds4 (P4 Inc3): aligned-SoA twin of load_tiles_iq2_xxs. x points at the+// weight-server artifact [__half dq | uint2 qs] (iq2_xxs_aligned_derepack_kernel+// in qfn_mmq.cu is the byte-level spec) and soa_nblk is the total block+// count. Each lane's (q2, aux32) int pair is exactly one artifact uint2, so+// the loads are 8-byte aligned and fully coalesced; the unpack math is the+// standard loader's, making tile_x bit-identical.+template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_iq2_xxs_soa(+ const char * __restrict__ x, const int64_t soa_nblk, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_IQ2_XXS, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = (MMQ_ITER_K / (4 * QR2_XXS)) / 2;+ constexpr int nrows = warp_size / threads_per_row;+ const int kqsx = warp_size > threads_per_row ? threadIdx.x % threads_per_row : threadIdx.x;++ const uint64_t dq_bytes = ((uint64_t)soa_nblk * 2u + 63u) & ~63ull;+ const half * dq = (const half *) x;+ const uint2 * qs = (const uint2 *) (x + dq_bytes);++#if defined(__gfx1151__)+#pragma unroll 2+#else+#pragma unroll+#endif+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * nrows) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ // Flat block index; < 2^25 for both model shapes, so 32-bit element+ // indexing is safe.+ const uint32_t blk = (uint32_t)(kbx0 + i*stride);++ const uint2 v = qs[blk*8u + kqsx];+ const int q2 = (int)v.x;+ const uint8_t * aux8 = (const uint8_t *) &q2;+ const uint32_t aux32 = v.y;++#pragma unroll+ for (int l = 0; l < QR2_XXS; ++l) {+ const uint2 grid_pos = ((const uint2*)iq2xxs_grid)[aux8[l]];+#if defined(__gfx1151__)+ const uint32_t sign_bits = (aux32 >> (7 * l)) & 0x7fu;+ const uint32_t signs = sign_bits | ((__popc(sign_bits) & 1u) << 7);++ const int grid0 = iq2_xxs_apply_sign4(grid_pos.x, signs);+ const int grid1 = iq2_xxs_apply_sign4(grid_pos.y, signs >> 4);+#else+ const uint32_t signs = unpack_ksigns(aux32 >> (7 * l));++ const int signs0 = __vcmpne4(signs & 0x08040201, 0);+ const int grid0 = __vsub4(grid_pos.x ^ signs0, signs0);++ const int signs1 = __vcmpne4(signs & 0x80402010, 0);+ const int grid1 = __vsub4(grid_pos.y ^ signs1, signs1);+#endif++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ /* ds4: one 8-byte store, not two 4-byte ones.+ *+ * Left as two scalar stores the compiler pairs them into+ * `ds_store_2addr_stride64_b32`, whose two addresses are 64 dwords+ * apart -- exactly 32 banks, hence the same bank -- so every such+ * instruction self-conflicts before cross-lane conflicts are counted.+ * Counters put this loader at 19.7% LDS bank-conflict cycles against+ * the dense Q8 path's 7.1%. The pair is always 8-byte aligned here+ * (the row stride is even, and so are `8*kqsx` and `2*l`), so writing+ * it as one `int2` gives `ds_store_b64` across banks b and b+1. */+ *reinterpret_cast<int2 *>(+ &x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + 8*kqsx + 2*l]) =+ make_int2(grid0, grid1);+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l + 0)] = grid0;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l + 1)] = grid1;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ const int ls = aux32 >> 27 | 1; // (scale * 2 + 1)+ const float d = dq[blk];+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + kqsx] = d * ls / 8; // (d * scale + d / 2) / 4+#else+ x_df[i*(MMQ_TILE_NE_K/4) + i/4 + kqsx] = d * ls / 8; // (d * scale + d / 2) / 4+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_iq2_xs(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = MMQ_DP4A_TXS_Q8_0_16;+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = (MMQ_ITER_K / (4 * QR2_XS)) / 2;+ constexpr int nrows = warp_size / threads_per_row;+ const int kqsx = threadIdx.x % threads_per_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * nrows) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_iq2_xs * bxi = (const block_iq2_xs *) x + kbx0 + i*stride;++ const int2 q2_packed = make_int2(get_int_b2(bxi->qs, 2*kqsx+0), get_int_b2(bxi->qs, 2*kqsx+1));+ const uint16_t * q2 = (const uint16_t *) &q2_packed;++ #pragma unroll+ for (int l = 0; l < QR2_XS; ++l) {+ const uint2 grid_pos = ((const uint2*)iq2xs_grid)[q2[l] & 0x1FF];+ const uint32_t signs = unpack_ksigns(q2[l] >> 9);++ const int signs0 = __vcmpne4(signs & 0x08040201, 0);+ const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0);++ const int signs1 = __vcmpne4(signs & 0x80402010, 0);+ const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q3_K + 8*kqsx + (2*l + 0)] = grid_l;+ x_qs[i*MMQ_MMA_TILE_X_K_Q3_K + 8*kqsx + (2*l + 1)] = grid_h;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l + 0)] = grid_l;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l + 1)] = grid_h;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ const int ls = bxi->scales[kqsx];+ const float d = bxi->d;+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q3_K + 2*kqsx+0] = ((ls & 0x0F)*d + d/2)/4;+ x_df[i*MMQ_MMA_TILE_X_K_Q3_K + 2*kqsx+1] = ((ls >> 4)*d + d/2)/4;+#else+ x_df[i*(2*MMQ_TILE_NE_K*2/QI8_0) + i/(QI8_0/4) + 2*kqsx+0] = ((ls & 0x0F)*d + d/2)/4;+ x_df[i*(2*MMQ_TILE_NE_K*2/QI8_0) + i/(QI8_0/4) + 2*kqsx+1] = ((ls >> 4)*d + d/2)/4;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_iq2_s(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_IQ2_S, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ constexpr int threads_per_row = (MMQ_ITER_K / (4 * QR2_S)) / 2;+ constexpr int nrows = warp_size / threads_per_row;+ const int kqsx = threadIdx.x % threads_per_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * nrows) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_iq2_s * bxi = (const block_iq2_s *) x + kbx0 + i*stride;++ const int qs_packed = get_int_b2(bxi->qs, kqsx);+ const uint8_t * qs = (const uint8_t *) &qs_packed;++ const int qh = bxi->qh[kqsx];++ const int signs_packed_32 = get_int_b2(bxi->qs, QK_K/32 + kqsx);+ const uint8_t * signs_packed_8 = (const uint8_t *) &signs_packed_32;++#pragma unroll+ for (int l = 0; l < QR2_S; ++l) {+ const int * grid_pos = (const int *)(iq2s_grid + (qs[l] | ((qh << (8-2*l)) & 0x300)));++ const int signs0 = __vcmpne4(((signs_packed_8[l] & 0x03) << 7) | ((signs_packed_8[l] & 0x0C) << 21), 0x00000000);+ const int signs1 = __vcmpne4(((signs_packed_8[l] & 0x30) << 3) | ((signs_packed_8[l] & 0xC0) << 17), 0x00000000);++ const int grid_l = __vsub4(grid_pos[0] ^ signs0, signs0);+ const int grid_h = __vsub4(grid_pos[1] ^ signs1, signs1);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q3_K + 8*kqsx + (2*l + 0)] = grid_l;+ x_qs[i*MMQ_MMA_TILE_X_K_Q3_K + 8*kqsx + (2*l + 1)] = grid_h;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l + 0)] = grid_l;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l + 1)] = grid_h;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ const int ls = bxi->scales[kqsx];+ const float d = bxi->d;+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q3_K + 2*kqsx+0] = ((ls & 0x0F)*d + d/2)/4;+ x_df[i*MMQ_MMA_TILE_X_K_Q3_K + 2*kqsx+1] = ((ls >> 4)*d + d/2)/4;+#else+ x_df[i*(2*MMQ_TILE_NE_K*2/QI8_0) + i/(QI8_0/4) + 2*kqsx+0] = ((ls & 0x0F)*d + d/2)/4;+ x_df[i*(2*MMQ_TILE_NE_K*2/QI8_0) + i/(QI8_0/4) + 2*kqsx+1] = ((ls >> 4)*d + d/2)/4;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_iq3_xxs(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_IQ3_XXS, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = (MMQ_ITER_K / (4 * QR3_XXS)) / 2;+ constexpr int nrows = warp_size / threads_per_row;+ const int kqsx = threadIdx.x % threads_per_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * nrows) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_iq3_xxs * bxi = (const block_iq3_xxs *) x + kbx0 + i*stride;++ const int2 q3_packed = make_int2(get_int_b2(bxi->qs, 2*kqsx+0), get_int_b2(bxi->qs, 2*kqsx+1));+ const uint8_t * q3 = (const uint8_t *) &q3_packed;+ const uint32_t aux32 = get_int_b2(bxi->qs, QK_K/16 + kqsx);++#pragma unroll+ for (int l = 0; l < QR3_XXS; ++l) {+ const int2 grid_pos = make_int2(iq3xxs_grid[q3[2*l+0]], iq3xxs_grid[q3[2*l+1]]);+ const uint32_t signs = unpack_ksigns(aux32 >> (7*l));++ const int signs0 = __vcmpne4(signs & 0x08040201, 0);+ const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0);++ const int signs1 = __vcmpne4(signs & 0x80402010, 0);+ const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + 8*kqsx + (2*l + 0)] = grid_l;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + 8*kqsx + (2*l + 1)] = grid_h;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l + 0)] = grid_l;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l + 1)] = grid_h;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ const int ls = aux32 >> 28;+ const float d = bxi->d;+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + kqsx] = (ls*d + d/2)/2;+#else+ x_df[i*(MMQ_TILE_NE_K/4) + i/4 + kqsx] = (ls*d + d/2)/2;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_iq3_s(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_IQ3_S, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = (MMQ_ITER_K / (4 * QR3_S)) / 2;+ constexpr int nrows = warp_size / threads_per_row;+ const int kqsx = threadIdx.x % threads_per_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * nrows) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_iq3_s * bxi = (const block_iq3_s *) x + kbx0 + i*stride;++ const int2 qs_packed = make_int2(get_int_b2(bxi->qs, 2*kqsx+0), get_int_b2(bxi->qs, 2*kqsx+1));+ const uint8_t * qs = (const uint8_t *) &qs_packed;++ const int qh = bxi->qh[kqsx];++ const int signs_packed_32 = get_int_b2(bxi->signs, kqsx);+ const uint8_t * signs_packed_8 = (const uint8_t *) &signs_packed_32;++#pragma unroll+ for (int l = 0; l < QR3_S; ++l) {+ const int2 grid_pos = make_int2(+ iq3s_grid[qs[2*l+0] | ((qh << (8 - 2*l)) & 0x100)],+ iq3s_grid[qs[2*l+1] | ((qh << (7 - 2*l)) & 0x100)]);++ const int signs0 = __vcmpne4(((signs_packed_8[l] & 0x03) << 7) | ((signs_packed_8[l] & 0x0C) << 21), 0x00000000);+ const int signs1 = __vcmpne4(((signs_packed_8[l] & 0x30) << 3) | ((signs_packed_8[l] & 0xC0) << 17), 0x00000000);++ const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0);+ const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + 8*kqsx + (2*l+0)] = grid_l;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + 8*kqsx + (2*l+1)] = grid_h;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l+0)] = grid_l;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l+1)] = grid_h;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ const int ls = 1 + 2*((bxi->scales[kqsx/2] >> (((2*kqsx) << 1) & 0x04)) & 0x0F);+ const float d = bxi->d;+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + kqsx] = ls*d;+#else+ x_df[i*(MMQ_TILE_NE_K/4) + i/4 + kqsx] = ls*d;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_iq1_s(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ half2 * x_ds = (half2 *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_IQ3_S, mmq_y);+ int * x_qs = (int *) x_tile;+ half2 * x_ds = (half2 *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR1_S);+ constexpr int nrows = warp_size / threads_per_row;+ const int kqsx = threadIdx.x % threads_per_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * nrows) {+ int i = i0 + threadIdx.y*nrows + threadIdx.x/threads_per_row;++ if (need_check) {+ i = min(i, i_max);+ }++ const block_iq1_s * bxi = (const block_iq1_s *) x + kbx0 + i*stride;++ const int qs_packed = get_int_b2(bxi->qs, kqsx);+ const uint8_t * qs = (const uint8_t *) &qs_packed;++ const int qh = bxi->qh[kqsx];++ #pragma unroll+ for (int l = 0; l < QR1_S/2; ++l) {+ const int grid = iq1s_grid_gpu[qs[l] | (((qh >> (3*l)) & 0x07) << 8)];++ const int grid0 = (grid >> 0) & 0x0F0F0F0F;+ const int grid1 = (grid >> 4) & 0x0F0F0F0F;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + 8*kqsx + (2*l+0)] = grid0;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_1 + 8*kqsx + (2*l+1)] = grid1;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l+0)] = grid0;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + 8*kqsx + (2*l+1)] = grid1;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ const float d1q = __half2float(bxi->d) * (((qh >> 11) & 0x0E) + 1);+ const float delta = -1.0f + IQ1S_DELTA - (qh & 0x8000) * (2.0f*IQ1S_DELTA/0x8000);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_ds[i*MMQ_MMA_TILE_X_K_Q8_1 + kqsx] = make_half2(d1q, d1q*delta);+#else+ x_ds[i*(MMQ_TILE_NE_K/4) + i/4 + kqsx] = make_half2(d1q, d1q*delta);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template <int mmq_y, bool need_check> static __device__ __forceinline__ void load_tiles_iq4_xs(+ const char * __restrict__ x, int * __restrict__ x_tile, const int kbx0, const int i_max, const int stride) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + MMQ_TILE_NE_K*2);+#else+ constexpr tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(GGML_TYPE_IQ4_XS, mmq_y);+ int * x_qs = (int *) x_tile;+ float * x_df = (float *) (x_qs + txs.qs);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int threads_per_row = MMQ_ITER_K / (4 * QR4_XS);+ constexpr int nrows = warp_size / threads_per_row;+ const int kqsx = threadIdx.x % threads_per_row;++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nrows*nwarps) {+ int i = i0 + (nrows == 1 ? threadIdx.y : threadIdx.y*nrows + threadIdx.x/threads_per_row);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_iq4_xs * bxi = (const block_iq4_xs *) x + kbx0 + i*stride;++ const int aux_q4 = get_int_b4(bxi->qs, kqsx);+ const int2 v = get_int_from_table_16(aux_q4, kvalues_iq4nl);+ const int k0 = 8 * (kqsx / 4) + kqsx % 4;++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + k0 + 0] = v.x;+ x_qs[i*MMQ_MMA_TILE_X_K_Q8_0 + k0 + 4] = v.y;+#else+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0 + 0] = v.x;+ x_qs[i*(2*MMQ_TILE_NE_K + 1) + k0 + 4] = v.y;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }++ constexpr int rows_per_warp = warp_size / 8;+#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += nwarps * rows_per_warp) {+ int i = i0 + threadIdx.y * rows_per_warp + threadIdx.x / (MMQ_TILE_NE_K/4);++ if (need_check) {+ i = min(i, i_max);+ }++ const block_iq4_xs * bxi = (const block_iq4_xs *) x + kbx0 + i*stride;++ const float d = __half2float(bxi->d);++ const int ls = ((bxi->scales_l[(threadIdx.x % 8)/2] >> (4*(threadIdx.x % 2))) & 0x0F)+ | (((bxi->scales_h >> (2*(threadIdx.x % 8))) & 0x03) << 4);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ x_df[i*MMQ_MMA_TILE_X_K_Q8_0 + threadIdx.x % 8] = d * (ls - 32);+#else+ x_df[i*(MMQ_TILE_NE_K/4) + i/4 + threadIdx.x % 8] = d * (ls - 32);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ }+}++template<int mmq_x, int mmq_y, bool need_check, bool sanitize_output>+static __device__ __forceinline__ void mmq_write_back_dp4a(+ const float * __restrict__ sum, const int32_t * __restrict__ ids_dst, float * __restrict__ dst,+ const int stride, const int i_max, const int j_max,+ const float * __restrict__ epilogue_gate, const float * __restrict__ epilogue_weights,+ float * __restrict__ epilogue_mid, half * __restrict__ epilogue_mid_h,+ const float epilogue_clamp) {+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++ if (j > j_max) {+ return;+ }++#pragma unroll+ for (int i0 = 0; i0 < mmq_y; i0 += warp_size) {+ const int i = i0 + threadIdx.x;++ if (need_check && i > i_max) {+ continue;+ }++ const int dst_j = ids_dst ? ids_dst[j] : j;+ const int dst_i = dst_j*stride + i;+ float u = sum[(j0/nwarps) * (mmq_y/warp_size) + i0/warp_size];+ if (!epilogue_mid) {+ if constexpr (sanitize_output) {+ if (!isfinite(u)) u = 0.0f;+ }+ dst[dst_i] = u;+ continue;+ }+ float g = epilogue_gate[dst_i];+ if (!isfinite(g)) g = 0.0f;+ if (!isfinite(u)) u = 0.0f;+ if (epilogue_clamp > 1.0e-6f) {+ if (g > epilogue_clamp) g = epilogue_clamp;+ if (u > epilogue_clamp) u = epilogue_clamp;+ if (u < -epilogue_clamp) u = -epilogue_clamp;+ }+ const float value =+ (g / (1.0f + expf(-g))) * u * epilogue_weights[dst_j];+ epilogue_mid[dst_i] = value;+ if (epilogue_mid_h) epilogue_mid_h[dst_i] = __float2half(value);+ }+ }+}++template<ggml_type type, int mmq_x, int mmq_y, bool need_check, bool sanitize_output>+static __device__ __forceinline__ void mmq_write_back_mma(+ const float * __restrict__ sum, const int * __restrict__ ids_dst, float * __restrict__ dst,+ const int stride, const int i_max, const int j_max,+ const float * __restrict__ epilogue_gate, const float * __restrict__ epilogue_weights,+ float * __restrict__ epilogue_mid, half * __restrict__ epilogue_mid_h,+ const float epilogue_clamp) {++ constexpr int granularity = mmq_get_granularity_device(mmq_x);+ constexpr int nwarps = mmq_get_nwarps_device();++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ constexpr int tileC_IJ = mmq_get_granularity_device(0);+ typedef tile<tileC_IJ, tileC_IJ, int, DATA_LAYOUT_J_MAJOR> tile_C;+ constexpr int rows_per_warp = granularity;+#else+ typedef tile<16, 8, int> tile_C;+ constexpr int rows_per_warp = 2 * granularity;+#endif // defined(AMD_MFMA_AVAILABLE)+ constexpr int ntx = rows_per_warp/tile_C::I; // Number of x minitiles per warp.+ /* ds4: see QFN_WMMA_MMQ_NCW -- must mirror the vec_dot decomposition. */+ constexpr int ncw = mmq_get_ncw_device();+ /* mmq_x is a multiple of j_stride for every width the host selector offers;+ * unreachable specializations are still instantiated, so this cannot assert. */+ constexpr int j_stride = ncw*ntx*tile_C::J;+ const int cgroup = threadIdx.y % ncw;+ const int wrow = threadIdx.y / ncw;++ const int i0 = (wrow / ntx) * (ntx*tile_C::I);+#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ static_assert(nwarps*tile_C::I == mmq_y*ncw, "nwarps*tile_C::I != mmq_y*ncw");+#else+ GGML_UNUSED(nwarps);+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++#pragma unroll+ for (int j0 = cgroup*ntx*tile_C::J; j0 < mmq_x; j0 += j_stride) {+#pragma unroll+ for (int n = 0; n < ntx; ++n) {+#pragma unroll+ for (int l = 0; l < tile_C::ne; ++l) {+ const int j = j0 + (wrow % ntx) * tile_C::J + tile_C::get_j(l);++ if (j > j_max) {+ continue;+ }++ const int i = i0 + n*tile_C::I + tile_C::get_i(l);++ if (need_check && i > i_max) {+ continue;+ }++ const int dst_j = ids_dst ? ids_dst[j] : j;+ const int dst_i = dst_j*stride + i;+ float u = sum[((j0 - cgroup*ntx*tile_C::J)/j_stride*ntx + n)*tile_C::ne + l];+ if (!epilogue_mid) {+ if constexpr (sanitize_output) {+ if (!isfinite(u)) u = 0.0f;+ }+ dst[dst_i] = u;+ continue;+ }+ float g = epilogue_gate[dst_i];+ if (!isfinite(g)) g = 0.0f;+ if (!isfinite(u)) u = 0.0f;+ if (epilogue_clamp > 1.0e-6f) {+ if (g > epilogue_clamp) g = epilogue_clamp;+ if (u > epilogue_clamp) u = epilogue_clamp;+ if (u < -epilogue_clamp) u = -epilogue_clamp;+ }+ const float value =+ (g / (1.0f + expf(-g))) * u * epilogue_weights[dst_j];+ epilogue_mid[dst_i] = value;+ if (epilogue_mid_h) epilogue_mid_h[dst_i] = __float2half(value);+ }+ }+ }+}++// -------------------------------------------------------------------------------------------------------------------------------------++template <int mmq_x, int mmq_y, bool need_check, ggml_type type>+struct mmq_type_traits;++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q1_0> {+ static constexpr int vdr = VDR_Q1_0_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q1_0<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_q8_1_mma<mmq_x, mmq_y, MMQ_Q8_1_DS_LAYOUT_D4>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q4_0> {+ static constexpr int vdr = VDR_Q4_0_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q4_0<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_q8_1_mma<mmq_x, mmq_y, MMQ_Q8_1_DS_LAYOUT_DS4>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q4_0_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q4_1> {+ static constexpr int vdr = VDR_Q4_1_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q4_1<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_1_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q4_1_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q5_0> {+ static constexpr int vdr = VDR_Q5_0_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q5_0<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_q8_1_mma<mmq_x, mmq_y, MMQ_Q8_1_DS_LAYOUT_D4>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q5_1> {+ static constexpr int vdr = VDR_Q5_1_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q5_1<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_1_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_1_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q8_0> {+ static constexpr int vdr = VDR_Q8_0_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q8_0<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_q8_1_mma<mmq_x, mmq_y, MMQ_Q8_1_DS_LAYOUT_D4>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_MXFP4> {+ static constexpr int vdr = VDR_MXFP4_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_mxfp4<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_q8_1_mma<mmq_x, mmq_y, MMQ_Q8_1_DS_LAYOUT_D4>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_NVFP4> {+ static constexpr int vdr = VDR_NVFP4_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_nvfp4<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_16_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_16_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q2_K> {+ static constexpr int vdr = VDR_Q2_K_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q2_K<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q2_K_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q2_K_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q3_K> {+ static constexpr int vdr = VDR_Q3_K_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q3_K<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_16_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q3_K_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q4_K> {+ static constexpr int vdr = VDR_Q4_K_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q4_K<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_1_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q4_K_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q5_K> {+ static constexpr int vdr = VDR_Q5_K_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q5_K<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_1_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q5_K_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_Q6_K> {+ static constexpr int vdr = VDR_Q6_K_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_q6_K<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q6_K_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q6_K_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_IQ2_XXS> {+ static constexpr int vdr = VDR_IQ2_XXS_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_iq2_xxs<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_q8_1_mma<mmq_x, mmq_y, MMQ_Q8_1_DS_LAYOUT_D4>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_IQ2_XS> {+ static constexpr int vdr = VDR_IQ2_XS_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_iq2_xs<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_16_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_16_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_IQ2_S> {+ static constexpr int vdr = VDR_IQ2_S_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_iq2_s<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_16_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_16_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_IQ3_XXS> {+ static constexpr int vdr = VDR_IQ3_XXS_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_iq3_xxs<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_q8_1_mma<mmq_x, mmq_y, MMQ_Q8_1_DS_LAYOUT_D4>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_IQ3_S> {+ static constexpr int vdr = VDR_IQ3_S_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_iq3_s<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_q8_1_mma<mmq_x, mmq_y, MMQ_Q8_1_DS_LAYOUT_D4>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_IQ1_S> {+ static constexpr int vdr = VDR_IQ1_S_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_iq1_s<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_1_q8_1_mma<mmq_x, mmq_y>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_1_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_IQ4_NL> {+ static constexpr int vdr = VDR_IQ4_NL_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_iq4_nl<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_q8_1_mma<mmq_x, mmq_y, MMQ_Q8_1_DS_LAYOUT_D4>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_q8_1_dp4a<mmq_x, mmq_y>;+};++template <int mmq_x, int mmq_y, bool need_check>+struct mmq_type_traits<mmq_x, mmq_y, need_check, GGML_TYPE_IQ4_XS> {+ static constexpr int vdr = VDR_IQ4_XS_Q8_1_MMQ;+ static constexpr load_tiles_mmq_t load_tiles = load_tiles_iq4_xs<mmq_y, need_check>;+ static constexpr vec_dot_mmq_t vec_dot_mma = vec_dot_q8_0_q8_1_mma<mmq_x, mmq_y, MMQ_Q8_1_DS_LAYOUT_D4>;+ static constexpr vec_dot_mmq_t vec_dot_dp4a = vec_dot_q8_0_q8_1_dp4a<mmq_x, mmq_y>;+};++template <+ ggml_type type,+ int mmq_x,+ bool need_check,+ bool fixup,+ bool sanitize_output,+ bool iq2_raw_stride16 = false>+static __device__ __forceinline__ void mul_mat_q_process_tile(+ const char * __restrict__ x, const int offset_x, const int * __restrict__ y,+ const int * __restrict__ ids_dst, float * __restrict__ dst, float * __restrict__ tmp_fixup,+ const int stride_row_x, const int ncols_y, const int stride_col_dst,+ const int tile_x_max_i, const int tile_y_max_j, const int kb0_start, const int kb0_stop,+ const char * __restrict__ x_soa, const int64_t soa_blocks,+ const float * __restrict__ epilogue_gate, const float * __restrict__ epilogue_weights,+ float * __restrict__ epilogue_mid, half * __restrict__ epilogue_mid_h,+ const float epilogue_clamp) {++ constexpr int warp_size = ggml_hip_get_physical_warp_size();+ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int qk = ggml_hip_type_traits<type>::qk;+ constexpr int mmq_y = get_mmq_y_device();+ constexpr load_tiles_mmq_t load_tiles = mmq_type_traits<mmq_x, mmq_y, need_check, type>::load_tiles;++ extern __shared__ int data_mul_mat_q[];+ int * tile_y = data_mul_mat_q + mmq_x;+ int * tile_x = tile_y + GGML_PAD(mmq_x*MMQ_TILE_Y_K, nwarps*warp_size);++#if defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)+ constexpr vec_dot_mmq_t vec_dot = mmq_type_traits<mmq_x, mmq_y, need_check, type>::vec_dot_mma;+ constexpr mmq_write_back_t write_back = mmq_write_back_mma<type, mmq_x, mmq_y, need_check, sanitize_output>;+#else+ constexpr vec_dot_mmq_t vec_dot = mmq_type_traits<mmq_x, mmq_y, need_check, type>::vec_dot_dp4a;+ constexpr mmq_write_back_t write_back = mmq_write_back_dp4a<mmq_x, mmq_y, need_check, sanitize_output>;+#endif // defined(AMD_MFMA_AVAILABLE) || defined(AMD_WMMA_AVAILABLE)++ constexpr int ne_block = 4 * QK8_1;++ constexpr int ITER_K = get_iter_k(type);+ constexpr int blocks_per_iter = ITER_K / qk;++ float sum[mmq_x*mmq_y / (nwarps*warp_size)] = {0.0f};++ constexpr int sz = sizeof(block_q8_1_mmq) / sizeof(int);++ for (int kb0 = kb0_start; kb0 < kb0_stop; kb0 += blocks_per_iter) {+ // ds4 (P4 Inc3): when the caller supplies an aligned-SoA weight+ // artifact, load tiles from it directly instead of the raw block+ // stream (which, for repacked MoE tensors, would have to be+ // re-derepacked into a scratch every layer switch). x_soa is+ // warp-uniform so this branch is free; tile_x contents are+ // bit-identical between the two loaders.+ if constexpr (type == GGML_TYPE_Q2_K) {+ if (x_soa != nullptr) {+ load_tiles_q2_K_soa<mmq_y, need_check>(x_soa, soa_blocks, tile_x, offset_x + kb0, tile_x_max_i, stride_row_x);+ } else {+ load_tiles(x, tile_x, offset_x + kb0, tile_x_max_i, stride_row_x);+ }+ } else if constexpr (type == GGML_TYPE_IQ2_XXS) {+ if constexpr (iq2_raw_stride16) {+ load_tiles_iq2_xxs<mmq_y, need_check, 16>(+ x, tile_x, offset_x + kb0, tile_x_max_i, stride_row_x);+ } else if (x_soa != nullptr) {+ load_tiles_iq2_xxs_soa<mmq_y, need_check>(x_soa, soa_blocks, tile_x, offset_x + kb0, tile_x_max_i, stride_row_x);+ } else {+ load_tiles(x, tile_x, offset_x + kb0, tile_x_max_i, stride_row_x);+ }+ } else {+ load_tiles(x, tile_x, offset_x + kb0, tile_x_max_i, stride_row_x);+ }+ {+ const int * by0 = y + ncols_y * (kb0 * qk / ne_block) * sz;+#pragma unroll+ for (int l0 = 0; l0 < mmq_x * MMQ_TILE_Y_K; l0 += nwarps * warp_size) {+ int l = l0 + threadIdx.y*warp_size + threadIdx.x;++ tile_y[l] = by0[l];+ }+ }++ __syncthreads();++ vec_dot(tile_x, tile_y, sum, 0);++ __syncthreads();++ {+ const int * by0 = y + ncols_y * ((kb0 * qk / ne_block) * sz + sz);+#pragma unroll+ for (int l0 = 0; l0 < mmq_x * MMQ_TILE_Y_K; l0 += nwarps * warp_size) {+ int l = l0 + threadIdx.y*warp_size + threadIdx.x;++ tile_y[l] = by0[l];+ }+ }++ __syncthreads();++ vec_dot(tile_x, tile_y, sum, MMQ_TILE_NE_K);++ __syncthreads();+ }++ if (fixup) {+ write_back(sum, nullptr, tmp_fixup + blockIdx.x*(mmq_x*mmq_y),+ mmq_y, mmq_y, mmq_x, nullptr, nullptr, nullptr, nullptr, 0.0f);+ } else {+ write_back(sum, ids_dst, dst, stride_col_dst, tile_x_max_i, tile_y_max_j,+ epilogue_gate, epilogue_weights, epilogue_mid, epilogue_mid_h,+ epilogue_clamp);+ }+}+++// The mul_mat_q kernel implements "stream-k" work partitioning as described in https://arxiv.org/abs/2301.03598++template <ggml_type type, int mmq_x, bool need_check, bool sanitize_output, bool iq2_raw_stride16 = false>+#if defined(RDNA4) || defined(RDNA3) || defined(RDNA2) || defined(CDNA) || defined(GCN)+ __launch_bounds__(ggml_hip_get_physical_warp_size()*mmq_get_nwarps_device(), 2)+#endif // defined(RDNA4) || defined(RDNA3) || defined(RDNA2) || defined(CDNA) || defined(GCN)+static __global__ void mul_mat_q(+ const char * __restrict__ x, const int * __restrict__ y, const int32_t * __restrict__ ids_dst,+ const int32_t * __restrict__ expert_bounds, float * __restrict__ dst, float * __restrict__ tmp_fixup,+ const uint3 blocks_per_ne00, const int nrows_x, const int ncols_dst, const int stride_row_x, const int ncols_y, const int stride_col_dst,+ const uint3 channel_ratio, const uint3 nchannels_y, const int stride_channel_x, const int stride_channel_y, const int stride_channel_dst,+ const uint3 sample_ratio, const uint3 nsamples_y, const int stride_sample_x, const int stride_sample_y, const int stride_sample_dst,+ const uint3 ntx, const char * __restrict__ x_soa, const int64_t soa_blocks,+ const float * __restrict__ epilogue_gate, const float * __restrict__ epilogue_weights,+ float * __restrict__ epilogue_mid, half * __restrict__ epilogue_mid_h,+ const float epilogue_clamp, const char * const * __restrict__ x_ptrs) {++ // Skip unused template specializations for faster compilation:+ if (mmq_x > get_mmq_x_max_device() || mmq_x % mmq_get_granularity_device(mmq_x) != 0) {+ NO_DEVICE_CODE;+ return;+ }++ constexpr int nwarps = mmq_get_nwarps_device();+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ constexpr int qk = ggml_hip_type_traits<type>::qk;+ constexpr int mmq_y = get_mmq_y_device();++ const uint32_t nty = (nrows_x + mmq_y - 1) / mmq_y; // Number of tiles y++ // Initialize the ids for writing back data with just the index.+ // For regular matrix multiplications this is never changed.+ // For MoE the correct indices are loaded from ids_dst.+ extern __shared__ int ids_dst_shared[]; // Stored at beginning of shared memory.+#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps*warp_size) {+ const int j = j0 + threadIdx.y*warp_size + threadIdx.x;++ if (j0 + nwarps*warp_size > mmq_x && j >= mmq_x) {+ break;+ }++ ids_dst_shared[j] = j;+ }+ __syncthreads();++ // On non-CDNA AMD or old HIP the performance with stream-k was worse, use conventional tiling instead:+#if !defined(CDNA)+ {+ const uint2 tmp2 = fast_div_modulo(blockIdx.z, nchannels_y);+ const int wt = tmp2.x;+ const int zt = tmp2.y;+ const int it = blockIdx.x;++ // Defaults for regular matrix multiplication:+ int col_low = 0;+ int col_high = ncols_dst;+ int col_diff = ncols_dst;+ int base_off_y = wt*stride_sample_y + zt*stride_channel_y;+ int base_off_dst = wt*stride_sample_dst + zt*stride_channel_dst;++ if (ids_dst) {+ col_low = expert_bounds[zt + 0];+ col_high = expert_bounds[zt + 1];+ col_diff = col_high - col_low;++ base_off_y = 0;+ base_off_dst = 0;+ }++ const int tile_x_max_i = nrows_x - it*mmq_y - 1;+ const char * x_e = x_ptrs ? x_ptrs[fastdiv(zt, channel_ratio)] : x; // kyojin: per-expert pointer table+ const int offset_x = fastdiv(wt, sample_ratio)*stride_sample_x + (x_ptrs ? 0 : fastdiv(zt, channel_ratio)*stride_channel_x) + it*mmq_y*stride_row_x;++ /*+ * ds4: grid-stride over column tiles.+ *+ * gridDim.y currently covers the tile count exactly, so this runs once+ * per block and is equivalent to the upstream single-tile form. It is+ * kept in the general form because it costs nothing: clamping gridDim.y+ * below the tile count and striding was measured and bought nothing+ * end-to-end, an immediately-exiting workgroup being essentially free on+ * gfx1151 even with a full shared-memory reservation.+ *+ * Same tiles, same per-tile reduction order, disjoint outputs.+ */+ for (int jt = blockIdx.y, iter = 0; jt*mmq_x < col_diff; jt += gridDim.y, ++iter) {+ if (ids_dst) {+ if (iter != 0) {+ // The previous tile's write-back read ids_dst_shared.+ __syncthreads();+ }+#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps*warp_size) {+ const int j = j0 + threadIdx.y*warp_size + threadIdx.x;++ if (j0 + nwarps*warp_size > mmq_x && j >= mmq_x) {+ break;+ }++ // ds4 (S1.1a): the final column tile of an expert is partial+ // when col_diff % mmq_x != 0; reading all mmq_x lanes over-reads+ // ids_dst past col_high (OOB for the last expert -- confirmed by+ // compute-sanitizer memcheck). These lanes are masked out of+ // write-back (tile_y_max_j), so clamp the read to valid columns.+ const int j_col = jt*mmq_x + j;+ ids_dst_shared[j] = j_col < col_diff ? ids_dst[col_low + j_col] : 0;+ }+ __syncthreads();+ } else if (iter != 0) {+ __syncthreads();+ }++ const int offset_y = base_off_y + (col_low + jt*mmq_x)*(sizeof(block_q8_1_mmq)/sizeof(int));+ /* MoE write-back carries the absolute column in ids_dst_shared, so+ * only the dense path advances dst by the column tile. */+ const int offset_dst = base_off_dst + it*mmq_y ++ (ids_dst ? 0 : jt*mmq_x*stride_col_dst);+ const int tile_y_max_j = col_diff - jt*mmq_x - 1;++ constexpr bool fixup = false;+ mul_mat_q_process_tile<type, mmq_x, need_check, fixup, sanitize_output, iq2_raw_stride16>+ (x_e, offset_x, y + offset_y, ids_dst_shared, dst + offset_dst, tmp_fixup, stride_row_x, ncols_y, stride_col_dst,+ tile_x_max_i, tile_y_max_j, 0, blocks_per_ne00.z, x_soa, soa_blocks,+ epilogue_gate ? epilogue_gate + offset_dst : nullptr,+ epilogue_weights,+ epilogue_mid ? epilogue_mid + offset_dst : nullptr,+ epilogue_mid_h ? epilogue_mid_h + offset_dst : nullptr,+ epilogue_clamp);+ }+ return;+ }+#endif // !defined(CDNA4) && !defined(CDNA3)++ constexpr int ITER_K = get_iter_k(type);+ constexpr int blocks_per_iter = ITER_K / qk;++ // kbc == k block continuous, current index in continuous ijk space.+ int kbc = int64_t(blockIdx.x) *(nsamples_y.z*nchannels_y.z*ntx.z*nty*blocks_per_ne00.z) / gridDim.x;+ int kbc_stop = int64_t(blockIdx.x + 1)*(nsamples_y.z*nchannels_y.z*ntx.z*nty*blocks_per_ne00.z) / gridDim.x;++ kbc -= fastmodulo(kbc, blocks_per_ne00) % blocks_per_iter;+ kbc_stop -= fastmodulo(kbc_stop, blocks_per_ne00) % blocks_per_iter;++ // kb0 == k index when doing the matrix multiplication for an output tile.+ int kb0_start = fastmodulo(kbc, blocks_per_ne00);+ int kb0_stop = min(blocks_per_ne00.z, uint32_t(kb0_start + kbc_stop - kbc));+ while (kbc < kbc_stop && kb0_stop == int(blocks_per_ne00.z)) {+ int tmp = fastdiv(kbc, blocks_per_ne00);+ uint2 tmp2 = fast_div_modulo(tmp, ntx);+ const int jt = tmp2.y;+ tmp = tmp2.x;+ tmp2 = fast_div_modulo(tmp, nchannels_y);+ const int zt = tmp2.y;+ tmp = tmp2.x;+ tmp2 = fast_div_modulo(tmp, nsamples_y);+ const int wt = tmp2.y;+ const int it = tmp2.x;++ // Defaults for regular matrix multiplication:+ int col_low = 0;+ int col_high = ncols_dst;+ int col_diff = ncols_dst;+ int offset_y = wt*stride_sample_y + zt*stride_channel_y;+ int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*mmq_x*stride_col_dst;++ if (ids_dst) {+ col_low = expert_bounds[zt + 0];+ col_high = expert_bounds[zt + 1];+ col_diff = col_high - col_low;++ offset_y = 0;+ offset_dst = 0;++ if (jt*mmq_x >= col_diff) {+ kbc += blocks_per_ne00.z;+ kbc -= fastmodulo(kbc, blocks_per_ne00);++ kb0_start = 0;+ kb0_stop = min(blocks_per_ne00.z, uint32_t(kbc_stop - kbc));++ continue;+ }++ __syncthreads();+#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps*warp_size) {+ const int j = j0 + threadIdx.y*warp_size + threadIdx.x;++ if (j0 + nwarps*warp_size > mmq_x && j >= mmq_x) {+ break;+ }++ // ds4 (S1.1a): the final column tile of an expert is partial+ // when col_diff % mmq_x != 0; reading all mmq_x lanes over-reads+ // ids_dst past col_high (OOB for the last expert -- confirmed by+ // compute-sanitizer memcheck). These lanes are masked out of+ // write-back (tile_y_max_j), so clamp the read to valid columns.+ const int j_col = jt*mmq_x + j;+ ids_dst_shared[j] = j_col < col_diff ? ids_dst[col_low + j_col] : 0;+ }+ __syncthreads();+ }++ offset_y += (col_low + jt * mmq_x) * (sizeof(block_q8_1_mmq) / sizeof(int));+ offset_dst += it*mmq_y;++ const int tile_x_max_i = nrows_x - it*mmq_y - 1;+ const int tile_y_max_j = col_diff - jt*mmq_x - 1;++ const char * x_e = x_ptrs ? x_ptrs[fastdiv(zt, channel_ratio)] : x; // kyojin: per-expert pointer table+ const int offset_x = fastdiv(wt, sample_ratio)*stride_sample_x + (x_ptrs ? 0 : fastdiv(zt, channel_ratio)*stride_channel_x) + it*mmq_y*stride_row_x;++ constexpr bool fixup = false; // All but (potentially) the last iterations write their data to dst rather than the fixup buffer.+ mul_mat_q_process_tile<type, mmq_x, need_check, fixup, sanitize_output, iq2_raw_stride16>+ (x_e, offset_x, y + offset_y, ids_dst_shared, dst + offset_dst, tmp_fixup, stride_row_x, ncols_y, stride_col_dst,+ tile_x_max_i, tile_y_max_j, kb0_start, kb0_stop, x_soa, soa_blocks,+ epilogue_gate, epilogue_weights, epilogue_mid, epilogue_mid_h,+ epilogue_clamp);++ kbc += blocks_per_ne00.z;+ kbc -= fastmodulo(kbc, blocks_per_ne00);++ kb0_start = 0;+ kb0_stop = min(blocks_per_ne00.z, uint32_t(kbc_stop - kbc));+ }++ if (kbc >= kbc_stop) {+ return;+ }++ int tmp = fastdiv(kbc, blocks_per_ne00);+ uint2 tmp2 = fast_div_modulo(tmp, ntx);+ const int jt = tmp2.y;+ tmp = tmp2.x;+ tmp2 = fast_div_modulo(tmp, nchannels_y);+ const int zt = tmp2.y;+ tmp = tmp2.x;+ tmp2 = fast_div_modulo(tmp, nsamples_y);+ const int wt = tmp2.y;+ const int it = tmp2.x;++ // Defaults for regular matrix multiplication:+ int col_low = 0;+ int col_high = ncols_dst;+ int col_diff = ncols_dst;+ int offset_y = wt*stride_sample_y + zt*stride_channel_y;+ int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*mmq_x*stride_col_dst;++ if (ids_dst) {+ col_low = expert_bounds[zt + 0];+ col_high = expert_bounds[zt + 1];+ col_diff = col_high - col_low;++ offset_y = 0;+ offset_dst = 0;++ if (jt*mmq_x >= col_diff) {+ return;+ }++ // The memory layout for the fixup buffer is always contiguous, therefore reset ids:+ __syncthreads();+#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps*warp_size) {+ const int j = j0 + threadIdx.y*warp_size + threadIdx.x;++ if (j0 + nwarps*warp_size > mmq_x && j >= mmq_x) {+ break;+ }++ ids_dst_shared[j] = j;+ }+ __syncthreads();+ }++ offset_y += (col_low + jt * mmq_x) * (sizeof(block_q8_1_mmq) / sizeof(int));+ offset_dst += it*mmq_y;++ const int tile_x_max_i = nrows_x - it*mmq_y - 1;+ const int tile_y_max_j = col_diff - jt*mmq_x - 1;++ const char * x_e = x_ptrs ? x_ptrs[fastdiv(zt, channel_ratio)] : x; // kyojin: per-expert pointer table+ const int offset_x = fastdiv(wt, sample_ratio)*stride_sample_x + (x_ptrs ? 0 : fastdiv(zt, channel_ratio)*stride_channel_x) + it*mmq_y*stride_row_x;++ constexpr bool fixup = true; // Last index writes its data to fixup buffer to avoid data races with other blocks.+ mul_mat_q_process_tile<type, mmq_x, need_check, fixup, sanitize_output, iq2_raw_stride16>+ (x_e, offset_x, y + offset_y, ids_dst_shared, dst + offset_dst, tmp_fixup, stride_row_x, ncols_y, stride_col_dst,+ tile_x_max_i, tile_y_max_j, kb0_start, kb0_stop, x_soa, soa_blocks,+ epilogue_gate, epilogue_weights, epilogue_mid, epilogue_mid_h,+ epilogue_clamp);+}++template <ggml_type type, int mmq_x, bool need_check>+__launch_bounds__(ggml_hip_get_physical_warp_size()*mmq_get_nwarps_device()/2, 1)+static __global__ void mul_mat_q_stream_k_fixup(+ const int32_t * __restrict__ ids_dst, const int32_t * __restrict__ expert_bounds, float * __restrict__ dst,+ float * __restrict__ tmp_last_tile, const uint3 blocks_per_ne00, const int nrows_x, const int ncols_dst,+ const int stride_col_dst, const uint3 nchannels_y, const int stride_channel_dst, const uint3 nsamples_y,+ const int stride_sample_dst, const uint3 ntx) {+ constexpr int mmq_y = get_mmq_y_device();+ constexpr int qk = ggml_hip_type_traits<type>::qk;+ constexpr int ITER_K = get_iter_k(type);+ constexpr int blocks_per_iter = ITER_K / qk;++ constexpr int nwarps = mmq_get_nwarps_device()/2;+ constexpr int warp_size = ggml_hip_get_physical_warp_size();++ float sum[mmq_x / nwarps] = {0.0f};+ const int i = blockIdx.y*warp_size + threadIdx.x;++ const int nty = (nrows_x + mmq_y - 1) / mmq_y;++ const int bidx0 = blockIdx.x;++ // kbc == k block continuous, current index in continuous ijk space.+ int kbc0 = int64_t(blockIdx.x) *(nsamples_y.z*nchannels_y.z*ntx.z*nty*blocks_per_ne00.z) / gridDim.x;+ int kbc0_stop = int64_t(blockIdx.x + 1)*(nsamples_y.z*nchannels_y.z*ntx.z*nty*blocks_per_ne00.z) / gridDim.x;++ kbc0 -= fastmodulo(kbc0, blocks_per_ne00) % blocks_per_iter;+ kbc0_stop -= fastmodulo(kbc0_stop, blocks_per_ne00) % blocks_per_iter;++ const bool did_not_have_any_data = kbc0 == kbc0_stop;+ const bool wrote_beginning_of_tile = fastmodulo(kbc0, blocks_per_ne00) == 0;+ const bool did_not_write_last = fastdiv(kbc0, blocks_per_ne00) == fastdiv(kbc0_stop, blocks_per_ne00) && fastmodulo(kbc0_stop, blocks_per_ne00) != 0;+ if (did_not_have_any_data || wrote_beginning_of_tile || did_not_write_last) {+ return;+ }++ bool any_fixup = false;++ // Iterate over previous blocks and sum up partial sums written to fixup buffer.+ // All HIP blocks that get here must have a previous block that needs a fixup.+ int bidx = bidx0 - 1;+ int kbc_stop = kbc0;+ while(true) {+ int kbc = int64_t(bidx)*(nsamples_y.z*nchannels_y.z*ntx.z*nty*blocks_per_ne00.z) / gridDim.x;+ kbc -= fastmodulo(kbc, blocks_per_ne00) % blocks_per_iter;++ if (kbc == kbc_stop) { // Did not have any data.+ bidx--;+ kbc_stop = kbc;+ continue;+ }++ any_fixup = true;+++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++ sum[j0/nwarps] += tmp_last_tile[bidx*(mmq_x*mmq_y) + j*mmq_y + i];+ }++ // If this block started in a previous tile we are done and don't need to combine additional partial results.+ if (fastmodulo(kbc, blocks_per_ne00) == 0 || fastdiv(kbc, blocks_per_ne00) < fastdiv(kbc0, blocks_per_ne00)) {+ break;+ }+ bidx--;+ kbc_stop = kbc;+ }++ if (!any_fixup) {+ return;+ }++ int tmp = fastdiv(kbc0, blocks_per_ne00);+ uint2 tmp2 = fast_div_modulo(tmp, ntx);+ const int jt = tmp2.y;+ tmp = tmp2.x;+ tmp2 = fast_div_modulo(tmp, nchannels_y);+ const int zt = tmp2.y;+ tmp = tmp2.x;+ tmp2 = fast_div_modulo(tmp, nsamples_y);+ const int wt = tmp2.y;+ const int it = tmp2.x;++ if (!ids_dst) {+ const int offset_dst = wt*stride_sample_dst + zt*stride_channel_dst + jt*mmq_x*stride_col_dst + it*mmq_y;+ dst += offset_dst;++ const int i_max = nrows_x - it*mmq_y - 1;+ const int j_max = ncols_dst - jt*mmq_x - 1;+ if (need_check && i > i_max) {+ return;+ }++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++ if (j > j_max) {+ return;+ }++ dst[j*stride_col_dst + i] += sum[j0/nwarps];+ }+ return;+ }++ __shared__ int ids_dst_shared[mmq_x];+ const int col_low = expert_bounds[zt + 0];+ const int col_high = expert_bounds[zt + 1];+ const int col_diff = col_high - col_low;++ for (int j = threadIdx.y*warp_size + threadIdx.x; j < mmq_x; j += nwarps*warp_size) {+ // ds4 (S1.1a): clamp the partial last-tile read to valid columns; the+ // masked-out lanes (tile_y_max_j) otherwise over-read ids_dst past+ // col_high -- OOB for the last expert (compute-sanitizer memcheck).+ const int j_col = jt*mmq_x + j;+ ids_dst_shared[j] = j_col < col_diff ? ids_dst[col_low + j_col] : 0;+ }+ __syncthreads();++ const int offset_dst = it*mmq_y;+ dst += offset_dst;++ const int i_max = nrows_x - it*mmq_y - 1;+ const int j_max = col_diff - jt*mmq_x - 1;+ if (need_check && i > i_max) {+ return;+ }++#pragma unroll+ for (int j0 = 0; j0 < mmq_x; j0 += nwarps) {+ const int j = j0 + threadIdx.y;++ if (j > j_max) {+ return;+ }++ dst[ids_dst_shared[j]*stride_col_dst + i] += sum[j0/nwarps];+ }+}++struct mmq_args {+ const char * x; ggml_type type_x; const int * y; const int32_t * ids_dst; const int32_t * expert_bounds; float * dst;+ int64_t ncols_x; int64_t nrows_x; int64_t ncols_dst; int64_t stride_row_x; int64_t ncols_y; int64_t nrows_dst;+ int64_t nchannels_x; int64_t nchannels_y; int64_t stride_channel_x; int64_t stride_channel_y; int64_t stride_channel_dst;+ int64_t nsamples_x; int64_t nsamples_y; int64_t stride_sample_x; int64_t stride_sample_y; int64_t stride_sample_dst;+ bool use_stream_k; int64_t ncols_max;+ // ds4 (P4 Inc3): optional aligned-SoA weight artifact. When x_soa is+ // non-null (Q2_K / IQ2_XXS only) the kernel loads tiles from it and x is+ // ignored; soa_blocks = pair count (Q2_K) or block count (IQ2_XXS).+ // Trailing fields so existing aggregate initializers value-init them.+ const char * x_soa; int64_t soa_blocks;+ // Optional non-stream-K MoE epilogue. The up MMQ result remains in a+ // register while gate, router weight, and weighted SwiGLU are consumed.+ const float * epilogue_gate;+ const float * epilogue_weights;+ float * epilogue_mid;+ half * epilogue_mid_h;+ float epilogue_clamp;+ // Dense Q8 can fold its non-finite contract into final MMQ write-back.+ // This is false for routed and stream-K paths.+ bool sanitize_output;+ // ds4: real upper bound on one expert's bucket, when the caller knows it.+ // ncols_max still selects mmq_x (so the tile shape and its specializations+ // stay tied to the chunk width), while this bounds only the column-tile+ // grid dimension. Zero means "unknown", i.e. fall back to ncols_max.+ int64_t ncols_grid_max;+ // ds4: column-tile width the caller wants, when it knows the whole bucket+ // distribution and not just its maximum. The default rule picks the widest+ // tile that minimizes the tile count for `ncols_max`, which is right for a+ // dense column range but wrong for a routed one: a 512-token chunk spreads+ // 3,072 (row, expert) pairs over 256 buckets, so the mean bucket is twelve+ // rows and an 80-column tile spends most of its matrix-core issue on+ // padding. Zero keeps the default rule. See qfn_mmq_set_routed_tile_cols.+ int mmq_x_request;+ // kyojin: optional per-expert weight pointer table (device array of nchannels_x pointers). When+ // non-null, expert z reads x_ptrs[z] and stride_channel_x is ignored; x is ignored.+ const char * const * x_ptrs;+};++template<ggml_type type>+static size_t mmq_get_nbytes_shared(+ const int mmq_x, const int mmq_y, const int cc,+ const int warp_size, const int nwarps) {+ const tile_x_sizes txs = mmq_get_dp4a_tile_x_sizes(type, mmq_y);+ const int mmq_tile_x_k = mmq_get_mma_tile_x_k(type);+ const size_t nbs_ids = mmq_x*sizeof(int);+ const size_t nbs_x = (amd_mfma_available(cc) || amd_wmma_available(cc))+ ? mmq_y*mmq_tile_x_k*sizeof(int)+ : txs.qs*sizeof(int) + txs.dm*sizeof(half2) + txs.sc*sizeof(int);+ const size_t nbs_y = mmq_x * (sizeof(block_q8_1_mmq));+ /* This kernel is not residency-starved, which is why nothing that trades+ * shared memory for waves has ever paid here. Padding this figure to lower+ * workgroups per CU -- the one measurement that isolates residency, since+ * the grid, tile shape and per-wave work stay identical -- gave 4 wgs/WGP+ * 453.6, 3 wgs 445.0-454.1 and 2 wgs 418.2 tok/s: halving residency costs+ * 7.8% and 4->3 is free. See docs/internal/models/deepseek-v4-flash/BENCHMARKS.md. */+ return nbs_ids + nbs_x + GGML_PAD(nbs_y, nwarps*warp_size*sizeof(int));+}++template <ggml_type type, int mmq_x>+static void launch_mul_mat_q(ggml_backend_hip_context & ctx, const mmq_args & args, hipStream_t stream) {+ const int id = ggml_hip_get_device();+ const int cc = ggml_hip_info().devices[id].cc;+ const int nsm = ggml_hip_info().devices[id].nsm;+ const int warp_size = ggml_hip_info().devices[id].warp_size;+ const int nwarps = mmq_get_nwarps_host(cc, warp_size);+ const int mmq_y = get_mmq_y_host(cc);++ const dim3 block_dims(warp_size, nwarps, 1);++ const int nbytes_shared = mmq_get_nbytes_shared<type>(+ mmq_x, mmq_y, cc, warp_size, nwarps);++ HIP_SET_SHARED_MEMORY_LIMIT((mul_mat_q<type, mmq_x, false, false>), nbytes_shared);+ HIP_SET_SHARED_MEMORY_LIMIT((mul_mat_q<type, mmq_x, true, false>), nbytes_shared);+ if constexpr (type == GGML_TYPE_Q8_0) {+ HIP_SET_SHARED_MEMORY_LIMIT((mul_mat_q<type, mmq_x, false, true>), nbytes_shared);+ HIP_SET_SHARED_MEMORY_LIMIT((mul_mat_q<type, mmq_x, true, true>), nbytes_shared);+ }++ const int nty = (args.nrows_x + mmq_y - 1) / mmq_y;+ /* ds4: see mmq_args::ncols_grid_max -- the routed path knows the largest+ * expert bucket, and every column tile past it exits immediately while+ * still reserving the full shared-memory tile. */+ const int64_t ncols_grid =+ args.ncols_grid_max > 0 && args.ncols_grid_max < args.ncols_max+ ? args.ncols_grid_max+ : args.ncols_max;+ const int ntx = (ncols_grid + mmq_x - 1) / mmq_x;+ const int ntzw = args.nchannels_y * args.nsamples_y;+ const dim3 block_nums_xy_tiling(nty, ntx, ntzw);++ GGML_ASSERT(args.nchannels_y % args.nchannels_x == 0);+ GGML_ASSERT(args.nsamples_y % args.nsamples_x == 0);+ const int channel_ratio = args.nchannels_y / args.nchannels_x;+ const int sample_ratio = args.nsamples_y / args.nsamples_x;++ const uint3 blocks_per_ne00_fd = init_fastdiv_values(args.ncols_x / ggml_hip_type_traits<type>::qk);+ const uint3 ntx_fd = init_fastdiv_values(ntx);+ const uint3 nchannels_y_fd = init_fastdiv_values(args.nchannels_y);+ const uint3 nsamples_y_fd = init_fastdiv_values(args.nsamples_y);+ const uint3 channel_ratio_fd = init_fastdiv_values(channel_ratio);+ const uint3 sample_ratio_fd = init_fastdiv_values(sample_ratio);++ GGML_ASSERT(!args.epilogue_mid || !args.use_stream_k);++ if (!args.use_stream_k) {+ // The gfx1151 routed DeepSeek gate/up shape uses raw IQ2 weights with+ // 16 blocks per row. Keep one specialization instead of duplicating+ // the complete raw/SoA MMQ family.+ //+ // ds4: the column width is orthogonal to the raw-stride handling, and+ // the tile fill this shape achieves depends on it -- the mean expert+ // bucket is n_tokens * n_expert_used / n_experts, so a width that+ // divides it leaves far less of each tile empty. Keep every width the+ // selector can reach on this path rather than only 80.+ if constexpr (type == GGML_TYPE_IQ2_XXS) {+ if (cc == GGML_HIP_CC_OFFSET_AMD + 0x1151 &&+ args.ids_dst != nullptr && !args.sanitize_output &&+ args.x_soa == nullptr && args.stride_row_x == 16 &&+ args.nrows_x % mmq_y == 0) {+ constexpr bool need_check = false;+ constexpr bool sanitize_output = false;+ constexpr bool iq2_raw_stride16 = true;+ mul_mat_q<type, mmq_x, need_check, sanitize_output, iq2_raw_stride16>+ <<<block_nums_xy_tiling, block_dims, nbytes_shared, stream>>>(+ args.x, args.y, args.ids_dst, args.expert_bounds, args.dst, nullptr,+ blocks_per_ne00_fd, args.nrows_x, args.ncols_dst, args.stride_row_x,+ args.ncols_y, args.nrows_dst, channel_ratio_fd, nchannels_y_fd,+ args.stride_channel_x, args.stride_channel_y, args.stride_channel_dst,+ sample_ratio_fd, nsamples_y_fd, args.stride_sample_x,+ args.stride_sample_y, args.stride_sample_dst, ntx_fd,+ args.x_soa, args.soa_blocks, args.epilogue_gate,+ args.epilogue_weights, args.epilogue_mid, args.epilogue_mid_h,+ args.epilogue_clamp, args.x_ptrs);+ return;+ }+ }+ const auto launch_tiled = [&](auto sanitize_tag) {+ constexpr bool sanitize_output = decltype(sanitize_tag)::value;+ if (args.nrows_x % mmq_y == 0) {+ constexpr bool need_check = false;+ mul_mat_q<type, mmq_x, need_check, sanitize_output>+ <<<block_nums_xy_tiling, block_dims, nbytes_shared, stream>>>(+ args.x, args.y, args.ids_dst, args.expert_bounds, args.dst, nullptr,+ blocks_per_ne00_fd, args.nrows_x, args.ncols_dst, args.stride_row_x,+ args.ncols_y, args.nrows_dst, channel_ratio_fd, nchannels_y_fd,+ args.stride_channel_x, args.stride_channel_y, args.stride_channel_dst,+ sample_ratio_fd, nsamples_y_fd, args.stride_sample_x,+ args.stride_sample_y, args.stride_sample_dst, ntx_fd,+ args.x_soa, args.soa_blocks, args.epilogue_gate,+ args.epilogue_weights, args.epilogue_mid, args.epilogue_mid_h,+ args.epilogue_clamp, args.x_ptrs);+ } else {+ constexpr bool need_check = true;+ mul_mat_q<type, mmq_x, need_check, sanitize_output>+ <<<block_nums_xy_tiling, block_dims, nbytes_shared, stream>>>(+ args.x, args.y, args.ids_dst, args.expert_bounds, args.dst, nullptr,+ blocks_per_ne00_fd, args.nrows_x, args.ncols_dst, args.stride_row_x,+ args.ncols_y, args.nrows_dst, channel_ratio_fd, nchannels_y_fd,+ args.stride_channel_x, args.stride_channel_y, args.stride_channel_dst,+ sample_ratio_fd, nsamples_y_fd, args.stride_sample_x,+ args.stride_sample_y, args.stride_sample_dst, ntx_fd,+ args.x_soa, args.soa_blocks, args.epilogue_gate,+ args.epilogue_weights, args.epilogue_mid, args.epilogue_mid_h,+ args.epilogue_clamp, args.x_ptrs);+ }+ };+ if constexpr (type == GGML_TYPE_Q8_0) {+ if (args.sanitize_output) {+ launch_tiled(std::true_type{});+ } else {+ launch_tiled(std::false_type{});+ }+ } else {+ GGML_ASSERT(!args.sanitize_output);+ launch_tiled(std::false_type{});+ }+ return;+ }++ GGML_ASSERT(!args.sanitize_output);+ // For the stream-k kernel it is possible to run it with tiling by setting the number of HIP blocks equal to the number of tiles.+ // This is worthwhile if the efficiency of tiling is high and skipping the fixup kernel is more important.+ const int ntiles_dst = ntx * nty * ntzw;+ const int tiles_nwaves = (ntiles_dst + nsm - 1) / nsm;+ const int tiles_efficiency_percent = 100 * ntiles_dst / (nsm*tiles_nwaves);+ const dim3 block_nums_stream_k(nsm, 1, 1);++ GGML_ASSERT(ntiles_dst * blocks_per_ne00_fd.z < (1 << 30)); // Assert that variable kbc will not overflow.++ const bool fixup_needed = ntiles_dst % block_nums_stream_k.x != 0;++ ggml_hip_pool & pool = qfn_pool_for_stream(id, stream);+ ggml_hip_pool_alloc<float> tmp_fixup(pool);+ if (fixup_needed) {+ tmp_fixup.alloc(block_nums_stream_k.x * mmq_x*mmq_y);+ HIP_CHECK(hipMemsetAsync(tmp_fixup.ptr, 0,+ (size_t)block_nums_stream_k.x * (size_t)mmq_x * (size_t)mmq_y * sizeof(float),+ stream));+ }++ const dim3 block_nums_fixup(block_nums_stream_k.x, mmq_y/warp_size, 1);+ const dim3 block_dims_fixup(block_dims.x, block_dims.y/2, block_dims.z);++ if (args.nrows_x % mmq_y == 0) {+ constexpr bool need_check = false;+ mul_mat_q<type, mmq_x, need_check, false><<<block_nums_stream_k, block_dims, nbytes_shared, stream>>>+ (args.x, args.y, args.ids_dst, args.expert_bounds, args.dst, tmp_fixup.ptr,+ blocks_per_ne00_fd, args.nrows_x, args.ncols_dst, args.stride_row_x, args.ncols_y, args.nrows_dst,+ channel_ratio_fd, nchannels_y_fd, args.stride_channel_x, args.stride_channel_y, args.stride_channel_dst,+ sample_ratio_fd, nsamples_y_fd, args.stride_sample_x, args.stride_sample_y, args.stride_sample_dst,+ ntx_fd, args.x_soa, args.soa_blocks,+ args.epilogue_gate, args.epilogue_weights, args.epilogue_mid,+ args.epilogue_mid_h, args.epilogue_clamp, args.x_ptrs);++ if (!fixup_needed) {+ return;+ }++ HIP_CHECK(hipGetLastError());+ mul_mat_q_stream_k_fixup<type, mmq_x, need_check><<<block_nums_fixup, block_dims_fixup, 0, stream>>>+ (args.ids_dst, args.expert_bounds, args.dst, tmp_fixup.ptr, blocks_per_ne00_fd, args.nrows_x, args.ncols_dst,+ args.nrows_dst, nchannels_y_fd, args.stride_channel_dst, nsamples_y_fd, args.stride_sample_dst,+ ntx_fd);+ } else {+ constexpr bool need_check = true;+ mul_mat_q<type, mmq_x, need_check, false><<<block_nums_stream_k, block_dims, nbytes_shared, stream>>>+ (args.x, args.y, args.ids_dst, args.expert_bounds, args.dst, tmp_fixup.ptr,+ blocks_per_ne00_fd, args.nrows_x, args.ncols_dst, args.stride_row_x, args.ncols_y, args.nrows_dst,+ channel_ratio_fd, nchannels_y_fd, args.stride_channel_x, args.stride_channel_y, args.stride_channel_dst,+ sample_ratio_fd, nsamples_y_fd, args.stride_sample_x, args.stride_sample_y, args.stride_sample_dst,+ ntx_fd, args.x_soa, args.soa_blocks,+ args.epilogue_gate, args.epilogue_weights, args.epilogue_mid,+ args.epilogue_mid_h, args.epilogue_clamp, args.x_ptrs);++ if (!fixup_needed) {+ return;+ }++ HIP_CHECK(hipGetLastError());+ mul_mat_q_stream_k_fixup<type, mmq_x, need_check><<<block_nums_fixup, block_dims_fixup, 0, stream>>>+ (args.ids_dst, args.expert_bounds, args.dst, tmp_fixup.ptr, blocks_per_ne00_fd, args.nrows_x, args.ncols_dst,+ args.nrows_dst, nchannels_y_fd, args.stride_channel_dst, nsamples_y_fd, args.stride_sample_dst,+ ntx_fd);+ }+}++template <ggml_type type>+void mul_mat_q_case(ggml_backend_hip_context & ctx, const mmq_args & args, hipStream_t stream) {+ const int id = ggml_hip_get_device();+ const int cc = ggml_hip_info().devices[id].cc;+ const size_t smpbo = ggml_hip_info().devices[id].smpbo;+ const int warp_size = ggml_hip_info().devices[id].warp_size;+ const int nwarps = mmq_get_nwarps_host(cc, warp_size);++ const int mmq_x_max = get_mmq_x_max_host(cc);+ const int mmq_y = get_mmq_y_host(cc);++ int mmq_x_best = 0;+ int ntiles_x_best = INT_MAX;++ /* ds4: with a column split a warp owns every ncw-th column tile, so the+ * width has to divide ncw * granularity. See QFN_WMMA_MMQ_NCW. */+ const int ncw = (amd_wmma_available(cc) && !amd_mfma_available(cc))+ ? QFN_WMMA_MMQ_NCW : 1;+ const auto mmq_x_usable = [&](const int mmq_x) {+ const int granularity = mmq_get_granularity_host(mmq_x, cc);+ return mmq_x > 0 && mmq_x <= mmq_x_max &&+ mmq_x % (granularity * ncw) == 0 &&+ mmq_get_nbytes_shared<type>(mmq_x, mmq_y, cc, warp_size,+ nwarps) <= smpbo;+ };++ /* ds4: a caller that knows the whole routed bucket distribution asks for a+ * width directly; see mmq_args::mmq_x_request. */+ const bool requested =+ args.mmq_x_request > 0 && mmq_x_usable(args.mmq_x_request);+ if (requested) {+ mmq_x_best = args.mmq_x_request;+ }++ for (int mmq_x = 8; !requested && mmq_x <= mmq_x_max && ntiles_x_best > 1;+ mmq_x += 8) {+ if (!mmq_x_usable(mmq_x)) {+ continue;+ }++ const int ntiles_x = (args.ncols_max + mmq_x - 1) / mmq_x;++ if (ntiles_x < ntiles_x_best) {+ mmq_x_best = mmq_x;+ ntiles_x_best = ntiles_x;+ }+ }++ switch (mmq_x_best) {+ case 8:+ launch_mul_mat_q<type, 8>(ctx, args, stream);+ break;+ case 16:+ launch_mul_mat_q<type, 16>(ctx, args, stream);+ break;+ case 24:+ launch_mul_mat_q<type, 24>(ctx, args, stream);+ break;+ case 32:+ launch_mul_mat_q<type, 32>(ctx, args, stream);+ break;+ case 40:+ launch_mul_mat_q<type, 40>(ctx, args, stream);+ break;+ case 48:+ launch_mul_mat_q<type, 48>(ctx, args, stream);+ break;+ case 56:+ launch_mul_mat_q<type, 56>(ctx, args, stream);+ break;+ case 64:+ launch_mul_mat_q<type, 64>(ctx, args, stream);+ break;+ case 72:+ launch_mul_mat_q<type, 72>(ctx, args, stream);+ break;+ case 80:+ launch_mul_mat_q<type, 80>(ctx, args, stream);+ break;+ case 88:+ launch_mul_mat_q<type, 88>(ctx, args, stream);+ break;+ case 96:+ launch_mul_mat_q<type, 96>(ctx, args, stream);+ break;+ case 104:+ launch_mul_mat_q<type, 104>(ctx, args, stream);+ break;+ case 112:+ launch_mul_mat_q<type, 112>(ctx, args, stream);+ break;+ case 120:+ launch_mul_mat_q<type, 120>(ctx, args, stream);+ break;+ case 128:+ launch_mul_mat_q<type, 128>(ctx, args, stream);+ break;+ default:+ fprintf(stderr, "mmq_x_best=%d\n", mmq_x_best);+ GGML_ABORT("fatal error");+ break;+ }+}++#define DECL_MMQ_CASE(type) \+ template void mul_mat_q_case<type>(ggml_backend_hip_context & ctx, const mmq_args & args, hipStream_t stream) \+++// -------------------------------------------------------------------------------------------------------------------------++void ggml_hip_mul_mat_q(+ ggml_backend_hip_context & ctx, const ggml_tensor * src0, const ggml_tensor * src1, const ggml_tensor * ids, ggml_tensor * dst);++void ggml_hip_op_mul_mat_q(+ ggml_backend_hip_context & ctx,+ const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,+ const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,+ const int64_t src1_padded_row_size, hipStream_t stream);++bool ggml_hip_should_use_mmq(enum ggml_type type, int cc, int64_t ne11, int64_t n_experts);diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/qfn_ggml_stubs.cu b/exllamav3/exllamav3_ext/rocm/gmmq/qfn_ggml_stubs.cunew file mode 100644index 0000000..1bcc315--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/qfn_ggml_stubs.cu@@ -0,0 +1,257 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/qfn_ggml_stubs.hip.cpp (renamed .cu), itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: gufo::hip::AllocateDevice -> hipMalloc (no gufo managed_alloc dependency).+#include "qfn_mmq_prelude.h"+namespace qfn_mmq {+// SPDX-License-Identifier: MIT+// Implementations of the ggml-API stubs declared in qfn_ggml_stubs.h plus+// the bodies of ggml_backend_hip_context / ggml_hip_info /+// new_pool_for_device that the vendored common.hpp declares without+// defining.+//++#include "common.hpp" // pulls in qfn_ggml_stubs.h via redirect headers++#include <hip/hip_runtime.h>+#include <hipblas/hipblas.h>++#include <chrono>+#include <cstdio>+#include <cstdlib>+#include <memory>+#include <map>+#include <mutex>+#include <vector>++// ----------------------------------------------------------------------------+// Device info singleton.+//+// Common.hpp declares `const ggml_hip_device_info & ggml_hip_info();` -+// we provide the body. The struct layout (common.hpp:1091) is:+// { int device_count; hip_device_info devices[GGML_HIP_MAX_DEVICES];+// std::array<float, GGML_HIP_MAX_DEVICES> default_tensor_split; }+// where hip_device_info has { cc, nsm, smpb, smpbo, integrated, vmm,+// vmm_granularity, total_vram, warp_size, supports_cooperative_launch }.+// ----------------------------------------------------------------------------++const ggml_hip_device_info & ggml_hip_info() {+ static ggml_hip_device_info info;+ static std::once_flag once;+ std::call_once(once, []{+ int count = 0;+ hipError_t err = hipGetDeviceCount(&count);+ if (err != hipSuccess) {+ fprintf(stderr, "ggml_hip_info: hipGetDeviceCount failed: %s\n", hipGetErrorString(err));+ count = 0;+ }+ if (count > GGML_HIP_MAX_DEVICES) count = GGML_HIP_MAX_DEVICES;+ info.device_count = count;+ for (int i = 0; i < count; i++) {+ hipDeviceProp_t p;+ HIP_CHECK(hipGetDeviceProperties(&p, i));+ unsigned arch = 0;+ if (sscanf(p.gcnArchName, "gfx%x", &arch) != 1) {+ fprintf(stderr, "ggml_hip_info: unsupported HIP architecture '%s'\n", p.gcnArchName);+ }+ info.devices[i].cc = GGML_HIP_CC_OFFSET_AMD + (int)arch;+ info.devices[i].nsm = p.multiProcessorCount;+ info.devices[i].smpb = p.sharedMemPerBlock;+ info.devices[i].smpbo = p.sharedMemPerBlockOptin;+ info.devices[i].integrated = p.integrated != 0;+ info.devices[i].vmm = false;+ info.devices[i].vmm_granularity = 0;+ info.devices[i].total_vram = p.totalGlobalMem;+ info.devices[i].warp_size = p.warpSize;+ info.devices[i].supports_cooperative_launch = p.cooperativeLaunch != 0;+ }+ });+ return info;+}++// ggml_hip_get_device / ggml_hip_set_device are declared (not defined) in+// common.hpp. We provide thin wrappers.++int ggml_hip_get_device() {+ int dev = 0;+ hipGetDevice(&dev);+ return dev;+}++void ggml_hip_set_device(int device) {+ int cur = -1;+ hipGetDevice(&cur);+ if (cur != device) {+ HIP_CHECK(hipSetDevice(device));+ }+}++int64_t ggml_time_us() {+ using clock = std::chrono::steady_clock;+ static const auto t0 = clock::now();+ return std::chrono::duration_cast<std::chrono::microseconds>(clock::now() - t0).count();+}++// ----------------------------------------------------------------------------+// ggml_hip_error: invoked by the HIP_CHECK / HIPBLAS_CHECK macros defined+// in common.hpp on the error path. Marked [[noreturn]] in the declaration+// (common.hpp:155) - abort() satisfies that contract.+// ----------------------------------------------------------------------------++[[noreturn]] void ggml_hip_error(const char * stmt, const char * func, const char * file, int line, const char * msg) {+ fprintf(stderr, "HIP error: %s\n call: %s\n in: %s at %s:%d\n", msg, stmt, func, file, line);+ fflush(stderr);+ abort();+}++// ----------------------------------------------------------------------------+// Scratch pool over one device arena.+// ----------------------------------------------------------------------------++namespace {++/* qwen38: every allocation the tier makes is scoped to one entry point and+ * released in reverse order, and each arena serves one stream (qfn_pool_for_stream),+ * so stream order makes a stack over device memory enough. It replaces+ * hipMallocAsync/hipFreeAsync, whose reuse of a just-freed block raced with+ * in-flight kernels on this ROCm and made prefill logits nondeterministic.+ *+ * The stack spans a list of blocks. An allocation that doesn't fit the current+ * block goes to the next one that fits (added if needed, at least twice the+ * last), so an arena can start small: the MTP and DFlash2 streams only quantize+ * a few rows. Blocks live as long as the pool: captured graphs bake in their+ * addresses and replay them after the arena has grown. */+struct qfn_stack_pool : public ggml_hip_pool {+ static constexpr size_t kFirstBlockBytes = (size_t) 16 << 20;++ struct block {+ char *base;+ size_t capacity;+ size_t top;+ };++ struct live_alloc {+ char *ptr;+ size_t bytes;+ size_t block;+ };++ int device;+ std::vector<block> blocks;+ size_t current = 0;+ std::vector<live_alloc> live;++ explicit qfn_stack_pool(int device) : device(device) {}++ ~qfn_stack_pool() override {+ for (const block &b : blocks) (void)hipFree(b.base);+ }++ void add_block(size_t bytes) {+ /* Drain the device first, as the single arena did. On ROCm 10.1 an+ * allocation made while kernels are in flight leaves later kernel+ * launches slower for seconds (+14 ms per short MTP request). Blocks+ * are only added by eager passes, never during capture. */+ (void)hipDeviceSynchronize();+ char *base = nullptr;+ HIP_CHECK(hipMalloc((void **) &base, bytes)); // kyojin: was gufo::hip::AllocateDevice+ blocks.push_back({base, bytes, 0});+ }++ void * alloc(size_t size, size_t * actual_size) override {+ ggml_hip_set_device(device);+ const size_t aligned = (size + 255) & ~(size_t) 255;+ if (blocks.empty()) {+ size_t first = kFirstBlockBytes;+ while (first < aligned) first *= 2;+ add_block(first);+ }+ if (blocks[current].top + aligned > blocks[current].capacity) {+ // Blocks after `current` are empty: the stack only reaches them through it.+ size_t next = current + 1;+ while (next < blocks.size() && blocks[next].capacity < aligned) ++next;+ if (next == blocks.size()) {+ size_t bytes = blocks.back().capacity * 2;+ while (bytes < aligned) bytes *= 2;+ add_block(bytes);+ }+ current = next;+ }+ block &b = blocks[current];+ char *ptr = b.base + b.top;+ b.top += aligned;+ live.push_back({ptr, aligned, current});+ if (actual_size) *actual_size = aligned;+ return ptr;+ }++ void free(void * ptr, size_t /*size*/) override {+ if (!ptr) return;+ if (live.empty() || live.back().ptr != ptr) {+ fprintf(stderr, "qfn_stack_pool: non-LIFO free\n");+ abort();+ }+ const live_alloc a = live.back();+ live.pop_back();+ blocks[a.block].top -= a.bytes;+ while (current > 0 && blocks[current].top == 0) --current;+ }+};++} // anonymous namespace++std::unique_ptr<ggml_hip_pool> ggml_backend_hip_context::new_pool_for_device(int device, int /*stream_no*/) {+ return std::unique_ptr<ggml_hip_pool>(new qfn_stack_pool(device));+}++// One scratch arena per (device, stream). The stack pool above relies on stream order to+// reuse memory safely, so it must never be shared by two streams: the MTP drafter runs on its+// own stream and reached these kernels too, and reused the target's scratch while the target's+// kernels were still reading it.+namespace {+std::mutex g_stream_pools_mutex;+std::map<std::pair<int, hipStream_t>, std::unique_ptr<ggml_hip_pool>> g_stream_pools;+} // anonymous namespace++ggml_hip_pool & qfn_pool_for_stream(int device, hipStream_t stream) {+ const std::lock_guard<std::mutex> lock(g_stream_pools_mutex);+ auto & pool = g_stream_pools[{device, stream}];+ if (!pool) pool = ggml_backend_hip_context::new_pool_for_device(device, 0);+ return *pool;+}++// The caller must have stopped every host submission and allocation on `stream` first: the+// lock protects the map, not the pool references handed out by qfn_pool_for_stream.+extern "C" void qfn_mmq_release_stream(hipStream_t stream) {+ const std::lock_guard<std::mutex> lock(g_stream_pools_mutex);+ for (auto it = g_stream_pools.begin(); it != g_stream_pools.end();) {+ if (it->first.second != stream) {+ ++it;+ continue;+ }+ // The arena may still back queued work on this stream. If the wait fails, the work+ // may still be running, so keep (leak) the arena rather than free memory in use.+ const hipError_t status = hipStreamSynchronize(stream);+ if (status != hipSuccess) {+ fprintf(stderr, "qfn_mmq_release_stream: stream %p not synchronized (%s); keeping its scratch arena\n",+ (void *) stream, hipGetErrorString(status));+ (void) it->second.release();+ }+ it = g_stream_pools.erase(it);+ }+}++ggml_backend_hip_context::~ggml_backend_hip_context() {+ if (copy_event) {+ hipEventDestroy(copy_event);+ copy_event = nullptr;+ }+ // streams[][], hipblas_handles[], and pools[][] are owned-by-value+ // (hipStream_t and hipblasHandle_t are opaque handles - destroying the+ // context "should" tear them down, but in our shim ds4 manages streams+ // externally and we leave them alone. The pools auto-destruct via+ // unique_ptr.).+}++} // namespace qfn_mmqdiff --git a/exllamav3/exllamav3_ext/rocm/gmmq/qfn_ggml_stubs.h b/exllamav3/exllamav3_ext/rocm/gmmq/qfn_ggml_stubs.hnew file mode 100644index 0000000..025bd48--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/qfn_ggml_stubs.h@@ -0,0 +1,306 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/qfn_ggml_stubs.h, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+// qfn_ggml_stubs.h - minimal ggml-API stubs for Flash-Next's vendored mmq kernels.+//+// The mmq.hpp / mma.hpp / vecdotq.hpp / quantize.hpp / mmid.hpp / common.hpp+// files in this directory are vendored verbatim from llama.cpp's ggml-hip+// backend (MIT, copyright 2023-2026 The ggml authors). They transitively+// #include "ggml.h", "ggml-impl.h", "ggml-hip.h" - in Flash-Next those names+// resolve to thin redirect headers in this directory which all #include this+// stubs file.+//+// This file declares the minimum surface of the ggml API that the vendored+// HIP code references, EXCLUDING what's already provided by common.hpp+// itself (compute-capability constants, MMA flags, ggml_hip_device_info,+// ggml_hip_pool, ggml_hip_pool_alloc, ggml_backend_hip_context, the+// HIP_CHECK / HIPBLAS_CHECK macros, ggml_hip_get_device, ggml_hip_set_device,+// ggml_hip_info). Those names live in common.hpp and we let it own them.+//+// Things this header DOES provide:+// * GGML_ASSERT / GGML_ABORT / GGML_UNUSED / GGML_UNUSED_VARS / GGML_PAD+// * GGML_MAX_DIMS / GGML_MAX_SRC / GGML_HIP_NAME / GGML_HIP_MAX_DEVICES /+// GGML_HIP_MAX_STREAMS / GGML_LOG_DEBUG+// * enum ggml_type (all 21 mmq type codes - we only USE a subset for V4+// Flash but the switch in mmq.cu's downstream replacement must compile)+// * enum ggml_glu_op (just for the unused mm_fusion_args fields)+// * struct ggml_tensor (complete enough for common.hpp's+// ggml_hip_concurrent_event::is_valid() to compile - we never call it)+// * int64_t ggml_nbytes(const ggml_tensor *) (stub - never called)+// * int64_t ggml_time_us() (used by USE_HIP_GRAPH paths we disable)+// * inline ggml_type_size() / ggml_blck_size() lookups+//+// Things ggml-common.h (vendored) owns:+// * ggml_half / ggml_half2 typedefs+// * GGML_EXTENSION macro+// * block_q*, block_iq* struct definitions++#pragma once++#include <cassert>+#include <cstdarg>+#include <cstddef>+#include <cstdint>+#include <cstdio>+#include <cstdlib>++// ----------------------------------------------------------------------------+// Macros+// ----------------------------------------------------------------------------++#ifndef GGML_ASSERT+#define GGML_ASSERT(cond) \+ do { if (!(cond)) { \+ fprintf(stderr, "GGML_ASSERT(%s) failed at %s:%d\n", #cond, __FILE__, __LINE__); \+ abort(); \+ } } while (0)+#endif++#ifndef GGML_ABORT+#define GGML_ABORT(fmt, ...) \+ do { \+ fprintf(stderr, "GGML_ABORT: " fmt " at %s:%d\n", ##__VA_ARGS__, __FILE__, __LINE__); \+ abort(); \+ } while (0)+#endif++#ifndef GGML_UNUSED+#define GGML_UNUSED(x) ((void)(x))+#endif++// Variadic GGML_UNUSED_VARS: drop up to 12 unused names without warnings.+#ifndef GGML_UNUSED_VARS+#define GGML_UNUSED_VARS_1(_1) GGML_UNUSED(_1)+#define GGML_UNUSED_VARS_2(_1,_2) GGML_UNUSED(_1); GGML_UNUSED(_2)+#define GGML_UNUSED_VARS_3(_1,_2,_3) GGML_UNUSED_VARS_2(_1,_2); GGML_UNUSED(_3)+#define GGML_UNUSED_VARS_4(_1,_2,_3,_4) GGML_UNUSED_VARS_3(_1,_2,_3); GGML_UNUSED(_4)+#define GGML_UNUSED_VARS_5(_1,_2,_3,_4,_5) GGML_UNUSED_VARS_4(_1,_2,_3,_4); GGML_UNUSED(_5)+#define GGML_UNUSED_VARS_6(_1,_2,_3,_4,_5,_6) GGML_UNUSED_VARS_5(_1,_2,_3,_4,_5); GGML_UNUSED(_6)+#define GGML_UNUSED_VARS_7(_1,_2,_3,_4,_5,_6,_7) GGML_UNUSED_VARS_6(_1,_2,_3,_4,_5,_6); GGML_UNUSED(_7)+#define GGML_UNUSED_VARS_8(_1,_2,_3,_4,_5,_6,_7,_8) GGML_UNUSED_VARS_7(_1,_2,_3,_4,_5,_6,_7); GGML_UNUSED(_8)+#define GGML_UNUSED_VARS_9(_1,_2,_3,_4,_5,_6,_7,_8,_9) GGML_UNUSED_VARS_8(_1,_2,_3,_4,_5,_6,_7,_8); GGML_UNUSED(_9)+#define GGML_UNUSED_VARS_10(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10) GGML_UNUSED_VARS_9(_1,_2,_3,_4,_5,_6,_7,_8,_9); GGML_UNUSED(_10)+#define GGML_UNUSED_VARS_11(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11) GGML_UNUSED_VARS_10(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10); GGML_UNUSED(_11)+#define GGML_UNUSED_VARS_12(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12) GGML_UNUSED_VARS_11(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11); GGML_UNUSED(_12)+#define GGML_UNUSED_VARS_PICK(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,NAME,...) NAME+#define GGML_UNUSED_VARS(...) \+ GGML_UNUSED_VARS_PICK(__VA_ARGS__, \+ GGML_UNUSED_VARS_12, GGML_UNUSED_VARS_11, GGML_UNUSED_VARS_10, \+ GGML_UNUSED_VARS_9, GGML_UNUSED_VARS_8, GGML_UNUSED_VARS_7, \+ GGML_UNUSED_VARS_6, GGML_UNUSED_VARS_5, GGML_UNUSED_VARS_4, \+ GGML_UNUSED_VARS_3, GGML_UNUSED_VARS_2, GGML_UNUSED_VARS_1)(__VA_ARGS__)+#endif++#ifndef GGML_PAD+#define GGML_PAD(x, n) (((x) + (n) - 1) / (n) * (n))+#endif++#ifndef GGML_MAX_DIMS+#define GGML_MAX_DIMS 4+#endif++#ifndef GGML_MAX_SRC+#define GGML_MAX_SRC 10+#endif++#ifndef GGML_HIP_NAME+#define GGML_HIP_NAME "QFN_HIP"+#endif++#ifndef GGML_HIP_MAX_DEVICES+#define GGML_HIP_MAX_DEVICES 16+#endif++#ifndef GGML_HIP_MAX_STREAMS+#define GGML_HIP_MAX_STREAMS 8+#endif++#ifndef GGML_LOG_DEBUG+#define GGML_LOG_DEBUG(...) ((void)0)+#endif++// Gufo manages HIP graphs outside this matrix adapter.+#undef GGML_HIP_USE_GRAPHS+#undef GGML_HIP_GRAPHS++// GGML_EXTENSION: ggml-common.h provides the canonical definition. We leave+// it undefined here so the vendored header's `#define GGML_EXTENSION+// __extension__` wins.++// ----------------------------------------------------------------------------+// Quantization type enum.+//+// Order matches llama.cpp's enum ggml_type. Values are pinned because the+// mmq switch uses them as case labels.+// ----------------------------------------------------------------------------++enum ggml_type {+ GGML_TYPE_F32 = 0,+ GGML_TYPE_F16 = 1,+ GGML_TYPE_Q4_0 = 2,+ GGML_TYPE_Q4_1 = 3,+ // GGML_TYPE_Q4_2 / Q4_3 deprecated+ GGML_TYPE_Q5_0 = 6,+ GGML_TYPE_Q5_1 = 7,+ GGML_TYPE_Q8_0 = 8,+ GGML_TYPE_Q8_1 = 9,+ GGML_TYPE_Q2_K = 10,+ GGML_TYPE_Q3_K = 11,+ GGML_TYPE_Q4_K = 12,+ GGML_TYPE_Q5_K = 13,+ GGML_TYPE_Q6_K = 14,+ GGML_TYPE_Q8_K = 15,+ GGML_TYPE_IQ2_XXS = 16,+ GGML_TYPE_IQ2_XS = 17,+ GGML_TYPE_IQ3_XXS = 18,+ GGML_TYPE_IQ1_S = 19,+ GGML_TYPE_IQ4_NL = 20,+ GGML_TYPE_IQ3_S = 21,+ GGML_TYPE_IQ2_S = 22,+ GGML_TYPE_IQ4_XS = 23,+ GGML_TYPE_I8 = 24,+ GGML_TYPE_I16 = 25,+ GGML_TYPE_I32 = 26,+ GGML_TYPE_I64 = 27,+ GGML_TYPE_F64 = 28,+ GGML_TYPE_IQ1_M = 29,+ GGML_TYPE_BF16 = 30,+ GGML_TYPE_MXFP4 = 39,+ GGML_TYPE_NVFP4 = 40,+ GGML_TYPE_Q1_0 = 41,+ GGML_TYPE_COUNT = 42,+};++enum ggml_glu_op {+ GGML_GLU_OP_REGLU,+ GGML_GLU_OP_GEGLU,+ GGML_GLU_OP_SWIGLU,+ GGML_GLU_OP_SWIGLU_OAI, // referenced by mmvq.cu's fused-GLU epilogue+ GGML_GLU_OP_COUNT,+};++// ----------------------------------------------------------------------------+// ggml_tensor: complete enough for common.hpp's+// ggml_hip_concurrent_event::is_valid() to compile cleanly. We NEVER+// instantiate or dereference one of these - the concurrent path is+// disabled.+//+// Field set matches the upstream order/types so hipGraph node_properties+// (which holds a `ggml_tensor node` by value inside `#ifdef USE_HIP_GRAPH`)+// also compiles. Sizes are conservative for storage; the adapter never copies into+// these.+// ----------------------------------------------------------------------------++struct ggml_tensor {+ enum ggml_type type;+ int32_t op; // enum ggml_op (opaque to us)+ int32_t flags;+ int64_t ne[GGML_MAX_DIMS]; // shape+ size_t nb[GGML_MAX_DIMS]; // stride bytes+ int32_t op_params[16]; // GGML_MAX_OP_PARAMS / sizeof(int32_t)+ struct ggml_tensor * src[GGML_MAX_SRC];+ struct ggml_tensor * view_src;+ size_t view_offs;+ void * data;+ char name[64]; // GGML_MAX_NAME+ void * extra;+ char padding[8];+};++// ggml_nbytes: byte size of tensor data. We never call this; provide a+// stub so common.hpp's is_valid() compiles. If anything does call it the+// returned 0 will surface as an immediate logic error.+static inline int64_t ggml_nbytes(const struct ggml_tensor * /*t*/) { return 0; }++// Microsecond timer (used only inside USE_HIP_GRAPH paths we disable).+int64_t ggml_time_us();++// ----------------------------------------------------------------------------+// Inline size traits.+//+// Lookup tables hand-aligned with llama.cpp's ggml_type_traits in+// ggml/src/ggml.c. Only types referenced by the vendored switch are needed.+// ----------------------------------------------------------------------------++inline size_t ggml_type_size(enum ggml_type t) {+ switch (t) {+ case GGML_TYPE_F32: return 4;+ case GGML_TYPE_F16: return 2;+ case GGML_TYPE_BF16: return 2;+ case GGML_TYPE_I8: return 1;+ case GGML_TYPE_I16: return 2;+ case GGML_TYPE_I32: return 4;+ case GGML_TYPE_I64: return 8;+ case GGML_TYPE_F64: return 8;+ case GGML_TYPE_Q4_0: return 18;+ case GGML_TYPE_Q4_1: return 20;+ case GGML_TYPE_Q5_0: return 22;+ case GGML_TYPE_Q5_1: return 24;+ case GGML_TYPE_Q8_0: return 34;+ case GGML_TYPE_Q8_1: return 36;+ case GGML_TYPE_Q2_K: return 84;+ case GGML_TYPE_Q3_K: return 110;+ case GGML_TYPE_Q4_K: return 144;+ case GGML_TYPE_Q5_K: return 176;+ case GGML_TYPE_Q6_K: return 210;+ case GGML_TYPE_Q8_K: return 292;+ case GGML_TYPE_IQ2_XXS: return 66;+ case GGML_TYPE_IQ2_XS: return 74;+ case GGML_TYPE_IQ2_S: return 82;+ case GGML_TYPE_IQ3_XXS: return 98;+ case GGML_TYPE_IQ3_S: return 110;+ case GGML_TYPE_IQ1_S: return 50;+ case GGML_TYPE_IQ1_M: return 56;+ case GGML_TYPE_IQ4_NL: return 18;+ case GGML_TYPE_IQ4_XS: return 136;+ case GGML_TYPE_MXFP4: return 17;+ case GGML_TYPE_NVFP4: return 18;+ case GGML_TYPE_Q1_0: return 36;+ default: return 0;+ }+}++inline int64_t ggml_blck_size(enum ggml_type t) {+ switch (t) {+ case GGML_TYPE_F32:+ case GGML_TYPE_F16:+ case GGML_TYPE_BF16:+ case GGML_TYPE_I8:+ case GGML_TYPE_I16:+ case GGML_TYPE_I32:+ case GGML_TYPE_I64:+ case GGML_TYPE_F64:+ return 1;+ case GGML_TYPE_Q4_0:+ case GGML_TYPE_Q4_1:+ case GGML_TYPE_Q5_0:+ case GGML_TYPE_Q5_1:+ case GGML_TYPE_Q8_0:+ case GGML_TYPE_Q8_1:+ case GGML_TYPE_IQ4_NL:+ case GGML_TYPE_MXFP4:+ return 32;+ case GGML_TYPE_NVFP4:+ return 64;+ case GGML_TYPE_Q2_K:+ case GGML_TYPE_Q3_K:+ case GGML_TYPE_Q4_K:+ case GGML_TYPE_Q5_K:+ case GGML_TYPE_Q6_K:+ case GGML_TYPE_Q8_K:+ case GGML_TYPE_IQ2_XXS:+ case GGML_TYPE_IQ2_XS:+ case GGML_TYPE_IQ2_S:+ case GGML_TYPE_IQ3_XXS:+ case GGML_TYPE_IQ3_S:+ case GGML_TYPE_IQ1_S:+ case GGML_TYPE_IQ1_M:+ case GGML_TYPE_IQ4_XS:+ case GGML_TYPE_Q1_0:+ return 256;+ default:+ return 1;+ }+}diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/qfn_mmq_prelude.h b/exllamav3/exllamav3_ext/rocm/gmmq/qfn_mmq_prelude.hnew file mode 100644index 0000000..7fe9582--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/qfn_mmq_prelude.h@@ -0,0 +1,42 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/qfn_mmq_prelude.h, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: #undef __HIP_NO_HALF_OPERATORS__ / __HIP_NO_HALF_CONVERSIONS__ (set by torch's HIP flags).+// Every system header the vendored tier includes, pulled in at global scope+// before the tier is wrapped in namespace qfn_mmq. The tier's own includes+// then hit include guards, so its definitions land in the namespace while+// the runtime headers stay global. The wrap keeps this copy from clashing+// with the identical tier linked for DeepSeek V4 Flash.+#pragma once+// kyojin: torch's HIP extension flags define these two; the vendored kernels rely on half operators and+// conversions (Gufo / llama.cpp build without them). Must run before the first HIP header.+#undef __HIP_NO_HALF_OPERATORS__+#undef __HIP_NO_HALF_CONVERSIONS__+#include <hip/hip_runtime.h>+#include <hip/hip_fp16.h>+#include <hip/hip_bf16.h>+#include <hip/hip_fp8.h>+#include <hipblas/hipblas.h>++#include <algorithm>+#include <array>+#include <cassert>+#include <cfloat>+#include <chrono>+#include <climits>+#include <cstdarg>+#include <cstddef>+#include <cstdint>+#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <map>+#include <memory>+#include <mutex>+#include <stddef.h>+#include <stdint.h>+#include <string>+#include <type_traits>+#include <unordered_map>+#include <vector>diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/quantize.cu b/exllamav3/exllamav3_ext/rocm/gmmq/quantize.cunew file mode 100644index 0000000..5ed41f5--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/quantize.cu@@ -0,0 +1,278 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/quantize.hip.cpp (renamed .cu), itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+#include "qfn_mmq_prelude.h"+namespace qfn_mmq {+#include "quantize.hpp"+#include <cstdint>++__launch_bounds__(HIP_QUANTIZE_BLOCK_SIZE, 1)+static __global__ void quantize_q8_1(+ const float * __restrict__ x, void * __restrict__ vy,+ const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,+ const int64_t ne0, const uint32_t ne1, const uint3 ne2) {+ const int64_t i0 = (int64_t)blockDim.x*blockIdx.x + threadIdx.x;++ if (i0 >= ne0) {+ return;+ }++ const int64_t i3 = fastdiv(blockIdx.z, ne2);+ const int64_t i2 = blockIdx.z - i3*ne2.z;+ const int64_t i1 = blockIdx.y;++ const int64_t & i00 = i0;+ const int64_t & i01 = i1;+ const int64_t & i02 = i2;+ const int64_t & i03 = i3;++ const int64_t i_cont = ((i3*ne2.z + i2) * ne1 + i1) * ne0 + i0;++ block_q8_1 * y = (block_q8_1 *) vy;++ const int64_t ib = i_cont / QK8_1; // block index+ const int64_t iqs = i_cont % QK8_1; // quant index++ const float xi = i0 < ne00 ? x[i03*s03 + i02*s02 + i01*s01 + i00] : 0.0f;+ float amax = fabsf(xi);++ amax = warp_reduce_max<QK8_1>(amax);++ const float d = amax / 127.0f;+ const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);++ // block_q8_1's sum is d*sum(qs), the sum of the values the dot products+ // consume, as ggml's CPU quantize_row_q8_1 stores it. The sum of the+ // unquantized x would leave the Q4_1/Q5_1 min term and the Q4_K/Q5_K MMQ+ // mins with m*sum(x - d*q), a rounding bias shared by every output row.+ const int sumq = warp_reduce_sum<QK8_1>((int) q);++ y[ib].qs[iqs] = q;++ if (iqs > 0) {+ return;+ }++ y[ib].ds = make_half2(d, d*sumq);+}++// quantize_q8_1 for one-expert slot rows (the decode down projection's+// view). Slot i1 is written at its rank among the slots ordered by expert+// and then slot, so slots that share an expert are adjacent; the first+// column block also records each rank's expert and source slot. Every row+// keeps quantize_q8_1's values; only its position changes.+__launch_bounds__(HIP_QUANTIZE_BLOCK_SIZE, 1)+static __global__ void quantize_q8_1_by_expert(+ const float * __restrict__ x, const int32_t * __restrict__ ids,+ void * __restrict__ vy, int32_t * __restrict__ sorted_ids,+ int32_t * __restrict__ slot_of_rank, const int64_t ne00,+ const int64_t s01, const int64_t ne0, const int slots) {+ const int64_t i0 = (int64_t)blockDim.x*blockIdx.x + threadIdx.x;+ const int i1 = blockIdx.y;+ const int lane = threadIdx.x % 32;+ const int32_t expert = ids[i1];+ int rank = 0;+ for (int base = 0; base < slots; base += 32) {+ const int j = base + lane;+ const int32_t other = j < slots ? ids[j] : 0;+ rank += __popc(static_cast<uint32_t>(__ballot(+ j < slots && (other < expert || (other == expert && j < i1)))));+ }++ if (i0 >= ne0) {+ return;+ }++ const int64_t i_cont = (int64_t)rank * ne0 + i0;++ block_q8_1 * y = (block_q8_1 *) vy;++ const int64_t ib = i_cont / QK8_1; // block index+ const int64_t iqs = i_cont % QK8_1; // quant index++ const float xi = i0 < ne00 ? x[i1*s01 + i0] : 0.0f;+ float amax = fabsf(xi);++ amax = warp_reduce_max<QK8_1>(amax);++ const float d = amax / 127.0f;+ const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);++ // block_q8_1's sum is d*sum(qs), the sum of the values the dot products+ // consume, as ggml's CPU quantize_row_q8_1 stores it. The sum of the+ // unquantized x would leave the Q4_1/Q5_1 min term and the Q4_K/Q5_K MMQ+ // mins with m*sum(x - d*q), a rounding bias shared by every output row.+ const int sumq = warp_reduce_sum<QK8_1>((int) q);++ y[ib].qs[iqs] = q;++ if (i0 == 0) {+ sorted_ids[rank] = expert;+ slot_of_rank[rank] = i1;+ }++ if (iqs > 0) {+ return;+ }++ y[ib].ds = make_half2(d, d*sumq);+}++template <mmq_q8_1_ds_layout ds_layout>+static __global__ void quantize_mmq_q8_1(+ const float * __restrict__ x, const int32_t * __restrict__ ids, void * __restrict__ vy,+ const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,+ const int64_t ne0, const int ne1, const int ne2) {++ constexpr int vals_per_scale = ds_layout == MMQ_Q8_1_DS_LAYOUT_D2S6 ? 64 : 32;+ constexpr int vals_per_sum = ds_layout == MMQ_Q8_1_DS_LAYOUT_D2S6 ? 16 : 32;++ const int64_t i0 = ((int64_t)blockDim.x*blockIdx.y + threadIdx.x)*4;++ if (i0 >= ne0) {+ return;+ }++ const int64_t i1 = blockIdx.x;+ const int64_t i2 = blockIdx.z % ne2;+ const int64_t i3 = blockIdx.z / ne2;++ const int64_t i00 = i0;+ const int64_t i01 = ids ? ids[i1] : i1;+ const int64_t i02 = i2;+ const int64_t i03 = i3;++ const float4 * x4 = (const float4 *) x;++ block_q8_1_mmq * y = (block_q8_1_mmq *) vy;++ const int64_t ib0 = blockIdx.z*((int64_t)gridDim.x*gridDim.y*blockDim.x/QK8_1); // first block of channel+ const int64_t ib = ib0 + (i0 / (4*QK8_1))*ne1 + blockIdx.x; // block index in channel+ const int64_t iqs = i0 % (4*QK8_1); // quant index in block++ // Load 4 floats per thread and calculate max. abs. value between them:+ const float4 xi = i0 < ne00 ? x4[(i03*s03 + i02*s02 + i01*s01 + i00)/4] : make_float4(0.0f, 0.0f, 0.0f, 0.0f);+ float amax = fabsf(xi.x);+ amax = fmaxf(amax, fabsf(xi.y));+ amax = fmaxf(amax, fabsf(xi.z));+ amax = fmaxf(amax, fabsf(xi.w));++ // Exchange max. abs. value between vals_per_scale/4 threads.+#pragma unroll+ for (int offset = vals_per_scale/8; offset > 0; offset >>= 1) {+ amax = fmaxf(amax, __shfl_xor(amax, offset, WARP_SIZE));+ }++ const float d_inv = 127.0f / amax;+ char4 q;+ q.x = roundf(xi.x*d_inv);+ q.y = roundf(xi.y*d_inv);+ q.z = roundf(xi.z*d_inv);+ q.w = roundf(xi.w*d_inv);+ const float d = 1.0f / d_inv;++ float sum;+ if (ds_layout != MMQ_Q8_1_DS_LAYOUT_D4) {+ // d*sum(q) of the quantized values, as in quantize_q8_1.+ int sumq = q.x + q.y + q.z + q.w;++ // Calculate sums across vals_per_sum/4 threads.+#pragma unroll+ for (int offset = vals_per_sum/8; offset > 0; offset >>= 1) {+ sumq += __shfl_xor(sumq, offset, WARP_SIZE);+ }+ sum = d*sumq;+ }++ // Write back 4 int8 values as a single 32 bit value for better memory bandwidth:+ char4 * yqs4 = (char4 *) y[ib].qs;+ yqs4[iqs/4] = q;++ if (ds_layout == MMQ_Q8_1_DS_LAYOUT_D2S6) {+ if (iqs % 16 != 0 || iqs >= 96) {+ return;+ }++ y[ib].d2s6[2 + iqs/16] = sum;++ if (iqs % 64 != 0) {+ return;+ }++ y[ib].d2s6[iqs/64] = d;++ return;+ }++ if (iqs % 32 != 0) {+ return;+ }++ if (ds_layout == MMQ_Q8_1_DS_LAYOUT_DS4) {+ y[ib].ds4[iqs/32] = make_half2(d, sum);+ } else {+ y[ib].d4[iqs/32] = d;+ }+}++void quantize_row_q8_1_hip(+ const float * x, const int32_t * ids, void * vy, const ggml_type type_src0,+ const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,+ const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, hipStream_t stream) {+ GGML_ASSERT(!ids);+ GGML_ASSERT(ne0 % QK8_1 == 0);++ const uint3 ne2_fastdiv = init_fastdiv_values(ne2);++ const int64_t block_num_x = (ne0 + HIP_QUANTIZE_BLOCK_SIZE - 1) / HIP_QUANTIZE_BLOCK_SIZE;+ const dim3 num_blocks(block_num_x, ne1, ne2*ne3);+ const dim3 block_size(HIP_QUANTIZE_BLOCK_SIZE, 1, 1);+ quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, ne00, s01, s02, s03, ne0, ne1, ne2_fastdiv);+ GGML_UNUSED(type_src0);+}++void quantize_row_q8_1_by_expert_hip(+ const float * x, const int32_t * ids, void * vy, int32_t * sorted_ids,+ int32_t * slot_of_rank, const int64_t ne00, const int64_t s01,+ const int64_t ne0, const int slots, hipStream_t stream) {+ GGML_ASSERT(ne0 % QK8_1 == 0 && slots > 0);+ const int64_t block_num_x = (ne0 + HIP_QUANTIZE_BLOCK_SIZE - 1) / HIP_QUANTIZE_BLOCK_SIZE;+ const dim3 num_blocks(block_num_x, slots, 1);+ const dim3 block_size(HIP_QUANTIZE_BLOCK_SIZE, 1, 1);+ quantize_q8_1_by_expert<<<num_blocks, block_size, 0, stream>>>(+ x, ids, vy, sorted_ids, slot_of_rank, ne00, s01, ne0, slots);+}++void quantize_mmq_q8_1_hip(+ const float * x, const int32_t * ids, void * vy, const ggml_type type_src0,+ const int64_t ne00, const int64_t s01, const int64_t s02, const int64_t s03,+ const int64_t ne0, const int64_t ne1, const int64_t ne2, const int64_t ne3, hipStream_t stream) {+ GGML_ASSERT(ne00 % 4 == 0);+ GGML_ASSERT(ne0 % (4*QK8_1) == 0);++ // ne1 tends to assume the highest values, therefore use it as the "x" dimension of the HIP grid:+ const int64_t block_num_y = (ne0 + 4*HIP_QUANTIZE_BLOCK_SIZE_MMQ - 1) / (4*HIP_QUANTIZE_BLOCK_SIZE_MMQ);+ const dim3 num_blocks(ne1, block_num_y, ne2*ne3);+ const dim3 block_size(HIP_QUANTIZE_BLOCK_SIZE_MMQ, 1, 1);+ switch (mmq_get_q8_1_ds_layout(type_src0)) {+ case MMQ_Q8_1_DS_LAYOUT_D4:+ quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D4>+ <<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2);+ break;+ case MMQ_Q8_1_DS_LAYOUT_DS4:+ quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_DS4>+ <<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2);+ break;+ case MMQ_Q8_1_DS_LAYOUT_D2S6:+ quantize_mmq_q8_1<MMQ_Q8_1_DS_LAYOUT_D2S6>+ <<<num_blocks, block_size, 0, stream>>>(x, ids, vy, ne00, s01, s02, s03, ne0, ne1, ne2);+ break;+ default:+ GGML_ABORT("fatal error");+ break;+ }+}++} // namespace qfn_mmqdiff --git a/exllamav3/exllamav3_ext/rocm/gmmq/quantize.hpp b/exllamav3/exllamav3_ext/rocm/gmmq/quantize.hppnew file mode 100644index 0000000..777b612--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/quantize.hpp@@ -0,0 +1,34 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/quantize.hpp, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+#pragma once++#include "common.hpp"+#include "mmq.hpp"++#include <cstdint>++#define HIP_QUANTIZE_BLOCK_SIZE 256+#define HIP_QUANTIZE_BLOCK_SIZE_MMQ 128++static_assert(MATRIX_ROW_PADDING % HIP_QUANTIZE_BLOCK_SIZE == 0, "Risk of out-of-bounds access.");+static_assert(MATRIX_ROW_PADDING % (4*HIP_QUANTIZE_BLOCK_SIZE_MMQ) == 0, "Risk of out-of-bounds access.");++void quantize_row_q8_1_hip(+ const float * x, const int32_t * ids, void * vy,+ ggml_type type_src0, int64_t ne00, int64_t s01, int64_t s02, int64_t s03,+ int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3, hipStream_t stream);++// quantize_row_q8_1_hip for `slots` one-expert rows, written in expert+// order: row r of vy is slot slot_of_rank[r], whose expert is sorted_ids[r].+void quantize_row_q8_1_by_expert_hip(+ const float * x, const int32_t * ids, void * vy, int32_t * sorted_ids,+ int32_t * slot_of_rank, int64_t ne00, int64_t s01, int64_t ne0,+ int slots, hipStream_t stream);++void quantize_mmq_q8_1_hip(+ const float * x, const int32_t * ids, void * vy,+ ggml_type type_src0, int64_t ne00, int64_t s01, int64_t s02, int64_t s03,+ int64_t ne0, int64_t ne1, int64_t ne2, int64_t ne3, hipStream_t stream);diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/vecdotq.hpp b/exllamav3/exllamav3_ext/rocm/gmmq/vecdotq.hppnew file mode 100644index 0000000..b7acbc1--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/vecdotq.hpp@@ -0,0 +1,1325 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/vecdotq.hpp, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+#pragma once++#include "common.hpp"++#include <cstdint>++static __device__ __forceinline__ int get_int_b1(const void * x, const int & i32) {+ const uint8_t * x8 = (const uint8_t *) x;++ int x32 = x8[4*i32 + 0] << 0;+ x32 |= x8[4*i32 + 1] << 8;+ x32 |= x8[4*i32 + 2] << 16;+ x32 |= x8[4*i32 + 3] << 24;++ return x32;+}++static __device__ __forceinline__ int get_int_b2(const void * x, const int & i32) {+ const uint16_t * x16 = (const uint16_t *) x; // assume at least 2 byte alignment++ int x32 = x16[2*i32 + 0] << 0;+ x32 |= x16[2*i32 + 1] << 16;++ return x32;+}++static __device__ __forceinline__ int get_int_b4(const void * x, const int & i32) {+ return ((const int *) x)[i32]; // assume at least 4 byte alignment+}++// q4 contains 8 indices with 4 bit each.+// This function selects those bytes from table that are at those indices and returns them as int2.+// The first int contains the bytes with even indices in q4, the second int contains the bytes with odd indices in q4.+static __device__ __forceinline__ int2 get_int_from_table_16(const int & q4, const int8_t * table) {+ // Load the 16-byte table into four 32-bit unsigned integers.+ const uint32_t *values = (const uint32_t *)table;++ const uint32_t q_even = q4;+ const uint32_t q_odd = (q4 >> 4);++ // Perform lookups in the lower half of the table (indices 0-7).+ uint32_t v_even_low = __builtin_amdgcn_perm(values[1], values[0], q_even & 0x07070707);+ uint32_t v_odd_low = __builtin_amdgcn_perm(values[1], values[0], q_odd & 0x07070707);++ // Perform lookups in the upper half of the table (indices 8-15).+ uint32_t v_even_high = __builtin_amdgcn_perm(values[3], values[2], q_even & 0x07070707);+ uint32_t v_odd_high = __builtin_amdgcn_perm(values[3], values[2], q_odd & 0x07070707);++ // Select between the low and high results based on the MSB of each index nibble.+ uint32_t mask_even = 0x03020100 | ((q_even & 0x08080808) >> 1);+ uint32_t res_x = __builtin_amdgcn_perm(v_even_high, v_even_low, mask_even);+ uint32_t mask_odd = 0x03020100 | ((q_odd & 0x08080808) >> 1);+ uint32_t res_y = __builtin_amdgcn_perm(v_odd_high, v_odd_low, mask_odd);++ return make_int2(res_x, res_y);+}++static __device__ __forceinline__ uint32_t unpack_ksigns(const uint8_t v) {+ // v is a 7 bit int, with the 8th sign being encodable as popcnt+ // with xor we can "correct" the bit instead of having to mask+ const uint32_t p = __popc(v) & 1;+ const uint32_t s = v ^ p << 7;+ // broadcast over uint to allow for 0x08040201 / 0x80402010 as selectors+ return s * 0x01010101;+}++// VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called+// MMVQ = mul_mat_vec_q, MMQ = mul_mat_q++#define VDR_Q1_0_Q8_1_MMVQ 1 // Process one 32-element chunk at a time for parallelism+#define VDR_Q1_0_Q8_1_MMQ 4 // Q1_0 has 128 bits (4 ints) per block++#define VDR_Q4_0_Q8_1_MMVQ 2+#define VDR_Q4_0_Q8_1_MMQ 4++template <int vdr> static __device__ __forceinline__ float vec_dot_q4_0_q8_1_impl(+ const int * v, const int * u, const float & d4, const half2 & ds8) {++ int sumi = 0;++#pragma unroll+ for (int i = 0; i < vdr; ++i) {+ const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;+ const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;++ // SIMD dot product of quantized values+ sumi = ggml_hip_dp4a(vi0, u[2*i+0], sumi);+ sumi = ggml_hip_dp4a(vi1, u[2*i+1], sumi);+ }++ const float2 ds8f = __half22float2(ds8);++ // second part effectively subtracts 8 from each quant value+ return d4 * (sumi * ds8f.x - (8*vdr/QI4_0) * ds8f.y);+}++#define VDR_Q4_1_Q8_1_MMVQ 2+#define VDR_Q4_1_Q8_1_MMQ 4++template <int vdr> static __device__ __forceinline__ float vec_dot_q4_1_q8_1_impl(+ const int * v, const int * u, const half2 & dm4, const half2 & ds8) {++ int sumi = 0;++#pragma unroll+ for (int i = 0; i < vdr; ++i) {+ const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;+ const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;++ // SIMD dot product of quantized values+ sumi = ggml_hip_dp4a(vi0, u[2*i+0], sumi);+ sumi = ggml_hip_dp4a(vi1, u[2*i+1], sumi);+ }++ // Keep scale products in F32, including small activations.+ const float2 dm4f = __half22float2(dm4);+ const float2 ds8f = __half22float2(ds8);+ const float d4d8 = dm4f.x * ds8f.x;+ const float m4s8 = dm4f.y * ds8f.y;++ // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it+ return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));+}++#define VDR_Q5_0_Q8_1_MMVQ 2+#define VDR_Q5_0_Q8_1_MMQ 4++template <int vdr> static __device__ __forceinline__ float vec_dot_q5_0_q8_1_impl(+ const int * vl, const int * vh, const int * u, const float & d5, const half2 & ds8) {++ int sumi = 0;++#pragma unroll+ for (int i = 0; i < vdr; ++i) {+ int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits+ vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4+ vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12+ vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20+ vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28+ sumi = ggml_hip_dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values++ int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits+ vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4+ vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12+ vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20+ vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28+ sumi = ggml_hip_dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values+ }++ const float2 ds8f = __half22float2(ds8);++ // second part effectively subtracts 16 from each quant value+ return d5 * (sumi * ds8f.x - (16*vdr/QI5_0) * ds8f.y);+}++#define VDR_Q5_1_Q8_1_MMVQ 2+#define VDR_Q5_1_Q8_1_MMQ 4++template <int vdr> static __device__ __forceinline__ float vec_dot_q5_1_q8_1_impl(+ const int * vl, const int * vh, const int * u, const half2 & dm5, const half2 & ds8) {++ int sumi = 0;++#pragma unroll+ for (int i = 0; i < vdr; ++i) {+ // Spread four high bits into separate bytes. Each nibble is at most+ // 15, so multiplication cannot carry into another selected bit.+ const unsigned lo =+ ((static_cast<unsigned>(vh[i]) & 15u) * 0x00204081u) & 0x01010101u;+ const unsigned hi =+ (((static_cast<unsigned>(vh[i]) >> 16) & 15u) * 0x00204081u) &+ 0x01010101u;+ const int vi0 = (vl[i] & 0x0F0F0F0F) | (lo << 4);+ sumi = ggml_hip_dp4a(vi0, u[2*i+0], sumi); // SIMD dot product of quantized values++ const int vi1 = ((vl[i] >> 4) & 0x0F0F0F0F) | (hi << 4);+ sumi = ggml_hip_dp4a(vi1, u[2*i+1], sumi); // SIMD dot product of quantized values+ }++#ifdef FAST_FP16_AVAILABLE+ const float2 tmp = __half22float2(__hmul2(dm5, ds8));+ const float d5d8 = tmp.x;+ const float m5s8 = tmp.y;+#else+ const float2 dm5f = __half22float2(dm5);+ const float2 ds8f = __half22float2(ds8);+ const float d5d8 = dm5f.x * ds8f.x;+ const float m5s8 = dm5f.y * ds8f.y;+#endif // FAST_FP16_AVAILABLE++ // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it+ return sumi*d5d8 + m5s8 / (QI5_1 / vdr);+}++#define VDR_Q8_0_Q8_1_MMVQ 2+#define VDR_Q8_0_Q8_1_MMQ 8++template <typename T, int vdr> static __device__ __forceinline__ T vec_dot_q8_0_q8_1_impl(+ const int * v, const int * u, const T & d8_0, const T & d8_1) {++ int sumi = 0;++#pragma unroll+ for (int i = 0; i < vdr; ++i) {+ // SIMD dot product of quantized values+ sumi = ggml_hip_dp4a(v[i], u[i], sumi);+ }++ return d8_0*d8_1 * ((T) sumi);+}++template <int vdr> static __device__ __forceinline__ float vec_dot_q8_1_q8_1_impl(+ const int * v, const int * u, const half2 & dm8, const half2 & ds8) {++ int sumi = 0;++#pragma unroll+ for (int i = 0; i < vdr; ++i) {+ // SIMD dot product of quantized values+ sumi = ggml_hip_dp4a(v[i], u[i], sumi);+ }++#ifdef FAST_FP16_AVAILABLE+ const float2 tmp = __half22float2(__hmul2(dm8, ds8));+ const float d8d8 = tmp.x;+ const float m8s8 = tmp.y;+#else+ const float2 dm8f = __half22float2(dm8);+ const float2 ds8f = __half22float2(ds8);+ const float d8d8 = dm8f.x * ds8f.x;+ const float m8s8 = dm8f.y * ds8f.y;+#endif // FAST_FP16_AVAILABLE++ // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it+ return sumi*d8d8 + m8s8 / (QI8_1 / vdr);+}++template <int vdr> static __device__ __forceinline__ float vec_dot_q8_0_16_q8_1_impl(+ const int * v, const int * u, const float * d8_0, const float & d8_1) {++ float sumf = 0.0f;++#pragma unroll+ for (int i0 = 0; i0 < vdr; i0 += QI8_0/2) {+ int sumi = 0;++#pragma unroll+ for (int i = i0; i < i0 + QI8_0/2; ++i) {+ // SIMD dot product of quantized values+ sumi = ggml_hip_dp4a(v[i], u[i], sumi);+ }++ sumf += d8_0[i0/(QI8_0/2)]*sumi;+ }++ return d8_1*sumf;+}++#define VDR_MXFP4_Q8_1_MMVQ 2+#define VDR_MXFP4_Q8_1_MMQ 4++static __device__ __forceinline__ float vec_dot_mxfp4_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_mxfp4 * bq4 = (const block_mxfp4 *) vbq + kbx;++ const int * q8 = (const int *) bq8_1->qs + iqs;++ int sumi = 0;+#pragma unroll+ for (int l = 0; l < VDR_MXFP4_Q8_1_MMVQ; ++l) {+ const int aux_q4 = get_int_b1(bq4->qs, iqs + l);+ const int2 v = get_int_from_table_16(aux_q4, kvalues_mxfp4);++ sumi = ggml_hip_dp4a(v.x, q8[l + 0], sumi);+ sumi = ggml_hip_dp4a(v.y, q8[l + 4], sumi);+ }++ const float d = ggml_hip_e8m0_to_fp32(bq4->e) * 0.5f * __low2float(bq8_1->ds);+ return d * sumi;+}++#define VDR_NVFP4_Q8_1_MMVQ 4+#define VDR_NVFP4_Q8_1_MMQ 8++static __device__ __forceinline__ float vec_dot_nvfp4_q8_1(+ const void * __restrict__ vbq,+ const block_q8_1 * __restrict__ bq8_1,+ const int32_t & kbx,+ const int32_t & iqs) {++ const block_nvfp4 * bq4 = (const block_nvfp4 *) vbq + kbx;+ float sum = 0.0f;+#pragma unroll+ for (int i = 0; i < VDR_NVFP4_Q8_1_MMVQ/2; i++) {+ const int32_t iqs0 = iqs + 2*i;+ const int32_t iqs1 = iqs0 + 1;+ const int32_t is = iqs0 >> 1;+ const int2 v0 = get_int_from_table_16(get_int_b4(bq4->qs, iqs0), kvalues_mxfp4);+ const int2 v1 = get_int_from_table_16(get_int_b4(bq4->qs, iqs1), kvalues_mxfp4);+ const block_q8_1 * bq8 = bq8_1 + (is >> 1);+ const int32_t i8 = ((is & 1) << 2);++ int sumi = ggml_hip_dp4a(v0.x, get_int_b4(bq8->qs, i8 + 0), 0);+ sumi = ggml_hip_dp4a(v0.y, get_int_b4(bq8->qs, i8 + 2), sumi);+ sumi = ggml_hip_dp4a(v1.x, get_int_b4(bq8->qs, i8 + 1), sumi);+ sumi = ggml_hip_dp4a(v1.y, get_int_b4(bq8->qs, i8 + 3), sumi);++ const float d = ggml_hip_ue4m3_to_fp32(bq4->d[is]) * __low2float(bq8->ds);+ sum += d * float(sumi);+ }++ return sum;+}+#define VDR_Q2_K_Q8_1_MMVQ 1+#define VDR_Q2_K_Q8_1_MMQ 4++// contiguous v/x values+static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmvq(+ const int & v, const int * __restrict__ u, const uint8_t * __restrict__ scales,+ const half2 & dm2, const float * __restrict__ d8) {++ float sumf_d = 0.0f;+ float sumf_m = 0.0f;++#pragma unroll+ for (int i = 0; i < QR2_K; ++i) {+ const int sc = scales[2*i];++ const int vi = (v >> (2*i)) & 0x03030303;++ sumf_d += d8[i] * (ggml_hip_dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product++ // fill int with 4x m+ int m = sc >> 4;+ m |= m << 8;+ m |= m << 16;+ sumf_m += d8[i] * ggml_hip_dp4a(m, u[i], 0); // multiply constant q2_K part with sum of q8_1 values+ }++ const float2 dm2f = __half22float2(dm2);++ return dm2f.x*sumf_d - dm2f.y*sumf_m;+}++// contiguous v/x + u/y values+template <int ns8>+static __device__ __forceinline__ float vec_dot_q2_K_q8_1_impl_mmq(+ const int * __restrict__ v, const int * __restrict__ u, const half2 * dm2, const float & d8, const half2 * s8) {++ float sumf = 0.0f;+ float sumf_d8 = 0.0f;++#pragma unroll+ for (int i0 = 0; i0 < QR2_K*VDR_Q2_K_Q8_1_MMQ; i0 += QI8_1) {+ const float2 dm2f0 = __half22float2(dm2[i0/(QI8_1/2) + 0]);+ int sumi_d0 = 0;++ const float2 dm2f1 = __half22float2(dm2[i0/(QI8_1/2) + 1]);+ int sumi_d1 = 0;++#pragma unroll+ for (int i = i0; i < i0 + QI8_1/2; ++i) {+ sumi_d0 = ggml_hip_dp4a(v[i], u[i], sumi_d0);+ }+ sumf_d8 += dm2f0.x * sumi_d0;++#pragma unroll+ for (int i = i0 + QI8_1/2; i < i0 + QI8_1; ++i) {+ sumi_d1 = ggml_hip_dp4a(v[i], u[i], sumi_d1);+ }+ sumf_d8 += dm2f1.x * sumi_d1;++ if (i0/QI8_1 < ns8) {+ const float2 s8f = __half22float2(s8[i0/QI8_1]);+ sumf -= dm2f0.y*s8f.x;+ sumf -= dm2f1.y*s8f.y;+ } else {+ int sumi_m0 = 0;+#pragma unroll+ for (int i = i0; i < i0 + QI8_1/2; ++i) {+ sumi_m0 = ggml_hip_dp4a(0x01010101, u[i], sumi_m0);+ }+ sumf_d8 -= dm2f0.y * sumi_m0;++ int sumi_m1 = 0;+#pragma unroll+ for (int i = i0 + QI8_1/2; i < i0 + QI8_1; ++i) {+ sumi_m1 = ggml_hip_dp4a(0x01010101, u[i], sumi_m1);+ }+ sumf_d8 -= dm2f1.y * sumi_m1;+ }+ }++ return sumf + d8*sumf_d8;+}++#define VDR_Q3_K_Q8_1_MMVQ 1+#define VDR_Q3_K_Q8_1_MMQ 2++// contiguous v/x values+static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmvq(+ const int & vl, const int & vh, const int * __restrict__ u, const uint8_t * __restrict__ scales,+ const int & scale_offset, const float & d3, const float * __restrict__ d8) {++ float sumf = 0.0f;++#pragma unroll+ for (int i = 0; i < QR3_K; ++i) {+ const int isc = scale_offset + 2*i;++ const int isc_low = isc % (QK_K/32);+ const int sc_shift_low = 4 * (isc / (QK_K/32));+ const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;++ const int isc_high = isc % (QK_K/64);+ const int sc_shift_high = 2 * (isc / (QK_K/64));+ const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;++ const int sc = (sc_low | sc_high) - 32;++ const int vil = (vl >> (2*i)) & 0x03030303;++ const int vih = ((vh >> i) << 2) & 0x04040404;++ const int vi = __vsubss4(vil, vih);++ sumf += d8[i] * (ggml_hip_dp4a(vi, u[i], 0) * sc); // SIMD dot product+ }++ return d3 * sumf;+}++// contiguous v/x + u/y values+static __device__ __forceinline__ float vec_dot_q3_K_q8_1_impl_mmq(+ const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ scales,+ const float & d3, const float & d8) {++ int sumi = 0;++#pragma unroll+ for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {+ int sumi_sc = 0;++#pragma unroll+ for (int i = i0; i < i0 + QI8_1/2; ++i) {+ sumi_sc = ggml_hip_dp4a(v[i], u[i], sumi_sc); // SIMD dot product+ }++ sumi += sumi_sc * scales[i0 / (QI8_1/2)];+ }++ return d3*d8 * sumi;+}++#define VDR_Q4_K_Q8_1_MMVQ 2+#define VDR_Q4_K_Q8_1_MMQ 8++// contiguous v/x values+static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_vmmq(+ const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,+ const uint8_t * __restrict__ m, const half2 & dm4, const float * __restrict__ d8) {++ float sumf_d = 0.0f;+ float sumf_m = 0.0f;++#pragma unroll+ for (int i = 0; i < QR4_K; ++i) {+ const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;+ const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;++ const int dot1 = ggml_hip_dp4a(v1i, u[2*i+1], ggml_hip_dp4a(v0i, u[2*i+0], 0)); // SIMD dot product+ const int dot2 = ggml_hip_dp4a(0x01010101, u[2*i+1], ggml_hip_dp4a(0x01010101, u[2*i+0], 0)); // sum of u++ sumf_d += d8[i] * (dot1 * sc[i]);+ sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values+ }++ const float2 dm4f = __half22float2(dm4);++ return dm4f.x*sumf_d - dm4f.y*sumf_m;+}++// contiguous v/x + u/y values+static __device__ __forceinline__ float vec_dot_q4_K_q8_1_impl_mmq(+ const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,+ const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {++ float sumf_d = 0.0f;+ float sumf_m = 0.0f;++#pragma unroll+ for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {+ int sumi_d = 0;++#pragma unroll+ for (int j = 0; j < QI8_1; ++j) {+ sumi_d = ggml_hip_dp4a((v[j] >> (4*i)) & 0x0F0F0F0F, u[i*QI8_1 + j], sumi_d); // SIMD dot product+ }++ const float2 ds8f = __half22float2(ds8[i]);++ sumf_d += ds8f.x * (sc[i] * sumi_d);+ sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val+ }++ const float2 dm4f = __half22float2(dm4);++ return dm4f.x*sumf_d - dm4f.y*sumf_m;+}++#define VDR_Q5_K_Q8_1_MMVQ 2+#define VDR_Q5_K_Q8_1_MMQ 8++// contiguous v/x values+static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_vmmq(+ const int * __restrict__ vl, const int * __restrict__ vh, const int * __restrict__ u, const uint8_t * __restrict__ sc,+ const uint8_t * __restrict__ m, const half2 & dm5, const float * __restrict__ d8) {++ float sumf_d = 0.0f;+ float sumf_m = 0.0f;++#pragma unroll+ for (int i = 0; i < QR5_K; ++i) {+ const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;+ const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;++ const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;+ const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;++ const int v0i = vl0i | vh0i;+ const int v1i = vl1i | vh1i;++ const int dot1 = ggml_hip_dp4a(v0i, u[2*i+0], ggml_hip_dp4a(v1i, u[2*i+1], 0)); // SIMD dot product+ const int dot2 = ggml_hip_dp4a(0x01010101, u[2*i+0], ggml_hip_dp4a(0x01010101, u[2*i+1], 0)); // sum of u++ sumf_d += d8[i] * (dot1 * sc[i]);+ sumf_m += d8[i] * (dot2 * m[i]);++ }++ const float2 dm5f = __half22float2(dm5);++ return dm5f.x*sumf_d - dm5f.y*sumf_m;+}++// contiguous v/x + u/y values+static __device__ __forceinline__ float vec_dot_q5_K_q8_1_impl_mmq(+ const int * __restrict__ v, const int * __restrict__ u, const uint8_t * __restrict__ sc,+ const uint8_t * __restrict__ m, const half2 & dm4, const half2 * __restrict__ ds8) {++ float sumf_d = 0.0f;+ float sumf_m = 0.0f;++#pragma unroll+ for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {+ int sumi_d = 0;++#pragma unroll+ for (int j = 0; j < QI8_1; ++j) {+ sumi_d = ggml_hip_dp4a(v[i*QI8_1 + j], u[i*QI8_1 + j], sumi_d); // SIMD dot product+ }++ const float2 ds8f = __half22float2(ds8[i]);++ sumf_d += ds8f.x * (sc[i] * sumi_d);+ sumf_m += ds8f.y * m[i]; // sum of q8_1 block * q4_K min val+ }++ const float2 dm4f = __half22float2(dm4);++ return dm4f.x*sumf_d - dm4f.y*sumf_m;+}++#define VDR_Q6_K_Q8_1_MMVQ 1+#define VDR_Q6_K_Q8_1_MMQ 8++// contiguous v/x values+static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmvq(+ const int & vl, const int & vh, const int * __restrict__ u, const int8_t * __restrict__ scales,+ const float & d, const float * __restrict__ d8) {++ float sumf = 0.0f;++#pragma unroll+ for (int i = 0; i < QR6_K; ++i) {+ const int sc = scales[4*i];++ const int vil = (vl >> (4*i)) & 0x0F0F0F0F;++ const int vih = ((vh >> (4*i)) << 4) & 0x30303030;++ const int vi = __vsubss4((vil | vih), 0x20202020); // vi = (vil | vih) - 32++ sumf += d8[i] * (ggml_hip_dp4a(vi, u[i], 0) * sc); // SIMD dot product+ }++ return d*sumf;+}++// contiguous v/x + u/y values+static __device__ __forceinline__ float vec_dot_q6_K_q8_1_impl_mmq(+ const int * __restrict__ v, const int * __restrict__ u, const int8_t * __restrict__ sc,+ const float & d6, const float * __restrict__ d8) {++ float sumf_d = 0.0f;++ const int sc_packed = get_int_b4(sc, 0);+ const int8_t * sc_reg = (const int8_t *) &sc_packed;++#pragma unroll+ for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {+ int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale++#pragma unroll+ for (int i = i0; i < i0 + 2; ++i) {+ sumi_d.x = ggml_hip_dp4a(v[2*i+0], u[2*i+0], sumi_d.x); // SIMD dot product+ sumi_d.x = ggml_hip_dp4a(v[2*i+1], u[2*i+1], sumi_d.x); // SIMD dot product++ sumi_d.y = ggml_hip_dp4a(v[2*i+4], u[2*i+4], sumi_d.y); // SIMD dot product+ sumi_d.y = ggml_hip_dp4a(v[2*i+5], u[2*i+5], sumi_d.y); // SIMD dot product+ }++ sumf_d += d8[i0/4] * (sc_reg[i0/2+0]*sumi_d.x + sc_reg[i0/2+1]*sumi_d.y);+ }++ return d6 * sumf_d;+}++static __device__ __forceinline__ float vec_dot_q1_0_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q1_0 * bq1_0 = (const block_q1_0 *) vbq + kbx;++ // Q1_0: 128 elements with ONE scale+ // Q8_1: 32 elements per block with individual scales+ // iqs selects which of the 4 chunks of 32 elements to process (0-3)++ const float d1 = bq1_0->d;++ // Process only the chunk specified by iqs+ const block_q8_1 * bq8_1_chunk = bq8_1 + iqs;++ // Load 32 bits (4 bytes) for this chunk from Q1_0+ const int offset = iqs * 4;+ const int v = bq1_0->qs[offset + 0] | (bq1_0->qs[offset + 1] << 8) |+ (bq1_0->qs[offset + 2] << 16) | (bq1_0->qs[offset + 3] << 24);++ // Unpack 32 bits into 32 signed values (-1 or +1)+ int vi_bytes[8];+#pragma unroll+ for (int j = 0; j < 8; ++j) {+ const int shift = j * 4;+ const int bits4 = (v >> shift) & 0x0F;+ const int b0 = (bits4 & 0x01) ? 1 : -1;+ const int b1 = (bits4 & 0x02) ? 1 : -1;+ const int b2 = (bits4 & 0x04) ? 1 : -1;+ const int b3 = (bits4 & 0x08) ? 1 : -1;+ vi_bytes[j] = (b0 & 0xFF) | ((b1 & 0xFF) << 8) | ((b2 & 0xFF) << 16) | ((b3 & 0xFF) << 24);+ }++ // Compute dot product for this 32-element chunk+ int sumi = 0;+#pragma unroll+ for (int j = 0; j < 8; ++j) {+ const int u = get_int_b4(bq8_1_chunk->qs, j);+ sumi = ggml_hip_dp4a(vi_bytes[j], u, sumi);+ }++ // Apply Q1_0's single scale and this chunk's Q8_1 scale+ const float d8 = __low2float(bq8_1_chunk->ds);+ return d1 * d8 * sumi;+}++static __device__ __forceinline__ float vec_dot_q4_0_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq + kbx;++ int v[VDR_Q4_0_Q8_1_MMVQ];+ int u[2*VDR_Q4_0_Q8_1_MMVQ];++#pragma unroll+ for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {+ v[i] = get_int_b2(bq4_0->qs, iqs + i);+ u[2*i+0] = get_int_b4(bq8_1->qs, iqs + i);+ u[2*i+1] = get_int_b4(bq8_1->qs, iqs + i + QI4_0);+ }++ return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);+}+++static __device__ __forceinline__ float vec_dot_q4_1_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq + kbx;++ int v[VDR_Q4_1_Q8_1_MMVQ];+ int u[2*VDR_Q4_1_Q8_1_MMVQ];++#pragma unroll+ for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {+ v[i] = get_int_b4(bq4_1->qs, iqs + i);+ u[2*i+0] = get_int_b4(bq8_1->qs, iqs + i);+ u[2*i+1] = get_int_b4(bq8_1->qs, iqs + i + QI4_1);+ }++ return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);+}++static __device__ __forceinline__ float vec_dot_q5_0_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq + kbx;++ int vl[VDR_Q5_0_Q8_1_MMVQ];+ int vh[VDR_Q5_0_Q8_1_MMVQ];+ int u[2*VDR_Q5_0_Q8_1_MMVQ];++#pragma unroll+ for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {+ vl[i] = get_int_b2(bq5_0->qs, iqs + i);+ vh[i] = get_int_b2(bq5_0->qh, 0) >> (4 * (iqs + i));+ u[2*i+0] = get_int_b4(bq8_1->qs, iqs + i);+ u[2*i+1] = get_int_b4(bq8_1->qs, iqs + i + QI5_0);+ }++ return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);+}++static __device__ __forceinline__ float vec_dot_q5_1_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq + kbx;++ int vl[VDR_Q5_1_Q8_1_MMVQ];+ int vh[VDR_Q5_1_Q8_1_MMVQ];+ int u[2*VDR_Q5_1_Q8_1_MMVQ];++#pragma unroll+ for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {+ vl[i] = get_int_b4(bq5_1->qs, iqs + i);+ vh[i] = get_int_b4(bq5_1->qh, 0) >> (4 * (iqs + i));+ u[2*i+0] = get_int_b4(bq8_1->qs, iqs + i);+ u[2*i+1] = get_int_b4(bq8_1->qs, iqs + i + QI5_1);+ }++ return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);+}++static __device__ __forceinline__ float vec_dot_q8_0_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq + kbx;++ int v[VDR_Q8_0_Q8_1_MMVQ];+ int u[VDR_Q8_0_Q8_1_MMVQ];++#pragma unroll+ for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {+ v[i] = get_int_b2(bq8_0->qs, iqs + i);+ u[i] = get_int_b4(bq8_1->qs, iqs + i);+ }++ return vec_dot_q8_0_q8_1_impl<float, VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d, __low2half(bq8_1->ds));+}++static __device__ __forceinline__ float vec_dot_q2_K_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q2_K * bq2_K = (const block_q2_K *) vbq + kbx;++ const int bq8_offset = QR2_K * (iqs / QI8_1);+ const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);++ const uint8_t * scales = bq2_K->scales + scale_offset;++ const int v = get_int_b4(bq2_K->qs, iqs);+ int u[QR2_K];+ float d8[QR2_K];++#pragma unroll+ for (int i = 0; i < QR2_K; ++ i) {+ u[i] = get_int_b4(bq8_1[bq8_offset + i].qs, iqs % QI8_1);+ d8[i] = __low2float(bq8_1[bq8_offset + i].ds);+ }++ return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);+}++static __device__ __forceinline__ float vec_dot_q3_K_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q3_K * bq3_K = (const block_q3_K *) vbq + kbx;++ const int bq8_offset = QR3_K * (iqs / (QI3_K/2));+ const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);++ const float d = bq3_K->d;++ const int vl = get_int_b2(bq3_K->qs, iqs);++ // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted+ const int vh = ~get_int_b2(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;++ int u[QR3_K];+ float d8[QR3_K];++#pragma unroll+ for (int i = 0; i < QR3_K; ++i) {+ u[i] = get_int_b4(bq8_1[bq8_offset + i].qs, iqs % QI8_1);+ d8[i] = __low2float(bq8_1[bq8_offset + i].ds);+ }++ return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);+}++// The 6-bit scale and min of sub-blocks 2j and 2j + 1 (j in 0..3) of a Q4_K+// block. The twelve scale bytes are read as three words by every lane, and+// both packings are formed before a select: with a branch on j, the third+// word's load was sunk into the j >= 2 side, whose wait then covered every+// load the wave had in flight. The 16-bit words and masks are the usual ones.+static __device__ __forceinline__ void q4_K_scale_pair(+ const block_q4_K * __restrict__ b, const int j, uint16_t * __restrict__ aux) {+ const uint32_t * words = (const uint32_t *)b->scales;+ const int shift = 16*(j & 1);+ const uint32_t lo = (words[0] >> shift) & 0xffff; // scales[j % 2]+ const uint32_t mid = (words[1] >> shift) & 0xffff; // scales[j % 2 + 2]+ const uint32_t hi = (words[2] >> shift) & 0xffff; // scales[j % 2 + 4]+ const uint32_t low0 = lo & 0x3f3f;+ const uint32_t low1 = mid & 0x3f3f;+ const uint32_t high0 = ((hi >> 0) & 0x0f0f) | ((lo & 0xc0c0) >> 2);+ const uint32_t high1 = ((hi >> 4) & 0x0f0f) | ((mid & 0xc0c0) >> 2);+ aux[0] = j < 2 ? low0 : high0;+ aux[1] = j < 2 ? low1 : high1;+}++static __device__ __forceinline__ float vec_dot_q4_K_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q4_K * bq4_K = (const block_q4_K *) vbq + kbx;++ int v[2];+ int u[2*QR4_K];+ float d8[QR4_K];++ // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6+ const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));++ // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12+ // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44+ // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76+ // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108++ const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));+ v[0] = q4[0];+ v[1] = q4[4];++ uint16_t aux[2];+ q4_K_scale_pair(bq4_K, bq8_offset/2, aux);+ const uint8_t * sc = (const uint8_t *)aux;+ const uint8_t * m = sc + 2;++ for (int i = 0; i < QR4_K; ++i) {+ const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;+ d8[i] = __low2float(bq8i->ds);++ const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);+ u[2*i+0] = q8[0];+ u[2*i+1] = q8[4];+ }++ return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);+}++static __device__ __forceinline__ float vec_dot_q5_K_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q5_K * bq5_K = (const block_q5_K *) vbq + kbx;++ int vl[2];+ int vh[2];+ int u[2*QR5_K];+ float d8[QR5_K];++ const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));+ const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));+ const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));++ vl[0] = ql[0];+ vl[1] = ql[4];++ vh[0] = qh[0] >> bq8_offset;+ vh[1] = qh[4] >> bq8_offset;++ const uint16_t * scales = (const uint16_t *)bq5_K->scales;+ uint16_t aux[2];+ const int j = bq8_offset/2;+ if (j < 2) {+ aux[0] = scales[j+0] & 0x3f3f;+ aux[1] = scales[j+2] & 0x3f3f;+ } else {+ aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);+ aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);+ }+ const uint8_t * sc = (const uint8_t *)aux;+ const uint8_t * m = sc + 2;++#pragma unroll+ for (int i = 0; i < QR5_K; ++i) {+ const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;+ d8[i] = __low2float(bq8i->ds);++ const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);+ u[2*i+0] = q8[0];+ u[2*i+1] = q8[4];+ }++ return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);+}++static __device__ __forceinline__ float vec_dot_q6_K_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_q6_K * bq6_K = (const block_q6_K *) vbq + kbx;++ const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);+ const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);+ const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));++ const int vl = get_int_b2(bq6_K->ql, iqs);+ const int vh = get_int_b2(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;++ const int8_t * scales = bq6_K->scales + scale_offset;++ int u[QR6_K];+ float d8[QR6_K];++#pragma unroll+ for (int i = 0; i < QR6_K; ++i) {+ u[i] = get_int_b4(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);+ d8[i] = __low2float(bq8_1[bq8_offset + 2*i].ds);+ }++ return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);+}++#define VDR_IQ2_XXS_Q8_1_MMVQ 2+#define VDR_IQ2_XXS_Q8_1_MMQ 2++static __device__ __forceinline__ float vec_dot_iq2_xxs_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq + kbx;++ const int q2 = get_int_b2(bq2->qs, iqs);+ const uint8_t * aux8 = (const uint8_t *) &q2;+ const uint32_t aux32 = get_int_b2(bq2->qs, iqs + 1);++ int sumi = 0;+#pragma unroll+ for (int k0 = 0; k0 < 8; k0 += 2) {+ const uint2 grid_pos = ((const uint2*)iq2xxs_grid)[aux8[k0/2]];+ const uint32_t signs = unpack_ksigns(aux32 >> (7 * k0 / 2));++ const int signs0 = __vcmpne4(signs & 0x08040201, 0);+ const int grid0 = __vsub4(grid_pos.x ^ signs0, signs0);+ const int u0 = get_int_b4(bq8_1[iqs/2].qs, k0 + 0);+ sumi = ggml_hip_dp4a(grid0, u0, sumi);++ const int signs1 = __vcmpne4(signs & 0x80402010, 0);+ const int grid1 = __vsub4(grid_pos.y ^ signs1, signs1);+ const int u1 = get_int_b4(bq8_1[iqs/2].qs, k0 + 1);+ sumi = ggml_hip_dp4a(grid1, u1, sumi);+ }++ const int ls = aux32 >> 27 | 1; // (scale * 2 + 1)+ sumi = sumi * ls / 8; // (sumi * scale + sumi / 2) / 4+ const float d = __half2float(bq2->d) * __low2float(bq8_1[iqs/2].ds);+ return d * sumi;+}++#define VDR_IQ2_XS_Q8_1_MMVQ 2+#define VDR_IQ2_XS_Q8_1_MMQ 2++static __device__ __forceinline__ float vec_dot_iq2_xs_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq + kbx;++ const int2 q2_packed = make_int2(get_int_b2(bq2->qs, iqs + 0), get_int_b2(bq2->qs, iqs + 1));+ const uint16_t * q2 = (const uint16_t *) &q2_packed;+ const int ls0 = bq2->scales[iqs/2] & 0x0F;+ const int ls1 = bq2->scales[iqs/2] >> 4;++ int sumi0 = 0;+ int sumi1 = 0;+#pragma unroll+ for (int l0 = 0; l0 < 8; l0 += 2) {+ const uint2 grid_pos = ((const uint2*)iq2xs_grid)[q2[l0/2] & 0x1FF];+ const uint32_t signs = unpack_ksigns(q2[l0/2] >> 9);++ const int signs0 = __vcmpne4(signs & 0x08040201, 0);+ const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0);+ const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0);++ const int signs1 = __vcmpne4(signs & 0x80402010, 0);+ const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1);+ const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1);++ if (l0 < 4) {+ sumi0 = ggml_hip_dp4a(grid_l, u0, sumi0);+ sumi0 = ggml_hip_dp4a(grid_h, u1, sumi0);+ } else {+ sumi1 = ggml_hip_dp4a(grid_l, u0, sumi1);+ sumi1 = ggml_hip_dp4a(grid_h, u1, sumi1);+ }+ }+ const int sumi = (sumi0*ls0 + sumi1*ls1 + (sumi0 + sumi1)/2)/4;+ const float d = __half2float(bq2->d) * __low2float(bq8_1[iqs/2].ds);+ return d * sumi;+}++#define VDR_IQ2_S_Q8_1_MMVQ 2+#define VDR_IQ2_S_Q8_1_MMQ 2++static __device__ __forceinline__ float vec_dot_iq2_s_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_iq2_s * bq2 = (const block_iq2_s *) vbq + kbx;++ const int qs_packed = get_int_b2(bq2->qs, iqs/2);+ const uint8_t * qs = (const uint8_t *) &qs_packed;++ const int qh = bq2->qh[iqs/2];++ const int signs_packed_32 = get_int_b2(bq2->qs, QK_K/32 + iqs/2);+ const uint8_t * signs_packed_8 = (const uint8_t *) &signs_packed_32;++ const int ls0 = bq2->scales[iqs/2] & 0x0F;+ const int ls1 = bq2->scales[iqs/2] >> 4;++ int sumi0 = 0;+ int sumi1 = 0;+#pragma unroll+ for (int l0 = 0; l0 < 8; l0 += 2) {+ const int * grid_pos = (const int *)(iq2s_grid + (qs[l0/2] | ((qh << (8-l0)) & 0x300)));++ const int signs0 = __vcmpne4(((signs_packed_8[l0/2] & 0x03) << 7) | ((signs_packed_8[l0/2] & 0x0C) << 21), 0x00000000);+ const int signs1 = __vcmpne4(((signs_packed_8[l0/2] & 0x30) << 3) | ((signs_packed_8[l0/2] & 0xC0) << 17), 0x00000000);++ const int grid_l = __vsub4(grid_pos[0] ^ signs0, signs0);+ const int grid_h = __vsub4(grid_pos[1] ^ signs1, signs1);++ const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0);+ const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1);++ if (l0 < 4) {+ sumi0 = ggml_hip_dp4a(grid_l, u0, sumi0);+ sumi0 = ggml_hip_dp4a(grid_h, u1, sumi0);+ } else {+ sumi1 = ggml_hip_dp4a(grid_l, u0, sumi1);+ sumi1 = ggml_hip_dp4a(grid_h, u1, sumi1);+ }+ }+ const int sumi = (sumi0*ls0 + sumi1*ls1 + (sumi0 + sumi1)/2)/4;++ const float d = __half2float(bq2->d) * __low2float(bq8_1[iqs/2].ds);+ return d * sumi;+}++#define VDR_IQ3_XXS_Q8_1_MMVQ 2+#define VDR_IQ3_XXS_Q8_1_MMQ 2++static __device__ __forceinline__ float vec_dot_iq3_xxs_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_iq3_xxs * bq3 = (const block_iq3_xxs *) vbq + kbx;++ const int2 q3_packed = make_int2(get_int_b2(bq3->qs, iqs), get_int_b2(bq3->qs, iqs+1));+ const uint8_t * q3 = (const uint8_t *) &q3_packed;+ const uint32_t aux32 = get_int_b2(bq3->qs, QK_K/16 + iqs/2);++ int sumi = 0;+#pragma unroll+ for (int l0 = 0; l0 < 8; l0 += 2) {+ const int2 grid_pos = make_int2(iq3xxs_grid[q3[l0 + 0]], iq3xxs_grid[q3[l0 + 1]]);+ const uint32_t signs = unpack_ksigns(aux32 >> (7*l0/2));++ const int signs0 = __vcmpne4(signs & 0x08040201, 0);+ const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0);++ const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0);++ const int signs1 = __vcmpne4(signs & 0x80402010, 0);+ const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1);++ const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1);++ sumi = ggml_hip_dp4a(grid_l, u0, sumi);+ sumi = ggml_hip_dp4a(grid_h, u1, sumi);+ }++ const int ls = aux32 >> 28;+ sumi = (ls*sumi + sumi/2)/2;+ const float d = __half2float(bq3->d) * __low2float(bq8_1[iqs/2].ds);+ return d * sumi;+}++#define VDR_IQ3_S_Q8_1_MMVQ 2+#define VDR_IQ3_S_Q8_1_MMQ 2++// TODO: don't use lookup table for signs+static __device__ __forceinline__ float vec_dot_iq3_s_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_iq3_s * bq3 = (const block_iq3_s *) vbq + kbx;++ const int2 qs_packed = make_int2(get_int_b2(bq3->qs, iqs + 0), get_int_b2(bq3->qs, iqs + 1));+ const uint8_t * qs = (const uint8_t *) &qs_packed;++ const int qh = bq3->qh[iqs/2];++ const int signs_packed_32 = get_int_b2(bq3->signs, iqs/2);+ const uint8_t * signs_packed_8 = (const uint8_t *) &signs_packed_32;++ int sumi = 0;+#pragma unroll+ for (int l0 = 0; l0 < 8; l0 += 2) {+ const int2 grid_pos = make_int2(+ iq3s_grid[qs[l0 + 0] | ((qh << (8 - l0)) & 0x100)],+ iq3s_grid[qs[l0 + 1] | ((qh << (7 - l0)) & 0x100)]);++ const int signs0 = __vcmpne4(((signs_packed_8[l0/2] & 0x03) << 7) | ((signs_packed_8[l0/2] & 0x0C) << 21), 0x00000000);+ const int signs1 = __vcmpne4(((signs_packed_8[l0/2] & 0x30) << 3) | ((signs_packed_8[l0/2] & 0xC0) << 17), 0x00000000);++ const int grid_l = __vsub4(grid_pos.x ^ signs0, signs0);+ const int grid_h = __vsub4(grid_pos.y ^ signs1, signs1);++ const int u0 = get_int_b4(bq8_1[iqs/2].qs, l0 + 0);+ const int u1 = get_int_b4(bq8_1[iqs/2].qs, l0 + 1);++ sumi = ggml_hip_dp4a(grid_l, u0, sumi);+ sumi = ggml_hip_dp4a(grid_h, u1, sumi);+ }++ sumi *= 1 + 2*((bq3->scales[iqs/4] >> ((iqs << 1) & 0x04)) & 0x0F);++ const float d = __half2float(bq3->d) * __low2float(bq8_1[iqs/2].ds);+ return d * sumi;+}++#define VDR_IQ1_S_Q8_1_MMVQ 1+#define VDR_IQ1_S_Q8_1_MMQ 1++static __device__ __forceinline__ float vec_dot_iq1_s_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {+ const block_iq1_s * bq1 = (const block_iq1_s *) vbq + kbx;++ const int qs_packed = get_int_b2(bq1->qs, iqs);+ const uint8_t * qs = (const uint8_t *) &qs_packed;++ const int qh = bq1->qh[iqs];++ int sumi = 0;+#pragma unroll+ for (int l0 = 0; l0 < 8; l0 += 2) {+ const int grid = iq1s_grid_gpu[qs[l0/2] | (((qh >> 3*(l0/2)) & 0x07) << 8)];++ const int grid0 = (grid >> 0) & 0x0F0F0F0F;+ const int grid1 = (grid >> 4) & 0x0F0F0F0F;++ const int u0 = get_int_b4(bq8_1[iqs].qs, l0 + 0);+ const int u1 = get_int_b4(bq8_1[iqs].qs, l0 + 1);++ sumi = ggml_hip_dp4a(grid0, u0, sumi);+ sumi = ggml_hip_dp4a(grid1, u1, sumi);+ }++ const float d1q = __half2float(bq1->d) * (((qh >> 11) & 0x0E) + 1);+ const float delta = -1.0f + IQ1S_DELTA - (qh & 0x8000) * (2.0f*IQ1S_DELTA/0x8000);+ const float2 ds = __half22float2(bq8_1[iqs].ds);+ return d1q * (ds.x*sumi + ds.y*delta);+}++#define VDR_IQ1_M_Q8_1_MMVQ 1+#define VDR_IQ1_M_Q8_1_MMQ 1++static __device__ __forceinline__ float vec_dot_iq1_m_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_iq1_m * bq1 = (const block_iq1_m *) vbq + kbx;++ const int qs_packed = get_int_b4(bq1->qs, iqs);+ const uint8_t * qs = (const uint8_t *) &qs_packed;++ int sumi[2] = {0};+ float sumf[2] = {0.0f};+#pragma unroll+ for (int l0 = 0; l0 < 8; l0 += 2) {+ const int qhl = bq1->qh[2*iqs + l0/4] >> (4 * ((l0/2) % 2));++ const int grid = iq1s_grid_gpu[qs[l0/2] | ((qhl & 0x07) << 8)];++ const int grid0 = (grid >> 0) & 0x0F0F0F0F;+ const int grid1 = (grid >> 4) & 0x0F0F0F0F;++ const int u0 = get_int_b4(bq8_1[iqs].qs, l0 + 0);+ const int u1 = get_int_b4(bq8_1[iqs].qs, l0 + 1);++ sumi[l0/4] = ggml_hip_dp4a(grid0, u0, sumi[l0/4]);+ sumi[l0/4] = ggml_hip_dp4a(grid1, u1, sumi[l0/4]);++ const float delta = -1.0f + IQ1M_DELTA - (qhl & 0x08) * (2.0f*IQ1M_DELTA/0x08);+ int sumy = 0;+ sumy = ggml_hip_dp4a(u0, 0x01010101, sumy);+ sumy = ggml_hip_dp4a(u1, 0x01010101, sumy);+ sumf[l0/4] += delta*sumy;+ }++ const uint16_t * sc = (const uint16_t *) bq1->scales;++ iq1m_scale_t scale;+ scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00F0) | ((sc[2] >> 4) & 0x0F00) | (sc[3] & 0xF000);+ const float d = __half2float(scale.f16) * __low2float(bq8_1[iqs].ds);++ const int tmp = sc[iqs/2] >> (6*(iqs%2));+ const int sc0 = 2*((tmp >> 0) & 0x07) + 1;+ const int sc1 = 2*((tmp >> 3) & 0x07) + 1;+ return d * ((sumi[0] + sumf[0]) * sc0 + (sumi[1] + sumf[1]) * sc1);+}++#define VDR_IQ4_NL_Q8_1_MMVQ 2+#define VDR_IQ4_NL_Q8_1_MMQ 4++static __device__ __forceinline__ float vec_dot_iq4_nl_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_iq4_nl * bq4 = (const block_iq4_nl *) vbq + kbx;++ const int * q8 = (const int *) bq8_1->qs + iqs;++ int sumi = 0;+#pragma unroll+ for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {+ const int aux_q4 = get_int_b2(bq4->qs, iqs + l);+ const int2 v = get_int_from_table_16(aux_q4, kvalues_iq4nl);++ sumi = ggml_hip_dp4a(v.x, q8[l + 0], sumi);+ sumi = ggml_hip_dp4a(v.y, q8[l + 4], sumi);+ }++ const float d = __half2float(bq4->d) * __low2float(bq8_1->ds);+ return d * sumi;+}++#define VDR_IQ4_XS_Q8_1_MMVQ 4+#define VDR_IQ4_XS_Q8_1_MMQ 4++static __device__ __forceinline__ float vec_dot_iq4_xs_q8_1(+ const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & kbx, const int & iqs) {++ const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq + kbx;++ int sumi = 0;+#pragma unroll+ for (int j = 0; j < 4; ++j) {+ const int aux_q4 = get_int_b4(bq4->qs, iqs + j);+ const int2 v = get_int_from_table_16(aux_q4, kvalues_iq4nl);++ const int u0 = get_int_b4(bq8_1[iqs/4].qs, j + 0);+ const int u1 = get_int_b4(bq8_1[iqs/4].qs, j + 4);++ sumi = ggml_hip_dp4a(v.x, u0, sumi);+ sumi = ggml_hip_dp4a(v.y, u1, sumi);+ }++ const int ls = ((bq4->scales_l[iqs/8] >> (iqs & 0x04)) & 0x0F) | (((bq4->scales_h >> (iqs/2)) & 0x03) << 4);+ sumi *= ls - 32;++ const float d = __half2float(bq4->d) * __low2float(bq8_1[iqs/4].ds);+ return d * sumi;+}++// Decode each packed weight fragment once when several requests use an+// expert. Dot retains the original Q4_K/Q8_1 integer and floating sum order.+struct Q4MoeFragment {+ int v[2];+ uint16_t aux[2];+ half2 dm;++ __device__ __forceinline__ Q4MoeFragment(const void* weights, int block,+ int iqs) {+ const auto* w = static_cast<const block_q4_K*>(weights) + block;+ const int offset = QR4_K * ((iqs / 2) / (QI8_1 / 2));+ const auto* q = reinterpret_cast<const int*>(+ w->qs + 16 * offset + 4 * ((iqs / 2) % 4));+ v[0] = q[0];+ v[1] = q[4];+ dm = w->dm;+ q4_K_scale_pair(w, offset / 2, aux);+ }++ __device__ __forceinline__ float Dot(const block_q8_1* x, int iqs) const {+ int u[4];+ float d8[2];+ const int offset = QR4_K * ((iqs / 2) / (QI8_1 / 2));+#pragma unroll+ for (int i = 0; i < 2; ++i) {+ const auto* block = x + offset + i;+ d8[i] = __low2float(block->ds);+ const auto* q = reinterpret_cast<const int*>(block->qs) + ((iqs / 2) % 4);+ u[2 * i] = q[0];+ u[2 * i + 1] = q[4];+ }+ const auto* sc = reinterpret_cast<const uint8_t*>(aux);+ return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, sc + 2, dm, d8);+ }+};diff --git a/exllamav3/exllamav3_ext/rocm/gmmq/vendors/hip.h b/exllamav3/exllamav3_ext/rocm/gmmq/vendors/hip.hnew file mode 100644index 0000000..c7e06ec--- /dev/null+++ b/exllamav3/exllamav3_ext/rocm/gmmq/vendors/hip.h@@ -0,0 +1,142 @@+// SPDX-License-Identifier: MIT+// Adapted from Gufo (github.com/gufo-org/gufo, 07e1191, MIT, Copyright (c) 2026 Gufo contributors), path+// src/models/qwen38_flash_next/kernels/rocm/mmq/vendors/hip.h, itself adapted from llama.cpp 5c0e946 ggml/src/ggml-cuda+// (github.com/ggml-org/llama.cpp, MIT, Copyright (c) 2023-2026 The ggml authors). See THIRD_PARTY_NOTICES.md.+// Changes: none.+#pragma once++#define HIP_DISABLE_WARP_SYNC_BUILTINS 1+#include <hip/hip_runtime.h>+#include <hipblas/hipblas.h>+#include <hip/hip_fp16.h>+#include <hip/hip_bf16.h>++#define __trap() do { abort(); __builtin_unreachable(); } while (0)+++#if !defined(__HIP_PLATFORM_AMD__)+#error "The HIP backend supports only AMD targets"+#endif+++#if defined(__gfx900__) || defined(__gfx906__)+#define GCN5+#endif++#if defined(__gfx803__)+#define GCN4+#endif++#if defined(GCN5) || defined(GCN4)+#define GCN+#endif++#if defined(__gfx950__)+#define CDNA4+#endif++#if defined(__gfx942__)+#define CDNA3+#endif++#if defined(__gfx90a__)+#define CDNA2+#endif++#if defined(__gfx908__)+#define CDNA1+#endif++#if defined(CDNA4) || defined(CDNA3) || defined(CDNA2) || defined(CDNA1)+#define CDNA+#endif++#if defined(__GFX12__)+#define RDNA4+#endif++#if defined(__GFX11__)+#define RDNA3+#endif++#if defined(__gfx1150__) || defined(__gfx1151__)+#define RDNA3_5+#endif++#if defined(RDNA3) && !defined(RDNA3_5)+#define RDNA3_0+#endif++#if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \+ defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)+#define RDNA2+#endif++#if defined(__gfx1010__) || defined(__gfx1012__)+#define RDNA1+#endif++#if defined(RDNA4) || defined(RDNA3) || defined(RDNA2) || defined(RDNA1)+#define RDNA+#endif++#ifndef __has_builtin+#define __has_builtin(x) 0+#endif+++#if HIP_VERSION >= 60200000+#include <hip/hip_fp8.h>+#define FP8_AVAILABLE+#endif++typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));+typedef uint8_t uint8x4_t __attribute__((ext_vector_type(4)));++static __device__ __forceinline__ int __vsubss4(const int a, const int b) {+ const int8x4_t va = reinterpret_cast<const int8x4_t &>(a);+ const int8x4_t vb = reinterpret_cast<const int8x4_t &>(b);+#if __has_builtin(__builtin_elementwise_sub_sat)+ const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);+ return reinterpret_cast<const int &>(c);+#else+ int8x4_t c;+ int16_t tmp;+#pragma unroll+ for (int i = 0; i < 4; i++) {+ tmp = va[i] - vb[i];+ if (tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();+ if (tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();+ c[i] = tmp;+ }+ return reinterpret_cast<int &>(c);+#endif+}++static __device__ __forceinline__ int __vsub4(const int a, const int b) {+ return __vsubss4(a, b);+}++static __device__ __forceinline__ unsigned int __vcmpeq4(unsigned int a, unsigned int b) {+ const uint8x4_t &va = reinterpret_cast<const uint8x4_t &>(a);+ const uint8x4_t &vb = reinterpret_cast<const uint8x4_t &>(b);+ unsigned int c;+ uint8x4_t &vc = reinterpret_cast<uint8x4_t &>(c);+#pragma unroll+ for (int i = 0; i < 4; ++i) {+ vc[i] = va[i] == vb[i] ? 0xff : 0x00;+ }+ return c;+}++static __device__ __forceinline__ unsigned int __vcmpne4(unsigned int a, unsigned int b) {+ const uint8x4_t &va = reinterpret_cast<const uint8x4_t &>(a);+ const uint8x4_t &vb = reinterpret_cast<const uint8x4_t &>(b);+ unsigned int c;+ uint8x4_t &vc = reinterpret_cast<uint8x4_t &>(c);+#pragma unroll+ for (int i = 0; i < 4; ++i) {+ vc[i] = va[i] == vb[i] ? 0x00 : 0xff;+ }+ return c;+}diff --git a/exllamav3/modules/block_sparse_mlp.py b/exllamav3/modules/block_sparse_mlp.pyindex 7c18646..3af6b59 100644--- a/exllamav3/modules/block_sparse_mlp.py+++ b/exllamav3/modules/block_sparse_mlp.py@@ -222,6 +222,9 @@ _MOE_SH_FUSE = os.environ.get("EXL3_MOE_SH_FUSE", "1" if torch.version.hip else_MOE_SH_SAID = [False]# MCPM_FQ_MOE_IQ (default 1): IQ3_XXS / IQ2_S experts take the grouped q8_1 decode MoE (fq_moe_kq.cu FMT 4 / 5)_FQ_MOE_IQ = os.environ.get("MCPM_FQ_MOE_IQ", "1") != "0"+# HIP routed MMQ prefill MoE (rocm/gmmq, from Gufo / llama.cpp), default on with ROCm: Strix Halo Qwen3.8-Flash-Next UD-Q4_K_XL GGUF+# served 8K prefill 13.5-15.8 -> 10.8 s, decode flat, N1 md5 identical, perplexity 3.1671 -> 3.1797. 0 = the grouped Triton path.+_FQ_PF_GMMQ = os.environ.get("MCPM_FQ_PF_GMMQ", "1" if torch.version.hip else "0") == "1"# BOLT_FQ_MMVQ (default 1, CUDA): IQ experts (all 7 IQ formats, gate/up and down) take the llama.cpp mmvq-style slot-parallel decode MoE# (fq_mmvq.cu fq_moe_mmvq_grouped) instead of fq_moe_kq.cu. 0 = the previous kernels (A/B)._MQ_ID = {"iq2_xxs": 0, "iq2_xs": 1, "iq2_s": 2, "iq3_xxs": 3, "iq3_s": 4, "iq4_nl": 5, "iq4_xs": 6}@@ -2158,6 +2161,62 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):ext.fq_mlp_shared_iq2xxs(xr, up.packed.reshape(I, -1, 66), h)return down.forward(h, params)+ def _forward_foreign_prefill_gmmq(self, x, y, fmts, selected_experts, routing_weights, params, out_dtype, eshape):+ """HIP routed MMQ prefill MoE (MCPM_FQ_PF_GMMQ=1, rows >= MCPM_FQ_PF_GMMQ_MIN): llama.cpp's MMQ (q8_1 activations,+ integer dots on the raw GGUF blocks, via Gufo's gfx1151 port, rocm/gmmq) for gate, up and down, with a per-expert+ pointer table; SwiGLU and the routing-weighted sum in torch. Gate/up Q4_K / Q5_K, down Q4_K / Q5_K / Q5_1 / Q8_0.+ Returns the block output or None (the caller falls back)."""+ G = {"q4_k": 12, "q5_k": 13, "q5_1": 7, "q8_0": 8}+ R = y.shape[0]+ if not torch.version.hip or fmts[0] not in ("q4_k", "q5_k") or fmts[1] not in G or self.act_limit or \+ self.activation_fn not in ("silu", None) or R < int(os.environ.get("MCPM_FQ_PF_GMMQ_MIN", "64")):+ return None+ if (self.num_local_experts or self.num_experts) != self.num_experts or not hasattr(ext, "fq_mmq_moe"):+ return None+ top_k = self.num_experts_per_tok+ if tuple(selected_experts.shape) != (R, top_k):+ return None+ E, dev, K, I = self.num_experts, y.device, y.shape[1], self.intermediate_size+ tbl = getattr(self, "_fq_gmmq_ptrs", None)+ if tbl is None or tbl[0] != self.gates[0].packed.data_ptr() or tbl[4] != dev:+ mk = lambda ms: torch.tensor([m.packed.data_ptr() for m in ms], dtype=torch.int64, device=dev)+ tbl = (self.gates[0].packed.data_ptr(), mk(self.gates), mk(self.ups), mk(self.downs), dev)+ self._fq_gmmq_ptrs = tbl+ _, g_p, u_p, d_p, _ = tbl+ yc = y.contiguous()+ ids = selected_experts.to(torch.int32).contiguous()+ # grid hints (Gufo RouteHints): the column grid stops at the largest expert bucket (else every one of the E experts+ # launches tiles over all R * top_k rows), and the tile width minimises sum(ceil(c / w) * (w + 16)) over the buckets+ counts = torch.bincount(ids.view(-1), minlength=E)+ nz = counts[counts > 0].to(torch.int64)+ ws = torch.arange(16, 81, 16, device=dev)+ cost = ((nz[None, :] + ws[:, None] - 1) // ws[:, None] * (ws[:, None] + 16)).sum(1)+ hint = torch.stack((counts.max().to(torch.int64), ws[cost.argmin()])).tolist() # one host sync per layer+ max_rows, cols = int(hint[0]), int(hint[1])+ g = torch.empty((R * top_k, I), dtype=torch.float, device=dev)+ u = torch.empty((R * top_k, I), dtype=torch.float, device=dev)+ ext.fq_mmq_moe(yc, ids, g_p, G[fmts[0]], I, g, cols, max_rows)+ ext.fq_mmq_moe(yc, ids, u_p, G[fmts[0]], I, u, cols, max_rows)+ a = (torch.nn.functional.silu(g) * u).to(torch.half)+ del g, u+ dn = torch.empty((R * top_k, K), dtype=torch.float, device=dev)+ ext.fq_mmq_moe(a, ids.view(-1, 1), d_p, G[fmts[1]], K, dn, cols, max_rows)+ w = routing_weights.to(torch.float).reshape(R, top_k, 1)+ if os.environ.get("MCPM_FQ_PF_GMMQ_CHECK") == "1": # debug: where do non-finite values appear (y, a, dn)+ bad = lambda t: int((~torch.isfinite(t)).sum())+ print(f"gmmq_check R={R} fmts={fmts} y={bad(yc)} a={bad(a)} amax={float(a.float().abs().max()):.1f} "+ f"dn={bad(dn)} dnmax={float(dn.abs().nan_to_num(0).max()):.1f} max_rows={max_rows} cols={cols}", flush=True)+ final_hidden_states = (dn.view(R, top_k, K) * w).sum(1).reshape(eshape)+ if self.shared_experts:+ y_se = self.shared_experts.forward(x, {**params, "moe_valu": True} if _MOE_VALU else params)+ if self.shared_gate:+ ext.add_sigmoid_gate(y_se, self.shared_gate.forward(x, params), final_hidden_states)+ else:+ final_hidden_states += y_se+ if out_dtype is not None:+ final_hidden_states = final_hidden_states.to(out_dtype)+ return final_hidden_states+def _forward_foreign_prefill_grouped(self, x, y, selected_experts, routing_weights,params, out_dtype, eshape):"""MiniCPM IQ4_XS prefill MoE (rows >= MCPM_FQ_PF_GROUPED_MIN): two fused@@ -2171,6 +2230,10 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):return NoneR = y.shape[0]fmts = self._fq_grouped_kq_ok(y)+ if _FQ_PF_GMMQ and fmts is not None:+ r = self._forward_foreign_prefill_gmmq(x, y, fmts, selected_experts, routing_weights, params, out_dtype, eshape)+ if r is not None:+ return r_KQ = {"q4_k": 0, "q5_k": 1, "q6_k": 2}_KQ_DN = {**_KQ, "q8_0": 3, "q5_1": 4} if os.environ.get("MCPM_FQ_PF_DN32", "1") != "0" else _KQ# Q4_K/Q5_K/Q6_K gate-up; down also Q8_0 / Q5_1 (unsloth UD packs: before, those layers took the per-expert loop in prefill)diff --git a/tests/test_hip_gmmq.py b/tests/test_hip_gmmq.pynew file mode 100644index 0000000..f95c226--- /dev/null+++ b/tests/test_hip_gmmq.py@@ -0,0 +1,156 @@+"""Routed (MoE) HIP MMQ (ext.fq_mmq_moe, rocm/gmmq: llama.cpp MMQ via Gufo) vs a torch fp32 reference.++Random raw GGUF blocks (finite fp16 scales) for Q4_K / Q5_K (gate/up shape: K = 2560, M = 640) and Q5_1 / Q8_0 (down+shape: K = 640, M = 2560, K % 256 != 0 so the tail over-read path runs), each expert in its own allocation with a zeroed+tail pad (see rocm/gmmq/gmmq_api.h), random top-k ids. Reference: Kyojin's dequant (ext.fq_dequant_q4_k / q5_k,+foreign_quant.gap_formats.dequant for q5_1 / q8_0) then fp32 matmul. MMQ quantizes the activations to q8_1, so the+gate is max|err| / max|ref| < 2e-2 per case; the error against a q8_1-emulated reference is printed too.+Runs under pytest or as a plain script (ROCm only; skips elsewhere)."""+import os+import sys+import time++import torch++FMTS = { # name: (ggml type id, block values, block bytes, K, M)+ "q4_k": (12, 256, 144, 2560, 640),+ "q5_k": (13, 256, 176, 2560, 640),+ "q5_1": (7, 32, 24, 640, 2560),+ "q8_0": (8, 32, 34, 640, 2560),+}+TOL = 2e-2+PAD = 1024 # zeroed bytes after each expert (>= the MMQ K-tail over-read)+++def _ext():+ if not torch.cuda.is_available() or not torch.version.hip:+ return None+ try:+ import exllamav3_ext as ext+ except ImportError:+ return None+ return ext if hasattr(ext, "fq_mmq_moe") else None+++def _f16_bytes(t):+ return t.to(torch.float16).contiguous().view(torch.uint8).reshape(*t.shape, 2)+++def make_raw(fmt, E, M, K, g):+ """uint8 [E, M, nb * bb] of random blocks with finite fp16 scale fields."""+ _, qk, bb, _, _ = FMTS[fmt]+ nb = K // qk+ raw = torch.randint(0, 256, (E, M, nb, bb), dtype=torch.uint8, generator=g)+ sh = (E, M, nb)+ d = torch.empty(sh).uniform_(0.002, 0.01, generator=g)+ raw[..., 0:2] = _f16_bytes(d)+ if fmt in ("q4_k", "q5_k"):+ raw[..., 2:4] = _f16_bytes(torch.empty(sh).uniform_(0.0, 0.01, generator=g)) # dmin+ elif fmt == "q5_1":+ raw[..., 2:4] = _f16_bytes(torch.empty(sh).uniform_(-0.05, 0.05, generator=g)) # m+ return raw.reshape(E, M, nb * bb)+++def dequant(ext, fmt, raw_e):+ """raw_e uint8 [M, nb * bb] (device) -> fp32 [M, K] with Kyojin's dequant helpers."""+ _, qk, bb, _, _ = FMTS[fmt]+ M, w = raw_e.shape+ if fmt in ("q4_k", "q5_k"):+ out = torch.empty(M, w // bb * qk, dtype=torch.half, device=raw_e.device)+ (ext.fq_dequant_q4_k if fmt == "q4_k" else ext.fq_dequant_q5_k)(raw_e.contiguous(), out)+ return out.float()+ from exllamav3.foreign_quant.gap_formats import dequant as gap_dequant+ return gap_dequant(fmt, raw_e.contiguous()).float()+++def q8_1_emulate(x):+ """Per-32 symmetric int8 round trip of the activations (what quantize_mmq_q8_1 feeds the integer dots)."""+ T, K = x.shape+ xb = x.reshape(T, K // 32, 32)+ d = xb.abs().amax(-1, keepdim=True) / 127.0+ q = torch.where(d > 0, torch.round(xb / torch.where(d > 0, d, torch.ones_like(d))), torch.zeros_like(xb))+ return (q * d).reshape(T, K)+++def run_case(ext, fmt, E, T, n_used, x_dtype, seed, timing=False):+ tid, qk, bb, K, M = FMTS[fmt]+ dev = torch.device("cuda")+ g = torch.Generator().manual_seed(seed)+ raw = make_raw(fmt, E, M, K, g)+ bufs, views = [], []+ for e in range(E): # one allocation per expert (Kyojin stores each expert as its own tensor)+ n = raw[e].numel()+ b = torch.zeros(n + PAD, dtype=torch.uint8, device=dev)+ b[:n] = raw[e].reshape(-1).to(dev)+ bufs.append(b)+ views.append(b[:n].view(M, -1))+ w_ptrs = torch.tensor([v.data_ptr() for v in views], dtype=torch.int64, device=dev)+ x32 = torch.randn(T, K, generator=g)+ ids = torch.stack([torch.randperm(E, generator=g)[:n_used] for _ in range(T)]).to(torch.int32)+ x = x32.to(x_dtype).to(dev)+ xr = x.float()+ out = torch.full((T * n_used, M), float("nan"), dtype=torch.float, device=dev)+ ext.fq_mmq_moe(x, ids.to(dev).contiguous(), w_ptrs, tid, M, out)+ torch.cuda.synchronize()+ ref = torch.zeros(T * n_used, M, dtype=torch.float, device=dev)+ refq = torch.zeros_like(ref)+ xq = q8_1_emulate(xr)+ flat = ids.reshape(-1)+ for e in flat.unique().tolist():+ rows = (flat == e).nonzero().reshape(-1).to(dev) # r = t * n_used + slot+ W = dequant(ext, fmt, views[e])+ tok = rows // n_used+ ref[rows] = xr[tok] @ W.T+ refq[rows] = xq[tok] @ W.T+ scale = ref.abs().max().item()+ err = (out - ref).abs()+ errq = (out - refq).abs().max().item()+ finite = bool(torch.isfinite(out).all().item())+ rel = err.max().item() / scale+ ok = finite and rel < TOL+ msg = (f"{fmt:5s} E={E:3d} T={T:4d} n_used={n_used:2d} x={str(x_dtype).split('.')[-1]:7s} K={K} M={M}: "+ f"max_abs={err.max().item():.4e} max_rel={rel:.3e} mean_rel={err.mean().item() / scale:.3e} "+ f"vs_q8emu_rel={errq / scale:.3e} finite={finite} -> {'PASS' if ok else 'FAIL'}")+ if timing:+ for _ in range(2):+ ext.fq_mmq_moe(x, ids.to(dev).contiguous(), w_ptrs, tid, M, out)+ torch.cuda.synchronize()+ t0 = time.perf_counter()+ for _ in range(10):+ ext.fq_mmq_moe(x, ids.to(dev).contiguous(), w_ptrs, tid, M, out)+ torch.cuda.synchronize()+ msg += f" [{(time.perf_counter() - t0) / 10 * 1e3:.3f} ms/call]"+ print(msg, flush=True)+ return ok+++CASES = [ # (fmt, E, T, n_used, x dtype, timing)+ *[(f, 64, T, 10, torch.float, False) for f in FMTS for T in (1, 37, 300)],+ *[(f, 64, 129, 8, torch.half, False) for f in FMTS],+ *[(f, 512, 512, 10, torch.float, True) for f in FMTS],+]+++def run_all():+ ext = _ext()+ if ext is None:+ print("SKIP: needs a ROCm torch and an exllamav3_ext with fq_mmq_moe")+ return None+ fails = 0+ for i, (fmt, E, T, n_used, dt, timing) in enumerate(CASES):+ fails += not run_case(ext, fmt, E, T, n_used, dt, seed=100 + i, timing=timing)+ print(f"{'PASS' if fails == 0 else 'FAIL'}: {len(CASES) - fails}/{len(CASES)} cases")+ return fails == 0+++def test_hip_gmmq():+ import pytest+ res = run_all()+ if res is None:+ pytest.skip("ROCm-only op (fq_mmq_moe) not available")+ assert res+++if __name__ == "__main__":+ res = run_all()+ sys.exit(0 if res in (True, None) else 1)