MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / isa_dispatch.mojo
| 1 | """MojoLlama ISA Dispatch — comptime SIMD width selection and kernel parameterization. |
| 2 | |
| 3 | Dispatches between AVX-512 (F32x16) and AVX2 (F32x8) based on comptime target detection. |
| 4 | All kernels use parameteric SIMD width for maximum portability and performance. |
| 5 | |
| 6 | Architecture priority: |
| 7 | AVX-512-VNNI > AVX-512-F > AVX2+FMA > AVX2 > SSE2 > scalar |
| 8 | |
| 9 | llama.cpp uses separate compiled files per ISA path; we use comptime branching. |
| 10 | """ |
| 11 | from std.sys.info import CompilationTarget |
| 12 | |
| 13 | # ─── ISA Feature Detection ──────────────────────────────────────────── |
| 14 | |
| 15 | @always_inline |
| 16 | fn has_avx512f() -> Bool: |
| 17 | """Check if target supports AVX-512F (foundation).""" |
| 18 | return CompilationTarget.has_avx512f() |
| 19 | |
| 20 | @always_inline |
| 21 | fn has_avx512vnni() -> Bool: |
| 22 | """Check if target supports AVX-512-VNNI (int8 dot product).""" |
| 23 | return CompilationTarget.has_avx512vnni() |
| 24 | |
| 25 | @always_inline |
| 26 | fn has_avx2() -> Bool: |
| 27 | return CompilationTarget.has_avx2() |
| 28 | |
| 29 | @always_inline |
| 30 | fn has_fma() -> Bool: |
| 31 | return CompilationTarget.has_fma() |
| 32 | |
| 33 | # ─── SIMD Width Selection ───────────────────────────────────────────── |
| 34 | |
| 35 | alias SIMD_WIDTH_AVX2 = 8 # 256 bits / 32 = 8 floats |
| 36 | alias SIMD_WIDTH_AVX512 = 16 # 512 bits / 32 = 16 floats |
| 37 | alias SIMD_WIDTH_NEON = 4 # 128 bits / 32 = 4 floats (ARM) |
| 38 | |
| 39 | comptime fn get_simd_width() -> Int: |
| 40 | """Select SIMD width based on target ISA. |
| 41 | AVX-512: 16 floats per vector (2x AVX2 throughput) |
| 42 | AVX2: 8 floats per vector |
| 43 | Fallback: 4 floats (SSE2/NEON) |
| 44 | """ |
| 45 | if has_avx512f(): |
| 46 | return SIMD_WIDTH_AVX512 |
| 47 | elif has_avx2(): |
| 48 | return SIMD_WIDTH_AVX2 |
| 49 | else: |
| 50 | return 4 |
| 51 | |
| 52 | alias SIMD_WIDTH = get_simd_width() |
| 53 | alias F32xW = SIMD[DType.float32, SIMD_WIDTH] |
| 54 | alias I8xW = SIMD[DType.int8, SIMD_WIDTH] |
| 55 | alias U8xW = SIMD[DType.uint8, SIMD_WIDTH] |
| 56 | alias I32xW = SIMD[DType.int32, SIMD_WIDTH] |
| 57 | |
| 58 | # ─── ISA Info String ────────────────────────────────────────────────── |
| 59 | |
| 60 | fn isa_info() -> String: |
| 61 | """Return human-readable ISA info string.""" |
| 62 | comptime var parts = List[String]() |
| 63 | if has_avx512vnni(): |
| 64 | parts.append("AVX-512-VNNI") |
| 65 | if has_avx512f(): |
| 66 | parts.append("AVX-512F") |
| 67 | if has_avx2(): |
| 68 | parts.append("AVX2") |
| 69 | if has_fma(): |
| 70 | parts.append("FMA") |
| 71 | if len(parts) == 0: |
| 72 | return "scalar" |
| 73 | return "|".join(parts) |
| 74 | |
| 75 | fn simd_info() -> String: |
| 76 | """Return SIMD configuration info.""" |
| 77 | return "SIMD width=" + String(SIMD_WIDTH) + " floats (" + String(SIMD_WIDTH * 32) + " bits) [" + isa_info() + "]" |
| 78 | |
| 79 | # ─── Utility: Prefetch Hint ─────────────────────────────────────────── |
| 80 | |
| 81 | @always_inline |
| 82 | fn prefetch_read(ptr: UnsafePointer[UInt8, MutAnyOrigin], offset: Int): |
| 83 | """Emit prefetch hint for sequential read access. |
| 84 | Uses __builtin_prefetch equivalent for cache warming. |
| 85 | """ |
| 86 | # Mojo doesn't expose __builtin_prefetch yet, but we can use |
| 87 | # volatile read trick to hint the hardware prefetcher |
| 88 | pass # Placeholder — will use intrinsics when available |
| 89 | |
| 90 | # ─── Utility: Byte Swap for Big-Endian ───────────────────────────────── |
| 91 | |
| 92 | @always_inline |
| 93 | fn bswap16(x: UInt16) -> UInt16: |
| 94 | """Byte-swap a 16-bit value for big-endian GGUF fields.""" |
| 95 | return ((x & 0xFF) << 8) | ((x >> 8) & 0xFF) |
| 96 | |
| 97 | # ─── Half-Precision Float Conversion ────────────────────────────────── |
| 98 | |
| 99 | @always_inline |
| 100 | fn f16_to_f32(h: UInt16) -> Float32: |
| 101 | """Convert IEEE 754 half-precision to single-precision. |
| 102 | Uses F16C (VCVTPH2PS) on AVX2+ targets, software fallback otherwise. |
| 103 | This is the per-scalar version; for bulk conversion see f16x8_to_f32x8. |
| 104 | """ |
| 105 | from std.memory import bitcast |
| 106 | comptime if CompilationTarget.has_avx2(): |
| 107 | # F16C path: hardware float16 is stored as UInt16, bitcast to DType.float16 |
| 108 | # then implicit conversion to Float32 via Mojo's type system |
| 109 | return Float32(bitcast[DType.float16](h)) |
| 110 | else: |
| 111 | # Software decode: sign(1) | exponent(5) | mantissa(10) |
| 112 | var s = (UInt32(h) >> 15) & 1 |
| 113 | var e = (UInt32(h) >> 10) & 0x1f |
| 114 | var m = UInt32(h) & 0x3ff |
| 115 | if e == 0: |
| 116 | if m == 0: |
| 117 | # Zero (preserving sign) |
| 118 | return Float32(bitcast[DType.float32](s << 31)) |
| 119 | # Subnormal: renormalize |
| 120 | var mm = m |
| 121 | var c: UInt32 = 0 |
| 122 | while mm > 0: |
| 123 | mm >>= 1 |
| 124 | c += 1 |
| 125 | var sh = 24 - c |
| 126 | var vm = m << (sh + 13) |
| 127 | return Float32(bitcast[DType.float32]((s << 31) | ((UInt32(113 - sh)) << 23) | (vm & 0x7fffff))) |
| 128 | if e == 31: |
| 129 | # Inf or NaN |
| 130 | return Float32(bitcast[DType.float32]((s << 31) | 0x7f800000 | (m << 13))) |
| 131 | # Normalized |
| 132 | return Float32(bitcast[DType.float32]((s << 31) | ((e + 112) << 23) | (m << 13))) |
| 133 | |
| 134 | @always_inline |
| 135 | fn f16x8_to_f32x8(h0: UInt16, h1: UInt16, h2: UInt16, h3: UInt16, |
| 136 | h4: UInt16, h5: UInt16, h6: UInt16, h7: UInt16) -> F32xW: |
| 137 | """Convert 8 half-precision values to a SIMD vector. |
| 138 | On F16C targets, this uses VCVTPH2PS for 8-wide conversion. |
| 139 | """ |
| 140 | # For AVX-512 we only use the lower 8 lanes; upper 8 are zero |
| 141 | var result = F32xW(0.0) |
| 142 | result[0] = f16_to_f32(h0) |
| 143 | result[1] = f16_to_f32(h1) |
| 144 | result[2] = f16_to_f32(h2) |
| 145 | result[3] = f16_to_f32(h3) |
| 146 | result[4] = f16_to_f32(h4) |
| 147 | result[5] = f16_to_f32(h5) |
| 148 | result[6] = f16_to_f32(h6) |
| 149 | if SIMD_WIDTH > 7: |
| 150 | result[7] = f16_to_f32(h7) |
| 151 | if SIMD_WIDTH == 16: |
| 152 | # Upper 8 lanes are zero (caller should handle) |
| 153 | pass |
| 154 | return result |
| 155 | |
| 156 | @always_inline |
| 157 | fn load_f16_scale(ptr: UnsafePointer[UInt8, MutAnyOrigin], offset: Int) -> Float32: |
| 158 | """Load a Q4_0/Q8_0 style f16 scale from packed byte pair. |
| 159 | GGUF stores 16-bit quantization scales in little-endian format: |
| 160 | lo = ptr[offset], hi = ptr[offset+1] |
| 161 | """ |
| 162 | var lo = UInt32(ptr.load(offset)) |
| 163 | var hi = UInt32(ptr.load(offset + 1)) |
| 164 | return f16_to_f32(UInt16((hi << 8) | lo)) |
| 165 | |
| 166 | # ─── Utility: Saturating Cast ────────────────────────────────────────── |
| 167 | |
| 168 | @always_inline |
| 169 | fn saturating_cast_i8(x: Float32) -> Int8: |
| 170 | """Clamp float to [-128, 127] range and cast to Int8 for Q8_0 quantization.""" |
| 171 | if x > 127.0: |
| 172 | return Int8(127) |
| 173 | elif x < -128.0: |
| 174 | return Int8(-128) |
| 175 | else: |
| 176 | return Int8(x) |
| 177 | |
| 178 | @always_inline |
| 179 | fn saturating_cast_u8(x: Float32) -> UInt8: |
| 180 | """Clamp float to [0, 255] range and cast to UInt8.""" |
| 181 | if x > 255.0: |
| 182 | return UInt8(255) |
| 183 | elif x < 0.0: |
| 184 | return UInt8(0) |
| 185 | else: |
| 186 | return UInt8(x) |