oxionnx 0.1.3

Pure Rust ONNX inference engine — zero C/C++ dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use crate::graph::OpKind;
use crate::memory::SizeClassPool;
use crate::tensor::Tensor;
use crate::OnnxError;
use oxionnx_core::{OpContext, Operator};
use std::collections::HashMap;
use std::sync::Mutex;

use super::super::types::NodeProfile;
use super::super::Session;
use super::state::SessionRunState;

#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;

impl Session {
    /// Try to dispatch a single node through the hardware-acceleration hierarchy
    /// (CUDA → DirectML → wgpu GPU).  Returns `Ok(true)` if a backend handled the
    /// node and wrote its outputs into `state`; `Ok(false)` signals that no
    /// accelerator claimed the node and the caller must fall back to CPU.
    ///
    /// This mirrors the dispatch order in `run_sequential_inner` so that the
    /// parallel and sequential paths are consistent.
    #[cfg(not(target_arch = "wasm32"))]
    fn try_accelerated_node(
        &self,
        node: &crate::graph::Node,
        state: &mut SessionRunState,
        ref_counts: &mut HashMap<String, usize>,
        output_set: &std::collections::HashSet<&str>,
        resolved: &HashMap<String, Vec<usize>>,
    ) -> Result<bool, OnnxError> {
        let pool = self.pool.as_ref().map(|m| m as &Mutex<SizeClassPool>);

        // ── CUDA ────────────────────────────────────────────────────────────
        #[cfg(feature = "cuda")]
        {
            let try_cuda = self.cuda.is_some()
                && !matches!(
                    self.op_placement,
                    crate::execution_providers::OpPlacement::CpuOnly
                );
            if try_cuda {
                if let Some(cuda_ctx) = &self.cuda {
                    let cuda_start = std::time::Instant::now();
                    match oxionnx_cuda::try_cuda_dispatch(
                        node,
                        &self.weights,
                        state.as_map(),
                        cuda_ctx,
                    ) {
                        Ok(Some(results)) => {
                            let cuda_elapsed = cuda_start.elapsed();
                            if let Some(ref profiling) = self.profiling_data {
                                if let Ok(mut data) = profiling.lock() {
                                    data.push(NodeProfile {
                                        node_name: node.name.clone(),
                                        op_type: node.op.as_str().to_string(),
                                        duration: cuda_elapsed,
                                        output_shapes: results
                                            .iter()
                                            .map(|t| t.shape.clone())
                                            .collect(),
                                    });
                                }
                            }
                            for (name, tensor) in node.outputs.iter().zip(results) {
                                if !name.is_empty() {
                                    state.insert(name.clone(), tensor, pool);
                                }
                            }
                            self.decrement_refs_state(node, state, ref_counts, output_set);
                            return Ok(true);
                        }
                        Ok(None) => {
                            // Op not supported on CUDA — fall through to DirectML/CPU
                        }
                        Err(_e) => {
                            // CUDA dispatch error — fall through gracefully
                            #[cfg(debug_assertions)]
                            tracing::debug!(
                                op = %node.op.as_str(),
                                node = %node.name,
                                err = %_e,
                                "parallel: CUDA dispatch error, falling back",
                            );
                        }
                    }
                }
            }
        }

        // ── DirectML ────────────────────────────────────────────────────────
        #[cfg(feature = "directml")]
        {
            let try_dml = self.dml.is_some()
                && !matches!(
                    self.op_placement,
                    crate::execution_providers::OpPlacement::CpuOnly
                );
            if try_dml {
                if let Some(ctx) = &self.dml {
                    let dml_start = std::time::Instant::now();
                    match oxionnx_directml::try_directml_dispatch(
                        node,
                        &self.weights,
                        state.as_map(),
                        ctx,
                    ) {
                        Ok(Some(results)) => {
                            let dml_elapsed = dml_start.elapsed();
                            if let Some(ref profiling) = self.profiling_data {
                                if let Ok(mut data) = profiling.lock() {
                                    data.push(NodeProfile {
                                        node_name: node.name.clone(),
                                        op_type: node.op.as_str().to_string(),
                                        duration: dml_elapsed,
                                        output_shapes: results
                                            .iter()
                                            .map(|t| t.shape.clone())
                                            .collect(),
                                    });
                                }
                            }
                            for (name, tensor) in node.outputs.iter().zip(results) {
                                if !name.is_empty() {
                                    state.insert(name.clone(), tensor, pool);
                                }
                            }
                            self.decrement_refs_state(node, state, ref_counts, output_set);
                            return Ok(true);
                        }
                        Ok(None) => {
                            // Op not supported by DirectML — fall through to wgpu/CPU
                        }
                        Err(_e) => {
                            #[cfg(debug_assertions)]
                            tracing::debug!(
                                op = %node.op.as_str(),
                                node = %node.name,
                                err = %_e,
                                "parallel: DirectML dispatch error, falling back",
                            );
                        }
                    }
                }
            }
        }

        // ── wgpu GPU ────────────────────────────────────────────────────────
        #[cfg(feature = "gpu")]
        {
            use super::super::gpu_dispatch::{try_gpu_dispatch, GpuExecutionProvider};
            use crate::execution_providers::ProviderKind;

            let output_bytes =
                Self::estimate_output_bytes(node, state.as_map(), &self.weights, resolved);
            let placement = crate::execution_providers::decide_placement(
                &node.op,
                output_bytes,
                &self.op_placement,
            );
            if matches!(placement, ProviderKind::Gpu) {
                if let Some(gpu_ctx) = &self.gpu {
                    if let Some(results) =
                        try_gpu_dispatch(node, &self.weights, state.as_map(), gpu_ctx)?
                    {
                        for (name, tensor) in node.outputs.iter().zip(results) {
                            if !name.is_empty() {
                                state.insert(name.clone(), tensor, pool);
                            }
                        }
                        self.decrement_refs_state(node, state, ref_counts, output_set);
                        return Ok(true);
                    }
                    if GpuExecutionProvider::is_supported(node.op.as_str()) {
                        #[cfg(debug_assertions)]
                        tracing::debug!(
                            op = %node.op.as_str(),
                            node = %node.name,
                            "parallel: GPU fallback to CPU",
                        );
                    }
                }
            }
        }

        // Suppress unused-variable warnings when no GPU features are enabled.
        let _ = (node, state, ref_counts, output_set, resolved, pool);
        Ok(false)
    }

