#[allow(
unused_imports,
reason = "a split file takes its parent's imports whole; each uses some"
)]
use super::*;
#[derive(Clone, Copy)]
pub(super) struct SlotPlace<'k> {
pub(super) slot: &'k DistanceSlot,
pub(super) place: usize,
pub(super) in_shape: usize,
}
impl SlotPlace<'_> {
pub(super) fn is_ard(self) -> bool {
matches!(self.slot.shape(), SlotShape::Ard(_))
}
}
pub(super) fn shape_places(slots: &[DistanceSlot]) -> impl Iterator<Item = (bool, usize)> + '_ {
let mut seen = [0usize; 2];
slots.iter().map(move |slot| {
let ard = matches!(slot.shape(), SlotShape::Ard(_));
let at = seen[usize::from(ard)];
seen[usize::from(ard)] += 1;
(ard, at)
})
}
pub(crate) fn place_in_shape(slots: &[DistanceSlot], place: usize) -> usize {
shape_places(slots).nth(place).map_or(0, |(_, at)| at)
}
pub(super) fn ard_count(slots: &[DistanceSlot]) -> usize {
shape_places(slots).filter(|&(ard, _)| ard).count()
}
pub(super) fn slot_of<'k>(
slots: &'k [DistanceSlot],
source: &DistanceSource<'_>,
) -> Result<SlotPlace<'k>, GprError> {
let Some(place) = slots.iter().position(|slot| slot.id() == source.slot) else {
return Err(GprError::DistanceSlot {
kind: SlotErrorKind::NotRead,
slot: None,
});
};
let slot = &slots[place];
debug_assert_eq!(slot.shape(), source.data.shape());
Ok(SlotPlace {
slot,
place,
in_shape: place_in_shape(slots, place),
})
}
pub(super) fn duplicate(place: usize) -> GprError {
GprError::DistanceSlot {
kind: SlotErrorKind::Duplicate,
slot: Some(place),
}
}
pub(super) fn missing(place: Option<usize>) -> GprError {
GprError::DistanceSlot {
kind: SlotErrorKind::Missing,
slot: place,
}
}
pub(super) fn in_block(err: GprError, shape: SlotShape, k: usize) -> GprError {
match shape {
SlotShape::Ard(_) => err.in_dim(k),
SlotShape::Scalar => err,
}
}
pub(super) fn locate_block(slots: &[DistanceSlot], at: BlockAt, err: GprError) -> GprError {
let (ard, nth, err) = match at {
BlockAt::Scalar(nth) => (false, nth, err),
BlockAt::Ard(nth, k) => (true, nth, err.in_dim(k)),
};
match shape_places(slots).position(|place| place == (ard, nth)) {
Some(place) => err.in_slot(place),
None => err,
}
}
pub(super) fn in_slot_of(err: GprError, slots: &[DistanceSlot], id: SlotId) -> GprError {
match slots.iter().position(|slot| slot.id() == id) {
Some(place) => err.in_slot(place),
None => err,
}
}
pub(super) fn no_room() -> GprError {
GprError::UnsupportedKernelOperation {
reason: "the training store was not laid out for the change".to_owned(),
}
}