Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"rust-analyzer.linkedProjects": ["./Cargo.toml"]
"rust-analyzer.linkedProjects": ["./Cargo.toml", "./Cargo.toml"]
}
16 changes: 5 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
[package]
name = "phantom"
version = "0.1.0"
edition = "2021"
[workspace]
resolver = "2"

description = "A forward mode autodiff and deep learning library for Rust"
license = "MIT"
readme = "README.md"

[dependencies]
anyhow = "1.0.75"
thiserror = "1"
members = [
"phantom-core",
]
19 changes: 19 additions & 0 deletions phantom-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "phantom-core"
version = "0.1.0"
edition = "2021"

description = "A forward mode autodiff and deep learning library for Rust"
license = "MIT"
readme = "README.md"

[dependencies]
anyhow = { version = "1.0.75", features = ["backtrace"]}
num-traits = "0.2.16"
thiserror = "1"

# TODO: Switch back to the official gemm implementation once something similar to
# https://github.com/sarah-ek/gemm/pull/8 is available.
gemm = { git = "https://github.com/LaurentMazare/gemm.git", branch = "f16-vectorize-pack" }
rand = "0.8.5"
num_cpus = "1.16.0"
95 changes: 95 additions & 0 deletions phantom-core/src/backend/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::operation::{BinaryOperation, UnaryOperation};
use crate::{CPUStorage, DType, Layout, Result, Shape};

pub(crate) trait BackendStorage: Sized {
type Device: BackendDevice;

fn to_cpu(&self) -> Result<CPUStorage>;

fn dtype(&self) -> DType;

fn to_dtype(&self, layout: &Layout, dtype: DType) -> Result<Self>;

fn device(&self) -> &Self::Device;

fn try_clone(&self, layout: &Layout) -> Result<Self>;

fn copy_strided_source(
&self,
destination: &mut Self,
destination_offset: usize,
source_layout: &Layout,
) -> Result<()>;

fn unary_operation<T: UnaryOperation>(&self, layout: &Layout) -> Result<Self>;

fn binary_operation<T: BinaryOperation>(
&self,
rhs: &Self,
lhs_layout: &Layout,
rhs_layout: &Layout,
) -> Result<Self>;

fn affine(&self, layout: &Layout, add: f64, mul: f64) -> Result<Self>;

fn matmul(
&self,
rhs: &Self,
bmnk: (usize, usize, usize, usize),
lhs_layout: &Layout,
rhs_layout: &Layout,
) -> Result<Self>;

fn where_condition(
&self,
_: &Layout,
_: &Self,
_: &Layout,
_: &Self,
_: &Layout,
) -> Result<Self>;

// fn conv1d(
// &self,
// _l: &Layout,
// _kernel: &Self,
// _kernel_l: &Layout,
// _params: &crate::conv::ParamsConv1D,
// ) -> Result<Self>;

fn embedding(&self, _: &Layout, _: &Self, _: &Layout) -> Result<Self>;

fn sum(&self, _: &Layout, _: &[usize]) -> Result<Self>;
}

pub(crate) trait BackendDevice: Sized + std::fmt::Debug + Clone {
type Storage: BackendStorage;

fn new(_: usize) -> Result<Self>;

fn location(&self) -> crate::device::DeviceLocation;

fn same_device(&self, _: &Self) -> bool;

fn zeros_impl(&self, shape: &Shape, dtype: DType) -> Result<Self::Storage>;

fn ones_impl(&self, shape: &Shape, dtype: DType) -> Result<Self::Storage>;

fn rand_uniform(
&self,
shape: &Shape,
dtype: DType,
lower_bound: f64,
upper_bound: f64,
) -> Result<Self::Storage>;

fn rand_normal(
&self,
shape: &Shape,
dtype: DType,
mean: f64,
std: f64,
) -> Result<Self::Storage>;

fn from_cpu(&self, storage: &CPUStorage) -> Result<Self::Storage>;
}
Loading