use alloc::vec::Vec;
use crate::{
Backend, DeviceId, TensorMetadata,
distributed::CollectiveTensor,
tensor::{Device, FloatTensor},
};
use crate::distributed::{DistributedConfig, DistributedParams, ReduceOperation};
#[cfg(feature = "std")]
use crate::distributed::{
close_distributed_sync_server, get_distributed_sync_client, start_distributed_sync_server,
};
#[derive(Clone)]
pub struct TensorRef<B: Backend>(pub *mut FloatTensor<B>);
unsafe impl<B> Sync for TensorRef<B> where B: Backend {}
unsafe impl<B> Send for TensorRef<B> where B: Backend {}
pub trait DistributedOps<B: Backend> {
fn start_communication_server(devices: &[B::Device], config: DistributedConfig) {
#[cfg(feature = "std")]
start_distributed_sync_server::<B>(devices, config);
#[cfg(not(feature = "std"))]
let _ = (devices, config);
}
fn close_communication_server(_device: &B::Device) {
#[cfg(feature = "std")]
close_distributed_sync_server::<B>();
}
fn register_sync_parameters(_device: &B::Device, distributed_params: Vec<DistributedParams>) {
#[cfg(feature = "std")]
if let Some(sync_client) = get_distributed_sync_client::<B>() {
sync_client.register_sync_parameters(distributed_params);
};
#[cfg(not(feature = "std"))]
let _ = distributed_params;
}
fn submit_sync_collective(device: &B::Device) {
#[cfg(feature = "std")]
if let Some(sync_client) = get_distributed_sync_client::<B>() {
sync_client.submit_sync_collective(device.clone());
};
#[cfg(not(feature = "std"))]
let _ = device;
}
fn submit_gradient_sync(tensor: TensorRef<B>, distributed_params: DistributedParams) {
#[cfg(feature = "std")]
if let Some(sync_client) = get_distributed_sync_client::<B>() {
sync_client.submit_gradient_sync(tensor, distributed_params);
};
#[cfg(not(feature = "std"))]
let _ = (tensor, distributed_params);
}
fn all_reduce(
_tensor: FloatTensor<B>,
_op: ReduceOperation,
_device_ids: Vec<DeviceId>,
) -> CollectiveTensor<B> {
unimplemented!()
}
fn sync_collective(_device: &B::Device) {
unimplemented!()
}
unsafe fn comm_device(tensor: &TensorRef<B>) -> Device<B> {
unsafe { &(*tensor.0) }.device()
}
unsafe fn float_from_ref(tensor: &TensorRef<B>) -> FloatTensor<B> {
unsafe { (*tensor.0).clone() }
}
}