已合并
feat(op): Add custom op support for __builtin_index_select #1113
feat(op): Add custom op support for __builtin_index_select #1113
已合并
candyhong创建于 1月16日
candyhong成员
1月16日

Background

This PR adds custom op support for __builtin_index_select, whose semantics align with torch.index_select.

This change improves the expressiveness of index-related kernels and enables more general indexing patterns in Triton custom ops.


Interface Introduction

The operator gathers elements from a source tensor in Global Memory (GM) into an output tensor in Unified Buffer (UB) using indices provided in UB along a specified dimension.

Usage Example

@triton.jit
def builtin_index_select_kernel(src_ptr, index_ptr, out_ptr):
    # Define 2x2 tile indices for output tensor
    r = tl.arange(0, 2)[:, None]  # Row indices: shape [2, 1]
    c = tl.arange(0, 2)[None, :]  # Column indices: shape [1, 2]

    # Load index tensor (shape [2]) from GM to UB
    idx = tl.load(index_ptr + tl.arange(0, 2))
    # Initialize empty 2x2 output tile in UB (default value: 0)
    dst = tl.full((2, 2), 0, dtype=tl.float32)

    # Invoke __builtin_index_select custom op to gather elements
    out_tile = al.custom(
        "__builtin_index_select",
        src_ptr,          # Pointer to source tensor in GM
        idx,              # Index tensor (in UB) for gathering
        dim=0,            # Dimension to gather along
        bound=4,          # Upper bound for valid index values (out-of-bound check)
        end_offset=(2, 2),# End offsets of each dimension for the index tensor
        start_offset=(0, 0), # Start offsets of each dimension for the source tensor
        src_stride=(4, 1),# Stride of each dimension for the source tensor in GM
        out=dst           # Output tensor (in UB) to store gathered elements
    )

    # Store the gathered tile from UB to output tensor in GM
    tl.store(out_ptr + r * 2 + c, out_tile)

Checklist

  • Select one of the following.
      • /test for lit tests
      • /unittest for C++ tests
      • /python/test for end-to-end tests
  • Select one of the following.
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 candyhong 的贡献)
Ccandyhong成员
1月16日 创建了 pull request,commit b17aa262
ascend-robot
ascend-robot成员
1月16日 评论:

Thank your for your pull-request.

The full list of commands accepted by me can be found at here.

You can get sig-info at here

likedislike
ascend-robot
ascend-robot成员
1月16日 评论:

以下是根据您提交的修改文件推荐的Reviewer和Committer序列,需各模块评审通过后方可合入

Module List Reviewers Committers
repo-Ascend/triton-ascend gymgit1, HaiLijuan, kang-ingu, chnjz233, saruagithub zhucehw, ccdedreams, HaiLijuan, wangtao489, shijingchang
likedislike
ascend-robotascend-robot成员
1月16日 添加了label:ascend-cla/yes
此处折叠了45条消息 查看更多
Wwangzhanpeng5
1月16日 解决了最后一个问题
wangzhanpeng5
1月16日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
1月16日 添加了label:lgtm
ascend-robot
ascend-robot成员
1月16日 评论:

Review Guide

This Pull-Request Passes Review.
Committers who wrote a comment of /approve are: zhang-chunli01.
Reviewers who wrote a comment of /lgtm are: zhang-chunli01, wangzhanpeng5.

likedislike
ascend-robotascend-robot成员
1月16日 合入了pull request