forked from tracel-ai/burn
-
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.
- Loading branch information
1 parent
a599eae
commit 3a91c2c
Showing
34 changed files
with
650 additions
and
273 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
mod base; | ||
pub use base::*; | ||
mod grad; | ||
pub use grad::*; | ||
|
||
// Not needed for now, usefull for different tensor memory layout | ||
// pub mod conversion; |
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,73 @@ | ||
use crate::{backend::Backend, Tensor}; | ||
use std::{any::Any, collections::HashMap}; | ||
|
||
/// Contains tensor of arbitrary dimension. | ||
#[derive(Debug)] | ||
pub struct TensorContainer<B: Backend, ID> { | ||
tensors: HashMap<ID, Box<dyn Any + Send + Sync>>, | ||
_b: B, | ||
} | ||
|
||
impl<B, ID> Default for TensorContainer<B, ID> | ||
where | ||
B: Backend, | ||
ID: std::hash::Hash + PartialEq + Eq, | ||
{ | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl<B, ID> TensorContainer<B, ID> | ||
where | ||
B: Backend, | ||
ID: std::hash::Hash + PartialEq + Eq, | ||
{ | ||
/// Create an empty container. | ||
pub fn new() -> Self { | ||
Self { | ||
tensors: HashMap::new(), | ||
_b: B::default(), | ||
} | ||
} | ||
|
||
/// Get a tensor with the given ID. | ||
pub fn get<const D: usize>(&self, id: &ID) -> Option<Tensor<B, D>> { | ||
let grad = match self.tensors.get(id) { | ||
Some(grad) => grad, | ||
None => return None, | ||
}; | ||
|
||
let tensor = grad | ||
.downcast_ref() | ||
.map(|primitive: &B::TensorPrimitive<D>| Tensor::from_primitive(primitive.clone())); | ||
tensor | ||
} | ||
|
||
/// Register a new tensor for the given ID. | ||
/// | ||
/// # Notes | ||
/// | ||
/// If a tensor is already registered for the given ID, it will be replaced. | ||
pub fn register<const D: usize>(&mut self, id: ID, value: Tensor<B, D>) { | ||
self.tensors.insert(id, Box::new(value.into_primitive())); | ||
} | ||
|
||
/// Remove a tensor for the given ID and returns it. | ||
pub fn remove<const D: usize>(&mut self, id: &ID) -> Option<Tensor<B, D>> { | ||
self.tensors | ||
.remove(id) | ||
.map(|item| item.downcast::<B::TensorPrimitive<D>>().unwrap()) | ||
.map(|primitive| Tensor::from_primitive(*primitive)) | ||
} | ||
|
||
/// The number of tensors registered. | ||
pub fn len(&self) -> usize { | ||
self.tensors.len() | ||
} | ||
|
||
/// If any tensor is contained. | ||
pub fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
} |
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.