Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
搜索
关闭
Previous
Previous
Next
Next
更全面的对比GPT4和Claude对MLIR的掌握能力
sockstack
/
163
/
2023-12-31 12:02:38
<p><span style="color: red; font-size: 18px">ChatGPT 可用网址,仅供交流学习使用,如对您有所帮助,请收藏并推荐给需要的朋友。</span><br><a href="https://ckai.xyz/?sockstack§ion=detail" target="__blank">https://ckai.xyz</a><br><br></p> <article class="baidu_pl"><div id="article_content" class="article_content clearfix"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-25cebea3f9.css"> <div id="content_views" class="markdown_views prism-atom-one-dark"> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg><blockquote> <p>本文构造了20个MLIR基础概念的问题以及使用OneFlow IR转换为Tosa IR的5个代码段来评测GPT4和Claude对于MLIR的掌握能力,我的结论是对于基础概念的理解Claude整体上和GPT4持平,而在阅读相关代码片段时Claude表现出了比GPT4更强一点的理解能力。</p> </blockquote> <h1> <a id="0x0__2"></a>0x0. 前言</h1> <p>前面的文章中以Review一段MLIR相关的Pass代码为例子,对比了ChatGPT3.5,Claude,NewBing在该任务上的理解能力。我得出的结论是在该MLIR的相关任务上,Claude>ChatGPT3.5>>Newbing,评论区有人指出GPT4 >> Clude。我感觉上次的任务设计得不是特别好,并没有充分考察ChatGPT和Claude对MLIR的一些核心概念的理解。然后我今天借到了一个GPT4的官方账号 (daquexian 大老师提供的),我打算更全面的对比一下GPT4和Claude在对MLIR的掌握能力。接下来将从MLIR基础概念以及一个OneFlow IR转换为TOSA IR的任务出发,更全面的评价GPT4和Claude对于MLIR的掌握能力。</p> <h1> <a id="0x1_MLIR_5"></a>0x1. MLIR基础概念</h1> <p><strong>Q1: MLIR中的IR是什么?为什么需要多层IR?</strong></p> <p>ChatGPT4的回答:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/be141b0acb48405e8872d36712ff455e.png" alt="在这里插入图片描述"></p> <p>Claude回答:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/359a18835cb64f36ac9d97ea3d39f423.png" alt="在这里插入图片描述"></p> <p>对于MLIR中的IR是什么GPT4的回答更好一些,然后对于为什么需要多层IR这个问题Claude相比于GPT4更加细节一点。</p> <p><strong>Q2. 介绍一下MLIR Dialect的概念,并描述一下Dialect的相关组件。</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/8a654c99d99a47118711f986e9c716be.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/6b09ae5aef7c4efcac6431cfffca8f17.png" alt="在这里插入图片描述"></p> <p>对于这个问题,GPT4的回答非常干净准确,而Claude虽然看起来更细节一些,但是总结得让人看起来很乱。</p> <p><strong>Q3: 在MLIR中如何定义和实现一个新的Dialect?</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/07c9170b3c6b4163b2ad259fa9bf4c6b.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/c3600b943c0b4c1892e8fd22733542a2.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/877ad99370614804a330c113d3982e0a.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/df012043a7ab42e6be924067c5cf2d2c.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/0940b186acc4401aaff56561622fd0ce.png" alt="在这里插入图片描述"></p> <p>按照GPT4给出的步骤基本可以自定义一个Dialect以及相关的组件,不过现在一般对于Dialect以及Operation甚至Pass的定义都依靠了MLIR的ODS系统在相应的<code>*.td</code>文件中定义,并自动生成C++代码更加方便和通用。这里还可以发现GPT4在列举结果的时候,序号一直是1,并没有递增,不知道是不是BUG。</p> <p>可以再看一下Claude的结果:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/039440051a0a42cc80a13f74567748fe.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/14628bf6b64a42c3b236a14a1c72b242.png" alt="在这里插入图片描述"></p> <p>Claude的结果和GPT4的结果没有GPT4那么细节(比如GPT4把头文件的位置都列出来了),但是整体的步骤和GPT4基本一样,也算是比较好的理解了这个问题。</p> <p><strong>Q4: 介绍一下MLIR Dialect中的ModuleOp, FuncOp, Block</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/410d5fe211b34eeba08c1e32e811292b.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/8c995646b8884a12aa9575e4a74769ba.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/c3185bc1373a427f80336f3f5cca6aa7.png" alt="在这里插入图片描述"></p> <p>GPT4的回答相比于Claude主要多了几个代码示例,但主体的内容两者是基本一样的。</p> <p><strong>Q5: 介绍一下MLIR Dialect中的Operation, Value, Type, Attribute</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/ba8f31d687fb47e59223a0c507634116.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/036a2700ab9341768b454e3b475d7950.png" alt="在这里插入图片描述"></p> <p>对概念的解释还是比较准确的,接下来看下Claude的结果:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/069a5138d5e54639b3c02ee0bbb32e5b.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/40bd4e9bc10e44daa4f801ba364cc286.png" alt="在这里插入图片描述"></p> <p>对于这个问题,感觉Claude的回答是要比GPT4的回答好一点的。</p> <p>为了进一步对比上述概念的理解,我们以MLIR的Toy Dialect示例中的transpose op定义为例,让GPT4和Claude分别详细解释下代码。</p> <p><strong>Q6: 详细解释下这行代码。</strong><code>%t_tensor = "toy.transpose"(%tensor) {inplace = true} : (tensor<2x3xf64>) -> tensor<3x2xf64> loc("example/file/path":12:1</code></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/f7fef653bb2a4b8a8421ee55809cb60b.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/3a2623263eba47689494599d84d75c49.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/147731665c2a4c0281cf37c7fc4bc4ba.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/969d34fb5ed44cf5b08e48d9e47aef16.png" alt="在这里插入图片描述"></p> <p>我们可以看到GPT4的源码分析更加细致,比如GPT4给我们解释了inplace的含义,但Claude并没有。</p> <p><strong>Q7:MLIR的Operation由Operation类和Op类的构成,请说一下它们的区别。</strong> 并说明一下ConstantOp和<code>mlir::Operation *operation</code>之间应该如何转换。</p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/95576bab67174f528cc0f117b24f8393.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/9e74bdec64984bf593077c53c1aa1476.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/1bfcdca4b7424c97b5beacdc9407581a.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/6e9086c3b181402792e639742bc2a7ae.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/8c50389098a744da89ec0be2ca9b3c75.png" alt="在这里插入图片描述"></p> <p>GPT4和Claude的回答都还不错。</p> <p><strong>Q8:介绍一下MLIR Value的2个派生类BlockArgument和OpResult,并说明一下Value和</strong>mlir::Operantion *operation之间如何相互转换。</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/745cdf7c19ed474687e59eccee2867af.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/11eae0d3f2684edd9d5cdf499d551379.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/48f847ef37754431beb1ea561de73a96.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/85ee3540f7174983acb1fa7192867514.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/62c9f44fbb914628a74d5adfa419a56c.png" alt="在这里插入图片描述"></p> <p>对于Value的基础概念,GPT4和Claude都理解得不错。</p> <p><strong>Q9: 介绍一下mlir::Type,mlir::ShapedType以及mlir::TensorType</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/86589065e94a4214b33c0d5e55bc778e.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/f712ea2ab80a4d759dbee2d0e88b10fb.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/efbab505d2924cf5945102bcaa09c4f3.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/0f1711aba7fe45388426a22d4b66ccb0.png" alt="在这里插入图片描述"></p> <p>就这个回答来说,Claude的回答是超越了GPT4的。它不仅为我们解释了概念,还指出这三个Type的派生类之间应该如何转换。</p> <p><strong>Q10: TensorType又可以分成RankedTensorType和UnrankedTensorType,请说明一下它们的区别以及如何判断当前的TensorType是否是Ranked。</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/acfa3b5ac5624b9e9f17ea4eb97c9398.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/67d64360fd7e4d748006a66e9cd111ec.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/a3cc0d49c13b48fc88c8d4531088f119.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/0343e40dc505431eb77ab355b8d0ac8e.png" alt="在这里插入图片描述"><br> 相比于GPT4,针对这个问题Claude的回答显得更好一点。主要好在它列出了RankedTensorType和UnrankedTensorType的主要成员函数并做了对比。</p> <p><strong>Q11: 介绍一下MLIR的Attribute</strong></p> <p>GPT4:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/c3d073f1e4a74b2ea03216d0638b556d.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/51754e6ef51340f582a6c010a30870f0.png" alt="在这里插入图片描述"><br> Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/04a6d6fe48ce4eb8a4a6ada04e2a853e.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/8362e4f517954f25993159ac12342129.png" alt="在这里插入图片描述"></p> <p>对于Attribute的解释,我感觉Claude也是比GPT4好一点。</p> <p><strong>Q12:介绍一下MLIR的Builder和OpBuilder。</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/274b0c17243a4b0abf224547dda8e08f.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/12faf022897a4323b169b60ce3ad46d5.png" alt="在这里插入图片描述"><br> Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/1edfccb3cd7b41359efd12b500aa8772.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/14154560c92f4b45b4e01b7e398ff293.png" alt="在这里插入图片描述"></p> <p>个人感觉GPT4和Claude的回答都不是很完善,如果综合一下GPT4和Claude的结果会更好,这个问题双方算是平手。</p> <p><strong>Q13: 介绍一下MLIR Context和Region</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/d4ec88ecca6545e18f05a9b8183d35e8.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/3d5f6afb2db14e36a6dd84841c962a3a.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/22d2060110004bbfa5691e630347505f.png" alt="在这里插入图片描述"></p> <p>这个问题的回答,感觉Claude更加准确,并且Claude指出了使用Context和Region的一般构建流程。</p> <p>我们知道在MLIR中定义OP有2种方式,一种是直接在C++定义,另外一种是基于ODS进行定义。目前主流的做法就是基于ODS也就是在一个td文件中定义OP,然后利用TableGen自动生成相应的C++代码。MLIR在OpBase.td中已经定义了一些公共的结构,接下来我们就提问GPT4和Claude是否知道这些公共的结构。</p> <p><strong>Q14: MLIR的OpBase.td有哪些公共结构</strong></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/0072144ebfae4c1cbbbf0b69ad5fa8a8.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/e00e93b7654b4c69afd591db909a4212.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/8bb3a5e86dcb483d9ae9d5c5816d5976.png" alt="在这里插入图片描述"></p> <p>我期待的回答应该类似于:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/a74b2628fdcd4649ae74aaad3659f842.png" alt="在这里插入图片描述"></p> <p>显然,GPT4和Claude都没有给出理想的答案,而是随机挑选了一些OpBase.td定义的类,所以这个问题并没有很好的被语言模型理解。</p> <p><strong>Q15: 解释一下TF Dialect中对AvgPool Op使用ODS进行定义涉及到的TableGen语法,AvgPool Op定义的代码如下</strong>:</p> <pre><code class="prism language-cpp"><span class="token macro property"><span class="token directive-hash">#</span> <span class="token directive keyword">https</span><span class="token expression"><span class="token operator">:</span></span><span class="token comment">//github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/tensorflow/ir/tf_generated_ops.td#L965</span></span> def TF_AvgPoolOp <span class="token operator">:</span> TF_Op<span class="token operator"><</span><span class="token string">"AvgPool"</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>Pure<span class="token punctuation">]</span><span class="token operator">></span> <span class="token punctuation">{<!-- --></span>let summary <span class="token operator">=</span> <span class="token string">"Performs average pooling on the input."</span><span class="token punctuation">;</span>let description <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span> Each entry in `output` is the mean of the corresponding size `ksize` window in `value`<span class="token punctuation">.</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>let arguments <span class="token operator">=</span> <span class="token punctuation">(</span>insArg<span class="token operator"><</span>TF_FloatTensor<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span><span class="token number">4</span><span class="token operator">-</span>D with shape `<span class="token punctuation">[</span>batch<span class="token punctuation">,</span> height<span class="token punctuation">,</span> width<span class="token punctuation">,</span> channels<span class="token punctuation">]</span>`<span class="token punctuation">.</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token operator">></span><span class="token operator">:</span>$value<span class="token punctuation">,</span>ConfinedAttr<span class="token operator"><</span>I64ArrayAttr<span class="token punctuation">,</span> <span class="token punctuation">[</span>ArrayMinCount<span class="token operator"><</span><span class="token number">4</span><span class="token operator">></span><span class="token punctuation">]</span><span class="token operator">></span><span class="token operator">:</span>$ksize<span class="token punctuation">,</span>ConfinedAttr<span class="token operator"><</span>I64ArrayAttr<span class="token punctuation">,</span> <span class="token punctuation">[</span>ArrayMinCount<span class="token operator"><</span><span class="token number">4</span><span class="token operator">></span><span class="token punctuation">]</span><span class="token operator">></span><span class="token operator">:</span>$strides<span class="token punctuation">,</span>TF_AnyStrAttrOf<span class="token operator"><</span><span class="token punctuation">[</span><span class="token string">"SAME"</span><span class="token punctuation">,</span> <span class="token string">"VALID"</span><span class="token punctuation">]</span><span class="token operator">></span><span class="token operator">:</span>$padding<span class="token punctuation">,</span>DefaultValuedOptionalAttr<span class="token operator"><</span>TF_ConvnetDataFormatAttr<span class="token punctuation">,</span> <span class="token string">"\"NHWC\""</span><span class="token operator">></span><span class="token operator">:</span>$data_format<span class="token punctuation">)</span><span class="token punctuation">;</span>let results <span class="token operator">=</span> <span class="token punctuation">(</span>outsRes<span class="token operator"><</span>TF_FloatTensor<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">{<!-- --></span>The average pooled output tensor<span class="token punctuation">.</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token operator">></span><span class="token operator">:</span>$output<span class="token punctuation">)</span><span class="token punctuation">;</span>TF_DerivedOperandTypeAttr T <span class="token operator">=</span> TF_DerivedOperandTypeAttr<span class="token operator"><</span><span class="token number">0</span><span class="token operator">></span><span class="token punctuation">;</span> <span class="token punctuation">}</span> </code></pre> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/7ffc75a95d7445018bffbf861e77ef17.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/fe9223ebdcee4baaa48894fd8f0d3f60.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/32832cf4a66944089eb9a0bee2b83e9e.png" alt="在这里插入图片描述"></p> <p>GPT4和Claude的结果比较类似,都是逐行解析了涉及到的TableGen语法。</p> <p><strong>Q16: 请解释一下MLIR中的Bufferization</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/0fb6fcfb400f4a7ebd556ba79ccf60d4.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/33cab057c06f4b319437b30bfe1076fb.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/533f981e24d6456cad660ae170ddfe67.png" alt="在这里插入图片描述"></p> <p>GPT4的回答宏观一些,Claude的回答更具体一些,都有优点。</p> <p><strong>Q17: 对于将分散的memref.alloc操作优化合并成单一的memref.alloc统一分配并通过类似memref.subview进行复用,你有什么建议吗,有现成的类似的pass实现吗</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/c1a7458989904b57be4afe6496f7ddbf.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/7c21e592094a4a139143e606220f9a3d.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/9f38ccf4116d484caba34971da06fac6.png" alt="在这里插入图片描述"></p> <p>对于这个问题,GPT4总结得比Claude更细一些。</p> <p><strong>Q18: 讲一下MLIR中One-Shot Bufferize设计的目的和目前成熟的应用</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/9b2cb5f915d04aec9b46f083761dc8c5.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/9ab3d7bbb68641be8c0bb88d63bf8273.png" alt="在这里插入图片描述"></p> <p>两种回答差不多,不分高下。</p> <p><strong>Q19: https://mlir.llvm.org/docs/Bufferization/ 这个网页你能解读一下吗</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/7de9c45055924bc39b0b03e1eda9217d.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/246b35ae082c4c10a9fc66929b7061d0.png" alt="在这里插入图片描述"></p> <p>GPT4的总结稍微更贴合网页一些,但是自己去浏览网页可以发现还是有一些细节被漏掉了,如果要学习Bufferization还是建议去看下原始的网页。</p> <p><strong>Q20: 解释下linalg的算子融合算法</strong></p> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/3d471d00f16c45e3baa31aad32510585.png" alt="在这里插入图片描述"><br> Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/ccfdff216254427f943e5cc76bd62384.png" alt="在这里插入图片描述"></p> <p>对Linalg的学习有限,个人感觉这两个答案都是比较宏观的,需要查看Linalg的文档或者代码进一步获取相关信息。</p> <p>上面一共问了20个问题,我个人感觉GPT4相比于Claude并没有很大的优势,我的初步结论是就MLIR的基础概念理解来看,GPT4和Claude几乎打成平手。</p> <h1> <a id="0x2__346"></a>0x2. 代码阅读</h1> <p>这一节,我以OneFlow中的OneFlow Dialect中的Op转换为TOSA Dialect中的Op为例子,对比一下GPT4和Claude对相关代码的理解能力。这部分代码在 https://github.com/Oneflow-Inc/oneflow/blob/master/oneflow/ir/lib/OneFlow/Conversion/OneFlowToTosa.cpp 这个c++文件中。</p> <p><strong>Code 1</strong></p> <p>解释一下</p> <pre><code class="prism language-cpp">Type <span class="token function">convertToSignless</span><span class="token punctuation">(</span>MLIRContext<span class="token operator">*</span> context<span class="token punctuation">,</span> Type type<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> ranked_tensor <span class="token operator">=</span> type<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">dyn_cast</span><span class="token generic class-name"><span class="token operator"><</span>RankedTensorType<span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> intTy <span class="token operator">=</span> ranked_tensor<span class="token punctuation">.</span><span class="token function">getElementType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token generic-function"><span class="token function">dyn_cast</span><span class="token generic class-name"><span class="token operator"><</span>IntegerType<span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>intTy<span class="token punctuation">.</span><span class="token function">isSignless</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> <span class="token class-name">RankedTensorType</span><span class="token double-colon punctuation">::</span><span class="token function">get</span><span class="token punctuation">(</span>ranked_tensor<span class="token punctuation">.</span><span class="token function">getShape</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token class-name">IntegerType</span><span class="token double-colon punctuation">::</span><span class="token function">get</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> intTy<span class="token punctuation">.</span><span class="token function">getWidth</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>mlir<span class="token double-colon punctuation">::</span>IntegerType<span class="token double-colon punctuation">::</span>SignednessSemantics<span class="token double-colon punctuation">::</span>Signless<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">return</span> type<span class="token punctuation">;</span> <span class="token punctuation">}</span>FunctionType <span class="token function">convertToSignlessFuncType</span><span class="token punctuation">(</span>MLIRContext<span class="token operator">*</span> context<span class="token punctuation">,</span> FunctionType funcType<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>llvm<span class="token double-colon punctuation">::</span>SmallVector<span class="token operator"><</span>Type<span class="token punctuation">,</span> <span class="token number">4</span><span class="token operator">></span> inputs<span class="token punctuation">;</span>llvm<span class="token double-colon punctuation">::</span>SmallVector<span class="token operator"><</span>Type<span class="token punctuation">,</span> <span class="token number">4</span><span class="token operator">></span> results<span class="token punctuation">;</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> arg <span class="token operator">:</span> funcType<span class="token punctuation">.</span><span class="token function">getInputs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> inputs<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">convertToSignless</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> arg<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> res <span class="token operator">:</span> funcType<span class="token punctuation">.</span><span class="token function">getResults</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> results<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">convertToSignless</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token class-name">FunctionType</span><span class="token double-colon punctuation">::</span><span class="token function">get</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> inputs<span class="token punctuation">,</span> results<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> </code></pre> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/16c0250c4aab4784b3508bcbe0467943.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/be8ca6273ab84e5d84913107d7aaa83b.png" alt="在这里插入图片描述"></p> <p>GPT4和Claude对这段代码的解释基本相同,并且都是正确的。</p> <p><strong>Code2</strong><br> 解释一下:</p> <pre><code class="prism language-cpp"><span class="token keyword">bool</span> <span class="token function">isSignLessTensorOrOther</span><span class="token punctuation">(</span>Type type<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> ranked_tensor <span class="token operator">=</span> type<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">dyn_cast</span><span class="token generic class-name"><span class="token operator"><</span>RankedTensorType<span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> intTy <span class="token operator">=</span> ranked_tensor<span class="token punctuation">.</span><span class="token function">getElementType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token generic-function"><span class="token function">dyn_cast</span><span class="token generic class-name"><span class="token operator"><</span>IntegerType<span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span>intTy<span class="token punctuation">.</span><span class="token function">isUnsigned</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">if</span> <span class="token punctuation">(</span>intTy<span class="token punctuation">.</span><span class="token function">isSigned</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">bool</span> <span class="token function">allSignless</span><span class="token punctuation">(</span>mlir<span class="token double-colon punctuation">::</span>TypeRange types<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> type <span class="token operator">:</span> types<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">isSignLessTensorOrOther</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">bool</span> <span class="token function">allSignless</span><span class="token punctuation">(</span>FunctionType funcType<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> arg <span class="token operator">:</span> funcType<span class="token punctuation">.</span><span class="token function">getInputs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">isSignLessTensorOrOther</span><span class="token punctuation">(</span>arg<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> res <span class="token operator">:</span> funcType<span class="token punctuation">.</span><span class="token function">getResults</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">isSignLessTensorOrOther</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> </code></pre> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/ed39bfda0c434874870d595cfa85e708.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/c1de82e9c8d24416b871e9e6a671d5ca.png" alt="在这里插入图片描述"></p> <p>GPT4和Claude的结果也比较类似,但Claude有个优点就是它对每个函数都给出了一个例子,可以帮助读者更方便的去理解这段代码的含义。</p> <p><strong>Code3</strong></p> <p>解释一下:</p> <pre><code class="prism language-cpp">Value <span class="token function">CreateTransposeValue</span><span class="token punctuation">(</span>Location<span class="token operator">&</span> loc<span class="token punctuation">,</span> ConversionPatternRewriter<span class="token operator">&</span> rewriter<span class="token punctuation">,</span> Value input<span class="token punctuation">,</span>ArrayRef<span class="token operator"><</span><span class="token keyword">int32_t</span><span class="token operator">></span> perms<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">int</span> perms_size <span class="token operator">=</span> perms<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">auto</span> transpose_perms <span class="token operator">=</span> rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">create</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>ConstOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> <span class="token class-name">RankedTensorType</span><span class="token double-colon punctuation">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>perms_size<span class="token punctuation">}</span><span class="token punctuation">,</span> rewriter<span class="token punctuation">.</span><span class="token function">getI32Type</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>rewriter<span class="token punctuation">.</span><span class="token function">getI32TensorAttr</span><span class="token punctuation">(</span>perms<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">auto</span> shape_type <span class="token operator">=</span> input<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token generic-function"><span class="token function">cast</span><span class="token generic class-name"><span class="token operator"><</span>ShapedType<span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>std<span class="token double-colon punctuation">::</span>vector<span class="token operator"><</span><span class="token keyword">int64_t</span><span class="token operator">></span> ranked_type<span class="token punctuation">;</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">auto</span><span class="token operator">&</span> index <span class="token operator">:</span> perms<span class="token punctuation">)</span> ranked_type<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>shape_type<span class="token punctuation">.</span><span class="token function">getDimSize</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">return</span> rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">create</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>TransposeOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> <span class="token class-name">RankedTensorType</span><span class="token double-colon punctuation">::</span><span class="token function">get</span><span class="token punctuation">(</span>ranked_type<span class="token punctuation">,</span> shape_type<span class="token punctuation">.</span><span class="token function">getElementType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> input<span class="token punctuation">,</span> transpose_perms<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>RankedTensorType <span class="token function">CreateTransposeType</span><span class="token punctuation">(</span>ShapedType output<span class="token punctuation">,</span> ArrayRef<span class="token operator"><</span><span class="token keyword">int32_t</span><span class="token operator">></span> perms<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>std<span class="token double-colon punctuation">::</span>vector<span class="token operator"><</span><span class="token keyword">int64_t</span><span class="token operator">></span> ranked_type<span class="token punctuation">;</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> index <span class="token operator">:</span> perms<span class="token punctuation">)</span> ranked_type<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>output<span class="token punctuation">.</span><span class="token function">getDimSize</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">return</span> <span class="token class-name">RankedTensorType</span><span class="token double-colon punctuation">::</span><span class="token function">get</span><span class="token punctuation">(</span>ranked_type<span class="token punctuation">,</span> output<span class="token punctuation">.</span><span class="token function">getElementType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> </code></pre> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/ad4f59a144b14bd982001d1c926592b1.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/e622422ed91b46d0a3931c3ac1a0949b.png" alt="在这里插入图片描述"></p> <p>相比之下,感觉Claude的解释可以胜出。</p> <p><strong>Code4</strong></p> <p>解释一下:</p> <pre><code class="prism language-cpp">Value <span class="token function">CreateBNOp</span><span class="token punctuation">(</span>Location loc<span class="token punctuation">,</span> ConversionPatternRewriter<span class="token operator">&</span> rewriter<span class="token punctuation">,</span> Type output_type<span class="token punctuation">,</span> Value x<span class="token punctuation">,</span>Value mean<span class="token punctuation">,</span> Value variance<span class="token punctuation">,</span> Value epsilon<span class="token punctuation">,</span> Value gamma<span class="token punctuation">,</span> Value beta<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token comment">// sub_op = sub(input, mean)</span><span class="token keyword">auto</span> sub_op0 <span class="token operator">=</span> rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">create</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>SubOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> output_type<span class="token punctuation">,</span> x<span class="token punctuation">,</span> mean<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// add_op0 = add(var, epsilon)</span><span class="token keyword">auto</span> add_op0 <span class="token operator">=</span> rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">create</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>AddOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> variance<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> variance<span class="token punctuation">,</span> epsilon<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// rsqrt_op = rsqrt(add_op0)</span><span class="token keyword">auto</span> rsqrt_op <span class="token operator">=</span> rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">create</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>RsqrtOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> variance<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> add_op0<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// op4 = mul(sub_op, rsqrt_op)</span><span class="token keyword">auto</span> mul_op0 <span class="token operator">=</span> rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">create</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>MulOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> output_type<span class="token punctuation">,</span> sub_op0<span class="token punctuation">,</span> rsqrt_op<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// op5 = mul(mul_op0, gamma)</span><span class="token keyword">auto</span> mul_op1 <span class="token operator">=</span> rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">create</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>MulOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> output_type<span class="token punctuation">,</span> mul_op0<span class="token punctuation">,</span> gamma<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// op6 = add(mul_op1, beta)</span>Value batch_norm <span class="token operator">=</span> rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">create</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>AddOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> output_type<span class="token punctuation">,</span> mul_op1<span class="token punctuation">,</span> beta<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">return</span> batch_norm<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> </code></pre> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/2075e59fc43842e39d98fe55cb7fde06.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/6dc4bbd8df4445749ff4b61ec78df10e.png" alt="在这里插入图片描述"></p> <p>感觉GPT4和Claude对代码的理解是一样的。</p> <p><strong>Code5</strong></p> <p>再看一个卷积Op,解释一下:</p> <pre><code class="prism language-cpp"><span class="token keyword">struct</span> <span class="token class-name">Conv2DOpLowering</span> <span class="token keyword">final</span> <span class="token operator">:</span> <span class="token keyword">public</span> OpConversionPattern<span class="token operator"><</span>Conv2DOp<span class="token operator">></span> <span class="token punctuation">{<!-- --></span><span class="token keyword">public</span><span class="token operator">:</span><span class="token keyword">using</span> OpConversionPattern<span class="token operator"><</span>Conv2DOp<span class="token operator">></span><span class="token double-colon punctuation">::</span>OpConversionPattern<span class="token punctuation">;</span>LogicalResult <span class="token function">matchAndRewrite</span><span class="token punctuation">(</span>Conv2DOp op<span class="token punctuation">,</span> OpAdaptor adaptor<span class="token punctuation">,</span>ConversionPatternRewriter<span class="token operator">&</span> rewriter<span class="token punctuation">)</span> <span class="token keyword">const</span> <span class="token keyword">override</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">auto</span> get_pair_int64_from_array <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span>ArrayAttr arr<span class="token punctuation">)</span> <span class="token operator">-></span> std<span class="token double-colon punctuation">::</span>pair<span class="token operator"><</span><span class="token keyword">int64_t</span><span class="token punctuation">,</span> <span class="token keyword">int64_t</span><span class="token operator">></span> <span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> <span class="token punctuation">{<!-- --></span>arr<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token generic-function"><span class="token function">cast</span><span class="token generic class-name"><span class="token operator"><</span>IntegerAttr<span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getSInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>arr<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token generic-function"><span class="token function">cast</span><span class="token generic class-name"><span class="token operator"><</span>IntegerAttr<span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getSInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">auto</span> stride_pairs <span class="token operator">=</span> <span class="token function">get_pair_int64_from_array</span><span class="token punctuation">(</span>op<span class="token punctuation">.</span><span class="token function">getStrides</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">auto</span> pad_pairs <span class="token operator">=</span> <span class="token function">get_pair_int64_from_array</span><span class="token punctuation">(</span>op<span class="token punctuation">.</span><span class="token function">getPaddingBeforeAttr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">auto</span> dilation_pairs <span class="token operator">=</span> <span class="token function">get_pair_int64_from_array</span><span class="token punctuation">(</span>op<span class="token punctuation">.</span><span class="token function">getDilationRate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">auto</span> pad <span class="token operator">=</span> rewriter<span class="token punctuation">.</span><span class="token function">getDenseI64ArrayAttr</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>pad_pairs<span class="token punctuation">.</span>first<span class="token punctuation">,</span> pad_pairs<span class="token punctuation">.</span>second<span class="token punctuation">,</span> pad_pairs<span class="token punctuation">.</span>first<span class="token punctuation">,</span> pad_pairs<span class="token punctuation">.</span>second<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">auto</span> stride <span class="token operator">=</span> rewriter<span class="token punctuation">.</span><span class="token function">getDenseI64ArrayAttr</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>stride_pairs<span class="token punctuation">.</span>first<span class="token punctuation">,</span> stride_pairs<span class="token punctuation">.</span>second<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">auto</span> dilation <span class="token operator">=</span>rewriter<span class="token punctuation">.</span><span class="token function">getDenseI64ArrayAttr</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>dilation_pairs<span class="token punctuation">.</span>first<span class="token punctuation">,</span> dilation_pairs<span class="token punctuation">.</span>second<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">auto</span> bias <span class="token operator">=</span> op<span class="token punctuation">.</span><span class="token function">getBias</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">auto</span> loc <span class="token operator">=</span> op<span class="token punctuation">.</span><span class="token function">getLoc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>bias<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">const</span> <span class="token keyword">auto</span> output_shape <span class="token operator">=</span> op<span class="token punctuation">.</span><span class="token function">getOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token generic-function"><span class="token function">cast</span><span class="token generic class-name"><span class="token operator"><</span>ShapedType<span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// support nhwc</span><span class="token keyword">const</span> <span class="token keyword">auto</span> output_channels <span class="token operator">=</span> output_shape<span class="token punctuation">.</span><span class="token function">getDimSize</span><span class="token punctuation">(</span>op<span class="token punctuation">.</span><span class="token function">IsNCHW</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token number">1</span> <span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">auto</span> bias_elem_type <span class="token operator">=</span> output_shape<span class="token punctuation">.</span><span class="token function">getElementType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">auto</span> type <span class="token operator">=</span> <span class="token class-name">RankedTensorType</span><span class="token double-colon punctuation">::</span><span class="token function">get</span><span class="token punctuation">(</span>output_channels<span class="token punctuation">,</span> bias_elem_type<span class="token punctuation">)</span><span class="token punctuation">;</span>bias <span class="token operator">=</span> rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">create</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>ConstOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>op<span class="token punctuation">.</span><span class="token function">getLoc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> type<span class="token punctuation">,</span> <span class="token class-name">DenseElementsAttr</span><span class="token double-colon punctuation">::</span><span class="token function">get</span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> rewriter<span class="token punctuation">.</span><span class="token function">getZeroAttr</span><span class="token punctuation">(</span>bias_elem_type<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">auto</span> in <span class="token operator">=</span> op<span class="token punctuation">.</span><span class="token function">getIn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">auto</span> weight <span class="token operator">=</span> op<span class="token punctuation">.</span><span class="token function">getWeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">auto</span> out_type <span class="token operator">=</span> op<span class="token punctuation">.</span><span class="token function">getOut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token generic-function"><span class="token function">cast</span><span class="token generic class-name"><span class="token operator"><</span>ShapedType<span class="token operator">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>out_type<span class="token punctuation">.</span><span class="token function">getRank</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token function">LOG</span><span class="token punctuation">(</span>FATAL<span class="token punctuation">)</span> <span class="token operator"><<</span> <span class="token string">"Failed to lowering oneflow op"</span><span class="token punctuation">;</span>op<span class="token operator">-></span><span class="token function">dump</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// support nhwc</span><span class="token keyword">if</span> <span class="token punctuation">(</span>op<span class="token punctuation">.</span><span class="token function">IsNCHW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">const</span> <span class="token keyword">auto</span> perms <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">auto</span> reverse_perms <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">;</span>in <span class="token operator">=</span> <span class="token function">CreateTransposeValue</span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> rewriter<span class="token punctuation">,</span> in<span class="token punctuation">,</span> perms<span class="token punctuation">)</span><span class="token punctuation">;</span>weight <span class="token operator">=</span> <span class="token function">CreateTransposeValue</span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> rewriter<span class="token punctuation">,</span> weight<span class="token punctuation">,</span> perms<span class="token punctuation">)</span><span class="token punctuation">;</span>out_type <span class="token operator">=</span> <span class="token function">CreateTransposeType</span><span class="token punctuation">(</span>out_type<span class="token punctuation">,</span> perms<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">auto</span> conv2d <span class="token operator">=</span>rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">create</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>Conv2DOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> out_type<span class="token punctuation">,</span> in<span class="token punctuation">,</span> weight<span class="token punctuation">,</span> bias<span class="token punctuation">,</span> pad<span class="token punctuation">,</span> stride<span class="token punctuation">,</span> dilation<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">auto</span> res <span class="token operator">=</span> <span class="token function">CreateTransposeValue</span><span class="token punctuation">(</span>loc<span class="token punctuation">,</span> rewriter<span class="token punctuation">,</span> conv2d<span class="token punctuation">,</span> reverse_perms<span class="token punctuation">)</span><span class="token punctuation">;</span>rewriter<span class="token punctuation">.</span><span class="token function">replaceOp</span><span class="token punctuation">(</span>op<span class="token punctuation">,</span> <span class="token punctuation">{<!-- --></span>res<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{<!-- --></span>rewriter<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">replaceOpWithNewOp</span><span class="token generic class-name"><span class="token operator"><</span>tosa<span class="token double-colon punctuation">::</span>Conv2DOp<span class="token operator">></span></span></span><span class="token punctuation">(</span>op<span class="token punctuation">,</span> out_type<span class="token punctuation">,</span> in<span class="token punctuation">,</span> weight<span class="token punctuation">,</span> bias<span class="token punctuation">,</span> pad<span class="token punctuation">,</span> stride<span class="token punctuation">,</span>dilation<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token function">success</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> </code></pre> <p>GPT4:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/fe30c77b76434ab09eb5aa56b418fdf2.png" alt="在这里插入图片描述"></p> <p>Claude:</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/7ab4b408fc2a4fa68659570532023c6b.png" alt="在这里插入图片描述"></p> <p>我们再问一下,为什么format为"NHWC"的时候要加transpose操作?</p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/4574567e1fe9428f8035ab28c453d4f4.png" alt="在这里插入图片描述"></p> <p><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/f3704d5af69e4e50b479f16b19a8df7e.png" alt="在这里插入图片描述"></p> <p>可以看到Claude理解了我的问题,并指出这是因为Tosa的conv2d不支持nhwc数据格式,所以需要加transpose。而GPT4在这个问题上就没有理解我的意思并字面意义的重复了我的问题。</p> <h1> <a id="0x3__578"></a>0x3. 总结</h1> <p>本文构造了20个MLIR基础概念的问题以及使用OneFlow IR转换为Tosa IR的5个代码段来评测GPT4和Claude对于MLIR的掌握能力,我的结论是对于基础概念的理解Claude整体上和GPT4持平,而在阅读相关代码片段时Claude表现出了比GPT4更强一点的理解能力。</p> </div> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-98b95bb57c.css" rel="stylesheet"> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-c216769e99.css" rel="stylesheet"> </div> <div id="treeSkill"></div> </article>
更全面的对比GPT4和Claude对MLIR的掌握能力
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-12-31
修改于
2024-10-03
上一篇:软件:常用 Linux 软件汇总,值得收藏
下一篇:chatgpt赋能python:Python如何将空格变成换行
尚未登录
登录 / 注册
文章分类
博客重构之路
5
Spring Boot简单入门
4
k8s 入门教程
0
MySQL 知识
1
NSQ 消息队列
0
ThinkPHP5 源码分析
5
使用 Docker 从零开始搭建私人代码仓库
3
日常开发汇总
3
标签列表
springboot
hyperf
swoole
webman
php
多线程
数据结构
docker
k8s
thinkphp
mysql
tailwindcss
flowbite
css
前端