rten 0.25.0

Machine learning runtime
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
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
use rayon::prelude::*;
use rten_tensor::prelude::*;
use rten_tensor::{NdTensorView, Tensor, TensorView};

use crate::{
    buffer_pool::{AutoReturn, BufferPool},
    infer_shapes::{InferShapes, UnaryOp},
    operator::{
        IntoOpResult, OpError, OpRunContext, Operator, OutputList, OutputType, OutputTypeList,
        OutputTypesContext,
    },
    ops::gather,
};

/// Validate a cos/sin cache and return its `(batch, seq_len)` dimensions.
///
/// The cache must have shape `[batch, seq_len, rotary_embedding_dim / 2]`, where
/// the batch and sequence dimensions may each be 1 to broadcast across the
/// corresponding input dimension.
fn rotary_cache_dims(
    cache_shape: &[usize],
    batch: usize,
    seq_len: usize,
    half: usize,
    bad_last_dim: &'static str,
) -> Result<(usize, usize), OpError> {
    let &[cache_batch, cache_seq, cache_half] = cache_shape else {
        return Err(OpError::InvalidValue("cos/sin cache must be a 3D tensor"));
    };
    if cache_half != half {
        return Err(OpError::InvalidValue(bad_last_dim));
    }
    if cache_seq != 1 && cache_seq != seq_len {
        return Err(OpError::InvalidValue(
            "cos/sin cache sequence length must be 1 or match the input",
        ));
    }
    if cache_batch != 1 && cache_batch != batch {
        return Err(OpError::InvalidValue(
            "cos/sin cache batch size must be 1 or match the input",
        ));
    }
    Ok((cache_batch, cache_seq))
}

pub(crate) fn rotary_embedding(
    pool: &BufferPool,
    input: TensorView<f32>,
    cos: TensorView<f32>,
    sin: TensorView<f32>,
    position_ids: Option<NdTensorView<i32, 2>>,
    interleaved: bool,
    num_heads: usize,
    rotary_embedding_dim: usize,
) -> Result<Tensor, OpError> {
    // Reshape input to `[batch, seq_len, num_heads, head_size]`.
    let reshaped_input = match input.shape() {
        &[batch, seq_len, hidden_size] => {
            if num_heads == 0 {
                return Err(OpError::InvalidValue(
                    "num_heads must not be 0 for 3 dimensioned input",
                ));
            }
            if hidden_size % num_heads != 0 {
                return Err(OpError::InvalidValue(
                    "hidden_size must be divisible by num_heads",
                ));
            }

            let head_size = hidden_size / num_heads;
            input.reshaped([batch, seq_len, num_heads, head_size])
        }
        [_batch, _num_heads, _seq_len, _head_size] => {
            input.nd_view().permuted([0, 2, 1, 3]).as_cow()
        }
        _ => {
            return Err(OpError::IncompatibleInputShapes(
                "Input processed needs 3-4 dimensions",
            ));
        }
    };
    let [batch, seq_len, num_heads, head_size] = reshaped_input.shape();

    let rotary_embedding_dim = if rotary_embedding_dim == 0 {
        head_size
    } else {
        rotary_embedding_dim
    };
    if rotary_embedding_dim == 0 || rotary_embedding_dim % 2 != 0 {
        return Err(OpError::InvalidValue(
            "rotary_embedding_dim must be a positive even number",
        ));
    }
    if rotary_embedding_dim > head_size {
        return Err(OpError::InvalidValue(
            "rotary_embedding_dim must not exceed head size",
        ));
    }
    let half = rotary_embedding_dim / 2;

    // Resolve the cos/sin caches to a `[batch, seq_len, half]` layout. When
    // `position_ids` is provided the caches are gathered by position, otherwise
    // they are indexed by `(batch, seq_len)` directly.
    let (cos_cache, sin_cache) = if let Some(position_ids) = position_ids {
        let cos = gather(pool, cos, 0, position_ids.as_dyn())?.into_cow();
        let sin = gather(pool, sin, 0, position_ids.as_dyn())?.into_cow();
        (cos, sin)
    } else {
        (cos.as_cow(), sin.as_cow())
    };

    let (cos_batch, cos_seq) = rotary_cache_dims(
        cos_cache.shape(),
        batch,
        seq_len,
        half,
        "Last dimension of cos cache does not match rotary_embedding_dim/2",
    )?;
    let (sin_batch, sin_seq) = rotary_cache_dims(
        sin_cache.shape(),
        batch,
        seq_len,
        half,
        "Last dimension of sin cache does not match rotary_embedding_dim/2",
    )?;

    // Make the inputs contiguous so each head vector and cache row is a
    // contiguous slice that the kernel can index directly.
    let input_contig = reshaped_input.to_contiguous_in(pool).auto_return(pool);
    let cos_contig = cos_cache.to_contiguous_in(pool).auto_return(pool);
    let sin_contig = sin_cache.to_contiguous_in(pool).auto_return(pool);
    let in_data = input_contig.data();
    let cos_data = cos_contig.data();
    let sin_data = sin_contig.data();

    let n_rows = batch * seq_len * num_heads;
    let out_len = n_rows * head_size;
    let mut out_data = pool.alloc::<f32>(out_len);

    // For each `(batch, seq, head)` row, apply the rotation to the first
    // `rotary_embedding_dim` elements and copy the remainder.
    let out_uninit = &mut out_data.spare_capacity_mut()[..out_len];
    in_data
        .par_chunks(head_size)
        .zip(out_uninit.par_chunks_mut(head_size))
        .enumerate()
        .for_each(|(row, (x, y))| {
            let bs = row / num_heads;
            let b = bs / seq_len;
            let s = bs % seq_len;

            let broadcast_idx = |dim_size: usize, index: usize| {
                if dim_size == 1 { 0 } else { index }
            };

            let cos_off =
                (broadcast_idx(cos_batch, b) * cos_seq + broadcast_idx(cos_seq, s)) * half;
            let sin_off =
                (broadcast_idx(sin_batch, b) * sin_seq + broadcast_idx(sin_seq, s)) * half;
            let cos_row = &cos_data[cos_off..cos_off + half];
            let sin_row = &sin_data[sin_off..sin_off + half];

            if interleaved {
                for i in 0..half {
                    let (x1, x2) = (x[2 * i], x[2 * i + 1]);
                    let (cos_i, sin_i) = (cos_row[i], sin_row[i]);
                    y[2 * i].write(x1 * cos_i - x2 * sin_i);
                    y[2 * i + 1].write(x1 * sin_i + x2 * cos_i);
                }
            } else {
                for i in 0..half {
                    let (x1, x2) = (x[i], x[half + i]);
                    let (cos_i, sin_i) = (cos_row[i], sin_row[i]);
                    y[i].write(x1 * cos_i - x2 * sin_i);
                    y[half + i].write(x1 * sin_i + x2 * cos_i);
                }
            }

            // Copy the elements that are not rotated.
            for (yj, &xj) in y[rotary_embedding_dim..]
                .iter_mut()
                .zip(&x[rotary_embedding_dim..])
            {
                yj.write(xj);
            }
        });

    // Safety: every element of `out_data[..out_len]` was initialized above.
    unsafe { out_data.set_len(out_len) };

    let mut output = Tensor::from_data(&[batch, seq_len, num_heads, head_size], out_data);
    if input.ndim() == 3 {
        output.reshape(input.shape());
    } else {
        output.permute(&[0, 2, 1, 3]);
    }

    Ok(output)
}

#[derive(Debug)]
pub struct RotaryEmbedding {
    pub interleaved: bool,
    pub num_heads: usize,
    pub rotary_embedding_dim: usize,
}

impl Operator for RotaryEmbedding {
    fn name(&self) -> &str {
        "RotaryEmbedding"
    }

    fn max_inputs(&self) -> Option<usize> {
        Some(4)
    }

    fn run(&self, ctx: &OpRunContext) -> Result<OutputList, OpError> {
        let input = ctx.inputs().require_as(0)?;
        let cos = ctx.inputs().require_as(1)?;
        let sin = ctx.inputs().require_as(2)?;
        let position_ids = ctx.inputs().get_as(3)?;

        let output = rotary_embedding(
            ctx.pool(),
            input,
            cos,
            sin,
            position_ids,
            self.interleaved,
            self.num_heads,
            self.rotary_embedding_dim,
        )?;

        output.into_op_result()
    }

    fn output_types(&self, _ctx: &OutputTypesContext) -> Option<OutputTypeList> {
        Some([OutputType::CopyFromInput(0)].into())
    }

    fn as_infer_shapes(&self) -> Option<&dyn InferShapes> {
        Some(&UnaryOp)
    }
}

#[cfg(feature = "contrib")]
pub use contrib::RotaryEmbeddingMicrosoft;

#[cfg(feature = "contrib")]
mod contrib;

#[cfg(test)]
mod tests {
    use crate::operator::OutputMask;
    use rten_tensor::{Tensor, test_util::expect_equal_with_tolerance};
    use rten_testing::TestCases;

    use crate::{
        BufferPool,
        operator::{InputList, OperatorExt},
    };

    use super::*;

