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
use crate::driver::CuResult;
/// Error returned by CUDA driver and J2K CUDA kernel helpers.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CudaError {
/// CUDA driver library or device is unavailable.
#[error("CUDA driver is unavailable: {message}")]
Unavailable {
/// Human-readable availability failure.
message: String,
},
/// CUDA Driver API call failed.
#[error("CUDA driver call {operation} failed with CUresult {code}{name}")]
Driver {
/// Driver operation name.
operation: &'static str,
/// Raw CUDA result code.
code: CuResult,
/// CUDA error name, when available.
name: String,
},
/// Host output buffer is too small for a device download.
#[error("CUDA copy output buffer too small: required {required}, have {have}")]
OutputTooSmall {
/// Required byte count.
required: usize,
/// Provided byte count.
have: usize,
},
/// Byte length cannot be represented by the kernel ABI.
#[error("CUDA byte length is too large for kernel launch: {len}")]
LengthTooLarge {
/// Byte length.
len: usize,
},
/// A host-side allocation needed by a CUDA operation could not be reserved.
#[error("CUDA host allocation failed for {bytes} bytes")]
HostAllocationFailed {
/// Requested host allocation size in bytes.
bytes: usize,
},
/// Simultaneously live host allocations would exceed the codec policy.
#[error(
"CUDA host allocation for {what} is too large: requested {requested} bytes, cap {cap} bytes"
)]
HostAllocationTooLarge {
/// Aggregate requested host byte count, saturated on overflow.
requested: usize,
/// Maximum permitted simultaneously live host bytes.
cap: usize,
/// Logical operation requiring the allocation.
what: &'static str,
},
/// Device byte length is not aligned to the requested typed view element.
#[error("CUDA buffer length {bytes} is not a multiple of typed element size {element_size}")]
LengthNotElementAligned {
/// Byte length.
bytes: usize,
/// Requested element size.
element_size: usize,
},
/// Image dimensions overflowed allocation or launch geometry.
#[error("CUDA image allocation size overflow for {width}x{height}x{channels}")]
ImageTooLarge {
/// Image width.
width: u32,
/// Image height.
height: u32,
/// Channel count.
channels: usize,
},
/// Internal runtime state lock was poisoned.
#[error("CUDA runtime state lock is poisoned: {message}")]
StatePoisoned {
/// Poison error message.
message: String,
},
/// A CUDA operation failed and the follow-up completion check also failed.
#[error(
"CUDA operation failed ({primary}); context completion could not be established ({completion})"
)]
CompletionFailed {
/// Error returned by the original CUDA operation.
primary: Box<CudaError>,
/// Error returned while establishing context-wide completion.
completion: Box<CudaError>,
},
/// A CUDA operation failed and releasing its retained resources also failed.
#[error(
"CUDA operation failed ({primary}); retained resource release also failed ({release})"
)]
ResourceReleaseFailed {
/// Error returned by the original CUDA operation.
primary: Box<CudaError>,
/// Error returned while releasing retained resources.
release: Box<CudaError>,
},
/// A J2K CUDA kernel reported a validated runtime failure.
#[error("CUDA kernel {kernel} reported status {code} detail {detail}")]
KernelStatus {
/// Kernel entry point or logical stage name.
kernel: &'static str,
/// Kernel-defined status code.
code: u32,
/// Kernel-defined detail code.
detail: u32,
},
/// A batched J2K CUDA kernel reported a failure for one descriptor.
#[error("CUDA kernel {kernel} reported status {code} detail {detail} for job {job_index}")]
KernelJobStatus {
/// Kernel entry point or logical stage name.
kernel: &'static str,
/// Zero-based descriptor index within this kernel launch.
job_index: usize,
/// Kernel-defined status code.
code: u32,
/// Kernel-defined detail code.
detail: u32,
},
/// Caller supplied arguments that cannot be represented by this runtime API.
#[error("CUDA invalid argument: {message}")]
InvalidArgument {
/// Human-readable validation failure.
message: String,
},
/// Validated host-side planning state contradicted materialized launch data.
#[error("CUDA internal invariant failed: {what}")]
InternalInvariant {
/// Stable description of the failed invariant.
what: &'static str,
},
}
impl CudaError {
/// True when the error means the CUDA driver or device is unavailable.
pub fn is_unavailable(&self) -> bool {
match self {
Self::Unavailable { .. } => true,
Self::CompletionFailed {
primary,
completion,
} => primary.is_unavailable() && completion.is_unavailable(),
_ => false,
}
}
/// Whether an error can mean that previously submitted CUDA work still
/// references caller-owned allocations.
///
/// External-runtime adapters use this conservative classification to
/// quarantine allocations instead of freeing storage after an uncertain
/// completion boundary. Pure validation, capacity, and kernel-status
/// errors return `false`.
#[doc(hidden)]
pub fn completion_is_uncertain(&self) -> bool {
matches!(
self,
Self::Driver { .. }
| Self::StatePoisoned { .. }
| Self::CompletionFailed { .. }
| Self::ResourceReleaseFailed { .. }
)
}
/// Descriptor index reported by a batched kernel status, when available.
#[doc(hidden)]
pub fn kernel_job_index(&self) -> Option<usize> {
match self {
Self::KernelJobStatus { job_index, .. } => Some(*job_index),
Self::CompletionFailed { primary, .. }
| Self::ResourceReleaseFailed {
primary,
release: _,
} => primary.kernel_job_index(),
_ => None,
}
}
/// Whether the retained runtime session must not execute later groups.
///
/// Validated argument, capacity, and kernel-status errors are scoped to
/// the affected submission. Driver, poisoned-state, and uncertain cleanup
/// errors can invalidate ordering or retained resources and therefore
/// terminate a persistent batch call.
#[doc(hidden)]
pub fn session_is_unusable(&self) -> bool {
matches!(
self,
Self::Unavailable { .. }
| Self::Driver { .. }
| Self::StatePoisoned { .. }
| Self::CompletionFailed { .. }
| Self::ResourceReleaseFailed { .. }
)
}
}
pub(crate) fn select_uncertain_completion_error(
primary_error: CudaError,
completion_error: Option<CudaError>,
) -> CudaError {
if let Some(completion_error) = completion_error {
return CudaError::CompletionFailed {
primary: Box::new(primary_error),
completion: Box::new(completion_error),
};
}
if matches!(
&primary_error,
CudaError::Driver { .. }
| CudaError::StatePoisoned { .. }
| CudaError::CompletionFailed { .. }
| CudaError::ResourceReleaseFailed { .. }
) {
return primary_error;
}
CudaError::StatePoisoned {
message: format!(
"CUDA context became poisoned while the preceding operation failed: {primary_error}"
),
}
}
pub(crate) fn select_resource_release_error(
primary_error: CudaError,
release_error: CudaError,
) -> CudaError {
CudaError::ResourceReleaseFailed {
primary: Box::new(primary_error),
release: Box::new(release_error),
}
}
#[cfg(test)]
mod tests {
use super::{select_resource_release_error, CudaError};
fn unavailable(message: &str) -> CudaError {
CudaError::Unavailable {
message: message.to_string(),
}
}
fn driver(operation: &'static str) -> CudaError {
CudaError::Driver {
operation,
code: 1,
name: "CUDA_ERROR_TEST".to_string(),
}
}
#[test]
fn mixed_completion_failure_is_not_fallback_eligible_unavailability() {
let mixed = CudaError::CompletionFailed {
primary: Box::new(unavailable("driver missing")),
completion: Box::new(driver("cuCtxSynchronize")),
};
assert!(!mixed.is_unavailable());
let unavailable_only = CudaError::CompletionFailed {
primary: Box::new(unavailable("driver missing")),
completion: Box::new(unavailable("completion unavailable")),
};
assert!(unavailable_only.is_unavailable());
}
#[test]
fn resource_release_failure_preserves_both_diagnostics_and_blocks_fallback() {
let error = select_resource_release_error(
unavailable("primary unavailable"),
driver("release pool hold"),
);
assert!(!error.is_unavailable());
assert!(matches!(error, CudaError::ResourceReleaseFailed { .. }));
let rendered = error.to_string();
assert!(rendered.contains("primary unavailable"));
assert!(rendered.contains("release pool hold"));
}
#[test]
fn completion_uncertainty_excludes_preflight_and_validated_kernel_status() {
assert!(!CudaError::InvalidArgument {
message: "preflight".to_string(),
}
.completion_is_uncertain());
assert!(!CudaError::KernelStatus {
kernel: "test",
code: 1,
detail: 2,
}
.completion_is_uncertain());
assert!(driver("cuEventSynchronize").completion_is_uncertain());
}
}