GroupTopkOperation

Applicable Products

Hardware Model

Supported

Atlas 350 accelerator cards

x

Atlas A3 inference products/Atlas A3 training products

√

Atlas A2 training products/Atlas A2 inference products

√

Atlas training products

x

Atlas inference products

x

Atlas 200I/500 A2 inference products

x

Description

(Hyperparameter of the GroupTopk operator) Divides the data of dimension 1 in input tensor 0 (which has two dimensions: dimension 0 and dimension 1) into groupNum groups, selects the maximum value of each group and the first k values in each group, and sets all data of other groups to 0.

Definition

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
struct GroupTopkParam {
    int32_t groupNum = 1;
    int32_t k = 0;
    enum GroupMultiFlag : uint16_t {
        UNDEFINED = 0, 
        SUM_MULTI_MAX  
    };
    uint16_t n = 1; 
    uint8_t rsv[12] = {0};
};

Parameters

Member

Type

Default Value

Value Range

Required (Yes/No)

Description

groupNum

int32_t

1

[1, expert_num]

No

Number of token groups. Note: expert_num is the value of the dimension dim_1 for the input parameter token.

k

int32_t

0

[1, groupNum]

Yes

Number of top K experts. The value must be greater than or equal to 1.

groupMultiFlag

uint16_t

UNDEFINED

[0,1]

No

Enumerated value, which specifies the calculation type of the value in a group.

  • UNDEFINED: default type. The maximum value in each group is used.
  • SUM_MULTI_MAX: sum of n maximum values in each group. The parameter n needs to be set.

n

uint16_t

1

[1, expert_num/groupNum]

No

Number of values in each group.

  • When groupMultiFlag is set to 1, the value of n must be greater than 0.
  • This parameter does not take effect when groupMultiFlag is set to 0.

rsv[12]

uint8_t

{0}

-

-

Reserved

Input

Parameter

Dimension

Data Type

Format

Description

token

[dim_0, dim_1]

float16/bf16

ND

Input tensor 0, 2D tensor. dim_0 indicates the number of tokens, and dim_1 indicates the total number of experts.

idxArr

[1024]

int32

ND

Input tensor 1, 1D tensor, used for auxiliary computation. It is an arithmetic sequence [0,1,2, ...,1023] with a fixed length of 1024.

Output

Parameter

Dimension

Data Type

Format

Description

output

[dim_0, dim_1]

float16/bf16

ND

Output tensor. There is only one output tensor, which is the output of the input tensor 0 written in place. The data type is the same as that of input tensor 0.

Constraints

1 ≤ expert_num ≤ 1024 and expert_num ≥ groupNum ≥ k ≥ 1, where expert_num can be exactly divided by groupNum.

Basic Functions

  • Function

    Groups the input expert scores, selects the maximum value in each group, selects the top k groups based on the maximum value in each group, and sets the scores of other groups to 0.

  • Formula

    For each token, the token data is evenly divided into groupNum groups.

    1. Obtain the maximum values of each group.
    2. Sort the maximum values of each group in descending order.
    3. Obtain the indexes of the first k groups and set the data of the groups corresponding to the other indexes to 0.
  • Computational Graph

    When groupMultiFlag is set to 0, the calculation process is as follows:

    Figure 1 Function example
  • Parameters

    For details, see Parameters. The value of groupMultiFlag must be UNDEFINED.

  • Input

    Parameter

    Dimension

    Data Type

    Format

    Description

    token

    [num_tokens, expert_num]

    float16/bf16

    ND

    2D tensor.

    • Dimension 0: number of tokens.
    • Dimension 1: total number of experts.

    idxArr

    [1024]

    int32

    ND

    1D tensor, used for auxiliary computation. It is an arithmetic sequence [0, 1, 2, ...,1023] with a fixed length of 1024.

  • Output

    Parameter

    Dimension

    Data Type

    Format

    Description

    output

    [num_tokens, expert_num]

    float16/bf16

    ND

    Output of the input token written in place. The data type is the same as that of the input token.

  • Example
    1
    2
    3
    4
    // Parameter construction
    atb::infer::GroupTopkParam param;
    param.groupNum = 8;
    param.k = 1;
    

Re-sorting and Selecting the First N Values in a Group

  • Function

    Groups the input expert scores, sorts the scores in each group in descending order, selects the first n values, sums the values, selects the top k groups based on the sum, and sets the scores of other groups to 0.

  • Formula

    For each token, the token data is evenly divided into groupNum groups.

    1. Sort the values in each group in descending order and obtain the first n values.
    2. Sum the n values.
    3. Sort the groups in descending order based on the obtained sum.
    4. Obtain the indexes of the first k groups and set the data of the groups corresponding to the other indexes to 0.

    When n is set to 1, the Basic Functions scenario applies.

  • Parameters

    For details, see Parameters. groupMultiFlag must be set to SUM_MULTI_MAX.

  • Input

    Parameter

    Dimension

    Data Type

    Format

    Description

    token

    [num_tokens, expert_num]

    float16/bf16

    ND

    2D tensor.

    • Dimension 0: number of tokens.
    • Dimension 1: total number of experts.

    idxArr

    [1024]

    int32

    ND

    1D tensor, used for auxiliary computation. It is an arithmetic sequence [0, 1, 2, ...,1023] with a fixed length of 1024.

  • Output

    Parameter

    Dimension

    Data Type

    Format

    Description

    output

    [num_tokens, expert_num]

    float16/bf16

    ND

    Output of input tensor 0 written in place. The data type is the same as that of input tensor 0.

  • Example
    1
    2
    3
    4
    5
    6
    // Parameter construction
    atb::infer::GroupTopkParam param;
    param.groupNum = 8;
    param.k = 1;
    param.groupMultiFlag = 1;
    param.n = 2;