Skip to content

[KMCompiler] Add optimized index_copy and index_copy_ kernels for Ascend - #5507

Closed
Onisen7 wants to merge 192 commits into
flagos-ai:masterfrom
Onisen7:add-ascend-index-copy
Closed

[KMCompiler] Add optimized index_copy and index_copy_ kernels for Ascend#5507
Onisen7 wants to merge 192 commits into
flagos-ai:masterfrom
Onisen7:add-ascend-index-copy

Conversation

@Onisen7

@Onisen7 Onisen7 commented Aug 15, 2026

Copy link
Copy Markdown

PR Category

Operator

Type of Change

Performance Optimization

Description

This PR adds Ascend-specific Triton implementations of index_copy and
index_copy_.

The implementation is added in
src/flag_gems/runtime/backend/_ascend/ops/index_copy_.py and exported from
src/flag_gems/runtime/backend/_ascend/ops/__init__.py.

The Ascend implementation:

  • uses a flat kernel for workloads with inner_size <= 4;
  • uses a row-oriented 2D kernel for wider contiguous suffixes;
  • uses 64-bit offset arithmetic for large tensors;
  • keeps runtime dimensions out of Triton specialization to reduce unnecessary
    recompilation;
  • selects the flat-kernel block size according to the workload size;
  • skips kernel launches for empty index or inner dimensions;
  • launches kernels under the input device context;
  • masks invalid indices to prevent out-of-bounds stores; and
  • supports both out-of-place index_copy and in-place index_copy_.

Issue

N/A

Progress

  • Change is properly reviewed (1 reviewer required, 2 recommended).
  • Change is responded to an issue. (N/A: no linked issue)
  • Change is fully covered by a UT.

Testing

Test environment:

  • Device: Ascend 910B
  • CANN: 8.5.0
  • Python: 3.11.13
  • flagtree: 0.6.0+ascend3.2

Functional test command:

python -m pytest -s tests/test_index_copy_.py

Result: 24 passed.

The test covers both index_copy and index_copy_ with the default dtype,
shape, and dimension parameterization in tests/test_index_copy_.py.

Performance

Benchmark command:

python -m pytest -s benchmark/test_index_copy.py

The default benchmark ran in kernel mode at the comprehensive level.
Speedup = PyTorch latency / FlagGems latency.

The generic implementation completed the functional test with 24 passed,
but did not complete the default Ascend benchmark. The new Ascend-specific
implementation completed all 48 benchmark rows: 2 APIs, 3 dtypes, and 8 input
configurations.

Operator float16 mean float32 mean bfloat16 mean Overall mean
index_copy 1.045x 0.872x 1.051x 0.989x
index_copy_ 1.430x 1.345x 1.428x 1.401x
Combined - - - 1.195x

The values above are arithmetic means of the configured benchmark rows.
Performance varies by shape, so the table does not imply that every individual
configuration is faster than the native implementation. Full per-shape logs
are available if additional review is needed.

Onisen7 and others added 30 commits August 2, 2026 16:32
Signed-off-by: Onisen7 <2084548879@qq.com>
Signed-off-by: Onisen7 <2084548879@qq.com>
Signed-off-by: Onisen7 <2084548879@qq.com>
Signed-off-by: Qiming Teng <tengqm@outlook.com>
Signed-off-by: Qiming Teng <tengqm@outlook.com>
Signed-off-by: Qiming Teng <tengqm@outlook.com>
Signed-off-by: Qiming Teng <tengqm@outlook.com>
Updated comment regarding numpy version requirements.

Signed-off-by: Qiming Teng <tengqm@outlook.com>
* add sunrise's customized ops

* add sunrise's change
…gos-ai#4141)

* [KernelGen][Nvidia] Add float_power_ operator with Triton kernel

* chore: rerun CI

* Update operators.yaml

Signed-off-by: Dingxingdi <784318034@qq.com>

---------

Signed-off-by: Dingxingdi <784318034@qq.com>
Co-authored-by: bin913 <842884726@qq.com>
…gos-ai#4341)

Signed-off-by: Dongxu-H <dxhan@baai.ac.cn>
Co-authored-by: Qiming Teng <tengqm@outlook.com>
Co-authored-by: Dongxu-H <dxhan@baai.ac.cn>
Co-authored-by: bin913 <842884726@qq.com>
…ai#4002)

* [KernelGen][Nvidia] Add arccosh_ operator with Triton kernel

* Fix(arccosh_):Rewrite arccosh_ with pointwise_dynamic and fix host-layer signature

* Update src/flag_gems/runtime/backend/_iluvatar/ops/arccosh_.py

Co-authored-by: wanghairui <wanghairui0927@gmail.com>
Signed-off-by: Dongxu-H <dxhan@baai.ac.cn>

* sort

---------

