Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CINN] Python DSL of CINN IR #56393

Closed
wants to merge 1 commit into from
Closed

Conversation

6clc
Copy link
Contributor

@6clc 6clc commented Aug 17, 2023

PR types

Others

PR changes

Others

Description

Pcard-72423

支持通过Python DSL定制CINN Lower Level IR的主PR。

单测目录: test/cinn/ir/
e2e测试目录: test/cinn/runtime

以element wise add为例

import cinn
import numpy as np
from cinn import to_cinn_llir
from cinn.runtime.data_array import DataArray

# python dsl构造elementwise add
@to_cinn_llir
def add_kernel(X, Y, Z, N):
    for idx in range(N):
        Z[idx] = X[idx] + Y[idx]


def test_launch():
    N = 32
    X_np = np.random.random(N).astype(np.float32)
    Y_np = np.random.random(N).astype(np.float32)
    Z_np = np.zeros((N), dtype=np.float32)
    target = cinn.common.DefaultNVGPUTarget()
    
    # 构造数据
    X = DataArray.from_numpy(X_np, target)
    Y = DataArray.from_numpy(Y_np, target)
    Z = DataArray.from_numpy(Z_np, target)

    # compile and run
    add_kernel[target](X, Y, Z, N)
    
    pred = Z.to_numpy()
    gt = np.add(X_np, Y_np)
    np.testing.assert_allclose(pred, gt)

@paddle-bot
Copy link

paddle-bot bot commented Aug 17, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot
Copy link

paddle-bot bot commented Aug 17, 2023

✅ This PR's description meets the template requirements!
Please wait for other CI results.

