onnx-runtime-ep-api 0.1.0-dev.5

Execution Provider API for the ORT 2.0 runtime: ExecutionProvider and Kernel traits, OpRegistry, and the ORT graph ABI bridge
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
//! The [`Kernel`] trait and kernel-match / cost types (§4.2).

use std::borrow::Cow;

use onnx_runtime_ir::TensorLayout;

use crate::error::Result;
use crate::tensor::{TensorMut, TensorView};
use crate::weight::WeightHandle;

/// A cost estimate for running a kernel, consumed by the placement cost model
/// (`docs/ORT2.md` §6). All time fields are in **microseconds**; a fuller model
/// (roofline, calibration) lands in `onnx-runtime-cost-model` (Phase 2).
///
/// The struct is `#[non_exhaustive]`: the Phase-2 cost model may add fields
/// (e.g. energy, occupancy) without breaking EP crates. Construct it via
/// [`Cost::ZERO`], [`Cost::new`], or the `with_*` builders rather than a struct
/// literal so those additions stay source-compatible.
///
/// The three time components (`compute_us`, `memory_us`, `transfer_us`) map onto
/// the design's *compute*, *memory-traffic*, and *layout/transfer* estimates;
/// `launch_us` captures fixed dispatch latency (§6.2 `launch_overhead`) and
/// `bytes_moved` carries the raw memory-traffic figure a roofline model needs
/// (§6.3, mirroring the design `Cost::memory_bytes`).
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct Cost {
    /// Estimated compute time (µs).
    pub compute_us: f64,
    /// Estimated memory-traffic time (µs).
    pub memory_us: f64,
    /// Estimated layout-conversion / cross-device copy time at boundaries (µs).
    pub transfer_us: f64,
    /// Fixed kernel-launch / dispatch latency (µs), independent of size.
    pub launch_us: f64,
    /// Estimated bytes of memory traffic (for roofline / bandwidth models).
    pub bytes_moved: u64,
}

impl Cost {
    /// The zero cost (free op).
    pub const ZERO: Cost = Cost {
        compute_us: 0.0,
        memory_us: 0.0,
        transfer_us: 0.0,
        launch_us: 0.0,
        bytes_moved: 0,
    };

    /// A cost from its three time components; `launch_us` and `bytes_moved`
    /// default to zero (set them via the builders).
    pub fn new(compute_us: f64, memory_us: f64, transfer_us: f64) -> Self {
        Self {
            compute_us,
            memory_us,
            transfer_us,
            ..Self::ZERO
        }
    }

    /// Set the fixed launch/dispatch latency.
    pub fn with_launch_us(mut self, launch_us: f64) -> Self {
        self.launch_us = launch_us;
        self
    }

    /// Set the estimated memory-traffic volume.
    pub fn with_bytes_moved(mut self, bytes_moved: u64) -> Self {
        self.bytes_moved = bytes_moved;
        self
    }

    /// Total estimated wall time (µs): the sum of all time components.
    pub fn total_us(&self) -> f64 {
        self.compute_us + self.memory_us + self.transfer_us + self.launch_us
    }
}

/// Result of [`crate::ExecutionProvider::supports_op`].
///
/// A decline reason travels with the decision that produced it. EPs must not
/// maintain a separate reason table: colocating the reason with `Unsupported`
/// keeps diagnostics from drifting away from the actual claim predicate.
pub enum KernelMatch {
    Supported {
        cost: Cost,
        /// Layouts the kernel requires for each input, if constrained.
        required_input_layouts: Option<Vec<TensorLayout>>,
        /// Layouts the kernel produces for each output.
        output_layouts: Vec<TensorLayout>,
    },
    Unsupported {
        /// Actionable explanation of what the EP accepts or how to fix fallback.
        reason: Cow<'static, str>,
    },
}

impl KernelMatch {
    /// Construct an unsupported match with its actionable decline reason.
    pub fn unsupported(reason: impl Into<Cow<'static, str>>) -> Self {
        Self::Unsupported {
            reason: reason.into(),
        }
    }

