use crate::Context;
use core::{
convert::Infallible,
sync::atomic::{AtomicUsize, Ordering},
};
use smol_bytes::Utf8Bytes;
use super::super::{
AsyncConstructor, Enablement, LaunchError, NodeSection, NodeWiring, Plan, Resources,
};
use crate::{
AsyncFulltextStore, AsyncResource, ChannelGraph, DeclaredResource, Delivery, DriverKind, Effect,
FanoutKind, FulltextTable, GraphBuilder, Identifiable, NodeId, Operator, ResourceCatalog,
ResourceDemand, ResourceSource, SlotName, Transformer, Wired, bare_options,
};
macro_rules! unit {
($name:ident) => {
struct $name;
impl Operator for $name {
type Error = Infallible;
type In = u8;
type Out = u8;
fn accept(
&mut self,
_ctx: &Context,
_delivery: Delivery<Self::In>,
_emitter: &mut crate::Emitter<u8>,
) -> Result<(), Self::Error> {
Ok(())
}
}
impl crate::Declared for $name {
const EFFECT: crate::Effect = crate::Effect::Pure;
type Channels = crate::Unchanneled;
type Mode = crate::Block;
type Options = ();
type Pair = crate::Buffered;
type Wires<O> = crate::Downstream<O>;
type Attribution<O> = ();
}
};
}
unit!(DecodeUnit);
unit!(CaptionsUnit);
struct Egemma(usize);
impl AsyncResource for Egemma {
type Error = Infallible;
type Options = usize;
async fn build(_ctx: &Resources, dim: usize) -> Result<Self, Self::Error> {
Ok(Self(dim))
}
}
impl DeclaredResource for Egemma {}
struct Whisper;
impl AsyncResource for Whisper {
type Error = Infallible;
type Options = ();
async fn build(_ctx: &Resources, (): ()) -> Result<Self, Self::Error> {
Ok(Self)
}
}
impl DeclaredResource for Whisper {}
struct Tokenizer(usize);
impl AsyncResource for Tokenizer {
type Error = Infallible;
type Options = ();
async fn build(ctx: &Resources, (): ()) -> Result<Self, Self::Error> {
Ok(Self(ctx.shared::<Egemma>().map_or(0, |model| model.0)))
}
}
impl DeclaredResource for Tokenizer {}
struct Corrupt;
#[derive(Debug, derive_more::Display)]
#[display("the weights are corrupt")]
struct CorruptError;
impl core::error::Error for CorruptError {}
impl AsyncResource for Corrupt {
type Error = CorruptError;
type Options = ();
async fn build(_ctx: &Resources, (): ()) -> Result<Self, Self::Error> {
Err(CorruptError)
}
}
impl DeclaredResource for Corrupt {}
#[derive(Debug)]
struct Decode {
threads: usize,
dim: usize,
}
struct DecodeOptions {
threads: usize,
}
impl Default for DecodeOptions {
fn default() -> Self {
Self { threads: 4 }
}
}
impl AsyncConstructor for Decode {
type Error = Infallible;
type Options = DecodeOptions;
async fn with_options(ctx: &Resources, options: DecodeOptions) -> Result<Self, Self::Error> {
Ok(Self {
threads: options.threads,
dim: ctx.shared::<Egemma>().map_or(0, |model| model.0),
})
}
}
#[derive(Debug)]
struct Ingest {
root: String,
}
struct IngestOptions {
root: String,
}
impl AsyncConstructor for Ingest {
type Error = Infallible;
type Options = IngestOptions;
async fn with_options(_ctx: &Resources, options: IngestOptions) -> Result<Self, Self::Error> {
Ok(Self { root: options.root })
}
}
#[derive(Debug)]
struct Watchdog;
impl AsyncConstructor for Watchdog {
type Error = Infallible;
type Options = ();
async fn with_options(_ctx: &Resources, (): ()) -> Result<Self, Self::Error> {
Ok(Self)
}
}
static LATER_BUILDS: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug)]
struct Fickle;
#[derive(Debug, PartialEq, Eq, derive_more::Display)]
#[display("the device is busy")]
struct FickleError;
impl core::error::Error for FickleError {}
impl AsyncConstructor for Fickle {
type Error = FickleError;
type Options = ();
async fn with_options(_ctx: &Resources, (): ()) -> Result<Self, Self::Error> {
Err(FickleError)
}
}
#[derive(Debug)]
struct Later;
impl AsyncConstructor for Later {
type Error = Infallible;
type Options = ();
async fn with_options(_ctx: &Resources, (): ()) -> Result<Self, Self::Error> {
LATER_BUILDS.fetch_add(1, Ordering::Relaxed);
Ok(Self)
}
}
fn graph() -> ChannelGraph {
let mut builder = GraphBuilder::new();
let decode = builder.source::<DecodeUnit>("decode", DriverKind::Thread);
let captions = builder.node::<CaptionsUnit>("captions", DriverKind::Async);
builder.edge(&decode, &captions, FanoutKind::Broadcast);
builder.validate().expect("a pair validates")
}
fn deployment(launched: &[&str], demanding: usize, demands: &[ResourceDemand]) -> Wired {
let graph = graph();
let enablement = launched.iter().fold(Enablement::new(), |enablement, node| {
enablement.launching(*node)
});
let mut plan = Plan::new(graph).with_enablement(enablement).with_resources(
ResourceCatalog::new()
.with::<Egemma>("egemma", ResourceSource::Prepare)
.with::<Whisper>("whisper", ResourceSource::Prepare)
.with::<Corrupt>("corrupt", ResourceSource::Prepare),
);
for node in 0..2 {
plan = plan.wiring(NodeId::new(node), NodeWiring::new(Effect::Lands).landing());
}
for demand in demands {
plan = plan.demanding(NodeId::new(demanding), *demand);
}
plan.validate().expect("the arrangement checks out")
}
fn demanding_egemma() -> Wired {
deployment(
&["decode", "captions"],
0,
&[ResourceDemand::of::<Egemma>()],
)
}
#[tokio::test]
async fn a_demanded_resource_is_built_and_lands_in_the_context() {
let wired = demanding_egemma();
let mut walk = wired.create();
assert!(
walk
.resource::<Egemma, _>(|| 768)
.await
.expect("the model loads")
);
assert!(walk.context().holds::<Egemma>());
assert_eq!(
walk.context().shared::<Egemma>().expect("it is there").0,
768
);
}
#[tokio::test]
async fn an_undemanded_resource_is_not_built_and_its_factory_never_runs() {
let wired = demanding_egemma();
let mut walk = wired.create();
let mut asked = false;
assert!(
!walk
.resource::<Whisper, _>(|| asked = true)
.await
.expect("nothing to refuse")
);
assert!(!asked, "an undemanded resource asked for its options");
assert!(!walk.context().holds::<Whisper>());
}
#[tokio::test]
async fn a_demand_from_a_node_that_does_not_launch_builds_nothing() {
let wired = deployment(&["decode"], 1, &[ResourceDemand::of::<Egemma>()]);
let mut walk = wired.create();
assert!(
!walk
.resource::<Egemma, _>(|| 768)
.await
.expect("nothing to refuse")
);
assert!(!walk.context().holds::<Egemma>());
}
#[tokio::test]
async fn a_resource_is_built_once_however_often_the_walk_is_told_about_it() {
let wired = demanding_egemma();
let mut walk = wired.create();
assert!(walk.resource::<Egemma, _>(|| 768).await.expect("it loads"));
assert!(
!walk
.resource::<Egemma, _>(|| 1024)
.await
.expect("nothing to refuse")
);
assert_eq!(
walk.context().shared::<Egemma>().expect("it is there").0,
768,
"the second telling replaced the first build"
);
}
#[tokio::test]
async fn a_refused_resource_build_names_the_resource() {
let wired = deployment(
&["decode", "captions"],
0,
&[ResourceDemand::of::<Corrupt>()],
);
let mut walk = wired.create();
let refused = walk
.resource::<Corrupt, _>(|| ())
.await
.expect_err("the weights are corrupt");
assert!(refused.is_loading());
assert!(
refused.refusals().is_empty(),
"a create failure is not a list"
);
let rendered = refused.to_string();
assert!(rendered.contains("Corrupt"), "{rendered}");
assert!(rendered.contains("the weights are corrupt"), "{rendered}");
}
#[tokio::test]
async fn a_section_that_states_its_options_builds_the_node_with_them() {
let wired = demanding_egemma();
let mut walk = wired.create();
let decode = walk
.node::<Decode>(
"decode",
Some(NodeSection::new(DecodeOptions { threads: 8 })),
bare_options!(Decode),
)
.await
.expect("it builds")
.expect("the deployment names it");
assert_eq!(decode.threads, 8);
}
#[tokio::test]
async fn a_bare_section_runs_the_node_at_its_options_default() {
let wired = demanding_egemma();
let mut walk = wired.create();
let decode = walk
.node::<Decode>(
"decode",
Some(NodeSection::default()),
bare_options!(Decode),
)
.await
.expect("it builds")
.expect("the deployment names it");
assert_eq!(decode.threads, DecodeOptions::default().threads);
assert_eq!(decode.threads, 4);
}
#[tokio::test]
async fn a_zero_knob_node_is_launched_by_being_named() {
let wired = demanding_egemma();
let mut walk = wired.create();
let watchdog = walk
.node::<Watchdog>(
"watchdog",
Some(NodeSection::default()),
bare_options!(Watchdog),
)
.await
.expect("nothing can refuse")
.expect("the deployment names it");
assert!(matches!(watchdog, Watchdog));
}
#[tokio::test]
async fn a_bare_section_over_mandatory_knobs_is_refused_by_name() {
let wired = demanding_egemma();
let mut walk = wired.create();
let refused = walk
.node::<Ingest>(
"ingest",
Some(NodeSection::default()),
bare_options!(Ingest),
)
.await
.expect_err("its knobs are mandatory");
assert!(refused.is_construction());
let rendered = refused.to_string();
assert!(rendered.contains("`ingest`"), "{rendered}");
assert!(rendered.contains("IngestOptions"), "{rendered}");
}
#[tokio::test]
async fn mandatory_knobs_written_in_the_section_build_the_node() {
let wired = demanding_egemma();
let mut walk = wired.create();
let ingest = walk
.node::<Ingest>(
"ingest",
Some(NodeSection::new(IngestOptions {
root: "/srv/media".to_owned(),
})),
bare_options!(Ingest),
)
.await
.expect("it builds")
.expect("the deployment names it");
assert_eq!(ingest.root, "/srv/media");
}
#[tokio::test]
async fn a_node_no_section_names_is_not_built() {
let wired = demanding_egemma();
let mut walk = wired.create();
let decode = walk
.node::<Decode>("decode", None, bare_options!(Decode))
.await
.expect("nothing to refuse");
assert!(decode.is_none());
}
#[tokio::test]
async fn a_written_section_builds_the_node() {
let wired = demanding_egemma();
let mut walk = wired.create();
let decode = walk
.node::<Decode>(
"decode",
Some(NodeSection::new(DecodeOptions { threads: 8 })),
bare_options!(Decode),
)
.await
.expect("it builds")
.expect("the deployment names it");
assert_eq!(decode.threads, 8);
}
#[tokio::test]
async fn a_refused_construction_names_the_mount_and_keeps_the_typed_source() {
let wired = demanding_egemma();
let mut walk = wired.create();
let refused = walk
.node::<Fickle>(
"captions",
Some(NodeSection::default()),
bare_options!(Fickle),
)
.await
.expect_err("the device is busy");
assert!(refused.is_construction());
assert!(refused.to_string().contains("`captions`"));
let typed = core::error::Error::source(&refused)
.and_then(core::error::Error::source)
.and_then(|error| error.downcast_ref::<FickleError>());
assert_eq!(typed, Some(&FickleError));
}
#[tokio::test]
async fn a_construction_reaches_the_resource_the_walk_built_before_it() {
let wired = demanding_egemma();
let mut walk = wired.create();
walk.resource::<Egemma, _>(|| 768).await.expect("it loads");
let decode = walk
.node::<Decode>(
"decode",
Some(NodeSection::default()),
bare_options!(Decode),
)
.await
.expect("it builds")
.expect("the deployment names it");
assert_eq!(decode.dim, 768);
}
#[tokio::test]
async fn the_walk_stops_at_the_first_refusal() {
let wired = demanding_egemma();
let before = LATER_BUILDS.load(Ordering::Relaxed);
async fn launch(wired: &Wired) -> Result<(Option<Fickle>, Option<Later>), LaunchError> {
let mut walk = wired.create();
let fickle = walk
.node::<Fickle>(
"captions",
Some(NodeSection::default()),
bare_options!(Fickle),
)
.await?;
let later = walk
.node::<Later>("decode", Some(NodeSection::default()), bare_options!(Later))
.await?;
Ok((fickle, later))
}
let refused = launch(&wired).await.expect_err("the device is busy");
assert!(refused.is_construction());
assert_eq!(
LATER_BUILDS.load(Ordering::Relaxed),
before,
"the walk built a node after the failure"
);
}
#[tokio::test]
async fn the_walk_hands_its_context_on() {
let wired = demanding_egemma();
let mut walk = wired.create();
walk.resource::<Egemma, _>(|| 768).await.expect("it loads");
let ctx = walk.into_context();
assert_eq!(ctx.len(), 1);
assert!(ctx.holds::<Egemma>());
}
struct Caption {
frame: u32,
}
impl Identifiable for Caption {
type Id = u32;
fn id_ref(&self) -> &u32 {
&self.frame
}
}
impl Transformer for Caption {
type Graphql = ();
type Key = u32;
type Lineage = ();
type Record = Self;
fn key(&self) -> u32 {
self.frame
}
}
impl FulltextTable for Caption {
const SLOT: &'static SlotName = SlotName::new("captions");
}
struct Shelf;
struct Opened(Utf8Bytes);
impl AsyncFulltextStore for Shelf {
type Error = Infallible;
type Handle = Opened;
type Options = ();
async fn open((): ()) -> Result<Self, Self::Error> {
Ok(Self)
}
fn fulltext(&self, slot: impl Into<Utf8Bytes>) -> Opened {
Opened(slot.into())
}
}
#[tokio::test]
async fn a_slot_is_opened_under_the_declarations_own_name() {
let wired = demanding_egemma();
let walk = wired.create();
let slot = walk.async_fulltext_slot::<Shelf, Caption>(&Shelf);
assert_eq!(slot.engine().0.as_str(), Caption::SLOT.as_ref());
assert_eq!(slot.engine().0.as_str(), "captions");
}
#[test]
fn the_bare_probe_answers_default_where_there_is_one() {
assert!(bare_options!(Decode).is_some());
assert_eq!(bare_options!(Watchdog), Some(()));
assert!(bare_options!(Ingest).is_none());
}
#[tokio::test]
async fn a_resource_build_reaches_what_the_walk_built_before_it() {
let wired = demanding_egemma();
let mut walk = wired.create();
let early = Tokenizer::build(walk.context(), ())
.await
.expect("nothing refuses");
assert_eq!(
early.0, 0,
"the walk had built nothing yet, so the lookup refused and the resource said so"
);
assert!(
walk
.resource::<Egemma, _>(|| 768)
.await
.expect("the model loads")
);
let late = Tokenizer::build(walk.context(), ())
.await
.expect("nothing refuses");
assert_eq!(
late.0, 768,
"the map the walk had filled is the one the construction read"
);
}