tenferro-tensor 0.3.0

Dense runtime tensors, views, backend traits, and backend-independent contracts for tenferro.
Documentation
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
use std::any::Any;
use std::cell::UnsafeCell;
use std::ops::{Deref, DerefMut, Range};

use crate::BackendId;
use crate::{DType, DeviceAccessError, DeviceAccessRequest, PreparedDeviceAccess, TensorScalar};

use super::diagnostics::{
    RequestedIdentity, StorageOperation, StorageOperationContext, StorageOperationError,
};
use super::identity::{RootResourceIdentity, RootResourceIdentityError};
use super::prepared::{AccessError, ProviderReadMapping, ProviderWriteMapping};
use super::span::{ByteRange, RootBoundSpan, RootResourceExtent};

/// Provider family metadata retained by the root allocation boundary.
#[doc(hidden)]
pub type ProviderKind = BackendId;

/// Metadata-only provider capability descriptor for the root boundary.
#[doc(hidden)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ProviderCapabilities {
    host_access: bool,
}

impl ProviderCapabilities {
    #[doc(hidden)]
    pub const fn none() -> Self {
        Self { host_access: false }
    }

    #[doc(hidden)]
    pub const fn host() -> Self {
        Self { host_access: true }
    }

    #[doc(hidden)]
    pub const fn host_access(self) -> bool {
        self.host_access
    }
}

/// The sole unsafe boundary for importing a uniquely owned provider root.
///
/// # Safety
///
/// Implementors must own exactly one provider allocation and its destructor,
/// report stable truthful metadata, and uphold `Send`/`Sync` for the boxed
/// value. The storage core does not recover from a violated provider contract.
/// Mapping hooks must return guards that remain valid for the complete borrow
/// of the imported root, retain the provider allocation for that borrow, and
/// expose initialized valid `TensorScalar` representations with stable exact
/// span length. Typed preparation checks the returned pointer alignment before
/// any typed access.
// INVARIANT: #1555/#1558/#1560 place the sole provider/mapping unsafe boundary
// in this private storage kernel; public tensor graph and AD paths remain safe.
#[doc(hidden)]
pub unsafe trait BackendAllocation: std::fmt::Debug + Send + Sync + 'static {
    fn root_extent(&self) -> RootResourceExtent;
    fn provider_kind(&self) -> ProviderKind;
    fn capabilities(&self) -> ProviderCapabilities;
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;

    ///
    /// # Errors
    ///
    /// Returns [`DeviceAccessError::Unsupported`] when the provider has no
    /// device preparation path, [`DeviceAccessError::InvalidRequest`] for a
    /// mismatched identity or layout request, or
    /// [`DeviceAccessError::ProviderFailure`] for provider setup failure.
    fn prepare_device_access(
        &self,
        _request: DeviceAccessRequest<'_>,
    ) -> Result<Box<dyn PreparedDeviceAccess>, DeviceAccessError> {
        Err(DeviceAccessError::Unsupported {
            backend: "unimplemented",
        })
    }

    /// Map the checked span to initialized bytes with valid `TensorScalar`
    /// representations for the requested dtype. The mapping must keep the
    /// exact span length stable and retain the provider allocation for the
    /// returned borrow; typed preparation checks the returned pointer's
    /// alignment before any typed access.
    ///
    /// # Errors
    ///
    /// Returns [`AccessError::InvalidLayout`] or [`AccessError::DTypeMismatch`]
    /// for invalid mapping metadata, [`AccessError::Unsupported`] when host
    /// mapping is unavailable, or [`AccessError::Provider`] for provider
    /// mapping failure.
    fn map_read(
        &self,
        _span: RootBoundSpan,
        _dtype: DType,
    ) -> Result<ProviderReadMapping<'_>, AccessError> {
        Err(AccessError::Unsupported {
            backend: "unimplemented",
        })
    }

    /// Map the checked span to writable bytes with valid `TensorScalar`
    /// representations for the requested dtype. The mapping must keep the
    /// exact span length stable and retain exclusive provider access for the
    /// returned borrow; typed preparation checks the returned pointer's
    /// alignment before any typed access.
    ///
    /// # Errors
    ///
    /// Returns [`AccessError::InvalidLayout`] or [`AccessError::DTypeMismatch`]
    /// for invalid mapping metadata, [`AccessError::Unsupported`] when writable
    /// mapping is unavailable, or [`AccessError::Provider`] for provider
    /// mapping failure.
    fn map_write(
        &self,
        _span: RootBoundSpan,
        _dtype: DType,
    ) -> Result<ProviderWriteMapping<'_>, AccessError> {
        Err(AccessError::Unsupported {
            backend: "unimplemented",
        })
    }
}

/// One non-`Clone` root-bound span authority.
pub(crate) struct OwnedSpanClaim {
    root: RootResourceIdentity,
    span: RootBoundSpan,
}

/// The sole private owner for one imported root span.
pub(crate) struct OwnedStorage {
    pin: RootResourcePin,
    claim: OwnedSpanClaim,
}

