pytensor icon indicating copy to clipboard operation
pytensor copied to clipboard

Group sum of vector inner products with same vector

Open ricardoV94 opened this issue 10 months ago • 2 comments

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.

ricardoV94 avatar Jan 25 '25 16:01 ricardoV94

Related: (a * x1).T @ (b * x2) -> a * b * (x1.T @ x2), when a and b are scalars

ricardoV94 avatar Jan 25 '25 16:01 ricardoV94

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...

The-Broken-Keyboard avatar Jan 28 '25 06:01 The-Broken-Keyboard