okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-old' into f-139-relbd34d0d87

main
11.8 KiB236 linesraw
1// Numerical parity for the CUDA intrinsics supplied by the HIP compatibility layer.
2#include <hip/hip_runtime.h>
3#include "strata/hip_compat/intrinsics.hpp"
4
5#include <cstdint>
6#include <cstdio>
7#include <limits>
8
9#define CHECK(call)                                                                                                  \
10    do {                                                                                                             \
11        const hipError_t error = (call);                                                                             \
12        if (error != hipSuccess) {                                                                                   \
13            std::fprintf(stderr, "%s: %s\n", #call, hipGetErrorString(error));                                     \
14            return 2;                                                                                                \
15        }                                                                                                            \
16    } while (0)
17
18namespace {
19
20constexpr int kLanes = 32;
21constexpr int kIntegerResults = 10;
22
23__global__ void intrinsic_probe(int* integer_results, float* float_shuffle, double* double_shuffle,
24                                unsigned* ballot, const uint32_t* dot_inputs) {
25    const int lane = static_cast<int>(threadIdx.x);
26    constexpr unsigned mask = 0xffffffffu;
27    const uint32_t a = 0x80ff017fu;
28    const uint32_t b = 0x0203fe81u;
29
30    integer_results[lane * kIntegerResults + 0] = __dp4a(static_cast<int>(a), static_cast<int>(b), 7);
31    integer_results[lane * kIntegerResults + 1] = __dp4a(0x7f7f7f7f, 0x7f7f7f7f, 0x7fffffff);
32    integer_results[lane * kIntegerResults + 2] = __vsub4(0x00ff0102, 0x01010103);
33    integer_results[lane * kIntegerResults + 3] = __vsub4(0x00000000, 0x01020304);
34    integer_results[lane * kIntegerResults + 4] = __vsubss4(0x807f0080, 0x017f0180);
35    integer_results[lane * kIntegerResults + 5] = __vcmpne4(0x00ff0102, 0x01010103);
36    integer_results[lane * kIntegerResults + 6] =
37        __dp4a(static_cast<int>(dot_inputs[lane * 2]), static_cast<int>(dot_inputs[lane * 2 + 1]),
38               0x7fffffff - lane);
39
40    __shared__ int exchanged[kLanes];
41    int sum = 0;
42    for (int iteration = 0; iteration < 64; ++iteration) {
43        exchanged[lane] = static_cast<int>(dot_inputs[lane * 2] & 255u) + iteration;
44        __syncwarp();
45        sum += exchanged[lane ^ 1];
46        __syncwarp();
47    }
48    integer_results[lane * kIntegerResults + 7] = sum;
49    const int input_a = static_cast<int>(dot_inputs[lane * 2]);
50    const int input_b = static_cast<int>(dot_inputs[lane * 2 + 1]);
51    integer_results[lane * kIntegerResults + 8] = __dp4a(input_a, input_b, input_a);
52    integer_results[lane * kIntegerResults + 9] = __dp4a(input_a, input_b, input_b);
53
54    float_shuffle[lane * 4 + 0] = __shfl_xor_sync(mask, static_cast<float>(lane), 1);
55    float_shuffle[lane * 4 + 1] = static_cast<float>(__shfl_down_sync(mask, lane, 4, 8));
56    float_shuffle[lane * 4 + 2] = static_cast<float>(__shfl_up_sync(mask, lane, 1));
57    float_shuffle[lane * 4 + 3] = static_cast<float>(__shfl_sync(mask, lane, 0));
58    double_shuffle[lane] = __shfl_xor_sync(mask, static_cast<double>(lane) + 0.25, 1);
59
60    const unsigned hit = __ballot_sync(mask, (lane & 1) == 0);
61    if (lane == 0) {
62        *ballot = hit;
63        __nanosleep(100);
64    }
65}
66
67// The byte-lane intrinsics over many operands: __byte_perm with selector bits it ignores (bit 3 of each nibble and
68// the upper half), since the i-quant table lookups pass data-dependent selectors; the packed subtracts and compare
69// on every sign and overflow combination (the compare's second operand keeps two lanes of the first).
70constexpr int kPermCases = 1024;
71constexpr int kPermResults = 4;
72__global__ void byte_perm_probe(const uint32_t* inputs, uint32_t* results) {
73    const int i = static_cast<int>(blockIdx.x * blockDim.x + threadIdx.x);
74    if (i >= kPermCases) return;
75    const uint32_t x = inputs[3 * i], y = inputs[3 * i + 1], s = inputs[3 * i + 2];
76    results[kPermResults * i + 0] = __byte_perm(x, y, s);
77    results[kPermResults * i + 1] = static_cast<uint32_t>(__vsub4(static_cast<int>(x), static_cast<int>(y)));
78    results[kPermResults * i + 2] = static_cast<uint32_t>(__vsubss4(static_cast<int>(x), static_cast<int>(y)));
79    results[kPermResults * i + 3] =
80        static_cast<uint32_t>(__vcmpne4(static_cast<int>(x), static_cast<int>(x ^ (s & 0xff00ff00u))));
81}
82
83uint32_t byte_perm_reference(uint32_t x, uint32_t y, uint32_t s) {
84    const uint64_t pair = (static_cast<uint64_t>(y) << 32) | x;
85    uint32_t out = 0;
86    for (int i = 0; i < 4; ++i) out |= static_cast<uint32_t>((pair >> (8 * ((s >> (4 * i)) & 7u))) & 0xffu) << (8 * i);
87    return out;
88}
89
90int signed_byte(uint32_t word, int lane) {
91    const unsigned value = (word >> (lane * 8)) & 0xffu;
92    return value < 0x80u ? static_cast<int>(value) : static_cast<int>(value) - 0x100;
93}
94
95int dp4a_reference(uint32_t a, uint32_t b, int c) {
96    int64_t sum = c;
97    for (int lane = 0; lane < 4; ++lane) sum += signed_byte(a, lane) * signed_byte(b, lane);
98    return static_cast<int32_t>(static_cast<uint32_t>(sum));
99}
100
101uint32_t byte_sub_reference(uint32_t a, uint32_t b) {
102    uint32_t out = 0;
103    for (int lane = 0; lane < 4; ++lane)
104        out |= ((((a >> (lane * 8)) & 0xffu) - ((b >> (lane * 8)) & 0xffu)) & 0xffu) << (lane * 8);
105    return out;
106}
107
108uint32_t signed_saturating_sub_reference(uint32_t a, uint32_t b) {
109    uint32_t out = 0;
110    for (int lane = 0; lane < 4; ++lane) {
111        int value = signed_byte(a, lane) - signed_byte(b, lane);
112        if (value < -128) value = -128;
113        if (value > 127) value = 127;
114        out |= (static_cast<uint32_t>(value) & 0xffu) << (lane * 8);
115    }
116    return out;
117}
118
119uint32_t not_equal_reference(uint32_t a, uint32_t b) {
120    uint32_t out = 0;
121    for (int lane = 0; lane < 4; ++lane)
122        if (((a >> (lane * 8)) & 0xffu) != ((b >> (lane * 8)) & 0xffu)) out |= 0xffu << (lane * 8);
123    return out;
124}
125
126}  // namespace
127
128int main() {
129    int device = 0;
130    CHECK(hipGetDevice(&device));
131    hipDeviceProp_t properties{};
132    CHECK(hipGetDeviceProperties(&properties, device));
133    if (properties.warpSize != kLanes) {
134        std::fprintf(stderr, "expected wave32, got wave%d\n", properties.warpSize);
135        return 1;
136    }
137
138    int* device_integers = nullptr;
139    float* device_float_shuffle = nullptr;
140    double* device_double_shuffle = nullptr;
141    unsigned* device_ballot = nullptr;
142    uint32_t* device_dot_inputs = nullptr;
143    CHECK(hipMalloc(reinterpret_cast<void**>(&device_integers), kLanes * kIntegerResults * sizeof(int)));
144    CHECK(hipMalloc(reinterpret_cast<void**>(&device_float_shuffle), kLanes * 4 * sizeof(float)));
145    CHECK(hipMalloc(reinterpret_cast<void**>(&device_double_shuffle), kLanes * sizeof(double)));
146    CHECK(hipMalloc(reinterpret_cast<void**>(&device_ballot), sizeof(unsigned)));
147    CHECK(hipMalloc(reinterpret_cast<void**>(&device_dot_inputs), kLanes * 2 * sizeof(uint32_t)));
148
149    uint32_t dot_inputs[kLanes * 2]{};
150    for (int lane = 0; lane < kLanes; ++lane) {
151        dot_inputs[lane * 2] = 0x80ff017fu ^ (0x01010101u * static_cast<uint32_t>(lane));
152        dot_inputs[lane * 2 + 1] = 0x0203fe81u + (0x11111111u * static_cast<uint32_t>(lane));
153    }
154    CHECK(hipMemcpy(device_dot_inputs, dot_inputs, sizeof(dot_inputs), hipMemcpyHostToDevice));
155
156    hipLaunchKernelGGL(intrinsic_probe, dim3(1), dim3(kLanes), 0, 0,
157                       device_integers, device_float_shuffle, device_double_shuffle, device_ballot, device_dot_inputs);
158    CHECK(hipDeviceSynchronize());
159
160    int integers[kLanes * kIntegerResults]{};
161    float float_shuffle[kLanes * 4]{};
162    double double_shuffle[kLanes]{};
163    unsigned ballot = 0;
164    CHECK(hipMemcpy(integers, device_integers, sizeof(integers), hipMemcpyDeviceToHost));
165    CHECK(hipMemcpy(float_shuffle, device_float_shuffle, sizeof(float_shuffle), hipMemcpyDeviceToHost));
166    CHECK(hipMemcpy(double_shuffle, device_double_shuffle, sizeof(double_shuffle), hipMemcpyDeviceToHost));
167    CHECK(hipMemcpy(&ballot, device_ballot, sizeof(ballot), hipMemcpyDeviceToHost));
168
169    constexpr uint32_t a = 0x80ff017fu, b = 0x0203fe81u;
170    const uint32_t expected_integer[6] = {
171        static_cast<uint32_t>(dp4a_reference(a, b, 7)),
172        static_cast<uint32_t>(dp4a_reference(0x7f7f7f7fu, 0x7f7f7f7fu, std::numeric_limits<int>::max())),
173        byte_sub_reference(0x00ff0102u, 0x01010103u),
174        byte_sub_reference(0x00000000u, 0x01020304u),
175        signed_saturating_sub_reference(0x807f0080u, 0x017f0180u),
176        not_equal_reference(0x00ff0102u, 0x01010103u),
177    };
178    bool ok = ballot == 0x55555555u;
179    for (int lane = 0; lane < kLanes; ++lane) {
180        for (int i = 0; i < 6; ++i)
181            ok = ok && static_cast<uint32_t>(integers[lane * kIntegerResults + i]) == expected_integer[i];
182        ok = ok && static_cast<uint32_t>(integers[lane * kIntegerResults + 6]) ==
183                     static_cast<uint32_t>(dp4a_reference(dot_inputs[lane * 2], dot_inputs[lane * 2 + 1],
184                                                          0x7fffffff - lane));
185        ok = ok && integers[lane * kIntegerResults + 7] ==
186                     64 * static_cast<int>(dot_inputs[(lane ^ 1) * 2] & 255u) + 2016;
187        for (int alias = 0; alias < 2; ++alias)
188            ok = ok && static_cast<uint32_t>(integers[lane * kIntegerResults + 8 + alias]) ==
189                static_cast<uint32_t>(dp4a_reference(dot_inputs[lane * 2], dot_inputs[lane * 2 + 1],
190                    static_cast<int>(dot_inputs[lane * 2 + alias])));
191        ok = ok && float_shuffle[lane * 4 + 0] == static_cast<float>(lane ^ 1);
192        const int down = lane % 8 < 4 ? lane + 4 : lane;
193        ok = ok && float_shuffle[lane * 4 + 1] == static_cast<float>(down);
194        ok = ok && float_shuffle[lane * 4 + 2] == static_cast<float>(lane == 0 ? 0 : lane - 1);
195        ok = ok && float_shuffle[lane * 4 + 3] == 0.0f;
196        ok = ok && double_shuffle[lane] == static_cast<double>(lane ^ 1) + 0.25;
197    }
198
199    uint32_t perm_inputs[kPermCases * 3]{};
200    uint32_t state = 0x9e3779b9u;
201    for (uint32_t& v : perm_inputs) {
202        state ^= state << 13; state ^= state >> 17; state ^= state << 5;
203        v = state;
204    }
205    uint32_t* device_perm_inputs = nullptr;
206    uint32_t* device_perm_results = nullptr;
207    CHECK(hipMalloc(reinterpret_cast<void**>(&device_perm_inputs), sizeof(perm_inputs)));
208    CHECK(hipMalloc(reinterpret_cast<void**>(&device_perm_results), kPermCases * kPermResults * sizeof(uint32_t)));
209    CHECK(hipMemcpy(device_perm_inputs, perm_inputs, sizeof(perm_inputs), hipMemcpyHostToDevice));
210    hipLaunchKernelGGL(byte_perm_probe, dim3(kPermCases / 256), dim3(256), 0, 0, device_perm_inputs, device_perm_results);
211    CHECK(hipDeviceSynchronize());
212    uint32_t perm_results[kPermCases * kPermResults]{};
213    CHECK(hipMemcpy(perm_results, device_perm_results, sizeof(perm_results), hipMemcpyDeviceToHost));
214    for (int i = 0; i < kPermCases; ++i) {
215        const uint32_t x = perm_inputs[3 * i], y = perm_inputs[3 * i + 1], s = perm_inputs[3 * i + 2];
216        const uint32_t* r = perm_results + kPermResults * i;
217        ok = ok && r[0] == byte_perm_reference(x, y, s);
218        ok = ok && r[1] == byte_sub_reference(x, y);
219        ok = ok && r[2] == signed_saturating_sub_reference(x, y);
220        ok = ok && r[3] == not_equal_reference(x, x ^ (s & 0xff00ff00u));
221    }
222    CHECK(hipFree(device_perm_results));
223    CHECK(hipFree(device_perm_inputs));
224
225    CHECK(hipFree(device_ballot));
226    CHECK(hipFree(device_dot_inputs));
227    CHECK(hipFree(device_double_shuffle));
228    CHECK(hipFree(device_float_shuffle));
229    CHECK(hipFree(device_integers));
230    if (!ok) {
231        std::fprintf(stderr, "HIP intrinsic parity failed (ballot 0x%08x)\n", ballot);
232        return 1;
233    }
234    std::puts("HIP intrinsics parity OK: dynamic signed dot4/overflow, byte permute, packed integer boundaries, wave32 shuffles, shared-memory exchange, ballot and sleep");
235    return 0;
236}