Skip to main content

qualia_core_db/inference/
compute_universe_epoch.rs

1//! Request-scoped lifetime for U1 producer work and its host ring envelopes.
2//! The work mutex is held through actual device/search completion and result
3//! publication. Closing waits for in-flight work, revokes the epoch, and clears
4//! the published mask before another request can use the fabric.
5use crate::tensor::resident_substrate::ResidentSnapshot;
6use std::sync::{
7    atomic::{AtomicBool, AtomicU64, Ordering},
8    Mutex,
9};
10use std::{cell::Cell, sync::Arc};
11static SOURCE: Mutex<Option<Arc<ResidentSnapshot>>> = Mutex::new(None);
12static KV: Mutex<Option<crate::tensor::kv_provenance::KvProvenanceMap>> = Mutex::new(None);
13#[cfg(all(not(target_arch = "wasm32"), feature = "gpu-runtime"))]
14static GPU_SOURCE: Mutex<Option<crate::tensor::volume_gpu::owned::OwnedTensorSearch>> =
15    Mutex::new(None);
16thread_local! {
17    static OWNER: Cell<u64> = const { Cell::new(0) };
18    static PRODUCING: Cell<bool> = const { Cell::new(false) };
19}
20pub(super) fn source() -> Option<Arc<ResidentSnapshot>> {
21    SOURCE.lock().ok()?.as_ref().cloned()
22}
23pub(super) fn can_publish() -> bool {
24    source().is_none() || OWNER.with(|v| v.get() == current()) || PRODUCING.with(Cell::get)
25}
26
27static WORK: Mutex<()> = Mutex::new(());
28static EPOCH: AtomicU64 = AtomicU64::new(1);
29static ACTIVE: AtomicBool = AtomicBool::new(true);
30
31pub(super) fn current() -> u64 {
32    EPOCH.load(Ordering::Acquire)
33}
34fn advance() -> u64 {
35    EPOCH
36        .try_update(Ordering::AcqRel, Ordering::Acquire, |value| {
37            value.checked_add(1)
38        })
39        .expect("compute request epoch exhausted")
40        + 1
41}
42pub(super) fn accepts(epoch: u64) -> bool {
43    ACTIVE.load(Ordering::Acquire) && current() == epoch
44}
45pub(super) fn produce(work: impl FnOnce() -> usize) -> usize {
46    let _guard = WORK.lock().unwrap_or_else(|e| e.into_inner());
47    if !ACTIVE.load(Ordering::Acquire) {
48        return 0;
49    }
50    PRODUCING.with(|flag| {
51        struct Reset<'a>(&'a Cell<bool>);
52        impl Drop for Reset<'_> {
53            fn drop(&mut self) {
54                self.0.set(false);
55            }
56        }
57        flag.set(true);
58        let _reset = Reset(flag);
59        work()
60    })
61}
62
63pub struct ComputeRequestEpoch {
64    epoch: u64,
65    previous_owner: u64,
66    _thread: std::marker::PhantomData<std::rc::Rc<()>>,
67}
68pub fn begin_request_epoch() -> ComputeRequestEpoch {
69    let _guard = WORK.lock().unwrap_or_else(|e| e.into_inner());
70    ACTIVE.store(false, Ordering::Release);
71    let epoch = advance();
72    *SOURCE.lock().unwrap_or_else(|e| e.into_inner()) = None;
73    *KV.lock().unwrap_or_else(|e| e.into_inner()) = None;
74    #[cfg(all(not(target_arch = "wasm32"), feature = "gpu-runtime"))]
75    {
76        *GPU_SOURCE.lock().unwrap_or_else(|e| e.into_inner()) = None;
77    }
78    let previous_owner = OWNER.with(|owner| owner.replace(epoch));
79    // Envelopes from older requests remain bounded and are skipped on pop.
80    while super::context_inject_ring().try_pop().is_some() {}
81    while super::topology_draft_ring().try_pop().is_some() {}
82    super::publish_attention_route_mask(super::AttentionRouteMask::default());
83    super::publish_query_tensor(crate::tensor::Tensor10D::default(), 0);
84    super::publish_decode_hint(0, 0);
85    ComputeRequestEpoch {
86        epoch,
87        previous_owner,
88        _thread: std::marker::PhantomData,
89    }
90}
91#[allow(dead_code)]
92impl ComputeRequestEpoch {
93    #[allow(unused_variables)]
94    pub(crate) fn bind_source(&self, source: Arc<ResidentSnapshot>, gpu_budget: usize) -> bool {
95        let _guard = WORK.lock().unwrap_or_else(|e| e.into_inner());
96        if current() != self.epoch || ACTIVE.load(Ordering::Acquire) {
97            return false;
98        }
99        #[cfg(all(not(target_arch = "wasm32"), feature = "gpu-runtime"))]
100        {
101            let Ok(gpu) =
102                crate::tensor::volume_gpu::owned::OwnedTensorSearch::prepare(&source, gpu_budget)
103            else {
104                return false;
105            };
106            *GPU_SOURCE.lock().unwrap_or_else(|e| e.into_inner()) = gpu;
107        }
108        *SOURCE.lock().unwrap_or_else(|e| e.into_inner()) = Some(source);
109        true
110    }
111    pub(crate) fn identity(&self) -> u64 {
112        self.epoch
113    }
114    /// Freeze producer work and ring publication across input admission and the
115    /// complete numerical step. Device work finishes before the guard releases.
116    pub(crate) fn pause(&self) -> Option<std::sync::MutexGuard<'static, ()>> {
117        let guard = WORK.lock().ok()?;
118        (current() == self.epoch).then_some(guard)
119    }
120    /// Publish this request's query/mask before allowing producer work. The
121    /// initializer must not call the producer recursively or start another epoch.
122    pub(crate) fn activate(&self, initialize: impl FnOnce()) -> bool {
123        let _guard = WORK.lock().unwrap_or_else(|e| e.into_inner());
124        if current() != self.epoch {
125            return false;
126        }
127        initialize();
128        ACTIVE.store(true, Ordering::Release);
129        true
130    }
131}
132impl Drop for ComputeRequestEpoch {
133    fn drop(&mut self) {
134        let _guard = WORK.lock().unwrap_or_else(|e| e.into_inner());
135        if current() == self.epoch {
136            ACTIVE.store(false, Ordering::Release);
137            super::publish_attention_route_mask(super::AttentionRouteMask::default());
138            super::publish_query_tensor(crate::tensor::Tensor10D::default(), 0);
139            super::publish_decode_hint(0, 0);
140            *SOURCE.lock().unwrap_or_else(|e| e.into_inner()) = None;
141            *KV.lock().unwrap_or_else(|e| e.into_inner()) = None;
142            #[cfg(all(not(target_arch = "wasm32"), feature = "gpu-runtime"))]
143            {
144                *GPU_SOURCE.lock().unwrap_or_else(|e| e.into_inner()) = None;
145            }
146            advance();
147        }
148        OWNER.with(|owner| owner.set(self.previous_owner));
149    }
150}
151pub(crate) fn scoped_kv_slot(index: u32) -> Option<Option<u32>> {
152    KV.lock()
153        .ok()?
154        .as_ref()
155        .map(|kv| kv.kv_slot_for_tensor(index))
156}
157#[allow(dead_code)]
158pub(crate) fn rebuild_kv(prompt: u32, page: u64) -> bool {
159    let Some(source) = source() else {
160        return false;
161    };
162    if !can_publish() {
163        return true;
164    }
165    let mut kv = crate::tensor::kv_provenance::KvProvenanceMap::new();
166    kv.build_prompt_alignment(prompt, source.node_count(), page);
167    *KV.lock().unwrap_or_else(|e| e.into_inner()) = Some(kv);
168    true
169}
170#[allow(dead_code)]
171pub(crate) fn record_kv(tensor: u32, slot: u32, page: u64) -> bool {
172    if source().is_none() {
173        return false;
174    }
175    if can_publish() {
176        if let Some(kv) = KV.lock().unwrap_or_else(|e| e.into_inner()).as_mut() {
177            kv.record(tensor, slot, page);
178        }
179    }
180    true
181}
182#[allow(dead_code)]
183pub(super) fn kv_digest() -> [u8; 32] {
184    use sha2::{Digest, Sha256};
185    let mut h = Sha256::new();
186    h.update(b"qualia:context-kv-provenance:v1\0");
187    if let Some(kv) = KV.lock().unwrap_or_else(|e| e.into_inner()).as_ref() {
188        for i in 0..crate::tensor::kv_provenance::MAX_KV_PROVENANCE as u32 {
189            h.update(kv.kv_slot_for_tensor(i).unwrap_or(u32::MAX).to_le_bytes());
190            h.update(kv.page_id_for_tensor(i).unwrap_or(0).to_le_bytes());
191        }
192    }
193    h.finalize().into()
194}
195pub(super) fn owned_gpu_search(
196    query: &crate::tensor::Tensor10D,
197    distance: f32,
198    out: &mut [usize],
199) -> Option<usize> {
200    #[cfg(all(not(target_arch = "wasm32"), feature = "gpu-runtime"))]
201    {
202        return GPU_SOURCE
203            .lock()
204            .ok()?
205            .as_ref()
206            .map(|gpu| gpu.search(query, distance, out));
207    }
208    #[cfg(not(all(not(target_arch = "wasm32"), feature = "gpu-runtime")))]
209    {
210        let _ = (query, distance, out);
211        None
212    }
213}
214#[allow(dead_code)]
215pub(crate) fn live(identity: u64, digest: [u8; 32]) -> bool {
216    current() == identity && source().is_some_and(|s| s.digest() == digest)
217}