    /// Whether the op is supported.
    pub fn is_supported(&self) -> bool {
        matches!(self, KernelMatch::Supported { .. })
    }

    /// The EP's decline reason, or `None` when the op is supported.
    pub fn reason(&self) -> Option<&str> {
        match self {
            Self::Supported { .. } => None,
            Self::Unsupported { reason } => Some(reason),
        }
    }
}

/// Whether a compiled kernel can participate in device-graph capture.
///
/// As with [`KernelMatch`], a decline reason travels with the decision that
/// produced it. EPs must not maintain a separate reason table: capture
/// eligibility is often shape-, dtype-, warmup-, or implementation-dependent,
/// so separating the reason from the predicate would let diagnostics drift.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CaptureSupport {
    /// The kernel's warmed execution path satisfies the device-graph contract.
    Supported,
    /// The kernel cannot currently be captured.
    Unsupported {
        /// Actionable explanation of the failed capture precondition.
        reason: Cow<'static, str>,
    },
}

impl CaptureSupport {
    /// Construct an unsupported result with its actionable decline reason.
    pub fn unsupported(reason: impl Into<Cow<'static, str>>) -> Self {
        Self::Unsupported {
            reason: reason.into(),
        }
    }

    /// Whether the kernel can currently participate in device-graph capture.
    pub fn is_supported(&self) -> bool {
        matches!(self, Self::Supported)
    }

    /// The kernel's capture-decline reason, or `None` when capture is supported.
    pub fn reason(&self) -> Option<&str> {
        match self {
            Self::Supported => None,
            Self::Unsupported { reason } => Some(reason),
        }
    }
}

/// The concrete implementation selected by a kernel's internal dispatcher.
///
/// Unlike [`KernelMatch`] (the EP's claim over a node) and [`CaptureSupport`]
/// (graph-capture eligibility), this records **which** implementation ran for an
/// already-claimed node and **why** the dispatch predicate chose it. A single
/// claimed op (e.g. `MatMulNBits`) can pick materially different kernels for the
/// same shape family, so node-level claims alone do not explain what executed.
/// The reason travels with the variant so a trace explains both.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct KernelVariantSelection {
    /// Stable implementation name suitable for filtering and aggregation.
    pub variant: &'static str,
    /// Human-readable explanation of the dispatch predicate that selected it.
    pub reason: Cow<'static, str>,
}

impl KernelVariantSelection {
    /// Construct a selected variant with its dispatch reason.
    pub fn new(variant: &'static str, reason: impl Into<Cow<'static, str>>) -> Self {
        Self {
            variant,
            reason: reason.into(),
        }
    }
}

/// Trace-argument key carrying the selected kernel implementation.
pub const ARG_KERNEL_VARIANT: &str = "kernel_variant";
/// Trace-argument key carrying why the kernel variant was selected.
pub const ARG_KERNEL_VARIANT_REASON: &str = "kernel_variant_reason";
/// Trace-argument key naming the device a kernel ran on (`cpu`, `cuda`, ...).
pub const ARG_DEVICE: &str = "device";
/// Trace-argument key carrying bytes moved by a kernel (inputs plus outputs).
pub const ARG_BYTES: &str = "bytes";
/// Trace-argument key carrying a kernel's floating-point operation count.
pub const ARG_FLOPS: &str = "flops";
/// Trace category for a span covering one worker's slice of a fanned-out node.
pub const CAT_KERNEL_WORKER: &str = "op.worker";

