oxicuda 0.1.0

OxiCUDA - Pure Rust CUDA replacement for the COOLJAPAN ecosystem (95% performance target)
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
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
//! Elementwise ONNX operators.

use std::collections::HashMap;

use super::{binary_elementwise_f32, get_float_attr, get_required_input, unary_elementwise_f32};
use crate::onnx_backend::ir::*;

// ─── Binary ops ─────────────────────────────────────────────

/// Add(A, B) -> element-wise addition with broadcasting.
pub fn execute_add(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    binary_elementwise_f32(inputs, |a, b| a + b)
}

/// Sub(A, B) -> element-wise subtraction with broadcasting.
pub fn execute_sub(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    binary_elementwise_f32(inputs, |a, b| a - b)
}

/// Mul(A, B) -> element-wise multiplication with broadcasting.
pub fn execute_mul(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    binary_elementwise_f32(inputs, |a, b| a * b)
}

/// Div(A, B) -> element-wise division with broadcasting.
pub fn execute_div(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    binary_elementwise_f32(inputs, |a, b| if b != 0.0 { a / b } else { f32::NAN })
}

/// Pow(A, B) -> element-wise power with broadcasting.
pub fn execute_pow(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    binary_elementwise_f32(inputs, |a, b| a.powf(b))
}

// ─── Unary ops ──────────────────────────────────────────────

/// Relu(X) -> max(0, x).
pub fn execute_relu(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, |x| x.max(0.0))
}

/// Sigmoid(X) -> 1 / (1 + exp(-x)).
pub fn execute_sigmoid(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, |x| 1.0 / (1.0 + (-x).exp()))
}

/// Tanh(X) -> hyperbolic tangent.
pub fn execute_tanh(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, f32::tanh)
}

/// Exp(X) -> e^x.
pub fn execute_exp(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, f32::exp)
}

/// Log(X) -> natural log.
pub fn execute_log(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, f32::ln)
}

/// Sqrt(X) -> square root.
pub fn execute_sqrt(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, f32::sqrt)
}

/// Abs(X) -> absolute value.
pub fn execute_abs(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, f32::abs)
}

/// Neg(X) -> negation.
pub fn execute_neg(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, |x| -x)
}

/// Ceil(X) -> ceiling.
pub fn execute_ceil(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, f32::ceil)
}

/// Floor(X) -> floor.
pub fn execute_floor(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, f32::floor)
}

/// Round(X) -> round to nearest even.
pub fn execute_round(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, f32::round)
}

/// Sign(X) -> signum.
pub fn execute_sign(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, |x| {
        if x > 0.0 {
            1.0
        } else if x < 0.0 {
            -1.0
        } else {
            0.0
        }
    })
}

/// Reciprocal(X) -> 1/x.
pub fn execute_reciprocal(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, |x| if x != 0.0 { 1.0 / x } else { f32::NAN })
}

/// Softplus(X) -> ln(1 + exp(x)).
pub fn execute_softplus(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    unary_elementwise_f32(inputs, |x| {
        if x > 20.0 {
            x // numerical stability
        } else {
            (1.0 + x.exp()).ln()
        }
    })
}

// ─── Parameterized unary ops ────────────────────────────────

/// LeakyRelu(X, alpha) -> max(alpha*x, x).
pub fn execute_leaky_relu(
    inputs: &[Option<&OnnxTensor>],
    attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    let alpha = get_float_attr(attrs, "alpha", 0.01)? as f32;
    unary_elementwise_f32(inputs, move |x| if x >= 0.0 { x } else { alpha * x })
}

/// Elu(X, alpha) -> x if x>=0, alpha*(exp(x)-1) otherwise.
pub fn execute_elu(
    inputs: &[Option<&OnnxTensor>],
    attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    let alpha = get_float_attr(attrs, "alpha", 1.0)? as f32;
    unary_elementwise_f32(
        inputs,
        move |x| {
            if x >= 0.0 { x } else { alpha * (x.exp() - 1.0) }
        },
    )
}

/// Selu(X, alpha, gamma) -> gamma * (x if x>=0, alpha*(exp(x)-1) otherwise).
pub fn execute_selu(
    inputs: &[Option<&OnnxTensor>],
    attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    let alpha = get_float_attr(attrs, "alpha", 1.673_263_168_334_961)? as f32;
    let gamma = get_float_attr(attrs, "gamma", 1.050_700_783_920_288)? as f32;
    unary_elementwise_f32(inputs, move |x| {
        if x >= 0.0 {
            gamma * x
        } else {
            gamma * (alpha * (x.exp() - 1.0))
        }
    })
}

/// Clip(X, min, max) -> clamp to [min, max].
pub fn execute_clip(
    inputs: &[Option<&OnnxTensor>],
    attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    let x = get_required_input(inputs, 0, "input")?;
    let data = x.as_f32()?;

    // Opset 11+: min/max as optional inputs
    let min_val = if let Some(min_t) = inputs.get(1).and_then(|o| *o) {
        let v = min_t.as_f32()?;
        v.first().copied().unwrap_or(f32::NEG_INFINITY)
    } else {
        get_float_attr(attrs, "min", f64::from(f32::NEG_INFINITY))? as f32
    };

    let max_val = if let Some(max_t) = inputs.get(2).and_then(|o| *o) {
        let v = max_t.as_f32()?;
        v.first().copied().unwrap_or(f32::INFINITY)
    } else {
        get_float_attr(attrs, "max", f64::from(f32::INFINITY))? as f32
    };

    let result: Vec<f32> = data.iter().map(|&v| v.clamp(min_val, max_val)).collect();
    Ok(vec![OnnxTensor::from_f32(&result, x.shape.clone())])
}

// ─── Ternary / special ops ──────────────────────────────────

/// Where(condition, X, Y) -> select X where true, Y where false.
pub fn execute_where(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    let cond = get_required_input(inputs, 0, "condition")?;
    let x = get_required_input(inputs, 1, "X")?;
    let y = get_required_input(inputs, 2, "Y")?;
    let cond_data = cond.as_bool()?;
    let x_data = x.as_f32()?;
    let y_data = y.as_f32()?;

    // Three-way broadcast
    let shape_xy = broadcast_shapes(&x.shape, &y.shape)?;
    let out_shape = broadcast_shapes(&cond.shape, &shape_xy)?;
    let total: usize = if out_shape.is_empty() {
        1
    } else {
        out_shape.iter().product()
    };
    let mut result = Vec::with_capacity(total);
    for i in 0..total {
        let multi = flat_to_multi(i, &out_shape);
        let ci = broadcast_index(&multi, &cond.shape, &out_shape);
        let xi = broadcast_index(&multi, &x.shape, &out_shape);
        let yi = broadcast_index(&multi, &y.shape, &out_shape);
        result.push(if cond_data[ci] {
            x_data[xi]
        } else {
            y_data[yi]
        });
    }
    Ok(vec![OnnxTensor::from_f32(&result, out_shape)])
}

/// Cast(X, to=dtype) -> type conversion.
pub fn execute_cast(
    inputs: &[Option<&OnnxTensor>],
    attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    let x = get_required_input(inputs, 0, "input")?;
    let to = attrs
        .get("to")
        .ok_or_else(|| OnnxError::InvalidAttribute("Cast requires 'to' attribute".into()))?
        .as_int()?;

    let target_dtype = match to {
        1 => DataType::Float32,
        2 => DataType::Uint8,
        3 => DataType::Int8,
        6 => DataType::Int32,
        7 => DataType::Int64,
        9 => DataType::Bool,
        10 => DataType::Float16,
        11 => DataType::Float64,
        other => {
            return Err(OnnxError::TypeError(format!(
                "unsupported Cast target type: {other}"
            )));
        }
    };

    cast_tensor(x, target_dtype)
}

/// FMA(A, B, C) -> A * B + C (fused multiply-add).
pub fn execute_fma(
    inputs: &[Option<&OnnxTensor>],
    _attrs: &HashMap<String, AttributeValue>,
) -> OnnxResult<Vec<OnnxTensor>> {
    let a = get_required_input(inputs, 0, "A")?;
    let b = get_required_input(inputs, 1, "B")?;
    let c = get_required_input(inputs, 2, "C")?;

    let a_data = a.as_f32()?;
    let b_data = b.as_f32()?;
    let c_data = c.as_f32()?;

    let ab_shape = broadcast_shapes(&a.shape, &b.shape)?;
    let out_shape = broadcast_shapes(&ab_shape, &c.shape)?;
    let total: usize = if out_shape.is_empty() {
        1
    } else {
        out_shape.iter().product()
    };
    let mut result = Vec::with_capacity(total);
    for i in 0..total {
        let multi = flat_to_multi(i, &out_shape);
        let ai = broadcast_index(&multi, &a.shape, &out_shape);
        let bi = broadcast_index(&multi, &b.shape, &out_shape);
        let ci = broadcast_index(&multi, &c.shape, &out_shape);
        result.push(a_data[ai].mul_add(b_data[bi], c_data[ci]));
    }
    Ok(vec![OnnxTensor::from_f32(&result, out_shape)])
}

// ─── Cast helpers ───────────────────────────────────────────

// ONNX uses specific integer codes for data types; we handle a subset
// of common int16 as Int32 storage internally (not full Float16 compute).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
enum Int16Placeholder {}

fn cast_tensor(x: &OnnxTensor, target: DataType) -> OnnxResult<Vec<OnnxTensor>> {
    // Fast path: same type
    if x.dtype == target {
        return Ok(vec![x.clone()]);
    }

    // Get source values as f64 intermediary
    let values = tensor_to_f64(x)?;

    // Convert to target
    let out = match target {
        DataType::Float32 => {
            let v: Vec<f32> = values.iter().map(|&x| x as f32).collect();
            OnnxTensor::from_f32(&v, x.shape.clone())
        }
        DataType::Float64 => OnnxTensor::from_f64(&values, x.shape.clone()),
        DataType::Int32 => {
            let v: Vec<i32> = values.iter().map(|&x| x as i32).collect();
            OnnxTensor::from_i32(&v, x.shape.clone())
        }
        DataType::Int64 => {
            let v: Vec<i64> = values.iter().map(|&x| x as i64).collect();
            OnnxTensor::from_i64(&v, x.shape.clone())
        }
        DataType::Bool => {
            let v: Vec<bool> = values.iter().map(|&x| x != 0.0).collect();
            OnnxTensor::from_bool(&v, x.shape.clone())
        }
        other => {
            return Err(OnnxError::TypeError(format!(
                "Cast to {other} not yet implemented"
            )));
        }
    };
    Ok(vec![out])
}

