-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from termoshtt/partial-summation2
Generate partial summation Rust code
- Loading branch information
Showing
16 changed files
with
348 additions
and
199 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[workspace] | ||
members = [ | ||
"einsum-derive", | ||
"einsum-solver", | ||
"einsum-codegen", | ||
] |
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,5 +1,5 @@ | ||
[package] | ||
name = "einsum-solver" | ||
name = "einsum-codegen" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
File renamed without changes.
File renamed without changes.
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,66 @@ | ||
//! For [ndarray](https://crates.io/crates/ndarray) crate | ||
pub mod naive; | ||
|
||
use crate::subscripts::Subscripts; | ||
use proc_macro2::TokenStream as TokenStream2; | ||
use quote::{format_ident, quote}; | ||
|
||
fn dim(n: usize) -> syn::Path { | ||
let ix = quote::format_ident!("Ix{}", n); | ||
syn::parse_quote! { ndarray::#ix } | ||
} | ||
|
||
/// Generate einsum function definition | ||
pub fn function_definition(subscripts: &Subscripts, inner: TokenStream2) -> TokenStream2 { | ||
let fn_name = format_ident!("{}", subscripts.escaped_ident()); | ||
let n = subscripts.inputs.len(); | ||
|
||
let args = &subscripts.inputs; | ||
let storages: Vec<syn::Ident> = (0..n).map(|n| quote::format_ident!("S{}", n)).collect(); | ||
let dims: Vec<syn::Path> = subscripts | ||
.inputs | ||
.iter() | ||
.map(|ss| dim(ss.indices().len())) | ||
.collect(); | ||
|
||
let out_dim = dim(subscripts.output.indices().len()); | ||
|
||
quote! { | ||
fn #fn_name<T, #(#storages),*>( | ||
#( #args: ndarray::ArrayBase<#storages, #dims> ),* | ||
) -> ndarray::Array<T, #out_dim> | ||
where | ||
T: ndarray::LinalgScalar, | ||
#( #storages: ndarray::Data<Elem = T> ),* | ||
{ | ||
#inner | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::{codegen::format_block, *}; | ||
|
||
#[test] | ||
fn function_definition_snapshot() { | ||
let mut namespace = Namespace::init(); | ||
let subscripts = Subscripts::from_raw_indices(&mut namespace, "ij,jk->ik").unwrap(); | ||
let inner = quote::quote! { todo!() }; | ||
let tt = format_block(super::function_definition(&subscripts, inner).to_string()); | ||
insta::assert_snapshot!(tt, @r###" | ||
fn ij_jk__ik<T, S0, S1>( | ||
arg0: ndarray::ArrayBase<S0, ndarray::Ix2>, | ||
arg1: ndarray::ArrayBase<S1, ndarray::Ix2>, | ||
) -> ndarray::Array<T, ndarray::Ix2> | ||
where | ||
T: ndarray::LinalgScalar, | ||
S0: ndarray::Data<Elem = T>, | ||
S1: ndarray::Data<Elem = T>, | ||
{ | ||
todo!() | ||
} | ||
"###); | ||
} | ||
} |
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
File renamed without changes.
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.