use std::{
collections::{BTreeMap, TryReserveError},
convert::Infallible,
error::Error,
fmt,
};
use rusttorch_core::{Result, RustTorchError, Tensor};
use crate::Checkpointable;
pub trait Collate<Sample> {
type Batch;
type Error;
fn collate(&mut self, samples: Vec<Sample>) -> std::result::Result<Self::Batch, Self::Error>;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct VecCollate;
impl Checkpointable for VecCollate {
type State = ();
fn save_state(&self) -> Self::State {}
fn validate_state(&self, _state: &Self::State) -> Result<()> {
Ok(())
}
fn load_validated(&mut self, _state: &Self::State) {}
}
impl<Sample> Collate<Sample> for VecCollate {
type Batch = Vec<Sample>;
type Error = Infallible;
fn collate(&mut self, samples: Vec<Sample>) -> std::result::Result<Self::Batch, Self::Error> {
Ok(samples)
}
}
#[derive(Clone, Copy, Debug)]
pub struct FnCollate<F> {
collate: F,
}
impl<F> FnCollate<F> {
pub fn new(collate: F) -> Self {
Self { collate }
}
}
impl<Sample, Batch, E, F> Collate<Sample> for FnCollate<F>
where
F: FnMut(Vec<Sample>) -> std::result::Result<Batch, E>,
{
type Batch = Batch;
type Error = E;
fn collate(&mut self, samples: Vec<Sample>) -> std::result::Result<Self::Batch, Self::Error> {
(self.collate)(samples)
}
}
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Bytes(pub Vec<u8>);
#[derive(Debug)]
#[non_exhaustive]
pub enum CollateError {
EmptyBatch,
ConversionBatchSize {
actual: usize,
},
SequenceLengthMismatch {
sample: usize,
expected: usize,
actual: usize,
},
OptionPresenceMismatch {
sample: usize,
expected_some: bool,
actual_some: bool,
},
MapKeysMismatch {
sample: usize,
},
SizeOverflow {
context: &'static str,
value: usize,
},
Allocation {
context: &'static str,
requested: usize,
source: TryReserveError,
},
LibTorch {
source: RustTorchError,
},
}
impl fmt::Display for CollateError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyBatch => formatter.write_str("cannot collate an empty batch"),
Self::ConversionBatchSize { actual } => write!(
formatter,
"default conversion requires exactly one sample, got {actual}"
),
Self::SequenceLengthMismatch {
sample,
expected,
actual,
} => write!(
formatter,
"sequence sample {sample} has length {actual}, expected {expected}"
),
Self::OptionPresenceMismatch {
sample,
expected_some,
actual_some,
} => write!(
formatter,
"option sample {sample} presence is {actual_some}, expected {expected_some}"
),
Self::MapKeysMismatch { sample } => {
write!(formatter, "map sample {sample} has a different key set")
}
Self::SizeOverflow { context, value } => {
write!(formatter, "{context} size {value} is not representable")
}
Self::Allocation {
context,
requested,
source,
} => write!(
formatter,
"failed to reserve {requested} elements for {context}: {source}"
),
Self::LibTorch { source } => write!(formatter, "LibTorch collation failed: {source}"),
}
}
}
impl Error for CollateError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Allocation { source, .. } => Some(source),
Self::LibTorch { source } => Some(source),
_ => None,
}
}
}
impl From<Infallible> for CollateError {
fn from(error: Infallible) -> Self {
match error {}
}
}
fn reserve_vec<T>(
capacity: usize,
context: &'static str,
) -> std::result::Result<Vec<T>, CollateError> {
let mut values = Vec::new();
values
.try_reserve_exact(capacity)
.map_err(|source| CollateError::Allocation {
context,
requested: capacity,
source,
})?;
Ok(values)
}
fn require_nonempty<T>(samples: &[T]) -> std::result::Result<(), CollateError> {
if samples.is_empty() {
Err(CollateError::EmptyBatch)
} else {
Ok(())
}
}
fn backend_error(source: impl Into<RustTorchError>) -> CollateError {
CollateError::LibTorch {
source: source.into(),
}
}
pub trait DefaultCollate: Sized {
type Batch;
fn default_collate(samples: Vec<Self>) -> std::result::Result<Self::Batch, CollateError>;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct DefaultCollator;
impl Checkpointable for DefaultCollator {
type State = ();
fn save_state(&self) -> Self::State {}
fn validate_state(&self, _state: &Self::State) -> Result<()> {
Ok(())
}
fn load_validated(&mut self, _state: &Self::State) {}
}
impl<Sample> Collate<Sample> for DefaultCollator
where
Sample: DefaultCollate,
{
type Batch = Sample::Batch;
type Error = CollateError;
fn collate(&mut self, samples: Vec<Sample>) -> std::result::Result<Self::Batch, Self::Error> {
Sample::default_collate(samples)
}
}
impl DefaultCollate for Tensor {
type Batch = Tensor;
fn default_collate(samples: Vec<Self>) -> std::result::Result<Self::Batch, CollateError> {
require_nonempty(&samples)?;
i32::try_from(samples.len()).map_err(|_| CollateError::SizeOverflow {
context: "tensor stack",
value: samples.len(),
})?;
Tensor::f_stack(&samples, 0).map_err(backend_error)
}
}
macro_rules! impl_numeric_collate {
($($type:ty),+ $(,)?) => {
$(
impl DefaultCollate for $type {
type Batch = Tensor;
fn default_collate(
samples: Vec<Self>,
) -> std::result::Result<Self::Batch, CollateError> {
require_nonempty(&samples)?;
i64::try_from(samples.len()).map_err(|_| CollateError::SizeOverflow {
context: "numeric tensor",
value: samples.len(),
})?;
Tensor::f_from_slice(&samples).map_err(backend_error)
}
}
)+
};
}
impl_numeric_collate!(u8, i8, i16, i32, i64, f32, f64, bool);
impl DefaultCollate for String {
type Batch = Vec<String>;
fn default_collate(samples: Vec<Self>) -> std::result::Result<Self::Batch, CollateError> {
require_nonempty(&samples)?;
Ok(samples)
}
}
impl DefaultCollate for Bytes {
type Batch = Vec<Bytes>;
fn default_collate(samples: Vec<Self>) -> std::result::Result<Self::Batch, CollateError> {
require_nonempty(&samples)?;
Ok(samples)
}
}
impl<T> DefaultCollate for Option<T>
where
T: DefaultCollate,
{
type Batch = Option<T::Batch>;
fn default_collate(samples: Vec<Self>) -> std::result::Result<Self::Batch, CollateError> {
require_nonempty(&samples)?;
let expected_some = samples[0].is_some();
for (sample, value) in samples.iter().enumerate().skip(1) {
let actual_some = value.is_some();
if actual_some != expected_some {
return Err(CollateError::OptionPresenceMismatch {
sample,
expected_some,
actual_some,
});
}
}
if !expected_some {
return Ok(None);
}
let sample_count = samples.len();
let mut present = reserve_vec(sample_count, "option values")?;
for value in samples {
match value {
Some(value) => present.push(value),
None => {
return Err(CollateError::OptionPresenceMismatch {
sample: present.len(),
expected_some: true,
actual_some: false,
});
}
}
}
T::default_collate(present).map(Some)
}
}
impl<T> DefaultCollate for Vec<T>
where
T: DefaultCollate,
{
type Batch = Vec<T::Batch>;
fn default_collate(samples: Vec<Self>) -> std::result::Result<Self::Batch, CollateError> {
require_nonempty(&samples)?;
let sequence_len = samples[0].len();
for (sample, sequence) in samples.iter().enumerate().skip(1) {
if sequence.len() != sequence_len {
return Err(CollateError::SequenceLengthMismatch {
sample,
expected: sequence_len,
actual: sequence.len(),
});
}
}
let sample_count = samples.len();
let mut columns = reserve_vec(sequence_len, "sequence columns")?;
for _ in 0..sequence_len {
columns.push(reserve_vec(sample_count, "sequence column values")?);
}
for sequence in samples {
for (column, value) in columns.iter_mut().zip(sequence) {
column.push(value);
}
}
let mut batch = reserve_vec(sequence_len, "collated sequence")?;
for column in columns {
batch.push(T::default_collate(column)?);
}
Ok(batch)
}
}
impl<K, V> DefaultCollate for BTreeMap<K, V>
where
K: Ord,
V: DefaultCollate,
{
type Batch = BTreeMap<K, V::Batch>;
fn default_collate(samples: Vec<Self>) -> std::result::Result<Self::Batch, CollateError> {
require_nonempty(&samples)?;
for (sample, map) in samples.iter().enumerate().skip(1) {
if !samples[0].keys().eq(map.keys()) {
return Err(CollateError::MapKeysMismatch { sample });
}
}
let sample_count = samples.len();
let mut maps = samples.into_iter();
let first = maps.next().ok_or(CollateError::EmptyBatch)?;
let mut columns = BTreeMap::new();
for (key, value) in first {
let mut values = reserve_vec(sample_count, "map values")?;
values.push(value);
columns.insert(key, values);
}
for map in maps {
for (values, value) in columns.values_mut().zip(map.into_values()) {
values.push(value);
}
}
let mut batch = BTreeMap::new();
for (key, values) in columns {
batch.insert(key, V::default_collate(values)?);
}
Ok(batch)
}
}
pub trait DefaultConvert: Sized {
type Output;
fn default_convert(self) -> std::result::Result<Self::Output, CollateError>;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct DefaultConverter;
impl Checkpointable for DefaultConverter {
type State = ();
fn save_state(&self) -> Self::State {}
fn validate_state(&self, _state: &Self::State) -> Result<()> {
Ok(())
}
fn load_validated(&mut self, _state: &Self::State) {}
}
impl DefaultConverter {
pub fn convert<T>(&mut self, sample: T) -> std::result::Result<T::Output, CollateError>
where
T: DefaultConvert,
{
sample.default_convert()
}
}
impl<Sample> Collate<Sample> for DefaultConverter
where
Sample: DefaultConvert,
{
type Batch = Sample::Output;
type Error = CollateError;
fn collate(&mut self, samples: Vec<Sample>) -> std::result::Result<Self::Batch, Self::Error> {
let actual = samples.len();
if actual != 1 {
return Err(CollateError::ConversionBatchSize { actual });
}
match samples.into_iter().next() {
Some(sample) => sample.default_convert(),
None => Err(CollateError::ConversionBatchSize { actual: 0 }),
}
}
}
macro_rules! impl_leaf_convert {
($($type:ty),+ $(,)?) => {
$(
impl DefaultConvert for $type {
type Output = Self;
fn default_convert(self) -> std::result::Result<Self::Output, CollateError> {
Ok(self)
}
}
)+
};
}
impl_leaf_convert!(Tensor, u8, i8, i16, i32, i64, f32, f64, bool, String, Bytes);
impl<T> DefaultConvert for Option<T>
where
T: DefaultConvert,
{
type Output = Option<T::Output>;
fn default_convert(self) -> std::result::Result<Self::Output, CollateError> {
self.map(DefaultConvert::default_convert).transpose()
}
}
impl<T> DefaultConvert for Vec<T>
where
T: DefaultConvert,
{
type Output = Vec<T::Output>;
fn default_convert(self) -> std::result::Result<Self::Output, CollateError> {
let mut output = reserve_vec(self.len(), "converted sequence")?;
for value in self {
output.push(value.default_convert()?);
}
Ok(output)
}
}
impl<K, V> DefaultConvert for BTreeMap<K, V>
where
K: Ord,
V: DefaultConvert,
{
type Output = BTreeMap<K, V::Output>;
fn default_convert(self) -> std::result::Result<Self::Output, CollateError> {
let mut output = BTreeMap::new();
for (key, value) in self {
output.insert(key, value.default_convert()?);
}
Ok(output)
}
}
macro_rules! impl_tuple {
($(($type:ident, $index:tt, $value:ident)),+ $(,)?) => {
impl<$($type),+> DefaultCollate for ($($type,)+)
where
$($type: DefaultCollate),+
{
type Batch = ($($type::Batch,)+);
fn default_collate(
samples: Vec<Self>,
) -> std::result::Result<Self::Batch, CollateError> {
require_nonempty(&samples)?;
let sample_count = samples.len();
let mut fields = ($(reserve_vec::<$type>(sample_count, "tuple field")?,)+);
for ($($value,)+) in samples {
$(fields.$index.push($value);)+
}
Ok(($($type::default_collate(fields.$index)?,)+))
}
}
impl<$($type),+> DefaultConvert for ($($type,)+)
where
$($type: DefaultConvert),+
{
type Output = ($($type::Output,)+);
fn default_convert(self) -> std::result::Result<Self::Output, CollateError> {
let ($($value,)+) = self;
Ok(($($value.default_convert()?,)+))
}
}
};
}
impl_tuple!((A, 0, a), (B, 1, b));
impl_tuple!((A, 0, a), (B, 1, b), (C, 2, c));
impl_tuple!((A, 0, a), (B, 1, b), (C, 2, c), (D, 3, d));
impl_tuple!((A, 0, a), (B, 1, b), (C, 2, c), (D, 3, d), (E, 4, e));
impl_tuple!(
(A, 0, a),
(B, 1, b),
(C, 2, c),
(D, 3, d),
(E, 4, e),
(F, 5, f)
);
impl_tuple!(
(A, 0, a),
(B, 1, b),
(C, 2, c),
(D, 3, d),
(E, 4, e),
(F, 5, f),
(G, 6, g)
);
impl_tuple!(
(A, 0, a),
(B, 1, b),
(C, 2, c),
(D, 3, d),
(E, 4, e),
(F, 5, f),
(G, 6, g),
(H, 7, h)
);