use std::{collections::BTreeMap, marker::PhantomData};
use rusttorch_core::{Device, Result, Tensor};
use crate::Bytes;
#[derive(Clone, Copy, Debug, Default)]
pub struct PinDisabled;
#[derive(Clone, Copy, Debug, Default)]
pub struct PinEnabled<M>(pub(crate) PhantomData<M>);
#[derive(Clone, Copy, Debug, Default)]
pub struct Auto;
#[derive(Clone, Copy, Debug, Default)]
pub struct Explicit;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PinMemoryStatus {
Disabled,
DisabledNoAccelerator,
Enabled(Device),
}
pub trait PinMemory: Sized {
fn pin_memory(self, device: Device) -> Result<Self>;
}
impl PinMemory for Tensor {
fn pin_memory(self, device: Device) -> Result<Self> {
self.f_pin_memory(device).map_err(Into::into)
}
}
macro_rules! impl_identity_pin {
($($type:ty),+ $(,)?) => {
$(
impl PinMemory for $type {
fn pin_memory(self, _device: Device) -> Result<Self> {
Ok(self)
}
}
)+
};
}
impl_identity_pin!(u8, i8, i16, i32, i64, f32, f64, bool, String, Bytes);
impl<T> PinMemory for Vec<T>
where
T: PinMemory,
{
fn pin_memory(self, device: Device) -> Result<Self> {
self.into_iter()
.map(|value| value.pin_memory(device))
.collect()
}
}
impl<T> PinMemory for Option<T>
where
T: PinMemory,
{
fn pin_memory(self, device: Device) -> Result<Self> {
self.map(|value| value.pin_memory(device)).transpose()
}
}
impl<K, V> PinMemory for BTreeMap<K, V>
where
K: Ord,
V: PinMemory,
{
fn pin_memory(self, device: Device) -> Result<Self> {
self.into_iter()
.map(|(key, value)| value.pin_memory(device).map(|value| (key, value)))
.collect()
}
}
macro_rules! impl_tuple_pin {
($(($type:ident, $value:ident)),+ $(,)?) => {
impl<$($type),+> PinMemory for ($($type,)+)
where
$($type: PinMemory),+
{
fn pin_memory(self, device: Device) -> Result<Self> {
let ($($value,)+) = self;
Ok(($($value.pin_memory(device)?,)+))
}
}
};
}
impl_tuple_pin!((A, a), (B, b));
impl_tuple_pin!((A, a), (B, b), (C, c));
impl_tuple_pin!((A, a), (B, b), (C, c), (D, d));
impl_tuple_pin!((A, a), (B, b), (C, c), (D, d), (E, e));
impl_tuple_pin!((A, a), (B, b), (C, c), (D, d), (E, e), (F, f));
impl_tuple_pin!((A, a), (B, b), (C, c), (D, d), (E, e), (F, f), (G, g));
impl_tuple_pin!(
(A, a),
(B, b),
(C, c),
(D, d),
(E, e),
(F, f),
(G, g),
(H, h)
);