You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to initiate a discussion on using Rust's strong type system to avoid undesired panics at runtime with certain operations. As of now, nothing would stop the library user from doing this:
let a:Array<f64> = randu::<f64>(dim4!(1,1,3,4));let b:Array<f64> = randu::<f64>(dim4!(1,1,2,5));// The following line panics during runtime (dimensions of a and b mismatch), but it is allowed to compilelet c:Array<f64> = a + b;
In that regard, the API is not that much different of what we would have with a weak type system language. Unfortunately we are not really taking advantage of the tools Rusts gives us in regards of safety. A possible solution is to wrap Array<T> in a tuple struct parametrized by the array dimensions, something like:
I.e. if the left operand matrix has dimensions [1, 1, 3, 4], then the right operand matrix must have dimensions [1, 1, 4, ZO] and the result will have dimensions [1, 1, 3, ZO], where ZO is the number of columns of the right matrix.
If something like this compiles then we know for sure we are not going to have any panic during runtime!
let a = Tensor::<1,1,3,4,f64>::randu();let b = Tensor::<1,1,4,2,f64>::randu();let c = Tensor::<1,1,3,2,f64>::randu();let z = a*b + c;
Looking forward to hearing your thoughts on whether you think this could be a good idea and what approach you would follow to design/implement the API. Thank you all! ❤️
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi all lovely Rust 🦀 users!
I would like to initiate a discussion on using Rust's strong type system to avoid undesired panics at runtime with certain operations. As of now, nothing would stop the library user from doing this:
In that regard, the API is not that much different of what we would have with a weak type system language. Unfortunately we are not really taking advantage of the tools Rusts gives us in regards of safety. A possible solution is to wrap
Array<T>
in a tuple struct parametrized by the array dimensions, something like:And then implement the same traits/functions of
Array<T>
forTensor<W,X,Y,Z,T>
:Then we can use them to have compile time guarantees that we are not doing anything mathematically unsound. This does NOT compile:
The implementation of matmul would look something like this:
I.e. if the left operand matrix has dimensions
[1, 1, 3, 4]
, then the right operand matrix must have dimensions[1, 1, 4, ZO]
and the result will have dimensions[1, 1, 3, ZO]
, where ZO is the number of columns of the right matrix.If something like this compiles then we know for sure we are not going to have any panic during runtime!
Looking forward to hearing your thoughts on whether you think this could be a good idea and what approach you would follow to design/implement the API. Thank you all! ❤️
Beta Was this translation helpful? Give feedback.
All reactions