Skip to content

【KernelGen】Add einsum operator - #1731

Merged
bin913 merged 4 commits into
flagos-ai:masterfrom
Schopenhauer-loves-Hegel:auto-gen/einsum
May 9, 2026
Merged

【KernelGen】Add einsum operator#1731
bin913 merged 4 commits into
flagos-ai:masterfrom
Schopenhauer-loves-Hegel:auto-gen/einsum

Conversation

@Schopenhauer-loves-Hegel

Copy link
Copy Markdown
Collaborator

PR Category

Operator

Type of Change

New Feature

Description

Add einsum operator implementation with Triton kernel.

  • Implementation mode: N/A
  • Accuracy test: 57/57 passed

Issue

N/A

Progress

  • Change is properly reviewed (1 reviewer required, 2 recommended).
  • Change is responded to an issue.
  • Change is fully covered by a UT.

Performance

torch.bfloat16

Shape Torch Latency (ms) Gems Latency (ms) Speedup
torch.Size([384, 384]) 0.0109 0.0119 0.912
torch.Size([4096, 4096]) 0.5583 0.5640 0.990
torch.Size([1024, 1024]) 0.0229 0.0263 0.872
torch.Size([2048, 2048]) 0.1116 0.0996 1.121
torch.Size([4096, 4096]) 0.5459 0.5637 0.968
torch.Size([2, 384, 384]) 0.0130 0.0130 1.000
torch.Size([2, 4096, 4096]) 1.0824 1.1406 0.949
torch.Size([16, 1024, 1024]) 0.1536 0.1606 0.957
torch.Size([16, 2048, 2048]) 1.1166 1.1304 0.988
torch.Size([16, 4096, 4096]) 8.8083 9.2300 0.954

torch.float16

Shape Torch Latency (ms) Gems Latency (ms) Speedup
torch.Size([384, 384]) 0.0140 0.0122 1.144
torch.Size([4096, 4096]) 0.5663 0.5762 0.983
torch.Size([1024, 1024]) 0.0209 0.0258 0.811
torch.Size([2048, 2048]) 0.0787 0.0988 0.796
torch.Size([4096, 4096]) 0.5729 0.5715 1.003
torch.Size([2, 384, 384]) 0.0130 0.0135 0.957
torch.Size([2, 4096, 4096]) 1.1233 1.1739 0.957
torch.Size([16, 1024, 1024]) 0.1568 0.1627 0.964
torch.Size([16, 2048, 2048]) 1.1334 1.1718 0.967
torch.Size([16, 4096, 4096]) 9.0352 9.4699 0.954

torch.float32

Shape Torch Latency (ms) Gems Latency (ms) Speedup
torch.Size([384, 384]) 0.0225 0.0340 0.661
torch.Size([4096, 4096]) 7.2281 8.0535 0.898
torch.Size([1024, 1024]) 0.1361 0.1938 0.702
torch.Size([2048, 2048]) 0.9777 1.1970 0.817
torch.Size([4096, 4096]) 7.2291 8.0542 0.898
torch.Size([2, 384, 384]) 0.0827 0.0263 3.144
torch.Size([2, 4096, 4096]) 14.3647 14.5997 0.984
torch.Size([16, 1024, 1024]) 1.9076 1.8544 1.029
torch.Size([16, 2048, 2048]) 14.3852 14.6119 0.984
torch.Size([16, 4096, 4096]) 114.7864 116.1884 0.988

Overall: median speedup = 0.960x, mean speedup = 1.012x (30 data points)


Generated by auto_gen tool with Claude Code

@Schopenhauer-loves-Hegel

Copy link
Copy Markdown
Collaborator Author

Closing in favor of grouped PRs to reduce review overhead.

@Schopenhauer-loves-Hegel Schopenhauer-loves-Hegel changed the title Add einsum operator 【KernelGen】Add einsum operator Mar 23, 2026
@CLAassistant

CLAassistant commented Apr 15, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@factnn

factnn commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

einsum Benchmark Speedup Results

einsum_matmul (float16)

