Clamp

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product/Atlas A3 inference product

x

Atlas A2 training product/Atlas A2 inference product

x

Atlas 200I/500 A2 inference product

x

Atlas inference product AI Core

x

Atlas inference product Vector Core

x

Atlas training product

x

Function Usage

The output is the input values clamped to the range [min, max], where values greater than max are set to max, values less than min are set to min, and all other values (including NaN) remain unchanged. If min is greater than max, all values (except NaN) are set to max. min and max can be a Scalar or LocalTensor value.

Prototype

1
2
template <const ClampConfig& config = DEFAULT_CLAMP_CONFIG, typename T, typename U, typename S>
__aicore__ inline void Clamp(const LocalTensor<T>& dst, const LocalTensor<T>& src, const U& min, const S& max, const uint32_t count)

Parameters

Table 1 Template parameters

Parameter

Description

config

Clamp algorithm configuration. This is an optional parameter of the ClampConfig type. The code below describes the definition.

isReuseSource: This parameter is reserved. Pass the default value false.

T

Operand data type.

For the Atlas 350 Accelerator Card, the supported data types are int8_t, uint8_t, int16_t, uint16_t, half, bfloat16_t, int32_t, uint32_t, float, int64_t, and uint64_t.

U

LocalTensor or Scalar type. The type is automatically inferred based on the input parameter min. You do not need to set this parameter, but ensure that min meets the data type constraints.

For the Atlas 350 Accelerator Card, the supported data types are int8_t, uint8_t, int16_t, uint16_t, half, bfloat16_t, int32_t, uint32_t, float, int64_t, and uint64_t.

S

LocalTensor or Scalar type. The type is automatically inferred based on the input parameter max. You do not need to set this parameter, but ensure that max meets the data type constraints.

For the Atlas 350 Accelerator Card, the supported data types are int8_t, uint8_t, int16_t, uint16_t, half, bfloat16_t, int32_t, uint32_t, float, int64_t, and uint64_t.

1
2
3
struct ClampConfig {
    bool isReuseSource;
};
Table 2 API parameters

Parameter

Input/Output

Description

dst

Output

Destination operand.

The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT.

src

Input

Source operand.

The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT.

The source operand must have the same data type as the destination operand.

min

Input

Data lower limit. The type can be Scalar or LocalTensor. When the type is LocalTensor, the supported TPosition is VECIN, VECCALC, or VECOUT.

The source and destination operands must have the same data type.

max

Input

Data upper limit. The type can be Scalar or LocalTensor. When the type is LocalTensor, the supported TPosition is VECIN, VECCALC, or VECOUT.

The source and destination operands must have the same data type.

count

Input

Number of elements involved in the computation.

Returns

None

Constraints

Examples

AscendC::LocalTensor<half> dst, src;
uint32_t count = 512;
half min = 30;
half max = 60;
AscendC::Clamp(dst, src, min, max, count); 
Result example:
1
2
3
4
5
6
7
8
Input (src):
[13, 78, 35, 95, 83,  2,  2, 95, 51, 73, 98,  3, 55, 32, 61,  2, 40, 26, 95, ... 63]
Input (min):
[30]
Input (max):
[60]
Output (dst):
[30, 60, 35, 60, 60, 30, 30, 60, 51, 60, 60, 30, 55, 32, 60, 30, 40, 30, 60, ... 60]