MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::simd Namespace Reference

Namespaces

namespace  detail
 Blocking parameters for T using the compiled SIMD width and the target hardware traits.

Classes

struct  widen_accumulator
 The accumulator a narrow type widens into: 16 bits doubles to 32, keeping signedness. More...
struct  widen_accumulator< std::int16_t >
struct  widen_accumulator< std::uint16_t >
struct  quad_accumulator
 The accumulator for a quad-widening operand PAIR. More...
struct  quad_accumulator< std::int8_t, std::int8_t >
struct  quad_accumulator< std::uint8_t, std::int8_t >
struct  quad_accumulator< std::uint8_t, std::uint8_t >
class  batch
struct  hw_traits
 Hardware description for blocking-parameter derivation. All sizes in bytes. More...
struct  blocking_params
 GEMM blocking parameters: mr x nr register microtile, kc/mc/nc cache blocks. More...

Concepts

concept  QuadPair
 Is (A, B) a quad-widening pair the hardware op accepts?

Typedefs

template<typename Narrow>
using widen_accumulator_t = typename widen_accumulator<Narrow>::type
template<typename A, typename B>
using quad_accumulator_t = typename quad_accumulator<A, B>::type

Enumerations

enum class  quad_dot_support { none , partial , all }
 How much of the quad multiply-accumulate this build gets natively. More...
