MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / mojo_file_test.mojo
| 1 | from std.prelude import * |
| 2 | from std import time |
| 3 | |
| 4 | @extern("fopen") |
| 5 | fn c_fopen(path: String, mode: String) -> Int64: ... |
| 6 | |
| 7 | @extern("fread") |
| 8 | fn c_fread(buf: Int64, size: Int64, count: Int64, fp: Int64) -> Int64: ... |
| 9 | |
| 10 | @extern("fclose") |
| 11 | fn c_fclose(fp: Int64) -> Int32: ... |
| 12 | |
| 13 | @extern("fseek") |
| 14 | fn c_fseek(fp: Int64, offset: Int64, whence: Int32) -> Int32: ... |
| 15 | |
| 16 | @extern("ftell") |
| 17 | fn c_ftell(fp: Int64) -> Int64: ... |
| 18 | |
| 19 | @extern("malloc") |
| 20 | fn c_malloc(size: Int64) -> Int64: ... |
| 21 | |
| 22 | @extern("free") |
| 23 | fn c_free(ptr: Int64): ... |
| 24 | |
| 25 | @extern("memcpy") |
| 26 | fn c_memcpy(dst: Int64, src: Int64, n: Int64): ... |
| 27 | |
| 28 | fn main(): |
| 29 | var path = "/tmp/mojo_weights/gpt-oss/meta.bin" |
| 30 | var fp = c_fopen(path, "rb") |
| 31 | if fp == 0: |
| 32 | print("Failed to open: ", path) |
| 33 | return |
| 34 | |
| 35 | c_fseek(fp, 0, 2) |
| 36 | var file_size = c_ftell(fp) |
| 37 | c_fseek(fp, 0, 0) |
| 38 | |
| 39 | var buf = c_malloc(file_size) |
| 40 | var nread = c_fread(buf, 1, file_size, fp) |
| 41 | c_fclose(fp) |
| 42 | |
| 43 | print("Read ", nread, " bytes") |
| 44 | |
| 45 | # Read first 100 bytes and print as string |
| 46 | # Use a local buffer via List |
| 47 | var content = List[UInt8]() |
| 48 | for i in range(Int(min(100, nread))): |
| 49 | content.append(UInt8((buf + i).load[UInt8]())) # Still doesn't work |
| 50 | |
| 51 | print("Content length: ", len(content)) |
| 52 | c_free(buf) |
| 53 | |
| 54 | print("File I/O test complete") |