Skip to content

Commit

Permalink
Benchmark neural net inputs generation
Browse files Browse the repository at this point in the history
  • Loading branch information
carsten-wenderdel committed Dec 6, 2023
1 parent 5edf1d0 commit db06af9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crates/benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ harness = false
[dependencies]
rayon.workspace = true


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

[[bench]]
name = "inputs_bench"
harness = false
44 changes: 44 additions & 0 deletions crates/benchmarks/benches/inputs_bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::helper::{contact_positions, race_positions};
use criterion::{criterion_group, criterion_main, Criterion};
use engine::inputs::{ContactInputsGen, InputsGen, RaceInputsGen};
use engine::position::Position;
use std::hint::black_box;

mod helper;

// This file contains benchmarks for generating neural net inputs for a given position.

// Helper Methods

fn sum_of_all_inputs<T: InputsGen>(positions: &[Position], inputs_gen: &T) {
// Create 20 inputs at once. Should have roughly the same size as number of legal moves from a random position.
let batch_size = 20;
positions.chunks_exact(batch_size).for_each(|chunk| {
inputs_gen.inputs_for_all(chunk);
});
}

// Benchmark methods

#[allow(dead_code)]
fn contact_inputs(c: &mut Criterion) {
let positions = contact_positions();
let inputs_gen = ContactInputsGen {};

c.bench_function("generate inputs for: contact", |b| {
b.iter(|| sum_of_all_inputs(black_box(&positions), &inputs_gen))
});
}

#[allow(dead_code)]
fn race_inputs(c: &mut Criterion) {
let positions = race_positions();
let inputs_gen = RaceInputsGen {};

c.bench_function("generate inputs for: race", |b| {
b.iter(|| sum_of_all_inputs(black_box(&positions), &inputs_gen))
});
}

criterion_group!(benches, contact_inputs, race_inputs);
criterion_main!(benches);

0 comments on commit db06af9

Please sign in to comment.