okami.mom/strata

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

Niko1221Merge the AMD HIP backend (PR #121 by Konstantinos Korres, rebased onto 0.1.24) and its setup support36ef9d6e6

main
5.1 KiB95 linesraw
1// Device parity for the scalar HIP implementation of the optional native QSA scorer.
2#include <hip/hip_runtime.h>
3#include "strata/kernels/native_qsa_score.hpp"
4
5#include <cmath>
6#include <cstdio>
7#include <vector>
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
18int main() {
19    using namespace strata::kernels;
20    constexpr int kDim = 128, kHeads = 4, kBlock = 4;
21    constexpr int kMaxCells = 8, kMaxBlocks = 3, kCells = 7;
22    const QsaShapes shapes = qsa_real_shapes();
23
24    std::vector<float> pooled(kMaxBlocks * kDim), query(kHeads * kDim), bias(kMaxBlocks),
25        scores(kMaxCells, -123.0f), expected(kMaxCells, -123.0f);
26    const float head_sign[kHeads] = {1.0f, -1.0f, 1.0f, -1.0f};
27    for (int row = 0; row < kMaxBlocks; ++row)
28        for (int d = 0; d < kDim; ++d)
29            pooled[row * kDim + d] = static_cast<float>(row + 1) * (1.0f + static_cast<float>(d % 7) / 8.0f) / 128.0f;
30    for (int head = 0; head < kHeads; ++head)
31        for (int d = 0; d < kDim; ++d)
32            query[head * kDim + d] = head_sign[head] * (1.0f + static_cast<float>((head + d) % 5) / 16.0f) / 16.0f;
33    for (int row = 0; row < kMaxBlocks; ++row) bias[row] = (static_cast<float>(row) - 1.0f) / 8.0f;
34
35    int32_t step[kStepCount] = {kCells - 1, kCells, kCells / kBlock, static_cast<int32_t>(kCells)};
36    bool saw_positive_head = false, saw_negative_head = false;
37    for (int row = 0; row <= step[kStepNBid]; ++row) {
38        float sum = 0.0f;
39        for (int head = 0; head < kHeads; ++head) {
40            float dot = 0.0f;
41            for (int d = 0; d < kDim; ++d)
42                dot = std::fma(pooled[row * kDim + d], query[head * kDim + d], dot);
43            saw_positive_head = saw_positive_head || dot > 0.0f;
44            saw_negative_head = saw_negative_head || dot < 0.0f;
45            sum = sum + (dot > 0.0f ? dot : 0.0f);
46        }
47        sum = sum + bias[row];
48        if (row == step[kStepNBid] && kCells % kBlock != 0) sum = sum + 1.0e9f;
49        const int lo = row * kBlock;
50        const int hi = (row + 1) * kBlock < kCells ? (row + 1) * kBlock : kCells;
51        for (int cell = lo; cell < hi; ++cell) expected[cell] = sum;
52    }
53    if (!saw_positive_head || !saw_negative_head) {
54        std::fprintf(stderr, "fixture did not exercise both positive and negative head scores\n");
55        return 1;
56    }
57
58    float *device_pooled = nullptr, *device_query = nullptr, *device_bias = nullptr, *device_scores = nullptr;
59    int32_t* device_step = nullptr;
60    CHECK(hipMalloc(reinterpret_cast<void**>(&device_pooled), pooled.size() * sizeof(float)));
61    CHECK(hipMalloc(reinterpret_cast<void**>(&device_query), query.size() * sizeof(float)));
62    CHECK(hipMalloc(reinterpret_cast<void**>(&device_bias), bias.size() * sizeof(float)));
63    CHECK(hipMalloc(reinterpret_cast<void**>(&device_scores), scores.size() * sizeof(float)));
64    CHECK(hipMalloc(reinterpret_cast<void**>(&device_step), sizeof(step)));
65    CHECK(hipMemcpy(device_pooled, pooled.data(), pooled.size() * sizeof(float), hipMemcpyHostToDevice));
66    CHECK(hipMemcpy(device_query, query.data(), query.size() * sizeof(float), hipMemcpyHostToDevice));
67    CHECK(hipMemcpy(device_bias, bias.data(), bias.size() * sizeof(float), hipMemcpyHostToDevice));
68    CHECK(hipMemcpy(device_scores, scores.data(), scores.size() * sizeof(float), hipMemcpyHostToDevice));
69    CHECK(hipMemcpy(device_step, step, sizeof(step), hipMemcpyHostToDevice));
70
71    hipStream_t stream;
72    CHECK(hipStreamCreate(&stream));
73    native_qsa_score(device_pooled, device_query, device_bias, shapes, device_step, kMaxBlocks, kMaxCells,
74                     device_scores, static_cast<void*>(stream));
75    CHECK(hipStreamSynchronize(stream));
76    CHECK(hipMemcpy(scores.data(), device_scores, scores.size() * sizeof(float), hipMemcpyDeviceToHost));
77
78    bool ok = true;
79    for (int cell = 0; cell < kMaxCells; ++cell) {
80        if (scores[cell] != expected[cell]) {
81            std::fprintf(stderr, "score[%d] got %.9g, expected %.9g\n", cell, scores[cell], expected[cell]);
82            ok = false;
83        }
84    }
85
86    CHECK(hipStreamDestroy(stream));
87    CHECK(hipFree(device_step));
88    CHECK(hipFree(device_scores));
89    CHECK(hipFree(device_bias));
90    CHECK(hipFree(device_query));
91    CHECK(hipFree(device_pooled));
92    if (!ok) return 1;
93    std::puts("native QSA HIP scalar parity OK: FMA order, per-head ReLU, bias, partial tail and padding");
94    return 0;
95}