[{"data":1,"prerenderedAt":613},["ShallowReactive",2],{"content-query-s2y8nToP5Q":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":607,"_id":608,"_source":609,"_file":610,"_stem":611,"_extension":612},"/technology-blogs/zh/1057","zh",false,"","小孢子的神奇之旅-如何阅读MindSpore报错信息(2)","自修孢问题解决的三步巅峰论，“1）理解问题原因 2）找到问题出在哪里 3）改了","2022-03-03","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/03/03/1340c61dde68436b9dac6332f17e0a68.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":604},"root",[17,25,31,44,52,57,62,67,72,77,84,89,94,99,104,109,114,119,124,129,134,139,144,149,154,159,164,169,174,179,184,189,194,199,204,209,213,218,223,228,233,238,243,248,253,258,263,268,273,278,283,288,293,298,303,308,313,318,323,332,337,342,347,352,357,361,366,373,381,386,390,394,398,403,408,412,417,421,425,430,438,443,447,451,455,459,463,468,472,476,481,486,491,496,501,505,513,517,522,526,533,537,542,551,556,561,565,569,573,578,582,586,590,594,599],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"小孢子的神奇之旅-如何阅读mindspore报错信息2",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":24,"value":30},"作者：自我修行的孢子",{"type":18,"tag":26,"props":32,"children":33},{},[34,36],{"type":24,"value":35},"链接：",{"type":18,"tag":37,"props":38,"children":42},"a",{"href":39,"rel":40},"https://bbs.huaweicloud.com/forum/forum.php?mod=viewthread&tid=171913",[41],"nofollow",[43],{"type":24,"value":39},{"type":18,"tag":26,"props":45,"children":46},{},[47],{"type":18,"tag":48,"props":49,"children":51},"img",{"alt":7,"src":50},"https://bbs-img.huaweicloud.com/data/forums/attachment/word/image1(21).png",[],{"type":18,"tag":26,"props":53,"children":54},{},[55],{"type":24,"value":56},"先回忆下自修孢问题解决的三步巅峰论，“1）理解问题原因 2）找到问题出在哪里 3）改了”，我们在上篇文章应用三步论解决了MindSpore Primitive算子执行的一个小问题，如果你读了MindSpore官网对算子分类的介绍，你会只知道还有一类nn算子。",{"type":18,"tag":26,"props":58,"children":59},{},[60],{"type":24,"value":61},"我们看下MindSpore官网对这两种算子的解释：",{"type":18,"tag":26,"props":63,"children":64},{},[65],{"type":24,"value":66},"• Primitive算子是开放给用户的最低阶算子接口，一个Primitive算子对应一个原语，它封装了底层的Ascend、GPU、AICPU、CPU等多种算子的具体实现，为用户提供基础算子能力。",{"type":18,"tag":26,"props":68,"children":69},{},[70],{"type":24,"value":71},"• nn算子是对低阶API的封装，主要包括卷积层算子、池化层算子、损失函数、优化器等。还提供了部分与Primitive算子同名的接口，主要作用是对Primitive算子进行进一步封装，为用户提供更友好的API，当nn算子功能满足用户的要求时可以直接使用nn算子，而当nn算子功能无法满足用户特定要求时可以使用低阶的Primitive算子实现特定的功能",{"type":18,"tag":26,"props":73,"children":74},{},[75],{"type":24,"value":76},"官网认真严肃的说明了两种算子的区别，孢子尝试用自带的歪风格来做一些自己的解读，见下图：",{"type":18,"tag":26,"props":78,"children":79},{},[80],{"type":18,"tag":48,"props":81,"children":83},{"alt":7,"src":82},"https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/07/201412czcncdlnmygrkevb.png",[],{"type":18,"tag":26,"props":85,"children":86},{},[87],{"type":24,"value":88},"• Primitive算子可以认为是一些基础调料，你可以根据自己的口味按需组合出让自己满意的味道。",{"type":18,"tag":26,"props":90,"children":91},{},[92],{"type":24,"value":93},"• nn算子则是满足大众口味调好的蘸料，也就是nn算子是由一个或者多个Primitive算子组合而成，你自己不用费脑筋调味，直接用就完了。",{"type":18,"tag":26,"props":95,"children":96},{},[97],{"type":24,"value":98},"即nn算子是由Primitive组成的，提供特定功能的api，但如果你觉得这个api不能满足你的要求，你可以自己用Primitive算子来组合实现，例如：你吃火锅非得用芥辣+花椒做蘸料才爽快，估计你是找不到有这种成品蘸料了，就需要你自己来配置这种蘸料。所以选择哪种API，还是要从你的需求出发。",{"type":18,"tag":26,"props":100,"children":101},{},[102],{"type":24,"value":103},"上次我们分析了Primitive算子的报错信息，本次来探索下nn算子的报错有没有啥不同。老规矩，没有困难制造困难，我们先来写段错误代码。",{"type":18,"tag":26,"props":105,"children":106},{},[107],{"type":24,"value":108},"错误代码示例如下：",{"type":18,"tag":26,"props":110,"children":111},{},[112],{"type":24,"value":113},"import numpy as np",{"type":18,"tag":26,"props":115,"children":116},{},[117],{"type":24,"value":118},"import mindspore",{"type":18,"tag":26,"props":120,"children":121},{},[122],{"type":24,"value":123},"import mindspore.nn as nn",{"type":18,"tag":26,"props":125,"children":126},{},[127],{"type":24,"value":128},"from mindspore import context, Tensor",{"type":18,"tag":26,"props":130,"children":131},{},[132],{"type":24,"value":133},"x = Tensor(np.array([[[[1, 2, 3, 4], [3, 4, 5, 6]]]]), mindspore.float32)",{"type":18,"tag":26,"props":135,"children":136},{},[137],{"type":24,"value":138},"net = nn.Moments(axis=5, keep_dims=False)",{"type":18,"tag":26,"props":140,"children":141},{},[142],{"type":24,"value":143},"output = net(x)",{"type":18,"tag":26,"props":145,"children":146},{},[147],{"type":24,"value":148},"print(output)",{"type":18,"tag":26,"props":150,"children":151},{},[152],{"type":24,"value":153},"接下来是见证问题的时刻：",{"type":18,"tag":26,"props":155,"children":156},{},[157],{"type":24,"value":158},"[ERROR] ANALYZER(7282,7fc2f86b1740,python):2021-12-02-10:53:10.506.281 [mindspore/ccsrc/pipeline/jit/static_analysis/async_eval_result.cc:84] HandleException] Exception happened, check the information as below.",{"type":18,"tag":26,"props":160,"children":161},{},[162],{"type":24,"value":163},"The function call stack (See file '/root/mindspore_test/rank_0/om/analyze_fail.dat' for more details):",{"type":18,"tag":26,"props":165,"children":166},{},[167],{"type":24,"value":168},"# 0 In file /root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/nn/layer/math.py(1003)",{"type":18,"tag":26,"props":170,"children":171},{},[172],{"type":24,"value":173},"if tensor_dtype == mstype.float16:",{"type":18,"tag":26,"props":175,"children":176},{},[177],{"type":24,"value":178},"# 1 In file /root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/nn/layer/math.py(1007)",{"type":18,"tag":26,"props":180,"children":181},{},[182],{"type":24,"value":183},"if not self.keep_dims:",{"type":18,"tag":26,"props":185,"children":186},{},[187],{"type":24,"value":188},"# 2 In file /root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/nn/layer/math.py(1005)",{"type":18,"tag":26,"props":190,"children":191},{},[192],{"type":24,"value":193},"mean = self.reduce_mean(x, self.axis)",{"type":18,"tag":26,"props":195,"children":196},{},[197],{"type":24,"value":198},"^",{"type":18,"tag":26,"props":200,"children":201},{},[202],{"type":24,"value":203},"Traceback (most recent call last):",{"type":18,"tag":26,"props":205,"children":206},{},[207],{"type":24,"value":208},"File \"test1.py\", line 26, in",{"type":18,"tag":26,"props":210,"children":211},{},[212],{"type":24,"value":143},{"type":18,"tag":26,"props":214,"children":215},{},[216],{"type":24,"value":217},"File \"/root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/nn/cell.py\", line 479, in __call__",{"type":18,"tag":26,"props":219,"children":220},{},[221],{"type":24,"value":222},"out = self.compile_and_run(*args)",{"type":18,"tag":26,"props":224,"children":225},{},[226],{"type":24,"value":227},"File \"/root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/nn/cell.py\", line 802, in compile_and_run",{"type":18,"tag":26,"props":229,"children":230},{},[231],{"type":24,"value":232},"self.compile(*inputs)",{"type":18,"tag":26,"props":234,"children":235},{},[236],{"type":24,"value":237},"File \"/root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/nn/cell.py\", line 789, in compile",{"type":18,"tag":26,"props":239,"children":240},{},[241],{"type":24,"value":242},"_cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)",{"type":18,"tag":26,"props":244,"children":245},{},[246],{"type":24,"value":247},"File \"/root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/common/api.py\", line 661, in compile",{"type":18,"tag":26,"props":249,"children":250},{},[251],{"type":24,"value":252},"result = self._graph_executor.compile(obj, args_list, phase, use_vm)",{"type":18,"tag":26,"props":254,"children":255},{},[256],{"type":24,"value":257},"File \"/root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/ops/operations/math_ops.py\", line 498, in __infer__",{"type":18,"tag":26,"props":259,"children":260},{},[261],{"type":24,"value":262},"return self.do_infer(input_x, axis)",{"type":18,"tag":26,"props":264,"children":265},{},[266],{"type":24,"value":267},"File \"/root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/ops/operations/math_ops.py\", line 467, in do_infer",{"type":18,"tag":26,"props":269,"children":270},{},[271],{"type":24,"value":272},"out_shape = _infer_shape_reduce(input_shp, axis_v, self.keep_dims, self.name)",{"type":18,"tag":26,"props":274,"children":275},{},[276],{"type":24,"value":277},"File \"/root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/ops/operations/math_ops.py\", line 44, in _infer_shape_reduce",{"type":18,"tag":26,"props":279,"children":280},{},[281],{"type":24,"value":282},"reduce_one_axis(axis)",{"type":18,"tag":26,"props":284,"children":285},{},[286],{"type":24,"value":287},"File \"/root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/ops/operations/math_ops.py\", line 34, in reduce_one_axis",{"type":18,"tag":26,"props":289,"children":290},{},[291],{"type":24,"value":292},"validator.check_int_range(one_axis, -dim, dim, Rel.INC_LEFT, 'axis', prim_name)",{"type":18,"tag":26,"props":294,"children":295},{},[296],{"type":24,"value":297},"File \"/root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/_checkparam.py\", line 413, in check_int_range",{"type":18,"tag":26,"props":299,"children":300},{},[301],{"type":24,"value":302},"return check_number_range(arg_value, lower_limit, upper_limit, rel, int, arg_name, prim_name)",{"type":18,"tag":26,"props":304,"children":305},{},[306],{"type":24,"value":307},"File \"/root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/_checkparam.py\", line 210, in check_number_range",{"type":18,"tag":26,"props":309,"children":310},{},[311],{"type":24,"value":312},"arg_name, prim_name, rel_str, arg_value, type(arg_value).__name__))",{"type":18,"tag":26,"props":314,"children":315},{},[316],{"type":24,"value":317},"ValueError: `axis` in `ReduceMean` should be in range of [-4, 4), but got 5.000e+00 with type `int`.",{"type":18,"tag":26,"props":319,"children":320},{},[321],{"type":24,"value":322},"报错信息似乎比上次的多了一些，我们依然用三步论来看这个问题。",{"type":18,"tag":26,"props":324,"children":325},{},[326],{"type":18,"tag":327,"props":328,"children":329},"strong",{},[330],{"type":24,"value":331},"第一步：阅读问题描述，理解问题原因。",{"type":18,"tag":26,"props":333,"children":334},{},[335],{"type":24,"value":336},"回忆下上篇帖子，我们先找到python抛出的异常信息。在MindSpore打印的信息比较多的时候，如何查找python抛出的异常信息呢？这里的技巧是查找关键字“Traceback (most recent call last):”到“xxxError(不同错误类型会有不同的描述，例如：ValueError):……”之间的记录，如下：",{"type":18,"tag":26,"props":338,"children":339},{},[340],{"type":24,"value":341},"Traceback (most recent call last): # 堆栈信息关键字",{"type":18,"tag":26,"props":343,"children":344},{},[345],{"type":24,"value":346},"…… # 堆栈信息",{"type":18,"tag":26,"props":348,"children":349},{},[350],{"type":24,"value":351},"ValueError: …… # 错误描述关键字",{"type":18,"tag":26,"props":353,"children":354},{},[355],{"type":24,"value":356},"基于上边的技巧，我们找到本次的问题描述如下：",{"type":18,"tag":26,"props":358,"children":359},{},[360],{"type":24,"value":317},{"type":18,"tag":26,"props":362,"children":363},{},[364],{"type":24,"value":365},"从描述看是ReduceMean这个算子的参数axis是5，没在[-4, 4)范围内。从官网描述可见ReduceMean是个ops接口（即是一个Primitive算子）。",{"type":18,"tag":26,"props":367,"children":368},{},[369],{"type":18,"tag":48,"props":370,"children":372},{"alt":7,"src":371},"https://bbs-img.huaweicloud.com/data/forums/attachment/word/image3(22).png",[],{"type":18,"tag":26,"props":374,"children":375},{},[376],{"type":18,"tag":327,"props":377,"children":378},{},[379],{"type":24,"value":380},"第二步：阅读堆栈信息，找到问题在哪里(具体的代码行)。",{"type":18,"tag":26,"props":382,"children":383},{},[384],{"type":24,"value":385},"这一步我们要在代码中找到算子的位置，基于堆栈可以找到output = net(x)这行代码",{"type":18,"tag":26,"props":387,"children":388},{},[389],{"type":24,"value":203},{"type":18,"tag":26,"props":391,"children":392},{},[393],{"type":24,"value":208},{"type":18,"tag":26,"props":395,"children":396},{},[397],{"type":24,"value":143},{"type":18,"tag":26,"props":399,"children":400},{},[401],{"type":24,"value":402},"……",{"type":18,"tag":26,"props":404,"children":405},{},[406],{"type":24,"value":407},"继续查看代码，net是由Moments算子赋值而来。这里就有点意思了，貌似这个报错不讲武德，不按照套路打呀。代码里并没有找到ReduceMean这个算子，只有一个Moments算子，它们是什么关系呢？",{"type":18,"tag":26,"props":409,"children":410},{},[411],{"type":24,"value":402},{"type":18,"tag":26,"props":413,"children":414},{},[415],{"type":24,"value":416},"net = nn.Moments(axis=5, keep_dims=False) # nn.Moment赋值给net",{"type":18,"tag":26,"props":418,"children":419},{},[420],{"type":24,"value":143},{"type":18,"tag":26,"props":422,"children":423},{},[424],{"type":24,"value":402},{"type":18,"tag":26,"props":426,"children":427},{},[428],{"type":24,"value":429},"这就是前面提到的蘸料与基础调料的关系了，这里的ReduceMean是组成nn算子Moments的元素，Moments传入的参数axis没有在正确范围内，导致了ReduceMean执行出错。",{"type":18,"tag":26,"props":431,"children":432},{},[433],{"type":18,"tag":327,"props":434,"children":435},{},[436],{"type":24,"value":437},"第三步：解决问题，达到人生巅峰。",{"type":18,"tag":26,"props":439,"children":440},{},[441],{"type":24,"value":442},"知道了原因，修复这个问题也就简单了，修改axis到正确范围，问题就解决了。",{"type":18,"tag":26,"props":444,"children":445},{},[446],{"type":24,"value":113},{"type":18,"tag":26,"props":448,"children":449},{},[450],{"type":24,"value":118},{"type":18,"tag":26,"props":452,"children":453},{},[454],{"type":24,"value":123},{"type":18,"tag":26,"props":456,"children":457},{},[458],{"type":24,"value":128},{"type":18,"tag":26,"props":460,"children":461},{},[462],{"type":24,"value":133},{"type":18,"tag":26,"props":464,"children":465},{},[466],{"type":24,"value":467},"net = nn.Moments(axis=1, keep_dims=False) #修改axis到正确范围",{"type":18,"tag":26,"props":469,"children":470},{},[471],{"type":24,"value":143},{"type":18,"tag":26,"props":473,"children":474},{},[475],{"type":24,"value":148},{"type":18,"tag":26,"props":477,"children":478},{},[479],{"type":24,"value":480},"回顾下本次冲击巅峰的过程，主要在Primitive(ReduceMean)->nn(Moments)映射的地方爬了个坡，其他过程根据已掌握的技能还算顺利。本次的经验告诉我们，一些问题的解决需要你翘起脚往里边看看，你就豁然开朗了。",{"type":18,"tag":26,"props":482,"children":483},{},[484],{"type":24,"value":485},"这里我们可以稍稍看下Moments的代码，Moments是一个继承了Cell的子类，也确实如我们分析，用到了ReduceMean算子。",{"type":18,"tag":26,"props":487,"children":488},{},[489],{"type":24,"value":490},"class Moments(Cell):",{"type":18,"tag":26,"props":492,"children":493},{},[494],{"type":24,"value":495},"......",{"type":18,"tag":26,"props":497,"children":498},{},[499],{"type":24,"value":500},"def __init__(self, axis=None, keep_dims=None):",{"type":18,"tag":26,"props":502,"children":503},{},[504],{"type":24,"value":495},{"type":18,"tag":26,"props":506,"children":507},{},[508],{"type":18,"tag":327,"props":509,"children":510},{},[511],{"type":24,"value":512},"self.reduce_mean = P.ReduceMean(keep_dims=True)",{"type":18,"tag":26,"props":514,"children":515},{},[516],{"type":24,"value":495},{"type":18,"tag":26,"props":518,"children":519},{},[520],{"type":24,"value":521},"def construct(self, x):",{"type":18,"tag":26,"props":523,"children":524},{},[525],{"type":24,"value":495},{"type":18,"tag":26,"props":527,"children":528},{},[529],{"type":18,"tag":327,"props":530,"children":531},{},[532],{"type":24,"value":193},{"type":18,"tag":26,"props":534,"children":535},{},[536],{"type":24,"value":495},{"type":18,"tag":26,"props":538,"children":539},{},[540],{"type":24,"value":541},"return mean, variance",{"type":18,"tag":26,"props":543,"children":544},{},[545],{"type":18,"tag":37,"props":546,"children":549},{"href":547,"rel":548},"https://gitee.com/mindspore/mindspore/tree/master/mindspore",[41],[550],{"type":24,"value":547},{"type":18,"tag":26,"props":552,"children":553},{},[554],{"type":24,"value":555},"不过这里Cell又是个啥？孢子先挖个坑下回填吧。",{"type":18,"tag":26,"props":557,"children":558},{},[559],{"type":24,"value":560},"今天的故事是不是就结束了？等下，下边这段报错信息又是个啥？心细的同学应该发现了本次的报错信息有如下明显的不同。出现了一个叫做“The function call stack”的堆栈信息。",{"type":18,"tag":26,"props":562,"children":563},{},[564],{"type":24,"value":163},{"type":18,"tag":26,"props":566,"children":567},{},[568],{"type":24,"value":168},{"type":18,"tag":26,"props":570,"children":571},{},[572],{"type":24,"value":173},{"type":18,"tag":26,"props":574,"children":575},{},[576],{"type":24,"value":577},"# 1 In file /root/anaconda3/envs/test/lib/python3.7/site-packages/mindspore/nn/layer/math.py(1007) #####更新",{"type":18,"tag":26,"props":579,"children":580},{},[581],{"type":24,"value":183},{"type":18,"tag":26,"props":583,"children":584},{},[585],{"type":24,"value":188},{"type":18,"tag":26,"props":587,"children":588},{},[589],{"type":24,"value":193},{"type":18,"tag":26,"props":591,"children":592},{},[593],{"type":24,"value":198},{"type":18,"tag":26,"props":595,"children":596},{},[597],{"type":24,"value":598},"这个……静态图模式您听说过吗？…… 又是一个新的故事，继续刨坑吧。",{"type":18,"tag":26,"props":600,"children":601},{},[602],{"type":24,"value":603},"自修孢金句“翘起脚往里瞧瞧，风景独好”",{"title":7,"searchDepth":605,"depth":605,"links":606},4,[],"markdown","content:technology-blogs:zh:1057.md","content","technology-blogs/zh/1057.md","technology-blogs/zh/1057","md",1776506111768]