1 报错形容

MindSpore报Dead code exist, please remove it.

1.1 零碎环境
*Hardware Environment(Ascend/GPU/CPU): Ascend/GPU/CPU

Software Environment:

– MindSpore version (source or binary): master or 1.6.0

– Python version (e.g., Python 3.7.5): 3.7.6

– OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic

– GCC/Compiler version (if compiled from source):

1.2 根本信息
1.2.1 脚本

例1:

import numpy as np
from mindspore import Tensor, nn
from mindspore import dtype as ms

class IfInWhileNet(nn.Cell):

def __init__(self):    super().__init__()    self.nums = [1, 2, 3]def construct(self, x, y, i):    out = x    while i < 3:        i = i + 1        out = out + 1        if x == 2:            break            # Dead code here            out = out + y    return out

forward_net = IfInWhileNet()
i = Tensor(np.array(0), dtype=ms.int32)
x = Tensor(np.array(0), dtype=ms.int32)
y = Tensor(np.array(1), dtype=ms.int32)

output = forward_net(x, y, i)

1.2.2报错信息

RuntimeError: mindspore/ccsrc/pipeline/jit/parse/parse.cc:696 ParseStatements] Dead code exist, please remove it.

In file test_while.py(20)

            out = out + y

起因剖析:

在while循环外面存在break语句,break语句前面的“out = out + y”实际上是一个冗余语句,因为该语句永远都不会被执行。

2 解决办法
基于下面已知的起因,很容易做出如下批改:

import numpy as np
from mindspore import Tensor, nn
from mindspore import dtype as ms

class IfInWhileNet(nn.Cell):

def __init__(self):    super().__init__()    self.nums = [1, 2, 3]def construct(self, x, y, i):    out = x    while i < 3:        i = i + 1        out = out + 1        if x == 2:            break            # Dead code here            #out = out + y    return out

forward_net = IfInWhileNet()
i = Tensor(np.array(0), dtype=ms.int32)
x = Tensor(np.array(0), dtype=ms.int32)
y = Tensor(np.array(1), dtype=ms.int32)

output = forward_net(x, y, i)

正文掉或者删除掉“out = out + y”这个冗余语句后,测试脚本执行胜利。

3 总结
break/continue/return 这种流程跳转语句前面不要写任何冗余不执行语句,会导致报Dead code异样。