开启固定权重类输入地址功能后出现精度问题
问题现象描述
图模式场景下执行推理脚本时,若开启固定权重类输入地址功能(config.experimental_config.frozen_parameter=True),发现图模式和Eager模式下模型精度不一致,图模式下精度存在问题,关闭该功能后正常。
可能的原因
- parameter对象的输入不符合预期,原始数据有误。
- 模型中存在非连续的parameter对象,导致计算的结果错误。
处理步骤
- 出现精度问题后,首先排查是否是Dynamo框架导致FX图有误。
先验证Eager模式执行效果,若执行后的模型精度正确,那么原因就是NPU后端图模式导致。
- 确认问题来源于图模式后,分别对比图模式下data dump数据(算子输入输出dump功能(图模式))和Eager模式下data dump数据(算子输入输出dump功能(Eager模式))。对比后发现原始输入存在差异,说明图模式处理输入参数时已经存在问题。
- 为进一步确认问题原因,开启TorchAir的Python侧日志,观察其处理输入的日志,发现该模型的输入input 0连续性不符合预期,是非连续的,如下所示
1 2 3 4 5 6
[DEBUG] TORCHAIR(2250956,python):2025-02-06 15:44:44 runtime inputs [DEBUG] TORCHAIR(2250956,python):2025-02-06 15:44:44 input 0: <class 'torch.Tensor'>(torch.Size([896, 1152]), torch.int8, contiguous=False) [DEBUG] TORCHAIR(2250956,python):2025-02-06 15:44:44 input 1: <class 'torch.Tensor'>(torch.Size([1152]), torch.bfloat16, contiguous=True) [DEBUG] TORCHAIR(2250956,python):2025-02-06 15:44:44 input 2: <class 'torch.Tensor'> (torch.Size([1]), torch.float32, contiguous=True) [DEBUG] TORCHAIR(2250956,python):2025-02-06 15:44:44 input 3: <class 'torch.Tensor'> (torch.Size([1, 896]), torch.int8, contiguous=True) [INFO] TORCHAIR(2250956,python):2025-02-06 15:44:44 input process func is:
- 修改推理脚本,将非连续输入parameter对象转为连续输入。
非连续输入无法直接传入GE进行计算,因此先确认该输入在模型中的位置,在脚本中使用“contiguous()”完成转换。
父主题: 定界与定位案例