This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libavx/avx_rows.h
T
2026-10-04 12:02:41 +02:00

56 lines
2.5 KiB
C++

/* avx_rows.h -- the quantised embedding gather's two halves: the row source, compiled once at the
* baseline, and the row decode, compiled per ISA level.
*
* embed_lookup_q over an E4M3 table into bf16 is a gather of rows that are, most of the time, not in
* memory at all: the n-gram table it exists for is tens of gigabytes in a container that is mapped
* and never loaded. Getting the bytes is the whole cost and is the same work at every ISA level --
* a row cache every thread shares, direct reads for its misses, a reader thread that runs a step
* ahead -- so that half is avx_ngram.cpp, compiled baseline, with one row cache for the process
* whatever level runs. Turning a row's codes into bf16 is the half that differs per level, so the
* level's kernel hands its own decoder in. */
#ifndef RAD_AVX_ROWS_H
#define RAD_AVX_ROWS_H
#include "rad_abi.h"
#include <cstdint>
namespace avx {
/* dst[i][0, n) = lut[src[i][0, n)] for every i < count: E4M3 codes through a table of all 256
* codes already scaled and rounded to bf16. */
typedef void (*RowDecode)(const uint8_t* const* src, uint16_t* const* dst, int64_t count,
int64_t n, const uint16_t* lut);
/* One call's gather, validated by the caller: `ids` holds M row ids, `tab` n_vocab rows of n_embd
* elements -- E4M3 codes, or bf16 as stored -- `row_bytes` apart, `out` M rows of n_embd bf16
* `x_ld` elements apart. The row cache and the reads deal in `row_bytes`; the decode in elements.
* `ahead` holds n_ahead ids a later call will gather, or is null. rank and world_size are the
* call's. */
struct RowGather {
const int32_t* ids;
int64_t M;
const uint8_t* tab;
int64_t n_vocab, n_embd, voff;
int64_t row_bytes;
float scale;
uint16_t* out;
int64_t x_ld;
const int32_t* ahead;
int64_t n_ahead;
int rank, world_size;
};
/* The gather, through the row cache when a file backs the table and from the table's memory when
* none does. Returns a RAD_* status; on anything but RAD_OK the output is not complete. */
int row_gather(const RowGather& g, RowDecode decode);
} /* namespace avx */
/* What the table must be: the E4M3 plane with ONE scale, or a plain bf16 one, stored as it is
* (avx_ngram.cpp). */
extern "C" int avx_layout_ngram(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, RadLayout*);
#endif /* RAD_AVX_ROWS_H */