Skip to main content

teeny_quant/cli/
inspect.rs

1/*
2 * Copyright (c) 2026 Teenygrad.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *   http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! `teeny-quant inspect`: lists a checkpoint's tensors (and, if present, its
18//! `quantization_config`).
19
20use std::path::PathBuf;
21
22use anyhow::{Context, Result};
23use clap::Args;
24
25use crate::format;
26use crate::read::read_metadata;
27
28/// `teeny-quant inspect` arguments.
29#[derive(Args, Debug)]
30pub struct InspectArgs {
31    /// `.safetensors` file to inspect.
32    pub path: PathBuf,
33}
34
35/// Runs `teeny-quant inspect`.
36pub fn run(args: InspectArgs) -> Result<()> {
37    let mapped = teeny_data::safetensors::SafeTensors::from_pretrained(&args.path)
38        .with_context(|| format!("failed to open '{}'", args.path.display()))?;
39    let tensors = mapped.tensors().with_context(|| {
40        format!(
41            "failed to read tensor headers from '{}'",
42            args.path.display()
43        )
44    })?;
45
46    let mut names = tensors.names();
47    names.sort();
48
49    println!("{:<55} {:<10} {:>14}  shape", "name", "dtype", "bytes");
50    let mut total_bytes = 0usize;
51    for name in &names {
52        let view = tensors
53            .tensor(name)
54            .with_context(|| format!("reading tensor '{name}'"))?;
55        let nbytes = view.data().len();
56        total_bytes += nbytes;
57        println!(
58            "{:<55} {:<10} {:>14}  {:?}",
59            name,
60            format!("{:?}", view.dtype()),
61            nbytes,
62            view.shape()
63        );
64    }
65    println!(
66        "\n{} tensor(s), {:.2} MiB total",
67        names.len(),
68        total_bytes as f64 / (1024.0 * 1024.0)
69    );
70
71    let metadata = read_metadata(&args.path)?;
72    if let Some(config) = format::config_from_metadata(&metadata)? {
73        println!(
74            "\nquantization_config:\n{}",
75            serde_json::to_string_pretty(&config)?
76        );
77    }
78
79    Ok(())
80}