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/parallel_matmul.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 / parallel_matmul.mojo` 180 lines · 6179 bytes Raw Blame History 1 """MojoLlama — parallel Q4_0 matmul via file-based IPC. 2 3 Compile: mojo build parallel_matmul.mojo -o mojollama_q4 4 5 Each invocation reads weights/input from binary files, computes 6 a chunk of rows, and writes results to an output file. Python 7 multiprocessing fans out across CPU cores — one process per chunk. 8 9 File formats (raw binary, no numpy dependency): 10 weights: [uint8] — n_blocks * 18 bytes (Q4_0 blocks) 11 input: [float32] — n_cols values 12 output: [float32] — n_rows values (pre-allocated, each worker writes subset) 13 14 Usage: 15 mojollama_q4 16 """ 17 18 from std.memory.unsafe_pointer import alloc, free, pointer_to_int 19 from python import Python 20 21 alias F32x8 = SIMD[DType.float32, 8] 22 alias U8x16 = SIMD[DType.uint8, 16] 23 24 25 def f16_to_f32(h: UInt16) -> Float32: 26 from std.sys.info import CompilationTarget 27 from std.memory import bitcast 28 comptime if CompilationTarget.has_avx2(): 29 return Float32(bitcast[DType.float16](h)) 30 else: 31 var s = (UInt32(h) >> 15) & 1 32 var e = (UInt32(h) >> 10) & 0x1f 33 var m = UInt32(h) & 0x3ff 34 if e == 0: 35 if m == 0: 36 return Float32(bitcast[DType.float32](s << 31)) 37 var mm = m 38 var c: UInt32 = 0 39 while mm > 0: 40 mm >>= 1 41 c += 1 42 var sh = 24 - c 43 return Float32( 44 bitcast[DType.float32]( 45 (s << 31) | ((UInt32(113 - sh)) << 23) | ((m << (sh + 13)) & 0x7fffff) 46 ) 47 ) 48 if e == 31: 49 return Float32( 50 bitcast[DType.float32]((s << 31) | 0x7f800000 | (m << 13)) 51 ) 52 return Float32( 53 bitcast[DType.float32]((s << 31) | ((e + 112) << 23) | (m << 13)) 54 ) 55 56 57 def q4_block_dot( 58 scale: Float32, nibbles: U8x16, x0: F32x8, x1: F32x8, x2: F32x8, x3: F32x8 59 ) -> Float32: 60 @parameter 61 def dg(s: Int, xv: F32x8) -> Float32: 62 var v = F32x8() 63 for j in range(8): 64 var bi = s + j // 2 65 var b = nibbles[bi] 66 v[j] = ( 67 Float32(Int8(b & 15) - 8) 68 if j % 2 == 0 69 else Float32(Int8((b >> 4) & 15) - 8) 70 ) 71 return (v * scale * xv).reduce_add() 72 return dg(0, x0) + dg(4, x1) + dg(8, x2) + dg(12, x3) 73 74 75 def q4_matmul_file( 76 w_path: String, 77 inp_path: String, 78 out_path: String, 79 n_rows: Int, 80 n_cols: Int, 81 start_row: Int, 82 end_row: Int, 83 ) raises: 84 """Read weights/input from binary files, compute chunk, write results.""" 85 var posix = Python.import_module("os") 86 87 # Calculate sizes 88 var bpr = n_cols // 32 # blocks per row 89 var bsize = 18 # bytes per block (2 scale + 16 nibbles) 90 91 var w_size = n_rows * bpr * bsize 92 var inp_size = n_cols * 4 # float32 93 var out_size = n_rows * 4 94 95 # Open files 96 var w_fd = posix.open(w_path, 0) # O_RDONLY 97 var inp_fd = posix.open(inp_path, 0) 98 var out_fd = posix.open(out_path, 2) # O_RDWR 99 100 # mmap the files (shared memory across processes) 101 var mmap_mod = Python.import_module("mmap") 102 var prot_read = 1 # PROT_READ 103 var prot_write = 2 # PROT_WRITE 104 var mmap_shared = 1 # MAP_SHARED 105 106 var w_mmap = mmap_mod.mmap(w_fd, w_size, prot=prot_read, flags=mmap_shared, offset=0) 107 var inp_mmap = mmap_mod.mmap(inp_fd, inp_size, prot=prot_read, flags=mmap_shared, offset=0) 108 var out_mmap = mmap_mod.mmap(out_fd, out_size, prot=prot_read | prot_write, flags=mmap_shared, offset=0) 109 110 # Get buffer pointers from mmap 111 var buf_mod = Python.import_module("builtins") 112 var w_view = buf_mod.memoryview(w_mmap).cast("B") 113 var inp_view = buf_mod.memoryview(inp_mmap).cast("f") 114 var out_view = buf_mod.memoryview(out_mmap).cast("f") 115 116 # Read data into Mojo-managed buffers 117 var w_buf = alloc[UInt8](w_size) 118 var inp_buf = alloc[Float32](n_cols) 119 var out_buf = alloc[Float32](n_rows) 120 121 # Copy from Python memory views to Mojo buffers 122 for i in range(n_cols): 123 inp_buf.store(i, Float32(inp_view[i])) 124 125 # For weights, copy byte-by-byte via Python 126 for i in range(w_size): 127 w_buf.store(i, UInt8(w_view[i])) 128 129 # Compute 130 for row in range(start_row, end_row): 131 var total: Float32 = 0.0 132 for blk in range(bpr): 133 var off = (row * bpr + blk) * bsize 134 var lo = w_buf.load(off) 135 var hi = w_buf.load(off + 1) 136 var scale = f16_to_f32((UInt16(hi) << 8) | UInt16(lo)) 137 var nibs = w_buf.load[width=16](off + 2) 138 var inp_off = blk * 32 139 var x0 = inp_buf.load[width=8](inp_off) 140 var x1 = inp_buf.load[width=8](inp_off + 8) 141 var x2 = inp_buf.load[width=8](inp_off + 16) 142 var x3 = inp_buf.load[width=8](inp_off + 24) 143 total += q4_block_dot(scale, nibs, x0, x1, x2, x3) 144 out_buf.store(row, total) 145 146 # Write results back to mmap 147 for row in range(start_row, end_row): 148 out_view[row] = out_buf.load(row) 149 150 # Cleanup 151 w_buf.free() 152 inp_buf.free() 153 out_buf.free() 154 w_mmap.close() 155 inp_mmap.close() 156 out_mmap.close() 157 posix.close(w_fd) 158 posix.close(inp_fd) 159 posix.close(out_fd) 160 161 162 def main() raises: 163 var sys = Python.import_module("sys") 164 var bltns = Python.import_module("builtins") 165 var argv = sys.argv 166 var argc = Int(bltns.len(argv)) 167 168 if argc < 8: 169 print("Usage: mojollama_q4 ") 170 sys.exit(1) 171 172 var w_path = String(argv[1]) 173 var inp_path = String(argv[2]) 174 var out_path = String(argv[3]) 175 var n_rows = Int(String(argv[4])) 176 var n_cols = Int(String(argv[5])) 177 var start_row = Int(String(argv[6])) 178 var end_row = Int(String(argv[7])) 179 180 q4_matmul_file(w_path, inp_path, out_path, n_rows, n_cols, start_row, end_row)