STRA-338 size-nce: SAFETY comments on the new unsafe helper bodies in rust_iq and rust_k_blocks

The helpers split out of dot_multi512_impl, dot_single_iq3, iq4xs, dot_multi256, dot_multi512 and the K-block dot wrap their
bodies in `unsafe { }` as the surrounding code does; each now carries a SAFETY line (target features cover the intrinsics and
helpers called, loads follow the file's Bounds note). Comments only; harness re-run identical.
This commit is contained in:
Pavlo Ratushnyi
2026-11-06 23:38:37 +01:00
parent 3e950869e0
commit 6fa7b73e01
3 changed files with 11 additions and 0 deletions
@@ -36,6 +36,7 @@ pub(super) unsafe fn dot_multi512_impl<const T: i32, const N: usize>(
/// The exact i32 accumulators of one weight block `b` against block `i` of each of the `N` tokens' activation rows.
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
unsafe fn sum_block512<const T: i32, const N: usize>(b: &[u8], ys: &[&[Q8K]], i: usize, fast: bool) -> [__m512i; 8] {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
let zero = _mm512_setzero_si512();
let mut ai = [_mm512_setzero_si512(); 8];
@@ -54,6 +55,7 @@ unsafe fn sum_block512<const T: i32, const N: usize>(b: &[u8], ys: &[&[Q8K]], i:
/// The 64 magnitude bytes, 32 16-bit scales and 64 sign bits of group `j` of block `b`.
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
unsafe fn operands512<const T: i32>(b: &[u8], j: usize, fast: bool) -> (__m512i, __m512i, u64) {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
// IQ3 formats (`iq3_multi_fast`): both halves' grid words straight into the 512-bit register, lane order =
// `decode`'s byte order
@@ -63,6 +65,7 @@ unsafe fn operands512<const T: i32>(b: &[u8], j: usize, fast: bool) -> (__m512i,
}
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
unsafe fn register_operands512<const T: i32>(b: &[u8], j: usize) -> (__m512i, __m512i, u64) {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
let (g0, s0, c0) = iq3_half::<T>(b, j, 0);
let (g1, s1, c1) = iq3_half::<T>(b, j, 1);
@@ -75,6 +78,7 @@ unsafe fn register_operands512<const T: i32>(b: &[u8], j: usize) -> (__m512i, __
}
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
unsafe fn stack_operands512(t: i32, b: &[u8], j: usize) -> (__m512i, __m512i, u64) {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
let (g0, s0, sc0) = decode(t, b, j, 0);
let (g1, s1, sc1) = decode(t, b, j, 1);
@@ -97,6 +101,7 @@ unsafe fn stack_operands512(t: i32, b: &[u8], j: usize) -> (__m512i, __m512i, u6
/// The 64 grid bytes of an IQ3_XXS (18) / IQ3_S (21) group straight from the grid table (`idx` <= 255 / <= 511).
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
unsafe fn gather_grid512(t: i32, b: &[u8], j: usize) -> __m512i {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
let q = &b[2 + 16 * j..];
let mut idx = _mm512_cvtepu8_epi32(_mm_loadu_si128(q.as_ptr().cast()));
@@ -170,6 +175,7 @@ pub(super) unsafe fn dot_single_iq3<const T: i32>(row: &[u8], y: &[Q8K]) -> f32
/// The two exact i32 accumulators (one per half) of one IQ3 weight block `b` against the activation block `yi`.
#[target_feature(enable = "avx2,fma,f16c")]
unsafe fn sum_block_iq3<const T: i32>(b: &[u8], yi: &Q8K) -> [__m256i; 2] {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
let mut a = [_mm256_setzero_si256(); 2];
for j in 0..4 {
@@ -205,6 +211,7 @@ pub(super) unsafe fn iq4xs(row: &[u8], y: &[Q8K]) -> f32 {
/// The two exact i32 accumulators (one per half) of one IQ4_XS weight block `b` against the activation block `yi`.
#[target_feature(enable = "avx2,fma,f16c")]
unsafe fn sum_block_iq4xs(values: __m128i, m4: __m128i, b: &[u8], yi: &Q8K) -> [__m256i; 2] {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
let mut sh = u16(b, 2);
let mut sums = [_mm256_setzero_si256(); 2];
@@ -172,6 +172,7 @@ pub(in super::super) unsafe fn dot_multi256(t: i32, row: &[u8], ys: &[&[Q8K]], o
/// The exact i32 accumulators of one weight block `b` against block `i` of every token's activation row.
#[target_feature(enable = "avx2,fma,f16c")]
unsafe fn sum_block256(t: i32, b: &[u8], ys: &[&[Q8K]], i: usize) -> [__m256i; 8] {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
let mut ai = [_mm256_setzero_si256(); 8];
for j in 0..4 {
@@ -205,6 +206,7 @@ pub(in super::super) unsafe fn dot_multi512(t: i32, row: &[u8], ys: &[&[Q8K]], o
/// `dot_multi512` of one format: the token count (1..=8) picks the monomorphised kernel.
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
unsafe fn dot_multi512_by_tokens<const T: i32>(row: &[u8], ys: &[&[Q8K]], out: &mut [f32]) {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
match ys.len() {
1 => dot_multi512_tn::<T, 1>(row, ys, out),
@@ -150,6 +150,7 @@ unsafe fn q5k_mins_term(x: &[u8], y: &Q8K) -> f32 {
#[target_feature(enable = "avx2")]
#[inline]
unsafe fn group_products(kind: i32, u: &Unpacked, y: &Q8K, at: usize) -> [__m256i; 4] {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
let mut p = [_mm256_setzero_si256(); 4];
for j in 0..4 {
@@ -191,6 +192,7 @@ unsafe fn q6k_offset(y: &Q8K, sc: &[i16; 16]) -> __m256i {
#[target_feature(enable = "avx2")]
#[inline]
unsafe fn sum_block(kind: i32, u: &Unpacked, y: &Q8K) -> __m256i {
// SAFETY: this fn's target features cover the intrinsics and helpers below; the loads follow the Bounds note above.
unsafe {
let mut sum = _mm256_setzero_si256();
for group in 0..2 {