Skip to content

Commit

Permalink
Merge pull request #121 from outbrain/correct-annotations
Browse files Browse the repository at this point in the history
Correct annotations
  • Loading branch information
ggaspersic authored Nov 2, 2023
2 parents d433efa + 6e006f7 commit 34519fa
Show file tree
Hide file tree
Showing 25 changed files with 145 additions and 316 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ rustc-hash = "1.1.0"
cbindgen = "0.23.0"

[lib]
crate_type = ["cdylib"]
name = "fw"
path="src/lib.rs"
crate_type = ["lib", "cdylib"]
doctest = false

[[bin]]
name="fw"
path="src/main.rs"

[dev-dependencies]
tempfile = "3.1.0"
mockstream = "0.0.3"
Expand Down
6 changes: 2 additions & 4 deletions src/block_ffm.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#![allow(dead_code,unused_imports, unused_mut, invalid_value)]
#![allow(invalid_value, unused_mut)]

use core::arch::x86_64::*;
use rustc_hash::FxHashSet;
use std::any::Any;
use std::error::Error;
use std::mem::{self, MaybeUninit};
use std::ops::Bound::Included;
use std::sync::Mutex;
use std::time::Instant;
use std::{io, ptr};

use merand48::*;
Expand All @@ -28,7 +26,6 @@ use crate::regressor;
use crate::regressor::{BlockCache, FFM_CONTRA_BUF_LEN};

const FFM_STACK_BUF_LEN: usize = 131072;
const FFM_CONTRA_CACHE_BUF_LEN: usize = 1024;
const STEP: usize = 4;
const ZEROES: [f32; STEP] = [0.0; STEP];

Expand Down Expand Up @@ -1179,6 +1176,7 @@ impl<L: OptimizerTrait + 'static> BlockFFM<L> {
}
}

#[cfg(test)]
mod tests {
use block_helpers::{slearn2, spredict2, spredict2_with_cache};

Expand Down
10 changes: 7 additions & 3 deletions src/block_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#![allow(dead_code,unused_imports)]

use crate::optimizer::OptimizerTrait;
use std::error::Error;
use std::io;
use std::io::Read;

use crate::feature_buffer;
use crate::graph;
use crate::optimizer::OptimizerSGD;
use crate::port_buffer;
use crate::regressor::{BlockCache, BlockTrait};
use std::cmp::min;
use std::mem;
use std::slice;

#[cfg(test)]
use crate::graph;

#[derive(Clone, Debug)]
#[repr(C)]
pub struct OptimizerData<L: OptimizerTrait> {
Expand Down Expand Up @@ -156,6 +156,7 @@ pub fn get_input_output_borrows(
}
}

#[cfg(test)]
pub fn slearn2(
bg: &mut graph::BlockGraph,
fb: &feature_buffer::FeatureBuffer,
Expand All @@ -169,6 +170,7 @@ pub fn slearn2(
pb.observations[0]
}

#[cfg(test)]
pub fn ssetup_cache2(
bg: &mut graph::BlockGraph,
cache_fb: &feature_buffer::FeatureBuffer,
Expand All @@ -185,6 +187,7 @@ pub fn ssetup_cache2(
);
}

#[cfg(test)]
pub fn spredict2_with_cache(
bg: &mut graph::BlockGraph,
fb: &feature_buffer::FeatureBuffer,
Expand All @@ -198,6 +201,7 @@ pub fn spredict2_with_cache(
pb.observations[0]
}

#[cfg(test)]
pub fn spredict2(
bg: &mut graph::BlockGraph,
fb: &feature_buffer::FeatureBuffer,
Expand Down
3 changes: 1 addition & 2 deletions src/block_misc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code,unused_imports)]

use std::any::Any;
use std::error::Error;

Expand Down Expand Up @@ -885,6 +883,7 @@ impl BlockTriangle {
}
}

#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
Expand Down
4 changes: 1 addition & 3 deletions src/block_neural.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code,unused_imports)]

use rand_distr::{Distribution, Normal, Uniform};
use rand_xoshiro::rand_core::SeedableRng;
use rand_xoshiro::Xoshiro256PlusPlus;
Expand All @@ -8,7 +6,6 @@ use std::error::Error;
use std::io;
use std::io::Error as IOError;
use std::io::ErrorKind;
use std::mem::MaybeUninit;

use crate::block_helpers;
use crate::block_misc;
Expand Down Expand Up @@ -481,6 +478,7 @@ impl<L: OptimizerTrait + 'static> BlockTrait for BlockNeuronLayer<L> {
}
}

#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
Expand Down
62 changes: 0 additions & 62 deletions src/block_normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,65 +264,3 @@ impl BlockStopBackward {
);
}
}

/*
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
use crate::block_misc;
use crate::feature_buffer;
use crate::feature_buffer::HashAndValueAndSeq;
use crate::vwmap;
use block_helpers::{slearn2, spredict2};
use block_misc::{Observe};
use crate::assert_epsilon;
fn fb_vec() -> feature_buffer::FeatureBuffer {
feature_buffer::FeatureBuffer {
label: 0.0,
example_importance: 1.0,
example_number: 0,
lr_buffer: Vec::new(),
ffm_buffer: Vec::new(),
}
}
#[test]
fn test_simple_positive() {
let mut mi = model_instance::ModelInstance::new_empty().unwrap();
let mut bg = BlockGraph::new();
let input_block = block_misc::new_const_block(&mut bg, vec![2.0]).unwrap();
let relu_block = new_relu_block(&mut bg, &mi, input_block).unwrap();
let observe_block = block_misc::new_observe_block(&mut bg, relu_block, Observe::Forward, Some(1.0)).unwrap();
bg.finalize();
bg.allocate_and_init_weights(&mi);
let mut pb = bg.new_port_buffer();
let fb = fb_vec();
assert_epsilon!(slearn2 (&mut bg, &fb, &mut pb, true), 2.0);
assert_epsilon!(slearn2 (&mut bg, &fb, &mut pb, true), 2.0); // relu desnt learn
}
fn test_simple_negative() {
let mut mi = model_instance::ModelInstance::new_empty().unwrap();
let mut bg = BlockGraph::new();
let input_block = block_misc::new_const_block(&mut bg, vec![-2.0]).unwrap();
let relu_block = new_relu_block(&mut bg, &mi, input_block).unwrap();
let observe_block = block_misc::new_observe_block(&mut bg, relu_block, Observe::Forward, Some(1.0)).unwrap();
bg.finalize();
bg.allocate_and_init_weights(&mi);
let mut pb = bg.new_port_buffer();
let fb = fb_vec();
assert_epsilon!(slearn2 (&mut bg, &fb, &mut pb, true), 0.0);
assert_epsilon!(slearn2 (&mut bg, &fb, &mut pb, true), 0.0); // relu desnt learn
}
}
*/
18 changes: 8 additions & 10 deletions src/block_relu.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#![allow(dead_code,unused_imports)]

use std::any::Any;
use std::error::Error;

use crate::block_helpers;
use crate::feature_buffer;
use crate::feature_buffer::FeatureBuffer;
use crate::graph;
use crate::graph::BlockGraph;
use crate::graph::{BlockGraph, BlockPtrOutput, InputSlot, OutputSlot};
use crate::model_instance;
use crate::port_buffer;
use crate::port_buffer::PortBuffer;
Expand All @@ -22,10 +19,10 @@ pub struct BlockRELU {
}

