Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
192 commits
Select commit Hold shift + click to select a range
e23abba
Create index_copy_
Onisen7 Aug 2, 2026
ffd6769
feat: add optimized index_copy_ for NVIDIA backend
Onisen7 Aug 2, 2026
8f97666
Merge branch 'master' of github.com:Onisen7/FlagGems
Onisen7 Aug 2, 2026
795d783
Delete src/flag_gems/runtime/backend/_nvidia/ops/index_copy_
Onisen7 Aug 2, 2026
0fc7db7
feat: add index_copy_ operator for Hygon backend
Onisen7 Aug 4, 2026
3daeba0
Update index_copy_.py
Onisen7 Aug 4, 2026
03018f1
feat: add index_copy_ operator for Ascend backend
Onisen7 Aug 7, 2026
88e5a6e
Merge branch 'master' of github.com:Onisen7/FlagGems into ascend-inde…
Onisen7 Aug 7, 2026
8e90dd5
feat: add index_copy_ for Ascend backend
Onisen7 Aug 7, 2026
9f7d4f0
Add index_copy_ op for ascend backend
Onisen7 Aug 14, 2026
8c39aed
Update __init__.py to export index_copy_ op
Onisen7 Aug 14, 2026
ddd3490
Update torch version to 2.9.1 in backends.yaml
tengqm Jul 27, 2026
e1f2ac0
Update numpy dependency to allow versions greater than 2
tengqm Jul 27, 2026
6e13fbf
Update upload-artifact action to version 7.0.0
tengqm Jul 28, 2026
1a02e73
Update upload-artifact action to version 7.0.0 (#5002)
tengqm Jul 28, 2026
fb157e0
Revise numpy version requirement comment
tengqm Jul 28, 2026
510fb24
Fix mul fallback error (#4999)
0x45f Jul 28, 2026
e2f9594
Add te rmsnorm_fwd and rmsnorm_bwd op (#4640)
0x45f Jul 28, 2026
29f8c82
Fix rms_norm implementation (#5009)
CLYtou Jul 28, 2026
533965b
fix issue 4055 (#4743)
huangyiqun Jul 29, 2026
2b44d40
fix issue 4057 (#4742)
huangyiqun Jul 29, 2026
693fbe1
fix issue 4058 (#4741)
huangyiqun Jul 29, 2026
6a04eff
[_sunrise] update sunrise ops (#5024)
Dayrker Jul 29, 2026
d0ad5e8
Make upload-artifact step optional in on-demand test workflow (#5007)
tengqm Jul 29, 2026
c4d5e4a
Fix hygon (#5016)
huangyiqun Jul 29, 2026
98be909
[KernelGen][Nvidia] Add float_power_ operator with Triton kernel (#4141)
Dingxingdi Jul 29, 2026
ff48da8
[KernelGen][Nvidia] Add max_unpool3d operator with Triton kernel (#4341)
Dingxingdi Jul 29, 2026
2863040
Pick MXFP4 MoE block_m by padding cost, not an absolute M cutoff (#5027)
zeroherolin Jul 29, 2026
597690b
[KernelGen][Nvidia] Add arccosh_ operator with Triton kernel (#4002)
XDYuanzhuLee Jul 29, 2026
9f1dce7
[KernelGen][Nvidia] Add _native_batch_norm_legit_functional operator …
XDYuanzhuLee Jul 29, 2026
6311c73
Add wheel to build tools (#5047)
tengqm Jul 29, 2026
7bc7d61
fix(config): lazy import c_operators — detect .so without loading (#4…
tengqm Jul 29, 2026
b443219
Add wheel to setup.sh for some backends (#5048)
tengqm Jul 29, 2026
1915247
[ENFLAME]enflame_to_FlagOS_20260729 (#5044)
chongzhouyang Jul 29, 2026
a93c514
fix(utils): add fallback_j1 (#5038)
103yiran Jul 29, 2026
1198017
[QC] Optimize flash_mla_with_kvcache for model1 (#5010)
monellz Jul 30, 2026
5624142
[Runtime][AMD] Add RDNA4 autotune configs and match arch by exact GPU…
WhatGhost Jul 30, 2026
159c46b
fix: replace triton_src kernel refs with ops/ paths for contiguous an…
yysheng26 Jul 30, 2026
1ddf2d1
fix(hygon): add fp64 path to mm (#4799)
douxetpur Jul 30, 2026
f3d8340
[KernelGen][Nvidia] Add arctan2 operator with Triton kernel (#4006)
bwbwzzz Jul 30, 2026
b8bd04a
[KernelGen][Nvidia] Add special_legendre_polynomial_p operator with T…
XDYuanzhuLee Jul 30, 2026
a4544a7
[KernelGen][Nvidia] Add erfc and special_erfc operator with Triton ke…
XDYuanzhuLee Jul 30, 2026
bdf878c
Add _pdist_forward operator with Triton kernel (#3948)
XDYuanzhuLee Jul 30, 2026
c027c92
[KernelGen][Nvidia] Add log_sigmoid_forward operator with Triton kern…
XDYuanzhuLee Jul 30, 2026
65a422e
build(deps): bump the actions-minor group across 1 directory with 3 u…
dependabot[bot] Jul 30, 2026
4a85be2
Add logging.debug in common ops (#5057)
0x45f Jul 31, 2026
cb660ae
Fix CI job to runner mapping (#5049)
tengqm Jul 31, 2026
d75eca0
Fix import error in ascend (#5101)
0x45f Jul 31, 2026
4aac8eb
[Ascend] Fix ascend ops when running Qwen3.6 vLLM with flagtree ascen…
zhzhcookie Jul 31, 2026
e81f89c
[KMCompiler][Nvidia] add operator linalg_lu_factor (#4945)
YangLong114514 Aug 3, 2026
16768a6
[KernelGen][Nvidia] Add subtract operator with Triton kernel (#4512)
chx7514 Aug 3, 2026
980e273
[KernelGen][Nvidia] Move sign operator to ops (#5030)
YufanPeter Aug 3, 2026
5cb4d07
[KernelGen][Nvidia] Move sgn operator to ops (#4990)
YufanPeter Aug 3, 2026
a78e857
[KernelGen][Nvidia] Add mish_backward operator with Triton kernel (#4…
Dingxingdi Aug 3, 2026
b41ca5e
[KernelGen][Nvidia] Add lstm operator with Triton kernel (#4288)
Dingxingdi Aug 3, 2026
4ff3b69
[KernelGen][Nvidia] Add miopen_batch_norm_backward operator with Trit…
Dingxingdi Aug 3, 2026
e8857a0
[KernelGen][Nvidia] Add igamma_ operator with Triton kernel (#4145)
Dingxingdi Aug 3, 2026
b32b27d
Optimize index_add for contiguous suffix layouts (#4670)
ZYX223 Aug 3, 2026
2d4e5af
add Devcontainer support (#5137)
Salvationhope Aug 3, 2026
c4c12c6
[KMCompiler][Nvidia] opt for log_normal_ operator (#4621)
littlekid-zxt Aug 3, 2026
e4e3d7d
[KMCompiler] Add Triton nonzero_static operator (#4661)
chenkc2025 Aug 3, 2026
09ade37
[KMCompiler][NVIDIA][Hygon] Add nansum operator with triton kernel (#…
Willing2329 Aug 3, 2026
671bc3d
[KMCompiler][NVIDIA] Add NVIDIA cholesky solve operator (#5011)
YangLong114514 Aug 3, 2026
6b32024
[KernelGen][Nvidia] Add _cdist_forward operator with Triton kernel (#…
XDYuanzhuLee Aug 4, 2026
92e8bfb
[KernelGen][Nvidia] Add _convert_weight_to_int4pack operator with Tri…
XDYuanzhuLee Aug 4, 2026
4b67150
[KernelGen][Nvidia] Add binary_cross_entropy_backward operator with T…
LoserCheems Aug 4, 2026
748d192
[KernelGen][Nvidia] Add expand_as operator (#5041)
LoserCheems Aug 4, 2026
da70a36
[KernelGen][Nvidia] Add flatten operator (#5039)
LoserCheems Aug 4, 2026
6c8da19
[KernelGen][Nvidia] Add unfold operator (#5037)
LoserCheems Aug 4, 2026
d9ceb2b
[KernelGen][Nvidia] Add unsafe_chunk operator with Triton kernel (#5028)
LoserCheems Aug 4, 2026
98c68c3
[KernelGen][Nvidia] Add eq_ operator with Triton kernel (#5108)
ShawnsYing Aug 4, 2026
8d6c4a6
Optimize wna16 MoE main loop: one mma per K tile instead of eight (#5…
zeroherolin Aug 4, 2026
7dd1ce1
[KernelGen][Nvidia] Add special_erfcx operator with Triton kernel (#3…
XDYuanzhuLee Aug 4, 2026
1425b7f
[KernelGen][Nvidia] Add alias operator (#5043)
LoserCheems Aug 4, 2026
e4cdfad
fix adaptive_avg_pool2d (#4952)
fpzh2011 Aug 4, 2026
da5202b
TEST CASE FIX -- iluvatar (#5131)
jia-heng Aug 4, 2026
2912c62
[TSINGMICRO] update tsingmicro backend to 0804 (#5181)
tsingmicro-public-e Aug 4, 2026
64520d3
fix(mul): gate optimized path on active backend device, not hardcoded…
tengqm Aug 4, 2026
501ece7
fix(apply_rotary_pos_emb): fix cos/sin error (#5073)
103yiran Aug 4, 2026
0581a6c
fix: resolve_conj performance test failure on mthreads backend. (#5109)
lyujheng Aug 4, 2026
eab9cd2
fix: resolve copy op accuracy failure on mthreads backend (#5107)
lyujheng Aug 4, 2026
08e124b
fix: vector_norm accuracy test failure on mthreads backend (#5106)
lyujheng Aug 4, 2026
92c2d15
fix: mul accuracy and performance on mthreads (#5111)
lyujheng Aug 4, 2026
96a644e
[KernelGen][Nvidia] Add chunk operator (#5033)
LoserCheems Aug 4, 2026
ba143f6
[KernelGen][Nvidia] Add isposinf operator with Triton kernel (#3923)
XDYuanzhuLee Aug 4, 2026
9285f40
[KernelGen][Nvidia] Add _make_dep_token operator with Triton kernel (…
XDYuanzhuLee Aug 4, 2026
ae5223e
[KernelGen][Nvidia] Add view_as_complex operator with Triton kernel (…
LoserCheems Aug 4, 2026
94f7b67
[KernelGen][Nvidia] Move hardtanh_ operator from experimental to ops …
YufanPeter Aug 4, 2026
e17b13d
[KernelGen][Nvidia] Add _reshape_alias operator (#5121)
xuanzhengdu-eng Aug 4, 2026
b35e06a
[KernelGen][Nvidia] Add less_ operator with Triton kernel (#5126)
ShawnsYing Aug 4, 2026
834623b
[KernelGen][Nvidia] Add less_equal_ operator with Triton kernel (#5124)
ShawnsYing Aug 4, 2026
03bffd6
[KernelGen][Nvidia] Add le_ operator with Triton kernel (#5122)
ShawnsYing Aug 4, 2026
bb785ed
fix: register empty as empty.memory_format so torch.empty uses FlagGe…
awayzjj Aug 5, 2026
f370b85
fix: recognize vLLM stable ABI extensions (#5150)
CherryLemon Aug 5, 2026
02583cd
[_sunrise] update sunrise ops (#5238)
Dayrker Aug 5, 2026
093ff90
Fix IX gate by pinning numpy version < 2 (#5244)
tengqm Aug 5, 2026
7d388b9
fix: index_add accuracy failure on mthreads (#5246)
lyujheng Aug 5, 2026
7c5f531
[KernelGen][metax] Fix num_warps exceeding hardware thread limit for …
yzw1128 Aug 5, 2026
860d923
Revert "[KernelGen][Nvidia] Add view_as_complex operator with Triton …
103yiran Aug 5, 2026
95ee7e3
[KernelGen][Nvidia] Move take operator to ops (#5070)
YufanPeter Aug 5, 2026
1901aff
[KernelGen][Nvidia] Move log_ operator to ops (#5045)
YufanPeter Aug 5, 2026
7d1d43c
Ship flaggems-setup as a top-level module and auto-install a compiler…
tengqm Aug 5, 2026
530cc20
fix(cpp): disable aten dispatch + add PrivateUse1 guard in fallback (…
tengqm Aug 5, 2026
6921cec
fix(cmake): add global include_directories for NPU backend (#4960)
tengqm Aug 5, 2026
fe01a9d
fix: add tsingmicro/txda to device detection (#5056)
tengqm Aug 5, 2026
28e68a2
docs(release): place dev tag on an empty commit after release, not on…
tengqm Aug 5, 2026
dd8c4b4
Upgrade triton for Sunrise (#5268)
tengqm Aug 5, 2026
d2f7f99
#19382 tang -> ptpu (#5267)
Dayrker Aug 5, 2026
5dc305e
Bump wheel version for CVE (#5271)
tengqm Aug 5, 2026
83fa63b
Revert "#19382 tang -> ptpu (#5267)" (#5278)
Dayrker Aug 6, 2026
91fc7df
[KernelGen][Nvidia] Add native_layer_norm operator with Triton kernel…
CoyeCHEN Aug 6, 2026
ac44f07
[KernelGen][Nvidia] Add _flash_attention_forward operator with Triton…
CoyeCHEN Aug 6, 2026
0a92a62
[KernelGen][Nvidia] Add _scaled_dot_product_flash_attention operator …
CoyeCHEN Aug 6, 2026
0ffc01d
[KernelGen][Nvidia] Add lift_fresh operator with Triton kernel (#5148)
ShawnsYing Aug 6, 2026
3232b2f
[KernelGen][Nvidia] Move fix_ operator to ops (#5158)
xuanzhengdu-eng Aug 6, 2026
12decb9
[KernelGen][Nvidia] Move hypot_ operator to ops (#5157)
xuanzhengdu-eng Aug 6, 2026
bf9c414
[KernelGen][Nvidia] Move heaviside_ operator to ops (#5156)
xuanzhengdu-eng Aug 6, 2026
d81e24d
[KernelGen][Nvidia] Move huber_loss operator to ops (#5154)
xuanzhengdu-eng Aug 6, 2026
f6dbaa7
[KernelGen][Nvidia] Move arccosh operator to ops (#5163)
YufanPeter Aug 6, 2026
e49a5f5
[KernelGe][Nvidia] Move hardsigmoid operator to ops (#5149)
YufanPeter Aug 6, 2026
a69d0df
[KernelGen][Nvidia] Add _cudnn_attention_forward operator with Triton…
yzw1128 Aug 6, 2026
dd31f12
[KernelGen][Nvidia] Add _adaptive_avg_pool2d_backward operator with T…
Yukun-Cui Aug 6, 2026
5c9bfdd
[KernelGen][Nvidia] Add replication_pad3d_backward operator with Trit…
Yukun-Cui Aug 6, 2026
d4ba514
[KernelGen][Nvidia] Add _weight_norm operator with Triton kernel (#5080)
Yukun-Cui Aug 6, 2026
2bf1b7f
[KernelGen][Nvidia] Add replication_pad2d operator with Triton kernel…
chx7514 Aug 6, 2026
b4bb015
[KernelGen][Nvidia] Add reflection_pad2d_backward operator with Trito…
chx7514 Aug 6, 2026
ddcfba1
[KernelGen][Nvidia] Add _fused_rms_norm operator with Triton kernel (…
chx7514 Aug 6, 2026
0a72e92
[KernelGen][Nvidia] Add cholesky_inverse operator with Triton kernel …
LoserCheems Aug 6, 2026
af679cf
[KernelGen][Nvidia] Move hardshrink operator to ops (#5155)
xuanzhengdu-eng Aug 6, 2026
59ae4f5
[KernelGen][Nvidia] Add max_pool2d_with_indices_backward operator (#5…
chx7514 Aug 6, 2026
e00a819
fix(utils): add pure-triton nextafter fallback for non-CUDA backends …
tengqm Aug 6, 2026
95d14b0
[Fix] fix enflame backend operator bug. (#5302)
chaaa-a Aug 6, 2026
9449e68
[KMCompiler][Nvidia]Add Polygamma Operator (#4500)
cheersluvs Aug 7, 2026
519d7e9
[KMCompiler][Nvidia] Add pairwise_distance operator with triton kerne…
Caeruleann Aug 7, 2026
044bc5f
[KMCompiler][Ascend]Add linalg_lstsq NPU backend support (#5173)
cheersluvs Aug 7, 2026
819d4ee
[KMCompiler] [Nvidia] add linalg_lu_factor_ex operator with Triton ke…
YangLong114514 Aug 7, 2026
9a2469b
Contribution doc update (#5307)
103yiran Aug 7, 2026
f40ff8b
Drop experimental operator sinc (#4810)
zheng1 Aug 7, 2026
c9425b2
[KernelGen][Nvidia] Add special_bessel_j0 operator with Triton kernel…
XDYuanzhuLee Aug 7, 2026
ff9a6be
fix sinc (#4892)
llaboon Aug 7, 2026
35154c6
Fix cumsum ZeroDivisionError on empty tensor (#4541)
DannyP0 Aug 7, 2026
fd6e811
fix add op test (#5325)
huangyiqun Aug 7, 2026
e818d44
[mthreads] Optimize flip for multi-dim tensors with trailing non-flip…
Oslomayor Aug 7, 2026
27638a8
add logger debug (#5210)
douxetpur Aug 7, 2026
f87c075
cambricon: update and fix kernel (#4435)
chenmiao1919 Aug 7, 2026
d0adba8
Fix code style (#5331)
0x45f Aug 7, 2026
cea9f5a
[KernelGen][Nvidia] Add lu_unpack operator with Triton kernel (#5072)
LoserCheems Aug 7, 2026
3880202
[KernelGen][Nvidia] Add block_diag operator with Triton kernel (#5068)
LoserCheems Aug 7, 2026
0085515
[KernelGen][Nvidia] Add addbmm operator with Triton kernel (#5055)
LoserCheems Aug 7, 2026
9821f07
fix(ops): skip triton kernels for complex dtypes in empty/lift_fresh/…
CLYtou Aug 7, 2026
8bf14ec
[KernelGen][Nvidia] Add scatter_add operator with Triton kernel (#4502)
chx7514 Aug 7, 2026
0feaee0
fix(conf): add missing info for special_log_softmax (#5333)
103yiran Aug 7, 2026
01effe8
fused: downcast slot_mapping to int32 for enflame on reshape_and_cach…
tengqm Aug 9, 2026
e551554
Append commit id to __version__ for dev builds (#5346)
tengqm Aug 9, 2026
f2e0546
fix(utils): add pure-triton j0 and log2 fallbacks for non-CUDA backen…
tengqm Aug 9, 2026
97c599c
Split multi-version vendors into versioned backend keys (#5350)
tengqm Aug 9, 2026
902c0ec
fix(cambricon): tolerate triton forks lacking TRITON_MAX_TENSOR_NUMEL…
tengqm Aug 9, 2026
f21a6b2
Drop experimental _unsafe_view operator (#4723)
tengqm Aug 10, 2026
84e47e3
[KernelGen][Nvidia] Add _has_compatible_shallow_copy_type operator (#…
xuanzhengdu-eng Aug 10, 2026
82acdbf
[KernelGen][Nvidia] Add sym_storage_offset operator with Triton kerne…
CoyeCHEN Aug 10, 2026
b40b641
fix: div accuracy test failures on mthreads backend (#5295)
lyujheng Aug 10, 2026
a38cc4f
fix: add mthreads mul specialization for complex dtype operations (#5…
lyujheng Aug 10, 2026
95fc69a
[AUTOTUNER] Add (XGB+GA) FlagTune support (via FlagTree) (#4740)
HenryRenYz Aug 10, 2026
5d86d41
Metax fix shared ops (#5334)
CLYtou Aug 10, 2026
0a013a7
Metax c550 operator fixes (#5337)
CLYtou Aug 10, 2026
30142d6
[MTHREADS] optimize conv1d_padding autotune configs (#5343)
Oslomayor Aug 10, 2026
6941300
[MTHREADS] optimize conv3d_padding autotune configs (#5353)
Oslomayor Aug 10, 2026
a2af686
[KernelGen][Nvidia] Add special_i1e operator with Triton kernel (#5276)
CoyeCHEN Aug 10, 2026
bb8907d
Fix special_chebyshev_polynomial_w ut (#5352)
0x45f Aug 10, 2026
efbb669
[MTHREADS] Fix flip op: correct multi-dim index decoding and per-dim …
lyujheng Aug 10, 2026
0a0e318
[KernelGen][Nvidia] Add special_exp2 operator with Triton kernel (#5275)
CoyeCHEN Aug 11, 2026
bc28753
[KernelGen][Nvidia] Add special_erf operator with Triton kernel (#5274)
CoyeCHEN Aug 11, 2026
0a9d125
fix(ascend): align full_like with updated full_kernel API (#5308)
yzw1128 Aug 11, 2026
5d0f16c
fix: prevent run_tests.py deadlock when worker crashes (#5358)
wuwentao Aug 11, 2026
64fa801
[KMCompiler][Ascend] Add polygamma NPU backend support (#4809)
cheersluvs Aug 11, 2026
d4db72b
[KMCompiler] Add igammac operator with Triton kernel (#4860)
LittleShun1214 Aug 11, 2026
736fdb8
[KMCompiler][Ascend] nonzero static ascend (#4921)
chenkc2025 Aug 11, 2026
13a87b4
[KMCompiler][NVIDIA][Hygon]Add replication_pad2d_backward operator wi…
Willing2329 Aug 11, 2026
2422a2e
[KMCompiler] Add Iluvatar cholesky solve backend (#5242)
YangLong114514 Aug 11, 2026
228a826
[KMCompiler] Add Ascend cholesky solve backend (#5299)
YangLong114514 Aug 11, 2026
c1bc66c
[KMCompiler] Add MetaX cholesky solve backend (#5247)
YangLong114514 Aug 11, 2026
e79b393
fix: increase op test job timeout (#5391)
wuwentao Aug 11, 2026
3bcb766
Use Triton for metax CI (#5396)
tengqm Aug 11, 2026
1c63f26
fix testcase iluvatar (#5375)
jia-heng Aug 11, 2026
ac5cca1
Restore flagtree backend for metax CI (#5405)
tengqm Aug 11, 2026
412cfba
restore: use stable_two_path version of index_copy_
Onisen7 Aug 15, 2026
22c7c04
Update index_copy_.py
Onisen7 Aug 15, 2026
2c6783e
Update index_copy_.py
Onisen7 Aug 15, 2026
a80ea2b
Merge branch 'master' into add-ascend-index-copy
Onisen7 Aug 15, 2026
da72fcc
Delete src/flag_gems/runtime/backend/_nvidia/ops/index_copy_.py
Onisen7 Aug 15, 2026
49e64b7
Delete src/flag_gems/runtime/backend/_hygon/ops/index_copy_.py
Onisen7 Aug 15, 2026
c1e2431
Merge branch 'master' into add-ascend-index-copy
Onisen7 Aug 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/flag_gems/runtime/backend/_ascend/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
from .hstack import hstack
from .index import index
from .index_add import index_add, index_add_
from .index_copy_ import index_copy, index_copy_
from .index_select import index_select
from .isin import isin
from .linalg_cross import linalg_cross, linalg_cross_out
Expand Down Expand Up @@ -161,6 +162,8 @@
"index",
"index_add",
"index_add_",
"index_copy",
"index_copy_",
"index_select",
"isin",
"linalg_lstsq",
Expand Down
176 changes: 176 additions & 0 deletions src/flag_gems/runtime/backend/_ascend/ops/index_copy_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Copyright 2026 FlagOS Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import math

import triton
import triton.language as tl

from flag_gems.runtime import torch_device_fn
from flag_gems.utils import libentry
from flag_gems.utils import triton_lang_extension as tle

logger = logging.getLogger(__name__)


@libentry()
@triton.jit(
# runtime dims; avoid per-shape recompilation
do_not_specialize=[
"input_dim",
"index_len",
"inner_size",
"numel",
]
)
def index_copy_ascend_flat_kernel(
out_ptr,
src_ptr,
index_ptr,
input_dim,
index_len,
inner_size,
numel,
BLOCK_SIZE: tl.constexpr,
):
offsets = tle.program_id(axis=0) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = offsets < numel
offsets_i64 = offsets.to(tl.int64)

# [outer, index, inner] decomposition
inner_offset = offsets_i64 % inner_size
src_row = offsets_i64 // inner_size
index_offset = src_row % index_len
outer_offset = src_row // index_len

dst_index = tl.load(index_ptr + index_offset, mask=mask, other=0)
valid_index = (dst_index >= 0) & (dst_index < input_dim)

value = tl.load(src_ptr + offsets_i64, mask=mask, other=0.0)
dst_offset = (outer_offset * input_dim + dst_index) * inner_size + inner_offset

# Triton Ascend does not support the mask argument of tl.device_assert.
tl.store(out_ptr + dst_offset, value, mask=mask & valid_index)


@libentry()
@triton.jit(
do_not_specialize=[
"input_dim",
"index_len",
"inner_size",
"row_count",
]
)
def index_copy_ascend_row_kernel(
out_ptr,
src_ptr,
index_ptr,
input_dim,
index_len,
inner_size,
row_count,
BLOCK_SIZE: tl.constexpr,
):
# one program per [outer, index] row
row_id = tle.program_id(axis=0)
row_id_i64 = row_id.to(tl.int64)

inner_offsets = tle.program_id(axis=1) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
inner_offsets_i64 = inner_offsets.to(tl.int64)

row_mask = row_id < row_count
inner_mask = inner_offsets < inner_size

index_offset = row_id_i64 % index_len
outer_offset = row_id_i64 // index_len

dst_index = tl.load(index_ptr + index_offset, mask=row_mask, other=0)
valid_index = (dst_index >= 0) & (dst_index < input_dim)
mask = row_mask & inner_mask & valid_index

src_offsets = row_id_i64 * inner_size + inner_offsets_i64
dst_offsets = (
outer_offset * input_dim + dst_index
) * inner_size + inner_offsets_i64

value = tl.load(src_ptr + src_offsets, mask=mask, other=0.0)
tl.store(out_ptr + dst_offsets, value, mask=mask)


def _select_flat_block_size(numel, inner_size):
# narrow block keeps more programs in flight for small workloads
if inner_size == 1 and numel <= 4096:
return 256
return 1024


def _launch_index_copy(out, dim, index, src):
index_len = index.numel()
inner_size = math.prod(out.shape[dim + 1 :])
outer_size = math.prod(out.shape[:dim])

if index_len == 0 or inner_size == 0:
return

input_dim = out.size(dim)
numel = outer_size * index_len * inner_size

with torch_device_fn.device(out.device):
if inner_size <= 4:
# flat kernel for tiny suffixes
block_size = _select_flat_block_size(numel, inner_size)
grid = (triton.cdiv(numel, block_size),)
index_copy_ascend_flat_kernel[grid](
out,
src,
index,
input_dim,
index_len,
inner_size,
numel,
BLOCK_SIZE=block_size,
)
return

# row kernel for wider suffixes
row_count = outer_size * index_len
block_size = 256
grid = (row_count, triton.cdiv(inner_size, block_size))
index_copy_ascend_row_kernel[grid](
out,
src,
index,
input_dim,
index_len,
inner_size,
row_count,
BLOCK_SIZE=block_size,
)


def index_copy(inp, dim, index, src):
logger.debug("GEMS ASCEND INDEX_COPY")
dim %= inp.ndim
out = inp.clone()
_launch_index_copy(out, dim, index, src)
return out


def index_copy_(inp, dim, index, src):
logger.debug("GEMS ASCEND INDEX_COPY_")
dim %= inp.ndim
_launch_index_copy(inp, dim, index, src)
return inp