Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group sum of vector inner products with same vector #1175

Open
ricardoV94 opened this issue Jan 25, 2025 · 2 comments
Open

Group sum of vector inner products with same vector #1175

ricardoV94 opened this issue Jan 25, 2025 · 2 comments

Comments

@ricardoV94
Copy link
Member

ricardoV94 commented Jan 25, 2025

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 ricardoV94 changed the title Group dot products with same vector Group sum of vector inner products with same vector Jan 25, 2025
@ricardoV94
Copy link
Member Author

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

@The-Broken-Keyboard
Copy link

The-Broken-Keyboard commented Jan 28, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants