Skip to content

Commit

Permalink
Perf4 (#98)
Browse files Browse the repository at this point in the history
* Faster performance with matrix integration optimization

* Make pixel arenas use 32-bit floats. Remove lots of float conversions

* Reduce casts to usize

* Fix feature detection

* Fix white artifacts

* Remove unnecessary display frame cloning

* Run with opencv

* Maybe slightly faster without float log2

* Maybe slightly faster without float log2

* Make some asserts debug only

* New scheme for throwing away integrations of multi-nodes, for faster transcode speed

* Cleanup

* Fix artifacting

* Faster bevy image generation

* Cleanup

* Updates for tests

* Update run

* Cleanup

* Cleanup

* Cleanup dependency clippy

* Apply clippy fixes

* Lots more cleanup

* Fix tests
  • Loading branch information
ac-freeman authored Dec 28, 2023
1 parent a5d1947 commit b5c4fa4
Show file tree
Hide file tree
Showing 52 changed files with 1,387 additions and 1,314 deletions.
6 changes: 6 additions & 0 deletions .idea/adder-codec-rs.iml

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

2 changes: 1 addition & 1 deletion .idea/runConfigurations/Clippy.xml

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

2 changes: 1 addition & 1 deletion .idea/runConfigurations/Run_adder_viz_dynamic.xml

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

4 changes: 2 additions & 2 deletions .idea/runConfigurations/Run_simul_release.xml

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

2 changes: 1 addition & 1 deletion .idea/runConfigurations/Test_stable.xml

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ opt-level = 3
panic = "unwind"

[profile.dev.package."adder-codec-rs"]
opt-level = 2
opt-level = 3
overflow-checks = false
#panic = "abort"

Expand All @@ -27,7 +27,7 @@ overflow-checks = false


[profile.dev.package."*"]
opt-level = 1
opt-level = 3

[profile.dev]
opt-level = 1
Expand Down
2 changes: 1 addition & 1 deletion adder-codec-core/src/codec/compressed/fenwick/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Weights {
}

/// Initialize the weights with the given counts
pub fn new_with_counts(n: usize, counts: &Vec<u64>) -> Self {
pub fn new_with_counts(n: usize, counts: &[u64]) -> Self {
// we add one extra value here to account for the EOF (stored at the FIRST index)
let fenwick_counts = vec![0; n + 1];

Expand Down
31 changes: 18 additions & 13 deletions adder-codec-core/src/codec/compressed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub type TResidual = i16;

#[cfg(test)]
mod tests {
use crate::codec::compressed::stream;
use crate::codec::encoder::Encoder;
use crate::codec::{CodecMetadata, EncoderOptions};
use crate::{Coord, Event, PlaneSize};
Expand All @@ -29,12 +28,15 @@ mod tests {
};

let output = crate::codec::compressed::stream::CompressedOutput::new(meta, Vec::new());
let mut encoder = Encoder::new_compressed(output, EncoderOptions::default(PlaneSize{
width: 100,
height: 100,
channels: 1
}));
let meta = encoder.meta().clone();
let mut encoder = Encoder::new_compressed(
output,
EncoderOptions::default(PlaneSize {
width: 100,
height: 100,
channels: 1,
}),
);
let meta = *encoder.meta();
let mut test_event = Event {
coord: Coord {
x: 0,
Expand All @@ -54,12 +56,15 @@ mod tests {
assert_eq!(writer.len(), meta.header_size);

let output = crate::codec::compressed::stream::CompressedOutput::new(meta, Vec::new());
let mut encoder = Encoder::new_compressed(output, EncoderOptions::default(PlaneSize{
width: 100,
height: 100,
channels: 1
}));
let meta = encoder.meta().clone();
let mut encoder = Encoder::new_compressed(
output,
EncoderOptions::default(PlaneSize {
width: 100,
height: 100,
channels: 1,
}),
);
let meta = *encoder.meta();
encoder.ingest_event(test_event).unwrap();
test_event.t += 100;
encoder.ingest_event(test_event).unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
use crate::codec::compressed::fenwick::context_switching::FenwickModel;
use crate::codec::compressed::fenwick::Weights;
use crate::codec::compressed::TResidual;
use crate::codec::CodecMetadata;
use crate::{AbsoluteT, DeltaT, EventCoordless, Intensity, D, D_EMPTY, D_SHIFT};
use crate::{AbsoluteT, DeltaT, EventCoordless, Intensity, D, D_SHIFT};
use arithmetic_coding_adder_dep::Encoder;
use bitstream_io::{BigEndian, BitWrite, BitWriter};

pub struct Contexts {
/// Decimation factor residuals context
pub(crate) d_context: usize,

/// Dt_ref interval count residuals context (how many dt_ref intervals away is our predicted interval from the actual)
pub(crate) dtref_context: usize,

/// Timestamp residuals context
pub(crate) t_context: usize,

t_residual_max: i64,

dt_max: i64,

/// EOF context
pub(crate) eof_context: usize,

Expand All @@ -31,38 +24,30 @@ pub const D_RESIDUAL_OFFSET: i16 = 255;
pub const BITSHIFT_ENCODE_FULL: u8 = 15;

impl Contexts {
pub fn new(source_model: &mut FenwickModel, dt_ref: DeltaT, dt_max: DeltaT) -> Contexts {
pub fn new(source_model: &mut FenwickModel, dt_ref: DeltaT) -> Contexts {
let d_context = source_model.push_context_with_weights(d_residual_default_weights());
let dtref_context = source_model.push_context_with_weights(d_residual_default_weights());

// TODO: Configure this based on the delta_t_max parameter!!
let t_weights = t_residual_default_weights(dt_ref);
let t_residual_max = (t_weights.len() as i64 - 2) / 2;
let t_context = source_model.push_context_with_weights(t_weights);

let eof_context =
source_model.push_context_with_weights(Weights::new_with_counts(1, &vec![1]));
let eof_context = source_model.push_context_with_weights(Weights::new_with_counts(1, &[1]));
let bitshift_context =
source_model.push_context_with_weights(Weights::new_with_counts(16, &vec![1; 16]));
source_model.push_context_with_weights(Weights::new_with_counts(16, &[1; 16]));

Contexts {
d_context,
dtref_context,
t_context,
t_residual_max,
dt_max: dt_max as i64,
eof_context,
bitshift_context,
}
}

pub(crate) fn check_too_far(&self, reference_start_t: AbsoluteT, t: AbsoluteT) -> bool {
t < reference_start_t - self.dt_max as AbsoluteT
}

/// Find out how much we need to bitshift the t_residual to fit within the range of the model
pub(crate) fn residual_to_bitshift(&self, t_residual_i64: i64) -> (u8, i64) {
if t_residual_i64.abs() < self.t_residual_max as i64 {
if t_residual_i64.abs() < self.t_residual_max {
(0, t_residual_i64)
// } else if t_residual_i64.abs() > self.dt_max {
} else {
Expand Down Expand Up @@ -104,19 +89,13 @@ impl Contexts {
dt_ref: DeltaT,
c_thresh_max: f64,
) -> (u8, i64) {
if t_residual_i64.abs() < self.t_residual_max as i64 {
if t_residual_i64.abs() < self.t_residual_max {
(0, t_residual_i64)
// } else if t_residual_i64.abs() > self.dt_max {
}
// else if event.d == D_EMPTY {
// // JUST LOSSLESS FOR NOW
// (BITSHIFT_ENCODE_FULL, t_residual_i64)
// }
else {
} else {
let actual_dt = event.t - prev_event.t;
let actual_intensity = self.event_to_intensity(event.d, actual_dt, dt_ref);
let mut recon_intensity = actual_intensity;
let mut bitshift = 0;
let mut bitshift: u8 = 0;
let mut t_residual = t_residual_i64.abs();
loop {
if t_residual > self.t_residual_max
Expand All @@ -135,12 +114,10 @@ impl Contexts {
break;
}
}
if bitshift > 0 {
bitshift -= 1;
}
bitshift = bitshift.saturating_sub(1);
t_residual = t_residual_i64.abs() >> bitshift;

if t_residual.abs() < self.t_residual_max as i64 {
if t_residual.abs() < self.t_residual_max {
if t_residual_i64 < 0 {
(bitshift, -t_residual)
} else {
Expand All @@ -154,14 +131,13 @@ impl Contexts {
}
}

pub fn t_residual_default_weights(dt_ref: DeltaT) -> Weights {
pub fn t_residual_default_weights(_dt_ref: DeltaT) -> Weights {
// t residuals can fit within i16

// After we've indexed into the correct interval, our timestamp residual can span [-dt_ref, dt_ref]

// We have dt_max/dt_ref count of intervals per adu
let mut counts: Vec<u64> = vec![1; (u8::MAX as usize + 1)];
// let mut counts: Vec<u64> = vec![1; u16::MAX as usize];
let mut counts: Vec<u64> = vec![1; u8::MAX as usize + 1];

// Give higher probability to smaller residuals
// for i in counts.len() / 3..counts.len() * 2 / 3 {
Expand All @@ -172,8 +148,8 @@ pub fn t_residual_default_weights(dt_ref: DeltaT) -> Weights {
// counts[len / 2 - 1] = 10;
// counts[len / 2 + 1] = 10;
counts[0] = 100;
for i in 0..10 {
counts[i] = 10;
for item in counts.iter_mut().take(10) {
*item = 10;
}

Weights::new_with_counts(counts.len(), &counts)
Expand Down
Loading

0 comments on commit b5c4fa4

Please sign in to comment.