-
Notifications
You must be signed in to change notification settings - Fork 493
【KernelGen】Add einsum operator #1731
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
Merged
bin913
merged 4 commits into
flagos-ai:master
from
Schopenhauer-loves-Hegel:auto-gen/einsum
May 9, 2026
+801
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8b25c6c
feat: add einsum operator with tests and benchmark
factnn bb78236
Apply suggestions from code review
factnn ac54def
fix: use consts.FLOAT_DTYPES and base.Benchmark in einsum benchmark
factnn aa0c2c6
fix: fix einsum benchmark - use correct base classes and shapes
factnn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| from typing import Generator | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from . import base, consts | ||
|
|
||
|
|
||
| class EinsumBenchmark(base.Benchmark): | ||
| DEFAULT_METRICS = consts.DEFAULT_METRICS[:] + ["tflops"] | ||
| DEFAULT_SHAPES = [(1, 512, 512, 512), (1, 1024, 1024, 1024), (16, 512, 512, 512)] | ||
|
|
||
| def __init__(self, *args, batched=False, input_fn=None, **kwargs): | ||
| self.batched = batched | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def set_more_shapes(self): | ||
| return [] | ||
|
|
||
| def set_shapes(self, *args, **kwargs): | ||
| self.shapes = self.DEFAULT_SHAPES | ||
|
|
||
| def get_input_iter(self, dtype) -> Generator: | ||
| for b, m, n, k in self.shapes: | ||
| if self.batched: | ||
| inp1 = torch.randn([b, m, k], dtype=dtype, device=self.device) | ||
| inp2 = torch.randn([b, k, n], dtype=dtype, device=self.device) | ||
| else: | ||
| inp1 = torch.randn([m, k], dtype=dtype, device=self.device) | ||
| inp2 = torch.randn([k, n], dtype=dtype, device=self.device) | ||
| yield inp1, inp2 | ||
|
|
||
| def get_tflops(self, op, *args, **kwargs): | ||
| A, B = args[0], args[1] | ||
| if self.batched: | ||
| return A.shape[0] * A.shape[1] * B.shape[2] * A.shape[2] * 2 | ||
| return A.shape[0] * B.shape[1] * A.shape[1] * 2 | ||
|
|
||
|
|
||
| class EinsumGenericBenchmark(base.GenericBenchmark): | ||
| def set_shapes(self, *args, **kwargs): | ||
| pass # keep shapes set by caller | ||
|
|
||
|
|
||
| def dot_input_fn(shape, dtype, device): | ||
| (n,) = shape | ||
| yield torch.randn(n, dtype=dtype, device=device), torch.randn( | ||
| n, dtype=dtype, device=device | ||
| ) | ||
|
|
||
|
|
||
| def outer_input_fn(shape, dtype, device): | ||
| m, n = shape | ||
| yield torch.randn(m, dtype=dtype, device=device), torch.randn( | ||
| n, dtype=dtype, device=device | ||
| ) | ||
|
|
||
|
|
||
| def unary_2d_input_fn(shape, dtype, device): | ||
| m, n = shape | ||
| yield (torch.randn(m, n, dtype=dtype, device=device),) | ||
|
|
||
|
|
||
| def unary_3d_input_fn(shape, dtype, device): | ||
| m, n, k = shape | ||
| yield (torch.randn(m, n, k, dtype=dtype, device=device),) | ||
|
|
||
|
|
||
| def ellipsis_input_fn(shape, dtype, device): | ||
| b, h, m, k, n = shape | ||
| yield torch.randn(b, h, m, k, dtype=dtype, device=device), torch.randn( | ||
| b, h, k, n, dtype=dtype, device=device | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.einsum | ||
| def test_einsum_matmul(): | ||
| bench = EinsumBenchmark( | ||
| input_fn=None, | ||
| op_name="einsum", | ||
| torch_op=lambda A, B: torch.einsum("ij,jk->ik", A, B), | ||
| dtypes=consts.FLOAT_DTYPES, | ||
| ) | ||
| bench.run() | ||
|
|
||
|
|
||
| @pytest.mark.einsum | ||
| def test_einsum_bmm(): | ||
| bench = EinsumBenchmark( | ||
| input_fn=None, | ||
| op_name="einsum", | ||
| torch_op=lambda A, B: torch.einsum("bij,bjk->bik", A, B), | ||
| dtypes=consts.FLOAT_DTYPES, | ||
| batched=True, | ||
| ) | ||
| bench.run() | ||
|
tengqm marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @pytest.mark.einsum | ||
| def test_einsum_dot(): | ||
| bench = EinsumGenericBenchmark( | ||
| input_fn=dot_input_fn, | ||
| op_name="einsum", | ||
| torch_op=lambda A, B: torch.einsum("i,i->", A, B), | ||
| dtypes=consts.FLOAT_DTYPES, | ||
| ) | ||
| bench.shapes = [(1024,), (4096,), (65536,)] | ||
| bench.run() | ||
|
|
||
|
|
||
| @pytest.mark.einsum | ||
| def test_einsum_outer(): | ||
| bench = EinsumGenericBenchmark( | ||
| input_fn=outer_input_fn, | ||
| op_name="einsum", | ||
| torch_op=lambda A, B: torch.einsum("i,j->ij", A, B), | ||
| dtypes=consts.FLOAT_DTYPES, | ||
| ) | ||
| bench.shapes = [(1024, 1024), (4096, 4096)] | ||
| bench.run() | ||
|
|
||
|
|
||
| @pytest.mark.einsum | ||
| def test_einsum_trace(): | ||
| bench = EinsumGenericBenchmark( | ||
| input_fn=unary_2d_input_fn, | ||
| op_name="einsum", | ||
| torch_op=lambda A: torch.einsum("ii->", A), | ||
| dtypes=consts.FLOAT_DTYPES, | ||
| ) | ||
| bench.shapes = [(1024, 1024), (4096, 4096)] | ||
| bench.run() | ||
|
|
||
|
|
||
| @pytest.mark.einsum | ||
| def test_einsum_diagonal(): | ||
| bench = EinsumGenericBenchmark( | ||
| input_fn=unary_2d_input_fn, | ||
| op_name="einsum", | ||
| torch_op=lambda A: torch.einsum("ii->i", A), | ||
| dtypes=consts.FLOAT_DTYPES, | ||
| ) | ||
| bench.shapes = [(1024, 1024), (4096, 4096)] | ||
| bench.run() | ||
|
|
||
|
|
||
| @pytest.mark.einsum | ||
| def test_einsum_transpose(): | ||
| bench = EinsumGenericBenchmark( | ||
| input_fn=unary_2d_input_fn, | ||
| op_name="einsum", | ||
| torch_op=lambda A: torch.einsum("ij->ji", A), | ||
| dtypes=consts.FLOAT_DTYPES, | ||
| ) | ||
| bench.shapes = [(1024, 1024), (4096, 4096)] | ||
| bench.run() | ||
|
|
||
|
|
||
| @pytest.mark.einsum | ||
| def test_einsum_sum_all(): | ||
| bench = EinsumGenericBenchmark( | ||
| input_fn=unary_3d_input_fn, | ||
| op_name="einsum", | ||
| torch_op=lambda A: torch.einsum("ijk->", A), | ||
| dtypes=consts.FLOAT_DTYPES, | ||
| ) | ||
| bench.shapes = [(64, 64, 64), (128, 128, 128)] | ||
| bench.run() | ||
|
|
||
|
|
||
| @pytest.mark.einsum | ||
| def test_einsum_sum_dim(): | ||
| bench = EinsumGenericBenchmark( | ||
| input_fn=unary_3d_input_fn, | ||
| op_name="einsum", | ||
| torch_op=lambda A: torch.einsum("ijk->j", A), | ||
| dtypes=consts.FLOAT_DTYPES, | ||
| ) | ||
| bench.shapes = [(64, 64, 64), (128, 128, 128)] | ||
| bench.run() | ||
|
|
||
|
|
||
| @pytest.mark.einsum | ||
| def test_einsum_ellipsis(): | ||
| bench = EinsumGenericBenchmark( | ||
| input_fn=ellipsis_input_fn, | ||
| op_name="einsum", | ||
| torch_op=lambda A, B: torch.einsum("...ij,...jk->...ik", A, B), | ||
| dtypes=consts.FLOAT_DTYPES, | ||
| ) | ||
| bench.shapes = [(2, 4, 64, 64, 128), (2, 8, 128, 128, 256)] | ||
| bench.run() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.