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 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 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 return static_jit_compile_sequential(compiler, program, token, library).await;
37 }
38
39 use std::collections::HashMap;
44 use tokio::sync::watch;
45
46 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 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 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 for mut rx in dep_rxs {
91 let _ = rx.wait_for(|v| *v).await;
92 }
93
94 let (gadget, cmt, cm, error_future) = comp.compile(info.instruction, tok).await;
96
97 let _ = ready_tx.send(true);
100
101 let error_handle = tokio::spawn(error_future);
104
105 (info.idx, gadget, cmt, cm, error_handle)
106 }));
107 }
108
109 let mut results: Vec<_> = futures_util::future::join_all(handles)
111 .await
112 .into_iter()
113 .map(|r| r.unwrap())
114 .collect();
115
116 results.sort_by_key(|(idx, _, _, _, _)| *idx);
118
119 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 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
146async 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#[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(rt);
220 });
221
222 let outcome = loop {
223 match rx.recv_timeout(Duration::from_millis(50)) {
231 Ok(library) => break Ok(library),
232 Err(RecvTimeoutError::Disconnected) => {
233 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 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}