MindSpore报错untimeError: Exceed function call depth limit 1000.

1 系统环境

硬件环境(Ascend/GPU/CPU): CPU
MindSpore版本: 1.9.0
执行模式(PyNative/ Graph): Graph
Python版本: 3.7.5
操作系统平台:不限

2 报错信息

2.1 问题描述

在CPU平台上,调用GRU可以处理的数据的长度受限。

2.2 报错信息

RuntimeError: Exceed function call depth limit 1000, (function call depth: 1001, simulate call depth: 999).
It's always happened with complex construction of code or infinite recursion or loop.
Please check the code if it's has the infinite recursion or call 'context.set_context(max_call_depth=value)' to adjust this value.
If max_call_depth is set larger, the system max stack depth should be set larger too to avoid stack overflow.
For more details, please refer to the FAQ at https://www.mindspore.cn.

2.3 脚本代码(代码格式,可上传附件)

from mindspore import nn,Tensor
import numpy as np

net = nn.GRU(10, 16, 2, has_bias=True, batch_first=True, bidirectional=False)
x = Tensor(np.ones([3, 500, 10]).astype(np.float32)) #序列长度改成500就会报错
h0 = Tensor(np.ones([1 * 2, 3, 16]).astype(np.float32))
output, hn = net(x, h0)
print(output.shape)

3 根因分析

CPU上该算子由小算子拼接实现,有个步骤需循环seq_len次,在静态图模式下,会有复杂度过高的问题.
尝试:或者使用动态图模式。

4 解决方案

尝试1:设置为动态图模式

from mindspore import nn,Tensor  
import numpy as np  
    
ms.set_context(mode=ms.PYNATIVE_MODE)  
net = nn.GRU(10, 16, 2, has_bias=True, batch_first=True, bidirectional=False)  
x = Tensor(np.ones([3, 500, 10]).astype(np.float32)) #序列长度改成500就会报错  
h0 = Tensor(np.ones([1 * 2, 3, 16]).astype(np.float32))  
output, hn = net(x, h0)  
print(output.shape)

尝试2:使用MindSpore2.0.0版本,默认是动态图模式。