onnx-runtime-ep-cuda 0.1.0-dev.6

CUDA execution provider for the ORT 2.0 runtime (Phase 2a: cudarc + cuBLASLt MatMul; custom fused kernels deferred)
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
//! Integer **bitwise** ops (`BitwiseAnd`, `BitwiseOr`, `BitwiseXor`,
//! `BitwiseNot`) and the unsigned **`BitShift`** on the GPU via runtime-compiled
//! (NVRTC) `extern "C"` kernels. This mirrors the CPU EP's coverage
//! (`crates/onnx-runtime-ep-cpu/src/kernels/bitwise.rs` and `bitshift.rs`) so a
//! native CUDA model does not fall back to the CPU for these ops.
//!
//! ## Scope (all limits are actionable errors, never panics — RULES.md #1)
//!
//! * **Bitwise binary** (`BitwiseAnd`, `BitwiseOr`, `BitwiseXor`): two
//!   broadcast-compatible integer inputs of the **same** dtype
//!   (Int8/16/32/64, Uint8/16/32/64) → same-dtype output. NumPy-style
//!   right-aligned broadcasting, reusing the [`super::elementwise`] metadata.
//! * **Bitwise unary** (`BitwiseNot`): one integer input → same-dtype output,
//!   bitwise complement (`~x`).
//! * **BitShift** (`direction` = `LEFT`/`RIGHT`): two broadcast-compatible
//!   **unsigned** integer inputs (Uint8/16/32/64). The shift amount is taken
//!   modulo the exact CPU contract — `checked_shl`/`checked_shr` yields `0` when
//!   the (u32-truncated) amount is `>=` the operand bit width — so the kernel
//!   mirrors `bitshift.rs` bit-for-bit.
//!
//! Each op is one thread-per-element grid-stride kernel (bandwidth-bound).

use std::ffi::c_void;
use std::sync::{Arc, Mutex};

use cudarc::driver::{LaunchConfig, PushKernelArg};

use onnx_runtime_ep_api::{
    DeviceGraphResource, EpError, Kernel, KernelFactory, Result, TensorMut, TensorView,
};
use onnx_runtime_ir::{Attribute, DataType, Node};

use super::elementwise::{
    BroadcastMetadataCache, BroadcastMetadataKey, capture_shape_eligible,
    require_matching_capture_signature,
};
use crate::error::{driver_err, not_implemented};
use crate::runtime::{CudaRuntime, cuptr};

/// Threads per block for the 1-D grids (a full warp-multiple block).
const BLOCK: u32 = 256;

/// Grid dimension for `n` elements at [`BLOCK`] threads, capped so a huge tensor
/// still fits the grid limit (grid-stride kernels still cover every element).
fn grid_for(n: usize) -> u32 {
    const MAX_BLOCKS: usize = 65_535;
    n.div_ceil(BLOCK as usize).clamp(1, MAX_BLOCKS) as u32
}

/// `n` as `u64`, matching the kernels' `unsigned long long` count parameter.
fn count_u64(op: &str, n: usize) -> Result<u64> {
    u64::try_from(n)
        .map_err(|_| EpError::KernelFailed(format!("cuda_ep {op}: {n} elements exceed u64")))
}

/// The NVRTC type token and entry-point suffix for an integer dtype.
fn integer_type_suffix(dtype: DataType) -> Option<(&'static str, &'static str)> {
    Some(match dtype {
        DataType::Int8 => ("signed char", "i8"),
        DataType::Int16 => ("short", "i16"),
        DataType::Int32 => ("int", "i32"),
        DataType::Int64 => ("long long", "i64"),
        DataType::Uint8 => ("unsigned char", "u8"),
        DataType::Uint16 => ("unsigned short", "u16"),
        DataType::Uint32 => ("unsigned int", "u32"),
        DataType::Uint64 => ("unsigned long long", "u64"),
        _ => return None,
    })
}

/// The entry-point suffix for an unsigned integer dtype (BitShift operands).
fn unsigned_suffix(dtype: DataType) -> Option<&'static str> {
    match dtype {
        DataType::Uint8 => Some("u8"),
        DataType::Uint16 => Some("u16"),
        DataType::Uint32 => Some("u32"),
        DataType::Uint64 => Some("u64"),
        _ => None,
    }
}

// ===========================================================================
// Bitwise binary (same integer dtype in/out) — NumPy broadcasting
// ===========================================================================

