Skip to main content

teeny_quant/
error.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//! Error types for `teeny-quant`.
18
19/// `teeny-quant`'s result alias.
20pub type Result<T> = std::result::Result<T, Error>;
21
22/// Errors produced by `teeny-quant`.
23#[derive(thiserror::Error, Debug)]
24pub enum Error {
25    /// An I/O operation (reading/writing a `.safetensors` file) failed.
26    #[error("I/O error: {0}")]
27    Io(#[from] std::io::Error),
28
29    /// Reading or writing the underlying `.safetensors` container failed.
30    #[error("safetensors error: {0}")]
31    SafeTensors(#[from] safetensors::SafeTensorError),
32
33    /// Reading the source checkpoint (via `teeny-data`) failed.
34    #[error("failed to read source checkpoint: {0}")]
35    Read(#[from] anyhow::Error),
36
37    /// Serializing/deserializing quantization metadata as JSON failed.
38    #[error("JSON error: {0}")]
39    Json(#[from] serde_json::Error),
40
41    /// A tensor's source dtype isn't one this crate knows how to quantize/upcast.
42    #[error("tensor '{tensor}' has unsupported source dtype {dtype:?}")]
43    UnsupportedDtype {
44        /// The tensor's name.
45        tensor: String,
46        /// The tensor's on-disk dtype.
47        dtype: safetensors::Dtype,
48    },
49
50    /// A requested tensor doesn't exist in the source checkpoint.
51    #[error("tensor '{0}' not found")]
52    TensorNotFound(String),
53
54    /// A quantization axis was out of range for the tensor's rank.
55    #[error("axis {axis} is out of range for tensor '{tensor}' with {rank} dimensions")]
56    InvalidAxis {
57        /// The tensor's name.
58        tensor: String,
59        /// The requested axis.
60        axis: usize,
61        /// The tensor's rank.
62        rank: usize,
63    },
64
65    /// A group size of `0` was requested for group-wise quantization.
66    #[error("group size must be non-zero (tensor '{0}')")]
67    InvalidGroupSize(String),
68
69    /// Two tensors that should describe the same underlying weight (e.g. a weight and its
70    /// dequantized reconstruction) have different shapes.
71    #[error("shape mismatch for tensor '{name}': {a:?} vs {b:?}")]
72    ShapeMismatch {
73        /// The tensor's name.
74        name: String,
75        /// The first shape.
76        a: Vec<usize>,
77        /// The second shape.
78        b: Vec<usize>,
79    },
80}