Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

HIP: __byte_perm as v_perm_b32 and SWAR __vsub4/__vsubss4/__vcmpne4 (from #262 by ttio2tech)

Niko1221600d73070c5f
diff --git a/docs/AMD_HIP.md b/docs/AMD_HIP.md
index 84ee8a551..00f625d2a 100644
--- a/docs/AMD_HIP.md
+++ b/docs/AMD_HIP.md
@@ -145,7 +145,9 @@ an RX 9070 XT 16 GB and a Radeon AI PRO R9700 32 GB (both gfx1201), a Ryzen 9 39
| RX 9070 XT 16 GB | 4,931 slots, 9.4 GiB | 15.6 GiB | 782 tok/s | 30.8 tok/s | 1,235 tok/s | 35.4 tok/s |
| R9700, TheRock 7.10 wheels | 12,288 slots | | 957 tok/s | 44.2 tok/s | 1,284 tok/s | 43.9 tok/s |
- The engine's resident memory was about 26 GB in every run.
+ The engine's resident memory was about 26 GB in every run. Since 0.1.31 `__byte_perm` is one `v_perm_b32` and the
+ packed byte subtracts/compare work on four lanes at once (#262, ttio2tech): decode +15% on the R9700 (46.0 -> 53.0
+ tok/s on a 4K prompt, 52.0 -> 60.5 warm) and +5-7% on the 9070 XT, prompts unchanged, the same tokens.
- **hipBLASLt:** there is no gfx1201 table in `tools/hip`. A table calibrated on the R9700 at the engine's shapes
(hipBLASLt 1.4.1; 0.98-1.76x per GEMM over hipBLAS) changed the end-to-end prompt speed by 0-3%, within noise,
so none is shipped: on gfx1201 the plain hipBLAS path is already close.
diff --git a/include/strata/hip_compat/intrinsics.hpp b/include/strata/hip_compat/intrinsics.hpp
index b1d7378c2..9e6eda74b 100644
--- a/include/strata/hip_compat/intrinsics.hpp
+++ b/include/strata/hip_compat/intrinsics.hpp
@@ -32,45 +32,46 @@ __device__ __forceinline__ int dp4a(int a, int b, int c) {
#endif
}
-// CUDA's packed byte subtract wraps independently in each unsigned byte lane.
+// CUDA's __byte_perm (default mode): result byte i is byte s.nibble[i] & 7 of the pair {y:x}, x the low word.
+// HIP's own version indexes a byte array in private memory, which becomes scratch traffic in the i-quant table
+// lookups; v_perm_b32 is the same selection in one instruction (selector bytes 0-3 pick from its second
+// operand, 4-7 from its first), so the nibble selector is spread to bytes and masked to 0-7.
+__device__ __forceinline__ uint32_t byte_perm(uint32_t x, uint32_t y, uint32_t s) {
+ const uint32_t sel = (s & 0x7u) | ((s & 0x70u) << 4) | ((s & 0x700u) << 8) | ((s & 0x7000u) << 12);
+ return __builtin_amdgcn_perm(y, x, sel);
+}
+
+// The packed byte operations below work on all four lanes at once (SWAR, Hacker's Delight 2-18): gfx11/gfx12 have
+// no packed 8-bit subtract, and a per-lane loop costs the Q3_K/Q6_K dots and the i-quant sign expansion several
+// times the instructions. kHigh is each lane's top bit.
+constexpr uint32_t kHigh = 0x80808080u;
+
+// CUDA's packed byte subtract wraps independently in each unsigned byte lane: subtract the low seven bits with the
+// minuend's top bit forced on (so no borrow crosses a lane), then fix each top bit by XOR.
__device__ __forceinline__ int vsub4(int a, int b) {
const uint32_t ua = static_cast<uint32_t>(a);
const uint32_t ub = static_cast<uint32_t>(b);
- uint32_t out = 0;
-#pragma unroll
- for (int lane = 0; lane < 4; ++lane) {
- const uint32_t x = (ua >> (lane * 8)) & 0xffu;
- const uint32_t y = (ub >> (lane * 8)) & 0xffu;
- out |= ((x - y) & 0xffu) << (lane * 8);
- }
- return static_cast<int>(out);
+ return static_cast<int>(((ua | kHigh) - (ub & ~kHigh)) ^ ((ua ^ ~ub) & kHigh));
}
-// CUDA's packed signed-byte saturating subtract.
+// CUDA's packed signed-byte saturating subtract: the wrapping difference, and in each lane that overflowed (operand
+// signs differ and the result's sign differs from the minuend's) the bound on the minuend's side, 0x7f or 0x80.
__device__ __forceinline__ int vsubss4(int a, int b) {
const uint32_t ua = static_cast<uint32_t>(a);
const uint32_t ub = static_cast<uint32_t>(b);
- uint32_t out = 0;
-#pragma unroll
- for (int lane = 0; lane < 4; ++lane) {
- int value = signed_byte(ua, lane) - signed_byte(ub, lane);
- value = value < -128 ? -128 : (value > 127 ? 127 : value);
- out |= (static_cast<uint32_t>(value) & 0xffu) << (lane * 8);
- }
- return static_cast<int>(out);
+ const uint32_t d = static_cast<uint32_t>(vsub4(a, b));
+ const uint32_t overflow = (ua ^ ub) & (ua ^ d) & kHigh;
+ const uint32_t mask = (overflow >> 7) * 0xffu;
+ const uint32_t bound = 0x7f7f7f7fu + ((ua & kHigh) >> 7);
+ return static_cast<int>((d & ~mask) | (bound & mask));
}
-// CUDA's four-lane byte compare, returning 0xff for each unequal lane and 0 otherwise.
+// CUDA's four-lane byte compare, returning 0xff for each unequal lane and 0 otherwise: a lane of a ^ b is nonzero
+// when its low seven bits carry into the top bit on adding 0x7f, or its top bit is already set.
__device__ __forceinline__ int vcmpne4(int a, int b) {
- const uint32_t ua = static_cast<uint32_t>(a);
- const uint32_t ub = static_cast<uint32_t>(b);
- uint32_t out = 0;
-#pragma unroll
- for (int lane = 0; lane < 4; ++lane) {
- if (((ua >> (lane * 8)) & 0xffu) != ((ub >> (lane * 8)) & 0xffu))
- out |= 0xffu << (lane * 8);
- }
- return static_cast<int>(out);
+ const uint32_t t = static_cast<uint32_t>(a) ^ static_cast<uint32_t>(b);
+ const uint32_t nonzero = (((t & ~kHigh) + ~kHigh) | t) & kHigh;
+ return static_cast<int>((nonzero >> 7) * 0xffu);
}
// CUDA's mask argument describes participating lanes. The current kernel set uses full
@@ -111,6 +112,7 @@ __device__ __forceinline__ unsigned ballot_sync(uint32_t mask, int predicate) {
} // namespace strata::hip_compat
#define __dp4a(a, b, c) (::strata::hip_compat::dp4a((a), (b), (c)))
+#define __byte_perm(x, y, s) (::strata::hip_compat::byte_perm((x), (y), (s)))
#define __vsub4(a, b) (::strata::hip_compat::vsub4((a), (b)))
#define __vsubss4(a, b) (::strata::hip_compat::vsubss4((a), (b)))
#define __vcmpne4(a, b) (::strata::hip_compat::vcmpne4((a), (b)))
diff --git a/tests/hip/intrinsics.cpp b/tests/hip/intrinsics.cpp
index e07c56797..ff2b93e39 100644
--- a/tests/hip/intrinsics.cpp
+++ b/tests/hip/intrinsics.cpp
@@ -50,6 +50,29 @@ __global__ void intrinsic_probe(int* integer_results, float* float_shuffle, doub
}
}
+// The byte-lane intrinsics over many operands: __byte_perm with selector bits it ignores (bit 3 of each nibble and
+// the upper half), since the i-quant table lookups pass data-dependent selectors; the packed subtracts and compare
+// on every sign and overflow combination (the compare's second operand keeps two lanes of the first).
+constexpr int kPermCases = 1024;
+constexpr int kPermResults = 4;
+__global__ void byte_perm_probe(const uint32_t* inputs, uint32_t* results) {
+ const int i = static_cast<int>(blockIdx.x * blockDim.x + threadIdx.x);
+ if (i >= kPermCases) return;
+ const uint32_t x = inputs[3 * i], y = inputs[3 * i + 1], s = inputs[3 * i + 2];
+ results[kPermResults * i + 0] = __byte_perm(x, y, s);
+ results[kPermResults * i + 1] = static_cast<uint32_t>(__vsub4(static_cast<int>(x), static_cast<int>(y)));
+ results[kPermResults * i + 2] = static_cast<uint32_t>(__vsubss4(static_cast<int>(x), static_cast<int>(y)));
+ results[kPermResults * i + 3] =
+ static_cast<uint32_t>(__vcmpne4(static_cast<int>(x), static_cast<int>(x ^ (s & 0xff00ff00u))));
+}
+
+uint32_t byte_perm_reference(uint32_t x, uint32_t y, uint32_t s) {
+ const uint64_t pair = (static_cast<uint64_t>(y) << 32) | x;
+ uint32_t out = 0;
+ for (int i = 0; i < 4; ++i) out |= static_cast<uint32_t>((pair >> (8 * ((s >> (4 * i)) & 7u))) & 0xffu) << (8 * i);
+ return out;
+}
+
int signed_byte(uint32_t word, int lane) {
const unsigned value = (word >> (lane * 8)) & 0xffu;
return value < 0x80u ? static_cast<int>(value) : static_cast<int>(value) - 0x100;
@@ -153,6 +176,32 @@ int main() {
ok = ok && double_shuffle[lane] == static_cast<double>(lane ^ 1) + 0.25;
}
+ uint32_t perm_inputs[kPermCases * 3]{};
+ uint32_t state = 0x9e3779b9u;
+ for (uint32_t& v : perm_inputs) {
+ state ^= state << 13; state ^= state >> 17; state ^= state << 5;
+ v = state;
+ }
+ uint32_t* device_perm_inputs = nullptr;
+ uint32_t* device_perm_results = nullptr;
+ CHECK(hipMalloc(reinterpret_cast<void**>(&device_perm_inputs), sizeof(perm_inputs)));
+ CHECK(hipMalloc(reinterpret_cast<void**>(&device_perm_results), kPermCases * kPermResults * sizeof(uint32_t)));
+ CHECK(hipMemcpy(device_perm_inputs, perm_inputs, sizeof(perm_inputs), hipMemcpyHostToDevice));
+ hipLaunchKernelGGL(byte_perm_probe, dim3(kPermCases / 256), dim3(256), 0, 0, device_perm_inputs, device_perm_results);
+ CHECK(hipDeviceSynchronize());
+ uint32_t perm_results[kPermCases * kPermResults]{};
+ CHECK(hipMemcpy(perm_results, device_perm_results, sizeof(perm_results), hipMemcpyDeviceToHost));
+ for (int i = 0; i < kPermCases; ++i) {
+ const uint32_t x = perm_inputs[3 * i], y = perm_inputs[3 * i + 1], s = perm_inputs[3 * i + 2];
+ const uint32_t* r = perm_results + kPermResults * i;
+ ok = ok && r[0] == byte_perm_reference(x, y, s);
+ ok = ok && r[1] == byte_sub_reference(x, y);
+ ok = ok && r[2] == signed_saturating_sub_reference(x, y);
+ ok = ok && r[3] == not_equal_reference(x, x ^ (s & 0xff00ff00u));
+ }
+ CHECK(hipFree(device_perm_results));
+ CHECK(hipFree(device_perm_inputs));
+
CHECK(hipFree(device_ballot));
CHECK(hipFree(device_dot_inputs));
CHECK(hipFree(device_double_shuffle));
@@ -162,6 +211,6 @@ int main() {
std::fprintf(stderr, "HIP intrinsic parity failed (ballot 0x%08x)\n", ballot);
return 1;
}
- std::puts("HIP intrinsics parity OK: dynamic signed dot4/overflow, packed integer boundaries, wave32 shuffles, ballot and sleep");
+ std::puts("HIP intrinsics parity OK: dynamic signed dot4/overflow, byte permute, packed integer boundaries, wave32 shuffles, ballot and sleep");
return 0;
}