You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arm CMSIS-NN s16 Softmax使用疑问:参数适配与LUT生成

关于Arm CMSIS-NN 16位Softmax函数的使用疑问

背景

我正在使用Arm CMSIS-NN的16位有符号输入Softmax函数,对应实现代码为arm_softmax_s16.c。测试用例提供了示例输入与预期输出数据,其中配置文件config_data.h里有两个参数:softmax_s16_input_mult和softmax_s16_input_left_shift,最初我不清楚它们的含义。

测试用例还用到了指数查找表(exp_lut_data.h)和一对一查找表(one_by_one_lut_data.h)。研究后发现,指数查找表是通过生成-10到0的均匀值,计算每个点的e^x后缩放到[2^1, 2^15-1]范围得到的。后来在softmax_settings.py中发现,这两个配置参数是由默认的input_scale生成的。

我的输入是通过fxpmath库得到的16位定点小数,格式为:1位符号位、6位整数位、9位小数位,具体输入如下:

#include <stdint.h>
static const int16_t logits[15] = {2019, 4958, 1855, -230, -7992, 1396, -1919, 2611, 658, 3588, 885, -4759, 3426, 1348, 5906};

我自己编写了Python函数来生成指数查找表,也可以修改该函数生成一对一查找表,函数代码如下:

def get_s16_exp_lut(input_range : list[int, int], num_vals : int, num_bits: int) -> np.array:
    """
    Takes in the specificed input range and the specified number of entries from CMSIS, 
    and computes the exponential of each point and scales it to num_bits range.                

    Example
    --------
    exp_lut = get_s16_exp_lut([-10, 0], 513, 16)
    """

    in_arr = np.linspace(input_range[0], input_range[1], num_vals)
    exp_in_arr = np.exp(in_arr)
    min_val = -2**(num_bits-1)
    max_val = 2**(num_bits - 1) - 1                
    normalised_exp = (exp_in_arr - np.min(exp_in_arr)) / (np.max(exp_in_arr) - np.min(exp_in_arr))
    scaled_exp = normalised_exp * (max_val - min_val) + min_val
    exp_lut_arr = np.round(scaled_exp).astype(np.int16)                
    return exp_lut_arr

核心疑问

  1. 我能否直接复用测试用例里的softmax_s16_input_mult和softmax_s16_input_left_shift?如果需要为我的输入场景计算专属的输入乘数和左移位值,具体该怎么操作?
  2. 在我的使用场景中,是否必须准备以下内容,还是可以直接复用现有资源:
    • 缩放到[-2^15, 2^15-1]范围的指数查找表
    • 缩放到[-2^15, 2^15-1]范围的一对一查找表
    • 输入乘数(Input Multiplier)
    • 输入左移位值(Input Left shift)

内容的提问来源于stack exchange,提问作者Caesar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 00:24:52