interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / src / mojollama / kernels / mojo_file_test.mojo

54 lines · 1313 bytes Raw Blame History
1from std.prelude import *
2from std import time
3
4@extern("fopen")
5fn c_fopen(path: String, mode: String) -> Int64: ...
6
7@extern("fread")
8fn c_fread(buf: Int64, size: Int64, count: Int64, fp: Int64) -> Int64: ...
9
10@extern("fclose")
11fn c_fclose(fp: Int64) -> Int32: ...
12
13@extern("fseek")
14fn c_fseek(fp: Int64, offset: Int64, whence: Int32) -> Int32: ...
15
16@extern("ftell")
17fn c_ftell(fp: Int64) -> Int64: ...
18
19@extern("malloc")
20fn c_malloc(size: Int64) -> Int64: ...
21
22@extern("free")
23fn c_free(ptr: Int64): ...
24
25@extern("memcpy")
26fn c_memcpy(dst: Int64, src: Int64, n: Int64): ...
27
28fn 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")