Skip to content

Commit

Permalink
Merge pull request #3 from alphal00p/tensors
Browse files Browse the repository at this point in the history
Tensors
  • Loading branch information
lcnbr authored Mar 12, 2024
2 parents a88e79c + 17a8e93 commit 2c918a8
Show file tree
Hide file tree
Showing 36 changed files with 9,732 additions and 1,551 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST

out.yaml
outmap.yaml
params.yaml
find-baseline.svg
*.data
*.old
eval.cpp
evaluate.cpp
perf.data
# logos
logos/

Expand Down
56 changes: 50 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,33 @@ repository = "https://github.com/alphal00p/gammaloop"
[profile.dev-optim]
inherits = "dev"
opt-level = 2
#lto=true

[profile.bench]
lto = "fat"

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }

[[bench]]
name = "gamma_net"
harness = false

[[bench]]
name="evaluate_net"
harness=false

[[example]]
name = "gamma_chains"
path= "examples/Rust/Tensors/gamma_chain.rs"

[[example]]
name = "gamma_network"
path="examples/Rust/Tensors/gamma_network.rs"

[[example]]
name="evaluate_network"
path="examples/Rust/Tensors/evaluate_network.rs"

[dependencies]
# You may need bleeding edge changes
Expand All @@ -32,8 +59,6 @@ lorentz_vector = { git = "https://github.com/benruijl/lorentz_vector", branch =
"hyperdual_support",
"f128_support",
] }
num = "0.3"
num-traits = "0.2"
hyperdual = { git = "https://gitlab.com/benruijl/hyperdual" }
rand = "0.8"
rayon = "1.5"
Expand All @@ -44,17 +69,36 @@ colored = "*"
yaml-rust = "0.4"
libc = "0.2.0"
statrs = "0.16.0"
smallvec = "1.7"
smallvec = { version = "1.7", features = ["const_generics"] }
itertools = "0.8"
smartstring = { version = "*", features = ["serde"] }
ahash = "*"
vectorize = "0.2.0"
log = "*"
env_logger = "*"
pyo3-log = "*"
nalgebra = "0.32.3"
num-complex = "0.4.4"
rug = "1.22.0"
wide = "0.7.13"
arbitrary-int = { version = "1.2.6", features = ["num-traits"] }
duplicate = "1.0.0"
rustc-hash = "1.1.0"
petgraph = "0.6.4"
enum-try-as-inner = "0.1.1"
indexmap = "2.2.2"
nohash-hasher = "0.2.0"
intmap = { git = "https://github.com/lcnbr/rust-intmap" }
permutation = "0.4.1"
slotmap = { version = "1.0.7", features = ["serde"] }
ahash = { version = "0.8.8", features = ["serde"] }
num = { version = "0.4.1", features = ["serde"] }
pprof = { version = "0.13.0", features = ["flamegraph"] }
derive_more = "0.99.17"
rand_xoshiro = "0.6.0"
funty = "2.0.0"
block-id = "0.2.1"
once_cell = "1.19.0"
enum_delegate = {git="https://gitlab.com/dawn_app/enum_delegate"}

[dependencies.pyo3]
features = ["multiple-pymethods"]
Expand All @@ -68,11 +112,11 @@ pyo3-build-config = "*"
crate-type = ["cdylib", "lib"]
name = "_gammaloop"
required-features = ["python_api"]

bench = false
[[bin]]
name = "cli"
required-features = ["binary"]

bench = false
[features]
extension-module = ["pyo3/extension-module"]
default = ["python_api"]
Expand Down
158 changes: 158 additions & 0 deletions benches/evaluate_net.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
use std::{fmt::Debug, ops::Neg};

use _gammaloop::tensor::{
ufo::{euclidean_four_vector, gamma},
AbstractIndex, ContractionCountStructure, FallibleMul, MixedTensor, Representation,
SetTensorData, Slot, SparseTensor, TensorNetwork, TensorStructure,
};

use ahash::{AHashMap, HashMap};
use criterion::{criterion_group, criterion_main, Criterion};

use rand::{distributions::Uniform, Rng, SeedableRng};
use rand_xoshiro::Xoroshiro64Star;
use symbolica::{
domains::float::Complex,
representations::{Atom, AtomView},
state::State,
};

