1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
use core::fmt::Debug;
use crate::{
memory_management::{MemoryHandle, MemoryManagement},
storage::ComputeStorage,
tune::AutotuneKey,
};
use alloc::vec::Vec;
use burn_common::reader::Reader;
/// The compute server is responsible for handling resources and computations over resources.
///
/// Everything in the server is mutable, therefore it should be solely accessed through the
/// [compute channel](crate::channel::ComputeChannel) for thread safety.
pub trait ComputeServer: Send + core::fmt::Debug
where
Self: Sized,
{
/// The kernel type defines the computation algorithms.
type Kernel: Send;
/// The [storage](ComputeStorage) type defines how data is stored and accessed.
type Storage: ComputeStorage;
/// The [memory management](MemoryManagement) type defines strategies for allocation in the [storage](ComputeStorage) type.
type MemoryManagement: MemoryManagement<Self::Storage>;
/// The key used to cache operations used on specific inputs in autotune
type AutotuneKey: AutotuneKey;
/// Given a handle, returns the owned resource as bytes.
fn read(&mut self, handle: &Handle<Self>) -> Reader<Vec<u8>>;
/// Given a resource as bytes, stores it and returns the memory handle.
fn create(&mut self, data: &[u8]) -> Handle<Self>;
/// Reserves `size` bytes in the storage, and returns a handle over them.
fn empty(&mut self, size: usize) -> Handle<Self>;
/// Executes the `kernel` over the given memory `handles`.
///
/// Kernels have mutable access to every resource they are given
/// and are responsible of determining which should be read or written.
fn execute(&mut self, kernel: Self::Kernel, handles: &[&Handle<Self>]);
/// Wait for the completion of every task in the server.
fn sync(&mut self);
}
/// Server handle containing the [memory handle](MemoryManagement::Handle).
#[derive(new, Debug)]
pub struct Handle<Server: ComputeServer> {
/// Handle for the memory in use.
pub memory: <Server::MemoryManagement as MemoryManagement<Server::Storage>>::Handle,
}
impl<Server: ComputeServer> Handle<Server> {
/// If the tensor handle can be mut with an inplace operation.
pub fn can_mut(&self) -> bool {
self.memory.can_mut()
}
}
impl<Server: ComputeServer> Clone for Handle<Server> {
fn clone(&self) -> Self {
Self {
memory: self.memory.clone(),
}
}
}