/// NVRTC source: one `extern "C"` kernel per bitwise op/integer dtype. The
/// output dtype equals the operand dtype (unlike the bool-producing logical
/// ops). `&`/`|`/`^` are bit-level identical on signed and unsigned operands.
const BITWISE_BINARY_SRC: &str = r#"
__device__ __forceinline__ void broadcast_indices(unsigned long long out, const unsigned long long* m, int rank, unsigned long long* ai, unsigned long long* bi) {
    *ai = 0; *bi = 0;
    for (int axis = rank - 1; axis >= 0; --axis) {
        unsigned long long coord = out % m[axis]; out /= m[axis];
        *ai += coord * m[rank + axis]; *bi += coord * m[2 * rank + axis];
    }
}
#define DEFINE_BITWISE(name, type, suffix, expr) \
extern "C" __global__ void name##_##suffix(const type* a, const type* b, type* y, const unsigned long long* m, int rank, const unsigned long long n) { \
    for (unsigned long long i = blockIdx.x*blockDim.x + threadIdx.x; i < n; i += (unsigned long long)gridDim.x * blockDim.x) { \
        unsigned long long ai, bi; broadcast_indices(i, m, rank, &ai, &bi); y[i] = (type)(expr); \
    } \
}
#define DEFINE_BITWISE_FOR_TYPE(type, suffix) \
DEFINE_BITWISE(band, type, suffix, a[ai] & b[bi]) \
DEFINE_BITWISE(bor, type, suffix, a[ai] | b[bi]) \
DEFINE_BITWISE(bxor, type, suffix, a[ai] ^ b[bi])
DEFINE_BITWISE_FOR_TYPE(signed char, i8)
DEFINE_BITWISE_FOR_TYPE(short, i16)
DEFINE_BITWISE_FOR_TYPE(int, i32)
DEFINE_BITWISE_FOR_TYPE(long long, i64)
DEFINE_BITWISE_FOR_TYPE(unsigned char, u8)
DEFINE_BITWISE_FOR_TYPE(unsigned short, u16)
DEFINE_BITWISE_FOR_TYPE(unsigned int, u32)
DEFINE_BITWISE_FOR_TYPE(unsigned long long, u64)
"#;

const BITWISE_BINARY_MODULE: &str = "bitwise_binary_int";

/// NVRTC source: unsigned `BitShift` (LEFT/RIGHT). The shift amount is truncated
/// to `unsigned int` and compared against the operand bit width, matching the
/// CPU `checked_shl`/`checked_shr(amount as u32)` contract exactly (an amount
/// `>=` the width yields `0`). Small-type operands promote to `int` for the
/// shift, so the `(type)` store reproduces the CPU wrapping narrow.
const BITSHIFT_SRC: &str = r#"
__device__ __forceinline__ void broadcast_indices(unsigned long long out, const unsigned long long* m, int rank, unsigned long long* ai, unsigned long long* bi) {
    *ai = 0; *bi = 0;
    for (int axis = rank - 1; axis >= 0; --axis) {
        unsigned long long coord = out % m[axis]; out /= m[axis];
        *ai += coord * m[rank + axis]; *bi += coord * m[2 * rank + axis];
    }
}
#define DEFINE_SHIFT(name, type, suffix, bits, op) \
extern "C" __global__ void name##_##suffix(const type* a, const type* b, type* y, const unsigned long long* m, int rank, const unsigned long long n) { \
    for (unsigned long long i = blockIdx.x*blockDim.x + threadIdx.x; i < n; i += (unsigned long long)gridDim.x * blockDim.x) { \
        unsigned long long ai, bi; broadcast_indices(i, m, rank, &ai, &bi); \
        unsigned int amount = (unsigned int)b[bi]; \
        y[i] = (amount >= bits) ? (type)0 : (type)(a[ai] op amount); \
    } \
}
#define DEFINE_SHIFT_FOR_TYPE(type, suffix, bits) \
DEFINE_SHIFT(shl, type, suffix, bits, <<) \
DEFINE_SHIFT(shr, type, suffix, bits, >>)
DEFINE_SHIFT_FOR_TYPE(unsigned char, u8, 8u)
DEFINE_SHIFT_FOR_TYPE(unsigned short, u16, 16u)
DEFINE_SHIFT_FOR_TYPE(unsigned int, u32, 32u)
DEFINE_SHIFT_FOR_TYPE(unsigned long long, u64, 64u)
"#;

