Reputation: 6650
I'm using Array2
or Array1
in rust for matrices in a neural network. (Yes, I know there are libraries for NNs. I want to do it with my own code for learning. The question stands independently of the neural networks, but neural networks serve as a suitable example, because here in many cases, the matrix sizes are known at compile time.)
Can the compiler do better optimizations when it knows the size of the Array2
matrices or Array1
vectors at compile time? How to make sure it knows them?
Here is code of my neural network layer with lines marked where I think that maybe we can give extra information to the compiler. Or is it not even required in the struct?
extern crate ndarray;
use ndarray::prelude::*;
use ndarray_rand::RandomExt;
use rand::distributions::Uniform;
struct LayerWithBias<const input_size: usize, const output_size: usize> {
/// weights
w: Array2<f32>, // do something with input_size and output_size here?
/// biases
b: Array1<f32>, // and do something with just output_size here?
}
impl<const input_size: usize, const output_size: usize>
LayerWithBias<input_size, output_size> {
fn new() -> Self {
let w = Array2::random((output_size, input_size), Uniform::new(-1.0, 1.0));
let b = Array1::zeros(output_size);
LayerWithBias { w, b }
}
}
Upvotes: 0
Views: 98
Reputation: 58785
No, there aren't any specific optimisations that you can enable by telling the compiler the number of items that your ndarray
collections will eventually hold.
The only way the compiler could possibly take advantage of that sort of information would be for fixed-sized arrays, which ndarray
's Array1
and Array2
are not. Even then, the kinds of optimisations that it could do would be for small arrays, for example to ensure that they fill cache lines efficiently. But if you have a sufficiently large amount of data that it's worth using ndarray
then this isn't going to make a difference; your data is contiguous and will fill many cache lines optimally.
Upvotes: 0