forked from coreylowman/dfdx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'unstack' into new-base
- Loading branch information
Showing
7 changed files
with
401 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::{ | ||
prelude::NoneTape, | ||
shapes::*, | ||
tensor::{unique_id, Cpu, Error, Tensor}, | ||
}; | ||
|
||
// note: in order to return NoneTape items and not require a tape type information T, | ||
// each element must be optional. | ||
impl<E: Dtype> super::UnstackKernel<E> for Cpu { | ||
fn forward<S: Shape, OptionalItems>( | ||
&self, | ||
stack: Tensor<S, E, Self, NoneTape>, | ||
) -> Result<OptionalItems, Error> | ||
where | ||
S: super::SubDim, | ||
OptionalItems: Array<Option<Tensor<S::Tail, E, Self, NoneTape>>, Dim = S::Head>, | ||
{ | ||
let (head, tail) = stack.shape().sub_dim(); | ||
let stack_data = stack.data.as_slice(); | ||
let unstack_num_elements = tail.num_elements(); | ||
Ok(OptionalItems::from_fn( | ||
|i| { | ||
let mut data = self | ||
.try_alloc_elem(unstack_num_elements, E::default()) | ||
// TODO: remove unwrap (needs try_from_fn) | ||
// https://github.com/rust-lang/rust/issues/89379 | ||
.unwrap(); | ||
|
||
data.copy_from_slice( | ||
&stack_data[i * unstack_num_elements..(i + 1) * unstack_num_elements], | ||
); | ||
|
||
Some(Tensor { | ||
id: unique_id(), | ||
data: std::sync::Arc::new(data), | ||
shape: *tail.shape(), | ||
strides: tail.strides(), | ||
device: self.clone(), | ||
tape: NoneTape, | ||
}) | ||
}, | ||
head, | ||
)) | ||
} | ||
fn backward( | ||
&self, | ||
grad_stack: &mut Self::Vec, | ||
grad_unstack: &Self::Vec, | ||
unstack_idx: usize, | ||
) -> Result<(), Error> { | ||
let unstack_num_elements = grad_unstack.len(); | ||
for (i, stacked) in grad_stack | ||
.iter_mut() | ||
.skip(unstack_idx * unstack_num_elements) | ||
.take(unstack_num_elements) | ||
.enumerate() | ||
{ | ||
*stacked += grad_unstack[i]; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
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,27 @@ | ||
use crate::{ | ||
prelude::NoneTape, | ||
shapes::*, | ||
tensor::{Cuda, Error, Tensor}, | ||
}; | ||
use cudarc::types::CudaTypeName; | ||
|
||
impl<E: Dtype + CudaTypeName> super::UnstackKernel<E> for Cuda { | ||
fn forward<S: Shape, OptionalItems>( | ||
&self, | ||
_stack: Tensor<S, E, Self, NoneTape>, | ||
) -> Result<OptionalItems, Error> | ||
where | ||
S: super::SubDim, | ||
OptionalItems: Array<Option<Tensor<S::Tail, E, Self, NoneTape>>, Dim = S::Head>, | ||
{ | ||
todo!() | ||
} | ||
fn backward( | ||
&self, | ||
_grad_stack: &mut Self::Vec, | ||
_grad_unstack: &Self::Vec, | ||
_unstack_idx: usize, | ||
) -> Result<(), Error> { | ||
todo!() | ||
} | ||
} |
Oops, something went wrong.