Skip to main content

deq_runtime/
jit.rs

1include!("proto/deq.jit.rs");
2
3pub mod jit_compiler;
4pub mod loss_compiler;
5
6use crate::bin;
7use tokio_util::sync::CancellationToken;
8
9pub async fn static_jit_compile(mut jit_library: JitLibrary) -> bin::Library {
10    let compiler = jit_compiler::JitCompiler::new();
11    let program = std::mem::take(&mut jit_library.program);
12    let token = CancellationToken::new();
13    // copy the port types and gadget types from the JIT library
14    let mut library = bin::Library {
15        metadata: jit_library.metadata.clone(),
16        ..Default::default()
17    };
18    for port_type in jit_library.port_types.iter() {
19        library.port_types.push(port_type.base.as_ref().unwrap().clone());
20    }
21    for gadget_type in jit_library.gadget_types.iter() {
22        library.gadget_types.push(gadget_type.base.as_ref().unwrap().clone());
23    }
24    compiler.load_library(jit_library).await;
25
26    if program.is_empty() {
27        return library;
28    }
29
30    // Check if all gids are pre-assigned (required for parallel compilation)
31    let all_preassigned = program.iter().all(|i| i.gadget.as_ref().is_some_and(|g| g.gid != 0));
32
33    if !all_preassigned || program.len() < 64 {
34        // Fall back to sequential compilation when gids are auto-assigned
35        // or the program is small enough that spawn overhead dominates.
36        return static_jit_compile_sequential(compiler, program, token, library).await;
37    }
38
39    // Parallel compilation: spawn each gadget as a tokio task once its
40    // input dependencies are compiled. Error model futures are spawned
41    // immediately so they can resolve concurrently with later compilations.
42
43    use std::collections::HashMap;
44    use tokio::sync::watch;
45
46    // Create a ready-signal channel for each gid
47    let mut ready_txs: HashMap<u64, watch::Sender<bool>> = HashMap::new();
48
49    struct TaskInfo {
50        idx: usize,
51        instruction: JitInstruction,
52        gid: u64,
53        dep_gids: Vec<u64>,
54    }
55
56    let mut tasks: Vec<TaskInfo> = Vec::with_capacity(program.len());
57    for (idx, instruction) in program.into_iter().enumerate() {
58        let gadget = instruction.gadget.as_ref().unwrap();
59        let gid = gadget.gid;
60        let dep_gids: Vec<u64> = gadget.connectors.iter().map(|c| c.gid).collect();
61        let (tx, _) = watch::channel(false);
62        ready_txs.insert(gid, tx);
63        tasks.push(TaskInfo {
64            idx,
65            instruction,
66            gid,
67            dep_gids,
68        });
69    }
70
71    // Subscribe to all dependency channels BEFORE moving senders into tasks,
72    // so that all senders are still alive during subscription.
73    let mut all_dep_rxs: Vec<Vec<watch::Receiver<bool>>> = Vec::with_capacity(tasks.len());
74    for info in &tasks {
75        let rxs: Vec<watch::Receiver<bool>> = info.dep_gids.iter().map(|d| ready_txs[d].subscribe()).collect();
76        all_dep_rxs.push(rxs);
77    }
78
79    // Spawn compilation tasks
80    let n = tasks.len();
81    let mut handles = Vec::with_capacity(n);
82
83    for (info, dep_rxs) in tasks.into_iter().zip(all_dep_rxs) {
84        let ready_tx = ready_txs.remove(&info.gid).unwrap();
85        let comp = std::sync::Arc::clone(&compiler);
86        let tok = token.clone();
87
88        handles.push(tokio::spawn(async move {
89            // Wait for all input gadgets to finish compiling
90            for mut rx in dep_rxs {
91                let _ = rx.wait_for(|v| *v).await;
92            }
93
94            // Compile gadget (acquires write lock on gadgets, then releases)
95            let (gadget, cmt, cm, error_future) = comp.compile(info.instruction, tok).await;
96
97            // Signal that this gadget is compiled — dependents can proceed,
98            // and input gadgets' error model futures can now resolve
99            let _ = ready_tx.send(true);
100
101            // Spawn error model resolution as a concurrent task so it can
102            // overlap with later gadget compilations on other threads
103            let error_handle = tokio::spawn(error_future);
104
105            (info.idx, gadget, cmt, cm, error_handle)
106        }));
107    }
108
109    // Collect all gadget compilation results
110    let mut results: Vec<_> = futures_util::future::join_all(handles)
111        .await
112        .into_iter()
113        .map(|r| r.unwrap())
114        .collect();
115
116    // Sort by original program index to maintain deterministic output order
117    results.sort_by_key(|(idx, _, _, _, _)| *idx);
118
119    // Build library in program order
120    let mut error_handles = Vec::with_capacity(n);
121    for (_, gadget, cmt, mut cm, error_handle) in results {
122        cm.terminal_error_model = None;
123        library.program.push(bin::Instruction {
124            create: Some(bin::instruction::Create::Gadget(gadget)),
125        });
126        library.check_model_types.push(cmt);
127        library.program.push(bin::Instruction {
128            create: Some(bin::instruction::Create::CheckModel(cm)),
129        });
130        error_handles.push(error_handle);
131    }
132
133    // Wait for all error model resolutions
134    let error_results = futures_util::future::join_all(error_handles).await;
135    for result in error_results {
136        let (error_model_type, error_model) = result.unwrap();
137        library.error_model_types.push(error_model_type);
138        library.program.push(bin::Instruction {
139            create: Some(bin::instruction::Create::ErrorModel(error_model)),
140        });
141    }
142
143    library
144}
145
146/// Sequential fallback for when gids are not pre-assigned.
147async fn static_jit_compile_sequential(
148    compiler: std::sync::Arc<jit_compiler::JitCompiler>,
149    program: Vec<JitInstruction>,
150    token: CancellationToken,
151    mut library: bin::Library,
152) -> bin::Library {
153    let mut error_model_futures = vec![];
154    for instruction in program {
155        let (gadget, check_model_type, mut check_model, error_model_future) =
156            compiler.compile(instruction, token.clone()).await;
157        check_model.terminal_error_model = None;
158        error_model_futures.push(error_model_future);
159        library.program.push(bin::Instruction {
160            create: Some(bin::instruction::Create::Gadget(gadget)),
161        });
162        library.check_model_types.push(check_model_type);
163        library.program.push(bin::Instruction {
164            create: Some(bin::instruction::Create::CheckModel(check_model)),
165        });
166    }
167    let error_models = futures_util::future::join_all(error_model_futures).await;
168    for (error_model_type, error_model) in error_models {
169        library.error_model_types.push(error_model_type);
170        library.program.push(bin::Instruction {
171            create: Some(bin::instruction::Create::ErrorModel(error_model)),
172        });
173    }
174    library
175}
176
177/// input the serialized JitLibrary, output the serialized Library
178///
179/// Releases the GIL during compilation and periodically polls
180/// ``Python::check_signals`` from the main Python thread so that Ctrl+C
181/// (``KeyboardInterrupt``) is honored: Rust/PyO3 bindings are not
182/// naturally interruptible — a long running native call keeps the GIL
183/// and blocks Python's signal handler from running, so a hang or
184/// expensive compile would otherwise be uninterruptible by the user.
185///
186/// ``PyErr_CheckSignals`` only processes signal handlers when invoked
187/// from the main interpreter thread, so the actual compilation runs on
188/// a background OS thread while this function (still on the main
189/// thread) periodically wakes up to check for pending signals.  When a
190/// signal is detected, the cancellation token is fired so the tokio
191/// tasks driving ``static_jit_compile`` unblock and the worker thread
192/// exits.
193#[cfg(feature = "python_binding")]
194#[pyo3::pyfunction]
195#[pyo3(name="static_jit_compile", signature = (jit_library))]
196pub fn py_static_jit_compile(py: pyo3::Python<'_>, jit_library: Vec<u8>) -> pyo3::PyResult<Vec<u8>> {
197    use prost::Message;
198    use std::sync::mpsc::{RecvTimeoutError, sync_channel};
199    use std::time::Duration;
200    let jit_library = JitLibrary::decode(&*jit_library).unwrap();
201
202    let cancel = CancellationToken::new();
203    let cancel_for_worker = cancel.clone();
204    let (tx, rx) = sync_channel::<bin::Library>(1);
205    let worker = std::thread::spawn(move || {
206        let rt = tokio::runtime::Runtime::new().unwrap();
207        let library = rt.block_on(async move {
208            tokio::select! {
209                library = static_jit_compile(jit_library) => Some(library),
210                _ = cancel_for_worker.cancelled() => None,
211            }
212        });
213        if let Some(library) = library {
214            let _ = tx.send(library);
215        }
216        // Drop the runtime to abort any still-running tasks (e.g.
217        // dependency-wait awaits in the parallel path that don't
218        // observe the cancellation token directly).
219        drop(rt);
220    });
221
222    let outcome = loop {
223        // ``recv_timeout`` blocks the calling thread (this is the main
224        // Python thread, holding the GIL) for up to 50ms.  Holding the
225        // GIL for that long is acceptable: ``check_signals`` requires
226        // the main thread anyway, the cadence is short enough that
227        // Ctrl+C still feels responsive, and no other Python thread can
228        // make progress while the JIT runtime is the only thing
229        // happening regardless.
230        match rx.recv_timeout(Duration::from_millis(50)) {
231            Ok(library) => break Ok(library),
232            Err(RecvTimeoutError::Disconnected) => {
233                // Worker exited without sending a result — it must have
234                // observed cancellation. Surface the pending signal
235                // (typically ``KeyboardInterrupt``) if any.
236                py.check_signals()?;
237                break Err(pyo3::exceptions::PyRuntimeError::new_err(
238                    "static_jit_compile worker exited unexpectedly",
239                ));
240            }
241            Err(RecvTimeoutError::Timeout) => {
242                if let Err(err) = py.check_signals() {
243                    cancel.cancel();
244                    // Drain any final result the worker may have
245                    // produced before observing the cancel.
246                    let _ = rx.recv();
247                    break Err(err);
248                }
249            }
250        }
251    };
252
253    let _ = worker.join();
254
255    let library = outcome?;
256    let mut buf = Vec::with_capacity(library.encoded_len());
257    library.encode(&mut buf).unwrap();
258    Ok(buf)
259}
260
261#[cfg(test)]
262mod tests {
263    use super::*;
264
265    #[tokio::test]
266    async fn static_compile_preserves_metadata() {
267        let metadata = prost_types::Struct {
268            fields: [(
269                "source".to_string(),
270                prost_types::Value {
271                    kind: Some(prost_types::value::Kind::StringValue("unit-test".to_string())),
272                },
273            )]
274            .into(),
275        };
276        let library = static_jit_compile(JitLibrary {
277            metadata: Some(metadata.clone()),
278            ..Default::default()
279        })
280        .await;
281
282        assert_eq!(library.metadata, Some(metadata));
283    }
284}