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
use crate::{
Float, Int, IntDType, Shape, Tensor, TensorData, TensorPrimitive, backend::Backend,
cartesian_grid,
};
use core::ops::Range;
impl<B> Tensor<B, 1, Int>
where
B: Backend,
{
/// Returns a new integer tensor on the specified device.
///
/// # Arguments
///
/// * `range` - The range of values to generate.
/// * `device` - The device to create the tensor on.
pub fn arange(range: Range<i64>, device: &B::Device) -> Self {
Tensor::new(B::int_arange(range, device))
}
/// Returns a new integer tensor on the specified device.
///
/// # Arguments
///
/// * `range` - The range of values to generate.
/// * `step` - The step between each value.
pub fn arange_step(range: Range<i64>, step: usize, device: &B::Device) -> Self {
Tensor::new(B::int_arange_step(range, step, device))
}
}
impl<const D: usize, B> Tensor<B, D, Int>
where
B: Backend,
{
/// Create a tensor from integers (i32), placing it on a given device.
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::{Tensor, Int};
///
/// fn example<B: Backend>() {
/// let device = B::Device::default();
/// let _x: Tensor<B, 1, Int> = Tensor::from_ints([1, 2], &device);
/// let _y: Tensor<B, 2, Int> = Tensor::from_ints([[1, 2], [3, 4]], &device);
/// }
/// ```
pub fn from_ints<A: Into<TensorData>>(ints: A, device: &B::Device) -> Self {
Self::from_data(ints.into().convert::<i32>(), device)
}
/// Returns a new tensor with the same shape and device as the current tensor and the data
/// cast to Float.
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::{Int, Tensor};
///
/// fn example<B: Backend>() {
/// let device = Default::default();
/// let int_tensor = Tensor::<B, 1, Int>::arange(0..5, &device);
/// let float_tensor = int_tensor.float();
/// }
/// ```
pub fn float(self) -> Tensor<B, D, Float> {
Tensor::new(TensorPrimitive::Float(B::int_into_float(self.primitive)))
}
/// Generates a cartesian grid for the given tensor shape on the specified device.
/// The generated tensor is of dimension `D2 = D + 1`, where each element at dimension D contains the cartesian grid coordinates for that element.
///
/// # Arguments
///
/// * `shape` - The shape specifying the dimensions of the tensor.
/// * `device` - The device to create the tensor on.
///
/// # Panics
///
/// Panics if `D2` is not equal to `D+1`.
///
/// # Examples
///
/// ```rust
/// use burn_tensor::Int;
/// use burn_tensor::{backend::Backend, Shape, Tensor};
/// fn example<B: Backend>() {
/// let device = Default::default();
/// let result: Tensor<B, 3, _> = Tensor::<B, 2, Int>::cartesian_grid([2, 3], &device);
/// println!("{}", result);
/// }
/// ```
pub fn cartesian_grid<S: Into<Shape>, const D2: usize>(
shape: S,
device: &B::Device,
) -> Tensor<B, D2, Int> {
cartesian_grid::<B, S, D, D2>(shape, device)
}
/// Applies the bitwise logical and operation with each bit representing the integer.
pub fn bitwise_and(self, other: Self) -> Self {
Self::new(B::bitwise_and(self.primitive, other.primitive))
}
/// Applies the bitwise logical or operation with another tensor.
pub fn bitwise_or(self, other: Self) -> Self {
Self::new(B::bitwise_or(self.primitive, other.primitive))
}
/// Applies the bitwise logical xor operation with another tensor.
pub fn bitwise_xor(self, other: Self) -> Self {
Self::new(B::bitwise_xor(self.primitive, other.primitive))
}
/// Applies the bitwise logical not operation.
pub fn bitwise_not(self) -> Self {
Self::new(B::bitwise_not(self.primitive))
}
/// Applies the bitwise logical and operation with each bit in the scalar and the integers in the tensor.
pub fn bitwise_and_scalar(self, other: B::IntElem) -> Self {
Self::new(B::bitwise_and_scalar(self.primitive, other))
}
/// Applies the bitwise logical or operation with each bit in the scalar and the integers in the tensor.
pub fn bitwise_or_scalar(self, other: B::IntElem) -> Self {
Self::new(B::bitwise_or_scalar(self.primitive, other))
}
/// Applies bitwise logical xor operation with each bit in the scalar and the integers in the tensor.
pub fn bitwise_xor_scalar(self, other: B::IntElem) -> Self {
Self::new(B::bitwise_xor_scalar(self.primitive, other))
}
/// Applies the bitwise left shift operation with the integers in the tensor.
pub fn bitwise_left_shift(self, other: Self) -> Self {
Self::new(B::bitwise_left_shift(self.primitive, other.primitive))
}
/// Applies the bitwise right shift operation with the integers in the tensor.
pub fn bitwise_right_shift(self, other: Self) -> Self {
Self::new(B::bitwise_right_shift(self.primitive, other.primitive))
}
/// Applies the bitwise left shift operation with the scalar.
pub fn bitwise_left_shift_scalar(self, other: B::IntElem) -> Self {
Self::new(B::bitwise_left_shift_scalar(self.primitive, other))
}
/// Applies the bitwise right shift operation with the scalar.
pub fn bitwise_right_shift_scalar(self, other: B::IntElem) -> Self {
Self::new(B::bitwise_right_shift_scalar(self.primitive, other))
}
/// Converts a tensor to the specified integer data type.
///
/// This is always a no-op when casting to the current dtype.
///
/// # Warning
/// Most backends don't have automatic type promotion at this time, so make sure that all tensors
/// have the same integer data type for operations multiple input tensors (e.g., binary ops).
pub fn cast<F: Into<IntDType>>(self, dtype: F) -> Tensor<B, D, Int> {
let dtype = dtype.into();
let self_dtype: IntDType = self.dtype().into();
if dtype == self_dtype {
// no-op.
return self;
}
Tensor::new(B::int_cast(self.primitive, dtype))
}
}