    /// Determine whether a node should be routed to hardware acceleration
    /// (CUDA, DirectML, or wgpu GPU) rather than rayon CPU parallelism.
    ///
    /// A node is GPU-eligible when:
    ///   - The `op_placement` is not `CpuOnly`, AND
    ///   - At least one hardware-acceleration backend context is available, AND
    ///   - The op is known to be GPU-capable (wgpu path) or the cuda/dml context
    ///     is present and willing to claim the op at dispatch time.
    ///
    /// GPU nodes within a depth level are serialised deliberately: GPU drivers
    /// queue work on-device and are not thread-safe to call concurrently from
    /// multiple rayon workers.
    #[cfg(not(target_arch = "wasm32"))]
    fn is_gpu_eligible_node(&self, node: &crate::graph::Node) -> bool {
        if matches!(
            self.op_placement,
            crate::execution_providers::OpPlacement::CpuOnly
        ) {
            return false;
        }

        #[cfg(feature = "cuda")]
        if self.cuda.is_some() {
            return true;
        }

        #[cfg(feature = "directml")]
        if self.dml.is_some() {
            return true;
        }

        #[cfg(feature = "gpu")]
        if self.gpu.is_some() && crate::execution_providers::is_gpu_capable(&node.op) {
            return true;
        }

        // Suppress unused-variable warning when no GPU features are enabled.
        let _ = node;
        false
    }

