GroupedMatmul Operator Performance Optimization

Case Study

This case analyzes and optimizes the performance of GroupedMatmul operators in the per-token quantization scenario. The computation process of a GroupedMatmul operator (written in Python code) is as follows:

1
2
3
4
5
offset = 0
for i in range(g):
    mmOut = x[offset:offset + groupList[i]] * weight[i] + bias[i]
    y[offset:offset + groupList[i]] = Gelu(mmOut * scale[i] * pertokenScale[offset:offset + groupList[i]])
    offset += groupList[i]

The verification platform is Atlas A2 training product/Atlas A2 inference product.

The following operator specifications are used as an example.

Table 1 Operator specifications

input

shape

data type

format

x

(1024,1024)

int8

ND

weight

(8,1024,8192)

int8

NZ

bias

(8,8192)

int32

ND

groupList

8

int64

ND

scale

(8,8192)

float

ND

pertokenScale

1024

float

ND

y

(1024,8192)

float16

ND

The optimization methods are described below:

Obtaining Profile Data

Eight cores are used for the test. That is, numBlocks is fixed at 8 in the current performance and subsequent tiling optimization.

Use the msProf tool to obtain the operator profile data.

Analyzing Main Bottlenecks

Eight cores are used for the test. Run the msprof op command to obtain the cycle ratios of instructions.

Figure 1 ArithmeticUtilization.csv for cycle ratios of instructions (total time: 218.1 μs)

The following figure shows the instruction pipeline chart obtained by running msprof op simulator.

Figure 2 Instruction pipeline chart

Analyze the performance based on the preceding two types of data (real data and simulation data).

Developing Optimization Solutions

Verifying Optimization Benefits

Summary