-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
d5a19df
commit 148826c
Showing
10 changed files
with
86 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use super::utils::move_tape_and_add_backward_op; | ||
use crate::prelude::*; | ||
|
||
/// Reshapes `t` into a differently shaped tensor. [T] and [R] must have the same | ||
/// number of elements. | ||
/// | ||
/// # Example | ||
/// ```rust | ||
/// # use dfdx::prelude::*; | ||
/// let t = Tensor1D::new([1.0, 2.0, 3.0, 4.0]); | ||
/// let r: Tensor2D<2, 2> = reshape(t); | ||
/// assert_eq!(r.data(), &[[1.0, 2.0], [3.0, 4.0]]); | ||
/// ``` | ||
pub fn reshape<T, R>(t: T) -> R | ||
where | ||
T: Tensor<Dtype = f32>, | ||
R: Tensor<Tape = T::Tape, Dtype = f32>, | ||
ConstEq<{ T::Array::NUM_ELEMENTS }, { R::Array::NUM_ELEMENTS }>: ConstTrue, | ||
ConstEq<{ R::Array::NUM_ELEMENTS }, { T::Array::NUM_ELEMENTS }>: ConstTrue, | ||
{ | ||
let mut result: R::NoTape = R::NoTape::zeros(); | ||
copy(t.data(), result.mut_data()); | ||
move_tape_and_add_backward_op(t, result, move |mut t, result, grads| { | ||
let (t_grad, result_grad) = grads.mut_and_ref(&t, &result); | ||
copy(result_grad, t.mut_data()); | ||
T::Device::add(t_grad, t.data()); | ||
}) | ||
} | ||
|
||
fn copy<Lhs: CountElements, Rhs: CountElements<Dtype = Lhs::Dtype>>(lhs: &Lhs, rhs: &mut Rhs) | ||
where | ||
ConstEq<{ Lhs::NUM_ELEMENTS }, { Rhs::NUM_ELEMENTS }>: ConstTrue, | ||
{ | ||
let l = lhs.ref_first_elem() as *const Lhs::Dtype; | ||
let r = rhs.mut_first_elem() as *mut Lhs::Dtype; | ||
unsafe { | ||
std::ptr::copy_nonoverlapping(l, r, Lhs::NUM_ELEMENTS); | ||
} | ||
} | ||
|
||
pub trait ConstTrue {} | ||
|
||
pub struct ConstEq<const A: usize, const B: usize>; | ||
impl<const N: usize> ConstTrue for ConstEq<N, N> {} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_0d_reshape() { | ||
let a = Tensor0D::new(3.14); | ||
let b: Tensor1D<1> = reshape(a.duplicate()); | ||
assert_eq!(b.data(), &[3.14]); | ||
|
||
let c: Tensor2D<1, 1> = reshape(a.duplicate()); | ||
assert_eq!(c.data(), &[[3.14]]); | ||
} | ||
|
||
#[test] | ||
fn test_1d_reshape() { | ||
let a = Tensor1D::new([0.1, 0.2, 0.3, 0.4, 0.5, 0.6]); | ||
let b: Tensor2D<2, 3, OwnedTape> = reshape(a.trace()); | ||
assert_eq!(b.data(), &[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]); | ||
let gradients = b.exp().mean().backward(); | ||
assert_eq!( | ||
gradients.ref_gradient(&a), | ||
&[0.18419516, 0.20356713, 0.22497648, 0.24863747, 0.2747869, 0.3036865] | ||
) | ||
} | ||
} |