1use std::{collections::BTreeMap, marker::PhantomData};
2
3use rusttorch_core::{Device, Result, Tensor};
4
5use crate::Bytes;
6
7#[derive(Clone, Copy, Debug, Default)]
9pub struct PinDisabled;
10
11#[derive(Clone, Copy, Debug, Default)]
13pub struct PinEnabled<M>(pub(crate) PhantomData<M>);
14
15#[derive(Clone, Copy, Debug, Default)]
17pub struct Auto;
18
19#[derive(Clone, Copy, Debug, Default)]
21pub struct Explicit;
22
23#[derive(Clone, Copy, Debug, Eq, PartialEq)]
25pub enum PinMemoryStatus {
26 Disabled,
28 DisabledNoAccelerator,
30 Enabled(Device),
32}
33
34pub trait PinMemory: Sized {
40 fn pin_memory(self, device: Device) -> Result<Self>;
42}
43
44impl PinMemory for Tensor {
45 fn pin_memory(self, device: Device) -> Result<Self> {
46 self.f_pin_memory(device).map_err(Into::into)
47 }
48}
49
50macro_rules! impl_identity_pin {
51 ($($type:ty),+ $(,)?) => {
52 $(
53 impl PinMemory for $type {
54 fn pin_memory(self, _device: Device) -> Result<Self> {
55 Ok(self)
56 }
57 }
58 )+
59 };
60}
61
62impl_identity_pin!(u8, i8, i16, i32, i64, f32, f64, bool, String, Bytes);
63
64impl<T> PinMemory for Vec<T>
65where
66 T: PinMemory,
67{
68 fn pin_memory(self, device: Device) -> Result<Self> {
69 self.into_iter()
70 .map(|value| value.pin_memory(device))
71 .collect()
72 }
73}
74
75impl<T> PinMemory for Option<T>
76where
77 T: PinMemory,
78{
79 fn pin_memory(self, device: Device) -> Result<Self> {
80 self.map(|value| value.pin_memory(device)).transpose()
81 }
82}
83
84impl<K, V> PinMemory for BTreeMap<K, V>
85where
86 K: Ord,
87 V: PinMemory,
88{
89 fn pin_memory(self, device: Device) -> Result<Self> {
90 self.into_iter()
91 .map(|(key, value)| value.pin_memory(device).map(|value| (key, value)))
92 .collect()
93 }
94}
95
96macro_rules! impl_tuple_pin {
97 ($(($type:ident, $value:ident)),+ $(,)?) => {
98 impl<$($type),+> PinMemory for ($($type,)+)
99 where
100 $($type: PinMemory),+
101 {
102 fn pin_memory(self, device: Device) -> Result<Self> {
103 let ($($value,)+) = self;
104 Ok(($($value.pin_memory(device)?,)+))
105 }
106 }
107 };
108}
109
110impl_tuple_pin!((A, a), (B, b));
111impl_tuple_pin!((A, a), (B, b), (C, c));
112impl_tuple_pin!((A, a), (B, b), (C, c), (D, d));
113impl_tuple_pin!((A, a), (B, b), (C, c), (D, d), (E, e));
114impl_tuple_pin!((A, a), (B, b), (C, c), (D, d), (E, e), (F, f));
115impl_tuple_pin!((A, a), (B, b), (C, c), (D, d), (E, e), (F, f), (G, g));
116impl_tuple_pin!(
117 (A, a),
118 (B, b),
119 (C, c),
120 (D, d),
121 (E, e),
122 (F, f),
123 (G, g),
124 (H, h)
125);