    // Test rotary embedding using ported test cases from
    // https://github.com/microsoft/onnxruntime/blob/e3c34da40639669f3dbb7ae95db0662afbec8cc9/onnxruntime/test/providers/cpu/llm/rotary_embedding_op_test.cc#L509
    #[test]
    fn test_rotary_embedding() {
        #[derive(Debug)]
        struct Case {
            input: Tensor<f32>,
            position_ids: Option<Tensor<i32>>,
            cos_cache: Tensor<f32>,
            sin_cache: Tensor<f32>,
            expected: Tensor<f32>,
            op: RotaryEmbedding,
        }

        let cases = [
            // RotaryEmbedding_Interleaved_SmallData_LlamaMSFT_4D_Input
            // Input shape [batch=1, num_heads=2, seq=3, head_size=4]. The
            // nested literal is `[num_heads, seq, head_size]`; `with_new_axis`
            // prepends the batch dim.
            Case {
                input: Tensor::from([
                    [
                        [-1.0408, 0.9166, -1.3042, -1.1097],
                        [-1.2188, 1.1676, -1.0574, -0.1188],
                        [-0.8110, 0.6737, -1.1233, -0.0919],
                    ],
                    [
                        [-0.1320, -0.2751, -0.2350, 0.0937],
                        [-0.7396, -1.2425, -0.1752, 0.6990],
                        [-0.6861, 0.7202, 0.1963, 0.6142],
                    ],
                ])
                .with_new_axis(0),
                position_ids: Some(Tensor::from([[0i32, 1, 2]])),
                cos_cache: Tensor::from([
                    [1.0000, 1.0000],
                    [0.5403, 0.9999],
                    [-0.4161, 0.9998],
                    [-0.9900, 0.9996],
                    [-0.6536, 0.9992],
                    [0.2837, 0.9988],
                    [0.9602, 0.9982],
                    [0.7539, 0.9976],
                ]),
                sin_cache: Tensor::from([
                    [0.0000, 0.0000],
                    [0.8415, 0.0100],
                    [0.9093, 0.0200],
                    [0.1411, 0.0300],
                    [-0.7568, 0.0400],
                    [-0.9589, 0.0500],
                    [-0.2794, 0.0600],
                    [0.6570, 0.0699],
                ]),
                expected: Tensor::from([
                    [
                        [-1.0408, 0.9166, -1.3042, -1.1097],
                        [-1.6411, -0.3948, -1.0561, -0.1294],
                        [-0.2751, -1.0178, -1.1212, -0.1143],
                    ],
                    [
                        [-0.1320, -0.2751, -0.2350, 0.0937],
                        [0.6460, -1.2937, -0.1822, 0.6972],
                        [-0.3694, -0.9235, 0.1840, 0.6180],
                    ],
                ])
                .with_new_axis(0),
                op: RotaryEmbedding {
                    interleaved: true,
                    num_heads: 2,
                    rotary_embedding_dim: 0,
                },
            },
            // RotaryEmbedding_NotInterleaved_SmallData_LlamaMSFT
            // Input shape [batch=1, seq=2, hidden=18] (num_heads=3, head_size=6).
            Case {
                input: Tensor::from([[
                    [
                        -1.0408, 0.9166, -1.3042, -1.1097, -1.2188, 1.1676, 1.0076, -0.7529,
                        -0.2250, -0.4327, -1.5071, -0.4586, -0.8663, -0.2656, 0.1665, 0.7911,
                        -0.9320, -0.8579,
                    ],
                    [
                        -1.0574, -0.1188, -0.9078, 0.3452, -0.5713, -0.2351, -0.8480, 0.5266,
                        -1.2944, -0.0243, -0.2354, -0.7087, -0.9647, -0.0991, -0.2994, -0.0650,
                        -1.5720, -1.3211,
                    ],
                ]]),
                position_ids: Some(Tensor::from([[0i32, 1]])),
                cos_cache: Tensor::from([
                    [1.0000, 1.0000, 1.0000],
                    [0.5403, 0.9989, 1.0000],
                    [-0.4161, 0.9957, 1.0000],
                    [-0.9900, 0.9903, 1.0000],
                ]),
                sin_cache: Tensor::from([
                    [0.0000, 0.0000, 0.0000],
                    [0.8415, 0.0464, 0.0022],
                    [0.9093, 0.0927, 0.0043],
                    [0.1411, 0.1388, 0.0065],
                ]),
                expected: Tensor::from([[
                    [
                        -1.0408, 0.9166, -1.3042, -1.1097, -1.2188, 1.1676, 1.0076, -0.7529,
                        -0.2250, -0.4327, -1.5071, -0.4586, -0.8663, -0.2656, 0.1665, 0.7911,
                        -0.9320, -0.8579,
                    ],
                    [
                        -0.8618, -0.0922, -0.9073, -0.7032, -0.5762, -0.2371, -0.4377, 0.5370,
                        -1.2929, -0.7267, -0.2107, -0.7115, -0.4666, -0.0261, -0.2965, -0.8469,
                        -1.5749, -1.3217,
                    ],
                ]]),
                op: RotaryEmbedding {
                    interleaved: false,
                    num_heads: 3,
                    rotary_embedding_dim: 0,
                },
            },
            // RotaryEmbedding_CustomRotaryDim_SmallData_Phi
            // Input shape [batch=1, seq=2, hidden=6] (num_heads=1, head_size=6).
            Case {
                input: Tensor::from([[
                    [-1.0408, 0.9166, -1.3042, -1.1097, -1.2188, 1.1676],
                    [1.0076, -0.7529, -0.2250, -0.4327, -1.5071, -0.4586],
                ]]),
                position_ids: Some(Tensor::from([[0i32, 1]])),
                cos_cache: Tensor::from([[1.0000, 1.0000], [1.0000, 0.5403]]),
                sin_cache: Tensor::from([[0.0000, 0.0000], [0.0000, 0.8415]]),
                expected: Tensor::from([[
                    [-1.0408, 0.9166, -1.3042, -1.1097, -1.2188, 1.1676],
                    [1.0076, -0.0427, -0.2250, -0.8673, -1.5071, -0.4586],
                ]]),
                op: RotaryEmbedding {
                    interleaved: false,
                    num_heads: 1,
                    rotary_embedding_dim: 4,
                },
            },
            // RotaryEmbedding_NotInterleaved_NoPosIds_SmallData_LlamaMSFT
            // Input shape [batch=1, seq=2, hidden=18]; cache [batch=1, seq=2, dim/2=3].
            Case {
                input: Tensor::from([[
                    [
                        -1.0408, 0.9166, -1.3042, -1.1097, -1.2188, 1.1676, 1.0076, -0.7529,
                        -0.2250, -0.4327, -1.5071, -0.4586, -0.8663, -0.2656, 0.1665, 0.7911,
                        -0.9320, -0.8579,
                    ],
                    [
                        -1.0574, -0.1188, -0.9078, 0.3452, -0.5713, -0.2351, -0.8480, 0.5266,
                        -1.2944, -0.0243, -0.2354, -0.7087, -0.9647, -0.0991, -0.2994, -0.0650,
                        -1.5720, -1.3211,
                    ],
                ]]),
                position_ids: None,
                cos_cache: Tensor::from([[[1.0000, 1.0000, 1.0000], [0.5403, 0.9989, 1.0000]]]),
                sin_cache: Tensor::from([[[0.0000, 0.0000, 0.0000], [0.8415, 0.0464, 0.0022]]]),
                expected: Tensor::from([[
                    [
                        -1.0408, 0.9166, -1.3042, -1.1097, -1.2188, 1.1676, 1.0076, -0.7529,
                        -0.2250, -0.4327, -1.5071, -0.4586, -0.8663, -0.2656, 0.1665, 0.7911,
                        -0.9320, -0.8579,
                    ],
                    [
                        -0.8618, -0.0922, -0.9073, -0.7032, -0.5762, -0.2371, -0.4377, 0.5370,
                        -1.2929, -0.7267, -0.2107, -0.7115, -0.4666, -0.0261, -0.2965, -0.8469,
                        -1.5749, -1.3217,
                    ],
                ]]),
                op: RotaryEmbedding {
                    interleaved: false,
                    num_heads: 3,
                    rotary_embedding_dim: 0,
                },
            },
            // RotaryEmbedding_Interleaved_NoPosIds_SmallData_LlamaMSFT
            // Input shape [batch=1, seq=3, hidden=8] (num_heads=2, head_size=4).
            Case {
                input: Tensor::from([[
                    [
                        -1.0408, 0.9166, -1.3042, -1.1097, -0.1320, -0.2751, -0.2350, 0.0937,
                    ],
                    [
                        -1.2188, 1.1676, -1.0574, -0.1188, -0.7396, -1.2425, -0.1752, 0.6990,
                    ],
                    [
                        -0.8110, 0.6737, -1.1233, -0.0919, -0.6861, 0.7202, 0.1963, 0.6142,
                    ],
                ]]),
                position_ids: None,
                cos_cache: Tensor::from([[[1.0000, 1.0000], [0.5403, 0.9999], [-0.4161, 0.9998]]]),
                sin_cache: Tensor::from([[[0.0000, 0.0000], [0.8415, 0.0100], [0.9093, 0.0200]]]),
                expected: Tensor::from([[
                    [
                        -1.0408, 0.9166, -1.3042, -1.1097, -0.1320, -0.2751, -0.2350, 0.0937,
                    ],
                    [
                        -1.6411, -0.3948, -1.0561, -0.1294, 0.6460, -1.2937, -0.1822, 0.6972,
                    ],
                    [
                        -0.2751, -1.0178, -1.1212, -0.1143, -0.3694, -0.9235, 0.1840, 0.6180,
                    ],
                ]]),
                op: RotaryEmbedding {
                    interleaved: true,
                    num_heads: 2,
                    rotary_embedding_dim: 0,
                },
            },
        ];

        cases.test_each(|case| {
            let Case {
                input,
                position_ids,
                cos_cache,
                sin_cache,
                expected,
                op,
            } = case;

            let result: Tensor<f32> = if let Some(pos_ids) = position_ids.as_ref() {
                op.run_simple((
                    input.view(),
                    cos_cache.view(),
                    sin_cache.view(),
                    pos_ids.view(),
                ))
            } else {
                op.run_simple((input.view(), cos_cache.view(), sin_cache.view()))
            }
            .unwrap();

            expect_equal_with_tolerance(&expected.view(), &result.view(), 1e-4, 0.0).unwrap();
        });
    }