fn indices(n: i32, m: i32) -> Vec<i32> {
let spacings: [i32; 2] = [n, m];
let mut start = 1;
let mut ranges = Vec::new();

for &spacing in spacings.iter() {
ranges.push((start..start + spacing).chain(std::iter::once(-1)));
start += spacing;
}

ranges.into_iter().flatten().collect()
}

fn gamma_net_param(
minkindices: &[i32],
vbar: [Complex<f64>; 4],
u: [Complex<f64>; 4],
) -> TensorNetwork<MixedTensor<ContractionCountStructure>> {
let mut i: i32 = 0;
let mut contracting_index = 0.into();
let mut result: Vec<MixedTensor<ContractionCountStructure>> =
vec![euclidean_four_vector(contracting_index, &vbar).into()];
for m in minkindices {
let ui = contracting_index;
contracting_index += 1.into();
let uj = contracting_index;
if *m > 0 {
let p: ContractionCountStructure = vec![Slot::from((
usize::try_from(*m).unwrap().into(),
Representation::Lorentz(4.into()),
))]
.into_iter()
.collect();
i += 1;
let pid = State::get_or_insert_fn(&format!("p{}", i), None).unwrap();

result.push(p.shadow_with(pid).into());

result.push(gamma(usize::try_from(*m).unwrap().into(), (ui, uj)).into());
} else {
result.push(
gamma(
AbstractIndex::from(usize::try_from(m.neg()).unwrap() + 10000),
(ui, uj),
)
.into(),
);
}
}
result.push(euclidean_four_vector(contracting_index, &u).into());
TensorNetwork::from(result)
}

fn test_tensor<S>(structure: S) -> SparseTensor<symbolica::domains::float::Complex<f64>, S>
where
S: TensorStructure,
{
let mut rng: Xoroshiro64Star = Xoroshiro64Star::from_entropy();

let mut tensor = SparseTensor::empty(structure);

let density = tensor.size();

let multipliable = Uniform::new(1., 10.);

for _ in 0..density {
tensor
.set_flat(
rng.gen_range(0..tensor.size()),
Complex::<f64>::new(rng.sample(multipliable), rng.sample(multipliable)),
)
.unwrap();
}

tensor
}

fn const_map_gen<'a, 'b, I>(
params: &'a [MixedTensor<I>],
const_map: &mut HashMap<AtomView<'b>, symbolica::domains::float::Complex<f64>>,
) where
'a: 'b,
I: TensorStructure + Clone + Debug,
{
for (_i, p) in params.iter().enumerate() {
let pdata = test_tensor(p.structure().clone()).to_dense();
p.try_as_symbolic()
.unwrap()
.try_as_dense()
.unwrap()
.append_const_map(&pdata, const_map);
}
}
fn criterion_benchmark(c: &mut Criterion) {
let one = Complex::<f64>::new(1.0, 0.0);

let vbar = [
one.mul_fallible(3.0).unwrap(),
one.mul_fallible(3.1).unwrap(),
one.mul_fallible(3.2).unwrap(),
one.mul_fallible(3.3).unwrap(),
];
let u = [
one.mul_fallible(4.0).unwrap(),
one.mul_fallible(4.1).unwrap(),
one.mul_fallible(4.2).unwrap(),
one.mul_fallible(4.3).unwrap(),
];
let minkindices = indices(20, 24);

let mut net = gamma_net_param(&minkindices, vbar, u);
net.generate_params();
let params = net.params.clone();
println!("{:?}", params.len());
net.contract_algo(|tn| tn.edge_to_min_degree_node_with_depth(2));
let mut const_map = AHashMap::new();

let i = Atom::new_var(State::I);
const_map.insert(i.as_view(), Complex::<f64>::new(0., 1.));

let mut group = c.benchmark_group("evaluate_net");

group.bench_function("Evaluate_net", |b| {
b.iter_batched(
|| net.clone(),
|mut net| {
const_map_gen(&params, &mut const_map);

net.evaluate_complex(&const_map);

net.contract();
},
criterion::BatchSize::SmallInput,
)
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading

0 comments on commit 2c918a8

Please sign in to comment.