fn validate_device_request(
    identity: RootResourceIdentity,
    request: DeviceAccessRequest<'_>,
) -> Result<(), DeviceAccessError> {
    let key = identity.extent().key();
    if request.allocation_domain() != key.domain() || request.allocation_id() != key.local() {
        return Err(DeviceAccessError::InvalidRequest {
            message: "prepared request does not match the root allocation identity".to_owned(),
        });
    }
    if request.byte_len() > identity.extent().byte_len() {
        return Err(DeviceAccessError::InvalidRequest {
            message: "prepared request exceeds the root allocation extent".to_owned(),
        });
    }
    if request.element_size() == 0 {
        return Err(DeviceAccessError::InvalidRequest {
            message: "prepared request has a zero element size".to_owned(),
        });
    }
    Ok(())
}

/// Read-only capability derived from a shared borrow of an owner.
pub(crate) struct StorageRef<'a> {
    owner: &'a OwnedStorage,
}

/// Exclusive capability derived from an exclusive borrow of an owner.
pub(crate) struct StorageMut<'a> {
    owner: &'a mut OwnedStorage,
}

/// Move-only host allocation used by the P3 owner boundary.
///
/// The mutex is only the provider mapping guard. Rust access authority still
/// comes from `StorageRef`/`StorageMut`; the provider trait takes `&self` so a
/// guard can retain the allocation for the complete prepared borrow.
pub(crate) struct HostAllocation<T> {
    extent: RootResourceExtent,
    data: UnsafeCell<crate::StorageBuffer<T>>,
}

/// Root-bound owner for a provider buffer supplied through the backend storage
/// boundary. The `StorageBuffer` remains physically inside this root; it is
/// never retained by a second tensor field.
pub(crate) struct BackendStorageAllocation<T> {
    extent: RootResourceExtent,
    data: UnsafeCell<crate::StorageBuffer<T>>,
}

/// Lifetime-only pin for one root resource. Host roots stay inline so the
/// common CPU owner path does not allocate a second box merely to erase a
/// scalar-specific host allocation. Backend roots retain their provider box.
pub(crate) enum RootResourcePin {
    HostF32(HostAllocation<f32>),
    HostF64(HostAllocation<f64>),
    HostI32(HostAllocation<i32>),
    HostI64(HostAllocation<i64>),
    HostBool(HostAllocation<bool>),
    HostC32(HostAllocation<num_complex::Complex32>),
    HostC64(HostAllocation<num_complex::Complex64>),
    Backend(Box<dyn BackendAllocation>),
}

struct HostByteReadGuard<'a, T: TensorScalar> {
    data: &'a [T],
    range: Range<usize>,
}

struct HostByteWriteGuard<'a, T: TensorScalar> {
    data: &'a mut [T],
    range: Range<usize>,
}

// SAFETY: the allocation is accessed through the group-owned shared/exclusive
// capabilities. `map_write` is called only for an exclusive group child; the
// provider trait's `&self` receiver is not itself an access capability.
unsafe impl<T: TensorScalar> Send for HostAllocation<T> {}
unsafe impl<T: TensorScalar> Sync for HostAllocation<T> {}

// SAFETY: the provider buffer is `Send + Sync` through `BackendStorage`; all
// access to the `UnsafeCell` is bounded by the move-only root borrow supplied
// by `StorageRef`/`StorageMut`.
unsafe impl<T: Send + Sync + 'static> Send for BackendStorageAllocation<T> {}
unsafe impl<T: Send + Sync + 'static> Sync for BackendStorageAllocation<T> {}

fn host_bytes<T: TensorScalar>(data: &[T], range: Range<usize>) -> &[u8] {
    let byte_len = data
        .len()
        .checked_mul(std::mem::size_of::<T>())
        .unwrap_or(0);
    debug_assert!(range.end <= byte_len);
    // SAFETY: `TensorScalar` is initialized, plain-data storage with a stable
    // size/alignment. The checked range is within the Vec allocation.
    unsafe {
        std::slice::from_raw_parts(
            (data.as_ptr() as *const u8).add(range.start),
            range.end - range.start,
        )
    }
}

fn host_bytes_mut<T: TensorScalar>(data: &mut [T], range: Range<usize>) -> &mut [u8] {
    let byte_len = data
        .len()
        .checked_mul(std::mem::size_of::<T>())
        .unwrap_or(0);
    debug_assert!(range.end <= byte_len);
    // SAFETY: `TensorScalar` is initialized, plain-data storage with a stable
    // size/alignment. The checked range is within the Vec allocation and the
    // mutex guard holds exclusive provider access for the borrow.
    unsafe {
        std::slice::from_raw_parts_mut(
            (data.as_mut_ptr() as *mut u8).add(range.start),
            range.end - range.start,
        )
    }
}

impl<T: TensorScalar> Deref for HostByteReadGuard<'_, T> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        host_bytes(self.data, self.range.clone())
    }
}

impl<T: TensorScalar> AsRef<[u8]> for HostByteReadGuard<'_, T> {
    fn as_ref(&self) -> &[u8] {
        self
    }
}

