pytensor
pytensor copied to clipboard
Group sum of vector inner products with same vector
Description
import numpy as np
x1, x2, y = np.random.normal(size=(3, 1000, 1))
assert np.isclose(x1.T @ y + x2.T @ y, (x1 + x2).T @ y)
%timeit x1.T @ y + x2.T @ y
%timeit (x1 + x2).T @ y
1.86 μs ± 21.1 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
1.33 μs ± 13.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
Works with either shared y, or shared x.
Related: (a * x1).T @ (b * x2) -> a * b * (x1.T @ x2), when a and b are scalars
I am curious to know the probable solution for this..
I mean won't this depend on vector sizes? because grouping would require some techniques ( which will has it's own cost maybe ) and if the vector sizes are small then maybe it would be better to go with sum of dot products itself.
Please clarify the scope of this issue ....and give a little more explanation that what is required to do...