owl
owl copied to clipboard
Option to disable broadcasting
Thank you for making Owl!
One of my pitfalls in numpy/pytorch is that I accidentally broadcast without knowing it. This happens often when you change something in one place, causing the size of a tensor to change in other places. The failure is silent and deadly, causing unexpected behavior.
I would love it if there was some way to disable broadcasting unless we explicitly allow it, thus giving us some more control in Owl than is there in numpy/pytorch. For normal functions, this could just be an extra optional argument, but for infix functions, it probably requires having 2 modules, one with broadcasting and one without, or new infix functions (maybe with a character appended) that disable broadcasting.
I do think no broadcasting should be the default, but I'm not sure it can be done at this stage. Also, obviously broadcasting from a constant to an array is fine: we already have special operators telling us it's happening.
My suggestion would be to create a separate, overlay library, that includes owl relevant modules but rebinds the operations with a non-broadcasting version (the most immediate way is by adding runtime checks on the shapes and failing or forwarding the computation to owl’s broadcasting operators).
This would make the external library very small and with little maintenance requirements. To override the operators one would just need to open the relevant module from the library instead of owl itself
Similar discussion found by @jzstark http://numpy-discussion.10968.n7.nabble.com/Silent-Broadcasting-considered-harmful-td39665.html
Numpy decided not to change, the reason is that broadcast logic is implmented down to the c level for efficiency considerations, same in owl. So the change will be significnat, and difficult to estimate the impact to the core.
What @mseri suggested seems the best solution to me, enforcing shape constraint at application level is much easier than changing the core part.