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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use super::*;
/// Metadata validation error emitted before storage access.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DescriptorError {
/// Wrong number of inputs for an operation.
Arity {
/// Operation this query targets.
operation: OperationKind,
/// Inclusive range the descriptor accepts.
expected: core::ops::RangeInclusive<usize>,
/// Value actually observed.
actual: usize,
},
/// Wrong number of outputs produced by a backend.
OutputArity {
/// Operation this query targets.
operation: OperationKind,
/// Inclusive range the descriptor accepts.
expected: core::ops::RangeInclusive<usize>,
/// Value actually observed.
actual: usize,
},
/// An input's rank fell outside the accepted range.
Rank {
/// Operation this query targets.
operation: OperationKind,
/// Zero-based index of the offending input.
input: usize,
/// Inclusive range the descriptor accepts.
expected: core::ops::RangeInclusive<usize>,
/// Value actually observed.
actual: usize,
},
/// Two inputs live on different devices.
DeviceMismatch {
/// Operation this query targets.
operation: OperationKind,
/// Zero-based index of the offending input.
input: usize,
/// Device the contract requires.
expected: DeviceId,
/// Device storage actually resides on.
actual: DeviceId,
},
/// The operation has no catalog row; it cannot be validated.
MissingCatalogEntry {
/// Operation this query targets.
operation: OperationKind,
},
/// The catalog row cannot infer what this request needs.
MissingInference {
/// Operation this query targets.
operation: OperationKind,
},
/// An attribute value failed validation.
InvalidAttribute {
/// Operation this query targets.
operation: OperationKind,
/// Name of the rejected attribute.
attribute: &'static str,
/// Bounded explanation of the failure.
reason: &'static str,
},
/// A shape rule rejected the geometry.
Shape(crate::shapes::ShapeError),
/// A declared output disagreed with computed metadata.
MetadataMismatch {
/// Operation this query targets.
operation: OperationKind,
/// Zero-based index of the disagreeing output.
output: usize,
/// Which metadata field disagreed.
field: &'static str,
},
/// Creation payload had the wrong kind.
PayloadKind {
/// Operation this query targets.
operation: OperationKind,
/// Kind name the descriptor required.
expected: &'static str,
},
/// Creation payload dtype differs from the descriptor's.
PayloadDTypeMismatch {
/// Operation this query targets.
operation: OperationKind,
/// Dtype metadata promised.
expected: DTypeDescriptor,
/// Dtype actually present.
actual: DTypeDescriptor,
},
/// Creation payload byte length disagrees with shape math.
PayloadByteLength {
/// Operation this query targets.
operation: OperationKind,
/// Byte length the descriptor computed.
expected: usize,
/// Value actually observed.
actual: usize,
},
/// A creation operation was dispatched without its borrowed bytes.
PayloadMissing {
/// Operation this query targets.
operation: OperationKind,
},
/// A non-creation operation received an execution payload.
UnexpectedPayload {
/// Operation this query targets.
operation: OperationKind,
},
}
impl From<crate::shapes::ShapeError> for DescriptorError {
fn from(error: crate::shapes::ShapeError) -> Self {
Self::Shape(error)
}
}
impl fmt::Display for DescriptorError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Arity {
operation,
expected,
actual,
} => write!(
f,
"{operation}: input arity {actual} is outside {expected:?}"
),
Self::OutputArity {
operation,
expected,
actual,
} => write!(
f,
"{operation}: output arity {actual} is outside {expected:?}"
),
Self::Rank {
operation,
input,
expected,
actual,
} => write!(
f,
"{operation}: input {input} rank {actual} is outside {expected:?}"
),
Self::DeviceMismatch {
operation,
input,
expected,
actual,
} => write!(
f,
"{operation}: input {input} device {actual:?} does not match {expected:?}"
),
Self::MissingCatalogEntry { operation } => write!(
f,
"{operation}: exact operation is absent from the canonical catalog"
),
Self::MissingInference { operation } => write!(
f,
"{operation}: exact output metadata inference is not implemented"
),
Self::InvalidAttribute {
operation,
attribute,
reason,
} => write!(f, "{operation}: invalid {attribute}: {reason}"),
Self::Shape(error) => fmt::Display::fmt(error, f),
Self::MetadataMismatch {
operation,
output,
field,
} => write!(
f,
"{operation}: output {output} {field} disagrees with inferred metadata"
),
Self::PayloadKind {
operation,
expected,
} => write!(f, "{operation}: expected {expected} creation payload"),
Self::PayloadDTypeMismatch {
operation,
expected,
actual,
} => write!(
f,
"{operation}: payload dtype {actual:?} does not match {expected:?}"
),
Self::PayloadByteLength {
operation,
expected,
actual,
} => write!(
f,
"{operation}: payload byte length {actual} does not match {expected}"
),
Self::PayloadMissing { operation } => {
write!(f, "{operation}: creation payload is missing")
}
Self::UnexpectedPayload { operation } => {
write!(f, "{operation}: unexpected execution payload")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for DescriptorError {}
/// Validation implemented by each concrete typed attribute schema.
#[cfg(feature = "std")]
pub(super) fn project_trace_attributes<T: serde::Serialize + ?Sized>(
value: &T,
) -> core::result::Result<
alloc::collections::BTreeMap<alloc::string::String, crate::graph::AttributeValue>,
&'static str,
> {
let value =
serde_json::to_value(value).map_err(|_| "canonical attribute serialization failed")?;
let serde_json::Value::Object(fields) = value else {
return Ok(alloc::collections::BTreeMap::new());
};
let mut attributes = alloc::collections::BTreeMap::new();
for (name, value) in fields {
let converted = match value {
serde_json::Value::Number(number) => number
.as_i64()
.map(crate::graph::AttributeValue::Int)
.or_else(|| {
number
.as_f64()
.map(|value| crate::graph::AttributeValue::Float(value as f32))
}),
serde_json::Value::String(value) => Some(crate::graph::AttributeValue::String(value)),
serde_json::Value::Array(values) if values.iter().all(serde_json::Value::is_number) => {
if values.iter().all(|value| value.as_i64().is_some()) {
Some(crate::graph::AttributeValue::Ints(
values
.iter()
.filter_map(serde_json::Value::as_i64)
.collect(),
))
} else {
Some(crate::graph::AttributeValue::Floats(
values
.iter()
.filter_map(serde_json::Value::as_f64)
.map(|value| value as f32)
.collect(),
))
}
}
serde_json::Value::Bool(value) => {
Some(crate::graph::AttributeValue::Int(i64::from(value)))
}
serde_json::Value::Array(_)
| serde_json::Value::Null
| serde_json::Value::Object(_) => None,
};
if let Some(value) = converted {
attributes.insert(name, value);
}
}
Ok(attributes)
}
/// Per-operation validation over its typed attribute set.
pub trait AttributeContract {
/// Validates this attribute set against its contract, returning structured errors.
fn validate(
&self,
operation: OperationKind,
inputs: &[LogicalTensorMeta],
) -> Result<(), DescriptorError>;
/// Pinned output shape, when declared.
fn declared_shape(&self) -> Option<&[usize]> {
None
}
/// Pinned dtype, when declared.
fn declared_dtype(&self) -> Option<DTypeDescriptor> {
None
}
/// Pinned device, when declared.
fn declared_device(&self) -> Option<DeviceId> {
None
}
/// Axis argument, when this set carries one.
fn axis(&self) -> Option<usize> {
None
}
/// Loss reduction mode, when present.
fn loss_reduction(&self) -> Option<LossReduction> {
None
}
/// Shape-transform description, when applicable.
fn shape_transform(&self) -> Option<ShapeTransform<'_>> {
None
}
/// Declared output count for these inputs, when fixed.
fn expected_output_count(&self, _inputs: &[LogicalTensorMeta]) -> Option<usize> {
None
}
/// Whether a bias input participates, when declared.
fn optional_bias(&self) -> Option<bool> {
None
}
/// Projects serializable scalar and array attributes for graph consumers.
/// The typed attribute struct remains the semantic source of truth.
#[cfg(feature = "std")]
fn trace_attributes(
&self,
) -> core::result::Result<
alloc::collections::BTreeMap<alloc::string::String, crate::graph::AttributeValue>,
&'static str,
>
where
Self: serde::Serialize,
{
project_trace_attributes(self)
}
}