tenferro-gpu 0.3.0

CubeCL-backed CUDA and WebGPU provider backends for tenferro tensors.
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
use std::fmt;

use tenferro_tensor::BoxError;

#[derive(Debug, thiserror::Error)]
enum CudaDriverDiscoveryError {
    #[error("CUDA driver call {function} failed: {source}")]
    DriverCall {
        function: &'static str,
        #[source]
        source: cudarc::driver::result::DriverError,
    },
    #[error("CUDA returned an invalid device count {count}")]
    InvalidDeviceCount { count: i32 },
    #[error("CUDA device ordinal {device:?} is out of range")]
    DeviceOrdinalOutOfRange { device: CudaDeviceId },
}

fn boxed_discovery_error(error: CudaDriverDiscoveryError) -> BoxError {
    Box::new(error)
}

struct CudaDriverApi;

/// Provider-qualified identity of a CUDA device ordinal.
///
/// A device ID is an opaque CUDA provider value. Use [`Self::ordinal`] only
/// when passing the selected ordinal to CUDA APIs. The ordinal is
/// process-visible and may change when `CUDA_VISIBLE_DEVICES` changes.
///
/// # Examples
///
/// ```
/// use tenferro_gpu::cuda::CudaDeviceId;
///
/// let device = CudaDeviceId::from_ordinal(2);
/// assert_eq!(device.ordinal(), 2);
/// ```
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct CudaDeviceId(u32);

impl CudaDeviceId {
    /// Construct a device ID from its CUDA ordinal.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_gpu::cuda::CudaDeviceId;
    ///
    /// const DEVICE: CudaDeviceId = CudaDeviceId::from_ordinal(0);
    /// assert_eq!(DEVICE.ordinal(), 0);
    /// ```
    pub const fn from_ordinal(ordinal: u32) -> Self {
        Self(ordinal)
    }

    /// Return the CUDA ordinal represented by this device ID.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_gpu::cuda::CudaDeviceId;
    ///
    /// let device = CudaDeviceId::from_ordinal(3);
    /// assert_eq!(device.ordinal(), 3);
    /// ```
    pub const fn ordinal(self) -> u32 {
        self.0
    }
}

impl fmt::Debug for CudaDeviceId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_tuple("CudaDeviceId")
            .field(&self.0)
            .finish()
    }
}

/// Immutable metadata describing one CUDA device.
///
/// Equality and debug output are deterministic only while the process-visible
/// CUDA topology remains unchanged; they do not provide stable identity across
/// topology changes.
///
/// # Examples
///
/// ```
/// use tenferro_gpu::cuda::CudaDeviceInfo;
///
/// let _type_name = std::any::type_name::<CudaDeviceInfo>();
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CudaDeviceInfo {
    id: CudaDeviceId,
    name: String,
}

impl CudaDeviceInfo {
    /// Construct device metadata for a CUDA device.
    pub(crate) fn new(id: CudaDeviceId, name: impl Into<String>) -> Self {
        Self {
            id,
            name: name.into(),
        }
    }

    /// Return this device's provider-qualified ID.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_gpu::cuda::CudaDeviceInfo;
    ///
    /// let _id = CudaDeviceInfo::id;
    /// ```
    pub fn id(&self) -> CudaDeviceId {
        self.id
    }

    /// Borrow this device's display name.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_gpu::cuda::CudaDeviceInfo;
    ///
    /// let _name = CudaDeviceInfo::name;
    /// ```
    pub fn name(&self) -> &str {
        &self.name
    }
}

trait DiscoveryDriver {
    fn initialize(&self) -> Result<(), BoxError>;

    fn device_count(&self) -> Result<u32, BoxError>;

    fn device_name(&self, device: CudaDeviceId) -> Result<String, BoxError>;
}