Signed-off-by: Dongxu-H <dxhan@baai.ac.cn>
Co-authored-by: kkkwb <1115095230@qq.com>
Co-authored-by: Dongxu-H <dxhan@baai.ac.cn>
Co-authored-by: wanghairui <wanghairui0927@gmail.com>
…with Triton kernel (flagos-ai#4003)

* fix: rebase _native_batch_norm_legit_functional onto current master (clean)

Clean additive rebase — only _native_batch_norm_legit_functional entries added.
No upstream registration files overwritten.

* fix: remove unused batch_norm_backward_kernel dead code

* Fix:add missing save_var assertion in _native_batch_norm_legit_functional test

* Fix isort order of _native_batch_norm_legit_functional import in iluvatar ops __init__.py

* fix(_native_batch_norm_legit_functional):Correct save_var return semantics

---------

Signed-off-by: Dongxu-H <dxhan@baai.ac.cn>
Co-authored-by: yangy0906 <yangyang0906c@163.com>
Co-authored-by: Dongxu-H <dxhan@baai.ac.cn>
Co-authored-by: kkkwb <1115095230@qq.com>
Co-authored-by: 103yiran <1039105206@qq.com>
Oslomayor and others added 26 commits August 15, 2026 16:35
Expand mthreads conv2d_forward autotune candidates for conv1d_padding.
Verified with pytest perf and accuracy runs: representative fp32 speedup improved from 0.12x to 0.24x on [64,48,1024]x[128,48,5], and from 0.28x to 0.58x on [16,24,2048]x[96,12,7].
Accuracy passed with pytest -svx -m conv1d_padding --ref cpu.
Added 19 autotune configs for conv3d_forward on MTHREADS (BLOCK_NI_DO_HO_WO: 32–512, BLOCK_CO: 16–32, BLOCK_CI: 16–32, warps: 2–8, stages: 1–4). Previously only one config existed, causing Gems to be 22x–333x slower than Torch. Expanded configs enable shape-specific optimization, improving Gems speed from 0.60x to 9.14x vs Torch across all shapes and dtypes.
…5308)

- Convert tensor fill_value to scalar via .item() before kernel launch
- Remove deprecated FILL_VALUE_IS_PTR parameter
- Add SUBBLOCK_SIZE parameter to match updated full_kernel signature

Co-authored-by: yzw1128 <yzw1128@users.noreply.github.com>
* fix: prevent run_tests.py deadlock when worker crashes

Fixes worker process crashes causing job to hang at 99% indefinitely.

Root cause: When a worker process crashes, it fails to send exit signal,
causing display_loop to wait forever (while exited < n_workers).
Additionally, stderr was redirected to /dev/null, hiding all crash info.

Changes:
- Redirect worker stderr to per-worker log files for debugging
- Add try/finally to ensure exit signal is always sent
- Add per-op exception handling to prevent single op from killing worker
- Use Process.is_alive() in display_loop to detect dead workers
- Pass Process list instead of count to enable liveness checks

Evidence: Production deadlock on 10.0.29.1 showed GPU 5 worker died at
10:51 (after 20/920 ops), other 7 workers completed at 13:13, main
process stuck in infinite loop waiting for 8th exit signal, all 8
workers became zombies. Orphan op 'convert_weight_to_int4pack' found
with completed results but never written to summary5.json.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* docs: add detailed comments explaining deadlock prevention mechanisms

- Explain why worker stderr goes to log files instead of /dev/null
- Clarify why try/finally is critical for exit signal reliability
- Document why per-op exception handling prevents worker cascade failures
- Explain display_loop's is_alive() checks for hard crash detection

These comments help reviewers understand the fix rationale and prevent
future modifications from reintroducing the deadlock.

* revert: remove worker log files to avoid subprocess file handle deadlocks

Reviewer feedback: open file handles in forked worker processes can cause
deadlocks. Revert to /dev/null redirection (original behavior).

The core fix (try/finally for exit signals + is_alive() checks) remains intact.
Defects B/C/D/E are accepted; only defect A is reverted per review.

* style: apply black formatting fixes

- Format multi-line dict for better readability (line 848)
- Format list comprehension across multiple lines (line 851)
- Add blank lines after import statements (lines 867, 883)
- Use double quotes for string attribute access (line 907)

Fixes CI pre-commit check failures.

* style: fix black formatting issues with quotes and trailing commas

* refactor: simplify worker exception handling - remove redundant outer try/except

The outer try/except (lines 880-884) was redundant because:
1. It only printed traceback without any recovery logic
2. stderr is redirected to /dev/null, so traceback is invisible anyway
3. finally block already guarantees exit signal is sent

Single-layer try/finally is sufficient and more readable.

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
* fix

* fix

* pre-commit

---------

Co-authored-by: jia-heng <you@example.com>
Signed-off-by: Onisen7 <2084548879@qq.com>
Signed-off-by: Onisen7 <2084548879@qq.com>
Signed-off-by: Onisen7 <2084548879@qq.com>
Signed-off-by: Onisen7 <2084548879@qq.com>
@Onisen7 Onisen7 closed this Aug 15, 2026
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.