cortiq-cli 0.8.21

Command-line tools for the CMF model format: inspect, convert, run and serve .cmf models.
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! `cortiq embed` — embeddings from an EmbeddingGemma 2 `.cmf`: texts,
//! images, videos, audio, and interleaved text + media, all in one 768-d
//! space.
//!
//! Every input is one embedding: unit length, 768-d, or a Matryoshka prefix
//! (`--dim 512|256|128`, re-normalized). `--prompt-name` applies one of the
//! model's task prompts (`SearchQuery`, `Document`, `QuestionAnswering`, …;
//! `--list-prompts` prints them) to the text inputs; `--title` fills the
//! Document prompt. `--jsonl` gives each line its own prompt/title/media.
//!
//! Media: `--image PATH|URL`, `--video PATH` and `--audio PATH`
//! (repeatable) are inputs of their own. A video is an mp4/webm/mov/…
//! (decoded with the `ffmpeg` executable), a `.y4m`, or a directory of
//! frames (`--video-fps` gives their rate; without it they are taken as
//! already sampled at 1 fps). Audio is WAV (decoded natively) or anything
//! `ffmpeg` reads; any rate or channel count is mixed down to mono and
//! resampled to 16 kHz; clips are cut at 30 s (25 tokens a second).
//! `--interleave` makes ONE input of the text and the media instead: the
//! text's `<|image|>` / `<|video|>` / `<|audio|>` placeholders take them in
//! order (or, with no placeholders, the media come first). `--image-tokens`
//! / `--video-tokens` set the soft-token budget (70 | 140 | 280 | 560 |
//! 1120; defaults 280 per image, 140 per frame).

use anyhow::{Context, Result, anyhow};
use cortiq_core::CmfModel;
use cortiq_engine::egemma2::{cosine, matryoshka};
use cortiq_engine::egemma2_audio as ea;
use cortiq_engine::egemma2_mm::{Media, MediaEncoder, MixedInput};
use cortiq_engine::egemma2_vision::decode_video;
use std::io::Write;
use std::sync::Arc;

pub struct EmbedArgs {
    pub model: String,
    pub texts: Vec<String>,
    pub file: Option<String>,
    pub jsonl: Option<String>,
    pub prompt_name: Option<String>,
    pub title: Option<String>,
    pub prompt: Option<String>,
    pub dim: usize,
    pub json: bool,
    pub npy: Option<String>,
    pub show_tokens: bool,
    pub list_prompts: bool,
    /// run the forward this many times and report each (in-process timing)
    pub repeat: usize,
    pub images: Vec<String>,
    pub videos: Vec<String>,
    /// audio files (WAV, or anything ffmpeg reads)
    pub audio: Vec<String>,
    pub video_fps: Option<f64>,
    pub image_tokens: Option<usize>,
    pub video_tokens: Option<usize>,
    pub interleave: bool,
}

/// One input as given, before its media are decoded.
#[derive(Clone, Debug, Default, PartialEq)]
struct Spec {
    text: String,
    images: Vec<String>,
    videos: Vec<String>,
    audio: Vec<String>,
    prompt_name: Option<String>,
    title: Option<String>,
    prompt: Option<String>,
    image_tokens: Option<usize>,
    video_tokens: Option<usize>,
}

fn str_or_list(v: Option<&serde_json::Value>) -> Result<Vec<String>> {
    match v {
        None | Some(serde_json::Value::Null) => Ok(Vec::new()),
        Some(serde_json::Value::String(s)) => Ok(vec![s.clone()]),
        Some(serde_json::Value::Array(a)) => a
            .iter()
            .map(|x| {
                x.as_str()
                    .map(str::to_string)
                    .ok_or_else(|| anyhow!("media lists hold strings (paths or URLs)"))
            })
            .collect(),
        Some(_) => Err(anyhow!("media must be a string or a list of strings")),
    }
}