const BITSHIFT_MODULE: &str = "bitwise_shift_uint";

/// A supported bitwise binary op.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BitwiseBinaryOp {
    And,
    Or,
    Xor,
}

impl BitwiseBinaryOp {
    fn stem(self) -> &'static str {
        match self {
            BitwiseBinaryOp::And => "band",
            BitwiseBinaryOp::Or => "bor",
            BitwiseBinaryOp::Xor => "bxor",
        }
    }

    fn op_name(self) -> &'static str {
        match self {
            BitwiseBinaryOp::And => "BitwiseAnd",
            BitwiseBinaryOp::Or => "BitwiseOr",
            BitwiseBinaryOp::Xor => "BitwiseXor",
        }
    }
}

/// Left or right `BitShift`, selected by the `direction` attribute.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ShiftDirection {
    Left,
    Right,
}

impl ShiftDirection {
    fn stem(self) -> &'static str {
        match self {
            ShiftDirection::Left => "shl",
            ShiftDirection::Right => "shr",
        }
    }
}

/// The dtype/kernel contract for a binary integer op.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum BinaryKind {
    /// Bitwise `&`/`|`/`^` across every integer dtype.
    Bitwise(BitwiseBinaryOp),
    /// Unsigned-only shift with a fixed direction.
    Shift(ShiftDirection),
}

impl BinaryKind {
    fn op_name(self) -> &'static str {
        match self {
            BinaryKind::Bitwise(op) => op.op_name(),
            BinaryKind::Shift(_) => "BitShift",
        }
    }

    /// Resolve the NVRTC entry point for `dtype`, or `None` when the dtype is
    /// outside this op's supported set.
    fn entry(self, dtype: DataType) -> Option<String> {
        match self {
            BinaryKind::Bitwise(op) => {
                let (_, suffix) = integer_type_suffix(dtype)?;
                Some(format!("{}_{suffix}", op.stem()))
            }
            BinaryKind::Shift(dir) => {
                let suffix = unsigned_suffix(dtype)?;
                Some(format!("{}_{suffix}", dir.stem()))
            }
        }
    }

    fn module(self) -> &'static str {
        match self {
            BinaryKind::Bitwise(_) => BITWISE_BINARY_MODULE,
            BinaryKind::Shift(_) => BITSHIFT_MODULE,
        }
    }

    fn src(self) -> &'static str {
        match self {
            BinaryKind::Bitwise(_) => BITWISE_BINARY_SRC,
            BinaryKind::Shift(_) => BITSHIFT_SRC,
        }
    }
}

/// Factory for a bitwise `BitwiseAnd`/`BitwiseOr`/`BitwiseXor` kernel.
pub struct BitwiseBinaryFactory {
    pub op: BitwiseBinaryOp,
    pub runtime: Arc<CudaRuntime>,
}

impl KernelFactory for BitwiseBinaryFactory {
    fn create(&self, _node: &Node, _input_shapes: &[Vec<usize>]) -> Result<Box<dyn Kernel>> {
        Ok(Box::new(BinaryIntKernel {
            kind: BinaryKind::Bitwise(self.op),
            runtime: self.runtime.clone(),
            metadata: Mutex::new(BroadcastMetadataCache::new(self.runtime.clone())),
            last_capture_safe_signature: Mutex::new(None),
            capture_seq_independent: false,
        }))
    }
}

/// Factory for the unsigned `BitShift` kernel (reads the `direction` attribute).
pub struct BitShiftFactory {
    pub runtime: Arc<CudaRuntime>,
}

impl KernelFactory for BitShiftFactory {
    fn create(&self, node: &Node, _input_shapes: &[Vec<usize>]) -> Result<Box<dyn Kernel>> {
        let direction = match node.attr("direction").and_then(Attribute::as_str) {
            Some("LEFT") => ShiftDirection::Left,
            Some("RIGHT") => ShiftDirection::Right,
            Some(other) => {
                return Err(EpError::KernelFailed(format!(
                    "cuda_ep BitShift: direction attribute must be LEFT or RIGHT, got {other:?}"
                )));
            }
            None => {
                return Err(EpError::KernelFailed(
                    "cuda_ep BitShift: direction attribute is required".into(),
                ));
            }
        };
        Ok(Box::new(BinaryIntKernel {
            kind: BinaryKind::Shift(direction),
            runtime: self.runtime.clone(),
            metadata: Mutex::new(BroadcastMetadataCache::new(self.runtime.clone())),
            last_capture_safe_signature: Mutex::new(None),
            capture_seq_independent: false,
        }))
    }
}

/// The dtype + operand/broadcast shapes a captured launch is pinned to.
#[derive(Clone, Debug, PartialEq, Eq)]
struct BinaryCaptureSignature {
    dtype: DataType,
    shapes: BroadcastMetadataKey,
}

/// NVRTC-backed integer binary kernel producing a **same-dtype** output. Covers
/// both the bitwise family (every integer dtype) and unsigned `BitShift` via
/// [`BinaryKind`], with NumPy-style right-aligned broadcasting and the same
/// persistent-metadata capture seam as [`super::pointwise::BinaryPredKernel`].
#[derive(Debug)]
pub struct BinaryIntKernel {
    kind: BinaryKind,
    runtime: Arc<CudaRuntime>,
    /// Persistent broadcast metadata so a captured launch performs no per-step
    /// host allocation/upload/free/synchronize.
    metadata: Mutex<BroadcastMetadataCache>,
    /// The dtype/shape signature recorded by the most recent successful
    /// fixed-decode call. `Some` iff the op is currently capture-safe.
    last_capture_safe_signature: Mutex<Option<BinaryCaptureSignature>>,
    /// Metadata-derived seq-independence: `true` iff all IR output dims are
    /// statically known (no growing seq axis), making the op capture-eligible
    /// regardless of the runtime row count.
    capture_seq_independent: bool,
}

impl BinaryIntKernel {
    fn run(&self, inputs: &[TensorView], outputs: &mut [TensorMut]) -> Result<()> {
        let mut last_signature = self.last_capture_safe_signature.lock().map_err(|_| {
            EpError::KernelFailed("cuda_ep bitwise capture signature lock was poisoned".into())
        })?;
        let warmed_signature = last_signature.clone();
        let op = self.kind.op_name();
        if inputs.len() != 2 || outputs.len() != 1 {
            return Err(EpError::KernelFailed(format!(
                "cuda_ep {op}: expected 2 inputs and 1 output, got {} and {}",
                inputs.len(),
                outputs.len()
            )));
        }
        let a = &inputs[0];
        let b = &inputs[1];
        let Some(entry) = self.kind.entry(a.dtype) else {
            return Err(not_implemented(format!(
                "{op}: operand dtype {:?} not supported on CUDA EP",
                a.dtype
            )));
        };
        if b.dtype != a.dtype {
            return Err(not_implemented(format!(
                "{op}: operands must have the same dtype on CUDA EP (got {:?} and {:?})",
                a.dtype, b.dtype
            )));
        }
        if outputs[0].dtype != a.dtype {
            return Err(EpError::KernelFailed(format!(
                "cuda_ep {op}: output dtype {:?} must equal input dtype {:?}",
                outputs[0].dtype, a.dtype
            )));
        }
        if !a.is_contiguous() || !b.is_contiguous() || !outputs[0].is_contiguous() {
            return Err(not_implemented(format!(
                "{op} with a non-contiguous (strided) operand; \
                 insert an explicit copy to materialise it before the op"
            )));
        }

        let out_shape = onnx_runtime_ir::broadcast_shapes(a.shape, b.shape).map_err(EpError::Ir)?;
        if outputs[0].shape != out_shape {
            return Err(EpError::KernelFailed(format!(
                "cuda_ep {op}: output shape {:?} must equal broadcast shape {:?}",
                outputs[0].shape, out_shape
            )));
        }

        let n = outputs[0].numel();
        let n_u64 = count_u64(op, n)?;
        let a_ptr = cuptr(a.data_ptr::<u8>() as *const c_void);
        let b_ptr = cuptr(b.data_ptr::<u8>() as *const c_void);
        let y_ptr = cuptr(outputs[0].data_ptr_mut::<u8>() as *const c_void);

        let capture_eligible = capture_shape_eligible(self.capture_seq_independent, &out_shape);
        let current_signature = capture_eligible.then(|| BinaryCaptureSignature {
            dtype: a.dtype,
            shapes: BroadcastMetadataKey {
                a_shape: a.shape.to_vec(),
                b_shape: b.shape.to_vec(),
                out_shape: out_shape.clone(),
            },
        });
        require_matching_capture_signature(
            &self.runtime,
            op,
            warmed_signature.as_ref(),
            current_signature.as_ref(),
        )?;

        let func = self
            .runtime
            .nvrtc_function(self.kind.module(), self.kind.src(), &entry)?;
        let mut metadata = self.metadata.lock().map_err(|_| {
            EpError::KernelFailed("cuda_ep bitwise metadata lock was poisoned".into())
        })?;
        let mut metadata_candidate = metadata.clone();
        let metadata_ptr = metadata_candidate.prepare(a.shape, b.shape, &out_shape)?;
        let rank = i32::try_from(out_shape.len())
            .map_err(|_| EpError::KernelFailed(format!("cuda_ep {op}: rank exceeds i32")))?;
        let cfg = LaunchConfig {
            grid_dim: (grid_for(n), 1, 1),
            block_dim: (BLOCK, 1, 1),
            shared_mem_bytes: 0,
        };
        let stream = self.runtime.stream();
        let mut builder = stream.launch_builder(&func);
        builder
            .arg(&a_ptr)
            .arg(&b_ptr)
            .arg(&y_ptr)
            .arg(&metadata_ptr)
            .arg(&rank)
            .arg(&n_u64);
        // SAFETY: `func` is the compiled bitwise/shift entry; its argument list is
        // (const T*, const T*, T*, metadata, rank, count), where T matches the
        // validated same-dtype operands. All pointers cover their allocations,
        // with matching rank/count/indexing; the metadata pointer is the
        // persistent cache buffer, valid across replays.
        unsafe { builder.launch(cfg) }.map_err(|e| driver_err(&format!("launch {entry}"), e))?;
        if !self.runtime.is_capturing()? {
            *metadata = metadata_candidate;
        }
        *last_signature = current_signature;
        Ok(())
    }
}