Size Torch (ms) Gems (ms) Speedup
[384,384]x[384,384] 0.013 0.015 0.866
[1024,1024]x[1024,1024] 0.022 0.027 0.813
[2048,2048]x[2048,2048] 0.077 0.104 0.740
[4096,4096]x[4096,4096] 0.545 0.748 0.728

einsum_matmul (float32)

Size Torch (ms) Gems (ms) Speedup
[384,384]x[384,384] 0.023 0.043 0.528
[1024,1024]x[1024,1024] 0.135 0.193 0.703
[4096,4096]x[4096,4096] 7.225 8.063 0.896

einsum_bmm (float16)

Size Torch (ms) Gems (ms) Speedup
[2,384,384]x2 0.013 0.015 0.860
[16,1024,1024]x2 0.155 0.174 0.890
[16,2048,2048]x2 1.135 1.247 0.911
[16,4096,4096]x2 8.872 10.185 0.871

einsum_bmm (float32)

Size Torch (ms) Gems (ms) Speedup
[2,384,384]x2 0.082 0.030 2.764
[16,1024,1024]x2 1.905 1.860 1.024
[16,4096,4096]x2 114.759 116.535 0.985

@factnn
factnn force-pushed the auto-gen/einsum branch 4 times, most recently from 8acb144 to 288c926 Compare April 23, 2026 07:53

@tengqm tengqm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some naming suggestions and sorting nits.
Wondering if we are doing a thorough performance testing when compared to accuracy checking. There are so many situations to cover.

Comment thread src/flag_gems/ops/__init__.py Outdated
Comment thread src/flag_gems/__init__.py Outdated
Comment thread tests/test_einsum.py Outdated
Comment thread tests/test_einsum.py Outdated
Comment thread tests/test_einsum.py Outdated
Comment thread tests/test_einsum.py Outdated
Comment thread tests/test_einsum.py Outdated
Comment thread tests/test_einsum.py Outdated
Comment thread benchmark/test_einsum_perf.py Outdated
Comment thread benchmark/test_einsum_perf.py Outdated
Comment thread benchmark/test_einsum_perf.py Outdated
Comment thread benchmark/test_einsum_perf.py Outdated
Comment thread tests/test_einsum.py Outdated
@factnn

factnn commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Thanks for the review! All three comments have been addressed:

  1. EINSUM_SHAPES style changed to if QUICK_MODE: ... else: ... pattern
  2. Removed duplicate standalone einsum_bmm_op / einsum_matmul_op functions, inlined as lambdas
  3. Regarding reusing existing shapes: einsum covers multiple operation patterns (matmul, bmm, dot, outer, trace, transpose, sum), each requiring different shape dimensions. The existing POINTWISE_SHAPES, REDUCTION_SHAPES, MNK_SHAPES etc. don't directly fit these varied formats, so custom shapes are kept for clarity.

Comment thread benchmark/test_einsum.py

@tengqm tengqm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall lgtm. thanks.

Comment thread benchmark/test_einsum.py
Comment thread benchmark/test_einsum_perf.py Outdated
Comment thread benchmark/test_einsum_perf.py Outdated
Comment thread benchmark/test_einsum_perf.py Outdated
@factnn
factnn force-pushed the auto-gen/einsum branch from 4376f8d to c2a01e6 Compare May 8, 2026 06:38
Comment thread benchmark/test_einsum.py Outdated
Comment thread benchmark/test_einsum.py Outdated
Comment thread benchmark/test_einsum.py Outdated
Comment thread benchmark/test_einsum.py Outdated
Comment thread benchmark/test_einsum.py Outdated
Comment thread benchmark/test_einsum.py Outdated
Comment thread benchmark/test_einsum.py Outdated
Comment thread benchmark/test_einsum.py Outdated
Comment thread benchmark/test_einsum.py Outdated
Comment thread benchmark/test_einsum.py Outdated
@factnn
factnn force-pushed the auto-gen/einsum branch from 18308a3 to 7e922e2 Compare May 8, 2026 07:38
@factnn

factnn commented May 8, 2026

Copy link
Copy Markdown
Contributor

UT Results

Branch rebased to upstream/master and tests passed:

python -m pytest tests/ -m einsum --ref cpu -x -q
60 passed