fn discover_with(driver: &impl DiscoveryDriver) -> Result<Vec<CudaDeviceInfo>, CudaDeviceError> {
    driver
        .initialize()
        .map_err(|source| CudaDeviceError::Discovery {
            operation: "initialize_driver",
            source,
        })?;
    let device_count = driver
        .device_count()
        .map_err(|source| CudaDeviceError::Discovery {
            operation: "enumerate_devices",
            source,
        })?;

    let mut devices = Vec::new();
    for ordinal in 0..device_count {
        let id = CudaDeviceId::from_ordinal(ordinal);
        let name = driver
            .device_name(id)
            .map_err(|source| CudaDeviceError::Discovery {
                operation: "get_device_name",
                source,
            })?;
        devices.push(CudaDeviceInfo::new(id, name));
    }
    Ok(devices)
}

impl DiscoveryDriver for CudaDriverApi {
    fn initialize(&self) -> Result<(), BoxError> {
        cudarc::driver::result::init().map_err(|source| {
            boxed_discovery_error(CudaDriverDiscoveryError::DriverCall {
                function: "cuInit",
                source,
            })
        })
    }

    fn device_count(&self) -> Result<u32, BoxError> {
        let count = cudarc::driver::result::device::get_count().map_err(|source| {
            boxed_discovery_error(CudaDriverDiscoveryError::DriverCall {
                function: "cuDeviceGetCount",
                source,
            })
        })?;
        u32::try_from(count).map_err(|_| {
            boxed_discovery_error(CudaDriverDiscoveryError::InvalidDeviceCount { count })
        })
    }

    fn device_name(&self, device: CudaDeviceId) -> Result<String, BoxError> {
        let ordinal = i32::try_from(device.ordinal()).map_err(|_| {
            boxed_discovery_error(CudaDriverDiscoveryError::DeviceOrdinalOutOfRange { device })
        })?;

        let cuda_device = cudarc::driver::result::device::get(ordinal).map_err(|source| {
            boxed_discovery_error(CudaDriverDiscoveryError::DriverCall {
                function: "cuDeviceGet",
                source,
            })
        })?;
        let name = cudarc::driver::result::device::get_name(cuda_device).map_err(|source| {
            boxed_discovery_error(CudaDriverDiscoveryError::DriverCall {
                function: "cuDeviceGetName",
                source,
            })
        })?;
        Ok(name)
    }
}

/// Discover CUDA devices visible to the current process.
///
/// Discovery initializes the CUDA driver and queries device ordinals directly.
/// It does not create a CUDA context, CUDA runtime, CubeCL runtime, backend, or
/// client. Device ordinals are process-visible and may change when
/// `CUDA_VISIBLE_DEVICES` changes. The returned values have deterministic
/// `Eq` and `Debug` behavior only while that process-visible topology remains
/// unchanged.
///
/// # Errors
///
/// Returns [`CudaDeviceError::Discovery`] for driver initialization, device
/// enumeration, or device-name lookup failures.
///
/// # Examples
///
/// ```
/// use tenferro_gpu::{cuda::cuda_devices, cuda::CudaDeviceError, cuda::CudaDeviceInfo};
///
/// let _discover: fn() -> Result<Vec<CudaDeviceInfo>, CudaDeviceError> = cuda_devices;
/// ```
pub fn cuda_devices() -> Result<Vec<CudaDeviceInfo>, CudaDeviceError> {
    discover_with(&CudaDriverApi)
}

pub(crate) fn unavailable_device_error(
    requested: CudaDeviceId,
    discovered: Vec<CudaDeviceInfo>,
) -> CudaDeviceError {
    CudaDeviceError::Unavailable {
        requested,
        discovered: discovered.into_boxed_slice(),
    }
}

