---
title: PadOperation
description: "| 硬件型号 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendtb/ascendtb_01_0069.html
sourcePath: /source/zh/canncommercial/900/API/ascendtb/ascendtb_01_0069.html
indexId: 4e2679a743c4555b52d2003a60005ebf4e0657cac7b490d965f29b36f4af9a0b64
---
# PadOperation

#### 产品支持情况

| 硬件型号 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | x |
| Atlas A3 推理系列产品 / Atlas A3 训练系列产品 | √ |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | √ |
| Atlas 训练系列产品 | x |
| Atlas 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | x |


#### 功能说明

对于输入input_ids，取出每个batch最后一个有效token的embedding向量。


#### 使用场景

经过pad（末尾填充0后）之后，input_ids中有batch个token id 序列，输出时，从每个batch中拿出最后一个token的词向量，并且拼接在一起。

使用示例：

tmp_out [token_num,hidden_dim] 为[37,16]

padding_offset[1,token_num] 为[37]

seqlen[batch,1] 为[[8],[13],[16]]

则input_ids[3,16]为：

```
[[1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0],[1,2,3,4,5,6,7,8,9,10,11,12,13,0,0,0],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]]
```

max_seq_len=16

则output[3,16]为：

```
[[1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0],[1,2,3,4,5,6,7,8,9,10,11,12,13,0,0,0],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]]
```

应用场景：

在自然语言处理（NLP）中，输入的句子长度通常不同。为了让它们在一个 batch 中处理，我们需要将它们填充（pad）到相同的长度。填充的方式通常是在句子的末尾添加零（0），直到所有句子长度相同。


#### 定义

```
struct PadParam {
    uint8_t rsv[8] = {0};
};
```


#### 参数列表

| 成员名称 | 类型 | 默认值 | 描述 |
| --- | --- | --- | --- |
| rsv[8] | uint8\_t | {0} | 预留参数。 |


#### 输入

| 参数 | 维度 | 数据类型 | 格式 | 描述 |
| --- | --- | --- | --- | --- |
| tmp\_out | [token\_num, hidden\_dim] | float16 | ND | 每一个token对应的embedding向量。 |
| padding\_offset | [1, token\_num] | int32 | ND | 由seq\_len和cum\_offsets\_out组合计算得到。 |
| seq\_len | [batch, 1] | int32 | ND | 每个batch中的有效token数量。 |
| input\_ids | [batch, max\_seq\_len] | int64 | ND | 经过pad（末尾填充0）之后，batch个token id序列。 |


#### 输出

| 参数 | 维度 | 数据类型 | 格式 | 描述 |
| --- | --- | --- | --- | --- |
| output | [batch, hidden\_dim] | float16 | ND | 取出的每个batch最后一个有效token的embedding向量。 |


#### 约束说明

1<=batch<=64
