use crate::dist::placement::{Max, Mean, Min, PartialReduction, Prod, Sum};
use crate::exec::ReduceOp;
use crate::shapes::Dyn;
use crate::tensor::dtype::{DType, DTypeId};
pub trait CollectiveDType: DType {}
impl CollectiveDType for f32 {}
impl CollectiveDType for f64 {}
impl CollectiveDType for u8 {}
impl CollectiveDType for u32 {}
impl CollectiveDType for i64 {}
impl CollectiveDType for half::f16 {}
impl CollectiveDType for half::bf16 {}
impl CollectiveDType for Dyn {}
pub trait CollectiveReductionDType<R: PartialReduction>: CollectiveDType {}
impl<K: CollectiveDType> CollectiveReductionDType<Sum> for K {}
impl<K: CollectiveDType> CollectiveReductionDType<Prod> for K {}
impl<K: CollectiveDType> CollectiveReductionDType<Max> for K {}
impl<K: CollectiveDType> CollectiveReductionDType<Min> for K {}
impl CollectiveReductionDType<Mean> for f32 {}
impl CollectiveReductionDType<Mean> for f64 {}
impl CollectiveReductionDType<Mean> for half::f16 {}
impl CollectiveReductionDType<Mean> for half::bf16 {}
impl CollectiveReductionDType<Mean> for Dyn {}
pub const fn validate_collective_dtype(dtype: DTypeId) -> Result<(), CollectiveError> {
match dtype {
DTypeId::U8
| DTypeId::U32
| DTypeId::I64
| DTypeId::BF16
| DTypeId::F16
| DTypeId::F32
| DTypeId::F64
| DTypeId::Bool => Ok(()),
DTypeId::Q8_0 => Err(CollectiveError::UnsupportedDType { dtype }),
}
}
pub fn validate_collective_reduction(dtype: DTypeId, op: ReduceOp) -> Result<(), CollectiveError> {
validate_collective_dtype(dtype)?;
if matches!(op, ReduceOp::Mean) && matches!(dtype, DTypeId::U8 | DTypeId::U32 | DTypeId::I64) {
Err(CollectiveError::UnsupportedReduction { dtype, op })
} else {
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GroupId {
token: u64,
ranks: usize,
}
impl GroupId {
pub const fn new(token: u64, ranks: usize) -> Result<Self, CollectiveError> {
if ranks == 0 {
Err(CollectiveError::EmptyGroup)
} else {
Ok(Self { token, ranks })
}
}
#[must_use]
pub const fn token(self) -> u64 {
self.token
}
#[must_use]
pub const fn ranks(self) -> usize {
self.ranks
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StreamId(u32);
impl StreamId {
#[must_use]
pub const fn new(id: u32) -> Self {
Self(id)
}
#[must_use]
pub const fn get(self) -> u32 {
self.0
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CollectiveKind {
AllReduce(ReduceOp),
AllGather,
ReduceScatter(ReduceOp),
AllToAll,
SendRecv {
source: usize,
destination: usize,
},
}
impl CollectiveKind {
#[must_use]
pub const fn adjoint(self) -> Self {
match self {
Self::AllReduce(op) => Self::AllReduce(op),
Self::AllGather => Self::ReduceScatter(ReduceOp::Sum),
Self::ReduceScatter(_) => Self::AllGather,
Self::AllToAll => Self::AllToAll,
Self::SendRecv {
source,
destination,
} => Self::SendRecv {
source: destination,
destination: source,
},
}
}
}
#[non_exhaustive]
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
pub enum CollectiveError {
#[error("a collective group must contain at least one rank")]
EmptyGroup,
#[error("dtype {dtype:?} has no scalar collective encoding")]
UnsupportedDType {
dtype: DTypeId,
},
#[error("collective group requires {expected} rank buffers, found {found}")]
InputCount {
expected: usize,
found: usize,
},
#[error("rank {rank} has dtype {found:?}, expected {expected:?}")]
DTypeMismatch {
rank: usize,
expected: DTypeId,
found: DTypeId,
},
#[error("buffer values have dtype {values:?}, but the tensor dtype is {typed:?}")]
BufferDType {
values: DTypeId,
typed: DTypeId,
},
#[error("rank {rank} has {found} elements, expected {expected}")]
ElementCount {
rank: usize,
expected: usize,
found: usize,
},
#[error("{elements} elements cannot be divided across {ranks} ranks")]
NonDivisible {
elements: usize,
ranks: usize,
},
#[error("point-to-point source and destination are both rank {rank}")]
SamePeer {
rank: usize,
},
#[error("point-to-point {endpoint} rank {rank} is outside a group of {ranks} ranks")]
PeerOutOfRange {
endpoint: &'static str,
rank: usize,
ranks: usize,
},
#[error("reduction {op:?} is unsupported for dtype {dtype:?}")]
UnsupportedReduction {
dtype: DTypeId,
op: ReduceOp,
},
#[error("reduction {op:?} overflowed dtype {dtype:?} at element {element}")]
ReductionOverflow {
dtype: DTypeId,
op: ReduceOp,
element: usize,
},
}