/// Record the standard per-kernel metrics on the active executor op-span.
///
/// This is the one implementation every execution provider shares, so a trace
/// means the same thing whichever provider produced it: `device` names where
/// the kernel ran, `bytes` is inputs plus outputs (absent inputs excluded), and
/// `flops` is the provider's own estimate. Providers previously each owned a
/// private copy of this, which is how the CUDA provider ended up recording
/// nothing at all.
///
/// `flops` is a closure so the estimate is never computed on the untraced path.
#[inline]
pub fn record_kernel_metrics(
    device: &'static str,
    inputs: &[TensorView<'_>],
    outputs: &[TensorMut<'_>],
    flops: impl FnOnce() -> u64,
) {
    if !kernel_variant_tracing_enabled() {
        return;
    }
    onnx_runtime_tracer::annotate_current_span_with(|| {
        let input_bytes = inputs
            .iter()
            .filter(|input| !input.is_absent())
            .fold(0_u64, |total, input| {
                total.saturating_add(input.byte_size() as u64)
            });
        let bytes = outputs.iter().fold(input_bytes, |total, output| {
            total.saturating_add(output.byte_size() as u64)
        });
        onnx_runtime_tracer::Args::new()
            .with(ARG_DEVICE, device)
            .with(ARG_BYTES, bytes)
            .with(ARG_FLOPS, flops())
    });
}

/// Open a span covering one worker's slice of a node that was fanned out across
/// a thread pool or stream, returning `None` when it should not be recorded.
///
/// The executor's op-span lives on the thread that dispatched the node, and
/// thread-local span state does not follow work onto a pool's threads. A worker
/// must therefore open its own span, which is what this does: it lands on that
/// worker's own trace lane and shows the parallel decomposition that the single
/// op-span flattens away.
///
/// Two things keep this affordable. It is gated at [`TraceVerbosity::Full`], so
/// the default op-level trace is unaffected; and a span costs a few hundred
/// nanoseconds, which is noise against a slice of a node but *not* against an
/// inner loop iteration. **Wrap a worker's whole chunk of work, never an inner
/// loop.**
///
/// `label` should identify the partitioned work (typically the op type), not
/// the worker: the worker is already identified by the lane the span lands on.
#[must_use]
#[inline]
pub fn kernel_worker_span(label: &'static str) -> Option<onnx_runtime_tracer::SpanGuard> {
    let context = onnx_runtime_tracer::global_context()?;
    if !context
        .verbosity()
        .includes(onnx_runtime_tracer::TraceVerbosity::Full)
    {
        return None;
    }
    Some(context.span(label, CAT_KERNEL_WORKER))
}

/// Whether kernel-variant trace annotations would currently be recorded.
///
/// This is true only when an enabled executor op-span is active on the current
/// thread, so callers can guard shape-dependent reason formatting behind it and
/// keep the disabled dispatch path allocation-free. It is the cheap thread-local
/// check that closes the "dead write" gap: without a live span, an annotation
/// has nothing to attach to, so there is no point formatting a reason.
#[must_use]
#[inline]
pub fn kernel_variant_tracing_enabled() -> bool {
    onnx_runtime_tracer::tracing_active()
}

/// Record a selected kernel variant on the active runtime trace span.
///
/// The annotation enriches the per-op span the executor opens for a traced run,
/// so it carries the node identity already. Callers should normally use
/// [`record_kernel_variant!`] so formatted reasons are built only when a span is
/// active.
#[inline]
pub fn record_kernel_variant_selection(selection: &KernelVariantSelection) {
    if !kernel_variant_tracing_enabled() {
        return;
    }
    onnx_runtime_tracer::annotate_current_span_with(|| {
        onnx_runtime_tracer::Args::new()
            .with(ARG_KERNEL_VARIANT, selection.variant)
            .with(ARG_KERNEL_VARIANT_REASON, selection.reason.as_ref())
    });
}

/// Record a selected kernel variant for a named *sub-decision* of the current
/// op.
///
/// A single claimed node can make several independent kernel-path choices in
/// sequence (e.g. `GroupQueryAttention` picks a prep-fusion path *and* an
/// attention-backend path). Recording each under the shared
/// [`ARG_KERNEL_VARIANT`] key would let a later choice overwrite an earlier one
/// on the same span, so each sub-decision is namespaced by `stage` into
/// `kernel_variant.<stage>` / `kernel_variant_reason.<stage>`. Use the plain
/// [`record_kernel_variant_selection`] for the node's terminal/primary variant.
#[inline]
pub fn record_kernel_variant_stage_selection(stage: &str, selection: &KernelVariantSelection) {
    if !kernel_variant_tracing_enabled() {
        return;
    }
    let variant_key = format!("{ARG_KERNEL_VARIANT}.{stage}");
    let reason_key = format!("{ARG_KERNEL_VARIANT_REASON}.{stage}");
    onnx_runtime_tracer::annotate_current_span_with(|| {
        onnx_runtime_tracer::Args::new()
            .with(variant_key, selection.variant)
            .with(reason_key, selection.reason.as_ref())
    });
}

/// Record a selected kernel implementation and lazily formatted reason on the
/// active executor op-span.
///
/// Formatting is skipped entirely unless a span is active
/// ([`kernel_variant_tracing_enabled`]), so instrumented dispatch sites stay
/// allocation-free on the hot decode path when tracing is off.
#[macro_export]
macro_rules! record_kernel_variant {
    ($variant:expr, $($arg:tt)+) => {{
        if $crate::kernel_variant_tracing_enabled() {
            let selection = $crate::KernelVariantSelection::new(
                $variant,
                ::std::format!($($arg)+),
            );
            $crate::record_kernel_variant_selection(&selection);
        }
    }};
}

/// Record a named *sub-decision* kernel variant on the active executor op-span.
///
/// Like [`record_kernel_variant!`] but namespaces the annotation under `$stage`
/// so multiple kernel-path choices made while executing one node do not clobber
/// each other. Formatting is skipped entirely unless a span is active.
#[macro_export]
macro_rules! record_kernel_variant_stage {
    ($stage:expr, $variant:expr, $($arg:tt)+) => {{
        if $crate::kernel_variant_tracing_enabled() {
            let selection = $crate::KernelVariantSelection::new(
                $variant,
                ::std::format!($($arg)+),
            );
            $crate::record_kernel_variant_stage_selection($stage, &selection);
        }
    }};
}

/// Decline the current capture-support query with an actionable reason.
///
/// Formatting is evaluated only on the decline path.
#[macro_export]
macro_rules! decline_capture {
    ($($arg:tt)+) => {
        return $crate::CaptureSupport::unsupported(format!($($arg)+))
    };
}

/// Require a capture precondition, declining with an actionable reason when false.
///
/// Formatting is evaluated only when the condition fails.
#[macro_export]
macro_rules! require_capture {
    ($condition:expr, $($arg:tt)+) => {
        if !$condition {
            $crate::decline_capture!($($arg)+);
        }
    };
}

/// Decline the current `supports_op` call with an actionable reason.
///
/// Formatting is evaluated only on the decline path.
#[macro_export]
macro_rules! deny {
    ($($arg:tt)+) => {
        return $crate::KernelMatch::unsupported(format!($($arg)+))
    };
}

/// Require a claim condition, declining with an actionable reason when false.
///
/// Formatting is evaluated only when the condition fails, keeping the hot
/// supported path allocation-free.
#[macro_export]
macro_rules! require {
    ($condition:expr, $($arg:tt)+) => {
        if !$condition {
            $crate::deny!($($arg)+);
        }
    };
}

/// A zero-copy **view output**: a kernel's declaration that one of its outputs
/// is a strided view aliasing one of its inputs' buffers, rather than freshly
/// computed bytes (`docs/ORT2.md` §5.4, lazy PyTorch-style views).
///
/// The `shape` / `strides` / `byte_offset` describe the output tensor relative
/// to the **same base pointer** as the referenced input view (i.e. relative to
/// the input's backing allocation, honoring any offset that input itself
/// already carried). Strides are in **elements** and may be negative (DLPack).
/// The executor records this metadata against the output value and does **not**
/// allocate a buffer or invoke the compute path for that slot; the source
/// buffer is kept alive until the view's consumers have run.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ViewOutput {
    /// Positional index (into the kernel's `inputs`) of the aliased input.
    pub input_index: usize,
    /// Output shape.
    pub shape: Vec<usize>,
    /// Output element strides relative to the aliased input's base pointer.
    pub strides: Vec<i64>,
    /// Byte offset of the output element origin from the aliased input's base.
    pub byte_offset: usize,
}

/// An executor-delivered kernel input. Existing EPs receive `Tensor` variants;
/// an EP advertising the `nxrt` capability may receive a lazy `Weight` at the
/// `pkg.nxrt::BlockQuantizedMoE` boundary.
pub enum KernelInput<'a> {
    Tensor(TensorView<'a>),
    Weight(&'a WeightHandle),
}

impl<'a> KernelInput<'a> {
    pub fn tensor(&self) -> Option<&TensorView<'a>> {
        match self {
            Self::Tensor(view) => Some(view),
            Self::Weight(_) => None,
        }
    }

    pub fn weight(&self) -> Option<&WeightHandle> {
        match self {
            Self::Tensor(_) => None,
            Self::Weight(weight) => Some(weight),
        }
    }
}

/// A kernel ready to execute a specific op with specific shapes (§4.2).
pub trait Kernel: Send {
    /// Tell the kernel which positional inputs are immutable graph constants.
    ///
    /// The session calls this exactly once, immediately after construction.
    /// Kernels may use it to prepack or memoize those inputs. Runtime inputs must
    /// never be marked constant: caching them would return stale results.
    fn set_constant_inputs(&mut self, constant_inputs: &[bool]) {
        let _ = constant_inputs;
    }

    /// Execute over device-resident inputs/outputs.
    fn execute(&self, inputs: &[TensorView], outputs: &mut [TensorMut]) -> Result<()>;

    /// Execute through the general weight-delivery seam.
    ///
    /// The default adapter accepts only resident tensor inputs and forwards to
    /// [`Kernel::execute`], so existing EPs compile and behave identically.
    /// Paging-aware kernels override this method to consume lazy handles.
    fn execute_with_inputs(
        &self,
        inputs: &[KernelInput<'_>],
        outputs: &mut [TensorMut],
    ) -> Result<()> {
        let views = inputs
            .iter()
            .map(|input| {
                input.tensor().copied().ok_or_else(|| {
                    crate::EpError::KernelFailed(
                        "kernel received a lazy WeightHandle without implementing \
                         execute_with_inputs"
                            .into(),
                    )
                })
            })
            .collect::<Result<Vec<_>>>()?;
        self.execute(&views, outputs)
    }

    /// Estimated FLOPs, if known (for the cost model).
    fn estimated_flops(&self) -> Option<u64> {
        None
    }

    /// Attempt to express this node's outputs as zero-copy [`ViewOutput`]s over
    /// its inputs instead of computing bytes (the layout/movement-op fast path).
    ///
    /// `inputs` carries the real (possibly already-strided) input views and
    /// `num_outputs` is the node's output arity. Returning:
    /// * `None` — the default — means "compute normally": the executor allocates
    ///   output buffers and calls [`Kernel::execute`].
    /// * `Some(specs)` means every output is a view; `specs.len()` MUST equal
    ///   `num_outputs`. A kernel that can view some but not all outputs must
    ///   return `None` (all-or-nothing) so correctness never regresses.
    ///
    /// When `Some` is returned, [`Kernel::execute`] is **not** invoked.
    fn view_outputs(&self, inputs: &[TensorView], num_outputs: usize) -> Option<Vec<ViewOutput>> {
        let _ = (inputs, num_outputs);
        None
    }

    /// Whether the kernel accepts a non-contiguous (strided) input at `idx`.
    fn supports_strided_input(&self, input_idx: usize) -> bool {
        let _ = input_idx;
        false
    }

    /// The layout the kernel writes most efficiently, if it has a preference.
    fn preferred_output_layout(&self) -> Option<TensorLayout> {
        None
    }

    /// Whether this kernel can participate in device-graph capture.
    ///
    /// The provided default is supported because capture is a runtime/EP
    /// capability: kernels that need restrictions override this method and
    /// return the exact failed precondition.
    fn capture_support(&self) -> CaptureSupport {
        CaptureSupport::Supported
    }

    /// Compatibility shim for existing CUDA-graph callers.
    ///
    /// New capture gates must use [`Kernel::capture_support`] so a decline
    /// reason is never discarded.
    fn cuda_graph_compatible(&self) -> bool {
        self.capture_support().is_supported()
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};

    use super::*;
    use crate::{DeviceId, DevicePtr};

    #[test]
    fn cost_zero_and_total() {
        assert_eq!(Cost::ZERO.total_us(), 0.0);
        let c = Cost::new(10.0, 5.0, 2.0);
        assert_eq!(c.total_us(), 17.0);
        assert_eq!(c.bytes_moved, 0);
        assert_eq!(c.launch_us, 0.0);
    }

    #[test]
    fn cost_builders_are_additive() {
        let c = Cost::new(10.0, 5.0, 2.0)
            .with_launch_us(3.0)
            .with_bytes_moved(4096);
        // launch time folds into the total; bytes_moved is metadata for roofline.
        assert_eq!(c.total_us(), 20.0);
        assert_eq!(c.bytes_moved, 4096);
    }

    #[test]
    fn kernel_match_reports_support() {
        let supported = KernelMatch::Supported {
            cost: Cost::new(1.0, 0.0, 0.0),
            required_input_layouts: None,
            output_layouts: vec![],
        };
        assert!(supported.is_supported());
        assert_eq!(supported.reason(), None);

        let unsupported = KernelMatch::unsupported("test EP supports no ops");
        assert!(!unsupported.is_supported());
        assert_eq!(unsupported.reason(), Some("test EP supports no ops"));
    }

    fn require_positive(value: i32) -> KernelMatch {
        require!(value > 0, "value must be positive, got {value}");
        KernelMatch::Supported {
            cost: Cost::ZERO,
            required_input_layouts: None,
            output_layouts: vec![],
        }
    }

    #[test]
    fn require_macro_carries_formatted_decline_reason() {
        let rejected = require_positive(-2);
        assert_eq!(rejected.reason(), Some("value must be positive, got -2"));
        assert!(require_positive(2).is_supported());
    }

    #[test]
    fn kernel_variant_selection_carries_winner_and_reason() {
        let selection =
            KernelVariantSelection::new("gemv", "M==1 decode uses the packed int4 GEMV path");
        assert_eq!(selection.variant, "gemv");
        assert_eq!(
            selection.reason,
            "M==1 decode uses the packed int4 GEMV path"
        );
    }

    #[test]
    fn record_kernel_variant_without_active_span_skips_formatting() {
        // No executor span is open here, so `tracing_active()` is false: the
        // macro must not evaluate its format arguments (the dead-write guard).
        let formatted = AtomicBool::new(false);
        record_kernel_variant!("gemv", "{}", {
            formatted.store(true, Ordering::Relaxed);
            "M==1 decode"
        });
        assert!(!formatted.load(Ordering::Relaxed));
    }

    #[test]
    fn record_kernel_variant_annotates_active_span() {
        use onnx_runtime_tracer::TraceContext;
        let (trace, events) = TraceContext::in_memory();
        {
            let _span = trace.span("MatMulNBits", "op");
            record_kernel_variant!(
                "gemv_f16",
                "K={}, N={} chose the vectorized GEMV",
                4864,
                896
            );
        }
        let events = events.events();
        assert_eq!(events.len(), 1);
        let args = events[0].args.as_ref().unwrap();
        assert_eq!(args[ARG_KERNEL_VARIANT], "gemv_f16");
        assert!(
            args[ARG_KERNEL_VARIANT_REASON]
                .as_str()
                .unwrap()
                .contains("K=4864, N=896")
        );
    }

    #[test]
    fn record_kernel_variant_stage_namespaces_multiple_subdecisions() {
        use onnx_runtime_tracer::TraceContext;
        let (trace, events) = TraceContext::in_memory();
        {
            let _span = trace.span("GroupQueryAttention", "op");
            // Two independent sub-decisions on the same node: the staged prep
            // choice must survive the terminal attention-backend choice rather
            // than being overwritten by the shared key.
            record_kernel_variant_stage!("prep", "gqa_prep_fused", "Sq==1, even head_dim");
            record_kernel_variant!("attention_gqa_decode_fp16_splitk", "split-K decode");
        }
        let events = events.events();
        assert_eq!(events.len(), 1);
        let args = events[0].args.as_ref().unwrap();
        assert_eq!(args[ARG_KERNEL_VARIANT], "attention_gqa_decode_fp16_splitk");
        assert_eq!(args["kernel_variant.prep"], "gqa_prep_fused");
        assert!(
            args["kernel_variant_reason.prep"]
                .as_str()
                .unwrap()
                .contains("even head_dim")
        );
    }

    struct DecliningCaptureKernel;

    impl Kernel for DecliningCaptureKernel {
        fn execute(&self, _inputs: &[TensorView], _outputs: &mut [TensorMut]) -> Result<()> {
            Ok(())
        }

        fn capture_support(&self) -> CaptureSupport {
            CaptureSupport::unsupported("per-call workspace allocation is not capturable")
        }
    }

    #[test]
    fn cuda_graph_compatible_shim_matches_capture_support() {
        let supported = LegacyKernel {
            called: Arc::new(AtomicBool::new(false)),
        };
        assert_eq!(
            supported.cuda_graph_compatible(),
            supported.capture_support().is_supported()
        );

        let unsupported = DecliningCaptureKernel;
        assert_eq!(
            unsupported.cuda_graph_compatible(),
            unsupported.capture_support().is_supported()
        );
        assert_eq!(
            unsupported.capture_support().reason(),
            Some("per-call workspace allocation is not capturable")
        );
    }

    struct LegacyKernel {
        called: Arc<AtomicBool>,
    }

    impl Kernel for LegacyKernel {
        fn execute(&self, inputs: &[TensorView], _outputs: &mut [TensorMut]) -> Result<()> {
            assert_eq!(inputs.len(), 1);
            assert_eq!(inputs[0].shape, &[4]);
            self.called.store(true, Ordering::Relaxed);
            Ok(())
        }
    }

    #[test]
    fn legacy_kernel_adapter_receives_the_resident_tensor_path() {
        let called = Arc::new(AtomicBool::new(false));
        let kernel = LegacyKernel {
            called: Arc::clone(&called),
        };
        let bytes = [1u8, 2, 3, 4];
        let shape = [4usize];
        let strides = [1i64];
        let inputs = [KernelInput::Tensor(TensorView::new(
            DevicePtr(bytes.as_ptr().cast()),
            onnx_runtime_ir::DataType::Uint8,
            &shape,
            &strides,
            DeviceId::cpu(),
        ))];

        kernel.execute_with_inputs(&inputs, &mut []).unwrap();
        assert!(called.load(Ordering::Relaxed));
    }
}

#[cfg(test)]
mod trace_standard_tests {
    use super::*;
    use onnx_runtime_tracer::{TraceContext, TraceVerbosity, set_global_context};

    /// Worker spans are the expensive tier and must stay off unless asked for.
    #[test]
    fn worker_span_is_gated_on_full_verbosity() {
        set_global_context(None);
        assert!(
            kernel_worker_span("probe").is_none(),
            "a worker span opened with no ambient context installed"
        );

        let (context, _collector) = TraceContext::in_memory();
        set_global_context(Some(context.with_verbosity(TraceVerbosity::Ops)));
        assert!(
            kernel_worker_span("probe").is_none(),
            "a worker span opened at Ops verbosity, which would put the \
             per-worker cost on every ordinary traced run"
        );

        let (context, collector) = TraceContext::in_memory();
        set_global_context(Some(context.with_verbosity(TraceVerbosity::Full)));
        {
            let span = kernel_worker_span("probe");
            assert!(span.is_some(), "no worker span at Full verbosity");
        }
        let events = collector.events();
        assert_eq!(events.len(), 1, "expected exactly one recorded worker span");
        assert_eq!(events[0].cat, CAT_KERNEL_WORKER);
        assert_eq!(events[0].name, "probe");

        set_global_context(None);
    }
}