From f5614a03bbf4ffe16eb32374abb60f7487e896d3 Mon Sep 17 00:00:00 2001 From: pascallinxweiler Date: Fri, 23 Oct 2026 20:47:13 +0200 Subject: [PATCH] libxe2: the norm and the fp8 quantisers 3-4x faster at decode, still bit-identical to libref At one row a work-group, rmsnorm_quant_fp8 took 108 us and gated_quant_fp8 84 us a call (26% and 10% of a decode step at a 1.8k context). The ordered sum of squares now reads its squares eight at a time and adds them in the same order; quant_group computes a group's values once into registers and writes out_bf16 itself; gated_quant_fp8 spreads a row's independent groups over ceil(groups/16) work-groups. rad-kbench: 208/208 and 206/206 at rel_l2 0. scripts/sycl/profile_decode.sh isolates decode steps' per-op device time (two profiled runs of the same request, 1 and 1 + STEPS tokens, differenced). Decode at 1.8k context: 34.8 -> 26.6 ms of device time a step; the two ops 9.1 -> 3.7 and 3.5 -> 0.9 ms. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- docs/SYCL_PROGRESS.md | 15 ++++++ libxe2/xe2_norm.cpp | 92 ++++++++++++++++++++++------------ scripts/sycl/profile_decode.sh | 55 ++++++++++++++++++++ 3 files changed, 131 insertions(+), 31 deletions(-) create mode 100755 scripts/sycl/profile_decode.sh diff --git a/docs/SYCL_PROGRESS.md b/docs/SYCL_PROGRESS.md index 83d1ddf3..bce631a1 100644 --- a/docs/SYCL_PROGRESS.md +++ b/docs/SYCL_PROGRESS.md @@ -412,6 +412,21 @@ sub-group walking every key for its head): 32 keys fixed it. - **Decode after a 1797-token prompt: 16.2 -> 29.8 tok/s**; short-context decode 32.0 tok/s; 1797-token prefill 4.61 s. +## Phase 4, step 4 (2026-10-23): the norm and the quantisers, still exact + +`scripts/sycl/profile_decode.sh MODEL PROMPT [STEPS] [flags]` isolates decode: the same request at +1 and 1 + STEPS tokens in two profiled servers, the difference per op divided by STEPS. At a +1.8k-token context, before: 34.8 ms/step of device time -- gemm_nt 9.65, attn_paged 9.51, +rmsnorm_quant_fp8 9.12 (108 us a call for one row), gated_quant_fp8 3.52 (84 us, one work-group a row +and every value computed twice, a double exp each). + +- The ordered sum reads its squares eight at a time, adding them in the same order. +- `quant_group` computes a group's values once into registers (up to 128 a group) and writes + out_bf16 itself; `gated_quant_fp8` spreads a row's groups over ceil(groups/16) work-groups. +- Still bit-identical: rad-kbench 208/208 and 206/206 at rel_l2 0. +- After: **26.6 ms/step** -- gemm_nt 9.62, attn_paged 9.50, rmsnorm_quant_fp8 3.75, logits_gemm 1.02, + gated_quant_fp8 0.87. + ## Next steps 1. ~~Milestone B, part 2: `minicpm5-2b-fp8.rad`~~ **Dropped by the user (2026-10-23): "instead of the diff --git a/libxe2/xe2_norm.cpp b/libxe2/xe2_norm.cpp index 83e2f8d4..770aefff 100644 --- a/libxe2/xe2_norm.cpp +++ b/libxe2/xe2_norm.cpp @@ -55,30 +55,64 @@ inline float ordered_sumsq(const sycl::nd_item<1>& it, const F& src, int64_t n) sq[i] = v * v; } sycl::group_barrier(grp); - if (t == 0) - for (int64_t i = 0; i < cn; ++i) ss += sq[i]; + if (t == 0) { + /* Eight independent loads a step, then the eight adds in index order: the same sum, + * without waiting on one local-memory load per addition. */ + int64_t i = 0; + for (; i + 8 <= cn; i += 8) { + float b[8]; + for (int j = 0; j < 8; ++j) b[j] = sq[i + j]; + for (int j = 0; j < 8; ++j) ss += b[j]; + } + for (; i < cn; ++i) ss += sq[i]; + } sycl::group_barrier(grp); } return sycl::group_broadcast(grp, ss, 0); } -/* One group's codes and scale, by the sub-group `sg` over values v(k0 + i), i < len. - * Returns the scale (the same in every lane). */ +/* One group's codes and scale, by the sub-group `sg` over values v(k0 + i), i < len; with + * `obase`, each value is also stored there (out_bf16). A group of up to kCached * 16 values is + * computed once into registers, so the amax pass and the codes read the same floats without + * evaluating v twice (for gated_quant_fp8 that is a double exp an element); a wider group + * evaluates v in each pass, which gives the same floats. Returns the scale (the same in every + * lane). */ +constexpr int kCached = 8; + template inline float quant_group(const sycl::sub_group& sg, const V& v, int64_t k0, int64_t len, float top, - char* qbase, uint32_t qdt, int64_t qoff) { + char* qbase, uint32_t qdt, int64_t qoff, char* obase = nullptr, + uint32_t odt = 0, int64_t ooff = 0) { const int lane = (int)sg.get_local_linear_id(); + auto code = [&](int64_t i, float x, float inv) { + float c = x * inv; + if (c > top) c = top; + if (c < -top) c = -top; + rad_store_f32(qbase, qdt, qoff + k0 + i, c); + if (obase) rad_store_f32(obase, odt, ooff + k0 + i, x); + }; float amax = 0.0f; + if (len <= (int64_t)kCached * kSub) { + float c[kCached]; + for (int j = 0; j < kCached; ++j) { + const int64_t i = lane + (int64_t)j * kSub; + c[j] = i < len ? v(k0 + i) : 0.0f; + amax = sycl::fmax(amax, sycl::fabs(c[j])); + } + amax = sycl::reduce_over_group(sg, amax, sycl::maximum()); + const float sc = amax > 0.0f ? amax * (1.0f / top) : 1.0f; + const float inv = 1.0f / sc; + for (int j = 0; j < kCached; ++j) { + const int64_t i = lane + (int64_t)j * kSub; + if (i < len) code(i, c[j], inv); + } + return sc; + } for (int64_t i = lane; i < len; i += kSub) amax = sycl::fmax(amax, sycl::fabs(v(k0 + i))); amax = sycl::reduce_over_group(sg, amax, sycl::maximum()); const float sc = amax > 0.0f ? amax * (1.0f / top) : 1.0f; const float inv = 1.0f / sc; - for (int64_t i = lane; i < len; i += kSub) { - float c = v(k0 + i) * inv; - if (c > top) c = top; - if (c < -top) c = -top; - rad_store_f32(qbase, qdt, qoff + k0 + i, c); - } + for (int64_t i = lane; i < len; i += kSub) code(i, v(k0 + i), inv); return sc; } @@ -237,14 +271,12 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) { const float w = rad_load_f32(gw.base, gw.dtype, i * gw.stride) + wadd; return rad_bf16_to_f32(rad_f32_to_bf16(src(i) * rs * w)); }; - if (has_ob) - for (int64_t i = t; i < n; i += kWg) - rad_store_f32(ob.base, ob.dtype, r * ob.pitch + i, v(i)); const sycl::sub_group sg = it.get_sub_group(); const int sgi = (int)sg.get_group_linear_id(); for (int64_t gi = sgi; gi < ngrp; gi += kSubs) { const int64_t k0 = gi * g, len = sycl::min(g, n - k0); - const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch); + const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch, + has_ob ? ob.base : nullptr, ob.dtype, r * ob.pitch); if (sg.get_local_linear_id() == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc); } @@ -280,29 +312,27 @@ int gated_quant_fp8(const RadArgs* a, RadStream st) { const float top = Q->dtype == RAD_I8 ? 127.0f : 448.0f; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; + /* The groups are independent, so a row spreads over ceil(ngrp / 16) work-groups, one group a + * sub-group: at decode (one row of 48 groups) three work-groups share the row, not one. */ constexpr int kSubs = (int)(kWg / kSub); + const int64_t per_row = (ngrp + kSubs - 1) / kSubs; return submit([&] { - syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg}, + syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * per_row) * kWg, kWg}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { - const int64_t r = (int64_t)it.get_group(0); - const int t = (int)it.get_local_id(0); + const int64_t r = (int64_t)it.get_group(0) / per_row; + const sycl::sub_group sg = it.get_sub_group(); + const int64_t gi = ((int64_t)it.get_group(0) % per_row) * kSubs + (int64_t)sg.get_group_linear_id(); + if (gi >= ngrp) return; /* uniform across the sub-group */ auto v = [&](int64_t i) { const float gt = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + i); const float up = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + n + i); - const float sg = sigmoidf(gt); - return rad_bf16_to_f32(rad_f32_to_bf16((act == 0 ? gt * sg : sg) * up)); + const float sgm = sigmoidf(gt); + return rad_bf16_to_f32(rad_f32_to_bf16((act == 0 ? gt * sgm : sgm) * up)); }; - if (has_ob) - for (int64_t i = t; i < n; i += kWg) - rad_store_f32(ob.base, ob.dtype, r * ob.pitch + i, v(i)); - const sycl::sub_group sg = it.get_sub_group(); - const int sgi = (int)sg.get_group_linear_id(); - for (int64_t gi = sgi; gi < ngrp; gi += kSubs) { - const int64_t k0 = gi * g, len = sycl::min(g, n - k0); - const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch); - if (sg.get_local_linear_id() == 0) - rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc); - } + const int64_t k0 = gi * g, len = sycl::min(g, n - k0); + const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch, + has_ob ? ob.base : nullptr, ob.dtype, r * ob.pitch); + if (sg.get_local_linear_id() == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc); }); }); } diff --git a/scripts/sycl/profile_decode.sh b/scripts/sycl/profile_decode.sh new file mode 100755 index 00000000..dae459a3 --- /dev/null +++ b/scripts/sycl/profile_decode.sh @@ -0,0 +1,55 @@ +#!/bin/sh +# profile_decode.sh -- per-op device time of decode steps alone, on the SYCL backend. +# +# --profile-ops reports every op over a whole run, so a prompt's prefill and the decode steps after +# it are mixed. This runs the same request twice in two fresh servers -- one generating 1 token, one +# generating 1 + STEPS -- and prints the difference per op type, divided by STEPS: the device time a +# decode step spends in each op at the prompt's context length. The numbers carry --profile-ops' +# synchronisations, so compare them with each other, not with unprofiled tok/s. +# +# Usage: scripts/sycl/profile_decode.sh MODEL PROMPT_FILE [STEPS] [extra radiance flags...] +# PROMPT_FILE plain text, sent as one /v1/completions prompt +# STEPS decode steps measured (default 128) +# Needs the oneAPI environment (it sources setvars.sh if icpx is not on PATH) and build-sycl/. +set -eu +model=${1:?usage: profile_decode.sh MODEL PROMPT_FILE [STEPS] [flags...]} +prompt=${2:?usage: profile_decode.sh MODEL PROMPT_FILE [STEPS] [flags...]} +steps=${3:-128} +[ $# -ge 3 ] && shift 3 || shift $# +root=$(cd "$(dirname "$0")/../.." && pwd) +if ! command -v icpx >/dev/null; then set +u; . /opt/intel/oneapi/setvars.sh >/dev/null; set -u; fi +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +port=${PORT:-8139} + +run() { # run TOKENS FLAGS... -> $work/TOKENS.tsv: op, total ms + tok=$1; shift + RADIANCE_SYCL_TIMING=1 RADIANCE_HOME="$root/build-sycl/radiance_home" \ + "$root/build-sycl/bin/radiance" --model "$model" --profile-ops --max-num-seqs 1 \ + --host 127.0.0.1 --port "$port" "$@" > "$work/$tok.log" 2>&1 & + pid=$! + i=0 + until grep -q 'server: listening' "$work/$tok.log"; do + i=$((i + 1)); [ $i -lt 600 ] || { echo "the server did not start" >&2; kill $pid; exit 1; } + kill -0 $pid 2>/dev/null || { tail -5 "$work/$tok.log" >&2; exit 1; } + sleep 1 + done + python3 -I -c 'import json,sys; print(json.dumps({"prompt": open(sys.argv[1]).read(), + "max_tokens": int(sys.argv[2]), "temperature": 0}))' "$prompt" "$tok" > "$work/req.json" + curl -sf -m 3600 "http://127.0.0.1:$port/v1/completions" -H 'Content-Type: application/json' \ + -d @"$work/req.json" > /dev/null + kill -INT $pid + wait $pid 2>/dev/null || true + n=$(grep -n -E 'calls +total ms' "$work/$tok.log" | tail -1 | cut -d: -f1) + awk -v from="$n" 'NR > from && $4 == "dev" { t[$7] += $2 } END { for (o in t) print o, t[o] }' \ + "$work/$tok.log" | sort > "$work/$tok.tsv" +} + +run 1 "$@" +run $((steps + 1)) "$@" +join "$work/1.tsv" "$work/$((steps + 1)).tsv" | awk -v n="$steps" ' + { d = $3 - $2; T += d; a[$1] = d } + END { + for (o in a) printf "%8.3f ms/step %5.1f%% %s\n", a[o] / n, 100 * a[o] / T, o + printf "%8.3f ms/step of device time over %d decode steps\n", T / n, n + }' | sort -rn