共计 1585 个字符,预计需要花费 4 分钟才能阅读完成。
1 报错形容
MindSpore 报 TypeError: Cannot joined the return values of different branches, perhaps you need to make them equal.
1.1 零碎环境
*Hardware Environment(Ascend/GPU/CPU): Ascend/GPU/CPU
Software Environment:
– MindSpore version (source or binary): 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 脚本
在控制流场景中,对同一个变量在不同分支赋不同类型的数据。
import mindspore as ms
import mindspore.nn as nn
from mindspore import ms_function, context
context.set_context(mode=context.GRAPH_MODE)
def test_join():
class JoinNet(nn.Cell):
def construct(self, x):
out = [1, 2, 3]
if x > 0:
out = x
return out
x = ms.Tensor([1])
net = JoinNet()
res = net(x)
print(“res:”, res)
1.2.2 报错
这里报错信息如下:
TypeError: mindspre/ccsrc/pipeline/jit/static_analysis/static_analysis.cc:780 ProcessEvalResults] Cannot join the return values of different branches, perhaps you need to make them equal.
Type Join Failed: Abstract type AbstractTensor cannot join with AbstractList.
In file ../test.py(11)
if x > 0:
^
起因剖析:
咱们看报错信息,在 TypeError 中,写到 Cannot join the return values of different branches,意思是不反对两个不同分支的 join 操作,out 在 true 分支被赋值为 Tensor 类型,在 false 分支被赋值为 list 类型,条件 x > y 为变量,因而在 return out 语句中无奈确定 out 的数据类型,会导致图模式下的编译出现异常。
2 解决办法
基于下面已知的起因,很容易做出如下批改:
import mindspore as ms
import mindspore.nn as nn
from mindspore import ms_function, context
context.set_context(mode=context.GRAPH_MODE)
def test_join():
class JoinNet(nn.Cell):
def construct(self, x):
out = Tensor([1, 2, 3])
if x > 0:
out = x
return out
x = ms.Tensor([1])
net = JoinNet()
res = net(x)
print(“res:”, res)
此时执行胜利,输入如下:
res: [1]
3 总结
定位报错问题的步骤:
1、找到报错的用户代码行:if x > 0:
2、剖析控制流的两个分支的 out 数据类型,将其调整成雷同类型。关注变量定义、初始化的正确性。
4 参考文档
https://www.mindspore.cn/docs…