Skip to main content

qualia_core_db/inference/inference_bench/
prepared_domino.rs

1//! Request-owned grammar execution. Evolving grammar state never writes back to
2//! the process template or becomes the starting state of another request.
3use crate::inference::speculative_decode::PreparedDomino;
4use sha2::{Digest, Sha256};
5use std::{
6    cell::RefCell,
7    marker::PhantomData,
8    rc::Rc,
9    sync::{
10        atomic::{AtomicBool, AtomicU64, Ordering},
11        Arc,
12    },
13};
14
15static NEXT: AtomicU64 = AtomicU64::new(1);
16struct Slot {
17    identity: u64,
18    grammar: Option<PreparedDomino>,
19    valid: Arc<AtomicBool>,
20}
21thread_local! { static SLOT:RefCell<Option<Slot>> = const { RefCell::new(None) }; }
22
23#[derive(Clone)]
24pub struct PreparedDominoLease {
25    identity: u64,
26    digest: [u8; 32],
27    valid: Arc<AtomicBool>,
28}
29impl PreparedDominoLease {
30    pub fn digest(&self) -> [u8; 32] {
31        self.digest
32    }
33    pub fn live(&self) -> bool {
34        self.valid.load(Ordering::Acquire)
35            && SLOT.with(|slot| {
36                slot.borrow().as_ref().is_some_and(|slot| {
37                    slot.identity == self.identity
38                        && slot.grammar.as_ref().is_none_or(PreparedDomino::valid)
39                })
40            })
41    }
42}
43pub struct PreparedDominoScope {
44    previous: Option<Slot>,
45    lease: PreparedDominoLease,
46    _thread: PhantomData<Rc<()>>,
47}
48impl PreparedDominoScope {
49    pub(crate) fn enter(grammar: Option<PreparedDomino>) -> Result<Self, &'static str> {
50        let identity = NEXT
51            .try_update(Ordering::AcqRel, Ordering::Acquire, |n| n.checked_add(1))
52            .map_err(|_| "DOMINO owner identity exhausted")?;
53        let valid = Arc::new(AtomicBool::new(true));
54        let digest = grammar
55            .as_ref()
56            .map(PreparedDomino::digest)
57            .unwrap_or_else(|| Sha256::digest(b"qualia:prepared-domino-absent:v1\0").into());
58        let lease = PreparedDominoLease {
59            identity,
60            digest,
61            valid: Arc::clone(&valid),
62        };
63        let previous = SLOT.with(|slot| {
64            slot.replace(Some(Slot {
65                identity,
66                grammar,
67                valid,
68            }))
69        });
70        Ok(Self {
71            previous,
72            lease,
73            _thread: PhantomData,
74        })
75    }
76    pub fn lease(&self) -> PreparedDominoLease {
77        self.lease.clone()
78    }
79}
80impl Drop for PreparedDominoScope {
81    fn drop(&mut self) {
82        self.lease.valid.store(false, Ordering::Release);
83        SLOT.with(|slot| {
84            slot.replace(self.previous.take());
85        });
86    }
87}
88
89pub(crate) fn with<T>(operation: impl FnOnce(&mut Option<PreparedDomino>) -> T) -> Option<T> {
90    SLOT.with(|slot| {
91        let mut slot = slot.borrow_mut();
92        let slot = slot.as_mut()?;
93        let result = operation(&mut slot.grammar);
94        if slot
95            .grammar
96            .as_ref()
97            .is_some_and(|grammar| !grammar.valid())
98        {
99            slot.valid.store(false, Ordering::Release);
100        }
101        Some(result)
102    })
103}
104pub(crate) fn invalidate() -> bool {
105    SLOT.with(|slot| {
106        let slot = slot.borrow();
107        if let Some(slot) = slot.as_ref() {
108            slot.valid.store(false, Ordering::Release);
109            true
110        } else {
111            false
112        }
113    })
114}