舍入和饱和模式 - 2023.2 简体中文

AI 引擎内核与计算图编程指南 (UG1079)

Document ID
UG1079
Release Date
2023-12-04
Version
2023.2 简体中文

aie::set_rounding()aie::set_saturation() API 用于设置 to_vector 运算的舍入和饱和模式。

aie::rounding_mode 包括:

floor
在 LSB 处截位,始终向下舍入(趋向负无穷)。默认。
ceil
始终向上舍入(趋向正无穷)。
positive_inf
半舍入趋向正无穷。
negative_inf
半舍入趋向负无穷。
symmetric_inf
半舍入趋向无穷(远离 0)。
symmetric_zero
半舍入趋向零(远离无穷)。
conv_even
半舍入趋向最接近的偶数。
conv_odd
半舍入趋向最接近的奇数。

aie::saturation_mode 包括:

none
不执行饱和,该值于 MSB 侧执行截位。默认。
saturate
控制饱和。饱和会对以下范围内的 n 位有符号值进行舍入
[- ( 2(n-1) ) : + 2(n-1) - 1 ]
。例如,如果 n=8,那么范围为 [-128:127]。
symmetric
控制对称饱和。对称饱和会对以下范围内的 n 位有符号值进行舍入
[- ( 2(n-1) -1 ) : + 2(n-1) - 1 ]
。例如,如果 n=8,那么范围为 [-127:127]。

舍入和饱和设置适用于 AI 引擎拼块。aie::get_roundingaie::get_saturation 方法可用于获取当前舍入和饱和模式。

以下提供的代码示例演示了如何设置和获取舍入和饱和模式:
aie::set_rounding(aie::rounding_mode::ceil);
aie::set_saturation(aie::saturation_mode::saturate);
aie::rounding_mode current_rnd=aie::get_rounding();
aie::saturation_mode current_sat=aie::get_saturation();
...
aie::set_rounding(aie::rounding_mode::floor);
aie::set_saturation(aie::saturation_mode::none);
注释: 舍入和饱和设置均为粘性设置。这表示 AI 引擎拼块会保留其模式直至更改。如果内核的运行时比率小于 1,则可与其他内核共享拼块,如果舍入和饱和模式影响内核操作,那么内核必须在内核代码中而不是在构造器或初始化函数中设置这些模式。这将确保当相同拼块上的其他内核使用不同的舍入或饱和模式值时,可保证舍入和饱和模式。