#[cfg(not(target_family = "wasm"))]
use core::future::Future;
#[cfg(all(not(target_family = "wasm"), feature = "remote-websocket"))]
pub use burn_dispatch::__remote::server::WebSocketTransport;
#[cfg(not(target_family = "wasm"))]
pub use burn_dispatch::__remote::server::{IrohTransport, Transport};
pub use burn_dispatch::__remote::{
BURN_REMOTE_ALPN, Credential, Endpoint, InvalidRelays, IrohRelays, RelayUrl,
ir::{CustomOpIr, HandleContainer},
server::{
AllowAll, AuthorizationRequest, ClientId, CustomOpRegistry, EmptyToken, IrohIdentity,
PeerAuthorizer, RemoteProtocol, ServeError, ServerLogging, TokenAuthorizer,
},
telemetry,
};
use burn_dispatch::__remote::{ir::BackendIr, server::ServerSettings};
use crate::Device;
use telemetry::TelemetryProbe;
#[derive(Clone)]
pub struct RemoteServer {
devices: Vec<Device>,
settings: ServerSettings,
}
impl RemoteServer {
pub fn new(devices: impl IntoIterator<Item = Device>) -> Self {
Self {
devices: devices.into_iter().collect(),
settings: ServerSettings::default(),
}
}
pub fn with_authorizer(mut self, authorizer: impl PeerAuthorizer) -> Self {
self.settings = self.settings.with_authorizer(authorizer);
self
}
pub fn with_telemetry(mut self, probe: TelemetryProbe) -> Self {
self.settings = self.settings.with_telemetry(probe);
self
}
pub fn with_custom_op<B: BackendIr, F>(mut self, id: &str, handler: F) -> Self
where
F: Fn(&mut HandleContainer<B::Handle>, &CustomOpIr, &B::Device) + Send + Sync + 'static,
{
self.settings = self.settings.with_custom_op::<B, F>(id, handler);
self
}
pub fn with_custom_ops<B: BackendIr>(mut self, registry: CustomOpRegistry<B>) -> Self {
self.settings = self.settings.with_custom_ops(registry);
self
}
#[cfg(not(target_family = "wasm"))]
pub fn serve(&self, transport: impl Into<Transport>) -> Result<(), ServeError> {
burn_dispatch::remote_server::serve(
self.dispatch_devices(),
self.settings.clone(),
transport.into(),
)
}
#[cfg(not(target_family = "wasm"))]
pub fn serve_async<T: Into<Transport>>(
&self,
transport: T,
) -> impl Future<Output = Result<(), ServeError>> + Send + 'static + use<T> {
burn_dispatch::remote_server::serve_async(
self.dispatch_devices(),
self.settings.clone(),
transport.into(),
)
}
pub fn into_protocol(self, endpoint: &Endpoint) -> Result<RemoteProtocol, ServeError> {
let devices = self
.devices
.into_iter()
.map(Device::into_dispatch)
.collect();
burn_dispatch::remote_server::into_protocol(devices, self.settings, endpoint)
}
#[cfg(not(target_family = "wasm"))]
fn dispatch_devices(&self) -> Vec<burn_dispatch::DispatchDevice> {
self.devices
.iter()
.cloned()
.map(Device::into_dispatch)
.collect()
}
}