-
-
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.
Re-using tensor storage when possible (#664)
* Tmp commit of phantom tensors * Fixing all kernels to use ghost tensors * Adding backward without data * Adding forward_reuse/backward_without_data for unary ops * binary kernel reuse * Reusing more for unary op * Removing refernece to output data * Fixing nightly ops * Refactor to use Result in ops * Cuda check passing * Marking kernels as const/df_uses_fx * Updates * Not saving inp for sum_to * Updates recip * Style * Prefering to use RHS when possible in cpu kernel * Prefering RHS for cuda as well
- Loading branch information
1 parent
452d1d8
commit 3ec1042
Showing
78 changed files
with
880 additions
and
422 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::{shapes::*, tensor::*}; | ||
|
||
/// Holds all the information a [Tensor] does, except without | ||
/// holding a reference to the data storage. | ||
/// | ||
/// This can held reduce memory usage by decreasing reference | ||
/// count on tensor data, meaning data can be re-used more. | ||
pub struct GhostTensor<S: Shape, E: Unit, D: DeviceStorage> { | ||
pub(crate) id: UniqueId, | ||
pub(crate) len: usize, | ||
pub(crate) shape: S, | ||
pub(crate) strides: S::Concrete, | ||
pub(crate) dev: D, | ||
marker: std::marker::PhantomData<E>, | ||
} | ||
|
||
impl<S: Shape, E: Unit, D: DeviceStorage, T> Tensor<S, E, D, T> { | ||
/// Creates a ghost tensor that doesn't hold a reference | ||
/// to the tensor's data. | ||
pub(crate) fn ghost(&self) -> GhostTensor<S, E, D> { | ||
GhostTensor { | ||
id: self.id, | ||
len: self.device.len(&self.data), | ||
shape: self.shape, | ||
strides: self.strides, | ||
dev: self.device.clone(), | ||
marker: std::marker::PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<S: Shape, E: Unit, D: DeviceStorage> super::storage_traits::HasErr for GhostTensor<S, E, D> { | ||
type Err = D::Err; | ||
} | ||
|
||
impl<S: Shape, E: Unit, D: DeviceStorage> super::storage_traits::AllocGrad | ||
for GhostTensor<S, E, D> | ||
{ | ||
type Gradient = D::Vec<E>; | ||
fn try_alloc_grad(&self) -> Result<Self::Gradient, D::Err> { | ||
self.dev.try_alloc_len(self.len) | ||
} | ||
} |
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
Oops, something went wrong.