factnn and others added 2 commits May 9, 2026 10:56
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Qiming Teng <tengqm@outlook.com>
Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
@factnn
factnn force-pushed the auto-gen/einsum branch from 8d112c8 to bb78236 Compare May 9, 2026 02:58
Comment thread src/flag_gems/ops/einsum.py
@factnn

factnn commented May 9, 2026

Copy link
Copy Markdown
Contributor

The ii->i pattern is supported — it's handled by _is_diagonal_pattern before reaching _is_sum_pattern, so the uniqueness check in _is_sum_pattern doesn't affect it. Verified:

A = torch.randn(3, 3, device='cuda')
torch.einsum('ii->i', A)  # matches FlagGems output exactly

@factnn

factnn commented May 9, 2026

Copy link
Copy Markdown
Contributor

The ii->i pattern is supported — it's handled by _is_diagonal_pattern before reaching _is_sum_pattern, so the uniqueness check in _is_sum_pattern doesn't affect it. Verified locally and results match exactly.

@bin913
bin913 merged commit e23da3c into flagos-ai:master May 9, 2026
18 checks passed
withdrawn919 pushed a commit to withdrawn919/FlagGems that referenced this pull request May 9, 2026
* feat: add einsum operator with tests and benchmark

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Apply suggestions from code review

Co-authored-by: Qiming Teng <tengqm@outlook.com>
Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>

* fix: use consts.FLOAT_DTYPES and base.Benchmark in einsum benchmark

* fix: fix einsum benchmark - use correct base classes and shapes

---------

Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
Co-authored-by: factnn <1050552884@qq.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
Co-authored-by: Qiming Teng <tengqm@outlook.com>
smridhi06 pushed a commit to smridhi06/FlagGems that referenced this pull request May 10, 2026
* feat: add einsum operator with tests and benchmark

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Apply suggestions from code review

Co-authored-by: Qiming Teng <tengqm@outlook.com>
Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>

* fix: use consts.FLOAT_DTYPES and base.Benchmark in einsum benchmark

* fix: fix einsum benchmark - use correct base classes and shapes

---------

Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
Co-authored-by: factnn <1050552884@qq.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
Co-authored-by: Qiming Teng <tengqm@outlook.com>
tengqm pushed a commit that referenced this pull request May 11, 2026
smridhi06 pushed a commit to smridhi06/FlagGems that referenced this pull request May 13, 2026
* feat: add einsum operator with tests and benchmark

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Apply suggestions from code review

Co-authored-by: Qiming Teng <tengqm@outlook.com>
Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>

* fix: use consts.FLOAT_DTYPES and base.Benchmark in einsum benchmark

* fix: fix einsum benchmark - use correct base classes and shapes

---------

Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
Co-authored-by: factnn <1050552884@qq.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
Co-authored-by: Qiming Teng <tengqm@outlook.com>
smridhi06 pushed a commit to smridhi06/FlagGems that referenced this pull request May 15, 2026
* feat: add einsum operator with tests and benchmark

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Apply suggestions from code review

Co-authored-by: Qiming Teng <tengqm@outlook.com>
Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>

* fix: use consts.FLOAT_DTYPES and base.Benchmark in einsum benchmark

* fix: fix einsum benchmark - use correct base classes and shapes

---------

Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
Co-authored-by: factnn <1050552884@qq.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
Co-authored-by: Qiming Teng <tengqm@outlook.com>
tengqm added a commit that referenced this pull request May 17, 2026
* feat: add einsum operator with tests and benchmark

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Apply suggestions from code review

Co-authored-by: Qiming Teng <tengqm@outlook.com>
Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>

* fix: use consts.FLOAT_DTYPES and base.Benchmark in einsum benchmark

* fix: fix einsum benchmark - use correct base classes and shapes

---------

Signed-off-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
Co-authored-by: factnn <1050552884@qq.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Zang Peiyu <166481866+factnn@users.noreply.github.com>
Co-authored-by: Qiming Teng <tengqm@outlook.com>
tengqm pushed a commit that referenced this pull request May 17, 2026
@Schopenhauer-loves-Hegel
Schopenhauer-loves-Hegel deleted the auto-gen/einsum branch May 29, 2026 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants