Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/reshape: Add evaluate_shape to reshape.rs to allow for variable #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions juice-examples/mackey-glass-rnn-regression/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn create_network(batch_size: usize, columns: usize) -> SequentialConfig {
// Reshape the input into NCHW Format
net_cfg.add_layer(LayerConfig::new(
"reshape",
LayerType::Reshape(ReshapeConfig::of_shape(&[batch_size, DATA_COLUMNS, 1, 1])),
LayerType::Reshape(ReshapeConfig::of_shape(&[-1, DATA_COLUMNS as isize, 1, 1])),
));

net_cfg.add_layer(LayerConfig::new(
Expand Down Expand Up @@ -168,7 +168,6 @@ pub(crate) fn train(
// Initialise a Sequential Layer
let net_cfg = create_network(batch_size, DATA_COLUMNS);
let mut solver = add_solver(backend, net_cfg, batch_size, learning_rate, momentum);

// Define Input & Labels
let input = SharedTensor::<f32>::new(&[batch_size, 1, DATA_COLUMNS]);
let input_lock = Arc::new(RwLock::new(input));
Expand Down Expand Up @@ -224,7 +223,8 @@ pub(crate) fn train(
pub(crate) fn test(backend: Rc<Backend<Cuda>>, batch_size: usize, file: &Path) -> Result<(), Box<dyn std::error::Error>> {
// Load in a pre-trained network
let mut network: Layer<Backend<Cuda>> = Layer::<Backend<Cuda>>::load(backend, file)?;

dbg!(&network);
panic!("End");
Comment on lines +226 to +227
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray dbg statement, this should not end up in master, not even in a test.

// Define Input & Labels
let input = SharedTensor::<f32>::new(&[batch_size, 1, DATA_COLUMNS]);
let input_lock = Arc::new(RwLock::new(input));
Expand Down Expand Up @@ -262,7 +262,7 @@ pub(crate) fn test(backend: Rc<Backend<Cuda>>, batch_size: usize, file: &Path) -
}

fn main() {
env_logger::builder().filter_level(log::LevelFilter::Info).init();
env_logger::builder().filter_level(log::LevelFilter::Trace).init();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info was picked intentionally to not flood first time users with lot's of overwhelming (=useless) info.

// Parse Arguments
let args: Args = docopt::Docopt::new(MAIN_USAGE)
.and_then(|d| d.deserialize())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn add_conv_net(
) -> SequentialConfig {
net_cfg.add_layer(LayerConfig::new(
"reshape",
ReshapeConfig::of_shape(&[batch_size, 1, pixel_dim, pixel_dim]),
ReshapeConfig::of_shape(&[batch_size as isize, 1, pixel_dim as isize, pixel_dim as isize]),
));
net_cfg.add_layer(LayerConfig::new(
"conv",
Expand Down Expand Up @@ -192,7 +192,7 @@ fn add_mlp(
) -> SequentialConfig {
net_cfg.add_layer(LayerConfig::new(
"reshape",
LayerType::Reshape(ReshapeConfig::of_shape(&[batch_size, pixel_count])),
LayerType::Reshape(ReshapeConfig::of_shape(&[batch_size as isize, pixel_count as isize])),
));
net_cfg.add_layer(LayerConfig::new(
"linear1",
Expand Down
1 change: 1 addition & 0 deletions juice/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ rand = "0.8"
num = "0.4"
capnp = "0.14"
timeit = "0.1"
anyhow = "1.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a library, thiserror is the preferred approach. Anyhow is for applications.


[build-dependencies]
capnpc = "0.14"
Expand Down
2 changes: 1 addition & 1 deletion juice/capnp/juice.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ struct NegativeLogLikelihoodConfig {
}

struct ReshapeConfig {
shape @0 :List(UInt64);
shape @0 :List(Int64);
}
69 changes: 59 additions & 10 deletions juice/src/layers/utility/reshape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
//! - `W` : width

use crate::capnp_util::*;
use crate::co::{IBackend, SharedTensor};
use crate::co::{IBackend, SharedTensor, TensorDesc};
use crate::juice_capnp::reshape_config as capnp_config;
use crate::layer::*;
use crate::util::ArcLock;
use anyhow::{anyhow, Result};

#[derive(Debug, Clone)]
/// Reshape Utility Layer
pub struct Reshape {
shape: Vec<usize>,
shape: Vec<isize>,
}

impl Reshape {
Expand All @@ -35,6 +36,38 @@ impl Reshape {
shape: config.shape.clone(),
}
}

fn evaluate_shape(&self, input_shape: &TensorDesc) -> Result<Vec<usize>> {
dbg!(&self.shape);
dbg!(input_shape);
let unknown_dimensions: usize = self.shape.iter().filter(|x| **x == -1).count();
let invalid_dimensions: usize = self.shape.iter().filter(|x| **x < -1).count();
if invalid_dimensions > 0 {
return Err(anyhow!("Invalid elements provided to Reshape"))
}
return match unknown_dimensions {
0 => Ok(self.shape.clone().into_iter().map(|x| x as usize).collect()),
1 => {
let total_prior_elements: usize = input_shape.iter().product();
let known_elements: usize = self.shape.iter().filter(|x| **x > -1).product::<isize>() as usize;
dbg!(total_prior_elements);
dbg!(known_elements);
if total_prior_elements != (total_prior_elements / known_elements * known_elements) {
Err(anyhow!(
"Dimensions {:?} do not cleanly reshape into {:?}",
input_shape, self.shape
))
} else {
let unknown_element: usize = total_prior_elements / known_elements;
Ok(self.shape
.iter()
.map(|x| if *x == -1 { unknown_element } else { *x as usize })
.collect())
}
}
_ => Err(anyhow!("More than 2 unknown elements provided to Reshape")),
}
}
}
Comment on lines +40 to 71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is in need of bunch of tests :) and also has stray dbg! statements and requires some love for the Err types (or the lack of those).


impl<B: IBackend> ILayer<B> for Reshape {
Expand All @@ -56,8 +89,19 @@ impl<B: IBackend> ILayer<B> for Reshape {
output_data: &mut Vec<ArcLock<SharedTensor<f32>>>,
output_gradient: &mut Vec<ArcLock<SharedTensor<f32>>>,
) {
output_data[0].write().unwrap().resize(&self.shape).unwrap();
output_gradient[0].write().unwrap().resize(&self.shape).unwrap();
// Shape Evaluation has to be done at run-time.
if !input_data.is_empty() {
let output_shape : Vec<usize> = match input_data[0].read() {
Ok(tensor) => self.evaluate_shape(tensor.desc()).unwrap(),
Err(E) => panic!("")
};
output_data[0].write().unwrap().resize(&output_shape).unwrap();
let output_grad_shape : Vec<usize> = match input_gradient[0].read() {
Ok(tensor) => self.evaluate_shape(tensor.desc()).unwrap(),
Err(E) => panic!("")
};
output_gradient[0].write().unwrap().resize(&output_grad_shape).unwrap();
}
}
}

Expand Down Expand Up @@ -92,15 +136,20 @@ impl<B: IBackend> ComputeParametersGradient<f32, B> for Reshape {}
pub struct ReshapeConfig {
/// The target shape that the input should assume.
///
/// Preceding dimensions are treated as independent inputs
/// Preceding dimensions are treated as independent inputs. At most one value can be -1,
/// indicating that the size of that element should be the remaining element dimensions, i.e.
/// Input [2,8] -> Reshape [-1, 4] -> Output [4, 4]
/// As the input has 16 elements, 16 / 4 is 4, so the output is [4, 4]
///
/// Causes an error if the total elements are incompatible with the dimensions selected.
///
/// Defaults to `1`
pub shape: Vec<usize>,
pub shape: Vec<isize>,
}

impl ReshapeConfig {
/// Create a ReshapeConfig that describes a Reshape layer with a provided shape.
pub fn of_shape(shape: &[usize]) -> ReshapeConfig {
pub fn of_shape(shape: &[isize]) -> ReshapeConfig {
ReshapeConfig {
shape: shape.to_owned(),
}
Expand All @@ -114,7 +163,7 @@ impl<'a> CapnpWrite<'a> for ReshapeConfig {
fn write_capnp(&self, builder: &mut Self::Builder) {
let mut shape = builder.reborrow().init_shape(self.shape.len() as u32);
for (i, dim) in self.shape.iter().enumerate() {
shape.set(i as u32, *dim as u64);
shape.set(i as u32, *dim as i64);
}
}
}
Expand All @@ -124,9 +173,9 @@ impl<'a> CapnpRead<'a> for ReshapeConfig {

fn read_capnp(reader: Self::Reader) -> Self {
let read_shape = reader.get_shape().unwrap();
let mut shape = Vec::new();
let mut shape: Vec<isize> = Vec::new();
for i in 0..read_shape.len() {
shape.push(read_shape.get(i) as usize)
shape.push(read_shape.get(i) as isize)
}

ReshapeConfig { shape: shape }
Expand Down