/// Structured failures from CUDA device discovery and initialization.
///
/// The error retains only provider-neutral device identities and metadata. It
/// does not expose the concrete CUDA runtime error type behind a source.
///
/// # Examples
///
/// ```
/// use tenferro_gpu::{cuda::CudaDeviceError, cuda::CudaDeviceId};
///
/// let error = CudaDeviceError::Unavailable {
///     requested: CudaDeviceId::from_ordinal(1),
///     discovered: Vec::new().into_boxed_slice(),
/// };
/// assert_eq!(error.requested(), Some(CudaDeviceId::from_ordinal(1)));
/// ```
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CudaDeviceError {
    /// Device discovery failed while performing the named operation.
    #[error("CUDA device discovery failed during {operation}: {source}")]
    Discovery {
        /// The provider-neutral discovery operation being performed.
        operation: &'static str,
        /// The underlying discovery failure.
        #[source]
        source: tenferro_tensor::BoxError,
    },
    /// The requested device was not present in the discovered device list.
    #[error(
        "requested CUDA device {requested:?} is unavailable; discovered devices: {discovered:?}"
    )]
    Unavailable {
        /// The device selected by the caller.
        requested: CudaDeviceId,
        /// Devices found during discovery, in discovery order.
        discovered: Box<[CudaDeviceInfo]>,
    },
    /// Device initialization failed while performing the named operation.
    #[error("CUDA device {device:?} initialization failed during {operation}: {source}")]
    Initialization {
        /// The device whose runtime could not be initialized.
        device: CudaDeviceId,
        /// The provider-neutral initialization operation being performed.
        operation: &'static str,
        /// The underlying initialization failure.
        #[source]
        source: tenferro_tensor::BoxError,
    },
}

