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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
//! `frink run`'s flags: the clap surface and the value types it parses into.
//!
//! Moved out of `run.rs` verbatim (a pure move) so the flag surface can
//! change without editing a 2500-line file.
use std::fmt;
use std::str::FromStr;
use clap::Args;
use frink_models::{SamplerOrder, SamplingParams};
/// llama.cpp-compatible completion flags.
#[derive(Args, Debug, Clone)]
pub struct InferArgs {
/// Model path (GGUF). Alias of llama.cpp `-m`.
#[arg(
short = 'm',
long = "model",
value_name = "FILE",
required_unless_present_any = ["list_devices", "hf_repo"]
)]
pub model: Option<String>,
/// Hugging Face repo to run, `user/repo[:QUANT]`, llama.cpp's
/// `-hf`.
///
/// Fetched into the frink cache on first use and reused after. The
/// tag after the colon is a QUANT LABEL, not a git revision, and it
/// matches without regard to case.
#[arg(
long = "hf-repo",
visible_aliases = ["hf", "hfr"],
value_name = "REPO[:QUANT]",
conflicts_with = "model"
)]
pub hf_repo: Option<String>,
/// Exact filename inside `--hf-repo`, llama.cpp's `-hff`.
#[arg(
long = "hf-file",
visible_alias = "hff",
value_name = "FILE",
requires = "hf_repo"
)]
pub hf_file: Option<String>,
/// llama.cpp's `--offline`: forces use of the cache and prevents
/// network access. A `-hf` reference resolves against the files the
/// frink cache already holds, and one it does not hold is refused
/// naming the cache directory.
#[arg(long, default_value_t = false)]
pub offline: bool,
/// Penalise a token for having appeared at all, llama.cpp's
/// `--presence-penalty`. `0.0` = off, which is llama.cpp's default.
///
/// The engine and `/v1/chat/completions` have always supported
/// this; the CLI hardcoded it to zero, so the two disagreed about
/// what `frink` could do.
#[arg(long = "presence-penalty", value_name = "P", default_value_t = 0.0)]
pub presence_penalty: f32,
/// Penalise a token in proportion to how often it has appeared,
/// llama.cpp's `--frequency-penalty`. `0.0` = off.
#[arg(long = "frequency-penalty", value_name = "P", default_value_t = 0.0)]
pub frequency_penalty: f32,
/// Prompt string. Alias of llama.cpp `-p`.
#[arg(short = 'p', long = "prompt", default_value = "")]
pub prompt: String,
/// Prompt from file. Alias of llama.cpp `-f`.
#[arg(short = 'f', long = "file", value_name = "FILE")]
pub file: Option<String>,
/// Number of tokens to predict (`-1` = fill remaining context).
#[arg(
short = 'n',
long = "n-predict",
visible_alias = "predict",
default_value_t = 128
)]
pub n_predict: i64,
/// Context size: `auto` = largest that fits the device memory
/// budget, `0` = the GGUF's own `{arch}.context_length` (else
/// 4096), or an explicit token count.
#[arg(short = 'c', long = "ctx-size", default_value_t = ContextSize::FromModel)]
pub ctx_size: ContextSize,
/// Refuse to load (exit 1) when the pre-load budget says the
/// requested context will not fit, instead of warning and trying
/// anyway. Off by default because frink mmaps its weights: an
/// over-budget model really can run, page-faulting, so the check
/// is advisory unless you say otherwise.
#[arg(long = "strict-budget", default_value_t = false)]
pub strict_budget: bool,
/// CPU threads (0 = leave rayon / env defaults). Sets `RAYON_NUM_THREADS`.
#[arg(short = 't', long = "threads", default_value_t = 0)]
pub threads: usize,
/// Sampling temperature (`0` = greedy).
#[arg(long = "temp", visible_alias = "temperature", default_value_t = 0.8)]
pub temperature: f32,
/// Top-k sampling (`0` = disabled).
#[arg(long = "top-k", default_value_t = 40)]
pub top_k: usize,
/// Top-p nucleus sampling.
#[arg(long = "top-p", default_value_t = 0.95)]
pub top_p: f32,
/// Constrain generation to a GBNF grammar (llama.cpp's `--grammar`).
#[arg(long = "grammar")]
pub grammar: Option<String>,
/// Read the GBNF grammar from a file (llama.cpp's `--grammar-file`).
#[arg(long = "grammar-file")]
pub grammar_file: Option<std::path::PathBuf>,
/// Constrain generation to a JSON Schema, converted to GBNF
/// (llama.cpp's `-j` / `--json-schema`).
#[arg(short = 'j', long = "json-schema")]
pub json_schema: Option<String>,
/// Read the JSON Schema from a file, llama.cpp's `-jf` /
/// `--json-schema-file`. Exclusive with the other two grammar flags.
#[arg(long = "json-schema-file", visible_alias = "jf", value_name = "FILE")]
pub json_schema_file: Option<std::path::PathBuf>,
/// Min-p sampling: drop every candidate less than this fraction as
/// likely as the most likely one (`0.0` = disabled).
///
/// llama.cpp's `--min-p`, and its default is **0.05**, not off
/// (`common/common.h:231`, `common/arg.cpp:1987`). frink had no
/// min-p at all, so it could not reproduce llama.cpp's own
/// out-of-the-box output for any prompt.
#[arg(long = "min-p", default_value_t = 0.05)]
pub min_p: f32,
/// How many recent tokens the penalties consider (`0` = off).
///
/// llama.cpp's `--repeat-last-n`, default 64
/// (`common/common.h:238`). frink had no window and scanned the
/// whole history.
#[arg(long = "repeat-last-n", default_value_t = 64)]
pub repeat_last_n: usize,
/// Repetition penalty (`1.0` = off).
#[arg(long = "repeat-penalty", default_value_t = 1.1)]
pub repeat_penalty: f32,
/// Locally typical sampling, llama.cpp's `--typical` (`1.0` = off).
///
/// Keeps the candidates whose surprisal is closest to the
/// distribution's entropy, from the middle outward, rather than the
/// most likely ones -- so it can drop the most likely token.
#[arg(long = "typical", visible_alias = "typical-p", default_value_t = 1.0)]
pub typical_p: f32,
/// Truncate at `n` standard deviations of the logits below the
/// maximum, llama.cpp's `--top-nsigma` (`-1.0` = off).
#[arg(
long = "top-nsigma",
visible_alias = "top-n-sigma",
default_value_t = -1.0
)]
pub top_n_sigma: f32,
/// The probability that XTC removes the top candidates on any one
/// token, llama.cpp's `--xtc-probability` (`0.0` = off).
#[arg(long = "xtc-probability", default_value_t = 0.0)]
pub xtc_probability: f32,
/// The probability a candidate must reach before XTC may remove it,
/// llama.cpp's `--xtc-threshold`. **Above 0.5 disables XTC**, which
/// is upstream's guard: above a half at most one candidate can clear
/// it and XTC never removes the last one.
#[arg(long = "xtc-threshold", default_value_t = 0.1)]
pub xtc_threshold: f32,
/// DRY sequence-repetition penalty multiplier, llama.cpp's
/// `--dry-multiplier` (`0.0` = off).
///
/// Unlike `--repeat-penalty`, which looks at single tokens, DRY
/// penalises the token that would EXTEND a repeated sequence, by
/// `multiplier * base ^ (length - allowed-length)`.
#[arg(long = "dry-multiplier", default_value_t = 0.0)]
pub dry_multiplier: f32,
/// The base of DRY's exponential, llama.cpp's `--dry-base`. Below
/// 1.0 disables DRY.
#[arg(long = "dry-base", default_value_t = 1.75)]
pub dry_base: f32,
/// Repetitions this long or shorter are free, llama.cpp's
/// `--dry-allowed-length`.
#[arg(long = "dry-allowed-length", default_value_t = 2)]
pub dry_allowed_length: i32,
/// How many recent tokens DRY scans for repetitions, llama.cpp's
/// `--dry-penalty-last-n` (`0` = off, `-1` = the context size).
#[arg(long = "dry-penalty-last-n", default_value_t = -1)]
pub dry_penalty_last_n: i32,
/// A string DRY refuses to look past, llama.cpp's
/// `--dry-sequence-breaker`. Repeatable.
///
/// Giving any breaker CLEARS llama.cpp's defaults (`\n`, `:`, `"`,
/// `*`), exactly as upstream's flag does (`common/arg.cpp:2119`),
/// and the literal `none` clears them without adding one. The
/// strings are tokenised against the loaded model's own vocabulary,
/// so a checkpoint with no real vocabulary refuses DRY rather than
/// running it with no breakers.
#[arg(long = "dry-sequence-breaker", value_name = "STRING")]
pub dry_sequence_breaker: Vec<String>,
/// The order the sampler chain runs in, `;`-separated, llama.cpp's
/// `--samplers`.
///
/// frink implements five of upstream's samplers, so a chain naming
/// `dry`, `xtc`, `typ_p`, `top_n_sigma`, `mirostat` or `infill` is
/// REFUSED by that name rather than built without it: a caller who
/// asked for `xtc` and was quietly served a chain with no XTC in it
/// got a different sampler and no way to tell.
///
/// Two further rules the refusal explains when it fires:
/// `penalties` must be first (frink penalises the whole vocabulary
/// before the candidate list exists) and `temperature` must be
/// present (the greedy-versus-sampled decision is taken from
/// `--temp` before the chain runs).
#[arg(
long = "samplers",
value_name = "LIST",
default_value_t = SamplerOrder::default()
)]
pub samplers: SamplerOrder,
/// The same chain as `--samplers`, one character per sampler,
/// llama.cpp's `--sampler-seq` (`edskypmxt`).
///
/// It was an alias of `--samplers` once, which read upstream's
/// character string as one unknown sampler NAME: the spelling was
/// accepted and its grammar was not. The characters are
/// `common_sampler_type_to_chr`'s.
#[arg(
long = "sampler-seq",
visible_alias = "sampling-seq",
value_name = "CHARS",
conflicts_with = "samplers",
value_parser = |s: &str| SamplerOrder::from_chars(s).map_err(|e| e.to_string())
)]
pub sampler_seq: Option<SamplerOrder>,
/// RNG seed (`-1` = time-based).
#[arg(short = 's', long = "seed", default_value_t = -1)]
pub seed: i64,
/// Device used for offloading (`none` disables GPU use).
#[arg(
long = "device",
visible_alias = "dev",
value_name = "DEVICE",
ignore_case = true,
value_parser = clap::builder::TypedValueParser::map(
clap::builder::PossibleValuesParser::new(OffloadDevice::ALL.map(|d| d.as_str())),
|s| s.parse::<OffloadDevice>().expect("a listed value parses"),
),
)]
pub device: Option<OffloadDevice>,
/// Print available offload devices and exit.
#[arg(long = "list-devices", default_value_t = false)]
pub list_devices: bool,
/// GPU layers: `0`, `auto`, `all`, or a count at or above the
/// model's layer count.
///
/// Partial placement is not implemented, and a PARTIAL count is now
/// REFUSED rather than silently rounded up -- see
/// [`GpuLayers::check_supported`]. This comment used to say "any
/// value above zero currently enables all supported operations",
/// which described the behaviour that was the bug: llama.cpp's
/// `-ngl N` offloads exactly N layers, so accepting the count and
/// offloading everything turned the flag into an out-of-memory on
/// the machine it exists to accommodate.
#[arg(
long = "n-gpu-layers",
visible_aliases = ["gpu-layers", "ngl"],
default_value = "auto",
value_name = "N"
)]
pub n_gpu_layers: GpuLayers,
/// Draft model for speculative decoding, llama.cpp's `-md`.
///
/// A smaller checkpoint from the SAME family and tokenizer as the
/// target. Decode reads every weight of the target per token, so
/// bandwidth divided by model bytes is a hard ceiling; a drafter
/// proposes several tokens and the target checks them all in one
/// pass, which changes what is read per token rather than how fast.
/// The output is exactly what the target would have written alone.
#[arg(
long = "model-draft",
short = 'd',
visible_aliases = ["md", "spec-draft-model"],
value_name = "FILE"
)]
pub model_draft: Option<String>,
/// LoRA adapter GGUF (llama.cpp's `--lora`), applied at scale 1.
/// Repeatable, and comma-separated values are accepted as upstream
/// accepts them. The file is what `convert_lora_to_gguf.py` writes.
///
/// Every adapter is applied inside the projections it names
/// (`W x + scale * alpha / rank * B (A x)`); the fused Metal stacks
/// cannot see it and are refused for the whole model, so an adapted
/// model runs on the per-matrix path on every backend.
#[arg(long = "lora", value_name = "FILE", action = clap::ArgAction::Append)]
pub lora: Vec<String>,
/// LoRA adapter with a scale, `FILE:SCALE` (llama.cpp's
/// `--lora-scaled`). Repeatable; adapters are numbered in the order
/// given, every `--lora` before every `--lora-scaled`.
#[arg(long = "lora-scaled", value_name = "FILE:SCALE", action = clap::ArgAction::Append)]
pub lora_scaled: Vec<String>,
/// Most tokens the drafter proposes per verification step
/// (llama.cpp's `--spec-draft-n-max`, default 3). A DFlash draft
/// clamps it to its trained block minus the anchor.
///
/// `--draft-max`, `--draft` and `--draft-n` are the spellings
/// llama.cpp used before it removed them, kept so older command
/// lines still run.
#[arg(
long = "spec-draft-n-max",
visible_aliases = ["draft-max", "draft", "draft-n"],
value_name = "N",
default_value_t = 3
)]
pub draft_max: usize,
/// Fewest tokens a draft must hold to be verified at all
/// (llama.cpp's `--spec-draft-n-min`, default 0): a shorter one is
/// dropped and the step decodes one token.
#[arg(long = "spec-draft-n-min", value_name = "N", default_value_t = 0)]
pub draft_min: usize,
/// Speculative decoding type (llama.cpp's `--spec-type`). Without
/// it the type follows the `-md` file, as upstream infers it:
/// `draft-dflash` for a `dflash` GGUF, `draft-simple` otherwise.
/// `none` turns speculation off. The other upstream types
/// (`draft-eagle3`, `draft-mtp`, `draft-dspark`, `ngram-*`) are
/// refused by name.
#[arg(long = "spec-type", value_name = "TYPE")]
pub spec_type: Option<String>,
/// Stop drafting when the drafter's own probability for the token
/// it just chose is below this (llama.cpp's `--spec-draft-p-min`,
/// default 0, which never stops early).
///
/// A guessing drafter is worse than none: the target pays for the
/// position either way, and a rejection also discards every
/// position after it.
#[arg(
long = "spec-draft-p-min",
visible_alias = "draft-p-min",
value_name = "P",
default_value_t = 0.0
)]
pub draft_p_min: f32,
/// System prompt, placed by the chat template, llama.cpp's `-sys` /
/// `--system-prompt`. Refused with `--no-cnv`, which has no template
/// to place it in.
#[arg(long = "system", visible_aliases = ["sys", "system-prompt"])]
pub system: Option<String>,
/// Read the system prompt from a file, llama.cpp's `-sysf` /
/// `--system-prompt-file`. One trailing newline is dropped, as
/// upstream drops it (`common/arg.cpp:1828`); the rest is verbatim.
#[arg(
long = "system-prompt-file",
visible_alias = "sysf",
value_name = "FNAME",
conflicts_with = "system"
)]
pub system_prompt_file: Option<std::path::PathBuf>,
/// Accepted and already what happens: frink renders the GGUF's own
/// `tokenizer.chat_template` with a Jinja engine, which is what
/// llama.cpp's `--jinja` turns on.
#[arg(long = "jinja", default_value_t = false)]
pub jinja: bool,
/// Refused: frink has no non-Jinja template matcher to fall back to.
/// `--no-cnv` is the raw completion.
#[arg(long = "no-jinja", default_value_t = false)]
pub no_jinja: bool,
/// A chat template in place of the GGUF's own, llama.cpp's
/// `--chat-template`: its Jinja source, or `chatml`. Any other bare
/// name is refused.
#[arg(
long = "chat-template",
value_name = "JINJA_TEMPLATE",
overrides_with = "chat_template_file"
)]
pub chat_template: Option<String>,
/// `--chat-template`, read from a file. The later of the two wins,
/// as in llama.cpp.
#[arg(
long = "chat-template-file",
value_name = "JINJA_TEMPLATE_FILE",
overrides_with = "chat_template"
)]
pub chat_template_file: Option<std::path::PathBuf>,
/// Template variables, as a JSON object, llama.cpp's
/// `--chat-template-kwargs`. Repeatable.
#[arg(long = "chat-template-kwargs", value_name = "STRING")]
pub chat_template_kwargs: Vec<String>,
/// Thinking in the chat, llama.cpp's `-rea`: `on`, `off` or `auto`
/// (the default), which turns it on where the template supports it.
#[arg(
long = "reasoning",
visible_alias = "rea",
value_name = "on|off|auto",
allow_hyphen_values = true
)]
pub reasoning: Option<String>,
/// The `reasoning_effort` template variable, llama.cpp's
/// `--reasoning-effort`. `default` leaves the template's own.
#[arg(long = "reasoning-effort", value_name = "LEVEL")]
pub reasoning_effort: Option<String>,
/// The `preserve_reasoning` template variable, llama.cpp's
/// `--reasoning-preserve` and its default.
#[arg(long = "reasoning-preserve", overrides_with = "no_reasoning_preserve")]
pub reasoning_preserve: bool,
/// `preserve_reasoning` false. The later of the pair wins.
#[arg(long = "no-reasoning-preserve", overrides_with = "reasoning_preserve")]
pub no_reasoning_preserve: bool,
/// The flags above, read by `apply_llama_flags`.
#[arg(skip)]
pub template_flags: frink_models::template_flags::TemplateFlags,
/// Run one throwaway forward pass before the timed prompt, llama.cpp's
/// `--warmup` (the default, on both).
#[arg(long = "warmup", overrides_with = "no_warmup", default_value_t = false)]
pub warmup: bool,
/// Skip the throwaway forward pass, llama.cpp's `--no-warmup`. The
/// prompt rate then includes first-dispatch cost.
#[arg(long = "no-warmup", overrides_with = "warmup", default_value_t = false)]
pub no_warmup: bool,
/// Flash attention, llama.cpp's `-fa` (`on`, `off`, `auto`). `on`
/// and `auto` are what frink does wherever the backend has a fused
/// kernel; `off` is refused ([`frink_core::argv::check_flash_attn`]).
#[arg(
long = "flash-attn",
visible_alias = "fa",
value_name = "MODE",
num_args = 0..=1,
default_missing_value = "auto"
)]
pub flash_attn: Option<String>,
/// Hugging Face access token for `-hf`, llama.cpp's `-hft` /
/// `--hf-token`. Sets `HF_TOKEN`, the variable the downloader reads.
#[arg(long = "hf-token", visible_alias = "hft", value_name = "TOKEN")]
pub hf_token: Option<String>,
/// Print the timing lines after the answer, llama.cpp's
/// `--show-timings` (the default).
#[arg(
long = "show-timings",
overrides_with = "no_show_timings",
default_value_t = false
)]
pub show_timings: bool,
/// Do not print the timing lines, llama.cpp's `--no-show-timings`.
#[arg(
long = "no-show-timings",
overrides_with = "show_timings",
default_value_t = false
)]
pub no_show_timings: bool,
/// Colour the prompt echo and the timings on stderr, llama.cpp's
/// `-co` / `--color` (`on`, `off`, `auto`; `auto` is the default and
/// means "when stderr is a terminal"). A bare `-co` means `on`.
#[arg(
long = "color",
visible_alias = "co",
value_name = "on|off|auto",
num_args = 0..=1,
default_missing_value = "on"
)]
pub color: Option<String>,
/// Raw prompt: skip chat-template wrap (llama.cpp `--no-cnv`).
///
/// llama.cpp REMOVED `--no-cnv` in its 0.4 launcher and spells the
/// same intent `-st` / `--single-turn`. Both are accepted here:
/// dropping the old name would break every command line written
/// against the older tool, and refusing the new one means a
/// current llama.cpp invocation fails against frink -- which is
/// the whole thing this flag exists to avoid.
#[arg(long = "no-cnv", default_value_t = false)]
pub no_cnv: bool,
/// Accepted and ignored: `frink run` is already one turn.
///
/// llama.cpp's `-st` / `--single-turn` means "run the CONVERSATION
/// for one turn, then exit" -- the chat template still applies. It
/// is NOT `--no-cnv`, which skips the template, and mapping one to
/// the other changes the answer: measured on the same prompt, the
/// templated reply is "The capital of France is Paris." and the
/// raw one continues " Paris\nThe capital city of France is...".
///
/// `frink run` generates once and exits, so the flag already
/// describes what it does. Accepted so a llama.cpp command line
/// runs unchanged, and ignored rather than silently redirected.
#[arg(long = "single-turn", visible_alias = "st", default_value_t = false)]
pub single_turn: bool,
/// Process `\\n` / `\\t` / `\\r` / `\\\\` escapes in `-p`. Use
/// `--no-escape` to pass the prompt through literally.
///
/// Defaults TRUE, matching llama.cpp (`common/common.h:563`), which
/// also spells the negation `--no-escape` (`common/arg.cpp:1799`).
/// frink defaulted false, so `-p "line one\\nline two"` reached the
/// model as a literal backslash-n on frink and as a newline on
/// llama.cpp -- the same command, a different prompt, and no error
/// either way.
#[arg(
short = 'e',
long = "escape",
default_value_t = true,
overrides_with = "no_escape"
)]
pub escape: bool,
/// Pass the prompt through literally, without expanding escapes.
#[arg(long = "no-escape", action = clap::ArgAction::SetTrue)]
pub no_escape: bool,
/// Ignore EOS and always emit up to `-n` tokens.
#[arg(long = "ignore-eos", default_value_t = false)]
pub ignore_eos: bool,
/// Print the final prompt before generation.
#[arg(long = "verbose-prompt", default_value_t = false)]
pub verbose_prompt: bool,
/// Multi-token prediction (MTP) draft heads — not loaded from GGUF yet.
#[arg(long = "mtp", default_value_t = false)]
pub mtp: bool,
/// KV cache dtype (llama.cpp `-ctk` analogue). Sets `FRINK_CTK`.
/// Values: `f16` (default), `q8_0`, `fp8` (the Q8_0 wire) and `q4`
/// (4 bits per element, with a Hadamard rotation on K where the
/// head width allows it). An unrecognised value falls back to
/// `f16`, as does every value on a backend whose KV cache is the
/// host `Vec<f32>`.
///
/// `env` is not decoration: `docs/CONFIG.md` has always documented
/// `FRINK_CTK` as "same as `--ctk`", and it could not be, because
/// the resolution below writes this field's value into that
/// variable unconditionally and the field's default is `f16`. An
/// environment that said `q4` was overwritten before any Metal
/// code read it (GitHub issue #297). Letting clap read the variable
/// as the default keeps one spelling: the flag wins when given, the
/// environment when it is not, and the write-back below is then
/// idempotent rather than destructive.
#[arg(
long = "ctk",
visible_alias = "cache-type-k",
value_name = "TYPE",
env = "FRINK_CTK",
default_value = "f16",
value_parser = frink_models::ctk::parse_value
)]
pub ctk: String,
}
impl InferArgs {
/// Whether the throwaway warm-up pass runs (`--warmup`, the default,
/// or `--no-warmup`, whichever came last).
pub fn warmup_enabled(&self) -> bool {
!self.no_warmup
}
/// The llama.cpp flags that are settled before anything is loaded,
/// once, for every engine path: refusals, `-sysf` read into
/// `--system`, `-hft` into `HF_TOKEN`, and the stderr presentation
/// (`-co`, `--no-show-timings`).
///
/// Called at the top of `run_infer`, before the dispatch to the
/// dedicated engines, so no path can see the flags unapplied.
pub fn apply_llama_flags(&mut self) -> anyhow::Result<()> {
if self.no_jinja {
anyhow::bail!(
"--no-jinja: frink has no non-Jinja template mode. It renders the GGUF's own \
tokenizer.chat_template, which is what llama.cpp's --jinja turns on, and has \
no built-in template matcher to fall back to. Use --no-cnv for a raw completion"
);
}
if let Some(mode) = &self.flash_attn {
frink_core::argv::check_flash_attn(mode).map_err(|e| anyhow::anyhow!(e))?;
}
self.resolve_template_flags()?;
if let Some(token) = &self.hf_token {
// SAFETY: single-threaded, before any worker or download.
unsafe { std::env::set_var("HF_TOKEN", token) };
}
self.read_system_prompt_file()?;
crate::cli_output::set_colour(self.colour_choice()?);
crate::cli_output::set_show_timings(self.show_timings_enabled());
Ok(())
}
/// The chat-template flags, checked before anything loads. They have
/// nothing to act on with `--no-cnv`, which skips the template, so
/// accepting them there would be a flag not honoured.
pub(crate) fn resolve_template_flags(&mut self) -> anyhow::Result<()> {
let reasoning = self
.reasoning
.as_deref()
.map(frink_core::argv::reasoning_switch)
.transpose()
.map_err(anyhow::Error::msg)?
.flatten();
self.template_flags = frink_models::template_flags::TemplateFlags::resolve(
frink_models::template_flags::TemplateFlagValues {
template: self.chat_template.as_deref(),
template_file: self.chat_template_file.as_deref(),
kwargs: &self.chat_template_kwargs,
reasoning,
reasoning_effort: self.reasoning_effort.as_deref(),
reasoning_preserve: frink_core::argv::flag_pair(
self.reasoning_preserve,
self.no_reasoning_preserve,
),
},
)
.map_err(anyhow::Error::msg)?;
if self.no_cnv && !self.template_flags.is_empty() {
anyhow::bail!(
"the chat-template flags have no effect with --no-cnv, which skips the chat \
template. Drop one of the two"
);
}
Ok(())
}
/// `-sysf FILE` read into `--system`, dropping one trailing newline
/// as llama.cpp does (`common/arg.cpp`, `--system-prompt-file`).
pub(crate) fn read_system_prompt_file(&mut self) -> anyhow::Result<()> {
if let Some(path) = self.system_prompt_file.take() {
let mut text = std::fs::read_to_string(&path)
.map_err(|e| anyhow::anyhow!("--system-prompt-file {}: {e}", path.display()))?;
if text.ends_with('\n') {
text.pop();
}
self.system = Some(text);
}
Ok(())
}
/// `-co`: `Some(true)` on, `Some(false)` off, `None` for `auto` or
/// no flag (colour when stderr is a terminal). A value llama.cpp's
/// `--color` would throw on is refused.
pub(crate) fn colour_choice(&self) -> anyhow::Result<Option<bool>> {
use frink_core::argv::Switch;
let Some(value) = &self.color else {
return Ok(None);
};
match frink_core::argv::llama_switch(value.trim()) {
Some(Switch::On) => Ok(Some(true)),
Some(Switch::Off) => Ok(Some(false)),
Some(Switch::Auto) => Ok(None),
None => anyhow::bail!("--color {value}: expected on, off or auto"),
}
}
/// Whether timing lines print (`--show-timings`, the default, or
/// `--no-show-timings`, whichever came last).
pub fn show_timings_enabled(&self) -> bool {
!self.no_show_timings
}
/// The grammar these flags describe, if any.
///
/// The three spellings are llama.cpp's and are MUTUALLY EXCLUSIVE
/// there. Refused together rather than silently picking one, because
/// a caller who passed both asked for two different constraints and
/// honouring either is answering a question they did not ask.
pub fn grammar_source(&self) -> anyhow::Result<Option<String>> {
let given = [
self.grammar.is_some(),
self.grammar_file.is_some(),
self.json_schema.is_some(),
self.json_schema_file.is_some(),
]
.iter()
.filter(|b| **b)
.count();
if given > 1 {
anyhow::bail!(
"--grammar, --grammar-file, --json-schema and --json-schema-file are mutually \
exclusive; pass exactly one"
);
}
if let Some(path) = &self.json_schema_file {
let schema = std::fs::read_to_string(path)
.map_err(|e| anyhow::anyhow!("--json-schema-file {}: {e}", path.display()))?;
return Ok(Some(
frink_models::grammar::json_schema_to_grammar(&schema)
.map_err(|e| anyhow::anyhow!("--json-schema-file: {e}"))?,
));
}
if let Some(g) = &self.grammar {
return Ok(Some(g.clone()));
}
if let Some(path) = &self.grammar_file {
return Ok(Some(std::fs::read_to_string(path).map_err(|e| {
anyhow::anyhow!("--grammar-file {}: {e}", path.display())
})?));
}
if let Some(schema) = &self.json_schema {
// Converted here rather than at the sampler, so a schema that
// cannot be expressed fails BEFORE the model is loaded.
return Ok(Some(
frink_models::grammar::json_schema_to_grammar(schema)
.map_err(|e| anyhow::anyhow!("--json-schema: {e}"))?,
));
}
Ok(None)
}
/// The DRY configuration these flags spell, before its sequence
/// breakers are tokenised.
///
/// llama.cpp's `--dry-sequence-breaker` CLEARS the defaults the
/// first time it is given (`common/arg.cpp:2119-2126`) and reads
/// the literal `none` as "no breakers at all". Both are reproduced
/// here, and `none` anywhere in the list clears it, because a caller
/// who wrote it meant it.
pub fn dry_request(&self) -> frink_models::dry::DryRequest {
let sequence_breakers = if self.dry_sequence_breaker.is_empty() {
frink_models::dry::DEFAULT_SEQUENCE_BREAKERS
.iter()
.map(|s| s.to_string())
.collect()
} else if self.dry_sequence_breaker.iter().any(|s| s == "none") {
Vec::new()
} else {
self.dry_sequence_breaker.clone()
};
frink_models::dry::DryRequest {
multiplier: self.dry_multiplier,
base: self.dry_base,
allowed_length: self.dry_allowed_length,
penalty_last_n: self.dry_penalty_last_n,
sequence_breakers,
}
}
/// The sampler these flags describe.
///
/// One function rather than one copy per generation path. There were
/// four identical literals here (the dense path, the engine path and
/// two chat paths), which is the shape ADR 0004 names as this
/// repo's most expensive failure: adding `--min-p` meant editing
/// four places, and a sampler added to three of them would be
/// silently absent from the fourth with every test still green.
///
/// Fallible, and taking the vocabulary, because of DRY: its sequence
/// breakers are STRINGS that only mean something against a
/// particular tokenizer, so a checkpoint with no real vocabulary
/// must refuse `--dry-multiplier` rather than run DRY with no
/// breakers. `vocab` is `None` for exactly those checkpoints, and
/// `ctx_size` is what `--dry-penalty-last-n -1` resolves to.
pub fn sampling(
&self,
vocab: Option<&dyn frink_models::dry::DryVocab>,
ctx_size: usize,
) -> anyhow::Result<SamplingParams> {
Ok(SamplingParams {
temperature: self.temperature,
top_p: self.top_p,
min_p: self.min_p,
top_k: self.top_k,
typical_p: self.typical_p,
top_n_sigma: self.top_n_sigma,
xtc_probability: self.xtc_probability,
xtc_threshold: self.xtc_threshold,
dry: self.dry_request().resolve(vocab, ctx_size)?,
repetition_penalty: self.repeat_penalty,
penalty_last_n: self.repeat_last_n,
presence_penalty: self.presence_penalty,
frequency_penalty: self.frequency_penalty,
sampler_order: self.sampler_seq.unwrap_or(self.samplers),
})
}
}
pub use frink_models::offload::OffloadDevice;
pub use frink_models::gpu_layers::GpuLayers;
/// What `-c` / `--ctx-size` was asked for, before any model is opened.
/// Same shape as [`GpuLayers`]: a symbolic value alongside the literal
/// one, resolved once the header and the device budget are known.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContextSize {
/// Largest context that fits the device memory budget.
Auto,
/// llama.cpp's `-c 0`: whatever the GGUF says it was trained for.
FromModel,
Tokens(usize),
}
impl FromStr for ContextSize {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value.trim() {
"auto" => Ok(Self::Auto),
"0" => Ok(Self::FromModel),
other => other
.parse::<usize>()
.map(Self::Tokens)
.map_err(|_| "expected 'auto', 0, or a positive token count".into()),
}
}
}
impl fmt::Display for ContextSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Auto => f.write_str("auto"),
Self::FromModel => f.write_str("0"),
Self::Tokens(n) => n.fmt(f),
}
}
}