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

Cleanup &vec![..] #86

Open
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["deep-learning", "neural-networks", "machine-learning", "framework"]
license = "MIT OR Apache-2.0"

[dependencies]
collenchyma = { version = "0.0.8", default-features = false, features = ["native"] } # native feature to read/write data into tensors
collenchyma = { version = "0.0.9", default-features = false, features = ["native"] } # native feature to read/write data into tensors
collenchyma-blas = { version = "0.2.0", default-features = false, features = ["native"] } # only compiles with native feature
collenchyma-nn = { version = "0.3.2", default-features = false }

Expand Down
48 changes: 19 additions & 29 deletions benches/network_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ mod cuda {
use std::sync::{Arc, RwLock};
use leaf::layers::*;
use leaf::layer::*;
use leaf::network::*;
use std::rc::Rc;

#[cfg(feature = "native")]
fn native_backend() -> Rc<Backend<Native>> {
Rc::new(Backend::<Native>::default().unwrap())
}

#[cfg(feature = "cuda")]
fn cuda_backend() -> Rc<Backend<Cuda>> {
Rc::new(Backend::<Cuda>::default().unwrap())
Expand Down Expand Up @@ -76,10 +70,10 @@ mod cuda {
#[ignore]
#[cfg(feature = "cuda")]
fn bench_mnsit_forward_1(b: &mut Bencher) {
let mut cfg = NetworkConfig::default();
let mut cfg = SequentialConfig::default();
// set up input
cfg.add_input("in", &vec![1, 30, 30]);
cfg.add_input("label", &vec![1, 1, 10]);
cfg.add_input("in", &[1, 30, 30]);
cfg.add_input("label", &[1, 1, 10]);
// set up sigmoid
let mut sig_cfg = LayerConfig::new("sig", LayerType::Sigmoid);
sig_cfg.add_input("in");
Expand All @@ -98,18 +92,14 @@ mod cuda {
// cfg.add_layer(loss_cfg);

let backend = cuda_backend();
let native_backend = native_backend();
let mut network = Network::from_config(backend.clone(), &cfg);
let loss = &mut 0f32;
let mut network = Layer::from_config(
backend.clone(), &LayerConfig::new("network", LayerType::Sequential(cfg)));

let _ = timeit_loops!(10, {
let inp = SharedTensor::<f32>::new(backend.device(), &vec![1, 30, 30]).unwrap();
let label = SharedTensor::<f32>::new(native_backend.device(), &vec![1, 1, 10]).unwrap();

let inp = SharedTensor::<f32>::new(backend.device(), &[1, 30, 30]).unwrap();
let inp_lock = Arc::new(RwLock::new(inp));
let label_lock = Arc::new(RwLock::new(label));

network.forward(&[inp_lock, label_lock], loss);
network.forward(&[inp_lock]);
});
// b.iter(|| {
// for _ in 0..1 {
Expand All @@ -128,9 +118,9 @@ mod cuda {
// #[ignore]
#[cfg(feature = "cuda")]
fn alexnet_forward(b: &mut Bencher) {
let mut cfg = NetworkConfig::default();
let mut cfg = SequentialConfig::default();
// Layer: data
cfg.add_input("data", &vec![128, 3, 224, 224]);
cfg.add_input("data", &[128, 3, 224, 224]);
// Layer: conv1
let conv1_layer_cfg = ConvolutionConfig {
num_output: 64,
Expand Down Expand Up @@ -265,15 +255,15 @@ mod cuda {

let backend = cuda_backend();
// let native_backend = native_backend();
let mut network = Network::from_config(backend.clone(), &cfg);
let mut network = Layer::from_config(
backend.clone(), &LayerConfig::new("network", LayerType::Sequential(cfg)));

let func = || {
let forward_time = timeit_loops!(1, {
let loss = &mut 0f32;
let inp = SharedTensor::<f32>::new(backend.device(), &vec![128, 3, 112, 112]).unwrap();
let inp = SharedTensor::<f32>::new(backend.device(), &[128, 3, 112, 112]).unwrap();

let inp_lock = Arc::new(RwLock::new(inp));
network.forward(&[inp_lock], loss);
network.forward(&[inp_lock]);
});
println!("Forward step: {}", forward_time);
};
Expand All @@ -285,9 +275,9 @@ mod cuda {
#[cfg(feature = "cuda")]
fn small_alexnet_forward(b: &mut Bencher) {
// let _ = env_logger::init();
let mut cfg = NetworkConfig::default();
let mut cfg = SequentialConfig::default();
// Layer: data
cfg.add_input("data", &vec![128, 3, 112, 112]);
cfg.add_input("data", &[128, 3, 112, 112]);
// Layer: conv1
let conv1_layer_cfg = ConvolutionConfig {
num_output: 32,
Expand Down Expand Up @@ -422,14 +412,14 @@ mod cuda {

let backend = cuda_backend();
// let native_backend = native_backend();
let mut network = Network::from_config(backend.clone(), &cfg);
let mut network = Layer::from_config(
backend.clone(), &LayerConfig::new("network", LayerType::Sequential(cfg)));

let mut func = || {
let loss = &mut 0f32;
let inp = SharedTensor::<f32>::new(backend.device(), &vec![128, 3, 112, 112]).unwrap();
let inp = SharedTensor::<f32>::new(backend.device(), &[128, 3, 112, 112]).unwrap();

let inp_lock = Arc::new(RwLock::new(inp));
network.forward(&[inp_lock], loss);
network.forward(&[inp_lock]);
};
{ func(); bench_profile(b, func, 10); }
}
Expand Down
12 changes: 6 additions & 6 deletions examples/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn bench_alexnet() {
#[cfg(all(feature="cuda", not(feature="native")))]
fn bench_alexnet() {
let mut cfg = SequentialConfig::default();
cfg.add_input("data", &vec![128, 3, 224, 224]);
cfg.add_input("data", &[128, 3, 224, 224]);

let conv1_layer_cfg = ConvolutionConfig { num_output: 64, filter_shape: vec![11], padding: vec![2], stride: vec![4] };
cfg.add_layer(LayerConfig::new("conv1", conv1_layer_cfg));
Expand Down Expand Up @@ -160,7 +160,7 @@ fn bench_alexnet() {
let func = || {
let forward_time = timeit_loops!(1, {
{
let inp = SharedTensor::<f32>::new(backend.device(), &vec![128, 3, 224, 224]).unwrap();
let inp = SharedTensor::<f32>::new(backend.device(), &[128, 3, 224, 224]).unwrap();

let inp_lock = Arc::new(RwLock::new(inp));
network.forward(&[inp_lock.clone()]);
Expand Down Expand Up @@ -202,7 +202,7 @@ fn bench_overfeat() {
#[cfg(all(feature="cuda", not(feature="native")))]
fn bench_overfeat() {
let mut cfg = SequentialConfig::default();
cfg.add_input("data", &vec![128, 3, 231, 231]);
cfg.add_input("data", &[128, 3, 231, 231]);

let conv1_layer_cfg = ConvolutionConfig { num_output: 96, filter_shape: vec![11], padding: vec![0], stride: vec![4] };
cfg.add_layer(LayerConfig::new("conv1", conv1_layer_cfg));
Expand Down Expand Up @@ -242,7 +242,7 @@ fn bench_overfeat() {
let func = || {
let forward_time = timeit_loops!(1, {
{
let inp = SharedTensor::<f32>::new(backend.device(), &vec![128, 3, 231, 231]).unwrap();
let inp = SharedTensor::<f32>::new(backend.device(), &[128, 3, 231, 231]).unwrap();

let inp_lock = Arc::new(RwLock::new(inp));
network.forward(&[inp_lock.clone()]);
Expand Down Expand Up @@ -284,7 +284,7 @@ fn bench_vgg_a() {
#[cfg(all(feature="cuda", not(feature="native")))]
fn bench_vgg_a() {
let mut cfg = SequentialConfig::default();
cfg.add_input("data", &vec![64, 3, 224, 224]);
cfg.add_input("data", &[64, 3, 224, 224]);

let conv1_layer_cfg = ConvolutionConfig { num_output: 64, filter_shape: vec![3], padding: vec![1], stride: vec![1] };
cfg.add_layer(LayerConfig::new("conv1", conv1_layer_cfg));
Expand Down Expand Up @@ -339,7 +339,7 @@ fn bench_vgg_a() {
let func = || {
let forward_time = timeit_loops!(1, {
{
let inp = SharedTensor::<f32>::new(backend.device(), &vec![64, 3, 224, 224]).unwrap();
let inp = SharedTensor::<f32>::new(backend.device(), &[64, 3, 224, 224]).unwrap();

let inp_lock = Arc::new(RwLock::new(inp));
network.forward(&[inp_lock.clone()]);
Expand Down
38 changes: 19 additions & 19 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ impl<B: IBackend> Layer<B> {
}

let backend: Rc<IBackend<F=B::F>> = self.backend.clone();
blob_data = Arc::new(RwLock::new(SharedTensor::new(backend.device(), &vec![1,1,1]).unwrap())); // [1,1,1] for CUDA
blob_gradient = Arc::new(RwLock::new(SharedTensor::new(backend.device(), &vec![1,1,1]).unwrap())); // [1,1,1] for CUDA
blob_data = Arc::new(RwLock::new(SharedTensor::new(backend.device(), &[1,1,1]).unwrap())); // [1,1,1] for CUDA
blob_gradient = Arc::new(RwLock::new(SharedTensor::new(backend.device(), &[1,1,1]).unwrap())); // [1,1,1] for CUDA
}
self.output_blob_names.push(blob_name.clone());
self.output_blobs_data.push(blob_data.clone());
Expand All @@ -227,8 +227,8 @@ impl<B: IBackend> Layer<B> {
info!("{} -> {}", self.name, blob_name);

let backend: Rc<IBackend<F=B::F>> = self.backend.clone();
let output_data = Arc::new(RwLock::new(SharedTensor::new(backend.device(), &vec![1,1,1]).unwrap())); // [1,1,1] for CUDA
let output_gradient = Arc::new(RwLock::new(SharedTensor::new(backend.device(), &vec![1,1,1]).unwrap())); // [1,1,1] for CUDA
let output_data = Arc::new(RwLock::new(SharedTensor::new(backend.device(), &[1,1,1]).unwrap())); // [1,1,1] for CUDA
let output_gradient = Arc::new(RwLock::new(SharedTensor::new(backend.device(), &[1,1,1]).unwrap())); // [1,1,1] for CUDA
self.output_blobs_data.push(output_data);
self.output_blobs_gradient.push(output_gradient);
}
Expand Down Expand Up @@ -460,7 +460,7 @@ impl<B: IBackend> Layer<B> {

let forward_time = timeit_loops!(1, {
if self.is_using_in_place() {
self.worker.forward(&self.backend, &vec![], &self.weights_data, &mut self.output_blobs_data);
self.worker.forward(&self.backend, &[], &self.weights_data, &mut self.output_blobs_data);
} else {
self.worker.forward(&self.backend, &self.input_blobs_data, &self.weights_data, &mut self.output_blobs_data);
}
Expand Down Expand Up @@ -498,8 +498,8 @@ impl<B: IBackend> Layer<B> {
if self.is_using_in_place() {
self.worker.backward_input(&self.backend,
&self.weights_data,
&vec![],
&vec![],
&[],
&[],
&self.input_blobs_data,
&mut self.input_blobs_gradient)
} else {
Expand Down Expand Up @@ -756,15 +756,15 @@ pub trait ILayer<B: IBackend> : ComputeOutput<f32, B> + ComputeInputGradient<f32
output_data: &mut [ArcLock<SharedTensor<f32>>]) {
// aquire all the locks
let inp: Vec<_> = input_data.iter().map(|b| b.read().unwrap()).collect();
let input_data_: Vec<&SharedTensor<f32>> = inp.iter().enumerate().map(|(_, val)| &**val).collect();
let input_data_: Vec<&SharedTensor<f32>> = inp.iter().map(|val| &**val).collect();

let wgts: Vec<_> = weights_data.iter().map(|w| w.read().unwrap()).collect();
let weights_data_: Vec<&SharedTensor<f32>> = wgts.iter().enumerate().map(|(_, val)| &**val).collect();
let weights_data_: Vec<&SharedTensor<f32>> = wgts.iter().map(|val| &**val).collect();

let out_ref = output_data.iter().cloned().collect::<Vec<_>>();
let mut out = &mut out_ref.iter().map(|b| b.write().unwrap()).collect::<Vec<_>>();
let mut output_w = &mut out.iter_mut().map(|a| a).collect::<Vec<_>>();
let mut output_data_: Vec<&mut SharedTensor<f32>> = output_w.iter_mut().enumerate().map(|(_, val)| &mut ***val).collect();
let mut output_data_: Vec<&mut SharedTensor<f32>> = output_w.iter_mut().map(|val| &mut ***val).collect();

self.compute_output(backend, &weights_data_, &input_data_, &mut output_data_);
}
Expand All @@ -785,17 +785,17 @@ pub trait ILayer<B: IBackend> : ComputeOutput<f32, B> + ComputeInputGradient<f32
input_data: &[ArcLock<SharedTensor<f32>>],
input_gradients: &mut [ArcLock<SharedTensor<f32>>]) {
let wgts_data: Vec<_> = weights_data.iter().map(|b| b.read().unwrap()).collect();
let weights_data_: Vec<&SharedTensor<f32>> = wgts_data.iter().enumerate().map(|(_, val)| &**val).collect();
let weights_data_: Vec<&SharedTensor<f32>> = wgts_data.iter().map(|val| &**val).collect();
let out_data: Vec<_> = output_data.iter().map(|b| b.read().unwrap()).collect();
let output_data_: Vec<&SharedTensor<f32>> = out_data.iter().enumerate().map(|(_, val)| &**val).collect();
let output_data_: Vec<&SharedTensor<f32>> = out_data.iter().map(|val| &**val).collect();
let out_gradient: Vec<_> = output_gradients.iter().map(|b| b.read().unwrap()).collect();
let output_gradients_: Vec<&SharedTensor<f32>> = out_gradient.iter().enumerate().map(|(_, val)| &**val).collect();
let output_gradients_: Vec<&SharedTensor<f32>> = out_gradient.iter().map(|val| &**val).collect();
let inp_data: Vec<_> = input_data.iter().map(|b| b.read().unwrap()).collect();
let input_data_: Vec<&SharedTensor<f32>> = inp_data.iter().enumerate().map(|(_, val)| &**val).collect();
let input_data_: Vec<&SharedTensor<f32>> = inp_data.iter().map(|val| &**val).collect();
let btm_gradient_ref = input_gradients.iter().cloned().collect::<Vec<_>>();
let mut btm_gradient = &mut btm_gradient_ref.iter().map(|b| b.write().unwrap()).collect::<Vec<_>>();
let mut input_gradient = &mut btm_gradient.iter_mut().map(|a| a).collect::<Vec<_>>();
let mut input_gradients_: Vec<&mut SharedTensor<f32>> = input_gradient.iter_mut().enumerate().map(|(_, val)| &mut ***val).collect();
let mut input_gradients_: Vec<&mut SharedTensor<f32>> = input_gradient.iter_mut().map(|val| &mut ***val).collect();

self.compute_input_gradient(backend, &weights_data_, &output_data_, &output_gradients_, &input_data_, &mut input_gradients_);
}
Expand All @@ -815,15 +815,15 @@ pub trait ILayer<B: IBackend> : ComputeOutput<f32, B> + ComputeInputGradient<f32
input_data: &[ArcLock<SharedTensor<f32>>],
weights_gradients: &mut [ArcLock<SharedTensor<f32>>]) {
let out_data: Vec<_> = output_data.iter().map(|b| b.read().unwrap()).collect();
let output_data_: Vec<&SharedTensor<f32>> = out_data.iter().enumerate().map(|(_, val)| &**val).collect();
let output_data_: Vec<&SharedTensor<f32>> = out_data.iter().map(|val| &**val).collect();
let out_gradients: Vec<_> = output_gradients.iter().map(|b| b.read().unwrap()).collect();
let output_gradients_: Vec<&SharedTensor<f32>> = out_gradients.iter().enumerate().map(|(_, val)| &**val).collect();
let output_gradients_: Vec<&SharedTensor<f32>> = out_gradients.iter().map(|val| &**val).collect();
let inp_data: Vec<_> = input_data.iter().map(|b| b.read().unwrap()).collect();
let input_data_: Vec<&SharedTensor<f32>> = inp_data.iter().enumerate().map(|(_, val)| &**val).collect();
let input_data_: Vec<&SharedTensor<f32>> = inp_data.iter().map(|val| &**val).collect();
let wgt_gradient_ref = weights_gradients.iter().cloned().collect::<Vec<_>>();
let mut wgt_gradient = &mut wgt_gradient_ref.iter().map(|b| b.write().unwrap()).collect::<Vec<_>>();
let mut weights_gradient = &mut wgt_gradient.iter_mut().map(|a| a).collect::<Vec<_>>();
let mut weights_gradients_: Vec<&mut SharedTensor<f32>> = weights_gradient.iter_mut().enumerate().map(|(_, val)| &mut ***val).collect();
let mut weights_gradients_: Vec<&mut SharedTensor<f32>> = weights_gradient.iter_mut().map(|val| &mut ***val).collect();

self.compute_parameters_gradient(backend, &output_data_, &output_gradients_, &input_data_, &mut weights_gradients_);
}
Expand Down
6 changes: 3 additions & 3 deletions src/layers/common/convolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ mod tests {
stride: vec![4],
};
let layer = Convolution::<Backend<Cuda>>::from_config(&cfg);
let num_spatial_dims = layer.num_spatial_dims(&vec![1, 3, 224, 224]);
let num_spatial_dims = layer.num_spatial_dims(&[1, 3, 224, 224]);
assert_eq!(2, num_spatial_dims);
assert_eq!(vec![11, 11], layer.spatial_filter_dims(2));
assert_eq!(vec![2, 2], layer.padding_dims(2));
assert_eq!(vec![4, 4], layer.stride_dims(2));
assert_eq!(vec![64, 3, 11, 11], layer.calculate_filter_shape(&vec![1, 3, 224, 224]));
assert_eq!(vec![1, 64, 55, 55], layer.calculate_output_shape(&vec![1, 3, 224, 224]));
assert_eq!(vec![64, 3, 11, 11], layer.calculate_filter_shape(&[1, 3, 224, 224]));
assert_eq!(vec![1, 64, 55, 55], layer.calculate_output_shape(&[1, 3, 224, 224]));
}
}
2 changes: 1 addition & 1 deletion src/layers/common/sequential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<B: IBackend + LayerOps<f32> + 'static> Sequential<B> {
pub fn from_config(backend: Rc<B>, config: &SequentialConfig) -> Sequential<B> {
let mut layer = Self::empty();

layer.init_layers(backend, &config.clone());
layer.init_layers(backend, config);

layer
}
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn write_batch_sample<T: NumCast + ::std::marker::Copy>(tensor: &mut SharedT
/// Create a Collenchyma SharedTensor for a scalar value.
pub fn native_scalar<T: NumCast + ::std::marker::Copy>(scalar: T) -> SharedTensor<T> {
let native = native_backend();
let mut shared_scalar = SharedTensor::<T>::new(native.device(), &vec![1]).unwrap();
let mut shared_scalar = SharedTensor::<T>::new(native.device(), &1).unwrap();
write_to_memory(shared_scalar.get_mut(native.device()).unwrap(), &[scalar]);

shared_scalar
Expand Down
8 changes: 4 additions & 4 deletions tests/layer_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod layer_spec {
#[test]
fn can_create_single_layer_sequential_layer() {
let mut model = SequentialConfig::default();
model.add_input("data", &vec![28, 28]);
model.add_input("data", &[28, 28]);
model.add_layer(LayerConfig::new("sigmoid", LayerType::Sigmoid));

Layer::from_config(cuda_backend(), &LayerConfig::new("model", LayerType::Sequential(model)));
Expand All @@ -69,7 +69,7 @@ mod layer_spec {
#[test]
fn can_create_simple_network_sequential_layer() {
let mut model = SequentialConfig::default();
model.add_input("data", &vec![1, 784]);
model.add_input("data", &[1, 784]);
model.add_layer(LayerConfig::new("linear1", LinearConfig { output_size: 1568 }));
model.add_layer(LayerConfig::new("sigmoid", LayerType::Sigmoid));
model.add_layer(LayerConfig::new("linear2", LinearConfig { output_size: 10 }));
Expand All @@ -83,12 +83,12 @@ mod layer_spec {
let cuda_backend = cuda_backend();

let mut normal_model = SequentialConfig::default();
normal_model.add_input("data", &vec![3]);
normal_model.add_input("data", &[3]);
normal_model.add_layer(LayerConfig::new("sigmoid", LayerType::Sigmoid));
let mut normal_network = Layer::from_config(cuda_backend.clone(), &LayerConfig::new("normal_model", LayerType::Sequential(normal_model)));

let mut reshape_model = SequentialConfig::default();
reshape_model.add_input("data", &vec![3]);
reshape_model.add_input("data", &[3]);
reshape_model.add_layer(LayerConfig::new("reshape", ReshapeConfig { shape: vec![1, 1, 3] }));
reshape_model.add_layer(LayerConfig::new("sigmoid", LayerType::Sigmoid));
let mut reshape_network = Layer::from_config(cuda_backend.clone(), &LayerConfig::new("reshape_model", LayerType::Sequential(reshape_model)));
Expand Down