---
title: Maxs
description: "| 产品 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendcopapi/atlasascendc_api_07_0056.html
sourcePath: /source/zh/canncommercial/900/API/ascendcopapi/atlasascendc_api_07_0056.html
indexId: 6f9d9092492b2f120db488871fcba739c2dc35f91e5a67296441fb68ba7ab58c76
---
# Maxs

#### 产品支持情况

| 产品 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | √ |
| Atlas A3 训练系列产品 / Atlas A3 推理系列产品 | √ |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | √ |
| Atlas 推理系列产品 AI Core | √ |
| Atlas 推理系列产品 Vector Core | x |
| Atlas 训练系列产品 | x |


#### 功能说明

源操作数矢量内每个元素与标量相比，如果比标量大，则取源操作数值，比标量的值小，则取标量值。

计算公式如下：


#### 函数原型

- tensor前n个数据计算
  1 2 template <typename T, bool isSetMask = true> __aicore__ inline void Maxs(const LocalTensor<T>& dst, const LocalTensor<T>& src, const T& scalarValue, const int32_t& count)

- tensor高维切分计算

  - mask逐bit模式
    1 2 template <typename T, bool isSetMask = true> __aicore__ inline void Maxs(const LocalTensor<T>& dst, const LocalTensor<T>& src, const T& scalarValue, uint64_t mask[], const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)

  - mask连续模式
    1 2 template <typename T, bool isSetMask = true> __aicore__ inline void Maxs(const LocalTensor<T>& dst, const LocalTensor<T>& src, const T& scalarValue, uint64_t mask, const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)


dst和src使用TensorTrait类型时，其数据类型TensorTrait和scalarValue的数据类型（对应TensorTrait中的LiteType类型）不一致。因此新增模板类型U表示scalarValue的数据类型，并通过std::enable_if检查T中萃取出的LiteType和U是否完全一致，一致则接口通过编译，否则编译失败。接口原型定义如下:

- tensor前n个数据计算
  1 2 template <typename T, typename U, bool isSetMask = true, typename Std::enable_if<Std::is_same<PrimT<T>, U>::value, bool>::type = true> __aicore__ inline void Maxs(const LocalTensor<T>& dst, const LocalTensor<T>& src, const U& scalarValue, const int32_t& count)

- tensor高维切分计算

  - mask逐bit模式
    1 2 template <typename T, typename U, bool isSetMask = true, typename Std::enable_if<Std::is_same<PrimT<T>, U>::value, bool>::type = true> __aicore__ inline void Maxs(const LocalTensor<T>& dst, const LocalTensor<T>& src, const U& scalarValue, uint64_t mask[], const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)

  - mask连续模式
    1 2 template <typename T, typename U, bool isSetMask = true, typename Std::enable_if<Std::is_same<PrimT<T>, U>::value, bool>::type = true> __aicore__ inline void Maxs(const LocalTensor<T>& dst, const LocalTensor<T>& src, const U& scalarValue, uint64_t mask, const uint8_t repeatTime, const UnaryRepeatParams& repeatParams)


#### 参数说明


**表1 模板参数说明**

