Skip to content

【KernelGen】Add index_copy_ operator - #1743

Merged
tengqm merged 22 commits into
flagos-ai:masterfrom
Schopenhauer-loves-Hegel:auto-gen/index_copy_
May 19, 2026
Merged

【KernelGen】Add index_copy_ operator#1743
tengqm merged 22 commits into
flagos-ai:masterfrom
Schopenhauer-loves-Hegel:auto-gen/index_copy_

Conversation

@Schopenhauer-loves-Hegel

Copy link
Copy Markdown
Collaborator

PR Category

Operator

Type of Change

New Feature

Description

Add index_copy_ operator implementation with Triton kernel.

  • Implementation mode: manual_kernel
  • Accuracy test: 24/24 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.float16

Shape Torch Latency (ms) Gems Latency (ms) Speedup
[64, 64] 0.0150 0.5200 0.029
[256, 256] 0.0160 0.5170 0.031
[1024, 1024] 0.0230 0.5180 0.044
[4096, 4096] 0.2280 0.5690 0.401
[1024, 65536] 1.0030 1.3320 0.753
[10000, 256] 0.0280 0.5120 0.055
[10000, 65536] 9.6590 11.7830 0.820

torch.float32

Shape Torch Latency (ms) Gems Latency (ms) Speedup
[64, 64] 0.0130 0.5120 0.025
[256, 256] 0.0140 0.5110 0.027
[1024, 1024] 0.0280 0.5650 0.049
[4096, 4096] 0.3140 0.5730 0.548
[1024, 65536] 1.3980 1.3760 1.016
[10000, 256] 0.0370 0.5160 0.072
[10000, 65536] 13.5900 12.2410 1.110

Overall: median speedup = 0.064x, mean speedup = 0.356x (14 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 index_copy_ operator 【KernelGen】Add index_copy_ operator Mar 23, 2026
@factnn
factnn force-pushed the auto-gen/index_copy_ branch from 445bd7c to 93c173f Compare April 15, 2026 20:29
@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

index_copy_ Benchmark Speedup Results

float16

Size Torch (ms) Gems (ms) Speedup
[64, 64] 0.016 0.717 0.022
[256, 256] 0.016 0.702 0.023
[1024, 1024] 0.027 0.731 0.036
[4096, 4096] 0.228 0.839 0.272
[1024, 65536] 1.005 1.383 0.726
[10000, 65536] 9.645 11.881 0.812

float32

Size Torch (ms) Gems (ms) Speedup
[64, 64] 0.013 0.722 0.018
[256, 256] 0.014 0.756 0.018
[1024, 1024] 0.028 0.737 0.038
[4096, 4096] 0.315 0.828 0.381
[1024, 65536] 1.393 1.460 0.954
[10000, 65536] 13.617 12.335 1.104

Note: Significant performance regression on small tensors due to kernel launch overhead. Large tensors (float32) show comparable or better performance.

@factnn
factnn force-pushed the auto-gen/index_copy_ branch from 93c173f to 513db40 Compare April 18, 2026 00:45
@factnn
factnn force-pushed the auto-gen/index_copy_ branch 2 times, most recently from 7d42283 to 8b7e646 Compare April 23, 2026 04:40

@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 looks good

Comment thread src/flag_gems/ops/index_copy_.py Outdated
Comment thread src/flag_gems/ops/index_copy_.py Outdated
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Apr 26, 2026
Comment thread benchmark/test_index_copy_perf.py Outdated


@pytest.mark.index_copy
def test_index_copy_perf():

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.

Suggested change
def test_index_copy_perf():
def test_index_copy():

Comment thread benchmark/test_index_copy_perf.py Outdated
op_name="index_copy",
torch_op=torch.index_copy,
input_fn=index_copy_input_fn,
dtypes=[torch.float16, torch.float32],

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.

why skip bf16?

Comment thread benchmark/test_index_copy_perf.py Outdated
op_name="index_copy_",
torch_op=torch.Tensor.index_copy_,
input_fn=index_copy_input_fn,
dtypes=[torch.float16, torch.float32],

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.

same question here

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.

shall we specify inplace=True?

Comment thread benchmark/test_index_copy_perf.py Outdated
Comment on lines +8 to +9
class TensorSelectBenchmark(GenericBenchmark2DOnly):
pass

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.

This additional layer makes no sense.

Comment thread PR_status.md Outdated
@@ -0,0 +1,45 @@
# Feb 27 KernelGen PR Status

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.

Remove this file.

Comment thread src/flag_gems/ops/index_copy_.py Outdated
def __call__(self, *args, **kwargs):
key = f"{self.arg_key(*args)}"
if key in self.overloads:
overload = self.overloads[key]

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.

If you return here with return overload(*args, **kwargs) here, you don't need the else on line 176.

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.

done

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.

No. You didn't return, still.

Comment thread src/flag_gems/ops/index_copy_.py
@factnn
factnn force-pushed the auto-gen/index_copy_ branch from 43b3b14 to 0b5f06e Compare May 10, 2026 13:32
@factnn
factnn force-pushed the auto-gen/index_copy_ branch from 0b5f06e to af10c71 Compare May 11, 2026 07:09
Comment thread benchmark/test_index_copy.py
Comment thread benchmark/test_index_copy_perf.py Outdated


def _inplace_input_fn(shape, dtype, device):
yield from _tensor_input_fn(shape, dtype, device)

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.

Why this wrapper?

Comment thread conf/operators.yaml
Comment thread src/flag_gems/ops/index_copy_.py Outdated
def __call__(self, *args, **kwargs):
key = f"{self.arg_key(*args)}"
if key in self.overloads:
overload = self.overloads[key]

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.

No. You didn't return, still.

Comment thread src/flag_gems/ops/index_copy_.py
Comment thread src/flag_gems/ops/index_copy_.py
@factnn factnn mentioned this pull request May 14, 2026
3 tasks
@tengqm
tengqm merged commit 8d23621 into flagos-ai:master May 19, 2026
18 checks passed
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