Skip to content

[Core:Bugfix] Track floating-point shape inputs in Module resize cache - #4974

Open
vklonghml wants to merge 2 commits into
alibaba:masterfrom
vklonghml:feature/paraformer-dynamic-shapes
Open

vklonghml wants to merge 2 commits into
alibaba:masterfrom
vklonghml:feature/paraformer-dynamic-shapes

Conversation

@vklonghml

@vklonghml vklonghml commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

修复参与形状推导的浮点输入内容变化后,Module 仍复用旧输出形状的问题。

根因

StaticModule::_resize() 通过缓存输入内容避免重复 resize,但原实现跳过所有浮点输入。Range 的起点、上限和步长允许为浮点值;当标量上限改变、输入张量形状不变时,可能错误复用上一次输出形状。例如,连续输入上限 3、13,实际输出长度却为 3、3。

Paraformer 导出的 decoder mask 也会触发此问题:整数序列长度经过 ReduceMax 和 Cast 转成浮点数后传给 Range,因此外部长度输入为整数并不能避免该错误。

修复

依据已有的 mInputNeedCPU 标记,将参与形状推导的浮点输入纳入原有的限长字节比较。普通浮点数据输入仍被跳过,避免大 embedding 引起不必要的 resize。保留主机缓冲区缺失或过大时的回退行为,以及已有的强制 resize 开关。

新增 expr/ModuleRangeContent 回归测试,覆盖浮点上限、整数转浮点上限、整数 Range,以及长度重复、增长和缩短的连续调用。

验证

  • 修复前,新增测试在第二个上限值处失败。
  • 修复后,CPU normal/low 精度、1/4 线程下,7 项 expr/Module 测试及 op/range 均通过。
  • 使用未改写 mask 的原始 Paraformer decoder 图,分别验证未量化、INT8、INT4 权重,连续输入 token 长度 3/13/13/2/5。每次调用的全部 18 个输出均与新加载的 Module 一致:浮点最大绝对误差为 0,整数输出完全一致。
  • MNN 和测试程序编译通过,git diff --check 通过。

Module

Core / Express

Type

  • Feature
  • Bugfix
  • Perf
  • Refact
  • Style
  • Doc
  • Test
  • Chore

Checklist

  • 提交信息符合 [Module:Type] Description 格式
  • 代码编译通过
  • 已在相关运行环境完成验证
  • 不包含无关的格式或风格修改

@wangzhaode wangzhaode self-assigned this Oct 15, 2026
@wangzhaode

Copy link
Copy Markdown
Collaborator

Thanks for the fix and the detailed validation! Tracking floating-point shape inputs is the right direction, and the repeated-length cases pass locally.

There is one remaining concern in StaticModule::_resize(): the newly included float inputs are still compared/copied using inputTensor->host<void>() and inputTensor->usize(). A backend-native FP16 tensor can retain logical FP32 metadata, so usize() does not necessarily describe the native allocation. This can read beyond the native storage or compare bytes that are not the host FP32 contents. The existing readMap/input-copy handling happens later, so it does not protect this cache access.

Could you please obtain a suitable host view before caching these inputs, or conservatively force resize for backend-native/device-backed inputs instead of reading their raw storage? Please keep ordinary floating-point data inputs skipped as they are now. A small focused validation is sufficient; no additional standalone test file is needed. Thanks!

@wangzhaode wangzhaode added the awaiting contributor Waiting for contributor to address review comments or rebase label Oct 15, 2026
@vklonghml

vklonghml commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@wangzhaode 感谢指出这个问题,已按建议采用保守 resize 方案,补充提交:9372a30b75fee7b6d3aab5dd9a1fa8d7af5f9336。

修改过程:

  • 确认内容缓存比较发生在 readMap / 输入复制之前,后面的转换不能保护前面的原生存储读取。
  • 在读取缓存字节之前,检查输入是否由 backend 持有或带有 device 标记;这些输入直接触发 resize,不再按逻辑 FP32 的字节数读取原生存储。普通浮点数据仍按原逻辑跳过,纯主机输入保留限长内容缓存。
  • 回退时同时清空旧缓存,避免“主机 A → 后端 B → 主机 A”误命中旧快照。此方案对后端持有的 CPU FP32 输入也保守回退;相同内容的后端形状输入会重复 resize。
  • 直接扩展现有 expr/ModuleRangeContent,没有新增独立测试文件。使用独立 Module 的 Square 输出直接作为 Range 上限,覆盖主机/后端交替、重复值、增长和缩短;低精度 producer 显式配置 RuntimeManager。

测试结果:

  • 临时追踪确认 low 场景命中 ARM82 原生输入(backend=13, device=1,逻辑字节数仍为 4),在原始字节读取前进入回退;诊断打印已移除。
  • 负向对照仅去掉清缓存操作,在“主机 4 → 后端 16 → 主机 4”的第三次调用失败;恢复清缓存后通过。
  • CPU normal/low、测试 runner 1/4 线程四组配置下,7 项 expr/Module 和 1 项 op/range 均通过,共 32 次用例执行。producer 使用独立 RuntimeManager,线程参数不代表所有子模块的线程数。
  • 未改写 mask 的原始 Paraformer decoder,未量化/INT8/INT4 各连续输入 3/13/13/2/5,15 组均通过。每次全部 18 个输出与重新加载 Module 的结果一致,浮点最大绝对误差为 0,整数输出一致。
  • MNN 与测试程序重新构建通过,git diff --check 通过。

麻烦再帮忙看一下,谢谢!

@vklonghml

Copy link
Copy Markdown
Contributor Author

@wangzhaode 大佬,有空再看下这里

@vklonghml
vklonghml force-pushed the feature/paraformer-dynamic-shapes branch from 9372a30 to 7a47eaf Compare October 23, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting contributor Waiting for contributor to address review comments or rebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants