use ruda_tensor::{
Backend, TensorMetadata,
tensor::{Device, FloatTensor},
};
use ruda_core::tensor::Shape;
use std::collections::HashMap;
use crate::PeerId;
pub type CollectiveTensorMap<B> = HashMap<PeerId, FloatTensor<B>>;
pub type PeerDeviceMap<B> = HashMap<PeerId, Device<B>>;
pub fn get_common_shape<B: Backend>(tensors: &CollectiveTensorMap<B>) -> Option<Shape> {
let mut it = tensors.values();
if let Some(first) = it.next() {
let shape = first.shape();
for tensor in it {
if tensor.shape() != shape {
return None;
}
}
return Some(shape);
}
None
}
pub fn get_peer_devices<B: Backend>(tensors: &CollectiveTensorMap<B>) -> PeerDeviceMap<B> {
tensors
.iter()
.map(|(id, tensor)| (*id, B::float_device(tensor)))
.collect()
}