Skip to content

[KernelGen][MThreads] Add clip_ Moore Threads specialized operator - #293

Open
Yukun-Cui wants to merge 1 commit into
flagos-ai:infra-cifrom
Yukun-Cui:pr/mthreads-clip_
Open

[KernelGen][MThreads] Add clip_ Moore Threads specialized operator#293
Yukun-Cui wants to merge 1 commit into
flagos-ai:infra-cifrom
Yukun-Cui:pr/mthreads-clip_

Conversation

@Yukun-Cui

@Yukun-Cui Yukun-Cui commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

[KernelGen][MThreads] Add clip_ Moore Threads specialized operator

Summary

Add a Moore Threads (MUSA) specialized Triton kernel for clip_, overriding the generic implementation via runtime.replace_customized_ops(). Implements the clip operation.

Testing

  • Reused the existing upstream accuracy tests tests/test_clip.py (-m clip_)
  • Validated against reference on the MUSA device; specialization confirmed active via the GEMS_MTHREADS CLIP_ debug log
  • Falls back to the generic implementation for unsupported dtype/device/shape (fp64/int64 are not supported on Moore Threads hardware)

Performance

Compared against the generic FlagGems implementation on Moore Threads (MUSA).

clip_

dtype Size Torch Latency (ms) Gems Latency (ms) Speedup
float16 1073741824 3.468320 3.460880 1.002x
float16 64, 64 0.005080 0.004200 1.210x
float16 4096, 4096 0.054520 0.054840 0.994x
float16 64, 512, 512 0.054680 0.055120 0.992x
float16 1024, 1024, 1024 3.467840 3.454600 1.004x
float32 1073741824 6.686300 7.151880 0.935x
float32 64, 64 0.004840 0.004120 1.175x
float32 4096, 4096 0.105040 0.106140 0.990x
float32 64, 512, 512 0.105200 0.106360 0.989x
float32 1024, 1024, 1024 6.663880 7.127280 0.935x
bfloat16 1073741824 3.447560 3.450960 0.999x
bfloat16 64, 64 0.005360 0.003960 1.354x
bfloat16 4096, 4096 0.054160 0.054760 0.989x
bfloat16 64, 512, 512 0.054480 0.054960 0.991x
bfloat16 1024, 1024, 1024 3.460440 3.458040 1.001x
Operator Arithmetic Mean Speedup
clip_ 1.04x

Files Changed

  • src/flag_gems/runtime/backend/_mthreads/ops/clip_.py: Moore Threads Triton kernel + fallback
  • src/flag_gems/runtime/backend/_mthreads/ops/__init__.py: Register import and __all__

@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | clip_:mthreads

1 similar comment
@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | clip_:mthreads

@github-actions

github-actions Bot commented Aug 18, 2026

Copy link
Copy Markdown

❌ On-demand test failed

Operator: | clip_
Runner: mthreads
Backend: mthreads-musa520

The test failed to complete. Check the workflow run for details.

@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | clip_:mthreads

@Dongxu-H Dongxu-H left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

PR includes 140 files changed for a single backend operator, which is unusual. The clip_.py implementation uses grid-stride loop pattern with hardcoded grid dimensions. Logger naming is correct. Implementation handles scalar min/max bounds with appropriate fallback for tensor bounds.

@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | clip_:mthreads

@Dongxu-H Dongxu-H left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

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.

2 participants