fn tensor_to_f64(x: &OnnxTensor) -> OnnxResult<Vec<f64>> {
    match x.dtype {
        DataType::Float32 => Ok(x.as_f32()?.iter().map(|&v| f64::from(v)).collect()),
        DataType::Float64 => x.as_f64(),
        DataType::Int32 => Ok(x.as_i32()?.iter().map(|&v| f64::from(v)).collect()),
        DataType::Int64 => Ok(x.as_i64()?.iter().map(|&v| v as f64).collect()),
        DataType::Bool => Ok(x
            .as_bool()?
            .iter()
            .map(|&v| if v { 1.0 } else { 0.0 })
            .collect()),
        other => Err(OnnxError::TypeError(format!(
            "Cast from {other} not implemented"
        ))),
    }
}

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

    fn empty_attrs() -> HashMap<String, AttributeValue> {
        HashMap::new()
    }

    fn assert_f32_near(actual: &[f32], expected: &[f32], eps: f32) {
        assert_eq!(actual.len(), expected.len(), "length mismatch");
        for (i, (a, e)) in actual.iter().zip(expected).enumerate() {
            assert!((a - e).abs() < eps, "index {i}: {a} != {e} (eps={eps})");
        }
    }

    #[test]
    fn test_add() {
        let a = OnnxTensor::from_f32(&[1.0, 2.0, 3.0], vec![3]);
        let b = OnnxTensor::from_f32(&[10.0, 20.0, 30.0], vec![3]);
        let r = execute_add(&[Some(&a), Some(&b)], &empty_attrs()).unwrap();
        assert_eq!(r[0].as_f32().unwrap(), vec![11.0, 22.0, 33.0]);
    }

    #[test]
    fn test_add_broadcast() {
        let a = OnnxTensor::from_f32(&[1.0, 2.0, 3.0], vec![3]);
        let b = OnnxTensor::from_f32(&[10.0], vec![1]);
        let r = execute_add(&[Some(&a), Some(&b)], &empty_attrs()).unwrap();
        assert_eq!(r[0].as_f32().unwrap(), vec![11.0, 12.0, 13.0]);
    }

    #[test]
    fn test_sub() {
        let a = OnnxTensor::from_f32(&[10.0, 20.0], vec![2]);
        let b = OnnxTensor::from_f32(&[1.0, 2.0], vec![2]);
        let r = execute_sub(&[Some(&a), Some(&b)], &empty_attrs()).unwrap();
        assert_eq!(r[0].as_f32().unwrap(), vec![9.0, 18.0]);
    }

    #[test]
    fn test_mul() {
        let a = OnnxTensor::from_f32(&[2.0, 3.0], vec![2]);
        let b = OnnxTensor::from_f32(&[4.0, 5.0], vec![2]);
        let r = execute_mul(&[Some(&a), Some(&b)], &empty_attrs()).unwrap();
        assert_eq!(r[0].as_f32().unwrap(), vec![8.0, 15.0]);
    }

    #[test]
    fn test_div() {
        let a = OnnxTensor::from_f32(&[10.0, 20.0], vec![2]);
        let b = OnnxTensor::from_f32(&[2.0, 5.0], vec![2]);
        let r = execute_div(&[Some(&a), Some(&b)], &empty_attrs()).unwrap();
        assert_eq!(r[0].as_f32().unwrap(), vec![5.0, 4.0]);
    }

    #[test]
    fn test_relu() {
        let x = OnnxTensor::from_f32(&[-1.0, 0.0, 1.0, 2.0], vec![4]);
        let r = execute_relu(&[Some(&x)], &empty_attrs()).unwrap();
        assert_eq!(r[0].as_f32().unwrap(), vec![0.0, 0.0, 1.0, 2.0]);
    }

    #[test]
    fn test_sigmoid() {
        let x = OnnxTensor::from_f32(&[0.0], vec![1]);
        let r = execute_sigmoid(&[Some(&x)], &empty_attrs()).unwrap();
        assert_f32_near(&r[0].as_f32().unwrap(), &[0.5], 1e-6);
    }

    #[test]
    fn test_tanh() {
        let x = OnnxTensor::from_f32(&[0.0], vec![1]);
        let r = execute_tanh(&[Some(&x)], &empty_attrs()).unwrap();
        assert_f32_near(&r[0].as_f32().unwrap(), &[0.0], 1e-6);
    }

    #[test]
    fn test_exp_log() {
        let x = OnnxTensor::from_f32(&[1.0], vec![1]);
        let r = execute_exp(&[Some(&x)], &empty_attrs()).unwrap();
        assert_f32_near(&r[0].as_f32().unwrap(), &[std::f32::consts::E], 1e-5);
        let r2 = execute_log(&[Some(&r[0])], &empty_attrs()).unwrap();
        assert_f32_near(&r2[0].as_f32().unwrap(), &[1.0], 1e-5);
    }

    #[test]
    fn test_abs_neg() {
        let x = OnnxTensor::from_f32(&[-3.0, 2.0], vec![2]);
        let r_abs = execute_abs(&[Some(&x)], &empty_attrs()).unwrap();
        assert_eq!(r_abs[0].as_f32().unwrap(), vec![3.0, 2.0]);
        let r_neg = execute_neg(&[Some(&x)], &empty_attrs()).unwrap();
        assert_eq!(r_neg[0].as_f32().unwrap(), vec![3.0, -2.0]);
    }

    #[test]
    fn test_leaky_relu() {
        let x = OnnxTensor::from_f32(&[-10.0, 5.0], vec![2]);
        let mut attrs = HashMap::new();
        attrs.insert("alpha".into(), AttributeValue::Float(0.1));
        let r = execute_leaky_relu(&[Some(&x)], &attrs).unwrap();
        assert_f32_near(&r[0].as_f32().unwrap(), &[-1.0, 5.0], 1e-6);
    }

    #[test]
    fn test_clip() {
        let x = OnnxTensor::from_f32(&[-5.0, 0.0, 5.0, 10.0], vec![4]);
        let min_t = OnnxTensor::from_f32(&[0.0], vec![]);
        let max_t = OnnxTensor::from_f32(&[6.0], vec![]);
        let r = execute_clip(&[Some(&x), Some(&min_t), Some(&max_t)], &empty_attrs()).unwrap();
        assert_eq!(r[0].as_f32().unwrap(), vec![0.0, 0.0, 5.0, 6.0]);
    }

    #[test]
    fn test_where_op() {
        let cond = OnnxTensor::from_bool(&[true, false, true], vec![3]);
        let x = OnnxTensor::from_f32(&[1.0, 2.0, 3.0], vec![3]);
        let y = OnnxTensor::from_f32(&[10.0, 20.0, 30.0], vec![3]);
        let r = execute_where(&[Some(&cond), Some(&x), Some(&y)], &empty_attrs()).unwrap();
        assert_eq!(r[0].as_f32().unwrap(), vec![1.0, 20.0, 3.0]);
    }

    #[test]
    fn test_cast_f32_to_i32() {
        let x = OnnxTensor::from_f32(&[1.5, 2.7, -3.1], vec![3]);
        let mut attrs = HashMap::new();
        attrs.insert("to".into(), AttributeValue::Int(6)); // INT32
        let r = execute_cast(&[Some(&x)], &attrs).unwrap();
        assert_eq!(r[0].dtype, DataType::Int32);
        assert_eq!(r[0].as_i32().unwrap(), vec![1, 2, -3]);
    }

    #[test]
    fn test_fma() {
        let a = OnnxTensor::from_f32(&[2.0, 3.0], vec![2]);
        let b = OnnxTensor::from_f32(&[4.0, 5.0], vec![2]);
        let c = OnnxTensor::from_f32(&[1.0, 1.0], vec![2]);
        let r = execute_fma(&[Some(&a), Some(&b), Some(&c)], &empty_attrs()).unwrap();
        assert_eq!(r[0].as_f32().unwrap(), vec![9.0, 16.0]);
    }

    #[test]
    fn test_pow() {
        let a = OnnxTensor::from_f32(&[2.0, 3.0], vec![2]);
        let b = OnnxTensor::from_f32(&[3.0, 2.0], vec![2]);
        let r = execute_pow(&[Some(&a), Some(&b)], &empty_attrs()).unwrap();
        assert_f32_near(&r[0].as_f32().unwrap(), &[8.0, 9.0], 1e-5);
    }

    #[test]
    fn test_ceil_floor_round() {
        let x = OnnxTensor::from_f32(&[1.3, 2.7, -0.5], vec![3]);
        let rc = execute_ceil(&[Some(&x)], &empty_attrs()).unwrap();
        assert_eq!(rc[0].as_f32().unwrap(), vec![2.0, 3.0, 0.0]);
        let rf = execute_floor(&[Some(&x)], &empty_attrs()).unwrap();
        assert_eq!(rf[0].as_f32().unwrap(), vec![1.0, 2.0, -1.0]);
        let rr = execute_round(&[Some(&x)], &empty_attrs()).unwrap();
        assert_eq!(rr[0].as_f32().unwrap(), vec![1.0, 3.0, -1.0]);
    }

    #[test]
    fn test_sign() {
        let x = OnnxTensor::from_f32(&[-3.0, 0.0, 5.0], vec![3]);
        let r = execute_sign(&[Some(&x)], &empty_attrs()).unwrap();
        assert_eq!(r[0].as_f32().unwrap(), vec![-1.0, 0.0, 1.0]);
    }
}