admin管理员组

文章数量:1354164

Claude在MLIR代码分析上完全超越了ChatGPT并表现十分惊艳,请阅读全文或者自己注册感受它的强大。结论:在本文的任务中,Claude > ChatGPT >> NewBing

0x0. 前言

这里将以oneflow IR部分中的一个Codegen任务(目标是在mlir codegen中支持oneflow stream,用oneflow stream替换pass中自己生成的stream,PR链接为:https://github/Oneflow-Inc/oneflow/pull/10149)为例,来对比一下newibing(chatgpt)和claude对mlir的理解能力。claude是Anthropic公司推出的类似于chatgpt的聊天机器人,这家公司是OpenAI的最大竞争对手之一,因为创办这家公司的人也是OpenAI的前员工。然后Claude是参考这个issue: https://www.zhihu/question/594115372/answer/2988759047 将其直接添加到slack里进行对话。

0x1. PR简介

PR链接为:https://github/Oneflow-Inc/oneflow/pull/10149

这个PR实现了3个Pass (定义在 OneFlowPasses.td),也就是:

def EliminateAllocOpsPass : Pass<"eliminate-alloc-ops", "ModuleOp"> {
   
  let summary = "";
  let constructor = "mlir::oneflow::createEliminateAllocOpsPass()";
  let dependentDialects = ["pdl_interp::PDLInterpDialect", "pdl::PDLDialect"];
}

def AppendOneFlowStreamPass : Pass<"append-ofstream", "ModuleOp"> {
   
  let summary = "append oneflow stream to gpu function arguments";
  let constructor = "mlir::oneflow::createAppendOneFlowStreamPass()";
}

def MgpuToOneFlowStreamPass : Pass<"mgpu-to-ofstream", "ModuleOp"> {
   
  let summary = "convert mlir abi about mgpu to oneflow stream, this pass should be invoked after append-ofstream pass";
  let constructor = "mlir::oneflow::createMgpuToOneFlowStreamPass()";
}

EliminateAllocOpsPass用来消除IR中的无效memref.alloc指令,AppendOneFlowStreamPass给GPU相关的函数添加GPU启动kernel需要的stream参数,MgpuToOneFlowStreamPass发生在AppendOneFlowStreamPass执行之后(它生成了stream参数)并把mgpu相关的stream abi替换为oneflow stream abi。

我们分别使用newbing和claude来让它们分析一下这几行OneFlowPasses.td中定义的Pass意图:

newbing:

newbing直接看不懂,其实我感觉claude也应该看不懂吧,抱着怀疑的态度问一下。


太疯狂了,claude不仅读懂了td文件的代码,甚至为我们列出了这个代码涉及到的MLIR概念。感觉是训练数据考虑了MLIR相关的预料?接下来我们再对比下C++实现的Pass代码。

0x2. 对比具体实现

PR链接为:https://github/Oneflow-Inc/oneflow/pull/10149

0x2.1 EliminateAllocOpsPass

EliminateAllocOpsPass使用MLIR提供的PDL语言来完成Pattern的匹配和重写,具体实现在 oneflow/ir/lib/OneFlow/PDLL/AllocEliminationPatterns.pdll

#include "OneFlow/OneFlowOps.td"

Constraint IsFuncArguments(value: Value) [{
   
  return success(llvm::dyn_cast<mlir::BlockArgument>(value));
}];

Pattern {
   
  let alloc = op<memref.alloc>();
  let copy = op<memref.copy>(alloc.0, arg: IsFuncArguments);

  rewrite alloc with {
   
    erase copy;
    replace alloc with arg;
  };
}

接下来,我们分别对比一下newbing和chatgpt对它的分析结果。

newbing并不能解析出这段代码是MLIR的PDL语言,当然也无法理解代码内容。我们可以再使用Claude试试。

个人感觉这个解释是非常强大且精准的,Claude的答案非常惊艳。

0x2.2 AppendOneFlowStreamPass

接下来我们看一下AppendOneFlowStreamPass的实现,这个实现是在oneflow/ir/lib/OneFlow/Transform/OneFlowStream.cpp这个文件,具体代码如下:

struct AppendOneFlowStreamPattern final :

本文标签: 为例对比测试代码ChatGptclaude