# mindspore推理报错NameError:The name 'LTM' is not defined, or not supported in graph mode.

**1. 系统环境**

硬件环境\(Ascend/GPU/CPU\): Ascend/GPU/CPU

软件环境: MindSpore 版本: 1.7.0

执行模式（静态图/动态图）： 不限

Python 版本: 3.9

操作系统平台: linux

**2. 报错信息**

2.1 问题描述

运行一下代码，mindspore推理报错。

2.2 报错信息

```
[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.3 脚本信息

```
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. 根因分析**

根据报错提示，分析可能是这一行不支持graph模式

```
LTM_ = [LTM[:, :i].sum(2) for i in range(k + 1)]
```

**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\)