impl CudaDeviceError {
    /// Return the operation associated with discovery or initialization.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_gpu::{cuda::CudaDeviceError, cuda::CudaDeviceId};
    ///
    /// let error = CudaDeviceError::Unavailable {
    ///     requested: CudaDeviceId::from_ordinal(1),
    ///     discovered: Vec::new().into_boxed_slice(),
    /// };
    /// assert_eq!(error.operation(), None);
    /// ```
    pub fn operation(&self) -> Option<&'static str> {
        match self {
            Self::Discovery { operation, .. } | Self::Initialization { operation, .. } => {
                Some(operation)
            }
            Self::Unavailable { .. } => None,
        }
    }

    /// Return the requested device for an unavailable-device error.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_gpu::{cuda::CudaDeviceError, cuda::CudaDeviceId};
    ///
    /// let requested = CudaDeviceId::from_ordinal(1);
    /// let error = CudaDeviceError::Unavailable {
    ///     requested,
    ///     discovered: Vec::new().into_boxed_slice(),
    /// };
    /// assert_eq!(error.requested(), Some(requested));
    /// ```
    pub fn requested(&self) -> Option<CudaDeviceId> {
        match self {
            Self::Unavailable { requested, .. } => Some(*requested),
            Self::Discovery { .. } | Self::Initialization { .. } => None,
        }
    }

    /// Borrow the devices discovered for an unavailable-device error.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_gpu::{cuda::CudaDeviceError, cuda::CudaDeviceId};
    ///
    /// let error = CudaDeviceError::Unavailable {
    ///     requested: CudaDeviceId::from_ordinal(1),
    ///     discovered: Vec::new().into_boxed_slice(),
    /// };
    /// assert!(error.discovered().is_some_and(<[_]>::is_empty));
    /// ```
    pub fn discovered(&self) -> Option<&[CudaDeviceInfo]> {
        match self {
            Self::Unavailable { discovered, .. } => Some(discovered),
            Self::Discovery { .. } | Self::Initialization { .. } => None,
        }
    }

    /// Return the device whose initialization failed.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_gpu::{cuda::CudaDeviceError, cuda::CudaDeviceId};
    ///
    /// let device = CudaDeviceId::from_ordinal(1);
    /// let error = CudaDeviceError::Initialization {
    ///     device,
    ///     operation: "create_client",
    ///     source: Box::new(std::io::Error::other("context failed")),
    /// };
    /// assert_eq!(error.device(), Some(device));
    /// ```
    pub fn device(&self) -> Option<CudaDeviceId> {
        match self {
            Self::Initialization { device, .. } => Some(*device),
            Self::Discovery { .. } | Self::Unavailable { .. } => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::collections::hash_map::DefaultHasher;
    use std::error::Error as _;
    use std::hash::{Hash, Hasher};

    use tenferro_tensor::BoxError;

    use super::{
        discover_with, unavailable_device_error, CudaDeviceError, CudaDeviceId, CudaDeviceInfo,
        DiscoveryDriver,
    };

    #[derive(Copy, Clone)]
    enum FakeDriverScenario {
        Success,
        InitializeFailure,
        CountFailure,
        NameFailure(CudaDeviceId),
    }

    struct FakeDriver {
        names: Vec<String>,
        scenario: FakeDriverScenario,
        calls: RefCell<Vec<&'static str>>,
        attempted_ordinals: RefCell<Vec<CudaDeviceId>>,
    }

    impl FakeDriver {
        fn new(names: Vec<String>, scenario: FakeDriverScenario) -> Self {
            Self {
                names,
                scenario,
                calls: RefCell::new(Vec::new()),
                attempted_ordinals: RefCell::new(Vec::new()),
            }
        }

        fn success(names: Vec<String>) -> Self {
            Self::new(names, FakeDriverScenario::Success)
        }

        fn calls(&self) -> Vec<&'static str> {
            self.calls.borrow().clone()
        }

        fn attempted_ordinals(&self) -> Vec<CudaDeviceId> {
            self.attempted_ordinals.borrow().clone()
        }
    }

    impl DiscoveryDriver for FakeDriver {
        fn initialize(&self) -> Result<(), BoxError> {
            self.calls.borrow_mut().push("initialize");
            match self.scenario {
                FakeDriverScenario::InitializeFailure => {
                    Err(Box::new(std::io::Error::other("fake initialize failure")))
                }
                FakeDriverScenario::Success
                | FakeDriverScenario::CountFailure
                | FakeDriverScenario::NameFailure(_) => Ok(()),
            }
        }

        fn device_count(&self) -> Result<u32, BoxError> {
            self.calls.borrow_mut().push("device_count");
            match self.scenario {
                FakeDriverScenario::CountFailure => {
                    Err(Box::new(std::io::Error::other("fake device count failure")))
                }
                FakeDriverScenario::Success
                | FakeDriverScenario::InitializeFailure
                | FakeDriverScenario::NameFailure(_) => Ok(self.names.len() as u32),
            }
        }

        fn device_name(&self, device: CudaDeviceId) -> Result<String, BoxError> {
            self.calls.borrow_mut().push("device_name");
            self.attempted_ordinals.borrow_mut().push(device);
            if matches!(self.scenario, FakeDriverScenario::NameFailure(failed) if failed == device)
            {
                return Err(Box::new(std::io::Error::other("fake device name failure")));
            }
            Ok(self.names[device.ordinal() as usize].clone())
        }
    }

    fn assert_cuda_device_id_traits<T>()
    where
        T: Copy + Clone + Eq + PartialEq + Ord + PartialOrd + Hash,
    {
    }

    #[test]
    fn cuda_device_id_has_value_semantics_and_deterministic_debug() {
        const ID: CudaDeviceId = CudaDeviceId::from_ordinal(7);

        assert_cuda_device_id_traits::<CudaDeviceId>();
        assert_eq!(ID.ordinal(), 7);
        assert!(ID < CudaDeviceId::from_ordinal(8));
        assert_eq!(format!("{ID:?}"), "CudaDeviceId(7)");

        let mut first_hasher = DefaultHasher::new();
        ID.hash(&mut first_hasher);
        let mut second_hasher = DefaultHasher::new();
        CudaDeviceId::from_ordinal(7).hash(&mut second_hasher);
        assert_eq!(first_hasher.finish(), second_hasher.finish());
    }

    #[test]
    fn cuda_device_info_exposes_id_and_name() {
        let id = CudaDeviceId::from_ordinal(2);
        let info = CudaDeviceInfo::new(id, "NVIDIA H100");

        assert_eq!(info.id(), id);
        assert_eq!(info.name(), "NVIDIA H100");
        assert_eq!(info, info.clone());
    }

    #[test]
    fn unavailable_device_selection_preserves_requested_id_and_discovered_records() {
        let requested = CudaDeviceId::from_ordinal(2);
        let discovered = vec![
            CudaDeviceInfo::new(CudaDeviceId::from_ordinal(0), "NVIDIA H100"),
            CudaDeviceInfo::new(CudaDeviceId::from_ordinal(1), "NVIDIA A100"),
        ];

        let error = unavailable_device_error(requested, discovered.clone());

        assert!(matches!(
            error,
            CudaDeviceError::Unavailable {
                requested: actual_requested,
                discovered: actual_discovered,
            } if actual_requested == requested && actual_discovered.as_ref() == discovered
        ));
    }

    #[test]
    fn discovery_of_zero_devices_returns_empty() {
        let driver = FakeDriver::success(Vec::new());

        assert!(discover_with(&driver).unwrap().is_empty());
    }

    #[test]
    fn discovery_preserves_ordinal_order_and_is_deterministic() {
        let driver = FakeDriver::success(vec!["NVIDIA A100".into(), "NVIDIA H100".into()]);
        let expected = vec![
            CudaDeviceInfo::new(CudaDeviceId::from_ordinal(0), "NVIDIA A100"),
            CudaDeviceInfo::new(CudaDeviceId::from_ordinal(1), "NVIDIA H100"),
        ];

        let first = discover_with(&driver).unwrap();
        let second = discover_with(&driver).unwrap();

        assert_eq!(first, expected);
        assert_eq!(first, second);
        assert_eq!(format!("{first:?}"), format!("{second:?}"));
    }

    #[test]
    fn discovery_initialize_failure_returns_provider_neutral_error() {
        let driver = FakeDriver::new(Vec::new(), FakeDriverScenario::InitializeFailure);

        let error = discover_with(&driver).expect_err("initialize failure should be returned");

        assert!(matches!(
            &error,
            CudaDeviceError::Discovery {
                operation: "initialize_driver",
                source,
            } if source.downcast_ref::<std::io::Error>().is_some()
                && source.to_string() == "fake initialize failure"
        ));
        assert_eq!(
            error.source().map(ToString::to_string).as_deref(),
            Some("fake initialize failure")
        );
        assert_eq!(driver.calls(), vec!["initialize"]);
    }

    #[test]
    fn discovery_count_failure_returns_provider_neutral_error() {
        let driver = FakeDriver::new(vec!["NVIDIA A100".into()], FakeDriverScenario::CountFailure);

        let error = discover_with(&driver).expect_err("count failure should be returned");

        assert!(matches!(
            &error,
            CudaDeviceError::Discovery {
                operation: "enumerate_devices",
                source,
            } if source.downcast_ref::<std::io::Error>().is_some()
                && source.to_string() == "fake device count failure"
        ));
        assert_eq!(
            error.source().map(ToString::to_string).as_deref(),
            Some("fake device count failure")
        );
        assert_eq!(driver.calls(), vec!["initialize", "device_count"]);
    }

    #[test]
    fn discovery_name_failure_returns_error_without_partial_devices() {
        let failed_device = CudaDeviceId::from_ordinal(1);
        let driver = FakeDriver::new(
            vec!["NVIDIA A100".into(), "NVIDIA H100".into()],
            FakeDriverScenario::NameFailure(failed_device),
        );

        let error = discover_with(&driver).expect_err("name failure should be returned");

        assert!(matches!(
            &error,
            CudaDeviceError::Discovery {
                operation: "get_device_name",
                source,
            } if source.downcast_ref::<std::io::Error>().is_some()
                && source.to_string() == "fake device name failure"
        ));
        assert_eq!(
            error.source().map(ToString::to_string).as_deref(),
            Some("fake device name failure")
        );
        assert_eq!(
            driver.attempted_ordinals(),
            vec![CudaDeviceId::from_ordinal(0), failed_device]
        );
        assert_eq!(
            driver.calls(),
            vec!["initialize", "device_count", "device_name", "device_name"]
        );
    }

    #[test]
    fn cuda_device_error_discovery_preserves_fields_and_source() {
        let error = CudaDeviceError::Discovery {
            operation: "enumerate_devices",
            source: Box::new(std::io::Error::other("driver query failed")),
        };

        assert!(matches!(
            &error,
            CudaDeviceError::Discovery {
                operation: "enumerate_devices",
                source,
            } if source.to_string() == "driver query failed"
        ));
        assert_eq!(
            error.to_string(),
            "CUDA device discovery failed during enumerate_devices: driver query failed"
        );
        assert_eq!(error.operation(), Some("enumerate_devices"));
        assert_eq!(error.requested(), None);
        assert_eq!(error.discovered(), None);
        assert_eq!(error.device(), None);
        assert_eq!(
            error.source().map(ToString::to_string).as_deref(),
            Some("driver query failed")
        );
    }

    #[test]
    fn cuda_device_error_unavailable_preserves_fields_without_source() {
        let requested = CudaDeviceId::from_ordinal(2);
        let discovered = vec![
            CudaDeviceInfo::new(CudaDeviceId::from_ordinal(0), "NVIDIA H100"),
            CudaDeviceInfo::new(CudaDeviceId::from_ordinal(1), "NVIDIA A100"),
        ]
        .into_boxed_slice();
        let error = CudaDeviceError::Unavailable {
            requested,
            discovered,
        };

        assert!(matches!(
            &error,
            CudaDeviceError::Unavailable {
                requested: actual_requested,
                discovered: actual_discovered,
            } if *actual_requested == requested
                && actual_discovered.as_ref() == [
                    CudaDeviceInfo::new(CudaDeviceId::from_ordinal(0), "NVIDIA H100"),
                    CudaDeviceInfo::new(CudaDeviceId::from_ordinal(1), "NVIDIA A100"),
                ]
        ));
        assert_eq!(
            error.to_string(),
            "requested CUDA device CudaDeviceId(2) is unavailable; discovered devices: [CudaDeviceInfo { id: CudaDeviceId(0), name: \"NVIDIA H100\" }, CudaDeviceInfo { id: CudaDeviceId(1), name: \"NVIDIA A100\" }]"
        );
        assert_eq!(error.operation(), None);
        assert_eq!(error.requested(), Some(requested));
        assert_eq!(
            error.discovered(),
            Some(
                [
                    CudaDeviceInfo::new(CudaDeviceId::from_ordinal(0), "NVIDIA H100"),
                    CudaDeviceInfo::new(CudaDeviceId::from_ordinal(1), "NVIDIA A100"),
                ]
                .as_slice()
            )
        );
        assert_eq!(error.device(), None);
        assert!(error.source().is_none());
    }

    #[test]
    fn cuda_device_error_initialization_preserves_fields_and_source() {
        let device = CudaDeviceId::from_ordinal(1);
        let error = CudaDeviceError::Initialization {
            device,
            operation: "create_client",
            source: Box::new(std::io::Error::other("CUDA context failed")),
        };

        assert!(matches!(
            &error,
            CudaDeviceError::Initialization {
                device: actual_device,
                operation: "create_client",
                source,
            } if *actual_device == device && source.to_string() == "CUDA context failed"
        ));
        assert_eq!(
            error.to_string(),
            "CUDA device CudaDeviceId(1) initialization failed during create_client: CUDA context failed"
        );
        assert_eq!(error.operation(), Some("create_client"));
        assert_eq!(error.requested(), None);
        assert_eq!(error.discovered(), None);
        assert_eq!(error.device(), Some(device));
        assert_eq!(
            error.source().map(ToString::to_string).as_deref(),
            Some("CUDA context failed")
        );
    }
}