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
use crate::{
shapes::{Dtype, Shape},
tensor::{unique_id, Cpu, Tensor},
};
impl<E: Dtype> super::ConcatKernel<E> for Cpu {
fn forward<A: Shape, B: Shape>(
&self,
a: &Tensor<A, E, Self>,
b: &Tensor<B, E, Self>,
) -> Result<Tensor<A::Catted, E, Self>, Self::Err>
where
A: super::ConcatShape<B>,
{
let shape = a.shape.concat_shape(&b.shape);
let mut data = std::vec::Vec::with_capacity(shape.num_elements());
if a.strides == a.shape.strides() {
data.extend(a.data.as_ref());
} else {
data.extend(a.as_vec());
}
if b.strides == b.shape.strides() {
data.extend(b.data.as_ref());
} else {
data.extend(b.as_vec());
}
Ok(Tensor {
id: unique_id(),
data: std::sync::Arc::new(data),
shape,
strides: shape.strides(),
device: self.clone(),
tape: Default::default(),
})
}
fn backward<A: Shape, B: Shape>(
&self,
_: &Tensor<A, E, Self>,
grad_a: &mut Self::Vec<E>,
_: &Tensor<B, E, Self>,
grad_b: &mut Self::Vec<E>,
_: &Tensor<A::Catted, E, Self>,
grad_out: &Self::Vec<E>,
) -> Result<(), Self::Err>
where
A: super::ConcatShape<B>,
{
let mut offset = 0;
for ga in grad_a.iter_mut() {
*ga += grad_out[offset];
offset += 1;
}
for gb in grad_b.iter_mut() {
*gb += grad_out[offset];
offset += 1;
}
Ok(())
}
}