Closed
[Bug-Report|缺陷反馈]: AttnRes正反向算子Unroll档位开至128时,申请workspace达到几十GB,导致OOM #1740
yin-pengAdd on  May 14Closed at  Sep 14
yin-peng
yin-peng
May 14 Create

Thanks for sending an issue! Please fill in the following template to help quickly solve your problem.

Describe the current behavior / 问题描述 (Mandatory / 必填)

当D为1536时,出现 NPU out of memory
F2FA0673-A41C-4FD0-924D-2775E30ADEB9.jpg
通过打印日志发现,属于内存过大问题,发现是loop function 内产生的临时tensor 占用了很大的内存:
image.png

Environment / 环境信息 (Mandatory / 必填)

Ascend910_93
cann-9.0.0

Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)

反向测试用例均会出现该问题

Describe the expected behavior / 预期结果 (Mandatory / 必填)

无报错NPU out of memory

无

Special notes for this issue/备注 (Optional / 选填)

likedislike
yin-pengyin-peng
May 15 Modify description of issue
lidongshenglidongshengCollaborators
May 19 Added label: Machine
lidongshenglidongshengCollaborators
May 19 Assign meiqing2 as responsible person
lidongsheng
lidongshengCollaborators
May 19 Comments:

需要进一步细化分析

likedislike
袁野袁野Collaborators
May 19 Removed responsible person meiqing2
袁野袁野Collaborators
May 19 Assign KaiqiJin as responsible person
袁野袁野Collaborators
May 19 Added label: Passes; Removed label: Machine
wangmingjunwangmingjunCollaborators
May 21 Assign zhaoshenjie as responsible person
wangmingjunwangmingjunCollaborators
May 21 Assign hwwangmingjun as responsible person
zhaoshenjie
zhaoshenjieCollaborators
May 22 Comments:

pass 侧 GlobalMemoryReuse 内存复用能力已达到能力上限,切图合图已无法通过旋钮来做进一步的图优化,看了一下没有 incast 被放大数十倍的情况,各个 GM 的存在都是符合计算预期的。按算子和 machine 的同学看,核心问题在

  1. shape 较小时(1, 1, 1, 1536)或(8, 1, 1, 1536)走了 128 的 unroll_list 档位,空间放大了至多 128 倍,属于空间换时间设计。建议调小 unroll_list
  2. stitch 设置 128 后,内存空间申请时最好根据调度和核心数动态调整,直接乘 128 显然是浪费的
likedislike
KaiqiJinKaiqiJinCollaborators
May 28 Removed responsible person KaiqiJin
KaiqiJinKaiqiJinCollaborators
May 28 Removed responsible person zhaoshenjie
KaiqiJinKaiqiJinCollaborators
May 28 Assign wan_jianfen as responsible person
KaiqiJin
KaiqiJinCollaborators
May 28 Comments:

@wan_jianfen 请跟踪一下这个后续,避免出现 unroll=128 * stitch=128 导致乘以16384 倍的情况

likedislike
KaiqiJinKaiqiJinCollaborators
May 28 Added label: Machine; Removed label: Passes
8 event messages are collapsed here。 See more
LordOfMysteriesLordOfMysteriesCollaborators
Jun 29 Associated pull request
LordOfMysteries
LordOfMysteriesCollaborators
Jul 1 Comments:

方案说明
比如如下的简单用例:
    for bt_idxin pypto.loop_unroll(index,**, unroll_list={512, 256, 128, 1} ): {
LEAF_FUNC_512() {};
LEAF_FUNC_256() {};
LEAF_FUNC_128() {};
LEAF_FUNC_1() {};
    }
(1)unrolllist语义的调整
原本逻辑是:stitch_function_max_num控制的是rootfunc的个数。
如果stitch_function_max_num配置了2,循环次数index达到2048的话,那么会命中4次512 unrolltimes的rootdunc,但是由于stitch_function_max_num配成了2,stitch会被拆成两个devtask提交。
 
修改后逻辑:stitch_function_max_num控制的个数指的是rootfunc的unrolltimes(如果没配置unrolllist,那就都是1,跟修改前没区别)。
如果上面算子不改配置,框架会默认拉大stitch_function_max_num到maxunrolltimes(512),循环次数index达到2048的话,上面的stitch就会被拆成4个devtask提交了,这时就建议使用新增加的内存驱动模式的配置来调整用例了。
 
(2)workspace内存配置
为了应对(1)中unrolllist的问题,增加了新的workspace内存配置,用户可以用这个配置来驱动stitch,会忽略stitch_function_max_num的限制,打破新语义下stitch_function_max_num=1024的上限。
    runtime_options={
        "stitch_function_max_num": 64,
        "max_workspace_kb": 20000
    },
原本我们由stitch_function_max_num配置估算出的内存是有很大的浪费的,内存模式下不再受stitch_function_max_num的限制了,直到内存耗尽才提交devtask,这样我们显示配置内存大小可以节省内存,也可以帮助我们评估自己算子的内存消耗。

内存驱动举例
以qat用例为例:这种属于大unrolllist、小stitch_function_max_num的场景,由于stitch_function_max_num有1024的上限,新的修改会导致达不到之前的效果。
image.png

不加内存配置会发现stitch会拆成许多小的task:
image.png

打屏或日志中会提示:
[WorkspaceSize] Estimated size is 264192KB under current config. Set max_workspace_kb to be greater than minimum(544KB) to enable memory-driven mode.

这时我们参考上述信息加上内存驱动的配置:
image.png

再跑用例恢复到原本的表现了:
image.png

一句话说明:使能了unrolllist的算子,需要按照打屏提示配置上workspace内存大小(max_workspace_kb)来使能内存驱动模式以达到修改前的表现。

likedislike
LordOfMysteries
LordOfMysteriesCollaborators
Jul 6 Comments:

已合入主线

likedislike
LordOfMysteriesLordOfMysteriesCollaborators
Jul 6 changed custom state from 待办的 to 已解决
6 event messages are collapsed here。 See more
LordOfMysteriesLordOfMysteriesCollaborators
Sep 14 Close issue