impl<T: TensorScalar> Deref for HostByteWriteGuard<'_, T> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        host_bytes(self.data, self.range.clone())
    }
}

impl<T: TensorScalar> DerefMut for HostByteWriteGuard<'_, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        host_bytes_mut(self.data, self.range.clone())
    }
}

impl<T: TensorScalar> AsRef<[u8]> for HostByteWriteGuard<'_, T> {
    fn as_ref(&self) -> &[u8] {
        self
    }
}

impl<T: TensorScalar> AsMut<[u8]> for HostByteWriteGuard<'_, T> {
    fn as_mut(&mut self) -> &mut [u8] {
        self
    }
}

impl<T: TensorScalar> std::fmt::Debug for HostAllocation<T> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("HostAllocation")
            .field("extent", &self.extent)
            .field(
                "element_count",
                // SAFETY: debug inspection is read-only and the allocation's
                // length is immutable after import.
                unsafe { &(*self.data.get()).len() },
            )
            .finish()
    }
}

unsafe impl<T: TensorScalar> BackendAllocation for HostAllocation<T> {
    fn root_extent(&self) -> RootResourceExtent {
        self.extent
    }

    fn provider_kind(&self) -> ProviderKind {
        ProviderKind::Cpu
    }

    fn capabilities(&self) -> ProviderCapabilities {
        ProviderCapabilities::host()
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn map_read(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<ProviderReadMapping<'_>, AccessError> {
        if !supported_representation(T::dtype(), dtype) {
            return Err(AccessError::DTypeMismatch {
                expected: T::dtype(),
                actual: dtype,
            });
        }
        // SAFETY: shared provider mappings are retained only under a shared
        // group borrow; the vector is never resized after import.
        let data = unsafe { &*self.data.get() };
        let crate::StorageBuffer::Host(data) = data else {
            return Err(AccessError::Unsupported { backend: "host" });
        };
        let range = host_byte_range(
            self.extent,
            span,
            data.len(),
            std::mem::size_of::<T>(),
            dtype_size(dtype),
        )?;
        Ok(ProviderReadMapping::from_guard(HostByteReadGuard {
            data,
            range,
        }))
    }

    fn map_write(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<ProviderWriteMapping<'_>, AccessError> {
        if !supported_representation(T::dtype(), dtype) {
            return Err(AccessError::DTypeMismatch {
                expected: T::dtype(),
                actual: dtype,
            });
        }
        // SAFETY: `GroupWriteView` provides the exclusive capability before
        // calling this provider hook; the vector is never resized after import.
        let data = unsafe { &mut *self.data.get() };
        let crate::StorageBuffer::Host(data) = data else {
            return Err(AccessError::Unsupported { backend: "host" });
        };
        let range = host_byte_range(
            self.extent,
            span,
            data.len(),
            std::mem::size_of::<T>(),
            dtype_size(dtype),
        )?;
        Ok(ProviderWriteMapping::from_guard(HostByteWriteGuard {
            data,
            range,
        }))
    }
}

impl<T: Send + Sync + 'static> std::fmt::Debug for BackendStorageAllocation<T> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("BackendStorageAllocation")
            .field("extent", &self.extent)
            .field(
                "backend_family",
                // SAFETY: the provider buffer is immutable through this debug
                // borrow and its length cannot change through `&self`.
                &unsafe {
                    match &*self.data.get() {
                        crate::StorageBuffer::Backend(buffer) => buffer.backend_family(),
                        crate::StorageBuffer::Host(_) => "host",
                    }
                },
            )
            .finish_non_exhaustive()
    }
}

unsafe impl<T: Send + Sync + 'static> BackendAllocation for BackendStorageAllocation<T> {
    fn root_extent(&self) -> RootResourceExtent {
        self.extent
    }

    fn provider_kind(&self) -> ProviderKind {
        // The provider family is diagnostic metadata. Concrete providers keep
        // their stronger runtime/device checks at their own ingress boundary.
        // INVARIANT: the family string is a compile-time backend identifier.
        let family = unsafe {
            match &*self.data.get() {
                crate::StorageBuffer::Backend(buffer) => buffer.backend_family(),
                crate::StorageBuffer::Host(_) => "host",
            }
        };
        match family {
            "cubecl" => ProviderKind::Cuda,
            "cubecl-webgpu" => ProviderKind::WebGpu,
            "host" => ProviderKind::Cpu,
            other => ProviderKind::Other(other),
        }
    }

    fn capabilities(&self) -> ProviderCapabilities {
        ProviderCapabilities::none()
    }

    fn prepare_device_access(
        &self,
        request: DeviceAccessRequest<'_>,
    ) -> Result<Box<dyn PreparedDeviceAccess>, DeviceAccessError> {
        let data = unsafe { &*self.data.get() };
        let crate::StorageBuffer::Backend(buffer) = data else {
            return Err(DeviceAccessError::Unsupported { backend: "host" });
        };
        buffer.prepare_device_access(request)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

impl<T: TensorScalar> HostAllocation<T> {
    fn host_slice_as<U: TensorScalar>(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<&[U], AccessError> {
        if dtype != U::dtype() || !supported_representation(T::dtype(), dtype) {
            return Err(AccessError::DTypeMismatch {
                expected: T::dtype(),
                actual: dtype,
            });
        }
        let data = unsafe { &*self.data.get() };
        let crate::StorageBuffer::Host(data) = data else {
            return Err(AccessError::Unsupported { backend: "host" });
        };
        let range = host_byte_range(
            self.extent,
            span,
            data.len(),
            size_of::<T>(),
            size_of::<U>(),
        )?;
        let bytes = host_bytes(data, range);
        if bytes.as_ptr().align_offset(align_of::<U>()) != 0 {
            return Err(AccessError::Provider {
                message: format!("host reinterpretation is not aligned for {:?}", dtype),
            });
        }
        let count = bytes.len() / size_of::<U>();
        // SAFETY: the sealed representation pair has equal size/alignment,
        // the byte range is element-aligned, and the host root borrow outlives
        // the returned slice.
        Ok(unsafe { std::slice::from_raw_parts(bytes.as_ptr().cast::<U>(), count) })
    }

    #[allow(clippy::mut_from_ref)]
    fn host_slice_as_mut<U: TensorScalar>(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<&mut [U], AccessError> {
        if dtype != U::dtype() || !supported_representation(T::dtype(), dtype) {
            return Err(AccessError::DTypeMismatch {
                expected: T::dtype(),
                actual: dtype,
            });
        }
        let data = unsafe { &mut *self.data.get() };
        let crate::StorageBuffer::Host(data) = data else {
            return Err(AccessError::Unsupported { backend: "host" });
        };
        let range = host_byte_range(
            self.extent,
            span,
            data.len(),
            size_of::<T>(),
            size_of::<U>(),
        )?;
        let bytes = host_bytes_mut(data, range);
        if bytes.as_ptr().align_offset(align_of::<U>()) != 0 {
            return Err(AccessError::Provider {
                message: format!("host reinterpretation is not aligned for {:?}", dtype),
            });
        }
        let count = bytes.len() / size_of::<U>();
        // SAFETY: the sealed representation pair has equal size/alignment,
        // the byte range is element-aligned, and the caller holds the unique
        // group mutable capability.
        Ok(unsafe { std::slice::from_raw_parts_mut(bytes.as_mut_ptr().cast::<U>(), count) })
    }
}

fn supported_representation(source: DType, target: DType) -> bool {
    source == target
        || matches!(
            (source, target),
            (DType::C32, DType::F32)
                | (DType::F32, DType::C32)
                | (DType::C64, DType::F64)
                | (DType::F64, DType::C64)
        )
}

fn dtype_size(dtype: DType) -> usize {
    match dtype {
        DType::F32 | DType::I32 => size_of::<f32>(),
        DType::F64 | DType::I64 => size_of::<f64>(),
        DType::Bool => size_of::<bool>(),
        DType::C32 => size_of::<num_complex::Complex32>(),
        DType::C64 => size_of::<num_complex::Complex64>(),
    }
}

pub(crate) trait HostRoot: TensorScalar {
    fn into_pin(extent: RootResourceExtent, data: Vec<Self>) -> RootResourcePin;
}

fn cast_host_vec<T: TensorScalar, U: TensorScalar>(data: Vec<T>) -> Vec<U> {
    assert_eq!(std::mem::size_of::<T>(), std::mem::size_of::<U>());
    assert_eq!(std::mem::align_of::<T>(), std::mem::align_of::<U>());
    let mut data = std::mem::ManuallyDrop::new(data);
    // SAFETY: TensorScalar is sealed to the seven scalar types below. The
    // matching dtype branch preserves size, alignment, and representation.
    unsafe { Vec::from_raw_parts(data.as_mut_ptr().cast::<U>(), data.len(), data.capacity()) }
}

impl<T: TensorScalar> HostRoot for T {
    fn into_pin(extent: RootResourceExtent, data: Vec<Self>) -> RootResourcePin {
        match T::dtype() {
            DType::F32 => RootResourcePin::HostF32(HostAllocation {
                extent,
                data: UnsafeCell::new(crate::StorageBuffer::Host(cast_host_vec(data))),
            }),
            DType::F64 => RootResourcePin::HostF64(HostAllocation {
                extent,
                data: UnsafeCell::new(crate::StorageBuffer::Host(cast_host_vec(data))),
            }),
            DType::I32 => RootResourcePin::HostI32(HostAllocation {
                extent,
                data: UnsafeCell::new(crate::StorageBuffer::Host(cast_host_vec(data))),
            }),
            DType::I64 => RootResourcePin::HostI64(HostAllocation {
                extent,
                data: UnsafeCell::new(crate::StorageBuffer::Host(cast_host_vec(data))),
            }),
            DType::Bool => RootResourcePin::HostBool(HostAllocation {
                extent,
                data: UnsafeCell::new(crate::StorageBuffer::Host(cast_host_vec(data))),
            }),
            DType::C32 => RootResourcePin::HostC32(HostAllocation {
                extent,
                data: UnsafeCell::new(crate::StorageBuffer::Host(cast_host_vec(data))),
            }),
            DType::C64 => RootResourcePin::HostC64(HostAllocation {
                extent,
                data: UnsafeCell::new(crate::StorageBuffer::Host(cast_host_vec(data))),
            }),
        }
    }
}

impl RootResourcePin {
    fn prepare_device_access(
        &self,
        request: DeviceAccessRequest<'_>,
    ) -> Result<Box<dyn PreparedDeviceAccess>, DeviceAccessError> {
        match self {
            Self::Backend(root) => root.prepare_device_access(request),
            _ => Err(DeviceAccessError::Unsupported { backend: "host" }),
        }
    }

    fn host_buffer<T: 'static>(&self) -> Option<&crate::StorageBuffer<T>> {
        let allocation = self.as_any().downcast_ref::<HostAllocation<T>>()?;
        // SAFETY: the returned reference is bounded by the shared root borrow;
        // host buffers never resize after import.
        Some(unsafe { &*allocation.data.get() })
    }

    pub(crate) fn backend_allocation(&self) -> Option<&dyn BackendAllocation> {
        match self {
            Self::Backend(root) => Some(root.as_ref()),
            _ => None,
        }
    }

    fn backend_buffer<T: 'static>(&self) -> Option<&crate::StorageBuffer<T>> {
        let allocation = self
            .as_any()
            .downcast_ref::<BackendStorageAllocation<T>>()?;
        // SAFETY: the returned reference is bounded by the shared root borrow;
        // provider buffers are not resized after root import.
        Some(unsafe { &*allocation.data.get() })
    }

    fn backend_buffer_mut<T: 'static>(&mut self) -> Option<&mut crate::StorageBuffer<T>> {
        let allocation = self
            .as_any_mut()
            .downcast_mut::<BackendStorageAllocation<T>>()?;
        // SAFETY: the caller holds the unique root borrow and provider buffers
        // are not resized after root import.
        Some(unsafe { &mut *allocation.data.get() })
    }

    fn root_extent(&self) -> RootResourceExtent {
        match self {
            Self::HostF32(root) => root.root_extent(),
            Self::HostF64(root) => root.root_extent(),
            Self::HostI32(root) => root.root_extent(),
            Self::HostI64(root) => root.root_extent(),
            Self::HostBool(root) => root.root_extent(),
            Self::HostC32(root) => root.root_extent(),
            Self::HostC64(root) => root.root_extent(),
            Self::Backend(root) => root.root_extent(),
        }
    }

    fn provider_kind(&self) -> ProviderKind {
        match self {
            Self::HostF32(root) => root.provider_kind(),
            Self::HostF64(root) => root.provider_kind(),
            Self::HostI32(root) => root.provider_kind(),
            Self::HostI64(root) => root.provider_kind(),
            Self::HostBool(root) => root.provider_kind(),
            Self::HostC32(root) => root.provider_kind(),
            Self::HostC64(root) => root.provider_kind(),
            Self::Backend(root) => root.provider_kind(),
        }
    }

    fn as_any(&self) -> &dyn Any {
        match self {
            Self::HostF32(root) => root,
            Self::HostF64(root) => root,
            Self::HostI32(root) => root,
            Self::HostI64(root) => root,
            Self::HostBool(root) => root,
            Self::HostC32(root) => root,
            Self::HostC64(root) => root,
            Self::Backend(root) => root.as_any(),
        }
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        match self {
            Self::HostF32(root) => root,
            Self::HostF64(root) => root,
            Self::HostI32(root) => root,
            Self::HostI64(root) => root,
            Self::HostBool(root) => root,
            Self::HostC32(root) => root,
            Self::HostC64(root) => root,
            Self::Backend(root) => root.as_any_mut(),
        }
    }

    fn map_read(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<ProviderReadMapping<'_>, AccessError> {
        match self {
            Self::HostF32(root) => root.map_read(span, dtype),
            Self::HostF64(root) => root.map_read(span, dtype),
            Self::HostI32(root) => root.map_read(span, dtype),
            Self::HostI64(root) => root.map_read(span, dtype),
            Self::HostBool(root) => root.map_read(span, dtype),
            Self::HostC32(root) => root.map_read(span, dtype),
            Self::HostC64(root) => root.map_read(span, dtype),
            Self::Backend(root) => root.map_read(span, dtype),
        }
    }

    fn map_write(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<ProviderWriteMapping<'_>, AccessError> {
        match self {
            Self::HostF32(root) => root.map_write(span, dtype),
            Self::HostF64(root) => root.map_write(span, dtype),
            Self::HostI32(root) => root.map_write(span, dtype),
            Self::HostI64(root) => root.map_write(span, dtype),
            Self::HostBool(root) => root.map_write(span, dtype),
            Self::HostC32(root) => root.map_write(span, dtype),
            Self::HostC64(root) => root.map_write(span, dtype),
            Self::Backend(root) => root.map_write(span, dtype),
        }
    }

    fn host_slice<T: TensorScalar>(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<&[T], AccessError> {
        match self {
            Self::HostF32(root) => root.host_slice_as(span, dtype),
            Self::HostF64(root) => root.host_slice_as(span, dtype),
            Self::HostI32(root) => root.host_slice_as(span, dtype),
            Self::HostI64(root) => root.host_slice_as(span, dtype),
            Self::HostBool(root) => root.host_slice_as(span, dtype),
            Self::HostC32(root) => root.host_slice_as(span, dtype),
            Self::HostC64(root) => root.host_slice_as(span, dtype),
            Self::Backend(_) => Err(AccessError::Unsupported { backend: "backend" }),
        }
    }

    fn host_slice_mut<T: TensorScalar>(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<&mut [T], AccessError> {
        match self {
            Self::HostF32(root) => root.host_slice_as_mut(span, dtype),
            Self::HostF64(root) => root.host_slice_as_mut(span, dtype),
            Self::HostI32(root) => root.host_slice_as_mut(span, dtype),
            Self::HostI64(root) => root.host_slice_as_mut(span, dtype),
            Self::HostBool(root) => root.host_slice_as_mut(span, dtype),
            Self::HostC32(root) => root.host_slice_as_mut(span, dtype),
            Self::HostC64(root) => root.host_slice_as_mut(span, dtype),
            Self::Backend(_) => Err(AccessError::Unsupported { backend: "backend" }),
        }
    }
}

fn host_byte_range(
    extent: RootResourceExtent,
    span: RootBoundSpan,
    element_count: usize,
    allocation_element_size: usize,
    requested_element_size: usize,
) -> Result<Range<usize>, AccessError> {
    let start = span
        .byte_offset()
        .checked_sub(extent.byte_offset())
        .ok_or_else(|| AccessError::Provider {
            message: "host mapping span precedes allocation".to_string(),
        })?;
    let end = start
        .checked_add(span.byte_len())
        .ok_or_else(|| AccessError::Provider {
            message: "host mapping span overflows".to_string(),
        })?;
    let total = element_count
        .checked_mul(allocation_element_size)
        .ok_or_else(|| AccessError::Provider {
            message: "host allocation byte length overflows".to_string(),
        })?;
    if end > total
        || !start.is_multiple_of(requested_element_size)
        || !span.byte_len().is_multiple_of(requested_element_size)
    {
        return Err(AccessError::Provider {
            message: "host mapping span is not an element-aligned subrange".to_string(),
        });
    }
    Ok(start..end)
}

#[derive(Debug, thiserror::Error)]
pub(crate) enum RootImportError {
    #[error("root-resource identity validation failed: {0}")]
    Identity(#[source] RootResourceIdentityError),
    #[error("backend `{backend}` did not provide {field} allocation identity")]
    MissingAllocationIdentity {
        backend: &'static str,
        field: &'static str,
    },
    #[error("backend buffer byte length overflows for element size {element_size}")]
    ByteLengthOverflow { element_size: usize },
}

/// Import one uniquely owned provider root and create its single root claim.
pub(crate) fn import_unique_root(
    allocation: Box<dyn BackendAllocation>,
) -> Result<OwnedStorage, Box<StorageOperationError<RootImportError>>> {
    let extent = allocation.root_extent();
    let requested = RequestedIdentity::Keyed {
        key: extent.key(),
        range: ByteRange::new(extent.byte_offset(), extent.byte_len()),
    };
    let context =
        StorageOperationContext::unresolved(StorageOperation::ImportUniqueRoot, requested);
    let identity = RootResourceIdentity::try_new(extent).map_err(|source| {
        Box::new(StorageOperationError::new(
            context,
            RootImportError::Identity(source),
        ))
    })?;
    let span = identity.root_span();
    Ok(OwnedStorage {
        pin: RootResourcePin::Backend(allocation),
        claim: OwnedSpanClaim {
            root: identity,
            span,
        },
    })
}

/// Import one backend-owned buffer as the sole root owner for a typed tensor.
pub(crate) fn import_backend_buffer<T: Send + Sync + 'static>(
    buffer: crate::StorageBuffer<T>,
) -> Result<OwnedStorage, Box<StorageOperationError<RootImportError>>> {
    let crate::StorageBuffer::Backend(provider) = &buffer else {
        return Err(Box::new(StorageOperationError::new(
            StorageOperationContext::unresolved(
                StorageOperation::ImportUniqueRoot,
                RequestedIdentity::Raw(ByteRange::new(0, 0)),
            ),
            RootImportError::MissingAllocationIdentity {
                backend: "host",
                field: "backend buffer",
            },
        )));
    };
    let element_size = std::mem::size_of::<T>();
    let byte_len = provider.len().checked_mul(element_size).ok_or_else(|| {
        Box::new(StorageOperationError::new(
            StorageOperationContext::unresolved(
                StorageOperation::ImportUniqueRoot,
                RequestedIdentity::Raw(ByteRange::new(0, usize::MAX)),
            ),
            RootImportError::ByteLengthOverflow { element_size },
        ))
    })?;
    let domain = provider.allocation_domain().ok_or_else(|| {
        Box::new(StorageOperationError::new(
            StorageOperationContext::unresolved(
                StorageOperation::ImportUniqueRoot,
                RequestedIdentity::Raw(ByteRange::new(0, byte_len)),
            ),
            RootImportError::MissingAllocationIdentity {
                backend: provider.backend_family(),
                field: "allocation domain",
            },
        ))
    })?;
    let allocation_id = provider.allocation_id().ok_or_else(|| {
        Box::new(StorageOperationError::new(
            StorageOperationContext::unresolved(
                StorageOperation::ImportUniqueRoot,
                RequestedIdentity::Raw(ByteRange::new(0, byte_len)),
            ),
            RootImportError::MissingAllocationIdentity {
                backend: provider.backend_family(),
                field: "allocation id",
            },
        ))
    })?;
    let key = super::identity::AllocationKey::new(domain, allocation_id);
    let extent = RootResourceExtent::try_new(key, 0, byte_len, std::mem::align_of::<T>()).map_err(
        |source| {
            Box::new(StorageOperationError::new(
                StorageOperationContext::unresolved(
                    StorageOperation::ImportUniqueRoot,
                    RequestedIdentity::Keyed {
                        key,
                        range: ByteRange::new(0, byte_len),
                    },
                ),
                RootImportError::Identity(RootResourceIdentityError::Extent(source)),
            ))
        },
    )?;
    let allocation = Box::new(BackendStorageAllocation {
        extent,
        data: UnsafeCell::new(buffer),
    });
    import_unique_root(allocation)
}

/// Import one host vector as the unique root for a typed tensor owner.
pub(crate) fn import_host_vec<T: TensorScalar>(
    data: Vec<T>,
) -> Result<OwnedStorage, Box<StorageOperationError<RootImportError>>> {
    static NEXT_HOST_ALLOCATION: std::sync::atomic::AtomicU64 =
        std::sync::atomic::AtomicU64::new(1);
    let element_size = std::mem::size_of::<T>();
    let byte_len = data.len().checked_mul(element_size).ok_or_else(|| {
        Box::new(StorageOperationError::new(
            StorageOperationContext::unresolved(
                StorageOperation::ImportUniqueRoot,
                RequestedIdentity::Raw(ByteRange::new(0, usize::MAX)),
            ),
            RootImportError::Identity(RootResourceIdentityError::Extent(
                super::span::SpanValidationError::RangeOverflow {
                    byte_offset: 0,
                    byte_len: usize::MAX,
                },
            )),
        ))
    })?;
    let key = super::identity::AllocationKey::new(
        crate::AllocationDomainId::fresh(),
        crate::AllocationId::from_backend_id(
            NEXT_HOST_ALLOCATION.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
        ),
    );
    let extent = RootResourceExtent::try_new(key, 0, byte_len, std::mem::align_of::<T>()).map_err(
        |source| {
            Box::new(StorageOperationError::new(
                StorageOperationContext::unresolved(
                    StorageOperation::ImportUniqueRoot,
                    RequestedIdentity::Keyed {
                        key,
                        range: ByteRange::new(0, byte_len),
                    },
                ),
                RootImportError::Identity(RootResourceIdentityError::Extent(source)),
            ))
        },
    )?;
    let identity = RootResourceIdentity::try_new(extent).map_err(|source| {
        Box::new(StorageOperationError::new(
            StorageOperationContext::unresolved(
                StorageOperation::ImportUniqueRoot,
                RequestedIdentity::Keyed {
                    key,
                    range: ByteRange::new(0, byte_len),
                },
            ),
            RootImportError::Identity(source),
        ))
    })?;
    let span = identity.root_span();
    Ok(OwnedStorage {
        pin: T::into_pin(extent, data),
        claim: OwnedSpanClaim {
            root: identity,
            span,
        },
    })
}

impl OwnedStorage {
    pub(crate) const fn as_ref(&self) -> StorageRef<'_> {
        StorageRef { owner: self }
    }

    pub(crate) fn as_mut(&mut self) -> StorageMut<'_> {
        StorageMut { owner: self }
    }

    pub(crate) fn into_root_pin(self) -> RootResourcePin {
        self.pin
    }

    pub(crate) const fn root_identity(&self) -> RootResourceIdentity {
        self.claim.root
    }

    pub(crate) fn provider_kind(&self) -> ProviderKind {
        self.pin.provider_kind()
    }

    pub(crate) const fn root_span(&self) -> RootBoundSpan {
        self.claim.span
    }

    pub(crate) fn host_buffer<T: 'static>(&self) -> Option<&crate::StorageBuffer<T>> {
        self.pin.host_buffer::<T>()
    }

    pub(crate) fn backend_buffer<T: 'static>(&self) -> Option<&crate::StorageBuffer<T>> {
        self.pin.backend_buffer::<T>()
    }

    pub(crate) fn backend_allocation(&self) -> Option<&dyn BackendAllocation> {
        self.pin.backend_allocation()
    }

    pub(crate) fn backend_buffer_mut<T: 'static>(
        &mut self,
    ) -> Option<&mut crate::StorageBuffer<T>> {
        self.pin.backend_buffer_mut::<T>()
    }

    pub(crate) fn into_host_vec<T: TensorScalar>(mut self) -> Result<Vec<T>, AccessError> {
        let allocation = self
            .pin
            .as_any_mut()
            .downcast_mut::<HostAllocation<T>>()
            .ok_or(AccessError::Unsupported {
                backend: "non-host",
            })?;
        // SAFETY: the move-only root pin proves there are no other owners, so
        // taking the vector cannot race with a provider mapping.
        let crate::StorageBuffer::Host(data) = (unsafe { &mut *allocation.data.get() }) else {
            return Err(AccessError::Unsupported {
                backend: "non-host",
            });
        };
        Ok(std::mem::take(data))
    }
}

impl<'a> StorageRef<'a> {
    pub(crate) const fn root_identity(&self) -> RootResourceIdentity {
        self.owner.claim.root
    }

    pub(crate) const fn span(&self) -> RootBoundSpan {
        self.owner.claim.span
    }

    pub(crate) fn provider_kind(&self) -> ProviderKind {
        self.owner.pin.provider_kind()
    }

    pub(crate) fn prepare_device_access(
        &self,
        request: DeviceAccessRequest<'_>,
    ) -> Result<Box<dyn PreparedDeviceAccess>, DeviceAccessError> {
        validate_device_request(self.owner.claim.root, request)?;
        self.owner.pin.prepare_device_access(request)
    }

    pub(super) fn map_read(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<ProviderReadMapping<'a>, AccessError> {
        let mapping = self.owner.pin.map_read(span, dtype)?;
        if mapping.bytes().len() != span.byte_len() {
            return Err(AccessError::LengthMismatch {
                expected: span.byte_len(),
                actual: mapping.bytes().len(),
            });
        }
        // SAFETY: the shared owner borrow lasts for `'a`; the mapping cannot
        // outlive the allocation it borrows, and no mutable access is exposed.
        Ok(unsafe {
            std::mem::transmute::<ProviderReadMapping<'_>, ProviderReadMapping<'a>>(mapping)
        })
    }

