modelc 0.1.8

Rust CLI that compiles LLM weights (GGUF, Safetensors, ONNX, PyTorch) into a single .modelc artifact and serves a local OpenAI-compatible inference API with Metal GPU and CPU SIMD acceleration.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Shared fixtures for integration tests; not every helper is referenced by each test binary.

#![allow(dead_code)]

pub mod http;

use std::collections::HashMap;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

use modelc::model::{DataType, Model, TensorData};

/// Minimal `tempfile`-like helpers — the `tempfile` crate is no longer a dependency.
/// We avoid pulling in a real `tempfile` crate by using `std::env::temp_dir()` plus
/// a process-unique counter. RAII cleanup removes the directory on drop.
pub struct TempDir {
    path: PathBuf,
}

impl TempDir {
    pub fn path(&self) -> &Path {
        &self.path
    }
}

impl Drop for TempDir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.path);
    }
}

pub struct TempFile {
    path: PathBuf,
}

impl TempFile {
    pub fn path(&self) -> &Path {
        &self.path
    }
}

impl Drop for TempFile {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.path);
    }
}

static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);

fn unique_suffix(label: &str) -> String {
    let pid = std::process::id();
    let seq = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    format!("modelc-{}-{}-{}-{}", label, pid, nanos, seq)
}

pub fn tempdir() -> std::io::Result<TempDir> {
    let path = std::env::temp_dir().join(unique_suffix("dir"));
    std::fs::create_dir_all(&path)?;
    Ok(TempDir { path })
}

pub fn tempfile() -> std::io::Result<TempFile> {
    let path = std::env::temp_dir().join(unique_suffix("file"));
    Ok(TempFile { path })
}

pub fn create_test_model() -> Model {
    let mut tensors = HashMap::new();

    let weight_data: Vec<u8> = [1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0]
        .iter()
        .flat_map(|f| f.to_le_bytes())
        .collect();
    tensors.insert(
        "weight".to_string(),
        TensorData {
            shape: vec![2, 3],
            dtype: DataType::F32,
            data: weight_data,
        },
    );

    let bias_data: Vec<u8> = [0.1f32, 0.2].iter().flat_map(|f| f.to_le_bytes()).collect();
    tensors.insert(
        "bias".to_string(),
        TensorData {
            shape: vec![2],
            dtype: DataType::F32,
            data: bias_data,
        },
    );

    let mut metadata = HashMap::new();
    metadata.insert("source".to_string(), "test".to_string());

    Model {
        name: "test_model".to_string(),
        architecture: "mlp".to_string(),
        tensors,
        metadata,
    }
}

pub fn create_large_test_model() -> Model {
    let mut tensors = HashMap::new();

    let hidden_dim = 64usize;
    let weight_data: Vec<u8> = (0..hidden_dim * hidden_dim)
        .flat_map(|i| (i as f32 / 100.0).to_le_bytes())
        .collect();
    tensors.insert(
        "layer0.weight".to_string(),
        TensorData {
            shape: vec![hidden_dim, hidden_dim],
            dtype: DataType::F32,
            data: weight_data,
        },
    );

    let bias_data: Vec<u8> = (0..hidden_dim)
        .flat_map(|i| (i as f32 / 100.0).to_le_bytes())
        .collect();
    tensors.insert(
        "layer0.bias".to_string(),
        TensorData {
            shape: vec![hidden_dim],
            dtype: DataType::F32,
            data: bias_data,
        },
    );

    let ln_weight: Vec<u8> = (0..hidden_dim).flat_map(|_| 1.0f32.to_le_bytes()).collect();
    tensors.insert(
        "layer0.ln_weight".to_string(),
        TensorData {
            shape: vec![hidden_dim],
            dtype: DataType::F32,
            data: ln_weight,
        },
    );

    let ln_bias: Vec<u8> = (0..hidden_dim).flat_map(|_| 0.0f32.to_le_bytes()).collect();
    tensors.insert(
        "layer0.ln_bias".to_string(),
        TensorData {
            shape: vec![hidden_dim],
            dtype: DataType::F32,
            data: ln_bias,
        },
    );

    tensors.insert(
        "layer1.weight".to_string(),
        TensorData {
            shape: vec![hidden_dim, hidden_dim],
            dtype: DataType::F32,
            data: vec![0u8; hidden_dim * hidden_dim * 4],
        },
    );

    tensors.insert(
        "layer1.bias".to_string(),
        TensorData {
            shape: vec![hidden_dim],
            dtype: DataType::F32,
            data: vec![0u8; hidden_dim * 4],
        },
    );

    Model {
        name: "large_test_model".to_string(),
        architecture: "mlp".to_string(),
        tensors,
        metadata: HashMap::new(),
    }
}