impl Kernel for BinaryIntKernel {
    fn execute(&self, inputs: &[TensorView], outputs: &mut [TensorMut]) -> Result<()> {
        self.run(inputs, outputs)
    }

    fn supports_strided_input(&self, _idx: usize) -> bool {
        false
    }

    fn device_graph_resources(&self) -> Vec<DeviceGraphResource> {
        self.metadata
            .lock()
            .ok()
            .and_then(|metadata| metadata.device_graph_resource())
            .into_iter()
            .collect()
    }

    fn capture_support(&self) -> onnx_runtime_ep_api::CaptureSupport {
        match self.last_capture_safe_signature.lock() {
            Ok(signature) if signature.is_some() => onnx_runtime_ep_api::CaptureSupport::Supported,
            Ok(_) => onnx_runtime_ep_api::CaptureSupport::unsupported(format!(
                "{} broadcast shape/dtype signature does not match the warmed capture signature",
                self.kind.op_name()
            )),
            Err(_) => onnx_runtime_ep_api::CaptureSupport::unsupported(format!(
                "{} capture signature is unavailable because its state lock was poisoned",
                self.kind.op_name()
            )),
        }
    }

    fn set_capture_seq_independent(&mut self, seq_independent: bool) {
        self.capture_seq_independent = seq_independent;
    }
}

// ===========================================================================
// BitwiseNot (integer → same integer dtype)
// ===========================================================================

/// NVRTC source: bitwise complement over every integer dtype.
const BITWISE_NOT_SRC: &str = r#"
#define DEFINE_NOT(type, suffix) \
extern "C" __global__ void bnot_##suffix(const type* x, type* y, const unsigned long long n) { \
    for (unsigned long long i = blockIdx.x*blockDim.x + threadIdx.x; i < n; i += (unsigned long long)gridDim.x * blockDim.x) \
        y[i] = (type)(~x[i]); \
}
DEFINE_NOT(signed char, i8)
DEFINE_NOT(short, i16)
DEFINE_NOT(int, i32)
DEFINE_NOT(long long, i64)
DEFINE_NOT(unsigned char, u8)
DEFINE_NOT(unsigned short, u16)
DEFINE_NOT(unsigned int, u32)
DEFINE_NOT(unsigned long long, u64)
"#;

const BITWISE_NOT_MODULE: &str = "bitwise_not_int";

/// Factory for [`BitwiseNotKernel`] (no attributes).
pub struct BitwiseNotFactory {
    pub runtime: Arc<CudaRuntime>,
}

impl KernelFactory for BitwiseNotFactory {
    fn create(&self, _node: &Node, _input_shapes: &[Vec<usize>]) -> Result<Box<dyn Kernel>> {
        Ok(Box::new(BitwiseNotKernel {
            runtime: self.runtime.clone(),
        }))
    }
}