@6clc 6clc force-pushed the cinn-ir-ast branch 2 times, most recently from 483ef5f to daaa0d4 Compare August 24, 2023 01:44
@@ -268,6 +268,27 @@ Expr For::Make(Var loop_var,

return Expr(node);
}
Expr For::Make(Var loop_var, Expr min, Expr extent, Expr body) {
auto node = make_shared<For>();
CHECK(loop_var.defined());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add message for CHECK failure. Now we would like every error message can be user-friendly.

Reference: https://github.com/PaddlePaddle/Paddle/wiki/Paddle-Error-Message-Writing-Specification

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

BindInfo bind_info = BindInfo();
node->set_bind_info(BindInfo());

if (node->is_vectorized()) CHECK(node->vectorize_info().valid());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 286\287, put any controlled statements inside blocks (i.e. use curly braces).

Reference: https://google.github.io/styleguide/cppguide.html#Formatting_Looping_Branching

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

//! kInput: arg is input
//! kOutput: arg is output
//! kUnknown: arg maybe input or output
enum class IO { kInput = 0, kOutput = 1, kUnknown = 2 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is IO a good name? Because IO is also related to print\file\hardware\network, can we name it as InputOutputType? ArgInOutType or something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this name is not very good, but so many modules of the current CINN have relied on this IO. It is too troublesome to modify. This is an enum property in ir::Argument, the ambiguity doesn't seem too serious.

const std::vector<Expr> &shape,
const std::vector<Expr> &domain,
const std::vector<Var> &reduce_axis) {
CHECK(!name.empty()) << "Tensor name is set empty";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Cannot set empty Tensor name in Tensor::Make"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

T = TypeVar('T')


class CINNLowerLevelIRJIT(Generic[T]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CinnLowerLevelIrJit is a better name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done



class DataArray:
""" """
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is here empty comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

), f"{node.id} is not defined in context"
return self.local_variables[node.id]

def visit_BinOp(self, node):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a temporary function?

I asked because it only handled Add here, and BinOp can potentially not only Load. So I guess it may not be correct?

Copy link
Contributor Author

@6clc 6clc Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code about visit_XXX will be re-written by new commit

llir_var_expr = ir.Expr(llir_var)
self.set_value(node.target.id, llir_var_expr)

llir_for_min = ir.Expr(ast_min)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess LLIR For doesn't support step? Should we report error to users if user has step in for range?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

step has already been checked in the previous code, almost at line 109.

        assert (
            len(iter_args) <= 2
        ), "CINN Low Level IR does not support setting the range step"

@paddle-ci-bot
Copy link

paddle-ci-bot bot commented Sep 5, 2023

Sorry to inform you that da6b637's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@CLAassistant
Copy link

CLAassistant commented Sep 19, 2023

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ 6clc
❌ liuchao


liuchao seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@6clc 6clc force-pushed the cinn-ir-ast branch 2 times, most recently from 8950eaf to f293535 Compare September 20, 2023 01:23
zhhsplendid pushed a commit that referenced this pull request Sep 22, 2023
拆分新特性:CINN Python DSL
单测和e2e测试在主开发PR上,详细见主开发PR描述 #56393

修改了IRCompare,支持对比IR的结构是否相同
封装了一些需要上下文信息才能构建的IR。 封装为IRContext类,开放出C++接口给Python层调用。包括下面的IR
zhhsplendid pushed a commit that referenced this pull request Sep 26, 2023
此PR封装了Python DSL需要的C++和Python层的接口

单测和e2e测试见主PR: #56393
@paddle-ci-bot
Copy link

paddle-ci-bot bot commented Sep 30, 2023

Sorry to inform you that 0f87e8c's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

zhhsplendid pushed a commit that referenced this pull request Oct 9, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:#56393

此PR只负责 解析python dsl中的compute定义

1. 装饰器@to_cinn_ir封装cinn的function kernel:

CinnLowerLevelIrJit支持从Jit运行时中数据类型、target类型、python ast。后续解析compute的信息都会从CinnLowerLevelIrJit这个类中获取。

CinnLowerLevelIrJit也支持静态获取上述信息,通过python的annotation来填充。

2. compute 语义解析
将整个AST分为三种类型:

stmts: Function, For, If, With ,对应封装上下文IR的PR: #57515

Assign: 表达式"lhs = rhs"的类型,Assign类型构成了stmts。
python/cinn/compiler/expr_executor.py中的exec_expr方法将rhs解析成cinn ir Expr
python/cinn/compiler/expr_executor.py中的exec_assign方法,将lhs=rhs表达的assign语义存储在局部变量表中。

Expr:组成Assign中的rhs。

3. 变量管理

python/cinn/compiler/utils.py中的class VariableTable:用于管理Python DSL中定义的变量,主要是下面两个功能。

每次Enter新的Context,会复制当前的变量表
每次Exit Context,会删除当前Context增加的变量,恢复上一轮Context的变量表。
zhhsplendid pushed a commit that referenced this pull request Oct 11, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:#56393

此PR只负责 解析python dsl中的schedule定义
Frida-a pushed a commit to Frida-a/Paddle that referenced this pull request Oct 14, 2023
拆分新特性:CINN Python DSL
单测和e2e测试在主开发PR上,详细见主开发PR描述 PaddlePaddle#56393

修改了IRCompare,支持对比IR的结构是否相同
封装了一些需要上下文信息才能构建的IR。 封装为IRContext类,开放出C++接口给Python层调用。包括下面的IR
Frida-a pushed a commit to Frida-a/Paddle that referenced this pull request Oct 14, 2023
此PR封装了Python DSL需要的C++和Python层的接口

单测和e2e测试见主PR: PaddlePaddle#56393
Frida-a pushed a commit to Frida-a/Paddle that referenced this pull request Oct 14, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:PaddlePaddle#56393

此PR只负责 解析python dsl中的compute定义

1. 装饰器@to_cinn_ir封装cinn的function kernel:

CinnLowerLevelIrJit支持从Jit运行时中数据类型、target类型、python ast。后续解析compute的信息都会从CinnLowerLevelIrJit这个类中获取。

CinnLowerLevelIrJit也支持静态获取上述信息,通过python的annotation来填充。

2. compute 语义解析
将整个AST分为三种类型:

stmts: Function, For, If, With ,对应封装上下文IR的PR: PaddlePaddle#57515

Assign: 表达式"lhs = rhs"的类型,Assign类型构成了stmts。
python/cinn/compiler/expr_executor.py中的exec_expr方法将rhs解析成cinn ir Expr
python/cinn/compiler/expr_executor.py中的exec_assign方法,将lhs=rhs表达的assign语义存储在局部变量表中。

Expr:组成Assign中的rhs。

3. 变量管理

python/cinn/compiler/utils.py中的class VariableTable:用于管理Python DSL中定义的变量,主要是下面两个功能。

每次Enter新的Context,会复制当前的变量表
每次Exit Context,会删除当前Context增加的变量,恢复上一轮Context的变量表。
Frida-a pushed a commit to Frida-a/Paddle that referenced this pull request Oct 14, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:PaddlePaddle#56393

此PR只负责 解析python dsl中的schedule定义
jiahy0825 pushed a commit to jiahy0825/Paddle that referenced this pull request Oct 16, 2023
拆分新特性:CINN Python DSL
单测和e2e测试在主开发PR上,详细见主开发PR描述 PaddlePaddle#56393

修改了IRCompare,支持对比IR的结构是否相同
封装了一些需要上下文信息才能构建的IR。 封装为IRContext类,开放出C++接口给Python层调用。包括下面的IR
jiahy0825 pushed a commit to jiahy0825/Paddle that referenced this pull request Oct 16, 2023
此PR封装了Python DSL需要的C++和Python层的接口

单测和e2e测试见主PR: PaddlePaddle#56393
jiahy0825 pushed a commit to jiahy0825/Paddle that referenced this pull request Oct 16, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:PaddlePaddle#56393

此PR只负责 解析python dsl中的compute定义

1. 装饰器@to_cinn_ir封装cinn的function kernel:

CinnLowerLevelIrJit支持从Jit运行时中数据类型、target类型、python ast。后续解析compute的信息都会从CinnLowerLevelIrJit这个类中获取。

CinnLowerLevelIrJit也支持静态获取上述信息,通过python的annotation来填充。

2. compute 语义解析
将整个AST分为三种类型:

stmts: Function, For, If, With ,对应封装上下文IR的PR: PaddlePaddle#57515

Assign: 表达式"lhs = rhs"的类型,Assign类型构成了stmts。
python/cinn/compiler/expr_executor.py中的exec_expr方法将rhs解析成cinn ir Expr
python/cinn/compiler/expr_executor.py中的exec_assign方法,将lhs=rhs表达的assign语义存储在局部变量表中。

Expr:组成Assign中的rhs。

3. 变量管理

python/cinn/compiler/utils.py中的class VariableTable:用于管理Python DSL中定义的变量,主要是下面两个功能。

每次Enter新的Context,会复制当前的变量表
每次Exit Context,会删除当前Context增加的变量,恢复上一轮Context的变量表。
jiahy0825 pushed a commit to jiahy0825/Paddle that referenced this pull request Oct 16, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:PaddlePaddle#56393

此PR只负责 解析python dsl中的schedule定义
zhhsplendid pushed a commit that referenced this pull request Oct 16, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:#56393

此PR只负责 给python dsl封装cinn ir的Runtime
@6clc 6clc closed this Oct 17, 2023
jiahy0825 pushed a commit to jiahy0825/Paddle that referenced this pull request Oct 26, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:PaddlePaddle#56393

此PR只负责 给python dsl封装cinn ir的Runtime
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
拆分新特性:CINN Python DSL
单测和e2e测试在主开发PR上,详细见主开发PR描述 PaddlePaddle#56393

修改了IRCompare,支持对比IR的结构是否相同
封装了一些需要上下文信息才能构建的IR。 封装为IRContext类,开放出C++接口给Python层调用。包括下面的IR
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
此PR封装了Python DSL需要的C++和Python层的接口

单测和e2e测试见主PR: PaddlePaddle#56393
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:PaddlePaddle#56393

此PR只负责 解析python dsl中的compute定义

1. 装饰器@to_cinn_ir封装cinn的function kernel:

CinnLowerLevelIrJit支持从Jit运行时中数据类型、target类型、python ast。后续解析compute的信息都会从CinnLowerLevelIrJit这个类中获取。

CinnLowerLevelIrJit也支持静态获取上述信息,通过python的annotation来填充。

2. compute 语义解析
将整个AST分为三种类型:

stmts: Function, For, If, With ,对应封装上下文IR的PR: PaddlePaddle#57515

Assign: 表达式"lhs = rhs"的类型,Assign类型构成了stmts。
python/cinn/compiler/expr_executor.py中的exec_expr方法将rhs解析成cinn ir Expr
python/cinn/compiler/expr_executor.py中的exec_assign方法,将lhs=rhs表达的assign语义存储在局部变量表中。

Expr:组成Assign中的rhs。

3. 变量管理

python/cinn/compiler/utils.py中的class VariableTable:用于管理Python DSL中定义的变量,主要是下面两个功能。

每次Enter新的Context,会复制当前的变量表
每次Exit Context,会删除当前Context增加的变量,恢复上一轮Context的变量表。
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:PaddlePaddle#56393

此PR只负责 解析python dsl中的schedule定义
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
拆分新特性:CINN Python DSL, 主PR和单测见:PaddlePaddle#56393

此PR只负责 给python dsl封装cinn ir的Runtime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants