Skip to content

【KernelGen】Add rsub operator - #1761

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

【KernelGen】Add rsub operator#1761
bin913 merged 2 commits into
flagos-ai:masterfrom
Schopenhauer-loves-Hegel:auto-gen/rsub

Conversation

@Schopenhauer-loves-Hegel

Copy link
Copy Markdown
Collaborator

PR Category

Operator

Type of Change

New Feature

Description

Add rsub operator implementation with Triton kernel.

  • Implementation mode: pointwise_dynamic
  • Accuracy test: 360/360 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
[1073741824] 4.7474 4.6703 1.017
[64, 64] 0.0068 0.0074 0.926
[4096, 4096] 0.0857 0.0857 1.001
[64, 512, 512] 0.0856 0.0857 0.999
[1024, 1024, 1024] 4.7481 4.6625 1.018
[1024, 1] 0.0071 0.0072 0.987
[1024, 16] 0.0074 0.0071 1.050
[1024, 256] 0.0086 0.0086 1.000
[1024, 4096] 0.0286 0.0288 0.992
[1024, 65536] 0.3061 0.3040 1.007
[64, 64, 1] 0.0078 0.0074 1.061
[64, 64, 16] 0.0073 0.0073 1.000
[64, 64, 256] 0.0134 0.0131 1.022
[64, 64, 4096] 0.0856 0.0856 1.000
[64, 64, 65536] 1.1927 1.1854 1.006

torch.float16

Shape Torch Latency (ms) Gems Latency (ms) Speedup
[1073741824] 4.7495 4.6840 1.014
[64, 64] 0.0073 0.0074 0.987
[4096, 4096] 0.0857 0.0856 1.001
[64, 512, 512] 0.0863 0.0855 1.009
[1024, 1024, 1024] 4.7464 4.6970 1.011
[1024, 1] 0.0068 0.0072 0.951
[1024, 16] 0.0074 0.0071 1.050
[1024, 256] 0.0086 0.0093 0.925
[1024, 4096] 0.0278 0.0287 0.968
[1024, 65536] 0.3060 0.3032 1.009
[64, 64, 1] 0.0080 0.0076 1.050
[64, 64, 16] 0.0078 0.0077 1.017
[64, 64, 256] 0.0133 0.0133 1.000
[64, 64, 4096] 0.0856 0.0855 1.001
[64, 64, 65536] 1.1934 1.1858 1.006

torch.float32

Shape Torch Latency (ms) Gems Latency (ms) Speedup
[1073741824] 9.4794 9.3395 1.015
[64, 64] 0.0068 0.0071 0.964
[4096, 4096] 0.1579 0.1578 1.001
[64, 512, 512] 0.1576 0.1575 1.001
[1024, 1024, 1024] 9.4795 9.3306 1.016
[1024, 1] 0.0068 0.0072 0.951
[1024, 16] 0.0071 0.0076 0.925
[1024, 256] 0.0101 0.0110 0.922
[1024, 4096] 0.0474 0.0480 0.987
[1024, 65536] 0.6022 0.5972 1.008
[64, 64, 1] 0.0074 0.0081 0.909
[64, 64, 16] 0.0079 0.0076 1.042
[64, 64, 256] 0.0165 0.0169 0.979
[64, 64, 4096] 0.1578 0.1584 0.996
[64, 64, 65536] 2.3780 2.3496 1.012

Overall: median speedup = 1.001x, mean speedup = 0.996x (45 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.

@tengqm tengqm added tests and removed tests labels Mar 1, 2026
@Schopenhauer-loves-Hegel Schopenhauer-loves-Hegel changed the title Add rsub operator 【KernelGen】Add rsub 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 18, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results for rsub operator

float16

Torch (ms) Gems (ms) Speedup Size
14.59 16.11 0.906 [1073741824]
0.007 0.008 0.878 [64,64]
0.085 0.089 0.964 [4096,4096]
0.086 0.089 0.965 [64,512,512]
13.32 17.02 0.782 [1024,1024,1024]
0.007 0.008 0.905 [1024,1]
0.008 0.008 0.967 [1024,16]
0.009 0.010 0.877 [1024,256]
0.028 0.030 0.930 [1024,4096]
0.305 0.312 0.979 [1024,65536]
0.007 0.008 0.873 [64,64,1]
0.008 0.007 1.122 [64,64,16]
0.012 0.014 0.879 [64,64,256]
0.085 0.088 0.975 [64,64,4096]
5.14 7.53 0.683 [64,64,65536]

float32

Torch (ms) Gems (ms) Speedup Size
24.30 30.52 0.796 [1073741824]
0.007 0.008 0.923 [64,64]
0.157 0.159 0.987 [4096,4096]
0.158 0.158 0.996 [64,512,512]
40.02 41.85 0.956 [1024,1024,1024]
0.008 0.008 1.000 [1024,1]
0.007 0.007 0.998 [1024,16]
0.010 0.011 0.922 [1024,256]
0.047 0.048 0.987 [1024,4096]
0.602 0.597 1.008 [1024,65536]
0.007 0.008 0.909 [64,64,1]
0.008 0.008 1.042 [64,64,16]
0.017 0.017 0.979 [64,64,256]
0.158 0.158 0.996 [64,64,4096]
2.38 2.35 1.012 [64,64,65536]

bfloat16

Torch (ms) Gems (ms) Speedup Size
13.41 16.42 0.817 [1073741824]
0.007 0.008 0.878 [64,64]
0.086 0.089 0.966 [4096,4096]
0.086 0.089 0.965 [64,512,512]
13.41 16.42 0.817 [1024,1024,1024]
0.008 0.007 1.169 [1024,1]
0.007 0.008 0.858 [1024,16]
0.009 0.010 0.902 [1024,256]
0.028 0.030 0.925 [1024,4096]
0.306 0.312 0.979 [1024,65536]
0.007 0.007 0.989 [64,64,1]
0.008 0.009 0.953 [64,64,16]
0.012 0.014 0.898 [64,64,256]
0.085 0.089 0.958 [64,64,4096]
4.88 5.29 0.923 [64,64,65536]

Overall: median speedup ~0.96x, mean ~0.94x — performance is on par with PyTorch, as expected for a simple pointwise op.

Comment thread src/flag_gems/ops/rsub.py Outdated
Comment thread src/flag_gems/ops/rsub.py Outdated
Comment thread tests/test_binary_pointwise_ops.py Outdated
Comment thread tests/test_binary_pointwise_ops.py Outdated
Comment thread benchmark/test_binary_pointwise_perf.py Outdated
tengqm
tengqm previously approved these changes Apr 23, 2026

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

Comment thread benchmark/test_rsub_perf.py Outdated
Comment thread benchmark/test_rsub_perf.py Outdated
Comment thread src/flag_gems/ops/__init__.py
Comment thread tests/test_rsub.py Outdated
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@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 rsub --ref cpu -x -q
36 passed

@bin913
bin913 merged commit 67c52e3 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
Co-authored-by: factnn <1050552884@qq.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
smridhi06 pushed a commit to smridhi06/FlagGems that referenced this pull request May 10, 2026
Co-authored-by: factnn <1050552884@qq.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
smridhi06 pushed a commit to smridhi06/FlagGems that referenced this pull request May 13, 2026
Co-authored-by: factnn <1050552884@qq.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
smridhi06 pushed a commit to smridhi06/FlagGems that referenced this pull request May 15, 2026
Co-authored-by: factnn <1050552884@qq.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
tengqm pushed a commit that referenced this pull request May 17, 2026
Co-authored-by: factnn <1050552884@qq.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
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