pub fn create_safetensors_file(path: &Path, tensors: Vec<(&str, &str, Vec<usize>, Vec<u8>)>) {
    let mut sorted: Vec<_> = tensors.into_iter().collect();
    sorted.sort_by(|a, b| a.0.cmp(b.0));

    let mut header = serde_json::Map::new();
    header.insert(
        "__metadata__".to_string(),
        serde_json::Value::Object(serde_json::Map::new()),
    );

    let mut offset = 0usize;
    let mut entries = Vec::new();
    for (name, dtype, shape, data) in sorted {
        let end = offset + data.len();
        entries.push((
            name.to_string(),
            dtype.to_string(),
            shape,
            data,
            offset,
            end,
        ));
        offset = end;
    }

    for (name, dtype, shape, _, start, end) in &entries {
        let mut obj = serde_json::Map::new();
        obj.insert(
            "dtype".to_string(),
            serde_json::Value::String(dtype.clone()),
        );
        obj.insert("shape".to_string(), serde_json::json!(shape));
        obj.insert(
            "data_offsets".to_string(),
            serde_json::json!([*start, *end]),
        );
        header.insert(name.clone(), serde_json::Value::Object(obj));
    }

    let header_json = serde_json::to_string(&header).unwrap();
    let header_bytes = header_json.as_bytes();

    let mut file = std::fs::File::create(path).unwrap();
    file.write_all(&(header_bytes.len() as u64).to_le_bytes())
        .unwrap();
    file.write_all(header_bytes).unwrap();
    for (_, _, _, data, _, _) in &entries {
        file.write_all(data).unwrap();
    }
}

pub fn f32_to_bytes(values: &[f32]) -> Vec<u8> {
    values.iter().flat_map(|f| f.to_le_bytes()).collect()
}

pub fn bytes_to_f32(bytes: &[u8]) -> Vec<f32> {
    bytes
        .chunks_exact(4)
        .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
        .collect()
}

const GPT2_HIDDEN: usize = 12;
const GPT2_FFN: usize = 48;
const GPT2_VOCAB: usize = 10;

fn fp32_matrix(rows: usize, cols: usize) -> Vec<u8> {
    (0..(rows * cols))
        .flat_map(|i| ((i as f32 % 7.0) - 3.0).to_le_bytes())
        .collect()
}

fn fp32_vector(len: usize, fill: f32) -> Vec<u8> {
    (0..len).flat_map(|_| fill.to_le_bytes()).collect()
}