    // Exercises batch sizes > 1.
    #[test]
    fn test_rotary_embedding_batched() {
        #[derive(Debug)]
        struct Case {
            input: Tensor<f32>,
            cos_cache: Tensor<f32>,
            sin_cache: Tensor<f32>,
            expected: Tensor<f32>,
        }

        // All cases use `num_heads=1`, `head_size=2` and non-interleaved rotation,
        // so with `half=1` the rotation reduces to:
        //   y[0] = x[0]*cos - x[1]*sin
        //   y[1] = x[0]*sin + x[1]*cos
        let cases = [
            // Distinct cache row per batch (cos/sin shape [batch=2, seq=1, 1]).
            // Batch 0 uses cos=1,sin=0 (identity); batch 1 uses cos=0,sin=1
            // (90-degree rotation).
            Case {
                input: Tensor::from([[[1.0, 2.0]], [[3.0, 4.0]]]),
                cos_cache: Tensor::from([[[1.0]], [[0.0]]]),
                sin_cache: Tensor::from([[[0.0]], [[1.0]]]),
                expected: Tensor::from([[[1.0, 2.0]], [[-4.0, 3.0]]]),
            },
            // Single-batch cache (shape [1, seq=1, 1]) broadcast across batch=2.
            // Both batches use cos=0,sin=1.
            Case {
                input: Tensor::from([[[1.0, 2.0]], [[3.0, 4.0]]]),
                cos_cache: Tensor::from([[[0.0]]]),
                sin_cache: Tensor::from([[[1.0]]]),
                expected: Tensor::from([[[-2.0, 1.0]], [[-4.0, 3.0]]]),
            },
        ];

        cases.test_each(|case| {
            let Case {
                input,
                cos_cache,
                sin_cache,
                expected,
            } = case;

            let op = RotaryEmbedding {
                interleaved: false,
                num_heads: 1,
                rotary_embedding_dim: 0,
            };
            let result: Tensor<f32> = op
                .run_simple((input.view(), cos_cache.view(), sin_cache.view()))
                .unwrap();

            expect_equal_with_tolerance(&expected.view(), &result.view(), 1e-4, 0.0).unwrap();
        });
    }