/// One JSON Lines input: a bare string, or an object
/// `{"text", "image", "video", "audio", "prompt_name"|"task", "title",
/// "prompt", "image_tokens", "video_tokens"}`.
fn parse_jsonl_line(line: &str, defaults: &Spec) -> Result<Spec> {
    let v: serde_json::Value = serde_json::from_str(line)?;
    if let Some(s) = v.as_str() {
        return Ok(Spec {
            text: s.to_string(),
            ..defaults.clone()
        });
    }
    let images = str_or_list(v.get("image").or_else(|| v.get("images")))?;
    let videos = str_or_list(v.get("video").or_else(|| v.get("videos")))?;
    let audio = str_or_list(v.get("audio"))?;
    let text = v
        .get("text")
        .or_else(|| v.get("input"))
        .and_then(|t| t.as_str());
    if text.is_none() && images.is_empty() && videos.is_empty() && audio.is_empty() {
        return Err(anyhow!(
            "expected a string or an object with \"text\", \"image\", \"video\" or \"audio\""
        ));
    }
    let s = |k: &str| v.get(k).and_then(|x| x.as_str()).map(str::to_string);
    let u = |k: &str| v.get(k).and_then(|x| x.as_u64()).map(|x| x as usize);
    let has = |k: &str| v.get(k).is_some();
    // sentence-transformers prompts text-only inputs: a line with media
    // takes a prompt only from itself
    let media = !images.is_empty() || !videos.is_empty() || !audio.is_empty();
    let inherit = |own: Option<String>, k: bool, d: &Option<String>| {
        if k {
            own
        } else if media {
            None
        } else {
            d.clone()
        }
    };
    Ok(Spec {
        text: text.unwrap_or("").to_string(),
        prompt_name: inherit(
            s("prompt_name").or_else(|| s("task")),
            has("prompt_name") || has("task"),
            &defaults.prompt_name,
        ),
        title: inherit(s("title"), has("title"), &defaults.title),
        prompt: inherit(s("prompt"), has("prompt"), &defaults.prompt),
        images,
        videos,
        audio,
        image_tokens: u("image_tokens").or(defaults.image_tokens),
        video_tokens: u("video_tokens").or(defaults.video_tokens),
    })
}

fn write_npy(path: &str, rows: &[Vec<f32>]) -> Result<()> {
    let n = rows.len();
    let d = rows.first().map(|r| r.len()).unwrap_or(0);
    let mut header = format!("{{'descr': '<f4', 'fortran_order': False, 'shape': ({n}, {d}), }}");
    // magic(6) + version(2) + len(2) + header + '\n' aligned to 64
    let total = 10 + header.len() + 1;
    header.push_str(&" ".repeat((64 - total % 64) % 64));
    header.push('\n');
    let mut out = Vec::with_capacity(10 + header.len() + n * d * 4);
    out.extend_from_slice(b"\x93NUMPY\x01\x00");
    out.extend_from_slice(&(header.len() as u16).to_le_bytes());
    out.extend_from_slice(header.as_bytes());
    for r in rows {
        for v in r {
            out.extend_from_slice(&v.to_le_bytes());
        }
    }
    std::fs::write(path, out).with_context(|| format!("write {path}"))
}

/// Read an image from a path, a `file://`, `http(s)://` or `data:` URL.
fn load_image(src: &str) -> Result<cortiq_engine::media::RgbFrame> {
    let bytes = cortiq_engine::media::load_image_bytes(&serde_json::json!({ "url": src }))
        .map_err(|e| anyhow!("{src}: {e}"))?;
    cortiq_engine::media::decode_rgb(&bytes).map_err(|e| anyhow!("{src}: {e}"))
}

