Skip to main content

burn_flex/ops/
int.rs

1//! Int tensor operations for the Flex backend.
2
3use alloc::vec::Vec;
4use burn_backend::{
5    DType, Distribution, ExecutionError, FloatDType, Scalar, TensorData, TensorMetadata,
6    ops::IntTensorOps,
7    tensor::{BoolTensor, Device, FloatTensor, IntTensor},
8};
9use burn_std::{Bytes, IntDType, Shape, Slice, bf16, f16};
10use num_traits::ToPrimitive;
11
12use crate::Layout;
13use crate::ops::binary::{binary_op_typed, int_binary_op, int_scalar_op, scalar_op_typed};
14use crate::{Flex, FlexTensor, ops::matmul};
15
16/// Convert a Scalar to (i64, u64) pair for the given dtype.
17/// Only the matching type's conversion is validated; the other gets a dummy 0.
18fn scalar_to_int_pair(dtype: DType, rhs: &Scalar) -> (i64, u64) {
19    if dtype == DType::U64 {
20        (0, rhs.to_u64().unwrap())
21    } else {
22        (rhs.to_i64().unwrap(), 0)
23    }
24}
25
26impl IntTensorOps<Flex> for Flex {
27    fn int_from_data(data: TensorData, _device: &Device<Flex>) -> IntTensor<Flex> {
28        FlexTensor::from_data(data)
29    }
30
31    async fn int_into_data(tensor: IntTensor<Flex>) -> Result<TensorData, ExecutionError> {
32        Ok(tensor.into_data())
33    }
34
35    fn int_to_device(tensor: IntTensor<Flex>, _device: &Device<Flex>) -> IntTensor<Flex> {
36        tensor
37    }
38
39    fn int_cat(tensors: Vec<IntTensor<Flex>>, dim: usize) -> IntTensor<Flex> {
40        crate::ops::cat::cat(tensors, dim)
41    }
42
43    fn int_reshape(tensor: IntTensor<Flex>, shape: Shape) -> IntTensor<Flex> {
44        tensor.reshape(shape)
45    }
46
47    fn int_slice(tensor: IntTensor<Flex>, slices: &[Slice]) -> IntTensor<Flex> {
48        crate::ops::slice::slice(tensor, slices)
49    }
50
51    fn int_empty(shape: Shape, _device: &Device<Flex>, dtype: IntDType) -> IntTensor<Flex> {
52        FlexTensor::empty(shape, dtype.into())
53    }
54
55    fn int_mask_where(
56        tensor: IntTensor<Flex>,
57        mask: BoolTensor<Flex>,
58        value: IntTensor<Flex>,
59    ) -> IntTensor<Flex> {
60        debug_assert_eq!(
61            tensor.dtype(),
62            value.dtype(),
63            "int_mask_where: dtype mismatch"
64        );
65        match tensor.dtype() {
66            DType::I64 => crate::ops::mask::mask_where::<i64>(tensor, mask, value),
67            DType::I32 => crate::ops::mask::mask_where::<i32>(tensor, mask, value),
68            DType::I16 => crate::ops::mask::mask_where::<i16>(tensor, mask, value),
69            DType::I8 => crate::ops::mask::mask_where::<i8>(tensor, mask, value),
70            DType::U64 => crate::ops::mask::mask_where::<u64>(tensor, mask, value),
71            DType::U32 => crate::ops::mask::mask_where::<u32>(tensor, mask, value),
72            DType::U16 => crate::ops::mask::mask_where::<u16>(tensor, mask, value),
73            DType::U8 => crate::ops::mask::mask_where::<u8>(tensor, mask, value),
74            dt => panic!("int_mask_where: unsupported dtype {:?}", dt),
75        }
76    }
77
78    fn int_mask_fill(
79        tensor: IntTensor<Flex>,
80        mask: BoolTensor<Flex>,
81        value: Scalar,
82    ) -> IntTensor<Flex> {
83        match tensor.dtype() {
84            DType::I64 => crate::ops::mask::mask_fill(tensor, mask, value.to_i64().unwrap()),
85            DType::I32 => crate::ops::mask::mask_fill(tensor, mask, value.to_i64().unwrap() as i32),
86            DType::I16 => crate::ops::mask::mask_fill(tensor, mask, value.to_i64().unwrap() as i16),
87            DType::I8 => crate::ops::mask::mask_fill(tensor, mask, value.to_i64().unwrap() as i8),
88            DType::U64 => crate::ops::mask::mask_fill(tensor, mask, value.to_u64().unwrap()),
89            DType::U32 => crate::ops::mask::mask_fill(tensor, mask, value.to_u64().unwrap() as u32),
90            DType::U16 => crate::ops::mask::mask_fill(tensor, mask, value.to_u64().unwrap() as u16),
91            DType::U8 => crate::ops::mask::mask_fill(tensor, mask, value.to_u64().unwrap() as u8),
92            dt => panic!("int_mask_fill: unsupported dtype {:?}", dt),
93        }
94    }
95
96    fn int_slice_assign(
97        tensor: IntTensor<Flex>,
98        slices: &[Slice],
99        value: IntTensor<Flex>,
100    ) -> IntTensor<Flex> {
101        crate::ops::slice::slice_assign(tensor, slices, value)
102    }
103
104    /// Gather ints along `dim` at the given indices.
105    ///
106    /// The `tensor` dispatches on its own int dtype (I8/I16/I32/I64 signed or
107    /// U8/U16/U32/U64 unsigned). The `indices` tensor may be any of those
108    /// widths too - it's normalised to `isize` by the shared `read_indices`
109    /// helper in `ops::gather_scatter` before the kernel runs, so callers are
110    /// not required to pre-convert to I64.
111    fn int_gather(
112        dim: usize,
113        tensor: IntTensor<Flex>,
114        indices: IntTensor<Flex>,
115    ) -> IntTensor<Flex> {
116        match tensor.dtype() {
117            DType::I64 => crate::ops::gather_scatter::gather::<i64>(tensor, dim, indices),
118            DType::I32 => crate::ops::gather_scatter::gather::<i32>(tensor, dim, indices),
119            DType::I16 => crate::ops::gather_scatter::gather::<i16>(tensor, dim, indices),
120            DType::I8 => crate::ops::gather_scatter::gather::<i8>(tensor, dim, indices),
121            DType::U64 => crate::ops::gather_scatter::gather::<u64>(tensor, dim, indices),
122            DType::U32 => crate::ops::gather_scatter::gather::<u32>(tensor, dim, indices),
123            DType::U16 => crate::ops::gather_scatter::gather::<u16>(tensor, dim, indices),
124            DType::U8 => crate::ops::gather_scatter::gather::<u8>(tensor, dim, indices),
125            dt => panic!("int_gather: unsupported dtype {:?}", dt),
126        }
127    }
128
129    /// Scatter-add int values at the given indices along `dim`.
130    ///
131    /// `tensor` and `value` must share the same int dtype; `indices` may be
132    /// any supported int width. See [`int_gather`](Self::int_gather) for the
133    /// full index-width policy.
134    fn int_scatter_add(
135        dim: usize,
136        tensor: IntTensor<Flex>,
137        indices: IntTensor<Flex>,
138        value: IntTensor<Flex>,
139    ) -> IntTensor<Flex> {
140        debug_assert_eq!(
141            tensor.dtype(),
142            value.dtype(),
143            "int_scatter_add: dtype mismatch"
144        );
145        match tensor.dtype() {
146            DType::I64 => {
147                crate::ops::gather_scatter::scatter_add::<i64>(tensor, dim, indices, value)
148            }
149            DType::I32 => {
150                crate::ops::gather_scatter::scatter_add::<i32>(tensor, dim, indices, value)
151            }
152            DType::I16 => {
153                crate::ops::gather_scatter::scatter_add::<i16>(tensor, dim, indices, value)
154            }
155            DType::I8 => crate::ops::gather_scatter::scatter_add::<i8>(tensor, dim, indices, value),
156            DType::U64 => {
157                crate::ops::gather_scatter::scatter_add::<u64>(tensor, dim, indices, value)
158            }
159            DType::U32 => {
160                crate::ops::gather_scatter::scatter_add::<u32>(tensor, dim, indices, value)
161            }
162            DType::U16 => {
163                crate::ops::gather_scatter::scatter_add::<u16>(tensor, dim, indices, value)
164            }
165            DType::U8 => crate::ops::gather_scatter::scatter_add::<u8>(tensor, dim, indices, value),
166            dt => panic!("int_scatter_add: unsupported dtype {:?}", dt),
167        }
168    }
169
170    fn int_scatter_nd(
171        data: IntTensor<Flex>,
172        indices: IntTensor<Flex>,
173        values: IntTensor<Flex>,
174        reduction: burn_backend::tensor::IndexingUpdateOp,
175    ) -> IntTensor<Flex> {
176        match data.dtype() {
177            DType::I64 => {
178                crate::ops::gather_scatter::scatter_nd::<i64>(data, indices, values, reduction)
179            }
180            DType::I32 => {
181                crate::ops::gather_scatter::scatter_nd::<i32>(data, indices, values, reduction)
182            }
183            DType::I16 => {
184                crate::ops::gather_scatter::scatter_nd::<i16>(data, indices, values, reduction)
185            }
186            DType::I8 => {
187                crate::ops::gather_scatter::scatter_nd::<i8>(data, indices, values, reduction)
188            }
189            DType::U64 => {
190                crate::ops::gather_scatter::scatter_nd::<u64>(data, indices, values, reduction)
191            }
192            DType::U32 => {
193                crate::ops::gather_scatter::scatter_nd::<u32>(data, indices, values, reduction)
194            }
195            DType::U16 => {
196                crate::ops::gather_scatter::scatter_nd::<u16>(data, indices, values, reduction)
197            }
198            DType::U8 => {
199                crate::ops::gather_scatter::scatter_nd::<u8>(data, indices, values, reduction)
200            }
201            dt => panic!("int_scatter_nd: unsupported dtype {:?}", dt),
202        }
203    }
204
205    fn int_gather_nd(data: IntTensor<Flex>, indices: IntTensor<Flex>) -> IntTensor<Flex> {
206        match data.dtype() {
207            DType::I64 => crate::ops::gather_scatter::gather_nd::<i64>(data, indices),
208            DType::I32 => crate::ops::gather_scatter::gather_nd::<i32>(data, indices),
209            DType::I16 => crate::ops::gather_scatter::gather_nd::<i16>(data, indices),
210            DType::I8 => crate::ops::gather_scatter::gather_nd::<i8>(data, indices),
211            DType::U64 => crate::ops::gather_scatter::gather_nd::<u64>(data, indices),
212            DType::U32 => crate::ops::gather_scatter::gather_nd::<u32>(data, indices),
213            DType::U16 => crate::ops::gather_scatter::gather_nd::<u16>(data, indices),
214            DType::U8 => crate::ops::gather_scatter::gather_nd::<u8>(data, indices),
215            dt => panic!("int_gather_nd: unsupported dtype {:?}", dt),
216        }
217    }
218
219    /// Select ints along `dim` by a 1D index tensor.
220    ///
221    /// The `indices` tensor may be any supported int width. See
222    /// [`int_gather`](Self::int_gather) for the full index-width policy.
223    fn int_select(
224        tensor: IntTensor<Flex>,
225        dim: usize,
226        indices: IntTensor<Flex>,
227    ) -> IntTensor<Flex> {
228        match tensor.dtype() {
229            DType::I64 => crate::ops::gather_scatter::select::<i64>(tensor, dim, indices),
230            DType::I32 => crate::ops::gather_scatter::select::<i32>(tensor, dim, indices),
231            DType::I16 => crate::ops::gather_scatter::select::<i16>(tensor, dim, indices),
232            DType::I8 => crate::ops::gather_scatter::select::<i8>(tensor, dim, indices),
233            DType::U64 => crate::ops::gather_scatter::select::<u64>(tensor, dim, indices),
234            DType::U32 => crate::ops::gather_scatter::select::<u32>(tensor, dim, indices),
235            DType::U16 => crate::ops::gather_scatter::select::<u16>(tensor, dim, indices),
236            DType::U8 => crate::ops::gather_scatter::select::<u8>(tensor, dim, indices),
237            dt => panic!("int_select: unsupported dtype {:?}", dt),
238        }
239    }
240
241    /// Select-add int values at a 1D index tensor along `dim`.
242    ///
243    /// `tensor` and `value` must share the same int dtype; `indices` may be
244    /// any supported int width. See [`int_gather`](Self::int_gather) for the
245    /// full index-width policy.
246    fn int_select_add(
247        tensor: IntTensor<Flex>,
248        dim: usize,
249        indices: IntTensor<Flex>,
250        value: IntTensor<Flex>,
251    ) -> IntTensor<Flex> {
252        debug_assert_eq!(
253            tensor.dtype(),
254            value.dtype(),
255            "int_select_add: dtype mismatch"
256        );
257        match tensor.dtype() {
258            DType::I64 => {
259                crate::ops::gather_scatter::select_add::<i64>(tensor, dim, indices, value)
260            }
261            DType::I32 => {
262                crate::ops::gather_scatter::select_add::<i32>(tensor, dim, indices, value)
263            }
264            DType::I16 => {
265                crate::ops::gather_scatter::select_add::<i16>(tensor, dim, indices, value)
266            }
267            DType::I8 => crate::ops::gather_scatter::select_add::<i8>(tensor, dim, indices, value),
268            DType::U64 => {
269                crate::ops::gather_scatter::select_add::<u64>(tensor, dim, indices, value)
270            }
271            DType::U32 => {
272                crate::ops::gather_scatter::select_add::<u32>(tensor, dim, indices, value)
273            }
274            DType::U16 => {
275                crate::ops::gather_scatter::select_add::<u16>(tensor, dim, indices, value)
276            }
277            DType::U8 => crate::ops::gather_scatter::select_add::<u8>(tensor, dim, indices, value),
278            dt => panic!("int_select_add: unsupported dtype {:?}", dt),
279        }
280    }
281
282    fn int_equal(
283        lhs: IntTensor<Flex>,
284        rhs: IntTensor<Flex>,
285        out_dtype: burn_std::BoolDType,
286    ) -> BoolTensor<Flex> {
287        crate::ops::comparison::int_equal(lhs, rhs, out_dtype)
288    }
289
290    fn int_equal_elem(
291        lhs: IntTensor<Flex>,
292        rhs: Scalar,
293        out_dtype: burn_std::BoolDType,
294    ) -> BoolTensor<Flex> {
295        let (i, u) = scalar_to_int_pair(lhs.dtype(), &rhs);
296        crate::ops::comparison::int_equal_elem(lhs, i, u, out_dtype)
297    }
298
299    fn int_greater(
300        lhs: IntTensor<Flex>,
301        rhs: IntTensor<Flex>,
302        out_dtype: burn_std::BoolDType,
303    ) -> BoolTensor<Flex> {
304        crate::ops::comparison::int_greater(lhs, rhs, out_dtype)
305    }
306
307    fn int_greater_elem(
308        lhs: IntTensor<Flex>,
309        rhs: Scalar,
310        out_dtype: burn_std::BoolDType,
311    ) -> BoolTensor<Flex> {
312        let (i, u) = scalar_to_int_pair(lhs.dtype(), &rhs);
313        crate::ops::comparison::int_greater_elem(lhs, i, u, out_dtype)
314    }
315
316    fn int_greater_equal(
317        lhs: IntTensor<Flex>,
318        rhs: IntTensor<Flex>,
319        out_dtype: burn_std::BoolDType,
320    ) -> BoolTensor<Flex> {
321        crate::ops::comparison::int_greater_equal(lhs, rhs, out_dtype)
322    }
323
324    fn int_greater_equal_elem(
325        lhs: IntTensor<Flex>,
326        rhs: Scalar,
327        out_dtype: burn_std::BoolDType,
328    ) -> BoolTensor<Flex> {
329        let (i, u) = scalar_to_int_pair(lhs.dtype(), &rhs);
330        crate::ops::comparison::int_greater_equal_elem(lhs, i, u, out_dtype)
331    }
332
333    fn int_lower(
334        lhs: IntTensor<Flex>,
335        rhs: IntTensor<Flex>,
336        out_dtype: burn_std::BoolDType,
337    ) -> BoolTensor<Flex> {
338        crate::ops::comparison::int_lower(lhs, rhs, out_dtype)
339    }
340
341    fn int_lower_elem(
342        lhs: IntTensor<Flex>,
343        rhs: Scalar,
344        out_dtype: burn_std::BoolDType,
345    ) -> BoolTensor<Flex> {
346        let (i, u) = scalar_to_int_pair(lhs.dtype(), &rhs);
347        crate::ops::comparison::int_lower_elem(lhs, i, u, out_dtype)
348    }
349
350    fn int_lower_equal(
351        lhs: IntTensor<Flex>,
352        rhs: IntTensor<Flex>,
353        out_dtype: burn_std::BoolDType,
354    ) -> BoolTensor<Flex> {
355        crate::ops::comparison::int_lower_equal(lhs, rhs, out_dtype)
356    }
357
358    fn int_lower_equal_elem(
359        lhs: IntTensor<Flex>,
360        rhs: Scalar,
361        out_dtype: burn_std::BoolDType,
362    ) -> BoolTensor<Flex> {
363        let (i, u) = scalar_to_int_pair(lhs.dtype(), &rhs);
364        crate::ops::comparison::int_lower_equal_elem(lhs, i, u, out_dtype)
365    }
366
367    fn int_add(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
368        int_binary_op(lhs, rhs, |a, b| a + b)
369    }
370
371    fn int_add_scalar(lhs: IntTensor<Flex>, rhs: Scalar) -> IntTensor<Flex> {
372        if lhs.dtype() == DType::U64 {
373            return scalar_op_typed(lhs, rhs.to_u64().unwrap(), |a: u64, b: u64| {
374                a.wrapping_add(b)
375            });
376        }
377        int_scalar_op(lhs, rhs.to_i64().unwrap(), |a, b| a + b)
378    }
379
380    fn int_sub(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
381        int_binary_op(lhs, rhs, |a, b| a - b)
382    }
383
384    fn int_sub_scalar(lhs: IntTensor<Flex>, rhs: Scalar) -> IntTensor<Flex> {
385        if lhs.dtype() == DType::U64 {
386            return scalar_op_typed(lhs, rhs.to_u64().unwrap(), |a: u64, b: u64| {
387                a.wrapping_sub(b)
388            });
389        }
390        int_scalar_op(lhs, rhs.to_i64().unwrap(), |a, b| a - b)
391    }
392
393    fn int_mul(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
394        int_binary_op(lhs, rhs, |a, b| a * b)
395    }
396
397    fn int_mul_scalar(lhs: IntTensor<Flex>, rhs: Scalar) -> IntTensor<Flex> {
398        if lhs.dtype() == DType::U64 {
399            return scalar_op_typed(lhs, rhs.to_u64().unwrap(), |a: u64, b: u64| {
400                a.wrapping_mul(b)
401            });
402        }
403        int_scalar_op(lhs, rhs.to_i64().unwrap(), |a, b| a * b)
404    }
405
406    fn int_div(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
407        // U64 values > i64::MAX produce wrong results through i64 cast
408        if lhs.dtype() == DType::U64 {
409            let (lhs, rhs) = crate::ops::expand::broadcast_binary(lhs, rhs);
410            return binary_op_typed(lhs, rhs, |a: u64, b: u64| a / b);
411        }
412        int_binary_op(lhs, rhs, |a, b| a / b)
413    }
414
415    fn int_div_scalar(lhs: IntTensor<Flex>, rhs: Scalar) -> IntTensor<Flex> {
416        if lhs.dtype() == DType::U64 {
417            return scalar_op_typed(lhs, rhs.to_u64().unwrap(), |a: u64, b: u64| a / b);
418        }
419        int_scalar_op(lhs, rhs.to_i64().unwrap(), |a, b| a / b)
420    }
421
422    fn int_remainder(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
423        // U64 values > i64::MAX produce wrong results through i64 cast
424        if lhs.dtype() == DType::U64 {
425            let (lhs, rhs) = crate::ops::expand::broadcast_binary(lhs, rhs);
426            return binary_op_typed(lhs, rhs, |a: u64, b: u64| a % b);
427        }
428        // Python/PyTorch-style remainder: result has same sign as divisor
429        int_binary_op(lhs, rhs, |a, b| ((a % b) + b) % b)
430    }
431
432    fn int_remainder_scalar(lhs: IntTensor<Flex>, rhs: Scalar) -> IntTensor<Flex> {
433        if lhs.dtype() == DType::U64 {
434            return scalar_op_typed(lhs, rhs.to_u64().unwrap(), |a: u64, b: u64| a % b);
435        }
436        // Python/PyTorch-style remainder: result has same sign as divisor
437        int_scalar_op(lhs, rhs.to_i64().unwrap(), |a, b| ((a % b) + b) % b)
438    }
439
440    // Precision limits: i64/u64 > 2^24 for f32/f16/bf16, > 2^53 for f64.
441    fn int_into_float(
442        tensor: IntTensor<Flex>,
443        out_dtype: burn_std::FloatDType,
444    ) -> FloatTensor<Flex> {
445        let tensor = tensor.to_contiguous();
446        let shape = tensor.layout().shape().clone();
447        let src = tensor.dtype();
448        let out_dt = DType::from(out_dtype);
449
450        // Read source ints, applying conversion per-element.
451        // Each arm binds `$x` to the native int value; `$conv` must work for all int types.
452        macro_rules! read_ints {
453            (|$x:ident| $conv:expr) => {
454                match src {
455                    DType::I64 => tensor.storage::<i64>().iter().map(|&$x| $conv).collect(),
456                    DType::I32 => tensor.storage::<i32>().iter().map(|&$x| $conv).collect(),
457                    DType::I16 => tensor.storage::<i16>().iter().map(|&$x| $conv).collect(),
458                    DType::I8 => tensor.storage::<i8>().iter().map(|&$x| $conv).collect(),
459                    DType::U64 => tensor.storage::<u64>().iter().map(|&$x| $conv).collect(),
460                    DType::U32 => tensor.storage::<u32>().iter().map(|&$x| $conv).collect(),
461                    DType::U16 => tensor.storage::<u16>().iter().map(|&$x| $conv).collect(),
462                    DType::U8 => tensor.storage::<u8>().iter().map(|&$x| $conv).collect(),
463                    _ => panic!("int_into_float: unsupported source dtype {:?}", src),
464                }
465            };
466        }
467
468        match out_dtype {
469            FloatDType::F64 => {
470                let data: Vec<f64> = read_ints!(|x| x as f64);
471                FlexTensor::new(Bytes::from_elems(data), Layout::contiguous(shape), out_dt)
472            }
473            FloatDType::F32 | FloatDType::Flex32 => {
474                let data: Vec<f32> = read_ints!(|x| x as f32);
475                FlexTensor::new(Bytes::from_elems(data), Layout::contiguous(shape), out_dt)
476            }
477            FloatDType::F16 => {
478                let data: Vec<f16> = read_ints!(|x| f16::from_f32(x as f32));
479                FlexTensor::new(Bytes::from_elems(data), Layout::contiguous(shape), out_dt)
480            }
481            FloatDType::BF16 => {
482                let data: Vec<bf16> = read_ints!(|x| bf16::from_f32(x as f32));
483                FlexTensor::new(Bytes::from_elems(data), Layout::contiguous(shape), out_dt)
484            }
485        }
486    }
487
488    fn int_swap_dims(tensor: IntTensor<Flex>, dim1: usize, dim2: usize) -> IntTensor<Flex> {
489        tensor.transpose(dim1, dim2)
490    }
491
492    fn int_permute(tensor: IntTensor<Flex>, axes: &[usize]) -> IntTensor<Flex> {
493        tensor.permute(axes)
494    }
495
496    fn int_flip(tensor: IntTensor<Flex>, axes: &[usize]) -> IntTensor<Flex> {
497        crate::ops::flip::flip(tensor, axes)
498    }
499
500    fn int_random(
501        shape: Shape,
502        distribution: Distribution,
503        _device: &Device<Flex>,
504        dtype: IntDType,
505    ) -> IntTensor<Flex> {
506        let mut seed = crate::backend::SEED.lock();
507        let mut rng = seed.take().unwrap_or_else(crate::backend::get_seeded_rng);
508        let data = match dtype {
509            IntDType::I64 => TensorData::random::<i64, _, _>(shape, distribution, &mut rng),
510            IntDType::I32 => TensorData::random::<i32, _, _>(shape, distribution, &mut rng),
511            IntDType::I16 => TensorData::random::<i16, _, _>(shape, distribution, &mut rng),
512            IntDType::I8 => TensorData::random::<i8, _, _>(shape, distribution, &mut rng),
513            IntDType::U64 => TensorData::random::<u64, _, _>(shape, distribution, &mut rng),
514            IntDType::U32 => TensorData::random::<u32, _, _>(shape, distribution, &mut rng),
515            IntDType::U16 => TensorData::random::<u16, _, _>(shape, distribution, &mut rng),
516            IntDType::U8 => TensorData::random::<u8, _, _>(shape, distribution, &mut rng),
517        };
518        *seed = Some(rng);
519        FlexTensor::from_data(data)
520    }
521
522    fn int_expand(tensor: IntTensor<Flex>, shape: Shape) -> IntTensor<Flex> {
523        crate::ops::expand::expand(tensor, shape)
524    }
525
526    fn int_matmul(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
527        matmul::int_matmul(lhs, rhs)
528    }
529
530    fn int_sum(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
531        crate::ops::reduce::sum(tensor)
532    }
533
534    fn int_sum_dim(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
535        crate::ops::reduce::sum_dim(tensor, dim)
536    }
537
538    fn int_prod(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
539        crate::ops::reduce::prod(tensor)
540    }
541
542    fn int_prod_dim(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
543        crate::ops::reduce::prod_dim(tensor, dim)
544    }
545
546    fn int_mean_dim(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
547        crate::ops::reduce::mean_dim(tensor, dim)
548    }
549
550    fn int_cumsum(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
551        match tensor.dtype() {
552            DType::I64 => crate::ops::cumulative::cumsum::<i64>(tensor, dim),
553            DType::I32 => crate::ops::cumulative::cumsum::<i32>(tensor, dim),
554            DType::I16 => crate::ops::cumulative::cumsum::<i16>(tensor, dim),
555            DType::I8 => crate::ops::cumulative::cumsum::<i8>(tensor, dim),
556            DType::U64 => crate::ops::cumulative::cumsum::<u64>(tensor, dim),
557            DType::U32 => crate::ops::cumulative::cumsum::<u32>(tensor, dim),
558            DType::U16 => crate::ops::cumulative::cumsum::<u16>(tensor, dim),
559            DType::U8 => crate::ops::cumulative::cumsum::<u8>(tensor, dim),
560            dt => panic!("int_cumsum: unsupported dtype {:?}", dt),
561        }
562    }
563
564    fn int_cumprod(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
565        match tensor.dtype() {
566            DType::I64 => crate::ops::cumulative::cumprod::<i64>(tensor, dim),
567            DType::I32 => crate::ops::cumulative::cumprod::<i32>(tensor, dim),
568            DType::I16 => crate::ops::cumulative::cumprod::<i16>(tensor, dim),
569            DType::I8 => crate::ops::cumulative::cumprod::<i8>(tensor, dim),
570            DType::U64 => crate::ops::cumulative::cumprod::<u64>(tensor, dim),
571            DType::U32 => crate::ops::cumulative::cumprod::<u32>(tensor, dim),
572            DType::U16 => crate::ops::cumulative::cumprod::<u16>(tensor, dim),
573            DType::U8 => crate::ops::cumulative::cumprod::<u8>(tensor, dim),
574            dt => panic!("int_cumprod: unsupported dtype {:?}", dt),
575        }
576    }
577
578    fn int_cummin(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
579        match tensor.dtype() {
580            DType::I64 => crate::ops::cumulative::cummin::<i64>(tensor, dim),
581            DType::I32 => crate::ops::cumulative::cummin::<i32>(tensor, dim),
582            DType::I16 => crate::ops::cumulative::cummin::<i16>(tensor, dim),
583            DType::I8 => crate::ops::cumulative::cummin::<i8>(tensor, dim),
584            DType::U64 => crate::ops::cumulative::cummin::<u64>(tensor, dim),
585            DType::U32 => crate::ops::cumulative::cummin::<u32>(tensor, dim),
586            DType::U16 => crate::ops::cumulative::cummin::<u16>(tensor, dim),
587            DType::U8 => crate::ops::cumulative::cummin::<u8>(tensor, dim),
588            dt => panic!("int_cummin: unsupported dtype {:?}", dt),
589        }
590    }
591
592    fn int_cummax(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
593        match tensor.dtype() {
594            DType::I64 => crate::ops::cumulative::cummax::<i64>(tensor, dim),
595            DType::I32 => crate::ops::cumulative::cummax::<i32>(tensor, dim),
596            DType::I16 => crate::ops::cumulative::cummax::<i16>(tensor, dim),
597            DType::I8 => crate::ops::cumulative::cummax::<i8>(tensor, dim),
598            DType::U64 => crate::ops::cumulative::cummax::<u64>(tensor, dim),
599            DType::U32 => crate::ops::cumulative::cummax::<u32>(tensor, dim),
600            DType::U16 => crate::ops::cumulative::cummax::<u16>(tensor, dim),
601            DType::U8 => crate::ops::cumulative::cummax::<u8>(tensor, dim),
602            dt => panic!("int_cummax: unsupported dtype {:?}", dt),
603        }
604    }
605
606    fn int_argmax(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
607        crate::ops::reduce::argmax(tensor, dim)
608    }
609
610    fn int_argmin(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
611        crate::ops::reduce::argmin(tensor, dim)
612    }
613
614    fn int_abs(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
615        crate::ops::unary::int_abs(tensor)
616    }
617
618    fn bitwise_and(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
619        int_binary_op(lhs, rhs, |a, b| a & b)
620    }
621
622    fn bitwise_and_scalar(lhs: IntTensor<Flex>, rhs: Scalar) -> IntTensor<Flex> {
623        if lhs.dtype() == DType::U64 {
624            return scalar_op_typed(lhs, rhs.to_u64().unwrap(), |a: u64, b: u64| a & b);
625        }
626        int_scalar_op(lhs, rhs.to_i64().unwrap(), |a, b| a & b)
627    }
628
629    fn bitwise_or(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
630        int_binary_op(lhs, rhs, |a, b| a | b)
631    }
632
633    fn bitwise_or_scalar(lhs: IntTensor<Flex>, rhs: Scalar) -> IntTensor<Flex> {
634        if lhs.dtype() == DType::U64 {
635            return scalar_op_typed(lhs, rhs.to_u64().unwrap(), |a: u64, b: u64| a | b);
636        }
637        int_scalar_op(lhs, rhs.to_i64().unwrap(), |a, b| a | b)
638    }
639
640    fn bitwise_xor(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
641        int_binary_op(lhs, rhs, |a, b| a ^ b)
642    }
643
644    fn bitwise_xor_scalar(lhs: IntTensor<Flex>, rhs: Scalar) -> IntTensor<Flex> {
645        if lhs.dtype() == DType::U64 {
646            return scalar_op_typed(lhs, rhs.to_u64().unwrap(), |a: u64, b: u64| a ^ b);
647        }
648        int_scalar_op(lhs, rhs.to_i64().unwrap(), |a, b| a ^ b)
649    }
650
651    fn bitwise_not(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
652        // Use scalar op with dummy value, only applying NOT to lhs
653        int_scalar_op(tensor, 0, |a, _| !a)
654    }
655
656    // Shift amounts masked to type width via wrapping_shl/wrapping_shr.
657    fn bitwise_left_shift(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
658        int_binary_op(lhs, rhs, |a, b| a.wrapping_shl(b as u32))
659    }
660
661    fn bitwise_left_shift_scalar(lhs: IntTensor<Flex>, rhs: Scalar) -> IntTensor<Flex> {
662        int_scalar_op(lhs, rhs.to_i64().unwrap(), |a, b| a.wrapping_shl(b as u32))
663    }
664
665    fn bitwise_right_shift(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
666        int_binary_op(lhs, rhs, |a, b| a.wrapping_shr(b as u32))
667    }
668
669    fn bitwise_right_shift_scalar(lhs: IntTensor<Flex>, rhs: Scalar) -> IntTensor<Flex> {
670        int_scalar_op(lhs, rhs.to_i64().unwrap(), |a, b| a.wrapping_shr(b as u32))
671    }
672
673    fn int_cast(tensor: IntTensor<Flex>, dtype: IntDType) -> IntTensor<Flex> {
674        let target_dtype: DType = dtype.into();
675
676        // If already the target dtype, return as-is
677        if tensor.dtype() == target_dtype {
678            return tensor;
679        }
680
681        // Make contiguous for easier iteration
682        let tensor = tensor.to_contiguous();
683        let shape = tensor.layout().shape().clone();
684
685        // Helper macro to convert between types
686        macro_rules! cast_impl {
687            ($storage:ident, $dst_type:ty) => {{
688                Some(Bytes::from_elems(
689                    $storage
690                        .iter()
691                        .map(|&x| x as $dst_type)
692                        .collect::<Vec<$dst_type>>(),
693                ))
694            }};
695        }
696
697        // Match source dtype to target dtype
698        let bytes = match tensor.dtype() {
699            // From I64
700            DType::I64 => {
701                let storage: &[i64] = tensor.storage();
702                match target_dtype {
703                    DType::I32 => cast_impl!(storage, i32),
704                    DType::I16 => cast_impl!(storage, i16),
705                    DType::I8 => cast_impl!(storage, i8),
706                    DType::U64 => cast_impl!(storage, u64),
707                    DType::U32 => cast_impl!(storage, u32),
708                    DType::U16 => cast_impl!(storage, u16),
709                    DType::U8 => cast_impl!(storage, u8),
710                    _ => None,
711                }
712            }
713
714            // From I32
715            DType::I32 => {
716                let storage: &[i32] = tensor.storage();
717                match target_dtype {
718                    DType::I64 => cast_impl!(storage, i64),
719                    DType::I16 => cast_impl!(storage, i16),
720                    DType::I8 => cast_impl!(storage, i8),
721                    DType::U64 => cast_impl!(storage, u64),
722                    DType::U32 => cast_impl!(storage, u32),
723                    DType::U16 => cast_impl!(storage, u16),
724                    DType::U8 => cast_impl!(storage, u8),
725                    _ => None,
726                }
727            }
728
729            // From I16
730            DType::I16 => {
731                let storage: &[i16] = tensor.storage();
732                match target_dtype {
733                    DType::I64 => cast_impl!(storage, i64),
734                    DType::I32 => cast_impl!(storage, i32),
735                    DType::I8 => cast_impl!(storage, i8),
736                    DType::U64 => cast_impl!(storage, u64),
737                    DType::U32 => cast_impl!(storage, u32),
738                    DType::U16 => cast_impl!(storage, u16),
739                    DType::U8 => cast_impl!(storage, u8),
740                    _ => None,
741                }
742            }
743
744            // From I8
745            DType::I8 => {
746                let storage: &[i8] = tensor.storage();
747                match target_dtype {
748                    DType::I64 => cast_impl!(storage, i64),
749                    DType::I32 => cast_impl!(storage, i32),
750                    DType::I16 => cast_impl!(storage, i16),
751                    DType::U64 => cast_impl!(storage, u64),
752                    DType::U32 => cast_impl!(storage, u32),
753                    DType::U16 => cast_impl!(storage, u16),
754                    DType::U8 => cast_impl!(storage, u8),
755                    _ => None,
756                }
757            }
758
759            // From U64
760            DType::U64 => {
761                let storage: &[u64] = tensor.storage();
762                match target_dtype {
763                    DType::I64 => cast_impl!(storage, i64),
764                    DType::I32 => cast_impl!(storage, i32),
765                    DType::I16 => cast_impl!(storage, i16),
766                    DType::I8 => cast_impl!(storage, i8),
767                    DType::U32 => cast_impl!(storage, u32),
768                    DType::U16 => cast_impl!(storage, u16),
769                    DType::U8 => cast_impl!(storage, u8),
770                    _ => None,
771                }
772            }
773
774            // From U32
775            DType::U32 => {
776                let storage: &[u32] = tensor.storage();
777                match target_dtype {
778                    DType::I64 => cast_impl!(storage, i64),
779                    DType::I32 => cast_impl!(storage, i32),
780                    DType::I16 => cast_impl!(storage, i16),
781                    DType::I8 => cast_impl!(storage, i8),
782                    DType::U64 => cast_impl!(storage, u64),
783                    DType::U16 => cast_impl!(storage, u16),
784                    DType::U8 => cast_impl!(storage, u8),
785                    _ => None,
786                }
787            }
788
789            // From U16
790            DType::U16 => {
791                let storage: &[u16] = tensor.storage();
792                match target_dtype {
793                    DType::I64 => cast_impl!(storage, i64),
794                    DType::I32 => cast_impl!(storage, i32),
795                    DType::I16 => cast_impl!(storage, i16),
796                    DType::I8 => cast_impl!(storage, i8),
797                    DType::U64 => cast_impl!(storage, u64),
798                    DType::U32 => cast_impl!(storage, u32),
799                    DType::U8 => cast_impl!(storage, u8),
800                    _ => None,
801                }
802            }
803
804            // From U8
805            DType::U8 => {
806                let storage: &[u8] = tensor.storage();
807                match target_dtype {
808                    DType::I64 => cast_impl!(storage, i64),
809                    DType::I32 => cast_impl!(storage, i32),
810                    DType::I16 => cast_impl!(storage, i16),
811                    DType::I8 => cast_impl!(storage, i8),
812                    DType::U64 => cast_impl!(storage, u64),
813                    DType::U32 => cast_impl!(storage, u32),
814                    DType::U16 => cast_impl!(storage, u16),
815                    _ => None,
816                }
817            }
818
819            _ => None,
820        };
821        let Some(bytes) = bytes else {
822            panic!(
823                "int_cast: unsupported conversion from {:?} to {:?}",
824                tensor.dtype(),
825                target_dtype
826            )
827        };
828        FlexTensor::new(bytes, Layout::contiguous(shape), target_dtype)
829    }
830
831    fn int_unfold(
832        tensor: IntTensor<Flex>,
833        dim: usize,
834        size: usize,
835        step: usize,
836    ) -> IntTensor<Flex> {
837        crate::ops::unfold::unfold_int(tensor, dim, size, step)
838    }
839
840    fn int_neg(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
841        int_scalar_op(tensor, 0i64, |a, _| a.wrapping_neg())
842    }
843
844    fn int_clamp(tensor: IntTensor<Flex>, min: Scalar, max: Scalar) -> IntTensor<Flex> {
845        if tensor.dtype() == DType::U64 {
846            let min_val = min.to_u64().unwrap();
847            let max_val = max.to_u64().unwrap();
848            return scalar_op_typed(tensor, 0u64, move |x: u64, _| x.clamp(min_val, max_val));
849        }
850        let min_val = min.to_i64().unwrap();
851        let max_val = max.to_i64().unwrap();
852        int_scalar_op(tensor, 0i64, move |x, _| x.clamp(min_val, max_val))
853    }
854
855    fn int_clamp_min(tensor: IntTensor<Flex>, min: Scalar) -> IntTensor<Flex> {
856        if tensor.dtype() == DType::U64 {
857            let min_val = min.to_u64().unwrap();
858            return scalar_op_typed(tensor, 0u64, move |x: u64, _| x.max(min_val));
859        }
860        let min_val = min.to_i64().unwrap();
861        int_scalar_op(tensor, 0i64, move |x, _| x.max(min_val))
862    }
863
864    fn int_clamp_max(tensor: IntTensor<Flex>, max: Scalar) -> IntTensor<Flex> {
865        if tensor.dtype() == DType::U64 {
866            let max_val = max.to_u64().unwrap();
867            return scalar_op_typed(tensor, 0u64, move |x: u64, _| x.min(max_val));
868        }
869        let max_val = max.to_i64().unwrap();
870        int_scalar_op(tensor, 0i64, move |x, _| x.min(max_val))
871    }
872
873    fn int_sign(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
874        if tensor.dtype() == DType::U64 {
875            return scalar_op_typed(tensor, 0u64, |x: u64, _| if x > 0 { 1 } else { 0 });
876        }
877        int_scalar_op(tensor, 0i64, |x, _| {
878            if x > 0 {
879                1
880            } else if x < 0 {
881                -1
882            } else {
883                0
884            }
885        })
886    }
887
888    fn int_mean(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
889        let n = tensor.layout().num_elements();
890        assert!(n > 0, "int_mean: cannot take mean of empty tensor");
891        let dtype = tensor.dtype();
892        let sum_result = crate::ops::reduce::sum(tensor);
893        // Compute in i64 to avoid truncation of n for small int types
894        macro_rules! compute_mean {
895            ($ty:ty) => {{
896                let data: &[$ty] = sum_result.storage();
897                let mean_val = (data[0] as i64 / n as i64) as $ty;
898                FlexTensor::new(
899                    Bytes::from_elems(alloc::vec![mean_val]),
900                    Layout::contiguous(Shape::from(alloc::vec![1])),
901                    dtype,
902                )
903            }};
904        }
905        match dtype {
906            DType::I64 => compute_mean!(i64),
907            DType::I32 => compute_mean!(i32),
908            DType::I16 => compute_mean!(i16),
909            DType::I8 => compute_mean!(i8),
910            other => panic!("int_mean: unsupported dtype {:?}", other),
911        }
912    }
913
914    fn int_max(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
915        crate::ops::reduce::max(tensor)
916    }
917
918    fn int_max_dim(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
919        crate::ops::reduce::max_dim(tensor, dim)
920    }
921
922    fn int_min(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
923        crate::ops::reduce::min(tensor)
924    }
925
926    fn int_min_dim(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
927        crate::ops::reduce::min_dim(tensor, dim)
928    }
929
930    fn int_max_dim_with_indices(
931        tensor: IntTensor<Flex>,
932        dim: usize,
933    ) -> (IntTensor<Flex>, IntTensor<Flex>) {
934        crate::ops::reduce::max_dim_with_indices(tensor, dim)
935    }
936
937    fn int_min_dim_with_indices(
938        tensor: IntTensor<Flex>,
939        dim: usize,
940    ) -> (IntTensor<Flex>, IntTensor<Flex>) {
941        crate::ops::reduce::min_dim_with_indices(tensor, dim)
942    }
943
944    fn int_any(tensor: IntTensor<Flex>, out_dtype: burn_std::BoolDType) -> BoolTensor<Flex> {
945        crate::ops::comparison::any_int(tensor, out_dtype)
946    }
947
948    fn int_any_dim(
949        tensor: IntTensor<Flex>,
950        dim: usize,
951        out_dtype: burn_std::BoolDType,
952    ) -> BoolTensor<Flex> {
953        crate::ops::comparison::any_int_dim(tensor, dim, out_dtype)
954    }
955
956    fn int_all(tensor: IntTensor<Flex>, out_dtype: burn_std::BoolDType) -> BoolTensor<Flex> {
957        crate::ops::comparison::all_int(tensor, out_dtype)
958    }
959
960    fn int_all_dim(
961        tensor: IntTensor<Flex>,
962        dim: usize,
963        out_dtype: burn_std::BoolDType,
964    ) -> BoolTensor<Flex> {
965        crate::ops::comparison::all_int_dim(tensor, dim, out_dtype)
966    }
967
968    fn int_powi(lhs: IntTensor<Flex>, rhs: IntTensor<Flex>) -> IntTensor<Flex> {
969        int_binary_op(lhs, rhs, |a, b| a.wrapping_pow(b as u32))
970    }
971
972    fn int_zeros(shape: Shape, _device: &Device<Flex>, dtype: IntDType) -> IntTensor<Flex> {
973        FlexTensor::zeros(shape, dtype.into())
974    }
975
976    fn int_ones(shape: Shape, _device: &Device<Flex>, dtype: IntDType) -> IntTensor<Flex> {
977        let dt: DType = dtype.into();
978        match dt {
979            DType::I64 => FlexTensor::filled_typed(shape, dt, 1i64),
980            DType::I32 => FlexTensor::filled_typed(shape, dt, 1i32),
981            DType::I16 => FlexTensor::filled_typed(shape, dt, 1i16),
982            DType::I8 => FlexTensor::filled_typed(shape, dt, 1i8),
983            DType::U64 => FlexTensor::filled_typed(shape, dt, 1u64),
984            DType::U32 => FlexTensor::filled_typed(shape, dt, 1u32),
985            DType::U16 => FlexTensor::filled_typed(shape, dt, 1u16),
986            DType::U8 => FlexTensor::filled_typed(shape, dt, 1u8),
987            _ => unreachable!(),
988        }
989    }
990
991    fn int_full(
992        shape: Shape,
993        fill_value: burn_backend::Scalar,
994        _device: &Device<Flex>,
995        dtype: IntDType,
996    ) -> IntTensor<Flex> {
997        let dt: DType = dtype.into();
998        let v = fill_value.to_i64().unwrap();
999        match dt {
1000            DType::I64 => FlexTensor::filled_typed(shape, dt, v),
1001            DType::I32 => FlexTensor::filled_typed(shape, dt, v as i32),
1002            DType::I16 => FlexTensor::filled_typed(shape, dt, v as i16),
1003            DType::I8 => FlexTensor::filled_typed(shape, dt, v as i8),
1004            DType::U64 => FlexTensor::filled_typed(shape, dt, v as u64),
1005            DType::U32 => FlexTensor::filled_typed(shape, dt, v as u32),
1006            DType::U16 => FlexTensor::filled_typed(shape, dt, v as u16),
1007            DType::U8 => FlexTensor::filled_typed(shape, dt, v as u8),
1008            _ => unreachable!(),
1009        }
1010    }
1011
1012    fn int_transpose(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
1013        let ndims = tensor.layout().num_dims();
1014        if ndims < 2 {
1015            return tensor;
1016        }
1017        tensor.transpose(ndims - 2, ndims - 1)
1018    }
1019
1020    fn int_repeat_dim(tensor: IntTensor<Flex>, dim: usize, times: usize) -> IntTensor<Flex> {
1021        crate::ops::repeat_dim::repeat_dim(tensor, dim, times)
1022    }
1023
1024    fn int_not_equal(
1025        lhs: IntTensor<Flex>,
1026        rhs: IntTensor<Flex>,
1027        out_dtype: burn_std::BoolDType,
1028    ) -> BoolTensor<Flex> {
1029        crate::ops::comparison::int_not_equal(lhs, rhs, out_dtype)
1030    }
1031
1032    fn int_not_equal_elem(
1033        lhs: IntTensor<Flex>,
1034        rhs: burn_backend::Scalar,
1035        out_dtype: burn_std::BoolDType,
1036    ) -> BoolTensor<Flex> {
1037        let (i, u) = scalar_to_int_pair(lhs.dtype(), &rhs);
1038        crate::ops::comparison::int_not_equal_elem(lhs, i, u, out_dtype)
1039    }
1040
1041    fn int_sort(tensor: IntTensor<Flex>, dim: usize, descending: bool) -> IntTensor<Flex> {
1042        crate::ops::sort::sort(tensor, dim, descending)
1043    }
1044
1045    fn int_sort_with_indices(
1046        tensor: IntTensor<Flex>,
1047        dim: usize,
1048        descending: bool,
1049    ) -> (IntTensor<Flex>, IntTensor<Flex>) {
1050        crate::ops::sort::sort_with_indices(tensor, dim, descending)
1051    }
1052
1053    fn int_argsort(tensor: IntTensor<Flex>, dim: usize, descending: bool) -> IntTensor<Flex> {
1054        crate::ops::sort::argsort(tensor, dim, descending)
1055    }
1056
1057    fn int_powi_scalar(lhs: IntTensor<Flex>, rhs: burn_backend::Scalar) -> IntTensor<Flex> {
1058        use num_traits::ToPrimitive;
1059        match rhs.to_i64().unwrap() {
1060            0 => Self::int_ones(lhs.shape(), &Default::default(), lhs.dtype().into()),
1061            1 => lhs,
1062            2 => Self::int_mul(lhs.clone(), lhs),
1063            _ => Self::int_powi_scalar_impl(lhs, rhs),
1064        }
1065    }
1066
1067    fn int_powi_scalar_impl(lhs: IntTensor<Flex>, rhs: burn_backend::Scalar) -> IntTensor<Flex> {
1068        use num_traits::ToPrimitive;
1069        let exp = rhs.to_i64().unwrap() as u32;
1070        if lhs.dtype() == DType::U64 {
1071            return scalar_op_typed(lhs, exp as u64, move |x: u64, _| x.wrapping_pow(exp));
1072        }
1073        int_scalar_op(lhs, exp as i64, move |x, _| x.wrapping_pow(exp))
1074    }
1075
1076    fn int_max_abs(tensor: IntTensor<Flex>) -> IntTensor<Flex> {
1077        let abs = Self::int_abs(tensor);
1078        crate::ops::reduce::max(abs)
1079    }
1080
1081    fn int_max_abs_dim(tensor: IntTensor<Flex>, dim: usize) -> IntTensor<Flex> {
1082        let abs = Self::int_abs(tensor);
1083        crate::ops::reduce::max_dim(abs, dim)
1084    }
1085
1086    fn int_arange(
1087        range: core::ops::Range<i64>,
1088        _device: &Device<Flex>,
1089        dtype: IntDType,
1090    ) -> IntTensor<Flex> {
1091        Self::int_arange_step(range, 1, &Default::default(), dtype)
1092    }
1093
1094    fn int_arange_step(
1095        range: core::ops::Range<i64>,
1096        step: usize,
1097        _device: &Device<Flex>,
1098        dtype: IntDType,
1099    ) -> IntTensor<Flex> {
1100        let dt: DType = dtype.into();
1101
1102        macro_rules! arange_typed {
1103            ($ty:ty) => {{
1104                let data: Vec<$ty> = range.step_by(step).map(|v| v as $ty).collect();
1105                let shape = Shape::from(alloc::vec![data.len()]);
1106                FlexTensor::new(Bytes::from_elems(data), Layout::contiguous(shape), dt)
1107            }};
1108        }
1109
1110        match dt {
1111            DType::I64 => arange_typed!(i64),
1112            DType::I32 => arange_typed!(i32),
1113            DType::I16 => arange_typed!(i16),
1114            DType::I8 => arange_typed!(i8),
1115            DType::U64 => arange_typed!(u64),
1116            DType::U32 => arange_typed!(u32),
1117            DType::U16 => arange_typed!(u16),
1118            DType::U8 => arange_typed!(u8),
1119            _ => unreachable!(),
1120        }
1121    }
1122}
1123
1124// Tests kept here exercise flex-specific behavior: dtype storage
1125// selection for every int width (I16/I32/U8/U16/U32/I64/U64), and edge
1126// cases of the dtype-specific kernels (u64 wrap, i64::MIN abs/neg, bit
1127// shift at width). Plain int arithmetic, scalar ops, bool->int cast
1128// smokes, and negative-stride (flipped/transposed) variants have been
1129// migrated to burn-backend-tests so they run against every backend.
1130// When adding new tests, keep them here only if they probe flex dtype
1131// storage; otherwise add them to
1132// crates/burn-backend-tests/tests/tensor/int/ops/.
1133#[cfg(test)]
1134mod tests {
1135    use alloc::vec;
1136    use burn_backend::TensorData;
1137    use burn_backend::ops::IntTensorOps;
1138
1139    use crate::Flex;
1140    use crate::FlexTensor;
1141
1142    #[test]
1143    fn test_u64_div_large_values() {
1144        let a = FlexTensor::from_data(TensorData::new(vec![u64::MAX], [1]));
1145        let b = FlexTensor::from_data(TensorData::new(vec![2u64], [1]));
1146        let result = Flex::int_div(a, b);
1147        let values: Vec<u64> = bytemuck::cast_slice(&result.into_data().bytes).to_vec();
1148        assert_eq!(values[0], u64::MAX / 2);
1149    }
1150
1151    #[test]
1152    fn test_u64_remainder_large_values() {
1153        let a = FlexTensor::from_data(TensorData::new(vec![u64::MAX], [1]));
1154        let b = FlexTensor::from_data(TensorData::new(vec![2u64], [1]));
1155        let result = Flex::int_remainder(a, b);
1156        let values: Vec<u64> = bytemuck::cast_slice(&result.into_data().bytes).to_vec();
1157        assert_eq!(values[0], u64::MAX % 2);
1158    }
1159
1160    #[test]
1161    fn test_int_abs_min_value() {
1162        // i64::MIN.abs() panics in debug; wrapping_abs returns MIN (matches PyTorch)
1163        let a = FlexTensor::from_data(TensorData::new(vec![i64::MIN], [1]));
1164        let result = Flex::int_abs(a);
1165        let values: Vec<i64> = bytemuck::cast_slice(&result.into_data().bytes).to_vec();
1166        assert_eq!(values[0], i64::MIN.wrapping_abs());
1167    }
1168
1169    #[test]
1170    fn test_int_neg_min_value() {
1171        // i64::MIN negation panics in debug; wrapping_neg returns MIN (matches PyTorch)
1172        let a = FlexTensor::from_data(TensorData::new(vec![i64::MIN], [1]));
1173        let result = Flex::int_neg(a);
1174        let values: Vec<i64> = bytemuck::cast_slice(&result.into_data().bytes).to_vec();
1175        assert_eq!(values[0], i64::MIN.wrapping_neg());
1176    }
1177
1178    #[test]
1179    fn test_int_shift_large_amount() {
1180        // Shift by >= bit width panics without wrapping; should not crash
1181        let a = FlexTensor::from_data(TensorData::new(vec![1i64], [1]));
1182        let b = FlexTensor::from_data(TensorData::new(vec![64i64], [1]));
1183        let _left = Flex::bitwise_left_shift(a.clone(), b.clone());
1184        let _right = Flex::bitwise_right_shift(a, b);
1185    }
1186
1187    #[test]
1188    fn test_int_into_float_f64() {
1189        use burn_backend::ops::IntTensorOps;
1190        use burn_std::FloatDType;
1191
1192        let t = FlexTensor::from_data(TensorData::new(vec![1i64, 2, -3], [3]));
1193        let result = Flex::int_into_float(t, FloatDType::F64);
1194        assert_eq!(result.dtype(), burn_backend::DType::F64);
1195        let data: Vec<f64> = result.into_data().try_into_vec().unwrap();
1196        assert_eq!(data, vec![1.0f64, 2.0, -3.0]);
1197    }
1198
1199    #[test]
1200    fn test_u64_add_scalar_large() {
1201        let t = FlexTensor::from_data(TensorData::new(vec![1u64, 2, 3], [3]));
1202        let big: u64 = (i64::MAX as u64) + 100;
1203        let result = Flex::int_add_scalar(t, burn_backend::Scalar::from(big));
1204        let data: Vec<u64> = result.into_data().try_into_vec().unwrap();
1205        assert_eq!(data, vec![big + 1, big + 2, big + 3]);
1206    }
1207
1208    #[test]
1209    fn test_u64_greater_elem_large() {
1210        let big: u64 = (i64::MAX as u64) + 100;
1211        let t = FlexTensor::from_data(TensorData::new(vec![big, big + 1, big - 1], [3]));
1212        let result = Flex::int_greater_elem(
1213            t,
1214            burn_backend::Scalar::from(big),
1215            burn_std::BoolStore::Native,
1216        );
1217        let data: Vec<bool> = result.into_data().try_into_vec().unwrap();
1218        assert_eq!(data, vec![false, true, false]);
1219    }
1220
1221    #[test]
1222    fn test_int_mask_fill_i32() {
1223        let t = FlexTensor::from_data(TensorData::new(vec![1i32, 2, 3, 4], [4]));
1224        let mask = FlexTensor::from_data(TensorData::new(vec![true, false, true, false], [4]));
1225        let result = Flex::int_mask_fill(t, mask, burn_backend::Scalar::from(0i64));
1226        let data: Vec<i32> = result.into_data().try_into_vec().unwrap();
1227        assert_eq!(data, vec![0, 2, 0, 4]);
1228    }
1229
1230    #[test]
1231    fn test_int_mask_fill_i16() {
1232        let t = FlexTensor::from_data(TensorData::new(vec![10i16, 20, 30, 40], [4]));
1233        let mask = FlexTensor::from_data(TensorData::new(vec![false, true, false, true], [4]));
1234        let result = Flex::int_mask_fill(t, mask, burn_backend::Scalar::from(-1i64));
1235        let data: Vec<i16> = result.into_data().try_into_vec().unwrap();
1236        assert_eq!(data, vec![10, -1, 30, -1]);
1237    }
1238
1239    #[test]
1240    fn test_int_mask_fill_u8() {
1241        let t = FlexTensor::from_data(TensorData::new(vec![1u8, 2, 3, 4], [4]));
1242        let mask = FlexTensor::from_data(TensorData::new(vec![true, true, false, false], [4]));
1243        let result = Flex::int_mask_fill(t, mask, burn_backend::Scalar::from(255i64));
1244        let data: Vec<u8> = result.into_data().try_into_vec().unwrap();
1245        assert_eq!(data, vec![255, 255, 3, 4]);
1246    }
1247
1248    #[test]
1249    fn test_int_mask_fill_u32() {
1250        let t = FlexTensor::from_data(TensorData::new(vec![100u32, 200, 300], [3]));
1251        let mask = FlexTensor::from_data(TensorData::new(vec![true, false, true], [3]));
1252        let result = Flex::int_mask_fill(t, mask, burn_backend::Scalar::from(0i64));
1253        let data: Vec<u32> = result.into_data().try_into_vec().unwrap();
1254        assert_eq!(data, vec![0, 200, 0]);
1255    }
1256
1257    #[test]
1258    fn test_int_mask_where_i32() {
1259        let t = FlexTensor::from_data(TensorData::new(vec![1i32, 2, 3, 4], [4]));
1260        let mask = FlexTensor::from_data(TensorData::new(vec![true, false, true, false], [4]));
1261        let v = FlexTensor::from_data(TensorData::new(vec![10i32, 20, 30, 40], [4]));
1262        let result = Flex::int_mask_where(t, mask, v);
1263        let data: Vec<i32> = result.into_data().try_into_vec().unwrap();
1264        assert_eq!(data, vec![10, 2, 30, 4]);
1265    }
1266
1267    #[test]
1268    fn test_int_mask_where_u8() {
1269        let t = FlexTensor::from_data(TensorData::new(vec![1u8, 2, 3, 4], [4]));
1270        let mask = FlexTensor::from_data(TensorData::new(vec![false, true, false, true], [4]));
1271        let v = FlexTensor::from_data(TensorData::new(vec![10u8, 20, 30, 40], [4]));
1272        let result = Flex::int_mask_where(t, mask, v);
1273        let data: Vec<u8> = result.into_data().try_into_vec().unwrap();
1274        assert_eq!(data, vec![1, 20, 3, 40]);
1275    }
1276
1277    #[test]
1278    fn test_int_gather_i32() {
1279        let t = FlexTensor::from_data(TensorData::new(vec![10i32, 20, 30, 40, 50, 60], [2, 3]));
1280        let indices = FlexTensor::from_data(TensorData::new(vec![2i64, 0, 1, 2], [2, 2]));
1281        let result = Flex::int_gather(1, t, indices);
1282        let data: Vec<i32> = result.into_data().try_into_vec().unwrap();
1283        assert_eq!(data, vec![30, 10, 50, 60]);
1284    }
1285
1286    #[test]
1287    fn test_int_select_u16() {
1288        let t = FlexTensor::from_data(TensorData::new(vec![10u16, 20, 30, 40, 50, 60], [2, 3]));
1289        let indices = FlexTensor::from_data(TensorData::new(vec![0i64, 1], [2]));
1290        let result = Flex::int_select(t, 1, indices);
1291        let data: Vec<u16> = result.into_data().try_into_vec().unwrap();
1292        assert_eq!(data, vec![10, 20, 40, 50]);
1293    }
1294
1295    #[test]
1296    fn test_int_cumsum_i32() {
1297        let t = FlexTensor::from_data(TensorData::new(vec![1i32, 2, 3, 4], [4]));
1298        let result = Flex::int_cumsum(t, 0);
1299        let data: Vec<i32> = result.into_data().try_into_vec().unwrap();
1300        assert_eq!(data, vec![1, 3, 6, 10]);
1301    }
1302
1303    #[test]
1304    fn test_int_cumprod_u8() {
1305        let t = FlexTensor::from_data(TensorData::new(vec![1u8, 2, 3, 4], [4]));
1306        let result = Flex::int_cumprod(t, 0);
1307        let data: Vec<u8> = result.into_data().try_into_vec().unwrap();
1308        assert_eq!(data, vec![1, 2, 6, 24]);
1309    }
1310
1311    #[test]
1312    fn test_int_cummin_i32() {
1313        let t = FlexTensor::from_data(TensorData::new(vec![3i32, 1, 4, 1, 5], [5]));
1314        let result = Flex::int_cummin(t, 0);
1315        let data: Vec<i32> = result.into_data().try_into_vec().unwrap();
1316        assert_eq!(data, vec![3, 1, 1, 1, 1]);
1317    }
1318
1319    #[test]
1320    fn test_int_cummax_u16() {
1321        let t = FlexTensor::from_data(TensorData::new(vec![3u16, 1, 4, 1, 5], [5]));
1322        let result = Flex::int_cummax(t, 0);
1323        let data: Vec<u16> = result.into_data().try_into_vec().unwrap();
1324        assert_eq!(data, vec![3, 3, 4, 4, 5]);
1325    }
1326
1327    #[test]
1328    fn test_int_scatter_add_i32() {
1329        let t = FlexTensor::from_data(TensorData::new(vec![0i32, 0, 0], [1, 3]));
1330        let indices = FlexTensor::from_data(TensorData::new(vec![0i64, 2, 1], [1, 3]));
1331        let values = FlexTensor::from_data(TensorData::new(vec![10i32, 20, 30], [1, 3]));
1332        let result = Flex::int_scatter_add(1, t, indices, values);
1333        let data: Vec<i32> = result.into_data().try_into_vec().unwrap();
1334        assert_eq!(data, vec![10, 30, 20]);
1335    }
1336
1337    #[test]
1338    fn test_int_select_add_u8() {
1339        let t = FlexTensor::from_data(TensorData::new(vec![1u8, 2, 3], [3]));
1340        let indices = FlexTensor::from_data(TensorData::new(vec![0i64, 2], [2]));
1341        let values = FlexTensor::from_data(TensorData::new(vec![10u8, 20], [2]));
1342        let result = Flex::int_select_add(t, 0, indices, values);
1343        let data: Vec<u8> = result.into_data().try_into_vec().unwrap();
1344        assert_eq!(data, vec![11, 2, 23]);
1345    }
1346
1347    #[test]
1348    fn test_int_random_i32() {
1349        use burn_backend::{DType, Distribution, ops::IntTensorOps};
1350        use burn_std::{IntDType, Shape};
1351
1352        let shape = Shape::from(vec![100]);
1353        let dist = Distribution::Uniform(0.0, 10.0);
1354        let device = crate::FlexDevice;
1355        let t = Flex::int_random(shape, dist, &device, IntDType::I32);
1356        assert_eq!(t.dtype(), DType::I32);
1357        let data: Vec<i32> = t.into_data().try_into_vec().unwrap();
1358        assert!(data.iter().all(|&v| (0..=10).contains(&v)));
1359    }
1360
1361    #[test]
1362    fn test_int_random_u8() {
1363        use burn_backend::{DType, Distribution, ops::IntTensorOps};
1364        use burn_std::{IntDType, Shape};
1365
1366        let shape = Shape::from(vec![50]);
1367        let dist = Distribution::Uniform(0.0, 100.0);
1368        let device = crate::FlexDevice;
1369        let t = Flex::int_random(shape, dist, &device, IntDType::U8);
1370        assert_eq!(t.dtype(), DType::U8);
1371    }
1372
1373    #[test]
1374    fn test_int_mean_i32() {
1375        use burn_backend::{DType, ops::IntTensorOps};
1376
1377        let t = FlexTensor::from_data(TensorData::new(vec![10i32, 20, 30], [3]));
1378        let result = Flex::int_mean(t);
1379        assert_eq!(result.dtype(), DType::I32);
1380        let data: Vec<i32> = result.into_data().try_into_vec().unwrap();
1381        assert_eq!(data, vec![20]); // (10 + 20 + 30) / 3 = 20
1382    }
1383}