    #[test]
    fn test_reject_indivisible_hidden_size() {
        let op = RotaryEmbedding {
            interleaved: false,
            num_heads: 2,
            rotary_embedding_dim: 0,
        };

        let input_data = Tensor::from([[0., 0., 0., 0., 0.]]).with_new_axis(0);
        let cos_cache = Tensor::from([[[1.0]]]);
        let sin_cache = Tensor::from([[[0.0]]]);
        let mut input_list = InputList::new();
        input_list.push(input_data.view());
        input_list.push(cos_cache.view());
        input_list.push(sin_cache.view());

        let pool = BufferPool::new();
        let ctx = OpRunContext::new(&pool, &input_list, OutputMask::all_used(1));

        assert!(op.run(&ctx).is_err());
    }

    #[test]
    fn test_reject_zero_or_odd_rotary_embedding_dim() {
        for rotary_embedding_dim in [0, 1] {
            let op = RotaryEmbedding {
                interleaved: false,
                num_heads: 1,
                rotary_embedding_dim,
            };

            let input = Tensor::from([[[1.]]]);
            let cos_cache = Tensor::<f32>::zeros(&[1, 0]);
            let sin_cache = Tensor::<f32>::zeros(&[1, 0]);

            let result =
                op.run_simple::<_, Tensor<f32>>((input.view(), cos_cache.view(), sin_cache.view()));

            assert_eq!(
                result,
                Err(OpError::InvalidValue(
                    "rotary_embedding_dim must be a positive even number"
                ))
            );
        }
    }
}