/// NVRTC-backed integer `BitwiseNot` kernel.
#[derive(Debug)]
pub struct BitwiseNotKernel {
    runtime: Arc<CudaRuntime>,
}

impl BitwiseNotKernel {
    fn run(&self, inputs: &[TensorView], outputs: &mut [TensorMut]) -> Result<()> {
        if inputs.len() != 1 || outputs.len() != 1 {
            return Err(EpError::KernelFailed(format!(
                "cuda_ep BitwiseNot: expected 1 input and 1 output, got {} and {}",
                inputs.len(),
                outputs.len()
            )));
        }
        let x = &inputs[0];
        let Some((_, suffix)) = integer_type_suffix(x.dtype) else {
            return Err(not_implemented(format!(
                "BitwiseNot: operand dtype {:?} not supported on CUDA EP",
                x.dtype
            )));
        };
        if outputs[0].dtype != x.dtype {
            return Err(EpError::KernelFailed(format!(
                "cuda_ep BitwiseNot: output dtype {:?} must equal input dtype {:?}",
                outputs[0].dtype, x.dtype
            )));
        }
        if !x.is_contiguous() || !outputs[0].is_contiguous() {
            return Err(not_implemented(
                "BitwiseNot with a non-contiguous (strided) operand; \
                 insert an explicit copy to materialise it before the op",
            ));
        }
        if outputs[0].numel() != x.numel() {
            return Err(EpError::KernelFailed(format!(
                "cuda_ep BitwiseNot: output has {} elements, expected {} (same shape as input)",
                outputs[0].numel(),
                x.numel()
            )));
        }

        let n = x.numel();
        let n_u64 = count_u64("BitwiseNot", n)?;
        let x_ptr = cuptr(x.data_ptr::<u8>() as *const c_void);
        let y_ptr = cuptr(outputs[0].data_ptr_mut::<u8>() as *const c_void);
        let entry = format!("bnot_{suffix}");
        let func = self
            .runtime
            .nvrtc_function(BITWISE_NOT_MODULE, BITWISE_NOT_SRC, &entry)?;
        let cfg = LaunchConfig {
            grid_dim: (grid_for(n), 1, 1),
            block_dim: (BLOCK, 1, 1),
            shared_mem_bytes: 0,
        };
        let stream = self.runtime.stream();
        let mut builder = stream.launch_builder(&func);
        builder.arg(&x_ptr).arg(&y_ptr).arg(&n_u64);
        // SAFETY: `func` is the compiled complement entry; the (const T*, T*,
        // unsigned long long) argument list matches its signature; both pointers
        // are live device allocations of `n` elements of the validated dtype.
        unsafe { builder.launch(cfg) }.map_err(|e| driver_err(&format!("launch {entry}"), e))?;
        if self.runtime.is_capturing()? {
            return Ok(());
        }
        self.runtime.synchronize()
    }
}

impl Kernel for BitwiseNotKernel {
    fn execute(&self, inputs: &[TensorView], outputs: &mut [TensorMut]) -> Result<()> {
        self.run(inputs, outputs)
    }

    fn supports_strided_input(&self, _idx: usize) -> bool {
        false
    }

    fn capture_support(&self) -> onnx_runtime_ep_api::CaptureSupport {
        onnx_runtime_ep_api::CaptureSupport::Supported
    }
}

