Skip to main content

burn_ndarray/ops/
int_tensor.rs

1// Language
2use crate::rand::get_seeded_rng;
3use alloc::vec::Vec;
4use burn_backend::backend::ExecutionError;
5use burn_backend::ops::IntTensorOps;
6use burn_backend::tensor::{FloatTensor, IntTensor};
7use burn_backend::{Distribution, IntDType, Scalar, TensorMetadata};
8
9use burn_backend::ElementConversion;
10use burn_std::{BoolDType, FloatDType};
11
12// Current crate
13use crate::SharedArray;
14use crate::execute_with_int_dtype;
15use crate::ops::matmul::matmul;
16use crate::{ExpElement, NdArrayDevice, SEED, execute_with_int_out_dtype, slice};
17use crate::{NdArray, cast_to_dtype, execute_with_dtype, tensor::NdArrayTensor};
18use crate::{cat_with_dtype, execute_with_float_out_dtype};
19
20// Workspace crates
21use super::{NdArrayBitOps, NdArrayMathOps, NdArrayOps};
22use burn_backend::{DType, Shape, TensorData};
23
24impl IntTensorOps<Self> for NdArray {
25    fn int_from_data(data: TensorData, _device: &NdArrayDevice) -> NdArrayTensor {
26        if data.dtype.is_int() || data.dtype.is_uint() {
27            NdArrayTensor::from_data(data)
28        } else {
29            unimplemented!("Unsupported dtype for `int_from_data`: {:?}", data.dtype)
30        }
31    }
32
33    async fn int_into_data(tensor: NdArrayTensor) -> Result<TensorData, ExecutionError> {
34        Ok(tensor.into_data())
35    }
36
37    fn int_to_device(tensor: NdArrayTensor, _device: &NdArrayDevice) -> NdArrayTensor {
38        tensor
39    }
40
41    fn int_reshape(tensor: NdArrayTensor, shape: Shape) -> NdArrayTensor {
42        execute_with_int_dtype!(tensor, |array| NdArrayOps::reshape(array, shape))
43    }
44
45    fn int_slice(tensor: NdArrayTensor, slices: &[burn_backend::Slice]) -> NdArrayTensor {
46        slice!(tensor, slices)
47    }
48
49    fn int_empty(shape: Shape, device: &NdArrayDevice, dtype: IntDType) -> NdArrayTensor {
50        Self::int_zeros(shape, device, dtype)
51    }
52
53    fn int_matmul(lhs: IntTensor<Self>, rhs: IntTensor<Self>) -> IntTensor<Self> {
54        execute_with_int_dtype!((lhs, rhs), matmul)
55    }
56
57    fn int_mask_where(
58        tensor: NdArrayTensor,
59        mask: NdArrayTensor,
60        source: NdArrayTensor,
61    ) -> NdArrayTensor {
62        execute_with_int_dtype!((tensor, source), |tensor, source| {
63            NdArrayOps::mask_where(tensor, mask.bool(), source)
64        })
65    }
66
67    fn int_mask_fill(tensor: NdArrayTensor, mask: NdArrayTensor, value: Scalar) -> NdArrayTensor {
68        execute_with_int_dtype!(tensor, |array| NdArrayOps::mask_fill(
69            array,
70            mask.bool(),
71            value.elem()
72        ))
73    }
74
75    fn int_slice_assign(
76        tensor: NdArrayTensor,
77        slices: &[burn_backend::Slice],
78        value: NdArrayTensor,
79    ) -> NdArrayTensor {
80        execute_with_int_dtype!((tensor, value), |tensor, value| NdArrayOps::slice_assign(
81            tensor, slices, value
82        ))
83    }
84
85    fn int_cat(tensors: Vec<NdArrayTensor>, dim: usize) -> NdArrayTensor {
86        cat_with_dtype!(tensors, dim, [I64, I32, I16, I8, U64, U32, U16, U8])
87    }
88
89    fn int_equal(lhs: NdArrayTensor, rhs: NdArrayTensor, _out_dtype: BoolDType) -> NdArrayTensor {
90        execute_with_int_dtype!((lhs, rhs), NdArrayMathOps::equal)
91    }
92
93    fn int_equal_elem(lhs: NdArrayTensor, rhs: Scalar, _out_dtype: BoolDType) -> NdArrayTensor {
94        execute_with_int_dtype!(lhs, |array| NdArrayMathOps::equal_elem(array, rhs.elem()))
95    }
96
97    fn int_greater(lhs: NdArrayTensor, rhs: NdArrayTensor, _out_dtype: BoolDType) -> NdArrayTensor {
98        execute_with_int_dtype!((lhs, rhs), NdArrayMathOps::greater)
99    }
100
101    fn int_greater_elem(lhs: NdArrayTensor, rhs: Scalar, _out_dtype: BoolDType) -> NdArrayTensor {
102        execute_with_int_dtype!(lhs, |array| NdArrayMathOps::greater_elem(array, rhs.elem()))
103    }
104
105    fn int_greater_equal(
106        lhs: NdArrayTensor,
107        rhs: NdArrayTensor,
108        _out_dtype: BoolDType,
109    ) -> NdArrayTensor {
110        execute_with_int_dtype!((lhs, rhs), NdArrayMathOps::greater_equal)
111    }
112
113    fn int_greater_equal_elem(
114        lhs: NdArrayTensor,
115        rhs: Scalar,
116        _out_dtype: BoolDType,
117    ) -> NdArrayTensor {
118        execute_with_int_dtype!(lhs, |array| NdArrayMathOps::greater_equal_elem(
119            array,
120            rhs.elem()
121        ))
122    }
123
124    fn int_lower(lhs: NdArrayTensor, rhs: NdArrayTensor, _out_dtype: BoolDType) -> NdArrayTensor {
125        execute_with_int_dtype!((lhs, rhs), NdArrayMathOps::lower)
126    }
127
128    fn int_lower_elem(lhs: NdArrayTensor, rhs: Scalar, _out_dtype: BoolDType) -> NdArrayTensor {
129        execute_with_int_dtype!(lhs, |array| NdArrayMathOps::lower_elem(array, rhs.elem()))
130    }
131
132    fn int_lower_equal(
133        lhs: NdArrayTensor,
134        rhs: NdArrayTensor,
135        _out_dtype: BoolDType,
136    ) -> NdArrayTensor {
137        execute_with_int_dtype!((lhs, rhs), NdArrayMathOps::lower_equal)
138    }
139
140    fn int_lower_equal_elem(
141        lhs: NdArrayTensor,
142        rhs: Scalar,
143        _out_dtype: BoolDType,
144    ) -> NdArrayTensor {
145        execute_with_int_dtype!(lhs, |array| NdArrayMathOps::lower_equal_elem(
146            array,
147            rhs.elem()
148        ))
149    }
150
151    fn int_add(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
152        execute_with_int_dtype!((lhs, rhs), NdArrayMathOps::add)
153    }
154
155    fn int_add_scalar(lhs: NdArrayTensor, rhs: Scalar) -> NdArrayTensor {
156        execute_with_int_dtype!(lhs, |array| NdArrayMathOps::add_scalar(array, rhs.elem()))
157    }
158
159    fn int_sub(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
160        execute_with_int_dtype!((lhs, rhs), NdArrayMathOps::sub)
161    }
162
163    fn int_sub_scalar(lhs: NdArrayTensor, rhs: Scalar) -> NdArrayTensor {
164        execute_with_int_dtype!(lhs, |array| NdArrayMathOps::sub_scalar(array, rhs.elem()))
165    }
166
167    fn int_mul(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
168        execute_with_int_dtype!((lhs, rhs), NdArrayMathOps::mul)
169    }
170
171    fn int_mul_scalar(lhs: NdArrayTensor, rhs: Scalar) -> NdArrayTensor {
172        execute_with_int_dtype!(lhs, |array| NdArrayMathOps::mul_scalar(array, rhs.elem()))
173    }
174
175    fn int_div(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
176        execute_with_int_dtype!((lhs, rhs), NdArrayMathOps::div)
177    }
178
179    fn int_div_scalar(lhs: NdArrayTensor, rhs: Scalar) -> NdArrayTensor {
180        execute_with_int_dtype!(lhs, |array| NdArrayMathOps::div_scalar(array, rhs.elem()))
181    }
182
183    fn int_remainder(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
184        execute_with_int_dtype!((lhs, rhs), NdArrayMathOps::remainder)
185    }
186
187    fn int_remainder_scalar(lhs: NdArrayTensor, rhs: Scalar) -> NdArrayTensor {
188        execute_with_int_dtype!(lhs, |array| NdArrayMathOps::remainder_scalar(
189            array,
190            rhs.elem()
191        ))
192    }
193
194    fn int_sum(tensor: NdArrayTensor) -> NdArrayTensor {
195        // Use view() for zero-copy on borrowed storage
196        execute_with_int_dtype!(tensor, E, |array: SharedArray<E>| NdArrayMathOps::sum_view(
197            array.view()
198        ))
199    }
200
201    fn int_sum_dim(tensor: NdArrayTensor, dim: usize) -> NdArrayTensor {
202        execute_with_int_dtype!(tensor, |array| NdArrayMathOps::sum_dim(array, dim))
203    }
204
205    fn int_prod(tensor: NdArrayTensor) -> NdArrayTensor {
206        // Use view() for zero-copy on borrowed storage
207        execute_with_int_dtype!(
208            tensor,
209            E,
210            |array: SharedArray<E>| NdArrayMathOps::prod_view(array.view())
211        )
212    }
213
214    fn int_prod_dim(tensor: NdArrayTensor, dim: usize) -> NdArrayTensor {
215        execute_with_int_dtype!(tensor, |array| NdArrayMathOps::prod_dim(array, dim))
216    }
217
218    fn int_mean(tensor: NdArrayTensor) -> NdArrayTensor {
219        // Use view() for zero-copy on borrowed storage
220        execute_with_int_dtype!(
221            tensor,
222            E,
223            |array: SharedArray<E>| NdArrayMathOps::mean_view(array.view())
224        )
225    }
226
227    fn int_mean_dim(tensor: NdArrayTensor, dim: usize) -> NdArrayTensor {
228        execute_with_int_dtype!(tensor, |array| NdArrayMathOps::mean_dim(array, dim))
229    }
230
231    fn int_max(tensor: NdArrayTensor) -> NdArrayTensor {
232        // Use view() for zero-copy on borrowed storage
233        execute_with_int_dtype!(tensor, E, |array: SharedArray<E>| NdArrayMathOps::max_view(
234            array.view()
235        ))
236    }
237
238    fn int_min(tensor: NdArrayTensor) -> NdArrayTensor {
239        // Use view() for zero-copy on borrowed storage
240        execute_with_int_dtype!(tensor, E, |array: SharedArray<E>| NdArrayMathOps::min_view(
241            array.view()
242        ))
243    }
244
245    fn int_cumsum(tensor: NdArrayTensor, dim: usize) -> NdArrayTensor {
246        execute_with_int_dtype!(tensor, |array| NdArrayMathOps::cumsum(array, dim))
247    }
248
249    fn int_cumprod(tensor: NdArrayTensor, dim: usize) -> NdArrayTensor {
250        execute_with_int_dtype!(tensor, |array| NdArrayMathOps::cumprod(array, dim))
251    }
252
253    fn int_cummin(tensor: NdArrayTensor, dim: usize) -> NdArrayTensor {
254        execute_with_int_dtype!(tensor, |array| NdArrayMathOps::cummin(array, dim))
255    }
256
257    fn int_cummax(tensor: NdArrayTensor, dim: usize) -> NdArrayTensor {
258        execute_with_int_dtype!(tensor, |array| NdArrayMathOps::cummax(array, dim))
259    }
260
261    fn int_gather(dim: usize, tensor: NdArrayTensor, indices: NdArrayTensor) -> NdArrayTensor {
262        execute_with_int_dtype!(tensor, E, |array| -> NdArrayTensor {
263            execute_with_int_dtype!(indices, |idx_array| NdArrayOps::gather(
264                dim, array, idx_array
265            ))
266        })
267    }
268
269    fn int_scatter(
270        dim: usize,
271        tensor: NdArrayTensor,
272        indices: NdArrayTensor,
273        value: NdArrayTensor,
274        update: burn_backend::tensor::IndexingUpdateOp,
275    ) -> NdArrayTensor {
276        match update {
277            burn_backend::tensor::IndexingUpdateOp::Add => {
278                execute_with_int_dtype!((tensor, value), I, |tensor, value| -> NdArrayTensor {
279                    execute_with_int_dtype!(indices, |idx_array| NdArrayOps::<I>::scatter(
280                        dim, tensor, idx_array, value
281                    ))
282                })
283            }
284            burn_backend::tensor::IndexingUpdateOp::Assign => {
285                execute_with_int_dtype!((tensor, value), I, |tensor, value| -> NdArrayTensor {
286                    execute_with_int_dtype!(indices, |idx_array| NdArrayOps::<I>::scatter_assign(
287                        dim, tensor, idx_array, value
288                    ))
289                })
290            }
291            burn_backend::tensor::IndexingUpdateOp::Mul => {
292                execute_with_int_dtype!((tensor, value), I, |tensor, value| -> NdArrayTensor {
293                    execute_with_int_dtype!(indices, |idx_array| NdArrayOps::<I>::scatter_mul(
294                        dim, tensor, idx_array, value
295                    ))
296                })
297            }
298            burn_backend::tensor::IndexingUpdateOp::Min => {
299                execute_with_int_dtype!((tensor, value), I, |tensor, value| -> NdArrayTensor {
300                    execute_with_int_dtype!(indices, |idx_array| NdArrayOps::<I>::scatter_min(
301                        dim, tensor, idx_array, value
302                    ))
303                })
304            }
305            burn_backend::tensor::IndexingUpdateOp::Max => {
306                execute_with_int_dtype!((tensor, value), I, |tensor, value| -> NdArrayTensor {
307                    execute_with_int_dtype!(indices, |idx_array| NdArrayOps::<I>::scatter_max(
308                        dim, tensor, idx_array, value
309                    ))
310                })
311            }
312        }
313    }
314
315    fn int_scatter_nd(
316        data: NdArrayTensor,
317        indices: NdArrayTensor,
318        values: NdArrayTensor,
319        reduction: burn_backend::tensor::IndexingUpdateOp,
320    ) -> NdArrayTensor {
321        execute_with_int_dtype!((data, values), I, |data, values| -> NdArrayTensor {
322            execute_with_int_dtype!(indices, |idx_array| NdArrayOps::<I>::scatter_nd(
323                data, idx_array, values, reduction
324            ))
325        })
326    }
327
328    fn int_gather_nd(data: NdArrayTensor, indices: NdArrayTensor) -> NdArrayTensor {
329        execute_with_int_dtype!(data, E, |array| -> NdArrayTensor {
330            execute_with_int_dtype!(indices, |idx_array| NdArrayOps::gather_nd(array, idx_array))
331        })
332    }
333
334    fn int_select(tensor: NdArrayTensor, dim: usize, indices: NdArrayTensor) -> NdArrayTensor {
335        execute_with_int_dtype!(tensor, E, |array| -> NdArrayTensor {
336            execute_with_int_dtype!(indices, |idx_array| NdArrayMathOps::select(
337                array, dim, idx_array
338            ))
339        })
340    }
341
342    fn int_select_assign(
343        tensor: NdArrayTensor,
344        dim: usize,
345        indices: NdArrayTensor,
346        value: NdArrayTensor,
347        update: burn_backend::tensor::IndexingUpdateOp,
348    ) -> NdArrayTensor {
349        match update {
350            burn_backend::tensor::IndexingUpdateOp::Add => {
351                execute_with_int_dtype!((tensor, value), I, |tensor, value| -> NdArrayTensor {
352                    execute_with_int_dtype!(indices, |idx_array| {
353                        NdArrayMathOps::<I>::select_assign(tensor, dim, idx_array, value)
354                    })
355                })
356            }
357            burn_backend::tensor::IndexingUpdateOp::Assign => {
358                execute_with_int_dtype!((tensor, value), I, |tensor, value| -> NdArrayTensor {
359                    execute_with_int_dtype!(indices, |idx_array| {
360                        NdArrayMathOps::<I>::select_assign_replace(tensor, dim, idx_array, value)
361                    })
362                })
363            }
364            burn_backend::tensor::IndexingUpdateOp::Mul => {
365                execute_with_int_dtype!((tensor, value), I, |tensor, value| -> NdArrayTensor {
366                    execute_with_int_dtype!(indices, |idx_array| {
367                        NdArrayMathOps::<I>::select_assign_mul(tensor, dim, idx_array, value)
368                    })
369                })
370            }
371            burn_backend::tensor::IndexingUpdateOp::Min => {
372                execute_with_int_dtype!((tensor, value), I, |tensor, value| -> NdArrayTensor {
373                    execute_with_int_dtype!(indices, |idx_array| {
374                        NdArrayMathOps::<I>::select_assign_min(tensor, dim, idx_array, value)
375                    })
376                })
377            }
378            burn_backend::tensor::IndexingUpdateOp::Max => {
379                execute_with_int_dtype!((tensor, value), I, |tensor, value| -> NdArrayTensor {
380                    execute_with_int_dtype!(indices, |idx_array| {
381                        NdArrayMathOps::<I>::select_assign_max(tensor, dim, idx_array, value)
382                    })
383                })
384            }
385        }
386    }
387    fn int_argmax(tensor: NdArrayTensor, dim: usize) -> NdArrayTensor {
388        // Use view() for zero-copy on borrowed storage
389        execute_with_int_dtype!(tensor, E, |array: SharedArray<E>| {
390            NdArrayMathOps::argmax_view::<E>(array.view(), dim)
391        })
392    }
393
394    fn int_argmin(tensor: NdArrayTensor, dim: usize) -> NdArrayTensor {
395        // Use view() for zero-copy on borrowed storage
396        execute_with_int_dtype!(tensor, E, |array: SharedArray<E>| {
397            NdArrayMathOps::argmin_view::<E>(array.view(), dim)
398        })
399    }
400
401    fn int_clamp_min(tensor: NdArrayTensor, min: Scalar) -> NdArrayTensor {
402        execute_with_int_dtype!(tensor, |array| NdArrayMathOps::clamp_min(array, min.elem()))
403    }
404
405    fn int_clamp_max(tensor: NdArrayTensor, max: Scalar) -> NdArrayTensor {
406        execute_with_int_dtype!(tensor, |array| NdArrayMathOps::clamp_max(array, max.elem()))
407    }
408
409    fn int_clamp(tensor: NdArrayTensor, min: Scalar, max: Scalar) -> NdArrayTensor {
410        execute_with_int_dtype!(tensor, |array| NdArrayMathOps::clamp(
411            array,
412            min.elem(),
413            max.elem()
414        ))
415    }
416
417    fn int_abs(tensor: NdArrayTensor) -> NdArrayTensor {
418        match tensor.dtype() {
419            DType::I64 | DType::I32 | DType::I16 | DType::I8 => {
420                execute_with_dtype!(tensor, I, NdArrayMathOps::abs, [
421                    I64 => i64, I32 => i32, I16 => i16, I8 => i8
422                ])
423            }
424            // Already unsigned
425            DType::U64 | DType::U32 | DType::U16 | DType::U8 => tensor,
426            other => panic!("Unsupported dtype: {other:?}"),
427        }
428    }
429
430    fn int_into_float(tensor: NdArrayTensor, out_dtype: FloatDType) -> FloatTensor<Self> {
431        execute_with_float_out_dtype!(out_dtype, F, {
432            execute_with_int_dtype!(tensor, IntElem, |array: SharedArray<IntElem>| {
433                array.mapv(|a: IntElem| a.elem::<F>()).into_shared()
434            })
435        })
436    }
437
438    fn int_swap_dims(tensor: NdArrayTensor, dim1: usize, dim2: usize) -> NdArrayTensor {
439        execute_with_int_dtype!(tensor, |array| NdArrayOps::swap_dims(array, dim1, dim2))
440    }
441
442    fn int_random(
443        shape: Shape,
444        distribution: Distribution,
445        device: &NdArrayDevice,
446        dtype: IntDType,
447    ) -> NdArrayTensor {
448        let mut seed = SEED.lock();
449        let mut rng = seed.take().unwrap_or_else(get_seeded_rng);
450
451        let effective_distribution = if distribution == Distribution::Default {
452            Distribution::Uniform(0.0, 255.0) // Assuming UniformInt is the integer variant
453        } else {
454            distribution
455        };
456
457        let tensor = execute_with_int_out_dtype!(
458            dtype,
459            I,
460            Self::int_from_data(
461                TensorData::random::<I, _, _>(shape, effective_distribution, &mut rng),
462                device,
463            )
464        );
465        *seed = Some(rng);
466        tensor
467    }
468
469    fn int_powi(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
470        execute_with_int_dtype!((lhs, rhs), I, |lhs, rhs| NdArrayMathOps::elementwise_op(
471            lhs,
472            rhs,
473            |a: &I, b: &I| { (a.elem::<i64>().pow(b.elem::<u32>())).elem() }
474        ))
475    }
476
477    fn int_permute(tensor: NdArrayTensor, axes: &[usize]) -> NdArrayTensor {
478        execute_with_int_dtype!(tensor, |array| NdArrayOps::permute(array, axes))
479    }
480
481    fn int_flip(tensor: NdArrayTensor, axes: &[usize]) -> NdArrayTensor {
482        execute_with_int_dtype!(tensor, |array| NdArrayOps::flip(array, axes))
483    }
484
485    fn int_sign(tensor: NdArrayTensor) -> NdArrayTensor {
486        match tensor.dtype() {
487            DType::I64 | DType::I32 | DType::I16 | DType::I8 => {
488                execute_with_dtype!(tensor, I, NdArrayMathOps::sign_op, [
489                    I64 => i64, I32 => i32, I16 => i16, I8 => i8
490                ])
491            }
492            DType::U64 | DType::U32 | DType::U16 | DType::U8 => {
493                Self::int_greater_elem(tensor, 0.into(), BoolDType::Native)
494            }
495            other => panic!("Unsupported dtype: {other:?}"),
496        }
497    }
498
499    fn int_expand(tensor: NdArrayTensor, shape: Shape) -> NdArrayTensor {
500        execute_with_int_dtype!(tensor, |array| NdArrayOps::expand(array, shape))
501    }
502
503    fn bitwise_and(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
504        execute_with_int_dtype!((lhs, rhs), NdArrayBitOps::bitand)
505    }
506
507    fn bitwise_and_scalar(lhs: NdArrayTensor, rhs: Scalar) -> NdArrayTensor {
508        execute_with_int_dtype!(lhs, |array| NdArrayBitOps::bitand_scalar(array, rhs.elem()))
509    }
510
511    fn bitwise_or(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
512        execute_with_int_dtype!((lhs, rhs), NdArrayBitOps::bitor)
513    }
514
515    fn bitwise_or_scalar(lhs: NdArrayTensor, rhs: Scalar) -> NdArrayTensor {
516        execute_with_int_dtype!(lhs, |array| NdArrayBitOps::bitor_scalar(array, rhs.elem()))
517    }
518
519    fn bitwise_xor(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
520        execute_with_int_dtype!((lhs, rhs), NdArrayBitOps::bitxor)
521    }
522
523    fn bitwise_xor_scalar(lhs: NdArrayTensor, rhs: Scalar) -> NdArrayTensor {
524        execute_with_int_dtype!(lhs, |array| NdArrayBitOps::bitxor_scalar(array, rhs.elem()))
525    }
526
527    fn bitwise_not(tensor: NdArrayTensor) -> NdArrayTensor {
528        execute_with_int_dtype!(tensor, NdArrayBitOps::bitnot)
529    }
530
531    fn bitwise_left_shift(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
532        execute_with_int_dtype!((lhs, rhs), I, |lhs, rhs| {
533            NdArrayMathOps::elementwise_op(lhs, rhs, |a: &I, b: &I| {
534                (a.elem::<i64>() << (b.elem::<u32>())).elem()
535            })
536        })
537    }
538
539    fn bitwise_left_shift_scalar(lhs: NdArrayTensor, rhs: Scalar) -> NdArrayTensor {
540        execute_with_int_dtype!(lhs, I, |array| {
541            NdArrayMathOps::elementwise_op_scalar(array, |a: I| {
542                (a.elem::<i64>() << rhs.elem::<u32>()).elem()
543            })
544        })
545    }
546
547    fn bitwise_right_shift(lhs: NdArrayTensor, rhs: NdArrayTensor) -> NdArrayTensor {
548        execute_with_int_dtype!((lhs, rhs), I, |lhs, rhs| {
549            NdArrayMathOps::elementwise_op(lhs, rhs, |a: &I, b: &I| {
550                (a.elem::<i64>() >> (b.elem::<u32>())).elem()
551            })
552        })
553    }
554
555    fn bitwise_right_shift_scalar(lhs: NdArrayTensor, rhs: Scalar) -> NdArrayTensor {
556        execute_with_int_dtype!(lhs, I, |array| {
557            NdArrayMathOps::elementwise_op_scalar(array, |a: I| {
558                (a.elem::<i64>() >> rhs.elem::<u32>()).elem()
559            })
560        })
561    }
562
563    fn int_cast(tensor: IntTensor<Self>, dtype: IntDType) -> IntTensor<Self> {
564        execute_with_int_dtype!(tensor, |array| cast_to_dtype(array, dtype.into()))
565    }
566
567    fn int_unfold(
568        tensor: IntTensor<Self>,
569        dim: usize,
570        size: usize,
571        step: usize,
572    ) -> IntTensor<Self> {
573        execute_with_int_dtype!(tensor, |array| NdArrayOps::unfold(array, dim, size, step))
574    }
575
576    fn int_powi_scalar_impl(lhs: IntTensor<Self>, rhs: Scalar) -> IntTensor<Self> {
577        execute_with_int_dtype!(lhs, I, |array| {
578            NdArrayMathOps::elementwise_op_scalar(array, |a: I| a.powi_elem(rhs.elem()))
579        })
580    }
581}