/// Minimal but complete single-layer GPT-2 fixture with tied `wte`/`lm_head`.
pub fn create_gpt2_test_model() -> Model {
    let h = GPT2_HIDDEN;
    let mut tensors = HashMap::new();
    let mut mk = |name: &str, shape: Vec<usize>, data: Vec<u8>| {
        tensors.insert(
            name.to_string(),
            TensorData {
                shape,
                dtype: DataType::F32,
                data,
            },
        );
    };

    mk("transformer.h.0.ln_1.weight", vec![h], fp32_vector(h, 1.0));
    mk("transformer.h.0.ln_1.bias", vec![h], fp32_vector(h, 0.0));
    mk(
        "transformer.h.0.attn.c_attn.weight",
        vec![3 * h, h],
        fp32_matrix(3 * h, h),
    );
    mk(
        "transformer.h.0.attn.c_attn.bias",
        vec![3 * h],
        fp32_vector(3 * h, 0.0),
    );
    mk(
        "transformer.h.0.attn.c_proj.weight",
        vec![h, h],
        fp32_matrix(h, h),
    );
    mk(
        "transformer.h.0.attn.c_proj.bias",
        vec![h],
        fp32_vector(h, 0.0),
    );
    mk("transformer.h.0.ln_2.weight", vec![h], fp32_vector(h, 1.0));
    mk("transformer.h.0.ln_2.bias", vec![h], fp32_vector(h, 0.0));
    mk(
        "transformer.h.0.mlp.c_fc.weight",
        vec![GPT2_FFN, h],
        fp32_matrix(GPT2_FFN, h),
    );
    mk(
        "transformer.h.0.mlp.c_fc.bias",
        vec![GPT2_FFN],
        fp32_vector(GPT2_FFN, 0.0),
    );
    mk(
        "transformer.h.0.mlp.c_proj.weight",
        vec![h, GPT2_FFN],
        fp32_matrix(h, GPT2_FFN),
    );
    mk(
        "transformer.h.0.mlp.c_proj.bias",
        vec![h],
        fp32_vector(h, 0.0),
    );
    mk("transformer.ln_f.weight", vec![h], fp32_vector(h, 1.0));
    mk("transformer.ln_f.bias", vec![h], fp32_vector(h, 0.0));
    mk(
        "transformer.wte.weight",
        vec![GPT2_VOCAB, h],
        fp32_matrix(GPT2_VOCAB, h),
    );

    Model {
        name: "mini_gpt2".to_string(),
        architecture: "gpt2".to_string(),
        tensors,
        metadata: HashMap::new(),
    }
}

const LLAMA_HIDDEN: usize = 12;
const LLAMA_INTER: usize = 48;
const LLAMA_VOCAB: usize = 10;

/// Minimal but complete single-layer LLaMA fixture with explicit `lm_head`.
pub fn create_llama_test_model() -> Model {
    let h = LLAMA_HIDDEN;
    let mut tensors = HashMap::new();
    let mut mk = |name: &str, shape: Vec<usize>, data: Vec<u8>| {
        tensors.insert(
            name.to_string(),
            TensorData {
                shape,
                dtype: DataType::F32,
                data,
            },
        );
    };

    mk(
        "model.layers.0.input_layernorm.weight",
        vec![h],
        fp32_vector(h, 1.0),
    );
    mk(
        "model.layers.0.self_attn.q_proj.weight",
        vec![h, h],
        fp32_matrix(h, h),
    );
    mk(
        "model.layers.0.self_attn.k_proj.weight",
        vec![h, h],
        fp32_matrix(h, h),
    );
    mk(
        "model.layers.0.self_attn.v_proj.weight",
        vec![h, h],
        fp32_matrix(h, h),
    );
    mk(
        "model.layers.0.self_attn.o_proj.weight",
        vec![h, h],
        fp32_matrix(h, h),
    );
    mk(
        "model.layers.0.post_attention_layernorm.weight",
        vec![h],
        fp32_vector(h, 1.0),
    );
    mk(
        "model.layers.0.mlp.gate_proj.weight",
        vec![LLAMA_INTER, h],
        fp32_matrix(LLAMA_INTER, h),
    );
    mk(
        "model.layers.0.mlp.up_proj.weight",
        vec![LLAMA_INTER, h],
        fp32_matrix(LLAMA_INTER, h),
    );
    mk(
        "model.layers.0.mlp.down_proj.weight",
        vec![h, LLAMA_INTER],
        fp32_matrix(h, LLAMA_INTER),
    );
    mk(
        "model.embed_tokens.weight",
        vec![LLAMA_VOCAB, h],
        fp32_matrix(LLAMA_VOCAB, h),
    );
    mk("model.norm.weight", vec![h], fp32_vector(h, 1.0));
    mk(
        "lm_head.weight",
        vec![LLAMA_VOCAB, h],
        fp32_matrix(LLAMA_VOCAB, h),
    );

    let mut metadata = HashMap::new();
    metadata.insert("attention.head_count".to_string(), "2".to_string());

    Model {
        name: "mini_llama".to_string(),
        architecture: "llama".to_string(),
        tensors,
        metadata,
    }
}