interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / crates / aienos-infer / examples / dot_bench.rs

85 lines · 3429 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Row-dot microbenchmark, one thread: ns per Q4_K / Q6_K row dot of 8192
17//! weights, scalar f32 path vs SDOT path (aienos#34 PI5-6). Random blocks
18//! with a fixed seed; `AIENOS_DOT_ITERS` (default 20000) rows per timing.
19//! Prints `key: value` lines. The SDOT numbers appear only when the CPU has
20//! dotprod (runtime detection on the host).
21
22use aienos_infer::dotprod::{dot_row_q8, Q8Act};
23use aienos_infer::quant::{dot_row, Q4K_BYTES, Q6K_BYTES, QK_K};
24use aienos_infer::{DotProd, GgmlType};
25use std::hint::black_box;
26use std::time::Instant;
27
28fn main() {
29 let iters: usize = std::env::var("AIENOS_DOT_ITERS")
30 .ok()
31 .and_then(|v| v.parse().ok())
32 .unwrap_or(20000);
33 let nb = 32; // 8192 weights, the Llama-3.2-1B FFN width
34 let mut s = 0x9e37_79b9_7f4a_7c15u64;
35 let mut rnd = || {
36 s ^= s >> 12;
37 s ^= s << 25;
38 s ^= s >> 27;
39 (s.wrapping_mul(0x2545_f491_4f6c_dd1d) >> 32) as u32
40 };
41 let x: Vec<f32> = (0..nb * QK_K)
42 .map(|_| (rnd() as f32 / u32::MAX as f32) * 2.0 - 1.0)
43 .collect();
44 let mut q8 = Q8Act::with_capacity(x.len());
45 q8.quantize(&x).unwrap();
46 #[cfg(target_arch = "aarch64")]
47 let tok = DotProd::new(std::arch::is_aarch64_feature_detected!("dotprod"));
48 #[cfg(not(target_arch = "aarch64"))]
49 let tok: Option<DotProd> = None;
50 println!("dotprod: {}", tok.is_some());
51 println!("weights_per_row: {}", nb * QK_K);
52 for (ty, bb, name) in [
53 (GgmlType::Q4K, Q4K_BYTES, "q4k"),
54 (GgmlType::Q6K, Q6K_BYTES, "q6k"),
55 ] {
56 let mut row: Vec<u8> = (0..nb * bb).map(|_| rnd() as u8).collect();
57 for b in row.chunks_exact_mut(bb) {
58 // sane f16 scales (0.0625) so values stay finite
59 let at = if ty == GgmlType::Q4K { 0 } else { 208 };
60 b[at..at + 2].copy_from_slice(&[0x00, 0x2c]);
61 if ty == GgmlType::Q4K {
62 b[2..4].copy_from_slice(&[0x00, 0x2c]);
63 }
64 }
65 let t0 = Instant::now();
66 let mut acc = 0f32;
67 for _ in 0..iters {
68 acc += dot_row(ty, black_box(&row), black_box(&x)).unwrap();
69 }
70 let scalar = t0.elapsed().as_nanos() as f64 / iters as f64;
71 black_box(acc);
72 println!("{name}_scalar_f32_ns_per_row: {scalar:.1}");
73 if let Some(t) = tok {
74 let t0 = Instant::now();
75 let mut acc = 0f32;
76 for _ in 0..iters {
77 acc += dot_row_q8(t, ty, black_box(&row), black_box(&q8)).unwrap();
78 }
79 let sdot = t0.elapsed().as_nanos() as f64 / iters as f64;
80 black_box(acc);
81 println!("{name}_sdot_ns_per_row: {sdot:.1}");
82 println!("{name}_speedup: {:.2}", scalar / sdot);
83 }
84 }
85}