mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 23:08:36 +00:00
perf(inkling2/sycl): spread the one-token loop-controller, attention-residual mix, router and small F32 kernels over the GPU, bit for bit the same results
On the Arc Pro B70 a decode step spends 16.5 ms of 45 in the attention-residual mix / loop-controller phase (10.6 ms) and the router phase (5.9 ms): kernels that run as one work-group (or a handful) per token, so one Xe core of 32 does the work and the step waits on its latency. Every change below keeps the arithmetic of each value, so the logits do not move (checked bit for bit). - k_ctrl_apply_norm_sg16: the gated mix runs one thread per channel over D/64 work-groups (the same 64-term dots in the same order); the RMS norm is a second kernel, one group per token, adding the squares in the order the single kernel did, then wg_sum16. - k_attn_res_mix_sg16: one work-group per (token, query) instead of one per token (the queries are independent; each repeats the cheap block norms), and the block norms and the block dot products are reduced in one batch each (wg_sum16_multi: the same reduce / local sum / reduce per value, two barriers for all of them instead of two per value). - k_router_topk8_sg16: the eight rounds of "best remaining expert, lowest index among equals" run on the 128 threads of the group with exact max / min reductions, instead of one thread scanning 8 x 128 entries. - k_linear_f32_sg16: with up to 1,024 rows (the loop controllers' 64-row projections, the router's 128 rows, the conv taps) each row is one sub-group in its own work-group, so the rows spread over the whole GPU instead of N/16 groups; the arithmetic of a row is unchanged. - The FP4 decode by integer ops is dropped: on the B70 it was 1.5-2.5% slower than the table. - CI: the speed job also checks identical answers (logits of 64 ids and 48 greedy tokens, base against branch, two runs each). Arc A750 (p1006, UD-IQ1_S, no XMX): base and branch give the same logits hash (2780509b400ba29d) and the same greedy hash in 4 runs.
This commit is contained in:
1 parent
8d98660cc6
commit
5ef71af9b2
7 files changed
+153
-157
No files matched your search
@@ -27,7 +27,7 @@ build_tree "$REPO" "$W/opt" || exit 11
|
||||
echo "== build base ($BASE)"
|
||||
git -C "$REPO" archive "$BASE" | tar -x -C "$W/base-src"
|
||||
build_tree "$W/base-src" "$W/base" || exit 12
|
||||
OPT=$W/opt/strata-inkling; BASEX=$W/base/strata-inkling
|
||||
OPT=$W/opt/strata-inkling; BASEX=$W/base/strata-inkling; ident_n=0
|
||||
|
||||
export ONEAPI_DEVICE_SELECTOR=level_zero:gpu SYCL_CACHE_PERSISTENT=0
|
||||
bench() { # <tag> <exe> <model> <env "K=V K=V" or -> <bench args...>
|
||||
@@ -58,21 +58,17 @@ run_model() { # <label> <url> <file>
|
||||
echo "== $L 2. where the time goes (branch)"
|
||||
bench "$L-opt-timing" "$OPT" "$M" "INK_SY_TIMING=1" $P
|
||||
bench "$L-opt-prof" "$OPT" "$M" "INK_SY_PROF=1" $P
|
||||
if [ "$L" = q4kxl ]; then
|
||||
echo "== $L 3. runtime knobs (branch)"
|
||||
bench "$L-opt-graph" "$OPT" "$M" "INK_SY_GRAPH=1" $P
|
||||
bench "$L-opt-d2dk" "$OPT" "$M" "INK_SY_D2D_KERNEL=1" $P
|
||||
bench "$L-opt-imm1" "$OPT" "$M" "UR_L0_USE_IMMEDIATE_COMMANDLISTS=1" $P
|
||||
bench "$L-opt-imm0" "$OPT" "$M" "UR_L0_USE_IMMEDIATE_COMMANDLISTS=0" $P
|
||||
bench "$L-opt-v2" "$OPT" "$M" "UR_LOADER_USE_LEVEL_ZERO_V2=1" $P
|
||||
bench "$L-opt-graph-imm1" "$OPT" "$M" "INK_SY_GRAPH=1 UR_L0_USE_IMMEDIATE_COMMANDLISTS=1" $P
|
||||
echo "== $L 4. prefill chunk size, 4096-token prompt, 8 generated (branch)"
|
||||
for r in 1 2; do
|
||||
for ch in 256 512 1024 2048 4096; do
|
||||
bench "$L-opt-chunk$ch-r$r" "$OPT" "$M" - --prompt-len 4096 --gen 8 --parallel 1 --chunk $ch
|
||||
done
|
||||
done
|
||||
fi
|
||||
echo "== $L 3. identical answers: logits of 64 ids (two runs each) and 48 greedy tokens of 96 ids, base against branch"
|
||||
local I64 I96; I64=$(seq -s, 101 164); I96=$(seq -s, 1000 1095)
|
||||
for tag in base opt base opt; do
|
||||
local X=$OPT; [ $tag = base ] && X=$BASEX
|
||||
ident_n=$((ident_n + 1))
|
||||
timeout 1500 "$X" --native "$M" --gpus 1 --max-context 4096 --draft off --prompt-ids "$I64" --gen 16 --dump-ref "$W/logits.bin" > "$W/logs/id64.$ident_n.log" 2>&1
|
||||
echo "IDENT $L logits64 $tag run $ident_n sha=$(sha256sum "$W/logits.bin" 2>/dev/null | cut -c1-16) ids=$(tail -n 1 "$W/logs/id64.$ident_n.log" | cut -c1-70)"
|
||||
rm -f "$W/logits.bin"
|
||||
timeout 1500 "$X" --native "$M" --gpus 1 --max-context 4096 --draft off --prompt-ids "$I96" --gen 48 > "$W/logs/g96.$ident_n.log" 2>&1
|
||||
echo "IDENT $L greedy96 $tag run $ident_n md5=$(tail -n 1 "$W/logs/g96.$ident_n.log" | md5sum | cut -c1-12) $(tail -n 1 "$W/logs/g96.$ident_n.log" | cut -c1-90)"
|
||||
done
|
||||
echo "== $L 5. parallel (C8 / C32 total decode), base vs branch"
|
||||
for par in 8 32; do
|
||||
bench "$L-base-par$par" "$BASEX" "$M" - --prompt-len 512 --gen 64 --parallel $par --max-context 4096
|
||||
|
||||
@@ -323,10 +323,6 @@ if(STRATA_BUILD_TESTS)
|
||||
add_executable(ink_sy_fit_test tests/core/ink_sy_fit_test.cpp)
|
||||
target_include_directories(ink_sy_fit_test PRIVATE src/inkling2/sycl)
|
||||
add_test(NAME ink_sy_fit_test COMMAND ink_sy_fit_test)
|
||||
# the Intel Arc SYCL E2M1 (FP4) decode by integer ops equals its table for every code (no GPU)
|
||||
add_executable(sy_e2m1_test tests/core/sy_e2m1_test.cpp)
|
||||
target_include_directories(sy_e2m1_test PRIVATE src/inkling2)
|
||||
add_test(NAME sy_e2m1_test COMMAND sy_e2m1_test)
|
||||
# how the prompt path's K/V append reaches the host copy under KV streaming: the switch both ways, the backend defaults, no pool (no GPU)
|
||||
add_executable(kv_host_dma_test tests/core/kv_host_dma_test.cpp)
|
||||
target_include_directories(kv_host_dma_test PRIVATE include)
|
||||
|
||||
@@ -486,6 +486,27 @@ inline void k_gemm_q8row_sg16(const uint8_t* W, int N, int K, const float* X, fl
|
||||
|
||||
// F32 SG16 Linear (exact FP32 for routers, controllers, dynamic sconv weight_proj, attn_sink_proj)
|
||||
inline void k_linear_f32_sg16(const float* W, int N, int K, const float* X, float* Y, int M) {
|
||||
if (N <= 1024) {
|
||||
// Few rows (the loop controllers, the router, the dynamic conv taps): one sub-group per row and one row per work-group, so the rows
|
||||
// spread over the whole GPU instead of N/16 work-groups; the arithmetic of a row is the code below, bit for bit.
|
||||
Q().parallel_for(NR((size_t)N, 16), [=](sycl::nd_item<1> it) INK_REQD_SG16 {
|
||||
const int row = (int)it.get_group(0), lane = (int)it.get_local_id(0);
|
||||
const float* wp = W + (size_t)row * K;
|
||||
for (int m0 = 0; m0 < M; m0 += 4) {
|
||||
const int mm = sycl::min(4, M - m0);
|
||||
float a[4] = {0, 0, 0, 0};
|
||||
for (int k = lane; k < K; k += 16) {
|
||||
const float w = wp[k];
|
||||
for (int i = 0; i < mm; ++i) a[i] += w * X[(size_t)(m0 + i) * K + k];
|
||||
}
|
||||
for (int i = 0; i < mm; ++i) {
|
||||
const float v = sg_sum(it, a[i]);
|
||||
if (lane == 0) Y[(size_t)(m0 + i) * N + row] = v;
|
||||
}
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
Q().parallel_for(NR((N + 15) / 16, 256), [=](sycl::nd_item<1> it) INK_REQD_SG16 {
|
||||
const int sg = (int)it.get_local_id(0) >> 4, lane = (int)it.get_local_id(0) & 15;
|
||||
const int row = (int)it.get_group(0) * 16 + sg;
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
#include <vector>
|
||||
|
||||
#include "../ink_json.h"
|
||||
#include "ink_sy_e2m1.h"
|
||||
#include "strata/artifact/strata_reader.hpp"
|
||||
#include "strata/core/device_error_exit.hpp"
|
||||
#ifdef INK_SYCL_WITH_MODEL_FILE
|
||||
@@ -396,6 +395,14 @@ inline float h2f_bits(uint16_t b) {
|
||||
}
|
||||
|
||||
|
||||
inline float e2m1_lut(uint32_t nib) {
|
||||
constexpr float kLut[16] = {
|
||||
0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f,
|
||||
-0.0f, -0.5f, -1.0f, -1.5f, -2.0f, -3.0f, -4.0f, -6.0f
|
||||
};
|
||||
return kLut[nib & 15u];
|
||||
}
|
||||
|
||||
inline float e4m3_to_f32(uint32_t c) {
|
||||
const uint32_t mag = c & 0x7Fu;
|
||||
if (mag == 0x7Fu) return 0.0f;
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
// ink_sy_e2m1.h - E2M1 (FP4, the NVFP4 / SF4 element) code to float for the Intel Arc SYCL Inkling kernels.
|
||||
// Header only and free of SYCL types, so a plain C++20 compiler can run the exactness test (tests/core/sy_e2m1_test.cpp).
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace ink_sycl {
|
||||
|
||||
// The reference: the 16-entry table (0, 0.5, 1, 1.5, 2, 3, 4, 6, then the same with the sign bit; code 8 is -0.0).
|
||||
inline float e2m1_table(uint32_t nib) {
|
||||
constexpr float kLut[16] = {
|
||||
0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f,
|
||||
-0.0f, -0.5f, -1.0f, -1.5f, -2.0f, -3.0f, -4.0f, -6.0f
|
||||
};
|
||||
return kLut[nib & 15u];
|
||||
}
|
||||
|
||||
#ifdef INK_SY_E2M1_TABLE // A/B builds only: the table, as before
|
||||
inline float e2m1_lut(uint32_t nib) { return e2m1_table(nib); }
|
||||
#else
|
||||
// The same values from integer ops. A table indexed by a run-time value lives in per-thread private memory on Intel GPUs;
|
||||
// this form is plain ALU work. Bit for bit equal to e2m1_table for all 16 codes.
|
||||
inline float e2m1_lut(uint32_t nib) {
|
||||
const uint32_t m = nib & 7u, e = m >> 1, h = m & 1u;
|
||||
const uint32_t norm = ((e + 126u) << 23) | (h << 22); // (1 + h/2) * 2^(e-1) for e >= 1
|
||||
const uint32_t sub = h ? 0x3F000000u : 0u; // e == 0: 0 or 0.5
|
||||
return __builtin_bit_cast(float, ((e == 0u) ? sub : norm) | ((nib & 8u) << 28));
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace ink_sycl
|
||||
@@ -43,53 +43,85 @@ inline void k_embed_sg16(const InkTensorInfo& eti, const float* norm_w, const in
|
||||
});
|
||||
}
|
||||
|
||||
// Several work-group sums at once: the same operations as N calls of wg_sum16 (sub-group reduce, cross-sub-group sum through local memory,
|
||||
// sub-group reduce), so every sum is bit for bit what wg_sum16 gives, but the barriers are shared: two for all N values instead of two each.
|
||||
// sh holds N * 16 floats.
|
||||
template <int N, class It, class P>
|
||||
inline void wg_sum16_multi(const It& it, float (&v)[N], P sh) {
|
||||
auto sg = it.get_sub_group();
|
||||
const int w = (int)sg.get_group_linear_id();
|
||||
const int l = (int)sg.get_local_linear_id();
|
||||
const int nw = (int)sg.get_group_linear_range();
|
||||
#pragma unroll
|
||||
for (int i = 0; i < N; ++i) v[i] = sycl::reduce_over_group(sg, v[i], sycl::plus<float>());
|
||||
sycl::group_barrier(it.get_group());
|
||||
if (l == 0) {
|
||||
#pragma unroll
|
||||
for (int i = 0; i < N; ++i) sh[i * 16 + w] = v[i];
|
||||
}
|
||||
sycl::group_barrier(it.get_group());
|
||||
#pragma unroll
|
||||
for (int i = 0; i < N; ++i) {
|
||||
const float r = (l < nw) ? (float)sh[i * 16 + l] : 0.0f;
|
||||
v[i] = sycl::reduce_over_group(sg, r, sycl::plus<float>());
|
||||
}
|
||||
}
|
||||
|
||||
// One work-group per (token, query): the queries are independent, and every query repeats the (cheap) block norms, so the values are bit for bit
|
||||
// those of the one-group-does-everything kernel; the S block norms and the S dot products are each reduced in one batch (two barriers) instead
|
||||
// of one after the other.
|
||||
inline void k_attn_res_mix_sg16(const float* blocks_buf, const float* cur_buf, const float* queries, float* out_buf,
|
||||
int n_blocks, int block_stride, int M, int n_queries) {
|
||||
const int M_stride = M * INK_D;
|
||||
Q().submit([&](sycl::handler& h) {
|
||||
sycl::local_accessor<float, 1> sh(16, h);
|
||||
sycl::local_accessor<float, 1> inv_s(8, h);
|
||||
sycl::local_accessor<float, 1> probs(32, h); // [4][8]
|
||||
h.parallel_for(NR(M, 256), [=](sycl::nd_item<1> it) INK_REQD_SG16 {
|
||||
const int m = (int)it.get_group(0), tid = (int)it.get_local_id(0);
|
||||
sycl::local_accessor<float, 1> sh(8 * 16, h);
|
||||
sycl::local_accessor<float, 1> probs(8, h);
|
||||
h.parallel_for(NR2((size_t)n_queries, (size_t)M, 256), [=](sycl::nd_item<2> it) INK_REQD_SG16 {
|
||||
const int m = (int)it.get_group(0), q = (int)it.get_group(1), tid = (int)it.get_local_id(1);
|
||||
const int S = n_blocks + 1;
|
||||
for (int s = 0; s < S; ++s) {
|
||||
const float* sp = (s < n_blocks) ? (blocks_buf + (size_t)s * block_stride + (size_t)m * INK_D) : (cur_buf + (size_t)m * INK_D);
|
||||
float ss = 0.0f;
|
||||
for (int d = tid; d < INK_D; d += 256) ss += sp[d] * sp[d];
|
||||
const float tot = wg_sum16(it, ss, sh);
|
||||
if (tid == 0) inv_s[s] = sycl::rsqrt(tot / (float)INK_D + INK_RMS_EPS);
|
||||
const float* sp[8];
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
sp[s] = (s < n_blocks) ? (blocks_buf + (size_t)s * block_stride + (size_t)m * INK_D) : (cur_buf + (size_t)m * INK_D);
|
||||
}
|
||||
float ss[8];
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
float a = 0.0f;
|
||||
if (s < S) for (int d = tid; d < INK_D; d += 256) a += sp[s][d] * sp[s][d];
|
||||
ss[s] = a;
|
||||
}
|
||||
wg_sum16_multi<8>(it, ss, sh);
|
||||
float inv_s[8];
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s) inv_s[s] = sycl::rsqrt(ss[s] / (float)INK_D + INK_RMS_EPS);
|
||||
sycl::group_barrier(it.get_group()); // sh is read again below
|
||||
const float inv_sqrt_d = 1.0f / 48.0f;
|
||||
const float* qv = queries + (size_t)q * INK_D;
|
||||
float dt[8];
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
float a = 0.0f;
|
||||
if (s < S) for (int d = tid; d < INK_D; d += 256) a += sp[s][d] * qv[d];
|
||||
dt[s] = a;
|
||||
}
|
||||
wg_sum16_multi<8>(it, dt, sh);
|
||||
if (tid == 0) {
|
||||
float sc[8];
|
||||
for (int s = 0; s < S; ++s) sc[s] = dt[s] * inv_s[s] * inv_sqrt_d;
|
||||
float mx = -1e30f;
|
||||
for (int s = 0; s < S; ++s) mx = sycl::fmax(mx, sc[s]);
|
||||
float sum = 0.0f;
|
||||
for (int s = 0; s < S; ++s) { sc[s] = sycl::exp(sc[s] - mx); sum += sc[s]; }
|
||||
const float isum = 1.0f / sum;
|
||||
for (int s = 0; s < S; ++s) probs[s] = sc[s] * (isum * inv_s[s]);
|
||||
}
|
||||
sycl::group_barrier(it.get_group());
|
||||
const float inv_sqrt_d = 1.0f / 48.0f;
|
||||
for (int q = 0; q < n_queries; ++q) {
|
||||
const float* qv = queries + (size_t)q * INK_D;
|
||||
for (int s = 0; s < S; ++s) {
|
||||
const float* sp = (s < n_blocks) ? (blocks_buf + (size_t)s * block_stride + (size_t)m * INK_D) : (cur_buf + (size_t)m * INK_D);
|
||||
float dt = 0.0f;
|
||||
for (int d = tid; d < INK_D; d += 256) dt += sp[d] * qv[d];
|
||||
const float sc = wg_sum16(it, dt, sh) * inv_s[s] * inv_sqrt_d;
|
||||
if (tid == 0) probs[q * 8 + s] = sc;
|
||||
}
|
||||
sycl::group_barrier(it.get_group());
|
||||
if (tid == 0) {
|
||||
float mx = -1e30f;
|
||||
for (int s = 0; s < S; ++s) mx = sycl::fmax(mx, probs[q * 8 + s]);
|
||||
float sum = 0.0f;
|
||||
for (int s = 0; s < S; ++s) { probs[q * 8 + s] = sycl::exp(probs[q * 8 + s] - mx); sum += probs[q * 8 + s]; }
|
||||
const float isum = 1.0f / sum;
|
||||
for (int s = 0; s < S; ++s) probs[q * 8 + s] *= isum * inv_s[s];
|
||||
}
|
||||
sycl::group_barrier(it.get_group());
|
||||
float* dst = out_buf + (size_t)q * M_stride + (size_t)m * INK_D;
|
||||
for (int d = tid; d < INK_D; d += 256) {
|
||||
float acc = 0.0f;
|
||||
for (int s = 0; s < S; ++s) {
|
||||
const float* sp = (s < n_blocks) ? (blocks_buf + (size_t)s * block_stride + (size_t)m * INK_D) : (cur_buf + (size_t)m * INK_D);
|
||||
acc += probs[q * 8 + s] * sp[d];
|
||||
}
|
||||
dst[d] = acc;
|
||||
}
|
||||
float* dst = out_buf + (size_t)q * M_stride + (size_t)m * INK_D;
|
||||
for (int d = tid; d < INK_D; d += 256) {
|
||||
float acc = 0.0f;
|
||||
for (int s = 0; s < S; ++s) acc += probs[s] * sp[s][d];
|
||||
dst[d] = acc;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -110,34 +142,39 @@ inline void k_ctrl_cat_rms_sg16(const float* cur_buf, const float* msg_buf, floa
|
||||
});
|
||||
}
|
||||
|
||||
// The loop controller's gated mix, then (when do_norm) the RMS norm. Two kernels instead of one work-group per token: the mix runs one thread per
|
||||
// output channel across D/64 work-groups (same order of the 64-term dots, so the same bits), the norm is one work-group per token that adds the
|
||||
// squares in exactly the order the old single kernel did (thread t takes channels t, t+256, ...; then wg_sum16), so the result is bit for bit the same.
|
||||
inline void k_ctrl_apply_norm_sg16(const float* cur_buf, const float* msg_buf, const float* gate_buf,
|
||||
const float* w_gain, const float* w_forget, const float* norm_w,
|
||||
float* out_buf, int M, int do_norm) {
|
||||
Q().submit([&](sycl::handler& h) {
|
||||
sycl::local_accessor<float, 1> sh(16, h);
|
||||
sycl::local_accessor<float, 1> g_sh(64, h);
|
||||
h.parallel_for(NR2(INK_D / 64, (size_t)M, 64), [=](sycl::nd_item<2> it) INK_REQD_SG16 {
|
||||
const int m = (int)it.get_group(0), blk = (int)it.get_group(1), tid = (int)it.get_local_id(1);
|
||||
g_sh[tid] = gate_buf[(size_t)m * 64 + tid];
|
||||
sycl::group_barrier(it.get_group());
|
||||
const int d = blk * 64 + tid;
|
||||
const float* wg = w_gain + (size_t)d * 64;
|
||||
const float* wf = w_forget + (size_t)d * 64;
|
||||
float sg = 0.0f, sf = 0.0f;
|
||||
for (int r = 0; r < 64; ++r) { sg += wg[r] * g_sh[r]; sf += wf[r] * g_sh[r]; }
|
||||
const float cp = cur_buf[(size_t)m * INK_D + d];
|
||||
const float mp = msg_buf[(size_t)m * INK_D + d];
|
||||
out_buf[(size_t)m * INK_D + d] = (1.0f - sycl::tanh(sycl::clamp(sf, -15.0f, 15.0f))) * cp
|
||||
+ sycl::tanh(sycl::clamp(sg, -15.0f, 15.0f)) * mp;
|
||||
});
|
||||
});
|
||||
if (!do_norm) return;
|
||||
Q().submit([&](sycl::handler& h) {
|
||||
sycl::local_accessor<float, 1> sh(16, h);
|
||||
h.parallel_for(NR(M, 256), [=](sycl::nd_item<1> it) INK_REQD_SG16 {
|
||||
const int m = (int)it.get_group(0), tid = (int)it.get_local_id(0);
|
||||
if (tid < 64) g_sh[tid] = gate_buf[(size_t)m * 64 + tid];
|
||||
sycl::group_barrier(it.get_group());
|
||||
const float* cp = cur_buf + (size_t)m * INK_D;
|
||||
const float* mp = msg_buf + (size_t)m * INK_D;
|
||||
float* dst = out_buf + (size_t)m * INK_D;
|
||||
float ss = 0.0f;
|
||||
for (int d = tid; d < INK_D; d += 256) {
|
||||
const float* wg = w_gain + (size_t)d * 64;
|
||||
const float* wf = w_forget + (size_t)d * 64;
|
||||
float sg = 0.0f, sf = 0.0f;
|
||||
for (int r = 0; r < 64; ++r) { sg += wg[r] * g_sh[r]; sf += wf[r] * g_sh[r]; }
|
||||
const float v = (1.0f - sycl::tanh(sycl::clamp(sf, -15.0f, 15.0f))) * cp[d]
|
||||
+ sycl::tanh(sycl::clamp(sg, -15.0f, 15.0f)) * mp[d];
|
||||
dst[d] = v;
|
||||
ss += v * v;
|
||||
}
|
||||
if (do_norm) {
|
||||
const float inv = sycl::rsqrt(wg_sum16(it, ss, sh) / (float)INK_D + INK_RMS_EPS);
|
||||
for (int d = tid; d < INK_D; d += 256) dst[d] = dst[d] * inv * norm_w[d];
|
||||
}
|
||||
for (int d = tid; d < INK_D; d += 256) { const float v = dst[d]; ss += v * v; }
|
||||
const float inv = sycl::rsqrt(wg_sum16(it, ss, sh) / (float)INK_D + INK_RMS_EPS);
|
||||
for (int d = tid; d < INK_D; d += 256) dst[d] = dst[d] * inv * norm_w[d];
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -323,21 +360,29 @@ inline void k_router_topk8_sg16(const float* r_logits, const float* r_bias, int*
|
||||
sycl::local_accessor<float, 1> sh_s(128, h);
|
||||
h.parallel_for(NR(M, 128), [=](sycl::nd_item<1> it) INK_REQD_SG16 {
|
||||
const int m = (int)it.get_group(0), tid = (int)it.get_local_id(0);
|
||||
auto grp = it.get_group();
|
||||
const float raw_l = r_logits[(size_t)m * 128 + tid];
|
||||
const float p_raw = 1.0f / (1.0f + sycl::exp(-raw_l));
|
||||
const float p = sycl::isfinite(p_raw) ? p_raw : 0.0f;
|
||||
const float sc = p + r_bias[tid];
|
||||
sh_p[tid] = p;
|
||||
sh_s[tid] = sycl::isfinite(sc) ? sc : -1e29f;
|
||||
sycl::group_barrier(it.get_group());
|
||||
sycl::group_barrier(grp);
|
||||
// eight rounds of "the best remaining expert, the lowest index among equals" (max and min are exact, so the choice is
|
||||
// the same as the serial scan it replaces); thread 0 keeps the weights in the old order
|
||||
int sel[8]; float pw[8]; float sum = 0.0f;
|
||||
for (int k = 0; k < 8; ++k) {
|
||||
const float v = sh_s[tid];
|
||||
const float mx = sycl::reduce_over_group(grp, v, sycl::maximum<float>());
|
||||
const int cand = (v == mx) ? tid : (1 << 30);
|
||||
int bi = sycl::reduce_over_group(grp, cand, sycl::minimum<int>());
|
||||
if (!(mx > -1e30f)) bi = k;
|
||||
if (tid == 0) { sel[k] = bi; pw[k] = sh_p[bi]; sum += pw[k]; }
|
||||
sycl::group_barrier(grp);
|
||||
if (tid == bi) sh_s[tid] = -1e30f;
|
||||
sycl::group_barrier(grp);
|
||||
}
|
||||
if (tid == 0) {
|
||||
int sel[8]; float pw[8]; float sum = 0.0f;
|
||||
for (int k = 0; k < 8; ++k) {
|
||||
int bi = -1; float bs = -1e30f;
|
||||
for (int e = 0; e < 128; ++e) if (sh_s[e] > bs) { bs = sh_s[e]; bi = e; }
|
||||
if (bi < 0) bi = k;
|
||||
sel[k] = bi; pw[k] = sh_p[bi]; sum += pw[k]; sh_s[bi] = -1e30f;
|
||||
}
|
||||
const float inv = route_scale / (sum + 1e-20f);
|
||||
for (int k = 0; k < 8; ++k) { topk_idx[(size_t)m * 8 + k] = sel[k]; topk_w[(size_t)m * 8 + k] = pw[k] * inv; }
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
// The Intel Arc SYCL kernels decode E2M1 (FP4) with integer ops instead of a table (a run-time indexed table sits in private memory
|
||||
// on Intel GPUs). The decode must equal the table bit for bit for every code, including the sign of zero. No GPU.
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "sycl/ink_sy_e2m1.h"
|
||||
|
||||
static uint32_t bits(float f) {
|
||||
uint32_t b;
|
||||
std::memcpy(&b, &f, 4);
|
||||
return b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int bad = 0;
|
||||
for (uint32_t nib = 0; nib < 256; ++nib) { // also the high bits: only the low four count
|
||||
const float a = ink_sycl::e2m1_lut(nib), b = ink_sycl::e2m1_table(nib);
|
||||
if (bits(a) != bits(b)) {
|
||||
std::printf("FAIL code %u: lut %08x table %08x\n", nib, bits(a), bits(b));
|
||||
++bad;
|
||||
}
|
||||
}
|
||||
const float expect[8] = {0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f};
|
||||
for (uint32_t m = 0; m < 8; ++m) {
|
||||
if (ink_sycl::e2m1_lut(m) != expect[m] || ink_sycl::e2m1_lut(m | 8u) != -expect[m]) {
|
||||
std::printf("FAIL magnitude %u\n", m);
|
||||
++bad;
|
||||
}
|
||||
}
|
||||
if (bits(ink_sycl::e2m1_lut(8u)) != 0x80000000u) {
|
||||
std::printf("FAIL code 8 is not -0.0\n");
|
||||
++bad;
|
||||
}
|
||||
if (bad) return 1;
|
||||
std::printf("sy_e2m1_test: OK\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user