rlx_cli/weights_resolve.rs
1// RLX — versatile ML compiler + runtime.
2// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, version 3.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16//! Shared `--weights` resolution for model CLIs (directory quants, split hints).
17
18use anyhow::{Result, anyhow};
19use rlx_core::gguf_support::DEFAULT_GGUF_PREFER_SUBSTR;
20use rlx_core::{ResolveWeightsOptions, resolve_weights_file_with_options};
21use std::path::{Path, PathBuf};
22
23/// CLI options for resolving a weights path (file or directory).
24#[derive(Debug, Clone, Default)]
25pub struct WeightsResolveCli {
26 pub prefer_gguf: Option<String>,
27 pub gguf_index: Option<usize>,
28}
29
30impl WeightsResolveCli {
31 pub fn to_resolve_opts(&self) -> ResolveWeightsOptions<'_> {
32 ResolveWeightsOptions {
33 prefer_gguf_substring: self
34 .prefer_gguf
35 .as_deref()
36 .or(Some(DEFAULT_GGUF_PREFER_SUBSTR)),
37 gguf_index: self.gguf_index,
38 }
39 }
40
41 /// Parse `--prefer-quant` / `-p` and `--gguf-index` from argv (does not consume `--weights`).
42 pub fn parse_optional_flags(args: &[String], i: &mut usize) -> Result<Self> {
43 let mut out = Self::default();
44 while *i < args.len() {
45 match args[*i].as_str() {
46 "--prefer-quant" | "--prefer" | "-p" => {
47 *i += 1;
48 out.prefer_gguf = Some(
49 args.get(*i)
50 .ok_or_else(|| anyhow!("missing value for --prefer-quant"))?
51 .clone(),
52 );
53 *i += 1;
54 }
55 "--gguf-index" => {
56 *i += 1;
57 out.gguf_index = Some(
58 args.get(*i)
59 .ok_or_else(|| anyhow!("missing value for --gguf-index"))?
60 .parse()?,
61 );
62 *i += 1;
63 }
64 _ => break,
65 }
66 }
67 Ok(out)
68 }
69}
70
71/// Resolve `path` using CLI defaults (`Q4_K_M` preference in multi-`.gguf` dirs).
72pub fn resolve_weights_cli(path: &Path, cli: &WeightsResolveCli) -> Result<PathBuf> {
73 resolve_weights_file_with_options(path, &cli.to_resolve_opts())
74}