/// Claim-time rejection reason for the bitwise/shift dtype contract, so the
/// partitioner routes an unsupported dtype to another EP instead of failing at
/// kernel time.
pub(crate) fn unsupported_reason(op: &str, input_dtypes: &[DataType]) -> Option<String> {
    let shift = op == "BitShift";
    let unary = op == "BitwiseNot";
    let Some(&a) = input_dtypes.first() else {
        return Some(format!("{op}: missing operand dtype for CUDA EP"));
    };
    let supported = if shift {
        unsigned_suffix(a).is_some()
    } else {
        integer_type_suffix(a).is_some()
    };
    if !supported {
        return Some(format!(
            "{op}: operand dtype {a:?} not supported on CUDA EP"
        ));
    }
    if !unary {
        let Some(&b) = input_dtypes.get(1) else {
            return Some(format!("{op}: missing second operand dtype for CUDA EP"));
        };
        if a != b {
            return Some(format!(
                "{op}: operands must have the same dtype on CUDA EP (got {a:?} and {b:?})"
            ));
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bitwise_binary_entry_points_present_for_every_integer_dtype() {
        for op in [
            BitwiseBinaryOp::And,
            BitwiseBinaryOp::Or,
            BitwiseBinaryOp::Xor,
        ] {
            assert!(
                BITWISE_BINARY_SRC
                    .contains(&format!("DEFINE_BITWISE({}, type, suffix,", op.stem())),
                "missing generator for {}",
                op.op_name()
            );
        }
        for suffix in ["i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64"] {
            assert_eq!(
                BitwiseBinaryOp::And.entry_suffix_for_test(suffix),
                format!("band_{suffix}")
            );
        }
    }

    #[test]
    fn shift_entry_points_are_unsigned_only() {
        assert_eq!(
            BinaryKind::Shift(ShiftDirection::Left).entry(DataType::Uint32),
            Some("shl_u32".to_string())
        );
        assert_eq!(
            BinaryKind::Shift(ShiftDirection::Right).entry(DataType::Uint8),
            Some("shr_u8".to_string())
        );
        // Signed operands are unsupported for BitShift.
        assert_eq!(
            BinaryKind::Shift(ShiftDirection::Left).entry(DataType::Int32),
            None
        );
    }

    #[test]
    fn shift_guard_matches_cpu_checked_shift_contract() {
        // The kernel truncates the amount to u32 and compares against the width,
        // reproducing `checked_shl(amount as u32)` returning None (-> 0).
        assert!(BITSHIFT_SRC.contains("unsigned int amount = (unsigned int)b[bi];"));
        assert!(BITSHIFT_SRC.contains("(amount >= bits) ? (type)0"));
        for bits in ["8u", "16u", "32u", "64u"] {
            assert!(
                BITSHIFT_SRC.contains(&format!(", {bits})")),
                "missing width {bits}"
            );
        }
    }

    #[test]
    fn not_entry_points_present_for_every_integer_dtype() {
        for suffix in ["i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64"] {
            assert!(
                BITWISE_NOT_SRC.contains(&format!(", {suffix})")),
                "missing BitwiseNot generator for {suffix}"
            );
        }
    }

    #[test]
    fn bitwise_binary_rejects_float_and_mismatched_dtypes() {
        assert_eq!(
            BinaryKind::Bitwise(BitwiseBinaryOp::And).entry(DataType::Float32),
            None
        );
        let reason = unsupported_reason("BitwiseAnd", &[DataType::Int32, DataType::Int64]).unwrap();
        assert!(reason.contains("same dtype"), "{reason}");
        let reason =
            unsupported_reason("BitwiseAnd", &[DataType::Float32, DataType::Float32]).unwrap();
        assert!(reason.contains("not supported"), "{reason}");
        assert_eq!(
            unsupported_reason("BitwiseAnd", &[DataType::Uint8, DataType::Uint8]),
            None
        );
    }

    #[test]
    fn bitshift_claim_requires_unsigned_operands() {
        assert!(unsupported_reason("BitShift", &[DataType::Int32, DataType::Int32]).is_some());
        assert_eq!(
            unsupported_reason("BitShift", &[DataType::Uint16, DataType::Uint16]),
            None
        );
    }

    #[test]
    fn bitwise_not_claim_is_unary() {
        assert_eq!(
            unsupported_reason("BitwiseNot", &[DataType::Int8]),
            None,
            "unary op must not require a second operand"
        );
        assert!(unsupported_reason("BitwiseNot", &[DataType::Float64]).is_some());
    }

    #[test]
    fn grid_covers_all_elements() {
        assert_eq!(grid_for(0), 1);
        assert_eq!(grid_for(BLOCK as usize + 1), 2);
        assert_eq!(grid_for(usize::MAX / 2), 65_535);
    }

    #[test]
    fn kernels_use_unsigned_64bit_indexing() {
        const LOOP: &str = "for (unsigned long long i = blockIdx.x*blockDim.x + threadIdx.x; i < n; i += (unsigned long long)gridDim.x * blockDim.x)";
        for src in [BITWISE_BINARY_SRC, BITSHIFT_SRC, BITWISE_NOT_SRC] {
            assert!(src.contains(LOOP), "kernel regressed to signed indexing");
            assert!(src.contains("const unsigned long long n)"));
        }
    }

    impl BitwiseBinaryOp {
        fn entry_suffix_for_test(self, suffix: &str) -> String {
            format!("{}_{suffix}", self.stem())
        }
    }
}