enum class  detect_levels : unsigned { none = 0u , l1 = 1u << 0 , l2 = 1u << 1 , both = l1 | l2 }
 target_hw_traits with the CACHE fields replaced by what the running machine reports (#222). More...

Functions

template<typename T>
T reduce_dot (const T *a, const T *b, std::size_t n)
 dot product: sum_i a[i]*b[i] (real float/double, or int32/uint32 mod 2^32).
template<typename Wide, typename Narrow>
requires std::is_floating_point_v<Wide>
Wide reduce_dot_widen (const Narrow *a, const Narrow *b, std::size_t n)
 Widening dot product: sum_i a[i]*b[i] with Narrow elements (e.g.
template<typename Wide, typename NA, typename NB = NA>
requires std::is_integral_v<Wide>
Wide reduce_dot_widen (const NA *a, const NB *b, std::size_t n)
 Widening INTEGER dot product: 16-bit operands accumulated in 32 bits, via the hardware's widening multiply-accumulate (#451 phase 2).
template<typename NA, typename NB = NA>
float reduce_dot_block_scaled (const NA *a, const NB *x, const float *scales, std::size_t n_blocks, std::size_t block)
 Block-scaled integer dot product (#628):
template<typename T>
T reduce_sum_squares (const T *a, std::size_t n)
 sum of squares: sum_i a[i]*a[i] (two_norm computes sqrt of this).
template<typename T>
void axpy (T alpha, const T *x, T *y, std::size_t n)
 axpy: y[i] += alpha*x[i].
template<typename T>
void scal (T alpha, T *x, std::size_t n)
 scal: x[i] *= alpha.
const char * backend_name () noexcept
 Name of the SIMD backend actually compiled – "scalar", or Highway's target ("AVX3_DL", "AVX2", "SSE4", "NEON", ...).
const char * quad_dot_support_name () noexcept
 "NATIVE", "PARTIAL" or "DECOMPOSED" – the token a benchmark sidecar records.
constexpr std::size_t vectorizable_length (std::size_t n, std::size_t w) noexcept
 Largest multiple of W not exceeding n – the SIMD body length; iterate the remainder [vectorizable_length(n) .
template<typename T>
T scalar_fma (T a, T b, T c)
 The SCALAR equivalent of fma(batch,batch,batch) – what one lane of it does.
constexpr std::size_t target_vec_registers ()
 The architectural vector register file of the build's target ISA (#643).
template<typename T>
constexpr blocking_params derive_blocking (std::size_t nvec, const hw_traits &hw=default_hw_traits)
 Derive GEMM blocking parameters for element type T with nvec SIMD lanes (e.g.
constexpr detect_levels operator| (detect_levels a, detect_levels b)
constexpr bool has_level (detect_levels set, detect_levels one)
constexpr hw_traits with_detected_caches (hw_traits base, const util::cache_info &c, detect_levels lv=detect_levels::both)
 Overlay the detected hierarchy on base, one level group at a time.
constexpr detect_levels configured_detect_levels ()
 The levels this translation unit was compiled to detect.
const hw_traits & detected_hw_traits ()
 Runtime cache detection is OPT-IN: define MTL5_ENABLE_CACHE_DETECTION to let the detected hierarchy feed kc/mc.
template<typename T>
const blocking_params & runtime_blocking ()
 Blocking parameters for T derived against the DETECTED hardware.
template<typename T>
requires (std::is_same_v<T, float> || std::is_same_v<T, double>)
batch< T > exp (batch< T > x)
 e^x lane by lane, for float and double lanes.
template<typename T>
requires (std::is_same_v<T, float> || std::is_same_v<T, double>)
void exp (const T *x, T *out, std::size_t n)
 out[i] = e^x[i] for i < n, by exp(batch<T>).

Variables

template<typename T>
constexpr bool is_lane_v
 Lane types batch<T> supports.
template<typename T>
constexpr bool is_integer_lane_v = is_lane_v<T> && std::is_integral_v<T>
 True for the integer lane types – the ones whose arithmetic WRAPS.
template<typename T>
constexpr bool is_widenable_v
 Narrow integer types that can be WIDENED into a lane by a dot-product accumulate (#451 phase 2).
template<typename T>
constexpr bool is_quad_widenable_v
 Narrow integer types whose dot product widens by FOUR into a 32-bit lane, via the hardware's quad multiply-accumulate (#451 phase 3) – VNNI's vpdpbusd on x86, SDOT/UDOT on NEON.
template<typename NA, typename NB>
constexpr bool has_native_quad_dot_v = false
 Does this build have a NATIVE quad multiply-accumulate for the pairing (NA, NB), or will Highway emulate it?
template<>
constexpr bool has_native_quad_dot_v< std::uint8_t, std::int8_t > = (MTL5_QUAD_NATIVE_U8I8 != 0)
template<>
constexpr bool has_native_quad_dot_v< std::int8_t, std::int8_t > = (MTL5_QUAD_NATIVE_I8I8 != 0)
template<>
constexpr bool has_native_quad_dot_v< std::uint8_t, std::uint8_t > = (MTL5_QUAD_NATIVE_U8U8 != 0)
constexpr quad_dot_support quad_dot_native_support
constexpr bool has_native_quad_dot = (quad_dot_native_support != quad_dot_support::none)
 Does this build have a native quad multiply-accumulate for ANY pairing?
template<typename T>
constexpr std::size_t width = batch<T>::size
 Lane count of batch<T> for the current build (1 on the scalar fallback).
constexpr hw_traits default_hw_traits
 Generic modern-x86 (AVX2-class) default; override per architecture.
constexpr hw_traits target_hw_traits
 The hardware description default_blocking and detected_hw_traits start from: default_hw_traits, with the register file and accumulator budget MEASURED for the target ISA (#643).
template<typename T>
constexpr blocking_params default_blocking = detail::default_blocking_for_target<T>()

Typedef Documentation

◆ quad_accumulator_t

template<typename A, typename B>
using mtl::simd::quad_accumulator_t = typename quad_accumulator<A, B>::type

◆ widen_accumulator_t

template<typename Narrow>
using mtl::simd::widen_accumulator_t = typename widen_accumulator<Narrow>::type

Enumeration Type Documentation

◆ detect_levels

enum class mtl::simd::detect_levels : unsigned
strong

target_hw_traits with the CACHE fields replaced by what the running machine reports (#222).

Detected once per process; a field the platform could not report keeps its compile-time default, so an undetectable machine behaves exactly as it did before this existed.

The FMA latency/issue width and the vector register file are NOT overridden. They determine mr x nr, which is a template argument of the compiled micro-kernel: a runtime value could not be applied to it, and pretending otherwise would produce a tile the kernel does not implement. Keeping them compile-time is also what guarantees runtime_blocking<T>().mr == default_blocking<T>.mr (likewise nr), so the two agree on the register tile by construction and only the cache blocks can differ. Overlay detected cache figures on a base description, field by field. Pure, so the fallback is testable with hierarchies the host does not have.

A 0 field means NOT DETECTED and keeps the base value.

L3 IS DELIBERATELY NOT APPLIED, though cache_info detects it. l3_bytes feeds only nc, and nc sets the jc BLOCK COUNT, njb = ceil(n/nc) – which the threaded nest hands to jc-teams round-robin, so the critical path is ceil(njb/jc_nt) and an njb that is not a multiple of jc_nt leaves teams unequal. Measured (Xeon E5-2420 v2, 6 threads, m=96 n=16384 k=1024, fp64): applying the detected 15 MB L3 moved nc 2048 -> 3840 and njb 8 -> 5, so two teams took 3 and 2 blocks instead of 4 and 4 – a 1.2x critical path and a measured 10-25% REGRESSION. kc and mc carry no such hazard: they size blocks within a partition rather than the number of partitioned units.

This is #408's failure mode one loop over – there a register-tile change moved mc 64 -> 60 and nib 16 -> 18, unbalancing the ic partition. The ic loop has detail::balanced_mc to absorb it.

THE jc LOOP NOW HAS ONE TOO, AND L3 STILL DOES NOT GO IN. This comment used to end "once a balanced_nc exists, L3 can be applied here". balanced_nc exists and is the DEFAULT (#429/#490, as detail::nc_model::m6_guarded), so that condition has been met – and #479 measured the pairing directly on four microarchitectures before anyone could act on it:

M2 = detected L3 + balanced_nc   102 arms, worst x0.548, 37 regressions
M5 = exact partition             102 arms, worst x0.548, 39 regressions

Up to 45% slower on an i7-12700K. M2 ALREADY INCLUDES the balancing, so the premise – that an unbalanced partition was the only thing making the detected L3 unsafe – is FALSIFIED rather than deferred. Balancing does not rescue it, and the imbalance was never the whole cause: what the same study found is that nc on these machines is too LARGE, not merely ragged, which is why the models that SHRINK it (M3, M4) win where M2 and M5 lose.

So this is not waiting on anything. Reaching for it needs a fresh measurement across >= 3 microarchitectures, not the observation that the old blocker is gone. test_cache_info.cpp asserts it unconditionally and fails first. docs/performance/benchmarking-methodology.md carries the hypothesis register.

A SHARED cache contributes only its per-core share. The BLIS model wants the L1/L2 one core gets to itself, and cache_info reports the instance size alongside how many physical cores share it, so the division happens here where the model is. It matters: an Alder Lake E-cluster publishes one 2 MiB L2 for four cores, and taking that at face value sized mc for roughly 4x the L2 a core actually has (#432). SMT siblings are deliberately NOT counted as sharers – see cache_info – so a hyperthreaded machine is unaffected. Which detected levels may override the compile-time model.

Separable because the four-machine A/B (#430) says the two do NOT behave alike: the Ryzen run moved mc alone (kc identical, mc 64 -> 256) and cost nothing single-threaded, while both machines whose kc moved lost throughput. L1 feeds kc and L2 feeds mc, so splitting the switch turns that observation into an experiment instead of an inference from three machines that each happened to vary something different.

Enumerator
none 
l1 

L1d size/assoc/line -> kc.

l2 

L2 size -> mc.

both 

◆ quad_dot_support

enum class mtl::simd::quad_dot_support
strong

How much of the quad multiply-accumulate this build gets natively.

Three states rather than two, because partial is the COMMON case: every machine measured so far that has the instruction at all has it for some pairings and not others. A benchmark that reports "native" or "decomposed" against a partial build is mislabelling at least one of its arms.

Enumerator
none 

every pairing emulated – e.g. SSE4, AVX2, plain NEON

partial 

some native, some emulated – AVX3_DL, and NEON+DotProd

all 

every pairing native – AVX10.2, or NEON with I8MM

Function Documentation

◆ axpy()

template<typename T>
void mtl::simd::axpy ( T alpha,
const T * x,
T * y,
std::size_t n )

axpy: y[i] += alpha*x[i].

◆ backend_name()

const char * mtl::simd::backend_name ( )
inlinenoexcept

Name of the SIMD backend actually compiled – "scalar", or Highway's target ("AVX3_DL", "AVX2", "SSE4", "NEON", ...).

This exists for PROVENANCE. Whether the int8 kernel got vpdpbusd or a decomposition into vpmaddwd is not visible in a result, only in a throughput number that nobody can check afterwards – and the difference is several times the work. AVX3_DL is the target that has the instruction; anything else on x86 does not, however much AVX-512 the machine supports. #451 phase 4 records this in every benchmark sidecar for that reason.

◆ configured_detect_levels()

detect_levels mtl::simd::configured_detect_levels ( )
constexpr

The levels this translation unit was compiled to detect.

MTL5_ENABLE_CACHE_DETECTION both (unchanged meaning) MTL5_ENABLE_CACHE_DETECTION_L1 kc only – the A/B arm that isolates kc MTL5_ENABLE_CACHE_DETECTION_L2 mc only – the arm that isolates mc

Defining the two single-level macros together is the same as defining the original one.

◆ derive_blocking()

template<typename T>
blocking_params mtl::simd::derive_blocking ( std::size_t nvec,
const hw_traits & hw = default_hw_traits )
constexpr

Derive GEMM blocking parameters for element type T with nvec SIMD lanes (e.g.

simd::width<T>) on hardware hw. constexpr / pure integer.

◆ detected_hw_traits()

const hw_traits & mtl::simd::detected_hw_traits ( )
inline

Runtime cache detection is OPT-IN: define MTL5_ENABLE_CACHE_DETECTION to let the detected hierarchy feed kc/mc.

Without it this returns the compile-time defaults, which is what MTL5 ships.

That is not caution, it is a measurement. Detection was wired in by #426 on the assumption that real cache sizes must beat figures hardcoded to a Haswell-class core. Measured on an i7-12700K (Golden Cove, P-cores pinned, AVX2, fp64, 5 interleaved rounds, min of 5) it LOST on 9 of 10 points:

detected  kc=384 mc=213      default  kc=256 mc=64

shape            T=1     T=8
1024^3          0.965   0.551      <- 3 of 8 threads left idle
2048^3          0.920   0.929
4096^3          0.901   0.985
852 x 8192      0.914   0.806      <- two 12.6 MB B panels in a 25 MB L3
852 x 12288     0.906   0.675
(ratio = detected / default throughput; < 1 is a loss)

Three distinct causes, and only the first is about cache sizing:

  1. At T=1 the larger kc/mc are slower at every size – 3.5% at 1024^3 and 8-10% at the rest. The analytical "half of L1, half of L2" sizing is beaten by the hand-tuned constants on this microarchitecture. No threading is involved.
  2. A larger mc means FEWER ic blocks, and gemm_blocked fixes ic_nt = min(budget, nib) from nib BEFORE balanced_mc runs. At 1024^3, mc 64 -> 213 takes nib 16 -> 5, so five threads of eight do the work.
  3. A larger kc inflates the packed B panel (kc x nc), and once the jc loop splits into teams each team holds one: 2 x 12.6 MB against a 25 MB L3.

(2) and (3) are the #408 / #429 family – a cache-derived parameter moving a BLOCK COUNT that the thread partition is sensitive to. They are real defects independent of detection, and detection is what exposed them.

So the machinery stays, tested and available, and the shipped blocking is byte-identical to pre-#426 until the model is shown to win on hardware. benchmarks/run_blocking_ab.sh is how that is decided; #430 is the experiment.

◆ exp() [1/2]

template<typename T>
requires (std::is_same_v<T, float> || std::is_same_v<T, double>)
batch< T > mtl::simd::exp ( batch< T > x)

e^x lane by lane, for float and double lanes.

See the header comment for the algorithm, the accuracy and the special values.

◆ exp() [2/2]

template<typename T>
requires (std::is_same_v<T, float> || std::is_same_v<T, double>)
void mtl::simd::exp ( const T * x,
T * out,
std::size_t n )

out[i] = e^x[i] for i < n, by exp(batch<T>).

The last n mod W elements go through a batch padded with zeros, so every element is computed by the same per-lane algorithm, whatever its index. x and out may be the same array.

◆ has_level()

bool mtl::simd::has_level ( detect_levels set,
detect_levels one )
constexpr

◆ operator|()

detect_levels mtl::simd::operator| ( detect_levels a,
detect_levels b )
constexpr

◆ quad_dot_support_name()

const char * mtl::simd::quad_dot_support_name ( )
inlinenoexcept

"NATIVE", "PARTIAL" or "DECOMPOSED" – the token a benchmark sidecar records.

◆ reduce_dot()

template<typename T>
T mtl::simd::reduce_dot ( const T * a,
const T * b,
std::size_t n )

dot product: sum_i a[i]*b[i] (real float/double, or int32/uint32 mod 2^32).

◆ reduce_dot_block_scaled()

template<typename NA, typename NB = NA>
float mtl::simd::reduce_dot_block_scaled ( const NA * a,
const NB * x,
const float * scales,
std::size_t n_blocks,
std::size_t block )

Block-scaled integer dot product (#628):

sum_b  scales[b] * ( sum_{j < block}  a[b*block + j] * x[b*block + j] )

over n_blocks consecutive blocks of block elements. This is the inner product of every block-quantized format – GGML's Q4_0/Q8_0 and Q4_K/Q6_K, OCP MXINT8, the 16-wide NVIDIA-style blocks – once the caller has decoded the per-block scale (fp16, E8M0, ...) into scales[b], typically scale_w[b] * scale_a[b]. Taking a decoded float keeps MTL5 format-agnostic.

The operand pairs are reduce_dot_widen's: int16 x int16 and uint16 x uint16 (pairwise widen), and int8 x int8, uint8 x int8 and uint8 x uint8 (quad widen). (int8, uint8) is refused for the same reason it is there: swap the arguments, which a dot product permits, and get the native instruction.

WHY NOT ONE reduce_dot_widen PER BLOCK. That is exact, and it is what callers had to write before this existed, but every call ends in a horizontal reduce, and for a short block the reduce IS the cost: a 32-element block is one AVX2 iteration, so wider vectors buy nothing (synapse measured its 32-wide formats at 0.83x of simply dequantizing to float). Here each block's integer sum stays in vector lanes and is converted and scaled in-register:

acc = fma(convert(block_lanes), broadcast(scales[b]), acc)

with one horizontal reduce for the whole call.

WHAT IS EXACT AND WHAT IS NOT. Each block's integer sum is exact (the widening accumulate's mod-2^32 argument). Two float steps follow, and both round: converting each LANE's partial sum to float (exact while it is below 2^24 in magnitude), and the fma accumulation across blocks. So the result equals the per-block loop up to float re-association, not bit for bit.

DETERMINISM. The order of every float operation is fixed for a given target: blocks are assigned round-robin to four float accumulators by block index, combined as (0 + 1) + (2 + 3), then reduced horizontally, and the scalar remainders (below), accumulated in block order, are added last. Nothing depends on alignment or on data, so the result is bit-for-bit reproducible run to run on one target. It is NOT reproducible across targets: the vector width, and for 16-bit operands the ISA's choice of which product lands in which lane, decide how a block's exact total is split across lanes, and so how the conversion rounds.

BLOCKS OF ANY LENGTH. Within a block, the largest multiple of the step (widen_step or quad_step) runs in vector lanes; the rest is summed exactly in a scalar integer, scaled, and accumulated in a separate float scalar. So every block length is correct on every target, and a block that is a multiple of the step pays nothing for the remainder path. A block SHORTER than one step is all remainder: correct, but not accelerated on that target (8-bit operands on AVX-512 have a 64-element step, so a 32-wide Q8_0 block falls here).

THE OVERFLOW PRECONDITION is per block, and stricter than reduce_dot_widen's "the sum wraps". Each lane's partial sum is converted separately, so a block whose sum leaves the accumulator does not produce the wrapped total. It produces an unspecified value, deterministic on one target. Require

sum_{j in block} |a_j * x_j|  <  2^31   (2^32 for the unsigned pairs)

which bounds every lane's partial sum as well as the total. For scale: int16 weights with |w| <= 2^12 against 8-bit activations stay below 2^28 at a 256-element block; int8 x int8 allows blocks of ~131 000.

◆ reduce_dot_widen() [1/2]

template<typename Wide, typename NA, typename NB = NA>
requires std::is_integral_v<Wide>
Wide mtl::simd::reduce_dot_widen ( const NA * a,
const NB * b,
std::size_t n )

Widening INTEGER dot product: 16-bit operands accumulated in 32 bits, via the hardware's widening multiply-accumulate (#451 phase 2).

int16 -> int32 and uint16 -> uint32. One instruction does the widening, the multiply and the accumulate – vpmaddwd on x86, SMLAL/UMLAL on NEON – so this is the first kernel here whose ISA support is the point rather than an implementation detail.

THE OVERFLOW CONTRACT

Two separate questions, with very different answers.

  • The PRODUCTS are always exact. |-2^15 * -2^15| = 2^30 fits an int32 with a bit to spare, so no individual term is ever lost.
  • The SUM wraps, and it wraps SOON. Full-range operands consume 31 of the accumulator's 32 bits per term, so the worst case (every term the same sign, both operands at the limit) overflows at k = 3. Random signed data does better – the terms random-walk rather than march, so |sum| grows like sqrt(k) – but only to roughly k = 36 before it is more likely than not to have exceeded 2^31.

So this is NOT the "accumulate 65 000 terms safely" case; that is int8 into int32 (phase 3), where a product needs 15 bits and leaves 16 of headroom, and it is why the VNNI and SDOT instructions are shaped the way they are. What makes 16-bit accumulation useful is small operands, not short vectors: at b bits of operand magnitude the headroom is about 2^(31-2b) terms, so 8-bit values in int16 storage give ~2^15 terms and 4-bit values give ~2^23.

When the sum does exceed the accumulator it WRAPS, exactly as everywhere else in mtl::simd – the result is the true sum reduced mod 2^32, never a trap, never a saturation, never undefined. Callers who need the full range should widen their storage, not hope. tests/unit/simd/test_int_accumulation.cpp pins the measured envelope for the 64-bit case.

LANE ORDER IS UNSPECIFIED AND THAT IS FINE. The hardware instruction is free to permute which product lands in which accumulator lane, and Highway promises only that the total is right. For a floating accumulator that would make the result ISA-dependent; here it is unobservable, because addition mod 2^32 is associative and commutative. Phase 0's contract is what lets this kernel use the instruction without a caveat. 8-BIT OPERANDS (#451 phase 3) are the same function with a different instruction and a very different headroom, so the two live together here.

16-bit: pairwise widen (vpmaddwd), 2 products per 32-bit lane 8-bit: quad widen (VNNI vpdpbusd, NEON SDOT), 4 products per lane

And the headroom is where the 8-bit case earns "the one that matters":

| pairing | max |product| | terms before the sum can overflow | |-------------—|-------------—|--------------------------------—| | int16 x int16 | 2^30 | 3 worst case, ~36 random | | int8 x int8 | 2^14 | ~131 000 | | uint8 x int8 | ~2^15 | ~65 000 | | uint8 x uint8 | ~2^16 | ~66 000 |

Four to five orders of magnitude, from the same 32-bit accumulator, purely because the operands are half as wide and the product therefore a quarter the size. This is why the quantized-inference instructions are 8-bit and not 16-bit, and why phase 2's kernel is useful only for small operands while this one is useful for real vectors.

THE SIGNEDNESS ASYMMETRY IS THE HARDWARE'S. VNNI implements unsigned x signed – unsigned activations against signed weights – and symmetric i8 x i8 only from AVX10.2. Before that Highway emulates the symmetric form with two dpbusd calls plus a shift and subtract, roughly three times the work. (int8, uint8) is rejected rather than silently reordered: the dot is symmetric, so swapping the arguments gets the native instruction, and a compile error saying so beats a quiet detour through the emulation.

◆ reduce_dot_widen() [2/2]

template<typename Wide, typename Narrow>
requires std::is_floating_point_v<Wide>
Wide mtl::simd::reduce_dot_widen ( const Narrow * a,
const Narrow * b,
std::size_t n )

Widening dot product: sum_i a[i]*b[i] with Narrow elements (e.g.

float) accumulated in a wider Wide (e.g. double). Each load widens Wide-lane- count narrow values to Wide lanes, then FMAs into Wide accumulators – so mixed-precision dot keeps the wide accumulator's accuracy at SIMD speed, instead of the scalar policy fallback. (On the scalar batch backend this is the correct scalar widening loop.) (Integer overload below; this one is the floating-point case.)

◆ reduce_sum_squares()

template<typename T>
T mtl::simd::reduce_sum_squares ( const T * a,
std::size_t n )

sum of squares: sum_i a[i]*a[i] (two_norm computes sqrt of this).

◆ runtime_blocking()

template<typename T>
const blocking_params & mtl::simd::runtime_blocking ( )
inline

Blocking parameters for T derived against the DETECTED hardware.

Use kc and mc from this; take mr/nr from default_blocking<T> (the tile the micro-kernel was instantiated with) and nc from there too, since L3 is not overridden – see with_detected_caches. Computed once per element type.

◆ scal()

template<typename T>
void mtl::simd::scal ( T alpha,
T * x,
std::size_t n )

scal: x[i] *= alpha.

◆ scalar_fma()

template<typename T>
T mtl::simd::scalar_fma ( T a,
T b,
T c )
inline

The SCALAR equivalent of fma(batch,batch,batch) – what one lane of it does.

A kernel's vector body and its scalar tail must agree, or an element's rounding depends on its INDEX: the first W*floor(n/W) elements are computed one way and the last n mod W another (#512).

IT IS THE PRIMITIVE ITSELF, not a reimplementation of it. Broadcasting to a batch, applying the same fma, and taking a lane back is exact – every lane holds the identical value, so reduce_max recovers it – and it CANNOT drift from the body, because it is the body's operation.

The first version of this did try to mirror the branch, guarded on HWY_NATIVE_FMA, on the reasoning that Highway's MulAdd is a hardware FMA only where the target has one. CI falsified it: on the clang Highway lane the batch fma fused while the guard read the macro as absent, so the mirror took the decomposed branch and disagreed with the very operation it was mirroring. A predicate that has to stay in sync with a third-party header's per-target macros is the wrong mechanism when the operation itself is one call away.

The tail runs at most W-1 times per kernel, so the broadcast and horizontal reduce are not worth avoiding for a correctness property.

Integer lanes need no special case: fma on an integer batch is the wrapping multiply-add, exact mod 2^N on both backends, which is the #451/#460 contract.

◆ target_vec_registers()

std::size_t mtl::simd::target_vec_registers ( )
constexpr

The architectural vector register file of the build's target ISA (#643).

default_hw_traits describes ONE machine, a Haswell-class AVX2 core, and its 16 is right for AVX2's 16 ymm. It is wrong for the 32-register ISAs: aarch64 (v0-v31, which SVE's z0-z31 alias) and AVX-512 with VL (zmm0-zmm31, and xmm/ymm16-31 under EVEX). Sized against 16, those builds got AVX2's 12 accumulator vectors out of 32: 6x4 on NEON and 6x16 on AVX-512.

Compile-time only, read from the compiler's target macros: the tile is a micro-kernel template argument, so it can follow what the compiler was told to emit and nothing the running machine reports.

◆ vectorizable_length()

std::size_t mtl::simd::vectorizable_length ( std::size_t n,
std::size_t w )
constexprnoexcept

Largest multiple of W not exceeding n – the SIMD body length; iterate the remainder [vectorizable_length(n) .

. n) scalar: for (i = 0; i < vectorizable_length(n, W); i += W) { ... } // SIMD for (; i < n; ++i) { ... } // tail

◆ with_detected_caches()

hw_traits mtl::simd::with_detected_caches ( hw_traits base,
const util::cache_info & c,
detect_levels lv = detect_levels::both )
constexpr

Overlay the detected hierarchy on base, one level group at a time.

line_bytes travels with the L1 group: it is read from the same descriptor, and on every machine measured so far it is 64 – identical to the default – so it cannot confound an L1-vs-L2 comparison in practice.

NOTE for anyone reading a split run: mc is derived from L2 given kc (mc ~ l2 / (2 * kc * sizeof)), so the l2-only arm does not reproduce the mc of the both arm. On the i7 the both arm gets mc=213 from kc=384, while the l2-only arm gets mc=320 from the default kc=256. That is the honest question – "what does this L2 imply for the blocking we ship?" – not a defect, but it does mean the four arms are not a clean 2x2 of the same numbers.

Variable Documentation

◆ default_blocking

template<typename T>
blocking_params mtl::simd::default_blocking = detail::default_blocking_for_target<T>()
inlineconstexpr

◆ default_hw_traits

hw_traits mtl::simd::default_hw_traits
inlineconstexpr
Initial value:
{
4, 2,
32u * 1024, 8, 64,
256u * 1024,
8u * 1024 * 1024,
4096,
16,
0,
}

Generic modern-x86 (AVX2-class) default; override per architecture.

Matches a Haswell-class core: 32 KB/8-way L1, 256 KB L2, 8 MB L3, FMA latency 4, 2 FMA units, 16 ymm registers.

◆ has_native_quad_dot

bool mtl::simd::has_native_quad_dot = (quad_dot_native_support != quad_dot_support::none)
inlineconstexpr

Does this build have a native quad multiply-accumulate for ANY pairing?

Retained with its original meaning, which is what every existing caller and every committed sidecar assumed: it is the "did this build get the instruction at all" question. It is deliberately NOT "all pairings" – that would read false on Zen 4 and on a Cortex-A78 alike and erase the distinction the sidecars exist to record. Use has_native_quad_dot_v<NA, NB> to say anything about a specific arm, and quad_dot_native_support to report.

◆ has_native_quad_dot_v

template<typename NA, typename NB>
bool mtl::simd::has_native_quad_dot_v = false
inlineconstexpr

Does this build have a NATIVE quad multiply-accumulate for the pairing (NA, NB), or will Highway emulate it?

See the table above.

False for any pairing the op does not accept at all – (i8, u8) is absent on purpose, so it reads as "not native", which is true and is also what a caller should act on.

◆ has_native_quad_dot_v< std::int8_t, std::int8_t >

template<>
bool mtl::simd::has_native_quad_dot_v< std::int8_t, std::int8_t > = (MTL5_QUAD_NATIVE_I8I8 != 0)
inlineconstexpr

◆ has_native_quad_dot_v< std::uint8_t, std::int8_t >

template<>
bool mtl::simd::has_native_quad_dot_v< std::uint8_t, std::int8_t > = (MTL5_QUAD_NATIVE_U8I8 != 0)
inlineconstexpr

◆ has_native_quad_dot_v< std::uint8_t, std::uint8_t >

template<>
bool mtl::simd::has_native_quad_dot_v< std::uint8_t, std::uint8_t > = (MTL5_QUAD_NATIVE_U8U8 != 0)
inlineconstexpr

◆ is_integer_lane_v

template<typename T>
bool mtl::simd::is_integer_lane_v = is_lane_v<T> && std::is_integral_v<T>
inlineconstexpr

True for the integer lane types – the ones whose arithmetic WRAPS.

◆ is_lane_v

template<typename T>
bool mtl::simd::is_lane_v
inlineconstexpr
Initial value:
=
std::is_same_v<T, float> || std::is_same_v<T, double> ||
std::is_same_v<T, std::int32_t> || std::is_same_v<T, std::uint32_t>

Lane types batch<T> supports.

Real floating point (the dense-BLAS case), plus 32-bit integers as of phase 0 of the integer-lane epic (#451). The integer entry is deliberately narrow:

  • 8- and 16-bit lanes are excluded because their VALUE is the widening accumulate (vpdpbusd / vpdpwssd / SDOT), not the plain lane – and x86 has no 8-bit vector multiply at all. They arrive with the accumulator contract, in phases 2-3.
  • 64-bit lanes are excluded because Mul is emulated on x86 before AVX512DQ, so a batch<int64_t> would advertise a vector multiply that is a scalar loop in disguise.

32-bit is exactly the width where the multiply is a single native instruction on every target ISA (vpmulld / NEON mul.4s), which is what makes it the honest plumbing case.

The floating half lists float and double rather than asking std::is_floating_point_v, which also admits long double. Highway has no long double vector type, so batch<long double> is a hard compile error there – and since this trait gates SimdDenseVector, saying otherwise sent dot(dense_vector<long double>, ...) into reduce_dot<long double> and broke a build that previously worked, because the old BlasDenseVector gate kept it on the generic loop. The static_assert below already claimed "float, double, int32_t and uint32_t"; this is the trait finally agreeing with it.

◆ is_quad_widenable_v

template<typename T>
bool mtl::simd::is_quad_widenable_v
inlineconstexpr
Initial value:
=
std::is_same_v<T, std::int8_t> || std::is_same_v<T, std::uint8_t>

Narrow integer types whose dot product widens by FOUR into a 32-bit lane, via the hardware's quad multiply-accumulate (#451 phase 3) – VNNI's vpdpbusd on x86, SDOT/UDOT on NEON.

Four products are summed into one accumulator lane by a single instruction.

This is the case the epic calls "the one that matters", and the reason is headroom rather than lane count: an 8-bit product needs 15 bits, leaving ~16 in an int32 accumulator – of the order of 10^5 terms, against the handful that 16-bit operands afford (see reduce_dot_widen in simd/algorithm.hpp).

◆ is_widenable_v

template<typename T>
bool mtl::simd::is_widenable_v
inlineconstexpr
Initial value:
=
std::is_same_v<T, std::int16_t> || std::is_same_v<T, std::uint16_t>

Narrow integer types that can be WIDENED into a lane by a dot-product accumulate (#451 phase 2).

These are storage/operand types, not lanes: batch<std::int16_t> does not exist and is not wanted, because a 16-bit lane's value is the widening accumulate, not the plain multiply.

◆ quad_dot_native_support

quad_dot_support mtl::simd::quad_dot_native_support
inlineconstexpr
Initial value:
= [] {
if constexpr (a && b && c) return quad_dot_support::all;
else if constexpr (a || b || c) return quad_dot_support::partial;
}()
@ partial
some native, some emulated – AVX3_DL, and NEON+DotProd
Definition batch.hpp:839
@ none
every pairing emulated – e.g. SSE4, AVX2, plain NEON
Definition batch.hpp:838
@ all
every pairing native – AVX10.2, or NEON with I8MM
Definition batch.hpp:840
constexpr bool has_native_quad_dot_v
Does this build have a NATIVE quad multiply-accumulate for the pairing (NA, NB), or will Highway emul...
Definition batch.hpp:815

◆ target_hw_traits

hw_traits mtl::simd::target_hw_traits
inlineconstexpr
Initial value:
= [] {
return hw;
}()
constexpr hw_traits default_hw_traits
Generic modern-x86 (AVX2-class) default; override per architecture.
Definition blocking.hpp:95
Hardware description for blocking-parameter derivation. All sizes in bytes.
Definition blocking.hpp:68

The hardware description default_blocking and detected_hw_traits start from: default_hw_traits, with the register file and accumulator budget MEASURED for the target ISA (#643).

aarch64 (NEON) 32 registers, 16 accumulators -> 8x4 fp64, 8x8 fp32 AVX-512 unchanged from AVX2: 6x16 fp64 everything else unchanged

The sweep that set these (fp64, default mr = 6 against 8, 10, 12, 14 at fixed nr/kc/mc/nc; median ratio to mr = 6, geometric mean over the shapes):

Arduino Ventuno Q, 4x Cortex-A78C, T=1 / T=4 fp64 8x4 16 acc 1.174 / 1.102 every shape >= 1.06 10x4 20 acc 1.156 / 1.045 12x4 24 acc 1.061 / 0.930 worst 0.840 14x4 28 acc 1.049 / 0.878 worst 0.736 fp32 8x8 16 acc 1.170 / 1.219 every shape >= 1.15 10x8 20 acc 1.171 / 1.160 12x8 24 acc 1.063 / 1.206 14x8 28 acc 1.030 / 1.028 worst 0.930

Ryzen 9 8945HS, Zen 4, AVX-512, T=1 / T=8 (WSL, 5-40% round-to-round spread) 8x16 16 acc 0.995 / 1.046 12x16 24 acc 1.032 / 1.021

So the NEON peak is at HALF the file, not #351's 3/4 – the rule the register file alone would apply (12x4) loses up to 16% at T=4. That loss is a block divisibility effect, the #408 family again: on the wide 64-row shapes each of four threads gets a 16-row block, which 8-row panels tile exactly and 12-row panels cover with 24 rows of work. Hence an explicit budget instead of a fraction. On AVX-512 no tile cleared the noise, which is the measured reason to leave it at the AVX2 description.

NOT MEASURED: other aarch64 cores (Apple M-series, Neoverse) and the Jetson's A78AE. docs/benchmarks/systems.md lists the runs.

MTL5_GEMM_TARGET_REGISTER_FILE replaces this with the full register file at the 3/4 rule, i.e. 12xnr on NEON and AVX-512: the bench_blocking_ab isaregs arm, kept so the rejected candidate stays measurable.

◆ width

template<typename T>
std::size_t mtl::simd::width = batch<T>::size
inlineconstexpr

Lane count of batch<T> for the current build (1 on the scalar fallback).