1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use super::*;
fn validate_root_bounds(values: &[KernelValue], root: KernelValueId) -> Result<(), KernelIrError> {
if root.index() >= values.len() {
return Err(KernelIrError::RootOutOfBounds {
root: root.index(),
len: values.len(),
});
}
Ok(())
}
fn validate_scalar_root(
values: &[KernelValue],
root: KernelValueId,
operation: &'static str,
message: &'static str,
) -> Result<(), KernelIrError> {
if !values[root.index()].kind.is_scalar() {
return Err(KernelInstruction::shape_error(
root.index(),
operation,
message,
));
}
Ok(())
}
fn validate_cache_outputs(
values: &[KernelValue],
outputs: &[KernelValueId],
) -> Result<(), KernelIrError> {
for output in outputs {
if output.index() >= values.len() {
return Err(KernelIrError::CacheOutputOutOfBounds {
output: output.index(),
len: values.len(),
});
}
}
Ok(())
}
fn validate_gradient_outputs(
values: &[KernelValue],
outputs: &[KernelValueId],
) -> Result<(), KernelIrError> {
for output in outputs {
let Some(value) = values.get(output.index()) else {
return Err(KernelIrError::GradientOutOfBounds {
output: output.index(),
len: values.len(),
});
};
if value.kind != KernelValueKind::Real {
return Err(KernelIrError::GradientKindMismatch {
output: output.index(),
actual: value.kind,
});
}
}
Ok(())
}
impl ScalarKernelIr {
/// Validates values and constructs a scalar kernel rooted at `root`.
///
/// # Errors
///
/// Returns [`KernelIrError`] when the value list is empty, `root` or an
/// operand is out of bounds, values are not topologically ordered, a
/// value's kind or class is inconsistent with its instruction, or the
/// root is not scalar.
pub fn new(values: Vec<KernelValue>, root: KernelValueId) -> Result<Self, KernelIrError> {
let ir = Self { values, root };
ir.validate()?;
Ok(ir)
}
/// Revalidates ordering, types, classes, and the scalar root.
///
/// # Errors
///
/// Returns [`KernelIrError`] when the IR is empty, its root or an operand
/// is out of bounds, its values are not topologically ordered, or a
/// value's kind, class, or shape is inconsistent with its instruction.
pub fn validate(&self) -> Result<(), KernelIrError> {
if self.values.is_empty() {
return validate_graph(&self.values);
}
validate_root_bounds(&self.values, self.root)?;
validate_graph(&self.values)?;
validate_scalar_root(
&self.values,
self.root,
"kernel root",
"root must be scalar",
)
}
/// Returns all IR values in topological order.
pub fn values(&self) -> &[KernelValue] {
&self.values
}
/// Returns the scalar output identifier.
pub fn root(&self) -> KernelValueId {
self.root
}
}
impl CacheKernelIr {
/// Validates values and constructs a cache kernel with the given outputs.
///
/// # Errors
///
/// Returns [`KernelIrError`] when `outputs` is empty, an output or operand
/// is out of bounds, values are not topologically ordered, or a value's
/// kind, class, or shape is inconsistent with its instruction.
pub fn new(
values: Vec<KernelValue>,
outputs: Vec<KernelValueId>,
) -> Result<Self, KernelIrError> {
if outputs.is_empty() {
return Err(KernelIrError::EmptyCacheOutputs);
}
validate_graph(&values)?;
validate_cache_outputs(&values, &outputs)?;
Ok(Self { values, outputs })
}
/// Returns all IR values in topological order.
pub fn values(&self) -> &[KernelValue] {
&self.values
}
/// Returns cache output identifiers in storage order.
pub fn outputs(&self) -> &[KernelValueId] {
&self.outputs
}
}
impl GradientKernelIr {
/// Validates and constructs a gradient kernel.
///
/// # Errors
///
/// Returns [`KernelIrError`] when the primal IR is invalid, the primal
/// root is not scalar, a gradient output is out of bounds, or a gradient
/// output is not real-valued.
pub fn new(
values: Vec<KernelValue>,
primal_root: KernelValueId,
outputs: Vec<KernelValueId>,
component: OutputComponent,
) -> Result<Self, KernelIrError> {
let ir = Self {
values,
primal_root,
outputs,
component,
};
ir.validate()?;
Ok(ir)
}
/// Revalidates the primal root and real gradient outputs.
///
/// # Errors
///
/// Returns [`KernelIrError`] when the primal IR is invalid, the primal
/// root is not scalar, a gradient output is out of bounds, or a gradient
/// output is not real-valued.
pub fn validate(&self) -> Result<(), KernelIrError> {
if self.values.is_empty() {
return validate_graph(&self.values);
}
validate_root_bounds(&self.values, self.primal_root)?;
validate_graph(&self.values)?;
validate_scalar_root(
&self.values,
self.primal_root,
"gradient primal root",
"primal root must be scalar",
)?;
validate_gradient_outputs(&self.values, &self.outputs)
}
/// Returns all primal and derivative IR values in topological order.
pub fn values(&self) -> &[KernelValue] {
&self.values
}
/// Returns the primal scalar output identifier.
pub fn primal_root(&self) -> KernelValueId {
self.primal_root
}
/// Returns derivative output identifiers.
pub fn outputs(&self) -> &[KernelValueId] {
&self.outputs
}
/// Returns the differentiated component of the complex primal.
pub fn component(&self) -> OutputComponent {
self.component
}
}