    /// Parallel execution: group nodes by topological depth and execute each
    /// depth level using a hybrid strategy:
    ///
    /// - GPU-eligible nodes (CUDA / DirectML / wgpu) within a depth level are
    ///   executed **serially** in GPU-dispatch order.  This is intentional: GPU
    ///   driver contexts are not safe to call concurrently from multiple rayon
    ///   workers, and on-device queuing already provides hardware parallelism.
    /// - CPU-only nodes within the same depth level are executed **concurrently**
    ///   via rayon's `par_iter()`.
    ///
    /// Note: inplace and slot-write optimisations are active for single-node levels.
    /// For multi-node levels they are intentionally disabled — those paths require
    /// exclusive mutable access to state during the operator call, which serialises
    /// all workers and defeats the purpose of rayon parallelism.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn run_parallel_inner(
        &self,
        state: &mut SessionRunState,
        ref_counts: &mut HashMap<String, usize>,
        output_set: &std::collections::HashSet<&str>,
    ) -> Result<(), OnnxError> {
        let depths = Self::compute_node_depths(&self.sorted_nodes, &self.weights);
        let mut groups = Self::group_by_depth(&depths);

        // Sort nodes within each level by critical-path cost (descending).
        // This ensures the heaviest work starts first, reducing tail latency.
        let critical_costs = crate::optimizer::cost_model::compute_critical_path_costs(
            &self.sorted_nodes,
            self.shape_cache.as_ref(),
        );
        for group in &mut groups {
            group.sort_by(|&a, &b| critical_costs[b].cmp(&critical_costs[a]));
        }

        let resolved = self
            .resolved_shapes
            .lock()
            .map(|s| s.clone())
            .unwrap_or_default();

        for group in &groups {
            if group.is_empty() {
                continue;
            }

            if group.len() == 1 {
                // Single node — try hardware acceleration first, then CPU path.
                let node = &self.sorted_nodes[group[0]];
                if let OpKind::Unknown(_) = &node.op {
                    continue;
                }

                // Try GPU/CUDA/DirectML dispatch; returns true if a backend claimed it.
                if self.try_accelerated_node(node, state, ref_counts, output_set, &resolved)? {
                    continue;
                }

                // No accelerator claimed the node — fall back to CPU dispatch_node
                // (inplace + slot-write optimisations active for single-node levels).
                let op_name = node.op.as_str();
                let operator = self.registry.get(op_name).ok_or_else(|| {
                    OnnxError::UnknownOp(format!("No operator registered for '{}'", op_name))
                })?;

                let elapsed =
                    self.dispatch_node(node, operator, state, ref_counts, output_set, &resolved)?;

                if let Some(ref profiling) = self.profiling_data {
                    if let Ok(mut data) = profiling.lock() {
                        // Collect output shapes from state after dispatch_node wrote them.
                        let output_shapes = node
                            .outputs
                            .iter()
                            .filter(|n| !n.is_empty())
                            .filter_map(|n| state.get(n))
                            .map(|t| t.shape.clone())
                            .collect();
                        data.push(NodeProfile {
                            node_name: node.name.clone(),
                            op_type: node.op.as_str().to_string(),
                            duration: elapsed,
                            output_shapes,
                        });
                    }
                }

                self.decrement_refs_state(node, state, ref_counts, output_set);
            } else {
                // Multiple nodes at this depth — hybrid dispatch:
                //
                //   Phase 1 (serial):   GPU-eligible nodes dispatched one-by-one through the
                //                       hardware acceleration hierarchy.  GPU contexts are not
                //                       thread-safe; serial dispatch is mandatory here.
                //
                //   Phase 2 (parallel): CPU-only nodes dispatched via rayon par_iter.
                //     Read sub-phase:   snapshot inputs (immutable borrow ends before write).
                //     Compute sub-phase: par_iter — no state access, full rayon parallelism.
                //     Write sub-phase:  sequential insert via state.insert (pool-backed).

                let nodes_at_depth: Vec<&crate::graph::Node> =
                    group.iter().map(|&i| &self.sorted_nodes[i]).collect();

                // ── Phase 1: serial GPU dispatch ─────────────────────────────
                let mut cpu_only_nodes: Vec<&crate::graph::Node> =
                    Vec::with_capacity(nodes_at_depth.len());

                for node in &nodes_at_depth {
                    if let OpKind::Unknown(_) = &node.op {
                        continue;
                    }
                    if self.is_gpu_eligible_node(node) {
                        // Try hardware acceleration; on miss fall through to CPU bucket.
                        if self
                            .try_accelerated_node(node, state, ref_counts, output_set, &resolved)?
                        {
                            continue;
                        }
                    }
                    // Either not GPU-eligible or no backend claimed it.
                    cpu_only_nodes.push(node);
                }

                // ── Phase 2: parallel CPU dispatch ───────────────────────────
                // Collect operators and pre-resolve inputs (read-only snapshot).
                let work_items: Vec<(&crate::graph::Node, &dyn Operator, Vec<Option<&Tensor>>)> =
                    cpu_only_nodes
                        .iter()
                        .filter(|n| !matches!(n.op, OpKind::Unknown(_)))
                        .map(|n| {
                            let op = self.registry.get(n.op.as_str()).ok_or_else(|| {
                                OnnxError::UnknownOp(format!(
                                    "No operator registered for '{}'",
                                    n.op.as_str()
                                ))
                            });
                            let inputs: Vec<Option<&Tensor>> = n
                                .inputs
                                .iter()
                                .map(|name| {
                                    if name.is_empty() {
                                        None
                                    } else {
                                        state.get(name).or_else(|| self.weights.get(name))
                                    }
                                })
                                .collect();
                            op.map(|o| (*n, o, inputs))
                        })
                        .collect::<Result<Vec<_>, _>>()?;

                // Execute in parallel — each produces (node_name, results, duration).
                type ParResult<'a> = Result<(&'a str, Vec<Tensor>, std::time::Duration), OnnxError>;
                let par_execute = || -> Vec<ParResult<'_>> {
                    work_items
                        .par_iter()
                        .map(|(node, operator, inputs)| {
                            let ctx = OpContext {
                                node,
                                inputs: inputs.clone(),
                                outer_scope: None,
                                registry: None,
                            };
                            let start = std::time::Instant::now();
                            let res = operator.execute(&ctx)?;
                            let elapsed = start.elapsed();
                            Ok((node.name.as_str(), res, elapsed))
                        })
                        .collect()
                };
                let par_results: Vec<ParResult<'_>> = if let Some(ref pool) = self.thread_pool {
                    pool.install(par_execute)
                } else {
                    par_execute()
                };

                // Write phase: insert all CPU outputs sequentially via state (pool-backed release).
                let pool = self.pool.as_ref().map(|m| m as &Mutex<SizeClassPool>);
                for result in par_results {
                    let (node_name, tensors, elapsed) = result?;
                    if let Some(node) = cpu_only_nodes.iter().find(|n| n.name == node_name) {
                        if let Some(ref profiling) = self.profiling_data {
                            if let Ok(mut data) = profiling.lock() {
                                data.push(NodeProfile {
                                    node_name: node.name.clone(),
                                    op_type: node.op.as_str().to_string(),
                                    duration: elapsed,
                                    output_shapes: tensors
                                        .iter()
                                        .map(|t| t.shape.clone())
                                        .collect(),
                                });
                            }
                        }
                        for (name, tensor) in node.outputs.iter().zip(tensors) {
                            if !name.is_empty() {
                                state.insert(name.clone(), tensor, pool);
                            }
                        }
                    }
                }

                // Decrement ref counts for all nodes in this group via state.
                // GPU nodes were already decremented inside try_accelerated_node;
                // CPU-only nodes are decremented here after the write phase.
                for node in &cpu_only_nodes {
                    self.decrement_refs_state(node, state, ref_counts, output_set);
                }
            }
        }
        Ok(())
    }

    /// Fallback on wasm32: parallel is not supported, delegate to sequential.
    #[cfg(target_arch = "wasm32")]
    pub(crate) fn run_parallel_inner(
        &self,
        state: &mut SessionRunState,
        ref_counts: &mut HashMap<String, usize>,
        output_set: &std::collections::HashSet<&str>,
    ) -> Result<(), OnnxError> {
        self.run_sequential_inner(state, ref_counts, output_set)
    }
}