/// Decode a spec's media and build the input.
fn build(enc: &MediaEncoder, s: &Spec, fps: Option<f64>) -> Result<(MixedInput, String)> {
    let mut media = Vec::with_capacity(s.images.len() + s.videos.len() + s.audio.len());
    let mut notes = Vec::new();
    // each kind fills its own placeholders in order
    for p in &s.images {
        let img = load_image(p)?;
        let m = enc
            .prepare_image(&img, s.image_tokens)
            .map_err(|e| anyhow!("{p}: {e}"))?;
        if let Media::Image(v) = &m {
            notes.push(format!(
                "image {}x{} → {}x{} px, {} tokens",
                img.width,
                img.height,
                v.pw * enc.proc.patch,
                v.ph * enc.proc.patch,
                v.n_soft(enc.proc.pool_k)
            ));
        }
        media.push(m);
    }
    for p in &s.videos {
        let dv = decode_video(std::path::Path::new(p), fps, &enc.proc)
            .map_err(|e| anyhow!("{p}: {e}"))?;
        let m = enc
            .prepare_frames(&dv.frames, s.video_tokens)
            .map_err(|e| anyhow!("{p}: {e}"))?;
        if let Media::Video(f) = &m {
            notes.push(format!(
                "video {} frames{} via {} → {} sampled {:?}, {} tokens each",
                dv.total_frames,
                dv.src_fps
                    .map(|f| format!(" @ {f:.3} fps"))
                    .unwrap_or_default(),
                dv.decoder,
                f.len(),
                dv.indices,
                f.first().map(|x| x.n_soft(enc.proc.pool_k)).unwrap_or(0)
            ));
        }
        media.push(m);
    }
    for p in &s.audio {
        let clip = ea::read_audio(std::path::Path::new(p)).map_err(anyhow::Error::msg)?;
        notes.push(format!(
            "audio {:.2} s → {} tokens{}",
            clip.len() as f64 / ea::SAMPLE_RATE as f64,
            ea::num_tokens(clip.len()),
            if clip.len() > ea::MAX_SAMPLES {
                " (cut at 30 s)"
            } else {
                ""
            }
        ));
        media.push(Media::Audio(clip));
    }
    Ok((
        MixedInput {
            text: s.text.clone(),
            media,
            prompt_name: s.prompt_name.clone(),
            title: s.title.clone(),
            prompt: s.prompt.clone(),
        },
        notes.join("; "),
    ))
}