pub fn new_relu_block(
bg: &mut graph::BlockGraph,
bg: &mut BlockGraph,
_mi: &model_instance::ModelInstance,
input: graph::BlockPtrOutput,
) -> Result<graph::BlockPtrOutput, Box<dyn Error>> {
input: BlockPtrOutput,
) -> Result<BlockPtrOutput, Box<dyn Error>> {
let num_inputs = bg.get_num_output_values(vec![&input]);
assert_ne!(num_inputs, 0);
let block = Box::new(BlockRELU {
Expand Down Expand Up @@ -63,17 +60,17 @@ impl BlockTrait for BlockRELU {
self
}

fn get_num_output_values(&self, output: graph::OutputSlot) -> usize {
fn get_num_output_values(&self, output: OutputSlot) -> usize {
assert_eq!(output.get_output_index(), 0);
self.num_inputs
}

fn set_input_offset(&mut self, input: graph::InputSlot, offset: usize) {
fn set_input_offset(&mut self, input: InputSlot, offset: usize) {
assert_eq!(input.get_input_index(), 0);
self.input_offset = offset;
}

fn set_output_offset(&mut self, output: graph::OutputSlot, offset: usize) {
fn set_output_offset(&mut self, output: OutputSlot, offset: usize) {
assert_eq!(output.get_output_index(), 0);
self.output_offset = offset;
}
Expand Down Expand Up @@ -135,6 +132,7 @@ impl BlockTrait for BlockRELU {
}
}

#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
Expand Down
2 changes: 0 additions & 2 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code,unused_imports)]

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::error::Error;
use std::fs;
Expand Down
2 changes: 0 additions & 2 deletions src/cmdline.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code,unused_imports)]

use crate::version;
use clap::{App, AppSettings, Arg};

Expand Down
3 changes: 1 addition & 2 deletions src/feature_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code,unused_imports)]

use crate::feature_transform_executor;
use crate::model_instance;
use crate::parser;
Expand Down Expand Up @@ -340,6 +338,7 @@ impl FeatureBufferTranslator {
}
}

#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
Expand Down
3 changes: 1 addition & 2 deletions src/feature_transform_executor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code,unused_imports)]

use crate::parser;
use crate::vwmap;
use std::error::Error;
Expand Down Expand Up @@ -243,6 +241,7 @@ pub trait FunctionExecutorTrait: DynClone + Send {
}
clone_trait_object!(FunctionExecutorTrait);

#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
Expand Down
58 changes: 2 additions & 56 deletions src/feature_transform_implementations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code,unused_imports)]

use std::error::Error;
use std::io::Error as IOError;
use std::io::ErrorKind;
Expand All @@ -15,59 +13,6 @@ use crate::feature_transform_executor::{
use crate::feature_transform_parser;
use crate::vwmap::{NamespaceDescriptor, NamespaceFormat, NamespaceType};

// Basic example of a "full blown" simple FunctionExecutorTrait
#[derive(Clone)]
struct FunctionExampleSqrt {
from_namespace: ExecutorFromNamespace,
}

impl FunctionExecutorTrait for FunctionExampleSqrt {
fn execute_function(
&self,
record_buffer: &[u32],
to_namespace: &mut ExecutorToNamespace,
_transform_executors: &TransformExecutors,
) {
feature_reader_float_namespace!(
record_buffer,
self.from_namespace.namespace_descriptor,
_hash_index,
hash_value,
float_value,
{
let transformed_float = float_value.sqrt();
let transformed_int = transformed_float as i32;
to_namespace
.emit_i32::<{ SeedNumber::Default as usize }>(transformed_int, hash_value);
}
);
}
}

impl FunctionExampleSqrt {
fn create_function(
_function_name: &str,
from_namespaces: &Vec<feature_transform_parser::Namespace>,
function_params: &Vec<f32>,
) -> Result<Box<dyn FunctionExecutorTrait>, Box<dyn Error>> {
// For simplicity of example, we just assert instead of full error reporting
assert!(function_params.is_empty());
assert_eq!(from_namespaces.len(), 1);
assert_eq!(
from_namespaces[0].namespace_descriptor.namespace_type,
NamespaceType::Primitive
);
assert_eq!(
from_namespaces[0].namespace_descriptor.namespace_format,
NamespaceFormat::F32
);
Ok(Box::new(Self {
from_namespace: ExecutorFromNamespace {
namespace_descriptor: from_namespaces[0].namespace_descriptor,
},
}))
}
}

// -------------------------------------------------------------------
// TransformerBinner - A basic binner
Expand Down Expand Up @@ -588,6 +533,7 @@ impl TransformerCombine {
}
}

#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
Expand Down Expand Up @@ -622,7 +568,7 @@ mod tests {
namespace_descriptor: ns_desc(0),
};

let to_namespace_empty = ExecutorToNamespace {
let _to_namespace_empty = ExecutorToNamespace {
namespace_descriptor: ns_desc(1),
namespace_seeds: default_seeds(1), // These are precomputed namespace seeds
tmp_data: Vec::new(),
Expand Down
Loading

0 comments on commit 34519fa

Please sign in to comment.