Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/MojoLlama/blob/main/src/mojollama/kernels/q4_ffi.mojo # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / src / mojollama / kernels / q4_ffi.mojo` 89 lines · 3689 bytes Raw Blame History 1 """MojoLlama Q4_0 matmul — shared library entry point. 2 3 Compile: mojo build --output-type shared-lib q4_ffi.mojo -o libq4mojo.so 4 5 Called from Python via ctypes: 6 lib = ctypes.CDLL("./libq4mojo.so") 7 lib.q4_matmul(weights_addr, input_addr, output_addr, n_rows, n_cols) 8 """ 9 10 from std.memory.unsafe_pointer import alloc 11 12 # ─── Float16 → Float32 ──────────────────────────────────────────────── 13 14 @always_inline 15 def f16_to_f32(h: UInt16) -> Float32: 16 from std.sys.info import CompilationTarget 17 from std.memory import bitcast 18 comptime if CompilationTarget.has_avx2(): 19 var f16 = bitcast[DType.float16](h) 20 return Float32(f16) 21 else: 22 var sign = (UInt32(h) >> 15) & 1 23 var exp = (UInt32(h) >> 10) & 0x1f 24 var mant = UInt32(h) & 0x3ff 25 if exp == 0: 26 if mant == 0: 27 return Float32(bitcast[DType.float32](sign << 31)) 28 var m = mant; var count: UInt32 = 0 29 while m > 0: m = m >> 1; count += 1 30 var shift = 24 - count 31 return Float32(bitcast[DType.float32]((sign << 31) | ((UInt32(113 - shift)) << 23) | ((mant << (shift + 13)) & 0x7fffff))) 32 elif exp == 31: 33 return Float32(bitcast[DType.float32]((sign << 31) | 0x7f800000 | (mant << 13))) 34 else: 35 return Float32(bitcast[DType.float32]((sign << 31) | ((exp + 112) << 23) | (mant << 13))) 36 37 38 # ─── Q4_0 Block Dot ─────────────────────────────────────────────────── 39 40 @always_inline 41 def q4_block_dot(scale: Float32, nibbles: SIMD[DType.uint8, 16], 42 x0: SIMD[DType.float32, 8], x1: SIMD[DType.float32, 8], 43 x2: SIMD[DType.float32, 8], x3: SIMD[DType.float32, 8]) -> Float32: 44 alias U8x16 = SIMD[DType.uint8, 16] 45 alias F32x8 = SIMD[DType.float32, 8] 46 47 @parameter 48 def dot_group(start_byte: Int, xv: F32x8) -> Float32: 49 var vals = F32x8() 50 for j in range(8): 51 var byte_idx = start_byte + j // 2 52 var b = nibbles[byte_idx] 53 vals[j] = Float32(Int8(b & 15) - 8) if j % 2 == 0 else Float32(Int8((b >> 4) & 15) - 8) 54 return (vals * scale * xv).reduce_add() 55 56 var total: Float32 = 0.0 57 total += dot_group(0, x0) 58 total += dot_group(4, x1) 59 total += dot_group(8, x2) 60 total += dot_group(12, x3) 61 return total 62 63 64 # ─── Exported C-compatible API ──────────────────────────────────────── 65 66 fn q4_matmul_forward( 67 weights_ptr: Int, # pointer to Q4_0 block data 68 input_ptr: Int, # pointer to float32 input vector 69 output_ptr: Int, # pointer to float32 output buffer 70 n_rows: Int, # number of output features 71 n_cols: Int # number of input features 72 ): 73 """Compute y = W @ x for Q4_0 quantized weights. 74 75 All pointers are raw addresses (from numpy.__array_interface__['data'][0]). 76 This is called from Python via ctypes. 77 """ 78 # Note: In current Mojo 1.0.0b1, we can't construct UnsafePointer from 79 # raw Int addresses. This function is the FFI stub — the actual 80 # computation runs inside a Python-script generated Mojo context. 81 # 82 # Workaround: compile this as a standalone executable that reads/writes 83 # binary files, invoked from bridge.py. 84 pass 85 86 87 def main(): 88 print("MojoLlama Q4_0 FFI Library") 89 print("Compile with: mojo build --output-type shared-lib q4_ffi.mojo -o libq4mojo.so")