-
-
Notifications
You must be signed in to change notification settings - Fork 104
AddInto #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
AddInto #256
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
54f9eae
begin on AddInto
Dimev c7cd6ee
base case
Dimev eb8fb7e
macro broken
Dimev 9750e04
only a few more trait bounds
Dimev fc56db9
previous plan did not work
Dimev cfa3a5c
more tape issues
Dimev c36d3bd
Merge branch 'main' into multi-input
Dimev 718a1fe
add into passes tests
Dimev 8b5b5c8
format
Dimev 4c3d3fd
added npz and fixed forward mut
Dimev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,238 @@ | ||
use crate::gradients::{CanUpdateWithGradients, GradientProvider, UnusedTensors}; | ||
use crate::prelude::*; | ||
|
||
/// Add inputs together into a single tensor. `T` should be a tuple | ||
//// where every element of the tuple has the same output type | ||
/// | ||
/// This provides a utility for networks where multiple inputs are needed | ||
/// | ||
/// # Generics | ||
/// - `T` the module to add the outputs together of | ||
/// | ||
/// # Examples | ||
/// ```rust | ||
/// # use dfdx::prelude::*; | ||
/// type Model = AddInto<(Linear<2, 5>, Linear<3, 5>)>; | ||
/// let model: Model = Default::default(); | ||
/// let _: Tensor1D<5> = model.forward((Tensor1D::<2>::zeros(), Tensor1D::<3>::zeros())); | ||
/// ``` | ||
#[derive(Debug, Default, Clone)] | ||
pub struct AddInto<T>(pub T); | ||
|
||
impl<T: CanUpdateWithGradients> CanUpdateWithGradients for AddInto<T> { | ||
fn update<G: GradientProvider>(&mut self, grads: &mut G, unused: &mut UnusedTensors) { | ||
self.0.update(grads, unused); | ||
} | ||
} | ||
|
||
impl<T: ResetParams> ResetParams for AddInto<T> { | ||
fn reset_params<R: rand::Rng>(&mut self, rng: &mut R) { | ||
self.0.reset_params(rng); | ||
} | ||
} | ||
|
||
macro_rules! tuple_impls { | ||
($head:ident $headin:ident [$($tails:ident $tailsin:ident),+]) => { | ||
impl< | ||
Output: Tensor<Dtype = f32>, | ||
$headin: Tensor<Dtype = f32>, | ||
$($tailsin: Tensor<Dtype = f32>,)+ | ||
$head: Module<$headin, Output = Output>, | ||
$($tails: Module<$tailsin, Output = Output>,)+ | ||
> Module<($headin, $($tailsin,)+)> for AddInto<($head, $($tails,)+)> { | ||
type Output = Output; | ||
|
||
#[allow(non_snake_case)] | ||
fn forward(&self, x: ($headin, $($tailsin,)+)) -> Self::Output { | ||
|
||
// inputs | ||
let ($head, $($tails),+) = x; | ||
|
||
// layers | ||
let ($headin, $($tailsin),+) = &self.0; | ||
|
||
// forward | ||
let ($head, $($tails),+) = ($headin.forward($head), $($tailsin.forward($tails)),+); | ||
|
||
// add together | ||
$( | ||
let $head = add($head, $tails); | ||
)+ | ||
|
||
$head | ||
} | ||
} | ||
|
||
|
||
impl< | ||
Output: Tensor<Dtype = f32>, | ||
$headin: Tensor<Dtype = f32>, | ||
$($tailsin: Tensor<Dtype = f32>,)+ | ||
$head: ModuleMut<$headin, Output = Output>, | ||
$($tails: ModuleMut<$tailsin, Output = Output>,)+ | ||
> ModuleMut<($headin, $($tailsin,)+)> for AddInto<($head, $($tails,)+)> { | ||
type Output = Output; | ||
|
||
#[allow(non_snake_case)] | ||
fn forward_mut(&mut self, x: ($headin, $($tailsin,)+)) -> Self::Output { | ||
|
||
// inputs | ||
let ($head, $($tails),+) = x; | ||
|
||
// layers | ||
let ($headin, $($tailsin),+) = &mut self.0; | ||
|
||
// forward | ||
let ($head, $($tails),+) = ($headin.forward_mut($head), $($tailsin.forward_mut($tails)),+); | ||
|
||
// add together | ||
$( | ||
let $head = add($head, $tails); | ||
)+ | ||
|
||
$head | ||
} | ||
} } | ||
} | ||
|
||
tuple_impls!(A Ai [B Bi]); | ||
tuple_impls!(A Ai [B Bi, C Ci]); | ||
tuple_impls!(A Ai [B Bi, C Ci, D Di]); | ||
tuple_impls!(A Ai [B Bi, C Ci, D Di, E Ei]); | ||
tuple_impls!(A Ai [B Bi, C Ci, D Di, E Ei, F Fi]); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{nn::tests::SimpleGradients, unique_id::HasUniqueId}; | ||
|
||
#[test] | ||
fn test_add_into_2() { | ||
type Model = AddInto<(Linear<2, 5>, Linear<3, 5>)>; | ||
let m: Model = Default::default(); | ||
let _: Tensor1D<5, OwnedTape> = | ||
m.forward((Tensor1D::zeros().traced(), Tensor1D::zeros().traced())); | ||
let _: Tensor2D<3, 5, OwnedTape> = m.forward(( | ||
Tensor2D::<3, 2>::zeros().traced(), | ||
Tensor2D::<3, 3>::zeros().traced(), | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_add_into_3() { | ||
type Model = AddInto<(Linear<2, 5>, Linear<3, 5>, Linear<4, 5>)>; | ||
let m: Model = Default::default(); | ||
let _: Tensor1D<5, OwnedTape> = m.forward(( | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
)); | ||
let _: Tensor2D<3, 5, OwnedTape> = m.forward(( | ||
Tensor2D::<3, 2>::zeros().traced(), | ||
Tensor2D::<3, 3>::zeros().traced(), | ||
Tensor2D::<3, 4>::zeros().traced(), | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_add_into_4() { | ||
type Model = AddInto<(Linear<2, 5>, Linear<3, 5>, Linear<4, 5>, Linear<5, 5>)>; | ||
let m: Model = Default::default(); | ||
let _: Tensor1D<5, OwnedTape> = m.forward(( | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
)); | ||
let _: Tensor2D<3, 5, OwnedTape> = m.forward(( | ||
Tensor2D::<3, 2>::zeros().traced(), | ||
Tensor2D::<3, 3>::zeros().traced(), | ||
Tensor2D::<3, 4>::zeros().traced(), | ||
Tensor2D::<3, 5>::zeros().traced(), | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_add_into_5() { | ||
type Model = AddInto<( | ||
Linear<2, 5>, | ||
Linear<3, 5>, | ||
Linear<4, 5>, | ||
Linear<5, 5>, | ||
Linear<6, 5>, | ||
)>; | ||
let m: Model = Default::default(); | ||
let _: Tensor1D<5, OwnedTape> = m.forward(( | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
)); | ||
let _: Tensor2D<3, 5, OwnedTape> = m.forward(( | ||
Tensor2D::<3, 2>::zeros().traced(), | ||
Tensor2D::<3, 3>::zeros().traced(), | ||
Tensor2D::<3, 4>::zeros().traced(), | ||
Tensor2D::<3, 5>::zeros().traced(), | ||
Tensor2D::<3, 6>::zeros().traced(), | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_add_into_6() { | ||
type Model = AddInto<( | ||
Linear<2, 5>, | ||
Linear<3, 5>, | ||
Linear<4, 5>, | ||
Linear<5, 5>, | ||
Linear<6, 5>, | ||
Linear<7, 5>, | ||
)>; | ||
let m: Model = Default::default(); | ||
let _: Tensor1D<5, OwnedTape> = m.forward(( | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
Tensor1D::zeros().traced(), | ||
)); | ||
let _: Tensor2D<3, 5, OwnedTape> = m.forward(( | ||
Tensor2D::<3, 2>::zeros().traced(), | ||
Tensor2D::<3, 3>::zeros().traced(), | ||
Tensor2D::<3, 4>::zeros().traced(), | ||
Tensor2D::<3, 5>::zeros().traced(), | ||
Tensor2D::<3, 6>::zeros().traced(), | ||
Tensor2D::<3, 7>::zeros().traced(), | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_missing_gradients() { | ||
let mut model: AddInto<(Linear<5, 3>, Linear<5, 3>)> = Default::default(); | ||
let mut g: SimpleGradients = Default::default(); | ||
|
||
// no gradients present | ||
let mut unused = Default::default(); | ||
model.update(&mut g, &mut unused); | ||
assert_eq!( | ||
&unused.ids, | ||
&[ | ||
*model.0 .0.weight.id(), | ||
*model.0 .0.bias.id(), | ||
*model.0 .1.weight.id(), | ||
*model.0 .1.bias.id() | ||
] | ||
); | ||
|
||
// weight gradient is present | ||
g.0.mut_gradient(&model.0 .0.weight); | ||
g.0.mut_gradient(&model.0 .0.bias); | ||
g.0.mut_gradient(&model.0 .1.weight); | ||
g.0.mut_gradient(&model.0 .1.bias); | ||
|
||
let mut unused = Default::default(); | ||
model.update(&mut g, &mut unused); | ||
assert!(unused.is_empty()); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.