1#![allow(clippy::all, clippy::pedantic, clippy::disallowed_methods)]
10#![allow(
11 unreachable_code,
12 unused_variables,
13 unused_imports,
14 dead_code,
15 unused_assignments
16)]
17
18use clap::{Parser, Subcommand, ValueEnum};
19use std::path::{Path, PathBuf};
20
21#[macro_use]
23#[allow(unused_macros, clippy::duplicated_attributes)]
24mod generated_contracts;
25
26mod commands;
27pub mod error;
28mod output;
29pub mod pipe;
30
31pub use error::CliError;
32
33pub mod qa_types {
35 pub use crate::commands::qa::{GateResult, QaReport, SystemInfo};
36}
37
38pub mod model_pull {
40 pub use crate::commands::pull::{list, run};
41}
42
43pub mod serve_auth {
47 #[cfg(feature = "inference")]
48 pub use crate::commands::serve::auth::layer;
49 pub use crate::commands::serve::auth::{apply, AuthGate};
50}
51
52#[cfg(feature = "inference")]
53pub mod federation;
54
55use commands::{
57 bench, canary, canary::CanaryCommands, cbtop, chat, compare_hf, compile, convert, data, debug,
58 diagnose, diff, distill, eval, explain, export, flow, hex, import, inspect, lint, mcp, merge,
59 oracle, pipeline, probar, profile, prune, publish, pull, qa, qualify, quantize, rosetta,
60 rosetta::RosettaCommands, run, serve, showcase, stamp, tensors, tokenize, trace, tree, tui,
61 validate, validate_manifest,
62};
63#[cfg(feature = "training")]
64use commands::{finetune, gpu, train, tune};
65
66#[cfg(feature = "training")]
67pub use commands::pretrain::PretrainMode;
68
69#[derive(Parser, Debug)]
74#[command(name = "apr")]
75#[command(author, version = concat!(env!("CARGO_PKG_VERSION"), " (", env!("APR_GIT_SHA"), ")"), about, long_about = None)]
76#[command(propagate_version = true)]
77pub struct Cli {
78 #[command(subcommand)]
79 pub command: Box<Commands>,
80
81 #[arg(long, global = true)]
83 pub json: bool,
84
85 #[arg(short, long, global = true)]
87 pub verbose: bool,
88
89 #[arg(short, long, global = true)]
91 pub quiet: bool,
92
93 #[arg(long, global = true)]
95 pub offline: bool,
96
97 #[arg(long, global = true)]
99 pub skip_contract: bool,
100}
101
102include!("commands_enum.rs");
103include!("model_ops_commands.rs");
104include!("extended_commands.rs");
105include!("tool_commands.rs");
106include!("data_commands.rs");
107#[cfg(feature = "training")]
108include!("train_commands.rs");
109include!("serve_commands.rs");
110include!("tokenize_commands.rs");
111include!("pipeline_commands.rs");
112include!("validate.rs");
113include!("dispatch_run.rs");
114include!("dispatch.rs");
115include!("dispatch_analysis.rs");
116include!("lib_07.rs");
117
118pub fn cli_main() -> std::process::ExitCode {
124 #[cfg(unix)]
126 #[allow(unsafe_code)]
127 unsafe {
128 libc::signal(libc::SIGPIPE, libc::SIG_DFL);
129 }
130
131 #[cfg(target_arch = "aarch64")]
133 #[allow(unsafe_code)]
134 unsafe {
135 let fpcr: u64;
136 core::arch::asm!("mrs {}, fpcr", out(reg) fpcr);
137 if fpcr & (1 << 19) != 0 {
138 let new_fpcr = fpcr & !(1 << 19);
139 core::arch::asm!("msr fpcr, {}", in(reg) new_fpcr);
140 }
141 }
142
143 let no_color = std::env::var("NO_COLOR").is_ok();
145 let is_tty = std::io::IsTerminal::is_terminal(&std::io::stdout());
146 if no_color || !is_tty {
147 colored::control::set_override(false);
148 }
149
150 let raw: Vec<String> = std::env::args().collect();
157 if raw.iter().any(|a| a == "--version") && raw.iter().any(|a| a == "--json") {
158 emit_version_json();
159 return std::process::ExitCode::SUCCESS;
160 }
161
162 let cli = Cli::parse();
163 match execute_command(&cli) {
164 Ok(()) => std::process::ExitCode::SUCCESS,
165 Err(e) => {
166 eprintln!("error: {e}");
167 e.exit_code()
168 }
169 }
170}
171
172pub fn emit_version_json() {
192 let cuda_feature = cfg!(feature = "cuda");
193
194 let cuda_runtime_available = std::process::Command::new("nvidia-smi")
198 .arg("-L")
199 .output()
200 .map(|o| o.status.success())
201 .unwrap_or(false);
202
203 let visible_devices: Vec<String> = if cuda_runtime_available {
207 std::process::Command::new("nvidia-smi")
208 .arg("-L")
209 .output()
210 .ok()
211 .and_then(|o| String::from_utf8(o.stdout).ok())
212 .map(|s| {
213 s.lines()
214 .filter_map(|line| {
215 line.strip_prefix("GPU ").and_then(|rest| {
217 rest.split_once(':').map(|(idx, _)| idx.trim().to_string())
218 })
219 })
220 .collect()
221 })
222 .unwrap_or_default()
223 } else {
224 Vec::new()
225 };
226
227 let body = serde_json::json!({
228 "name": "apr",
229 "version": env!("CARGO_PKG_VERSION"),
230 "git_sha": env!("APR_GIT_SHA"),
231 "cuda_feature": cuda_feature,
232 "cuda_runtime_available": cuda_runtime_available,
233 "visible_devices": visible_devices,
234 });
235
236 println!(
239 "{}",
240 serde_json::to_string_pretty(&body).expect("build version json")
241 );
242}