Skip to content

[KernelGen][MThreads] Add amp_foreach_non_finite_check_and_unscale_ Moore Threads specialized operator - #307

Open
Yukun-Cui wants to merge 2 commits into
flagos-ai:infra-cifrom
Yukun-Cui:pr/mthreads-amp_foreach_non_finite_check_and_unscale_
Open

[KernelGen][MThreads] Add amp_foreach_non_finite_check_and_unscale_ Moore Threads specialized operator#307
Yukun-Cui wants to merge 2 commits into
flagos-ai:infra-cifrom
Yukun-Cui:pr/mthreads-amp_foreach_non_finite_check_and_unscale_

Conversation

@Yukun-Cui

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

Copy link
Copy Markdown
Contributor

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

Summary

Add a Moore Threads (MUSA) specialized Triton kernel for amp_foreach_non_finite_check_and_unscale_, overriding the generic implementation via runtime.replace_customized_ops(). This op checks for non-finite values in a list of gradient tensors and unscales them by a given scaler, used in automatic mixed-precision training.

Testing

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

Performance

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

amp_foreach_non_finite_check_and_unscale_

dtype Size Torch Latency (ms) Gems Latency (ms) Speedup
float16 1073741824 8.534200 4.841320 1.763x
float16 64, 64 0.009240 0.015520 0.595x
float16 4096, 4096 0.709480 0.092120 7.702x
float16 64, 512, 512 0.763440 0.092520 8.252x
float16 1024, 1024, 1024 28.540560 4.839480 5.897x
float32 1073741824 16.861000 10.437079 1.615x
float32 64, 64 0.007640 0.015480 0.494x
float32 4096, 4096 0.489680 0.167440 2.925x
float32 64, 512, 512 0.472320 0.168240 2.807x
float32 1024, 1024, 1024 16.890640 10.409160 1.623x
Operator Arithmetic Mean Speedup
amp_foreach_non_finite_check_and_unscale_ 3.37x

Files Changed

  • src/flag_gems/runtime/backend/_mthreads/ops/amp_foreach_non_finite_check_and_unscale_.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 | amp_foreach_non_finite_check_and_unscale_:mthreads

1 similar comment
@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | amp_foreach_non_finite_check_and_unscale_:mthreads

@github-actions

github-actions Bot commented Aug 18, 2026

Copy link
Copy Markdown

❌ On-demand test failed

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

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

@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | amp_foreach_non_finite_check_and_unscale_:mthreads

3 similar comments
@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | amp_foreach_non_finite_check_and_unscale_:mthreads

@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | amp_foreach_non_finite_check_and_unscale_:mthreads

@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | amp_foreach_non_finite_check_and_unscale_: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

@Yukun-Cui
Yukun-Cui force-pushed the pr/mthreads-amp_foreach_non_finite_check_and_unscale_ branch from e591fec to 8aae535 Compare August 19, 2026 03:41

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

all list order incorrect, amp_foreach_non_finite_check_and_unscale should be after amax in alphabetical order

…py to _amp_foreach_non_finite_check_and_unscale_.py

Align the specialized op file name with the op symbol name
_amp_foreach_non_finite_check_and_unscale_ (inherited from the upstream
aten::_amp_foreach_non_finite_check_and_unscale_ op). Matches the naming
used by the hygon and cambricon backends for the same op. Update the
__init__ import path accordingly; isort moves it to the top of the
relative-import block. No functional change.
@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | amp_foreach_non_finite_check_and_unscale_:mthreads

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