Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
rileysu committed Jun 24, 2023
0 parents commit 01c6366
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 1,2 @@
/target
/old_src
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 1,3 @@
{
"rust-analyzer.showUnlinkedFileNotification": false
}
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 1,8 @@
[package]
name = "therml"
version = "0.1.0"
edition = "2021"

[dependencies]
ord_subset = "3.1.1"
wide = "0.7.8"
8 changes: 8 additions & 0 deletions ideas.md
Original file line number Diff line number Diff line change
@@ -0,0 1,8 @@
# Ideas

## Context

- Context creates tensors and handles data storage
- Internally will create graphs for operations
- Will handle gradient calc

5 changes: 5 additions & 0 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
mod tensor;

pub trait Engine {
type Data:
}
45 changes: 45 additions & 0 deletions src/engine/tensor/array.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,45 @@
use std::sync::Arc;
use crate::helper::{Shape, Stride};
use super::EngineTensor;

struct ArrayEngineTensor<T: Sized> {
data: Arc<[T]>,
shape: Shape,
stride: Stride,
}



impl<T: Sized> EngineTensor<T> for ArrayEngineTensor<T> {
type EngineTensorIterator = ArrayEngineTensorIterator<T>;

fn shape(&self) -> Shape {
todo!()
}

fn stride(&self) -> Stride {
todo!()
}

fn view(&self, shape: Shape) -> Result<Self, ()> {
todo!()
}

fn iter(&self, comp_range: &[std::ops::Range<usize>]) -> Self::EngineTensorIterator {
todo!()
}
}

struct ArrayEngineTensorIterator<T: Sized> {
base: ArrayEngineTensor<T>,
pos: Vec<usize>,
end: Vec<usize>,
}

impl<T: Sized> Iterator for ArrayEngineTensorIterator<T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {

}
}
27 changes: 27 additions & 0 deletions src/engine/tensor/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,27 @@
pub mod array;

use std::ops::Range;

use crate::helper::{Shape, Stride};

// No mutation should be done through this trait
// It exists purely as a way to get data for the engine
trait EngineTensor<T: Sized>
where Self: Sized
{
type EngineTensorIterator: Iterator<Item = T>;

fn shape(&self) -> Shape;
fn stride(&self) -> Stride;

fn is_contiguous(&self) -> bool {
Stride::from(self.shape()) == self.stride()
}

// Create a view into the same memory
fn view(&self, shape: Shape) -> Result<Self, ()>;

// Iterator from element to element in order
fn iter(&self, comp_range: &[Range<usize>]) -> Self::EngineTensorIterator;
//fn (comp_range: &[Range<usize>]) -> Self::DataSlice;
}
5 changes: 5 additions & 0 deletions src/helper/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
mod shape;
mod stride;

pub use shape::*;
pub use stride::*;
7 changes: 7 additions & 0 deletions src/helper/position.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
#[derive(Clone, PartialEq, Eq, Debug)]
struct Position(Box<[usize]>);

impl Position {

}

18 changes: 18 additions & 0 deletions src/helper/shape.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,18 @@
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Shape(Box<[usize]>);

impl Shape {
pub fn as_boxed_slice(&self) -> &Box<[usize]> {
&self.0
}

pub fn total_elements(&self) -> usize {
self.0.iter().product()
}
}

impl From<&[usize]> for Shape {
fn from(value: &[usize]) -> Self {
Self(Box::from(value))
}
}
49 changes: 49 additions & 0 deletions src/helper/stride.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,49 @@
use crate::helper::Shape;

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Stride(Box<[usize]>);

impl Stride {
pub fn as_boxed_slice(&self) -> &Box<[usize]> {
&self.0
}
}

impl From<&[usize]> for Stride {
fn from(value: &[usize]) -> Self {
Self(Box::from(value))
}
}

impl From<Shape> for Stride {
fn from(value: Shape) -> Self {
let mut stride: Vec<usize> = Vec::with_capacity(value.as_boxed_slice().len());

let mut next = 1usize;
for dim in value.as_boxed_slice().iter().rev() {
stride.push(next);
next = dim * next;
}

stride.reverse();

Stride(stride.into_boxed_slice())
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn from_shape_examples() {
let examples = [
(Shape::from([1].as_slice()), Stride::from([1].as_slice())),
(Shape::from([20, 50, 4].as_slice()), Stride::from([200, 4, 1].as_slice()))
];

for (shape, stride) in examples {
assert_eq!(Stride::from(shape), stride);
}
}
}
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,6 @@
mod engine;
mod helper;

fn main() {

}

0 comments on commit 01c6366

Please sign in to comment.