IR文件分析
概述
在即时编译(Just-In-Time Compilation,JIT)模式下运行用MindSpore编写的模型时,若设置了环境变量MS_DEV_SAVE_GRAPHS
的值为2,运行时会输出一些图编译过程中生成的中间文件,称为IR文件。当前主要有两种格式的IR文件:
ir后缀结尾的IR文件:一种比较直观易懂的以文本格式描述模型结构的文件,可以直接用文本编辑软件查看。
dot后缀结尾的IR文件:若设置了环境变量
MS_DEV_SAVE_GRAPHS
的值为3, 运行时会输出后缀为dot的ir文件。该文件描述了不同节点间的拓扑关系,可以用graphviz将此文件作为输入生成图片,方便用户直观地查看模型结构。
如何保存IR
通过设置环境变量MS_DEV_SAVE_GRAPHS
的值为2来保存各个编译阶段的中间代码。被保存的中间代码有两种格式,默认保存后缀名为.ir
的文本格式的ir文件。如果设置环境变量MS_DEV_SAVE_GRAPHS
的值为3会打印后缀名为.dot
的图形化格式的ir文件。当网络规模不大时,建议使用更直观的图形化格式来查看,当网络规模较大时建议使用更高效的文本格式来查看。
.dot
文件可以通过graphviz转换为图片格式来查看,例如将dot转换为png的命令是dot -Tpng *.dot -o *.png
。
在训练脚本train.py
中,添加如下代码,运行训练脚本时,MindSpore会自动将编译过程中产生的IR文件存放到指定路径。
import os
os.environ['MS_DEV_SAVE_GRAPHS'] = "3"
os.environ['MS_DEV_SAVE_GRAPHS_PATH'] = "path/to/ir/files"
执行训练命令后,在指定的路径下生成了若干个文件:
.
├──00_bootstrap_0000.ir
├──00_bootstrap_0001.dot
├──01_type_inference_0002.ir
├──01_type_inference_0003.dot
├──02_graph_reusing_0004.ir
├──02_graph_reusing_0005.dot
├──03_auto_monad_0006.ir
├──03_auto_monad_0007.dot
...
其中以数字下划线开头的IR文件是在前端编译图过程中生成的,编译过程中各阶段分别会保存一次计算图。下面介绍图编译过程中比较重要的阶段:
bootstrap
阶段负责解析入口函数,该阶段会初步生成MindIR,如果查看IR文件,可以观察到该一个基础的解析节点,代表图的入口函数,以及一个相应的有必需参数的调用节点。type_inference
阶段负责类型推导和符号解析。该阶段递归地解析程序的入口函数,解析对其他函数和对象的引用,并推断所有节点的数据类型和形状信息。与不支持的语法或未解决的引用相关的错误会在这个阶段被标记出来,为开发者提供早期反馈。optimize
阶段负责硬件无关的优化,自动微分与自动并行功能也是在该阶段展开。该阶段又可细分为若干个子阶段,在IR文件列表中,其中以opt_pass_[序号]
为前缀的文件分别是这些子阶段结束后保存的IR文件,非框架开发人员无需过多关注;validate
阶段负责校验编译出来的计算图,如果到此阶段IR中还有仅临时使用的内部算子,则会报错退出;task_emit
阶段负责将计算图传给后端进一步处理;execute
阶段负责启动执行图流程,该阶段的IR图是前端编译阶段的最终图。
此外,后端由于比较贴近底层,后端优化过程中保存的其他IR文件(如以hwopt
开头的文件)非框架开发人员也无需过多关注。非框架开发人员仅需查看名为graph_build_[图序号]_[IR文件序号].ir
的文件,即经过前后端全部优化后的IR。
由于IR文件序号放在文件末尾,按照文件名排序时,IR文件往往不是按照ir生成顺序排序的。若想以文件生成顺序排列IR文件,可以使用Linux的awk命令find ./ -name '*ir' | awk --field-separator="_" '{print $(NF) "--->" $0}' | sort -n
。
由于后端以子图为单位进行优化,故可能会保存多份文件,与前端多个子图都保存在同一文件中的机制不同。
IR文件解读
下面以一个简单的例子来说明IR文件的内容,运行该脚本:
import os
import mindspore
from mindspore import nn, ops
os.environ['MS_DEV_SAVE_GRAPHS'] = '2'
os.environ['MS_DEV_SAVE_GRAPHS_PATH'] = './ir'
class Net(nn.Cell):
def __init__(self):
super().__init__()
def func(x, y):
return ops.div(x, y)
@mindspore.jit
def construct(self, x, y):
a = ops.sub(x, 1)
b = ops.add(a, y)
if b :
b = ops.mul(b, self.func(a, b))
return b
input1 = mindspore.tensor(3, mindspore.float32)
input2 = mindspore.tensor(2, mindspore.float32)
net = Net()
out = net(input1, input2)
print(out)
ir文件介绍
使用文本编辑软件(例如vi
)打开执行完后输出的IR文件18_execute_0161.ir
,内容如下所示:
1 # IR entry: @19_1___main___Net_construct_304
2 # Total subgraphs: 3
3
4 # Attrs:
5 has_shard: 0
6 has_attached: 1
7 jit_level:
8 check_set_strategy_valid_once_only: 1
9 FLASH_SP_RUN_ONCE_ONLY: 1
10 pynative_run_in_graph: 0
11 less_bn: 0
12 auto_parallel_finish_pre_action: 1
13
14 # Total params: 2
15 # Params:
16 %para1_x: <Tensor[Float32], ()> : []
17 %para2_y: <Tensor[Float32], ()> : []
18
19 Node counting information:
20 Total number of nodes: 29
21 Total number of cnodes: 12
22
23 subgraph attr:
24 has_shard: 0
25 has_attached: 1
26 jit_level:
27 check_set_strategy_valid_once_only: 1
28 FLASH_SP_RUN_ONCE_ONLY: 1
29 pynative_run_in_graph: 0
30 less_bn: 0
31 auto_parallel_finish_pre_action: 1
32 subgraph instance: 19_1___main___Net_construct_304 : 0x135400418
33 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
34 subgraph @19_1___main___Net_construct_304() {
35 %0(CNode_310$a) = PrimFunc_Sub(%para1_x, Tensor(shape=[], dtype=Float32, value=1)) cnode_attrs: {checkpoint: Bool(1), is_dynamic_len: Bool(0)}
36 : (<Tensor[Float32], ()>, <Tensor[Float32], (), value=...>) -> (<Tensor[Float32], ()>)
37 # Fullname with scope: (Default/Sub-op1)
38 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
39 # In file t6.py:16, 12~25/ a = ops.sub(x, 1)/
40 # In file t6.py:16, 12~19/ a = ops.sub(x, 1)/<~~This line of code can be shared by multiple nodes, and may be duplicated./
41 # In file /workspace/mindspore/build/package/mindspore/ops/auto_generate/gen_ops_def.py:5251~5294, 0~31/def sub(input, other):/
42 # In file /workspace/mindspore/build/package/mindspore/ops/auto_generate/gen_ops_def.py:5294, 11~31/ return sub_op(input, other)/
43 %1(CNode_309$b) = PrimFunc_Add(%0, %para2_y) cnode_attrs: {checkpoint: Bool(1), is_dynamic_len: Bool(0)}
44 : (<Tensor[Float32], ()>, <Tensor[Float32], ()>) -> (<Tensor[Float32], ()>)
45 # Fullname with scope: (Default/Add-op1)
46 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
47 # In file t6.py:17, 12~25/ b = ops.add(a, y)/
48 # In file t6.py:17, 12~19/ b = ops.add(a, y)/<~~This line of code can be shared by multiple nodes, and may be duplicated./
49 # In file /workspace/mindspore/build/package/mindspore/ops/auto_generate/gen_ops_def.py:183~241, 0~31/def add(input, other):/
50 # In file /workspace/mindspore/build/package/mindspore/ops/auto_generate/gen_ops_def.py:241, 11~31/ return add_op(input, other)/
51 %2(CNode_308) = PrimFunc_Cast(%1, I64(30)) primitive_attrs: {output_names: [output], input_names: [x, dst_type]} cnode_attrs: {checkpoint: Bool(1), is_dynamic_len: Bool(0)}
52 : (<Tensor[Float32], ()>, <Int64, NoShape>) -> (<Tensor[Bool], ()>)
53 # Fullname with scope: (Default/Cast-op1)
54 # In file /workspace/mindspore/build/package/mindspore/_extends/parse/standard_method.py:2747~2749, 0~23/def bool_(x):/
55 # In file /workspace/mindspore/build/package/mindspore/_extends/parse/standard_method.py:2749, 11~23/ return x.__bool__()/
56 # In file /workspace/mindspore/build/package/mindspore/_extends/parse/standard_method.py:2749, 11~21/ return x.__bool__()/<~~This line of code can be shared by multiple nodes, and may be duplicated./
57 # In file /workspace/mindspore/build/package/mindspore/_extends/parse/standard_method.py:3267~3272, 0~34/def tensor_bool(x):/
58 # In file /workspace/mindspore/build/package/mindspore/_extends/parse/standard_method.py:3270~3271, 4~38/ if is_cond and F.isconstant(x):/
59 # In file /workspace/mindspore/build/package/mindspore/_extends/parse/standard_method.py:3272, 11~34/ return F.cast(x, mstype.bool_)/<~~This line of code can be shared by multiple nodes, and may be duplicated./
60 %3(CNode_317) = Partial(@20_4_✓__main___Net_construct_311, %1, %0) primitive_attrs: {side_effect_propagate: I64(1)} cnode_attrs: {checkpoint: Bool(1)}
61 : (<Func, NoShape>, <Tensor[Float32], ()>, <Tensor[Float32], ()>) -> (<Func, NoShape>)
62 # Fullname with scope: (Default/Partial-op0)
63 %4(CNode_316) = Partial(@21_14_✗__main___Net_construct_314, %1) primitive_attrs: {side_effect_propagate: I64(1)} cnode_attrs: {checkpoint: Bool(1)}
64 : (<Func, NoShape>, <Tensor[Float32], ()>) -> (<Func, NoShape>)
65 # Fullname with scope: (Default/Partial-op1)
66 %5(ValueNode_307) = Switch(%2, %3, %4) cnode_attrs: {checkpoint: Bool(1)}
67 : (<Tensor[Bool], ()>, <Func, NoShape>, <Func, NoShape>) -> (<Func, NoShape>)
68 # Fullname with scope: (Default/Switch-op4)
69 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
70 # In file t6.py:18~19, 8~43/ if b :/
71 %6(CNode_306) = %5[@FuncUnion(@20_4_✓__main___Net_construct_311, @21_14_✗__main___Net_construct_314)]()
72 : () -> (<Tensor[Float32], ()>)
73 # Fullname with scope: (5)
74 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
75 # In file t6.py:18~19, 8~43/ if b :/
76 Return(%6) cnode_attrs: {checkpoint: Bool(1)}
77 : (<Tensor[Float32], ()>)
78 # Fullname with scope: (Default/Return-op19)
79 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
80 # In file t6.py:18~19, 8~43/ if b :/
81 }
82
83
84 indirect: 1
85 subgraph attr:
86 defer_inline: 0
87 undeterminate: 0
88 subgraph instance: 20_4_✓__main___Net_construct_311 : 0x135400a18
89 # Parameters: 2, (<Tensor[Float32], ()>, <Tensor[Float32], ()>)
90 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
91 subgraph @20_4_✓__main___Net_construct_311(%para3_Parameter_320, %para4_Parameter_319) {
92 %0(output) = PrimFunc_Div(%para4_Parameter_319, %para3_Parameter_320)
93 : (<Tensor[Float32], ()>, <Tensor[Float32], ()>) -> (<Tensor[Float32], ()>)
94 # Fullname with scope: (Default/Div-op1)
95 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
96 # In file t6.py:19, 27~42/ b = ops.mul(b, self.func(a, b))/
97 # In file t6.py:19, 27~36/ b = ops.mul(b, self.func(a, b))/<~~This line of code can be shared by multiple nodes, and may be duplicated./
98 # In file t6.py:12~13, 4~28/ def func(x, y):/
99 # In file t6.py:13, 15~28/ return ops.div(x, y)/
100 # In file t6.py:13, 15~22/ return ops.div(x, y)/<~~This line of code can be shared by multiple nodes, and may be duplicated./
101 # In file /workspace/mindspore/build/package/mindspore/ops/function/math_func.py:707~766, 0~17/def div(input, other, *, rounding_mode=None):/
102 # In file /workspace/mindspore/build/package/mindspore/ops/function/math_func.py:762~765, 4~38/ if rounding_mode:/
103 # In file /workspace/mindspore/build/package/mindspore/ops/function/math_func.py:765, 17~38/ output = P.Div()(input, other)/<~~This line of code can be shared by multiple nodes, and may be duplicated./
104 %1(CNode_313$b) = PrimFunc_Mul(%para3_Parameter_320, %0) cnode_attrs: {is_dynamic_len: Bool(0)}
105 : (<Tensor[Float32], ()>, <Tensor[Float32], ()>) -> (<Tensor[Float32], ()>)
106 # Fullname with scope: (Default/Mul-op1)
107 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
108 # In file t6.py:19, 16~43/ b = ops.mul(b, self.func(a, b))/
109 # In file t6.py:19, 16~23/ b = ops.mul(b, self.func(a, b))/<~~This line of code can be shared by multiple nodes, and may be duplicated./
110 # In file /workspace/mindspore/build/package/mindspore/ops/auto_generate/gen_ops_def.py:3471~3518, 0~31/def mul(input, other):/
111 # In file /workspace/mindspore/build/package/mindspore/ops/auto_generate/gen_ops_def.py:3518, 11~31/ return mul_op(input, other)/
112 Return(%1)
113 : (<Tensor[Float32], ()>)
114 # Fullname with scope: (Default/Return-op20)
115 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
116 # In file t6.py:19, 12~43/ b = ops.mul(b, self.func(a, b))/
117 }
118
119
120 indirect: 1
121 subgraph attr:
122 defer_inline: 0
123 undeterminate: 0
124 subgraph instance: 21_14_✗__main___Net_construct_314 : 0x1353ff218
125 # Parameters: 1, (<Tensor[Float32], ()>)
126 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
127 subgraph @21_14_✗__main___Net_construct_314(%para5_Parameter_322) {
128 Return(%para5_Parameter_322)
129 : (<Tensor[Float32], ()>)
130 # Fullname with scope: (Default/Return-op21)
131 # In file t6.py:15~20, 4~16/ def construct(self, x, y):/
132 # In file t6.py:18~19, 8~43/ if b :/
133 }
以上内容可分为两个部分,第一部分为图的输入信息,第二部分为图的结构信息:
第1行表示该网络的顶图名称
@19_1___main___Net_construct_304
,也就是入口图。第2行表示该网络解析出来的图的数量,该IR文件展示了三张图的信息。分别为第23行的入口图
@19_1___main___Net_construct_304
;第84行的图20_4_✓__main___Net_construct_311
,对应着网络中if条件为true时所运行的图;第120行的图21_14_✗__main___Net_construct_314
,即对应着网络中if条件为false时所运行的图。第14行表示该网络有多少个输入。
第16-17行是输入列表,遵循
%para[序号]_[name] : <[data_type], (shape)>
的格式。
对于具体的图来说(此处以图@19_1___main___Net_construct_304
为例):
第23-81行展示了图结构的信息,图中含有若干个节点,即
CNode
。该图包含Sub
、Add
、Mul
这些在网路所调用的接口中所用到的算子。
CNode
的信息遵循如下格式,从左到右分别为序号、节点名称-debug_name、算子名称-op_name、输入节点-arg、节点的属性-primitive_attrs、输入和输出的规格、源码解析调用栈等信息。
由于ANF图为单向无环图,所以此处仅根据输入关系来体现节点与节点的连接关系。关联代码行则体现了CNode
与脚本源码之间的关系,例如第75行表明该节点是由脚本中if b
这一行解析而来。
%[序号]([debug_name]) = [op_name]([arg], ...) primitive_attrs: {[key]: [value], ...}
: (<[输入data_type]x[输入shape]>, ...) -> (<[输出data_type]x[输出shape]>, ...)
# 关联代码行
关于关联代码行的说明:
代码行信息包括文件路径,代码起始位置和终止位置。例如:
# In file /workspace/mindspore/build/package/mindspore/nn/wrap/cell_wrapper.py:437~441, 8~45
,则文件路径为:/workspace/mindspore/build/package/mindspore/nn/wrap/cell_wrapper.py
,代码起始位置为:第437行/第8列,代码终止位置为:第441行/第45列。如果代码不跨行,则不显示终止行信息,例如:# In file /workspace/mindspore/build/package/mindspore/nn/wrap/cell_wrapper.py:418, 19~37
,只显示第418行。代码行展示有两种模式,第一种是显示完整的调用栈,第二种为了减小文件的体积,只显示第一行,即省去了调用过程。默认按第一种模式,在所有ir文件中展示完整调用栈的代码行信息。
如果算子是反向传播算子,关联代码行除了会显示本身的代码,还会显示对应的正向代码,通过“Corresponding forward node candidate:”标识。
如果算子是融合算子,关联代码行会显示出融合的相关代码,通过“Corresponding code candidate:”标识,其中用分隔符“-”区分不同的代码。
经过编译器的若干优化处理后,节点可能经过了若干转换(如算子拆分、算子融合等),节点的源码解析调用栈信息与脚本可能无法完全一一对应,这里仅作为辅助手段。
在后端经过算子选择阶段后,输入输出规格信息(即
:
后内容)会有两行。第一行表示为HOST
侧的规格信息,第二行为DEVICE
侧的规格信息。
dot文件介绍
可以用graphviz将dot
格式的IR文件作为输入生成图片。例如,在Linux操作系统下,可以通过以下命令转换成一张PNG图片。
dot -Tpng -o 01_type_inference_0003.png 01_type_inference_0003.dot
转换之后得到类似下图的模型示意图,可以观察构建的静态图模型结构。不同的黑框区分了不同的子图,图与图之间的蓝色箭头表示相互之间的调用。蓝色区域表示参数,矩形表示图的参数列表,六边形和黑色箭头表示该参数作为CNode的输入参与计算过程。黄色矩形表示CNode节点,从图中可以看出,CNode输入从下标0开始,第0个输入(即紫色或绿色区域)表示该算子将要进行怎样的计算,通过虚箭头连接。类型一般为算子原语,也可以是另一张图。下标1之后的输入则为计算所需要的参数。
如何根据analyze_fail.ir文件分析图推导失败的原因
MindSpore在编译图的过程中,经常会出现type_inference
阶段的图推导失败的报错,开发者通常可以根据报错信息以及analyze_fail.ir文件,来定位出脚本中存在的问题。
例子1:参数数量不匹配
import os
import mindspore
from mindspore import nn, ops
os.environ['MS_DEV_SAVE_GRAPHS'] = '2'
os.environ['MS_DEV_SAVE_GRAPHS_PATH'] = './ir'
class Net(nn.Cell):
def __init__(self):
super().__init__()
def func(x, y):
return ops.div(x, y)
@mindspore.jit
def construct(self, x, y):
a = ops.sub(x, 1)
b = ops.add(a, y)
c = ops.mul(b, self.func(a, a, b))
input1 = mindspore.tensor(3, mindspore.float32)
input2 = mindspore.tensor(2, mindspore.float32)
net = Net()
out = net(input1, input2)
print(out)
会出现如下的报错:
1 Traceback (most recent call last):
2 File "/workspace/mindspore/test2.py", line 24, in <module>
3 out = net(input1, input2)
4 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/nn/cell.py", line 1338, in __call__
5 return self.construct(*args, **kwargs)
6 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/common/api.py", line 1090, in staging_specialize
7 out = jit_executor(*args, **kwargs)
8 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/common/api.py", line 180, in wrapper
9 results = fn(*arg, **kwargs)
10 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/common/api.py", line 667, in __call__
11 raise err
12 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/common/api.py", line 663, in __call__
13 phase = self.compile(self.fn.__name__, *args_list, **kwargs)
14 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/common/api.py", line 781, in compile
15 is_compile = self._graph_executor.compile(
16 TypeError: The parameters number of the function is 2, but the number of provided arguments is 3.
17 FunctionGraph ID : func_7
18 NodeInfo: In file /workspace/mindspore/test2.py:12~13, 4~28
19 def func(x, y):
20
21 ----------------------------------------------------
22 - C++ Call Stack: (For framework developers)
23 ----------------------------------------------------
24 mindspore/ccsrc/pipeline/jit/ps/static_analysis/stack_frame.cc:98 DoJump
25
26 ----------------------------------------------------
27 - The Traceback of Net Construct Code:
28 ----------------------------------------------------
29 # 0 In file /workspace/mindspore/test2.py:19, 23~41
30 c = ops.mul(b, self.func(a, a, b))
31 ^~~~~~~~~~~~~~~~~~
32 (See file '/workspace/mindspore/rank_0/om/analyze_fail.ir' for more details. Get instructions about `analyze_fail.ir` at https://www.mindspore.cn/search?inputValue=analyze_fail.ir)
以上的报错信息为:“TypeError: The parameters number of the function is 2, but the number of provided arguments is 3…”。
表明FunctionGraph ID : func_7
只需要2个参数,但是却提供了3个参数。从“The Traceback of Net Construct Code”中,可以知道出错的代码为:“In file /workspace/mindspore/test2.py:19 … self.func(a, a, b)”,是因为该处的函数调用传入参数的数目过多。
但如果报错信息不直观或者需要查看IR中已推导出的部分图信息,使用文本编辑软件(例如,vi)打开报错信息中的提示的文件(第32行括号中):/workspace/mindspore/rank_0/om/analyze_fail.ir
,文件中除了上述报错信息,还有如下内容:
1 # ===============================================================================================
2 # The following shows the last analyze fail log message.
3 # ===============================================================================================
4
5 ----------------------------------------------------
6 - Caught exception:
7 ----------------------------------------------------
8 The parameters number of the function is 2, but the number of provided arguments is 3.
9 FunctionGraph ID : func_7
10 NodeInfo: In file /workspace/mindspore/test2.py:12~13, 4~28
11 def func(x, y):
12
13 ----------------------------------------------------
14 - C++ Call Stack: (For framework developers)
15 ----------------------------------------------------
16 mindspore/ccsrc/pipeline/jit/ps/static_analysis/stack_frame.cc:98 DoJump
17
18 ----------------------------------------------------
19 - The Traceback of Net Construct Code:
20 ----------------------------------------------------
21 # 0 In file /workspace/mindspore/test2.py:19, 23~41
22 c = ops.mul(b, self.func(a, a, b))
23 ^~~~~~~~~~~~~~~~~~
24
25 # ===============================================================================================
26 # The following shows the IR when the function graphs evaluation fails to help locate the problem.
27 # You can search the last ------------------------> to the node which is evaluated failure.
28 # Refer to https://www.mindspore.cn/search?inputValue=analyze_fail.ir to get more instructions.
29 # ===============================================================================================
30
31 # IR entry: @__main___Net_construct_8
32 # Total subgraphs: 0
33
34 # Total params: 2
35 # Params:
36 %para1_x: <null>
37 %para2_y: <null>
38
39 subgraph attr:
40 subgraph instance: __main___Net_construct_8 : 0xf1667a0
41 # In file /workspace/mindspore/test2.py:15~19, 4~42/ @mindspore.jit/
42 subgraph @__main___Net_construct_8() {
43 %0(CNode_1) = resolve(NameSpace[Entry: '__main__.Net.construct'], __main__.Net.construct)
44 : (<External, NoShape>, <External, NoShape>) -> (<Func, NoShape>)
45 #scope: (Default)
46
47 #------------------------> 0
48 %1(CNode_2) = %0(%para1_x, %para2_y)
49 : (<Tensor[Float32], ()>, <Tensor[Float32], ()>) -> (<null>)
50 #scope: (Default)
51 Return(%1)
52 : (<null>)
53 #scope: (Default)
54 # In file /workspace/mindspore/test2.py:15~19, 4~42/ @mindspore.jit/
55 }
56 # Order:
57 # 1: @__main___Net_construct_8:CNode_1{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> Entry: '__main__.Net.construct', [2]: ValueNode<Symbol> __main__.Net.construct}
58 # 2: @__main___Net_construct_8:CNode_2{[0]: CNode_1, [1]: param_x, [2]: param_y}
59 # 3: @__main___Net_construct_8:CNode_9{[0]: ValueNode<Primitive> Return, [1]: CNode_2}
60
61
62 subgraph attr:
63 subgraph instance: __main___Net_construct_8 : 0xf4c9fb0
64 # In file /workspace/mindspore/test2.py:15~19, 4~42/ @mindspore.jit/
65 subgraph @__main___Net_construct_8(%para0_x, %para0_y) {
66 %0(CNode_10) = resolve(NameSpace[SymbolStr: 'Namespace:__main__'], ops)
67 : (<External, NoShape>, <External, NoShape>) -> (<External, NoShape>)
68 #scope: (Default)
69 # In file /workspace/mindspore/test2.py:17, 12~15/ a = ops.sub(x, 1)/
70 %1(CNode_11) = getattr(%0, "mul")
71 : (<External, NoShape>, <String, NoShape>) -> (<Func, NoShape>)
72 #scope: (Default)
73 # In file /workspace/mindspore/test2.py:19, 12~19/ c = ops.mul(b, self.func(a, a, b))/
74 %2(CNode_12) = getattr(%0, "add")
75 : (<External, NoShape>, <String, NoShape>) -> (<Func, NoShape>)
76 #scope: (Default)
77 # In file /workspace/mindspore/test2.py:18, 12~19/ b = ops.add(a, y)/
78 %3(CNode_13) = getattr(%0, "sub")
79 : (<External, NoShape>, <String, NoShape>) -> (<Func, NoShape>)
80 #scope: (Default)
81 # In file /workspace/mindspore/test2.py:17, 12~19/ a = ops.sub(x, 1)/
82 %4(a) = %3(%para0_x, I64(1))
83 : (<Tensor[Float32], ()>, <Int64, NoShape>) -> (<Tensor[Float32], ()>)
84 #scope: (Default)
85 # In file /workspace/mindspore/test2.py:17, 12~25/ a = ops.sub(x, 1)/
86 %5(b) = %2(%4, %para0_y)
87 : (<Tensor[Float32], ()>, <Tensor[Float32], ()>) -> (<Tensor[Float32], ()>)
88 #scope: (Default)
89 # In file /workspace/mindspore/test2.py:18, 12~25/ b = ops.add(a, y)/
90 %6(CNode_14) = resolve(NameSpace[ClassMember: 'Namespace:__main__..<Net::139759664946288>'], func)
91 : (<External, NoShape>, <External, NoShape>) -> (<Func, NoShape>)
92 #scope: (Default)
93 # In file /workspace/mindspore/test2.py:19, 23~32/ c = ops.mul(b, self.func(a, a, b))/
94
95 #------------------------> 1
96 %7(CNode_15) = %6(%4, %4, %5)
97 : (<Tensor[Float32], ()>, <Tensor[Float32], ()>, <Tensor[Float32], ()>) -> (<null>)
98 #scope: (Default)
99 # In file /workspace/mindspore/test2.py:19, 23~41/ c = ops.mul(b, self.func(a, a, b))/
100 %8(c) = %1(%5, %7)
101 : (<Tensor[Float32], ()>, <null>) -> (<null>)
102 #scope: (Default)
103 # In file /workspace/mindspore/test2.py:19, 12~42/ c = ops.mul(b, self.func(a, a, b))/
104 %9(CNode_16) = StopGradient(%8)
105 : (<null>) -> (<null>)
106 #scope: (Default)
107 # In file /workspace/mindspore/test2.py:15~19, 4~42/ @mindspore.jit/
108 %10(CNode_17) = Depend(None, %9) primitive_attrs: {side_effect_propagate: I64(1)} cnode_attrs: {topo_sort_rhs_first: Bool(1)}
109 : (<null>, <null>) -> (<null>)
110 #scope: (Default)
111 # In file /workspace/mindspore/test2.py:15~19, 4~42/ @mindspore.jit/
112 Return(%10)
113 : (<null>)
114 #scope: (Default)
115 # In file /workspace/mindspore/test2.py:15~19, 4~42/ @mindspore.jit/
116 }
117 # Order:
118 # 1: @__main___Net_construct_8:CNode_10{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> SymbolStr: 'Namespace:__main__', [2]: ValueNode<Symbol> ops}
119 # 2: @__main___Net_construct_8:CNode_13{[0]: ValueNode<Primitive> getattr, [1]: CNode_10, [2]: ValueNode<StringImm> sub}
120 # 3: @__main___Net_construct_8:CNode_18{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> MakeTuple}
121 # 5: @__main___Net_construct_8:a{[0]: CNode_13, [1]: param_x, [2]: ValueNode<Int64Imm> 1}
122 # 6: @__main___Net_construct_8:CNode_12{[0]: ValueNode<Primitive> getattr, [1]: CNode_10, [2]: ValueNode<StringImm> add}
123 # 7: @__main___Net_construct_8:CNode_19{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> MakeTuple}
124 # 9: @__main___Net_construct_8:b{[0]: CNode_12, [1]: a, [2]: param_y}
125 # 10: @__main___Net_construct_8:CNode_11{[0]: ValueNode<Primitive> getattr, [1]: CNode_10, [2]: ValueNode<StringImm> mul}
126 # 11: @__main___Net_construct_8:CNode_14{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> ClassMember: 'Namespace:__main__..<Net::139759664946288>', [2]: ValueNode<Symbol> func}
127 # 12: @__main___Net_construct_8:CNode_20{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> MakeTuple}
128 # 14: @__main___Net_construct_8:CNode_15{[0]: CNode_14, [1]: a, [2]: a, [3]: b}
129 # 15: @__main___Net_construct_8:CNode_21{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> MakeTuple}
130 # 17: @__main___Net_construct_8:c{[0]: CNode_11, [1]: b, [2]: CNode_15}
131 # 19: @__main___Net_construct_8:CNode_9{[0]: ValueNode<Primitive> Return, [1]: CNode_17}
132
133
134 # ===============================================================================================
135 # The total of function graphs in evaluation stack: 2
136 # ===============================================================================================
137
138
139 # ===============================================================================================
140 # The rest function graphs are the following:
141 # ===============================================================================================
142 No more function graphs.
analyze_fail.ir
文件与前文介绍过的ir文件格式一致,唯一有区别的地方在于analyze_fail.ir
文件中会指出推导出错的节点所在的位置,即第95行的------------------------> 1
。该箭头指向了推导出错的节点,为%7(CNode_15) = %6(%4, %4, %5) ...
。
根据(%4, %4, %5)
可知,该节点的输入参数有三个。从源码解析调用栈中可以知道实际该函数为self.func
,在脚本中的定义为def func(x, y):...
。
在函数定义中,只需要两个参数,故会在此处出现推导失败的报错,需要修改脚本中传入的参数个数以解决该问题。
例子2:BiasAdd输入之间shape不匹配
import numpy as np
import mindspore
from mindspore import nn, ops, Tensor, Parameter
from mindspore.common.initializer import initializer
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
self.weight = Parameter(initializer('normal', [32, 8]), name="weight")
self.bias = Parameter(initializer('zeros', [4]), name="bias")
@mindspore.jit
def construct(self, x1):
x = ops.matmul(x1, self.weight)
x = ops.bias_add(x, self.bias)
return x
net = Net()
x = mindspore.tensor(np.arange(3*32).reshape(3, 32), mindspore.float32)
out = net(x)
print('out', out.shape)
会出现如下的报错:
1 Traceback (most recent call last):
2 File "/workspace/mindspore/test2.py", line 20, in <module>
3 out = net(x)
4 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/nn/cell.py", line 1338, in __call__
5 return self.construct(*args, **kwargs)
6 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/common/api.py", line 1090, in staging_specialize
7 out = jit_executor(*args, **kwargs)
8 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/common/api.py", line 180, in wrapper
9 results = fn(*arg, **kwargs)
10 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/common/api.py", line 667, in __call__
11 raise err
12 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/common/api.py", line 663, in __call__
13 phase = self.compile(self.fn.__name__, *args_list, **kwargs)
14 File "/workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/common/api.py", line 781, in compile
15 is_compile = self._graph_executor.compile(
16 ValueError: For 'BiasAdd', bias[0] shape should be equal to input_x[1] shape when data_format is 0, but got bias shape: .[const vector]{4}, input_shape: [const vector] {3, 8}.
17
18 ----------------------------------------------------
19 - C++ Call Stack: (For framework developers)
20 ----------------------------------------------------
21 mindspore/ops/infer/ops_func_impl//bias_add.cc:71 CheckShapeValid
22
23 ----------------------------------------------------
24 - The Traceback of Net Construct Code:
25 ----------------------------------------------------
26 # 0 In file /workspace/mindspore/test2.py:15, 12~38
27 x = ops.bias_add(x, self.bias)
28 ^~~~~~~~~~~~~~~~~~~~~~~~~~
29 # 1 In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7093, 11~37
30 return bias_add_op(input_x, bias)
31 ^~~~~~~~~~~~~~~~~~~~~~~~~~
32 (See file '/workspace/mindspore/rank_0/om/analyze_fail.ir' for more details. Get instructions about `analyze_fail.ir` at https://www.mindspore.cn/search?inputValue=analyze_fail.ir)
根据以上报错可知,是算子BiasAdd
的第一个输入和第二个输入的shape
不匹配导致的错误。为了进一步了解算子的shape
是经过了什么样的变化,使用文本编辑软件(例如,vi)打开报错信息中的提示的文件:/workspace/mindspore/rank_0/om/analyze_fail.ir
,文件中除了上述报错信息,还有如下内容:
1 # ===============================================================================================
2 # The following shows the last analyze fail log message.
3 # ===============================================================================================
4
5 ----------------------------------------------------
6 - Caught exception:
7 ----------------------------------------------------
8 For 'BiasAdd', bias[0] shape should be equal to input_x[1] shape when data_format is 0, but got bias shape: .[const vector]{4}, input_shape: [const vector]{3, 8}.
9
10 ----------------------------------------------------
11 - C++ Call Stack: (For framework developers)
12 ----------------------------------------------------
13 mindspore/ops/infer/ops_func_impl//bias_add.cc:71 CheckShapeValid
14
15 ----------------------------------------------------
16 - The Traceback of Net Construct Code:
17 ----------------------------------------------------
18 # 0 In file /workspace/mindspore/test2.py:15, 12~38
19 x = ops.bias_add(x, self.bias)
20 ^~~~~~~~~~~~~~~~~~~~~~~~~~
21 # 1 In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7093, 11~37
22 return bias_add_op(input_x, bias)
23 ^~~~~~~~~~~~~~~~~~~~~~~~~~
24
25 # ===============================================================================================
26 # The following shows the IR when the function graphs evaluation fails to help locate the problem.
27 # You can search the last ------------------------> to the node which is evaluated failure.
28 # Refer to https://www.mindspore.cn/search?inputValue=analyze_fail.ir to get more instructions.
29 # ===============================================================================================
30
31 # IR entry: @__main___Net_construct_3
32 # Total subgraphs: 0
33
34 # Total params: 3
35 # Params:
36 %para1_x1: <null>
37 %para2_weight: <Ref[Tensor[Float32]], (32, 8), ref_key=weight, is_parameter> : has_default
38 %para3_bias: <Ref[Tensor[Float32]], (4), ref_key=bias, is_parameter> : has_default
39
40 subgraph attr:
41 subgraph instance: __main___Net_construct_3 : 0x13bfdd40
42 # In file /workspace/mindspore/test2.py:12~16, 4~16/ @mindspore.jit/
43 subgraph @__main___Net_construct_3() {
44 %0(CNode_5) = resolve(NameSpace[Entry: '__main__.Net.construct'], __main__.Net.construct)
45 : (<External, NoShape>, <External, NoShape>) -> (<Func, NoShape>)
46 #scope: (Default)
47
48 #------------------------> 0
49 %1(CNode_6) = %0(%para1_x1)
50 : (<Tensor[Float32], (3, 32)>) -> (<null>)
51 #scope: (Default)
52 Return(%1)
53 : (<null>)
54 #scope: (Default)
55 # In file /workspace/mindspore/test2.py:16, 8~16/ return x/
56 }
57 # Order:
58 # 1: @__main___Net_construct_3:CNode_5{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> Entry: '__main__.Net.construct', [2]: ValueNode<Symbol> __main__.Net.construct}
59 # 2: @__main___Net_construct_3:CNode_6{[0]: CNode_5, [1]: param_x1}
60 # 3: @__main___Net_construct_3:CNode_7{[0]: ValueNode<Primitive> Return, [1]: CNode_6}
61
62
63 subgraph attr:
64 subgraph instance: __main___Net_construct_3 : 0x13f5fc20
65 # In file /workspace/mindspore/test2.py:12~16, 4~16/ @mindspore.jit/
66 subgraph @__main___Net_construct_3(%para0_x1) {
67 %0(CNode_8) = resolve(NameSpace[SymbolStr: 'Namespace:__main__'], ops)
68 : (<External, NoShape>, <External, NoShape>) -> (<External, NoShape>)
69 #scope: (Default)
70 # In file /workspace/mindspore/test2.py:14, 12~15/ x = ops.matmul(x1, self.weight)/
71 %1(CNode_9) = getattr(%0, "bias_add")
72 : (<External, NoShape>, <String, NoShape>) -> (<Func, NoShape>)
73 #scope: (Default)
74 # In file /workspace/mindspore/test2.py:15, 12~24/ x = ops.bias_add(x, self.bias)/
75 %2(CNode_10) = getattr(%0, "matmul")
76 : (<External, NoShape>, <String, NoShape>) -> (<Func, NoShape>)
77 #scope: (Default)
78 # In file /workspace/mindspore/test2.py:14, 12~22/ x = ops.matmul(x1, self.weight)/
79 %3(CNode_11) = resolve(NameSpace[ClassMember: 'Namespace:__main__..<Net::136107621691200>'], weight)
80 : (<External, NoShape>, <External, NoShape>) -> (<Ref[Tensor[Float32]], (32, 8)>)
81 #scope: (Default)
82 # In file /workspace/mindspore/test2.py:14, 27~38/ x = ops.matmul(x1, self.weight)/
83 %4(x) = %2(%para0_x1, %3)
84 : (<Tensor[Float32], (3, 32)>, <Ref[Tensor[Float32]], (32, 8)>) -> (<Tensor[Float32], (3, 8)>)
85 #scope: (Default)
86 # In file /workspace/mindspore/test2.py:14, 12~39/ x = ops.matmul(x1, self.weight)/
87 %5(CNode_12) = resolve(NameSpace[ClassMember: 'Namespace:__main__..<Net::136107621691200>'], bias)
88 : (<External, NoShape>, <External, NoShape>) -> (<Ref[Tensor[Float32]], (4)>)
89 #scope: (Default)
90 # In file /workspace/mindspore/test2.py:15, 28~37/ x = ops.bias_add(x, self.bias)/
91
92 #------------------------> 1
93 %6(x) = %1(%4, %5)
94 : (<Tensor[Float32], (3, 8)>, <Ref[Tensor[Float32]], (4)>) -> (<null>)
95 #scope: (Default)
96 # In file /workspace/mindspore/test2.py:15, 12~38/ x = ops.bias_add(x, self.bias)/
97 Return(%6)
98 : (<null>)
99 #scope: (Default)
100 # In file /workspace/mindspore/test2.py:16, 8~16/ return x/
101 }
102 # Order:
103 # 1: @__main___Net_construct_3:CNode_8{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> SymbolStr: 'Namespace:__main__', [2]: ValueNode<Symbol> ops}
104 # 2: @__main___Net_construct_3:CNode_10{[0]: ValueNode<Primitive> getattr, [1]: CNode_8, [2]: ValueNode<StringImm> matmul}
105 # 3: @__main___Net_construct_3:CNode_11{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> ClassMember: 'Namespace:__main__..<Net::136107621691200>', [2]: ValueNode<Symbol> weight}
106 # 4: @__main___Net_construct_3:CNode_13{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> MakeTuple}
107 # 6: @__main___Net_construct_3:x{[0]: CNode_10, [1]: param_x1, [2]: CNode_11}
108 # 7: @__main___Net_construct_3:CNode_9{[0]: ValueNode<Primitive> getattr, [1]: CNode_8, [2]: ValueNode<StringImm> bias_add}
109 # 8: @__main___Net_construct_3:CNode_12{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> ClassMember: 'Namespace:__main__..<Net::136107621691200>', [2]: ValueNode<Symbol> bias}
110 # 9: @__main___Net_construct_3:CNode_14{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> MakeTuple}
111 # 11: @__main___Net_construct_3:x{[0]: CNode_9, [1]: x, [2]: CNode_12}
112 # 12: @__main___Net_construct_3:CNode_7{[0]: ValueNode<Primitive> Return, [1]: x}
113
114
115 subgraph attr:
116 subgraph instance: bias_add_4 : 0x13f65d00
117 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7057~7093/def bias_add(input_x, bias):/
118 subgraph @bias_add_4(%para0_input_x, %para0_bias) {
119 %0(CNode_15) = resolve(NameSpace[SymbolStr: 'Namespace:mindspore.ops.function.nn_func'], _get_cache_prim)
120 : (<External, NoShape>, <External, NoShape>) -> (<Func, NoShape>)
121 #scope: (Default)
122 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 18~33/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
123 %1(CNode_16) = resolve(NameSpace[SymbolStr: 'Namespace:mindspore.ops.function.nn_func'], P)
124 : (<External, NoShape>, <External, NoShape>) -> (<External, NoShape>)
125 #scope: (Default)
126 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 34~35/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
127 %2(CNode_17) = getattr(%1, "BiasAdd")
128 : (<External, NoShape>, <String, NoShape>) -> (<Func, NoShape>)
129 #scope: (Default)
130 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 34~43/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
131 %3(CNode_18) = %0(%2)
132 : (<Func, NoShape>) -> (<Func, NoShape>)
133 #scope: (Default)
134 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 18~44/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
135 %4(CNode_19) = resolve(NameSpace[CommonOPS: 'Namespace:mindspore._extends.parse.trope'], make_dict)
136 : (<External, NoShape>, <External, NoShape>) -> (<Func, NoShape>)
137 #scope: (Default)
138 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 18~64/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
139 %5(CNode_20) = resolve(NameSpace[CommonOPS: 'Namespace:mindspore._extends.parse.trope'], MakeTuple)
140 : (<External, NoShape>, <External, NoShape>) -> (<Func, NoShape>)
141 #scope: (Default)
142 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 18~64/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
143 %6(CNode_21) = %5("data_format")
144 : (<String, NoShape>) -> (<Tuple[String], TupleShape(NoShape)>)
145 #scope: (Default)
146 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 18~64/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
147 %7(CNode_22) = resolve(NameSpace[CommonOPS: 'Namespace:mindspore._extends.parse.trope'], MakeTuple)
148 : (<External, NoShape>, <External, NoShape>) -> (<Func, NoShape>)
149 #scope: (Default)
150 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 18~64/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
151 %8(CNode_23) = %7("NCHW")
152 : (<String, NoShape>) -> (<Tuple[String], TupleShape(NoShape)>)
153 #scope: (Default)
154 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 18~64/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
155 %9(CNode_24) = %4(%6, %8)
156 : (<Tuple[String], TupleShape(NoShape)>, <Tuple[String], TupleShape(NoShape)>) -> (<Dictionary[[data_format,],[String]], NoShape>)
157 #scope: (Default)
158 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 18~64/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
159 %10(bias_add_op) = DoUnpackCall(%3, %9)
160 : (<Func, NoShape>, <Dictionary[[data_format,],[String]], NoShape>) -> (<Func, NoShape>)
161 #scope: (Default)
162 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7092, 18~64/ bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW")/
163
164 #------------------------> 2
165 %11(CNode_25) = %10(%para0_input_x, %para0_bias)
166 : (<Tensor[Float32], (3, 8)>, <Ref[Tensor[Float32]], (4)>) -> (<null>)
167 #scope: (Default)
168 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7093, 11~37/ return bias_add_op(input_x, bias)/
169 Return(%11)
170 : (<null>)
171 #scope: (Default)
172 # In file /workspace/mindspore/tools/anaconda3/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:7093, 4~37/ return bias_add_op(input_x, bias)/
173 }
174 # Order:
175 # 1: @bias_add_4:CNode_15{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> SymbolStr: 'Namespace:mindspore.ops.function.nn_func', [2]: ValueNode<Symbol> _get_cache_prim}
176 # 2: @bias_add_4:CNode_16{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> SymbolStr: 'Namespace:mindspore.ops.function.nn_func', [2]: ValueNode<Symbol> P}
177 # 3: @bias_add_4:CNode_17{[0]: ValueNode<Primitive> getattr, [1]: CNode_16, [2]: ValueNode<StringImm> BiasAdd}
178 # 4: @bias_add_4:CNode_26{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> MakeTuple}
179 # 6: @bias_add_4:CNode_18{[0]: CNode_15, [1]: CNode_17}
180 # 7: @bias_add_4:CNode_20{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> MakeTuple}
181 # 8: @bias_add_4:CNode_21{[0]: CNode_20, [1]: ValueNode<StringImm> data_format}
182 # 9: @bias_add_4:CNode_22{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> MakeTuple}
183 # 10: @bias_add_4:CNode_23{[0]: CNode_22, [1]: ValueNode<StringImm> NCHW}
184 # 11: @bias_add_4:CNode_19{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> make_dict}
185 # 12: @bias_add_4:CNode_24{[0]: CNode_19, [1]: CNode_21, [2]: CNode_23}
186 # 13: @bias_add_4:bias_add_op{[0]: ValueNode<Primitive> DoUnpackCall, [1]: CNode_18, [2]: CNode_24}
187 # 14: @bias_add_4:CNode_27{[0]: ValueNode<Primitive> resolve, [1]: ValueNode<NameSpace> CommonOPS: 'Namespace:mindspore._extends.parse.trope', [2]: ValueNode<Symbol> MakeTuple}
188 # 16: @bias_add_4:CNode_25{[0]: bias_add_op, [1]: param_input_x, [2]: param_bias}
189 # 17: @bias_add_4:CNode_28{[0]: ValueNode<Primitive> Return, [1]: CNode_25}
190
191
192 # ===============================================================================================
193 # The total of function graphs in evaluation stack: 3/5 (Ignored 2 internal frames).
194 # ===============================================================================================
195
196
197 # ===============================================================================================
198 # The rest function graphs are the following:
199 # ===============================================================================================
200 No more function graphs.
搜索------------------------>
来到第92行,即推导出错的位置。根据...(%4, %5): (<Tensor[Float32], (3, 8)>, <Ref[Tensor[Float32]], (4)>) -> (
)
可知,算子BiasAdd
的输入是%4
和%5
这两个节点。其中,%4
的shape是[3, 8]
,%5
的shape是[4]
,不符合算子API中BiasAdd
算子的描述bias (Tensor) - 偏置Tensor,shape为 (C)。C必须与 input_x 的通道维度C相同...
的要求,故此处报错。
因此,为了解决该问题,可以修改%4
的shape,或修改%5
(即self.bias
)的shape。
如果修改
%5
(也就是self.bias
)的维度,只需要改成self.bias = Parameter(initializer('zeros', [8]), name="bias")
。如果修改
%4
的shape,先要明白%4
是什么。根据第83行可知,这是一个MatMul
算子,输出shape是[3, 8]
。该算子的输入是(%para0_x1, %3)
,第一个输入的shape是[3, 32]
(即传入的参数x
),第二个输入shape是[32, 8]
(即self.weight
)。为了满足和shape为[4]
的数据BiasAdd
的要求,需要使得%4
的输出shape为[3, 4]
,因此修改self.weight
为self.weight = Parameter(initializer('normal', [32, 4]), name="weight")
。