DynamicNTKOperation

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

√

Atlas 200I/500 A2 inference products

x

Description

Generates the rotation matrix (that is, the sin/cos matrix in the following formula) used by the RoPE operator when the inference length is greater than the training sequence length.

If the inference length is less than or equal to the training sequence length, no interpolation is performed. If the inference length is greater than the training sequence length, the base dynamic interpolation is amplified.

An outer product is performed on the position information positionIds and invFreq of the input token sequence, and then a cos/sin operation is performed to obtain a final result of rotary embedding.

Operator Context

Figure 1 Location of the DynamicNTK operator in the model

The DynamicNTK operator outputs sin and cos.

Input q, k, sin, and cos to the Rope operator and output the rotated q and k.

Operator Function Analysis

DynamicNTK is used to perform special processing on embedding when the inference length is greater than the training length. The base parameter is scaled based on the seqLen to generate new cos and sin. Models such as Llama, Qwen, and Yarn have different cos and sin generation solutions. The DynamicNTK operator implements the common processing logic of different models, and the differentiated processing logic is implemented by the models.

  • Operator calculation formula

    An outer product is performed on the position information positionIds and invFreq of the input token sequence, and then a cos/sin operation is performed to obtain a final result of rotary embedding.

  • Details:

    Obtain positionIds and invFreq of each batch based on the seqLen of each batch.

    For each batch:
    freq = torch.einsum('i,j->ij', positionIds, invFreq)
    emb = torch.cat((freq, freq), dim=-1)
    sinOut = emb.sin()
    cosOut = emb.cos()

Definition

1
2
3
4
struct DynamicNTKParam {
    aclDataType outDataType = ACL_DT_UNDEFINED;
    uint8_t rsv[12] = {0};
};

Parameters

Member

Type

Default Value

Description

outDataType

aclDataType

ACL_DT_UNDEFINED

Parameter for selecting the output data type.

  • ACL_DT_UNDEFINED: default value. This default type is unavailable. You need to select the other two output data types.
  • ACL_FLOAT16: Sets the output data type to float16.
  • ACL_BF16: Sets the output data type to bf16.

rsv[12]

uint8_t

{0}

Reserved

Input

Parameter

Dimension

Data Type

Format

Description

positionIds

[ntokens]

int32

ND

Token position sequence of multiple batches. The format is as follows: [0,1,2,…,batch1Len-1,0,1,2…batch2Len-1,0,1,2…].

InvFreqs

[batch, headSize/2]

float

ND

Position inverse frequency of each batch.

seqLens

[batch]

int32

ND

Sequence length of each batch.

The formula for calculating InvFreqs is as follows:

Output

Parameter

Dimension

Data Type

Format

Description

sin

[ntokens, headSize]

float16/bf16

ND

Output rotary embedding sin matrix.

cos

[ntokens, headSize]

float16/bf16

ND

Output cos matrix. The data type and shape must be the same as those of the output sin.

Constraints

  • headSize (the last dimension of the output tensor) is less than or equal to 2048 and is a multiple of 32.
  • The value of batch is less than or equal to 16.
  • The value of nTokens (dimension of positionIds) is less than or equal to 256000.
  • Value range of the InvFreqIn array: [0, 1). The value in the seqLens array is greater than 0, and the sum of the arrays is nTokens.