fff-rs / juice

The Hacker's Machine Learning Engine
1.11k stars 76 forks source link

Unhelpful panic message on (presumably?) invalid inputs to `forward` #182

Open elidupree opened 1 year ago

elidupree commented 1 year ago

I'm a beginner to Juice who is still trying to figure out basic usage, and not finding the book or juice-examples very revelatory. To try to figure things out by experiment, I wrote this (presumably invalid) code:

let backend = Rc::new(juice::util::native_backend());
let linear_1: LayerConfig = LayerConfig::new("linear1", LinearConfig { output_size: 30 });
let mut linear_network_with_one_layer = Layer::from_config(backend, &linear_1);
let input = SharedTensor::<f32>::new(&[1, 1, 50]);
let input_lock = Arc::new(RwLock::new(input));
linear_network_with_one_layer.forward(&[input_lock]);

I was expecting to get an error message like "Error: expected input of size $foo but got input of size $bar". Instead, I got this panic message, which gives me very little help in figuring out what the error is.

As a beginner to Juice, I'm not certain whether this qualifies as a "bug in Juice", but the API docs do not suggest that Layer::forward is supposed to be able to panic.

thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', /n/HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/juice-0.3.0/src/layer.rs:535:34
stack backtrace:
   0: rust_begin_unwind
             at /rustc/b7bc90fea3b441234a84b49fdafeb75815eebbab/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /rustc/b7bc90fea3b441234a84b49fdafeb75815eebbab/library/core/src/panicking.rs:65:14
   2: core::panicking::panic_bounds_check
             at /rustc/b7bc90fea3b441234a84b49fdafeb75815eebbab/library/core/src/panicking.rs:150:5
   3: <usize as core::slice::index::SliceIndex<[T]>>::index
             at /rustc/b7bc90fea3b441234a84b49fdafeb75815eebbab/library/core/src/slice/index.rs:259:10
   4: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
             at /rustc/b7bc90fea3b441234a84b49fdafeb75815eebbab/library/core/src/slice/index.rs:18:9
   5: <alloc::vec::Vec<T,A> as core::ops::index::Index<I>>::index
             at /rustc/b7bc90fea3b441234a84b49fdafeb75815eebbab/library/alloc/src/vec/mod.rs:2736:9
   6: juice::layer::Layer<B>::forward
             at /n/HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/juice-0.3.0/src/layer.rs:535:34
drahnr commented 1 year ago

That definitely is to be considered a bug.