1 系统环境
硬件环境(Ascend/GPU/CPU): Ascend/GPU/CPU
MindSpore版本: mindspore=1.7.0
执行模式(PyNative/ Graph):不限
Python版本: Python=3.9.13
操作系统平台: 不限
2 报错信息
2.1 问题描述
运行一下代码,mindspore推理报错
[CRITICAL] PARSER(3727058,7f213f7cd740,python):2023-04-27-17:58:18.788.462 [mindspore/ccsrc/pipeline/jit/parse/function_block.cc:257] HandleBuiltinNamespaceInfo] The name 'LTM' is not defined, or not supported in graph mode.
Traceback (most recent call last):
File "testtest.py", line 21, in <module>
outs = nets(inps)
File "/home/ma-user/anaconda3/envs/tmp/lib/python3.7/site-packages/mindspore/nn/cell.py", line 586, in __call__
out = self.compile_and_run(*args)
File "/home/ma-user/anaconda3/envs/tmp/lib/python3.7/site-packages/mindspore/nn/cell.py", line 964, in compile_and_run
self.compile(*inputs)
File "/home/ma-user/anaconda3/envs/tmp/lib/python3.7/site-packages/mindspore/nn/cell.py", line 937, in compile
_cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
File "/home/ma-user/anaconda3/envs/tmp/lib/python3.7/site-packages/mindspore/common/api.py", line 1006, in compile
result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())
NameError: mindspore/ccsrc/pipeline/jit/parse/function_block.cc:257 HandleBuiltinNamespaceInfo] The name 'LTM' is not defined, or not supported in graph mode.
# In file testtest.py(13)
LTM_ = [LTM[:, :i].sum(2) for i in range(k + 1)]
2.2 脚本代码(代码格式,可上传附件)
import numpy as np
import mindspore as ms
import mindspore.nn as nn
import mindspore.context as context
class model(nn.Cell):
def __init__(self,):
super().__init__()
def construct(self, LTM):
k = LTM.shape[2]
LTM_ = [LTM[:, :i].sum(2) for i in range(k + 1)]
return LTM_
if __name__ == '__main__':
context.set_context(mode=context.GRAPH_MODE, device_target='GPU', save_graphs=False)
inps = ms.Tensor(np.random.random((3, 8, 256, 256)))
nets = model()
outs = nets(inps)
import pdb; pdb.set_trace()
print(outs.shape)
3 根因分析
根据报错提示,LTM_ = [LTM[:, :i].sum(2) for i in range(k + 1)]
分析可能是上面一行不支持graph模式
4 解决方案
将LTM_ = [LTM[:, :i].sum(2) for i in range(k + 1)]换成多行的写法,发现i=0的时候reducesum的asix范围超限,因为range是从0
开始的,所以将LTM[:, :i]修改为LTM[:, :i+1],具体代码如下
import numpy as np
import mindspore as ms
import mindspore.nn as nn
import mindspore.context as context
class model(nn.Cell):
def init(self,):
super().init()
def construct(self, LTM):
k = LTM.shape[2]
LTM_ =
for i in range(k + 1):
LTM_.append(LTM[:, :i+1].sum(2))
#LTM_ = [LTM[:, :i+1].sum(2) for i in range(k + 1)]
return LTM_
if name == ‘main’:
context.set_context(mode=context.GRAPH_MODE, device_target=‘GPU’, save_graphs=False)
inps = ms.Tensor(np.random.random((3, 8, 256, 256)))
nets = model()
outs = nets(inps)
import pdb; pdb.set_trace()
print(outs[0].shape)
输入shape为(3, 1, 256)