Closed
[Requirement|需求建议]: 基于scf+token 的IR-1.3 #1185
Jie YinAdd on Apr 13Closed at Sep 16
Apr 13 Added label: requirement
Apr 13 Change title to "[Requirement|需求建议]: 基于scf+token 的IR-1.3", originally titled "[Requirement|需求建议]: 基于scf+token 的IR".
Apr 13 Change title to "[Requirement|需求建议]: 基于scf+token 的IR-1.3", originally titled "[Requirement|需求建议]: 基于scf+token 的IR".
Apr 13 Modify description of issue
Apr 13 Modify description of issue
Apr 13 Associated pull request
92 messages are collapsed here。 See more
Aug 5 Associated pull request
Aug 27 Deleted associated pull request: fix(pil): Simplify loop bounds and add jit cache logs
LiuChang
Sep 16 Comments:
Sep 16 Comments:
相关需求已合入,此issue关闭


Sep 16 changed custom state from 待办的 to 已解决
Sep 16 Close issue
Thanks for sending an requirement! Please fill in the following template to help quickly solve your problem.
Background(背景信息)
由于当前控制流缺少返回值,并且针对同一块内存区域的写后写和读后写存在并发操作下正确性问题,导致存在部分前端表达下,框架难以生成正确的代码。因此需要扩展IR的功能,具备以下功能:
Origin(信息来源)
Frontend Issue
Benefit / Necessity(价值/作用)
支持完备的前端表达,表达能力具备 torch 的程度
Design(设计方案)
PIL(Python Intermediate Language)
对于复杂的前端表达语法糖,将其转换为标准的简单语法,是一种简化分析的方法。参考 CIL 是一种将复杂前端语法简化,并且用于后续翻译的方法。前端首先采用 IL 的方式,将原生的 python 翻译成 PIL,相比原生 Python,具有以下特点:
id = expr(id | STAR id)* = idid.id = id_or_constid[slice] = id_or_constid_or_const binop id_or_constunaryop id_or_constid_or_const(id_or_const*)id_or_const . idid_or_const [slice][id_or_const*, STAR id_or_const](id_or_const*, STAR id_or_const){id_or_const*, STAR id_or_const}{(id_or_const: id_or_const)*, STAR STAR id_or_const }其中 slice 是 id_or_const ,或者
(id_or_const | None, id_or_const | None, id_or_const | None)*if id_or_const stmt* else stmt*for id_or_const in id_or_const stmt* stmt*while id_or_const stmt*breakcontinuereturn id_or_const | None通过变换,可以将所有的原生 python 转为 PIL
IR-1.3
由于当前存量代码中包含大量的
LogicalTensor和RawTensor,为了平滑演进,当前提出了 IR-1.3 ,即支持以下特性PIL -> IR1.3
针对IR的形式化定义,给出前端形式化的翻译:前端是一个形式化的状态机,其状态为 S,表示一个映射。其 key 是一个前端语言的左值,val 是 SSA IR 中每一条的返回值的名字
a(变量)、a.b(属性)、a[c](slice)定义函数
Parse(S, c): 给定当前的状态和目标代码,输出翻译后的 IR 结果S: 当前的状态vc:目标代码return:<M, C, R>,其中M: 左值表达式执行完更新后的状态SC: 输出的代码R: 当c是一个表达式时,r对应一个 python 对象,或者 dynamic 对象。该对象包含两个字段v: 表示<TensorVar>/<ScalarVar>/<ConstInt>/<ConstFloat>o: 表示<TokenVar>或者_定义以下标记
c0 :: c1: 表示在c0后接上c1let n = b: 表示将n赋值bL(n): 表示从前端语言的变量 n 开始的左值。因为左值通常都从单一符号开始。例如不会出现a op b结果是一个和 a/b 都不相关的左值c的情况L(n)可以是n、n.a、n[a]以及这些的组合MatchVarCreateTensorVarCreateScalarVarCreateTokenVarEval: 执行 python 本地的计算GetAttrGetItem表达式
Parse(S, NamedExpr(expr target, expr value) )Parse(S, Constant(constant value, string? kind) )Parse(S, BinOp(expr left, operator op, expr right) )Parse(S, UnaryOp(unaryop op, expr operand) )Parse(S, Dict(expr?* keys, expr* values) )Parse(S, Set(expr* elts) )Parse(S, List(expr* elts, Load()) )Parse(S, Tuple(expr* elts, Load()) )Parse(S, Call(expr func, expr* args, keyword* keywords) )attribute/subscript 的右值使用
Parse(S, Attribute(expr value, identifier attr, Load()) )Parse(S, Subscript(expr value, Slice(expr? lower, expr? upper, expr? step)* slice, Load()) )语句
Parse(S, stmt* body)Parse(S, If(expr test, stmt* body, stmt* orelse) )Parse(S, For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment) )Parse(S, Pass )Parse(S, Break )Parse(S, Continue )Parse(S, Return(expr? value) )IR 适配
数据结构修改
Var的类型,包含一个std::string name,将 LogicalTensor 继承子VarScalarVar的类型,继承自Var,且包含一个 DType 的字段Statement的类型,作为语句的基类IfStatement类型,字段参考 2.0 的字段ForStatement类型,字段参考 2.0 的字段YieldStatement类型,字段参考 2.0 的字段BlockStatement类型,字段参考 2.0 的字段OpStatement类型,字段参考 2.0 的字段,且其中包含的数据类型为std::vector<OperationPtr>OperationPtr是std::shared_ptr<Operation>Operation中的 ioperands 和 ooperands 类型改为VarPtr,在使用的时候转为LogicalTensorPtrVarPtr是std::shared_ptr<Var>LogicalTensorPtr是std::shared_ptr<LogicalTensor>Function中新增 body 的字段,类型是BlockStatementPtr用来在 IR 生成时,提供 scf 下的 IR接口变更
新增 IRBuilder 的C++类型,提供以下C++接口
CreateScalarVarCreateTensorVarCreateIfStmtCreateForStmtCreateYieldStmtCreateBlockStmtCreateOpStmtCreateOperationCreateFunction新增 irbuilder 的 python 类型,用于创建IR,提供以下 python 接口
create_scalar_varcreate_tensor_varcreate_if_stmtcreate_for_stmtcreate_yield_stmtcreate_block_stmtcreate_op_stmtcreate_operationcreate_function流程变更