| 参数名 | 描述 |
| --- | --- |
| T | 操作数数据类型。 Atlas 推理系列产品 AI Core，支持的数据类型为：half、int16\_t、float、int32\_t。 Atlas A2 训练系列产品 / Atlas A2 推理系列产品 ，支持的数据类型为：half、int16\_t、float、int32\_t。 Atlas A3 训练系列产品 / Atlas A3 推理系列产品 ，支持的数据类型为：half、int16\_t、float、int32\_t。 Atlas 200I/500 A2 推理产品 ，支持的数据类型为：half、int16\_t、float、int32\_t。 Atlas 350 加速卡，支持的数据类型为：uint8\_t、int8\_t、half、bfloat16\_t、int16\_t、float、int32\_t、uint64\_t、int64\_t。 |
| U | scalarValue数据类型。 Atlas 推理系列产品 AI Core，支持的数据类型为：half、int16\_t、float、int32\_t。 Atlas A2 训练系列产品 / Atlas A2 推理系列产品 ，支持的数据类型为：half、int16\_t、float、int32\_t。 Atlas A3 训练系列产品 / Atlas A3 推理系列产品 ，支持的数据类型为：half、int16\_t、float、int32\_t。 Atlas 200I/500 A2 推理产品 ，支持的数据类型为：half、int16\_t、float、int32\_t。 Atlas 350 加速卡，支持的数据类型为：uint8\_t、int8\_t、half、bfloat16\_t、int16\_t、float、int32\_t、uint64\_t、int64\_t。 |
| isSetMask | 是否在接口内部设置mask模式和mask值。 true，表示在接口内部设置。 tensor高维切分计算API/tensor前n个数据计算API内部使用了mask的Normal模式/Counter模式(https://www.hiascend.comdocument/detail/zh/canncommercial/900/programug/Ascendcopdevg/atlas\_ascendc\_10\_0024.html)，一般情况下保持isSetMask默认值即可，表示在API内部根据开发者传入的mask/count参数进行mask模式和mask值的设置。 false，表示在接口外部设置。 针对tensor高维切分计算接口，对性能要求较高的部分场景下，开发者需要使用SetMaskNorm/SetMaskCount设置mask模式，并通过SetVectorMask接口设置mask值。本接口入参中的mask值必须设置为MASK\_PLACEHOLDER。 针对tensor前n个数据计算接口，对性能要求较高的部分场景下，开发者需要使用SetMaskCount设置mask模式为Counter模式，并通过SetVectorMask接口设置mask值。本接口入参中的count不生效，建议设置成1。 针对以下型号，tensor前n个数据计算API中的isSetMask参数不生效，保持默认值即可。 针对Atlas 350 加速卡 Atlas 200I/500 A2 推理产品 |


**表2 参数说明**

| 参数名称 | 类型 | 说明 |
| --- | --- | --- |
| dst | 输出 | 目的操作数。 类型为LocalTensor，支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 |
| src | 输入 | 源操作数。 类型为LocalTensor，支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 数据类型需要与目的操作数保持一致。 |
| scalarValue | 输入 | 源操作数，数据类型需要与目的操作数Tensor中元素的数据类型保持一致。 |
| count | 输入 | 参与计算的元素个数。 |
| mask/mask[] | 输入 | mask(https://www.hiascend.comdocument/detail/zh/canncommercial/900/programug/Ascendcopdevg/atlas\_ascendc\_10\_0022.html\#ZH\-CN\_TOPIC\_0000002562282205\_\_zh\-cn\_topic\_0000002267504656\_zh\-cn\_topic\_0000001764162593\_section4252658182)用于控制每次迭代内参与计算的元素。 逐bit模式：可以按位控制哪些元素参与计算，bit位的值为1表示参与计算，0表示不参与。 mask为数组形式，数组长度和数组元素的取值范围和操作数的数据类型有关。当操作数为16位时，数组长度为2，mask[0]、mask[1]∈[0, 264\-1]并且不同时为0；当操作数为32位时，数组长度为1，mask[0]∈(0, 264\-1]；当操作数为64位时，数组长度为1，mask[0]∈(0, 232\-1]。 例如，mask=[8, 0]，8=0b1000，表示仅第4个元素参与计算。 连续模式：表示前面连续的多少个元素参与计算。取值范围和操作数的数据类型有关，数据类型不同，每次迭代内能够处理的元素个数最大值不同。当操作数为16位时，mask∈[1, 128]；当操作数为32位时，mask∈[1, 64]；当操作数为64位时，mask∈[1, 32]。 |
| repeatTime | 输入 | 重复迭代次数。 矢量计算单元，每次读取连续的256Bytes数据进行计算，为完成对输入数据的处理，必须通过多次迭代（repeat）才能完成所有数据的读取与计算。repeatTime表示迭代的次数。 关于该参数的具体描述请参考高维切分API(https://www.hiascend.comdocument/detail/zh/canncommercial/900/programug/Ascendcopdevg/atlas\_ascendc\_10\_0022.html)。 |
| repeatParams | 输入 | 元素操作控制结构信息，具体请参考UnaryRepeatParams。 |


#### 返回值说明

无


#### 约束说明

- 操作数地址对齐要求请参见  通用地址对齐约束
。
- 操作数地址重叠约束请参考  通用地址重叠约束
。

- 针对Atlas 350 加速卡，int8_t/uint8_t/uint64_t/int64_t数据类型仅支持tensor前n个数据计算接口。


#### 调用示例

更多样例可参考LINK。

- tensor高维切分计算样例-mask连续模式
  1 2 3 4 5 6 uint64_t mask = 128; int16_t scalar = 2; // repeatTime = 4, 单次迭代处理128个数，计算512个数需要迭代4次 // dstBlkStride, srcBlkStride = 1, 每个迭代内src0参与计算的数据地址间隔为1个datablock，表示单次迭代内数据连续读取和写入 // dstRepStride, srcRepStride = 8, 相邻迭代间的地址间隔为8个datablock，表示相邻迭代间数据连续读取和写入 AscendC::Maxs(dstLocal, srcLocal, scalar, mask, 4, { 1, 1, 8, 8 });


- tensor高维切分计算样例-mask逐bit模式
  1 2 3 4 5 6 uint64_t mask[2] = { UINT64_MAX, UINT64_MAX }; int16_t scalar = 2; // repeatTime = 4, 单次迭代处理128个数，计算512个数需要迭代4次 // dstBlkStride, srcBlkStride = 1, 每个迭代内src0参与计算的数据地址间隔为1个datablock，表示单次迭代内数据连续读取和写入 // dstRepStride, srcRepStride = 8, 相邻迭代间的地址间隔为8个datablock，表示相邻迭代间数据连续读取和写入 AscendC::Maxs(dstLocal, srcLocal, scalar, mask, 4, {1, 1, 8, 8});

- tensor前n个数据计算样例
  1 2 int16_t scalar = 2; AscendC::Maxs(dstLocal, srcLocal, scalar, 512);


结果示例如下：
```
输入数据src0Local：[1 2 3 ... 512]
输入数据scalar = 2
输出数据dstLocal：[2 2 3 ... 512]
```