    pub(super) fn host_slice<T: TensorScalar>(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<&'a [T], AccessError> {
        let slice = self.owner.pin.host_slice::<T>(span, dtype)?;
        // SAFETY: the root owner is borrowed for `'a`; the host allocation is
        // never resized after import and the representation helper returned a
        // slice bounded by that root.
        Ok(unsafe { std::mem::transmute::<&[T], &'a [T]>(slice) })
    }
}

impl<'a> StorageMut<'a> {
    pub(crate) const fn root_identity(&self) -> RootResourceIdentity {
        self.owner.claim.root
    }

    pub(crate) const fn span(&self) -> RootBoundSpan {
        self.owner.claim.span
    }

    pub(super) fn map_write(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<ProviderWriteMapping<'a>, AccessError> {
        let mapping = self.owner.pin.map_write(span, dtype)?;
        if mapping.len() != span.byte_len() {
            let actual = mapping.len();
            return Err(AccessError::LengthMismatch {
                expected: span.byte_len(),
                actual,
            });
        }
        // SAFETY: the checked write owns the exclusive `'a` borrow represented
        // by this `StorageMut`; callers do not use that reference while the
        // returned mapping is alive.
        Ok(unsafe {
            std::mem::transmute::<ProviderWriteMapping<'_>, ProviderWriteMapping<'a>>(mapping)
        })
    }

    pub(crate) fn prepare_device_access(
        &self,
        request: DeviceAccessRequest<'_>,
    ) -> Result<Box<dyn PreparedDeviceAccess>, DeviceAccessError> {
        validate_device_request(self.owner.claim.root, request)?;
        self.owner.pin.prepare_device_access(request)
    }

    pub(super) fn backend_buffer_mut<T: 'static>(
        &mut self,
    ) -> Option<&'a mut crate::StorageBuffer<T>> {
        let buffer = self.owner.pin.backend_buffer_mut::<T>()?;
        // SAFETY: `StorageMut` represents the exclusive root borrow for `'a`.
        Some(unsafe {
            std::mem::transmute::<&mut crate::StorageBuffer<T>, &'a mut crate::StorageBuffer<T>>(
                buffer,
            )
        })
    }

    pub(super) fn host_slice_mut<T: TensorScalar>(
        &self,
        span: RootBoundSpan,
        dtype: DType,
    ) -> Result<&'a mut [T], AccessError> {
        let slice = self.owner.pin.host_slice_mut::<T>(span, dtype)?;
        // SAFETY: the caller holds the group's exclusive borrow and the
        // representation helper bounded the slice by the imported root.
        Ok(unsafe { std::mem::transmute::<&mut [T], &'a mut [T]>(slice) })
    }
}