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
use alloc::vec::Vec;
use burn_std::distributed::ReduceOperation;
use crate::{
AsIndex, Device, Tensor, check,
check::{TensorCheck, unwrap_dim_index},
kind::{Basic, Numeric},
ops::BridgeTensor,
};
impl<const D: usize, K: Numeric> Tensor<D, K> {
/// Reduces every member's tensor with `op`, and gives every member the result.
///
/// Member `i` is position `i` of `tensors`, each on its own member's device and every one of
/// one shape and dtype. The outputs are in member order, each on its member's device. A device
/// may appear twice, which runs the collective over transfers rather than natively.
///
/// A [`ReduceOperation::Mean`] over integers divides by the member count, truncating toward
/// zero.
///
/// # Panics
///
/// - If `tensors` is empty.
/// - If the members don't all have the same shape and dtype.
/// - If the members are quantized.
///
/// # Example
///
/// ```rust
/// use burn_tensor::{Tensor, distributed::ReduceOperation};
///
/// let device = Default::default();
/// let members = vec![
/// Tensor::<1>::from_data([1.0, 2.0], &device),
/// Tensor::<1>::from_data([3.0, 4.0], &device),
/// ];
///
/// let reduced = Tensor::all_reduce(members, ReduceOperation::Sum);
///
/// assert_eq!(reduced[0].clone().try_into_vec_as::<f32>().unwrap(), vec![4.0, 6.0]);
/// assert_eq!(reduced[1].clone().try_into_vec_as::<f32>().unwrap(), vec![4.0, 6.0]);
/// ```
pub fn all_reduce(tensors: Vec<Self>, op: ReduceOperation) -> Vec<Self> {
check!(TensorCheck::collective("All Reduce", &tensors));
from_primitives(K::all_reduce(into_primitives(tensors), op))
}
/// Reduces every member's tensor with `op`, and gives member `i` chunk `i` along `dim` of the
/// result.
///
/// Member `i` is position `i` of `tensors`, each on its own member's device and every one of
/// one shape and dtype. Chunks are as even as possible, with the first members holding one
/// more. The dimension supports negative indexing.
///
/// A [`ReduceOperation::Mean`] over integers divides by the member count, truncating toward
/// zero.
///
/// # Panics
///
/// - If `tensors` is empty.
/// - If the members don't all have the same shape and dtype.
/// - If the members are quantized.
/// - If `dim` is outside the rank, or holds fewer elements than there are members.
///
/// # Example
///
/// ```rust
/// use burn_tensor::{Tensor, distributed::ReduceOperation};
///
/// let device = Default::default();
/// let members = vec![
/// Tensor::<1>::from_data([1.0, 2.0], &device),
/// Tensor::<1>::from_data([3.0, 4.0], &device),
/// ];
///
/// let scattered = Tensor::reduce_scatter(members, 0, ReduceOperation::Sum);
///
/// assert_eq!(scattered[0].clone().try_into_vec_as::<f32>().unwrap(), vec![4.0]);
/// assert_eq!(scattered[1].clone().try_into_vec_as::<f32>().unwrap(), vec![6.0]);
/// ```
pub fn reduce_scatter(tensors: Vec<Self>, dim: impl AsIndex, op: ReduceOperation) -> Vec<Self> {
let dim = unwrap_dim_index(dim.try_dim_index(D), "Reduce Scatter");
check!(TensorCheck::collective("Reduce Scatter", &tensors));
check!(TensorCheck::collective_chunks(
"Reduce Scatter",
&tensors,
dim
));
from_primitives(K::reduce_scatter(into_primitives(tensors), dim, op))
}
}
impl<const D: usize, K: Basic> Tensor<D, K> {
/// Gives every member every member's tensor, concatenated along `dim` in member order.
///
/// Member `i` is position `i` of `tensors`, each on its own member's device and every one of
/// one shape and dtype. The outputs are in member order, each on its member's device. The
/// dimension supports negative indexing.
///
/// # Panics
///
/// - If `tensors` is empty.
/// - If the members don't all have the same shape and dtype.
/// - If the members are quantized.
/// - If `dim` is outside the rank.
///
/// # Example
///
/// ```rust
/// use burn_tensor::Tensor;
///
/// let device = Default::default();
/// let members = vec![
/// Tensor::<1>::from_data([1.0, 2.0], &device),
/// Tensor::<1>::from_data([3.0, 4.0], &device),
/// ];
///
/// let gathered = Tensor::all_gather(members, 0);
///
/// assert_eq!(gathered[0].clone().try_into_vec_as::<f32>().unwrap(), vec![1.0, 2.0, 3.0, 4.0]);
/// assert_eq!(gathered[1].clone().try_into_vec_as::<f32>().unwrap(), vec![1.0, 2.0, 3.0, 4.0]);
/// ```
pub fn all_gather(tensors: Vec<Self>, dim: impl AsIndex) -> Vec<Self> {
let dim = unwrap_dim_index(dim.try_dim_index(D), "All Gather");
check!(TensorCheck::collective("All Gather", &tensors));
from_primitives(K::all_gather(into_primitives(tensors), dim))
}
/// Gives member `i` chunk `i` along `split_dim` of every member's tensor, concatenated along
/// `concat_dim` in member order.
///
/// Member `i` is position `i` of `tensors`, each on its own member's device and every one of
/// one shape and dtype. Chunks are as even as possible, with the first members holding one
/// more. Both dimensions support negative indexing.
///
/// # Panics
///
/// - If `tensors` is empty.
/// - If the members don't all have the same shape and dtype.
/// - If the members are quantized.
/// - If either dimension is outside the rank, or `split_dim` holds fewer elements than there
/// are members.
///
/// # Example
///
/// ```rust
/// use burn_tensor::Tensor;
///
/// let device = Default::default();
/// let members = vec![
/// Tensor::<1>::from_data([1.0, 2.0], &device),
/// Tensor::<1>::from_data([3.0, 4.0], &device),
/// ];
///
/// let exchanged = Tensor::all_to_all(members, 0, 0);
///
/// assert_eq!(exchanged[0].clone().try_into_vec_as::<f32>().unwrap(), vec![1.0, 3.0]);
/// assert_eq!(exchanged[1].clone().try_into_vec_as::<f32>().unwrap(), vec![2.0, 4.0]);
/// ```
pub fn all_to_all(
tensors: Vec<Self>,
split_dim: impl AsIndex,
concat_dim: impl AsIndex,
) -> Vec<Self> {
let split_dim = unwrap_dim_index(split_dim.try_dim_index(D), "All to All");
let concat_dim = unwrap_dim_index(concat_dim.try_dim_index(D), "All to All");
check!(TensorCheck::collective("All to All", &tensors));
check!(TensorCheck::collective_chunks(
"All to All",
&tensors,
split_dim
));
from_primitives(K::all_to_all(
into_primitives(tensors),
split_dim,
concat_dim,
))
}
/// Gives each of `devices` the tensor, in the order of `devices`.
///
/// To repeat a tensor over a shape rather than over devices, see [`Tensor::expand`].
///
/// # Panics
///
/// - If `devices` is empty.
/// - If the tensor is quantized.
///
/// # Example
///
/// ```rust
/// use burn_tensor::{Device, Tensor};
///
/// let device = Device::default();
/// let tensor = Tensor::<1>::from_data([1.0, 2.0], &device);
///
/// let members = Tensor::broadcast(tensor, &[device.clone(), device]);
///
/// assert_eq!(members[0].clone().try_into_vec_as::<f32>().unwrap(), vec![1.0, 2.0]);
/// assert_eq!(members[1].clone().try_into_vec_as::<f32>().unwrap(), vec![1.0, 2.0]);
/// ```
pub fn broadcast(tensor: Self, devices: &[Device]) -> Vec<Self> {
check!(TensorCheck::collective(
"Broadcast",
core::slice::from_ref(&tensor)
));
check!(TensorCheck::collective_devices("Broadcast", devices));
from_primitives(K::broadcast(tensor.primitive, devices))
}
}
fn into_primitives<const D: usize, K: Basic>(tensors: Vec<Tensor<D, K>>) -> Vec<BridgeTensor> {
tensors.into_iter().map(|tensor| tensor.primitive).collect()
}
fn from_primitives<const D: usize, K: Basic>(primitives: Vec<BridgeTensor>) -> Vec<Tensor<D, K>> {
primitives.into_iter().map(Tensor::new).collect()
}