1use std::error::Error;
2
3#[derive(Debug, thiserror::Error)]
5#[non_exhaustive]
6pub enum InitializationError<E>
7where
8 E: Error + 'static,
9{
10 #[error("random initialization requires rank {minimum}..={maximum}, got {actual}")]
12 InvalidRank {
13 actual: usize,
15 minimum: usize,
17 maximum: usize,
19 },
20 #[error("initialization parameter {parameter} must be finite, got {value}")]
22 NonFiniteParameter {
23 parameter: &'static str,
25 value: f64,
27 },
28 #[error("uniform lower bound must not exceed upper bound, got {low} > {high}")]
30 InvalidUniformBounds {
31 low: f64,
33 high: f64,
35 },
36 #[error("normal standard deviation must be non-negative, got {value}")]
38 NegativeStandardDeviation {
39 value: f64,
41 },
42 #[error("{parameter} must be positive, got {value}")]
44 InvalidFan {
45 parameter: &'static str,
47 value: usize,
49 },
50 #[error("attention d_model {d_model} must be positive and divisible by {heads} heads")]
52 InvalidHeadConfiguration {
53 d_model: usize,
55 heads: usize,
57 },
58 #[error("transformer layer count must be positive, got {layers}")]
60 InvalidLayerCount {
61 layers: usize,
63 },
64 #[error("fan_in + fan_out exceeds usize: {fan_in} + {fan_out}")]
66 FanArithmeticOverflow {
67 fan_in: usize,
69 fan_out: usize,
71 },
72 #[error("{lhs_name} * {rhs_name} exceeds usize: {lhs} * {rhs}")]
74 FanProductOverflow {
75 lhs_name: &'static str,
77 lhs: usize,
79 rhs_name: &'static str,
81 rhs: usize,
83 },
84 #[error("{operation} backend initialization failed")]
86 Backend {
87 operation: &'static str,
89 #[source]
91 source: E,
92 },
93}
94
95impl<E> From<E> for InitializationError<E>
96where
97 E: Error + 'static,
98{
99 fn from(source: E) -> Self {
100 Self::Backend {
101 operation: "parameter construction",
102 source,
103 }
104 }
105}