pub fn run(args: EmbedArgs) -> Result<()> {
    let t_load = std::time::Instant::now();
    let model = Arc::new(CmfModel::open(&args.model).with_context(|| args.model.clone())?);
    let pool = cortiq_engine::pool::Pool::from_env();
    let enc = MediaEncoder::load(&model, pool).map_err(anyhow::Error::msg)?;
    let load_s = t_load.elapsed().as_secs_f64();
    if args.list_prompts {
        for name in enc.text.prompts().names() {
            println!("{name}\t{:?}", enc.text.prompts().get(name).unwrap_or(""));
        }
        return Ok(());
    }
    if !cortiq_engine::egemma2::MATRYOSHKA_DIMS.contains(&args.dim) {
        return Err(anyhow!("--dim {}: use 768, 512, 256 or 128", args.dim));
    }
    let defaults = Spec {
        prompt_name: args.prompt_name.clone(),
        title: args.title.clone(),
        prompt: args.prompt.clone(),
        image_tokens: args.image_tokens,
        video_tokens: args.video_tokens,
        ..Default::default()
    };
    let mut specs: Vec<Spec> = Vec::new();
    let text_spec = |t: &str| Spec {
        text: t.to_string(),
        ..defaults.clone()
    };
    if args.interleave {
        if args.texts.len() > 1 || args.file.is_some() || args.jsonl.is_some() {
            return Err(anyhow!(
                "--interleave makes one input of ONE text and the --image/--video/--audio media \
                 (use --jsonl for several interleaved inputs)"
            ));
        }
        specs.push(Spec {
            text: args.texts.first().cloned().unwrap_or_default(),
            images: args.images.clone(),
            videos: args.videos.clone(),
            audio: args.audio.clone(),
            ..defaults.clone()
        });
    } else {
        specs.extend(args.texts.iter().map(|t| text_spec(t)));
        if let Some(f) = &args.file {
            let body = std::fs::read_to_string(f).with_context(|| f.clone())?;
            for line in body.lines().filter(|l| !l.trim().is_empty()) {
                specs.push(text_spec(line));
            }
        }
        if let Some(f) = &args.jsonl {
            let body = std::fs::read_to_string(f).with_context(|| f.clone())?;
            for (i, line) in body.lines().enumerate() {
                if line.trim().is_empty() {
                    continue;
                }
                specs.push(
                    parse_jsonl_line(line, &defaults).with_context(|| format!("{f}:{}", i + 1))?,
                );
            }
        }
        // media alone take no prompt (sentence-transformers)
        let bare = Spec {
            image_tokens: args.image_tokens,
            video_tokens: args.video_tokens,
            ..Default::default()
        };
        for p in &args.images {
            specs.push(Spec {
                images: vec![p.clone()],
                ..bare.clone()
            });
        }
        for p in &args.videos {
            specs.push(Spec {
                videos: vec![p.clone()],
                ..bare.clone()
            });
        }
        for p in &args.audio {
            specs.push(Spec {
                audio: vec![p.clone()],
                ..bare.clone()
            });
        }
    }
    if specs.is_empty() {
        return Err(anyhow!(
            "nothing to embed: give texts (positional or --text), --file, --jsonl, --image, --video or --audio"
        ));
    }
    let t_prep = std::time::Instant::now();
    let mut inputs = Vec::with_capacity(specs.len());
    let mut notes = Vec::with_capacity(specs.len());
    for (i, s) in specs.iter().enumerate() {
        let (x, note) = build(&enc, s, args.video_fps).with_context(|| format!("input {i}"))?;
        inputs.push(x);
        notes.push(note);
    }
    let prep_s = t_prep.elapsed().as_secs_f64();
    let ids: Vec<Vec<u32>> = inputs
        .iter()
        .enumerate()
        .map(|(i, x)| enc.input_ids(x).map_err(|e| anyhow!("input {i}: {e}")))
        .collect::<Result<_>>()?;
    let tokens: usize = ids.iter().map(|s| s.len()).sum();
    if args.show_tokens {
        for (i, s) in ids.iter().enumerate() {
            eprintln!("[{i}] {} tokens: {s:?}", s.len());
        }
    }
    let has_media = inputs.iter().any(|x| !x.media.is_empty());
    if has_media {
        // the towers' one-time load is not part of the timed forward
        let t = std::time::Instant::now();
        enc.warm_for(&inputs).map_err(anyhow::Error::msg)?;
        eprintln!("media tower(s) loaded in {:.2}s", t.elapsed().as_secs_f64());
    }
    let mut times = Vec::new();
    let mut full = Vec::new();
    for _ in 0..args.repeat.max(1) {
        let t0 = std::time::Instant::now();
        full = enc.embed(&inputs).map_err(anyhow::Error::msg)?;
        times.push(t0.elapsed().as_secs_f64());
    }
    if times.len() > 1 {
        let ms: Vec<String> = times.iter().map(|t| format!("{:.1}", t * 1e3)).collect();
        let mut sorted = times.clone();
        sorted.sort_by(|a, b| a.total_cmp(b));
        eprintln!(
            "repeat {}: [{}] ms; median {:.1} ms = {:.0} tok/s",
            times.len(),
            ms.join(", "),
            sorted[sorted.len() / 2] * 1e3,
            tokens as f64 / sorted[sorted.len() / 2]
        );
    }
    let secs = times[0];
    let vecs: Vec<Vec<f32>> = full
        .iter()
        .map(|v| matryoshka(v, args.dim))
        .collect::<std::result::Result<_, _>>()
        .map_err(anyhow::Error::msg)?;
    eprintln!(
        "embedded {} input(s), {tokens} tokens in {:.3}s ({:.0} tok/s; load {:.2}s{}, {})",
        vecs.len(),
        secs,
        tokens as f64 / secs.max(1e-9),
        load_s,
        if has_media {
            format!(", media decode+resize {prep_s:.2}s")
        } else {
            String::new()
        },
        if enc.text.quant.is_empty() {
            "?"
        } else {
            &enc.text.quant
        }
    );
    if let Some(p) = &args.npy {
        write_npy(p, &vecs)?;
        eprintln!("wrote {p} ({} x {})", vecs.len(), args.dim);
    }
    let stdout = std::io::stdout();
    let mut out = stdout.lock();
    if args.json {
        let data: Vec<serde_json::Value> = vecs
            .iter()
            .enumerate()
            .map(|(i, v)| serde_json::json!({"object": "embedding", "index": i, "embedding": v}))
            .collect();
        let body = serde_json::json!({
            "object": "list",
            "model": args.model,
            "data": data,
            "usage": {"prompt_tokens": tokens, "total_tokens": tokens},
        });
        writeln!(out, "{body}")?;
    } else if args.npy.is_none() {
        for (i, v) in vecs.iter().enumerate() {
            let head: Vec<String> = v.iter().take(6).map(|x| format!("{x:+.5}")).collect();
            let what = if notes[i].is_empty() {
                String::new()
            } else {
                format!(" ({})", notes[i])
            };
            writeln!(
                out,
                "[{i}] {} tokens, dim {}{what}: {} …",
                ids[i].len(),
                v.len(),
                head.join(" ")
            )?;
        }
        if (2..=8).contains(&vecs.len()) {
            writeln!(out, "cosine similarity:")?;
            for a in &vecs {
                let row: Vec<String> = vecs
                    .iter()
                    .map(|b| format!("{:.4}", cosine(a, b)))
                    .collect();
                writeln!(out, "  {}", row.join("  "))?;
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn jsonl_lines_override_defaults() {
        let d = Spec {
            prompt_name: Some("SearchQuery".into()),
            ..Default::default()
        };
        let a = parse_jsonl_line("\"hello\"", &d).unwrap();
        assert_eq!(a.text, "hello");
        assert_eq!(a.prompt_name.as_deref(), Some("SearchQuery"));
        let b =
            parse_jsonl_line(r#"{"text":"doc","prompt_name":"Document","title":"T"}"#, &d).unwrap();
        assert_eq!(b.prompt_name.as_deref(), Some("Document"));
        assert_eq!(b.title.as_deref(), Some("T"));
        let c = parse_jsonl_line(r#"{"text":"raw","prompt_name":null}"#, &d).unwrap();
        assert_eq!(c.prompt_name, None);
        let t = parse_jsonl_line(r#"{"input":"x","task":"Clustering"}"#, &d).unwrap();
        assert_eq!(t.prompt_name.as_deref(), Some("Clustering"));
        assert!(parse_jsonl_line("{\"nope\":1}", &d).is_err());
    }

    #[test]
    fn jsonl_media_lines() {
        let d = Spec {
            prompt_name: Some("SearchQuery".into()),
            image_tokens: Some(560),
            ..Default::default()
        };
        let a = parse_jsonl_line(r#"{"image":"a.png"}"#, &d).unwrap();
        assert_eq!(a.images, vec!["a.png".to_string()]);
        // media inputs do not inherit the default prompt
        assert_eq!(a.prompt_name, None);
        assert_eq!(a.image_tokens, Some(560));
        let b = parse_jsonl_line(
            r#"{"text":"A fox: <|image|> and <|video|>","image":["f.png"],"video":"v.mp4","video_tokens":70}"#,
            &d,
        )
        .unwrap();
        assert_eq!(b.videos, vec!["v.mp4".to_string()]);
        assert_eq!(b.video_tokens, Some(70));
        let c = parse_jsonl_line(r#"{"image":"a.png","task":"Document"}"#, &d).unwrap();
        assert_eq!(c.prompt_name.as_deref(), Some("Document"));
        assert!(parse_jsonl_line(r#"{"image":5}"#, &d).is_err());
    }

    #[test]
    fn jsonl_audio_lines() {
        let d = Spec {
            prompt_name: Some("SearchQuery".into()),
            ..Default::default()
        };
        let a = parse_jsonl_line(r#"{"audio":"a.wav"}"#, &d).unwrap();
        assert_eq!(a.audio, vec!["a.wav".to_string()]);
        // audio alone takes no default prompt
        assert_eq!(a.prompt_name, None);
        let b = parse_jsonl_line(
            r#"{"text":"Said: <|audio|> then <|image|>","audio":["a.wav"],"image":"f.png","task":"Clustering"}"#,
            &d,
        )
        .unwrap();
        assert_eq!(b.audio.len(), 1);
        assert_eq!(b.images.len(), 1);
        assert_eq!(b.prompt_name.as_deref(), Some("Clustering"));
        assert!(parse_jsonl_line(r#"{"audio":[1]}"#, &d).is_err());
        assert!(parse_jsonl_line(r#"{"audio":3}"#, &d).is_err());
    }

    #[test]
    fn npy_header_is_aligned() {
        let dir = std::env::temp_dir().join(format!("egemma2-npy-{}", std::process::id()));
        let p = dir.with_extension("npy");
        write_npy(p.to_str().unwrap(), &[vec![1.0, 2.0], vec![3.0, 4.0]]).unwrap();
        let b = std::fs::read(&p).unwrap();
        let hl = u16::from_le_bytes([b[8], b[9]]) as usize;
        assert_eq!((10 + hl) % 64, 0);
        assert_eq!(b.len(), 10 + hl + 16);
        let _ = std::fs::remove_file(&p);
    }
}