pypto.fillpad#

产品支持情况#

  • Ascend 950PR:支持

  • Atlas A3 训练系列产品/Atlas A3 推理系列产品:支持

  • Atlas A2 训练系列产品/Atlas A2 推理系列产品:支持

功能说明#

对输入Tensor进行填充(Padding)。

和pad不同,此接口不会改变张量的形状,他将填充区域(即超过validshape的区域)用指令的值进行填充。当前实现支持输入1-2维tensor,进行常量(Constant)模式的右侧(Right)和底部(Bottom)填充。

函数原型#

fillpad(input: Tensor, mode: str = "constant", value: Union[float, int] = 0) -> Tensor

参数说明#

参数名

输入/输出

说明

input

输入

需要进行填充的源操作数。
支持的类型为:Tensor。
Tensor支持的数据类型为:DT_FP32、DT_FP16、DT_BF16、DT_INT8、DT_INT16、DT_INT32、DT_UINT8、DT_UINT16、DT_UINT32。
不支持空Tensor;Shape支持1-2维;Shape Size不大于2147483647(即INT32_MAX)。

mode

输入

填充模式。
支持的类型为:str。
可选值为'constant''reflect''replicate''circular'
默认值:'constant'
注意:当前仅支持'constant'模式。

value

输入

当填充模式为常量填充('constant')时的填充值。
支持的类型为:float或int。
对于浮点类型(DT_FP32、DT_FP16、DT_BF16),支持任意浮点数值,包括-infinf0.0以及其他任意浮点数(如1.0-1.00.5等)。
对于整型类型(DT_INT8、DT_INT16、DT_INT32、DT_UINT8、DT_UINT16、DT_UINT32),支持任意整数值。
默认值:0

返回值说明#

返回输出Tensor,Tensor的数据类型和input相同,Shape也和input相同。

约束说明#

  1. mode当前仅支持'constant'(常量填充)模式,其他模式暂不支持。

  2. value支持任意浮点数值或整数值,填充值的数据类型会自动转换为与输入Tensor一致。

  3. 如果input不是Tensor类型,将抛出TypeError

  4. Tensor类型输入不支持TileOpFormat.TILEOP_NZ格式。

调用示例#

TileShape设置示例#

说明:调用该operation接口前,应通过set_vec_tile_shapes设置TileShape。

TileShape维度应和输出一致。

示例1:输入input shape为[m, n],则输出shape为[m, n],TileShape设置为[m1, n1],则m1, n1分别用于切分输出的m, n轴。

pypto.set_vec_tile_shapes(4, 16)

接口调用示例#

a = pypto.tensor([4, 4], pypto.DT_FP32)
out = pypto.fillpad(a, "constant", "-inf")

结果示例如下:

# 输入数据t4d (逻辑shape为 [4, 4]):
[[1.0, 2.0, 0.0, 0.0],
[3.0, 4.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0]]

# 输出数据out (逻辑shape为 [4, 4]):
[[1.0, 2.0, -inf, -inf],
[3.0, 4.0, -inf, -inf],
[-inf, -inf, -inf, -inf],
[-inf, -inf, -inf, -inf]]