-
Notifications
You must be signed in to change notification settings - Fork 4
计算图优化:自动inplace化 #32
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
Comments
参考 ———————————————————————————————————————————————————————— 以下是实现自动inplace化的完整代码,基于活跃变量分析复用输入变量(args)作为输出变量(returns),避免中间变量: 代码实现 python def compute_live_after(ir_list):
def inplace_optimize(ir_list):
代码说明
示例验证 输入:计算 (a + b) * (c + d) 的原始IR python original_ir = [ 输出:inplace化后的IR python optimized_ir = inplace_optimize(original_ir) 输出结果 python [ 验证说明
适用场景
该代码通过活跃变量分析和变量复用策略,在不引入显式inplace操作的前提下,高效减少中间变量,符合编译原理中数据流优化的核心思想。 |
如下,这是sigmoid的ir序列,可以忽略调print的部分
需求是,实现一个ir序列的编译器,能够减少中间变量,实现自动inplace化
等待大家挑战这个编译需求
~load(var /home/lipeng/model/deepxmodel/functional/sigmoided)->()
~print(tensor sigmoided, var %.4f)->()
shape:[3, 4, 5]
[0]=[
[-3.0000 -2.9000 -2.8000 -2.7000 -2.6000],
[-2.5000 -2.4000 -2.3000 -2.2000 -2.1000],
[-2.0000 -1.9000 -1.8000 -1.7000 -1.6000],
[-1.5000 -1.4000 -1.3000 -1.2000 -1.1000]
]
[1]=[
[-1.0000 -0.9000 -0.8000 -0.7000 -0.6000],
[-0.5000 -0.4000 -0.3000 -0.2000 -0.1000],
[0.0000 0.1000 0.2000 0.3000 0.4000],
[0.5000 0.6000 0.7000 0.8000 0.9000]
]
[2]=[
[1.0000 1.1000 1.2000 1.3000 1.4000],
[1.5000 1.6000 1.7000 1.8000 1.9000],
[2.0000 2.1000 2.2000 2.3000 2.4000],
[2.5000 2.6000 2.7000 2.8000 2.9000]
]
//从这里开始,是
~newtensor(vector [3 4 5])->(tensor 0)
~mulscalar(tensor sigmoided, var -1)->(tensor 0)
~newtensor(vector [3 4 5])->(tensor 1)
~exp(tensor 0)->(tensor 1)
~newtensor(vector [3 4 5])->(tensor 2)
~addscalar(tensor 1, var 1)->(tensor 2)
~newtensor(vector [3 4 5])->(tensor 3)
~rdivscalar(var 1, tensor 2)->(tensor 3)
//到这里结束
~print(tensor 3, var %.4f)->()
shape:[3, 4, 5]
[0]=[
[0.0474 0.0522 0.0573 0.0630 0.0691],
[0.0759 0.0832 0.0911 0.0998 0.1091],
[0.1192 0.1301 0.1419 0.1545 0.1680],
[0.1824 0.1978 0.2142 0.2315 0.2497]
]
[1]=[
[0.2689 0.2891 0.3100 0.3318 0.3543],
[0.3775 0.4013 0.4256 0.4502 0.4750],
[0.5000 0.5250 0.5498 0.5744 0.5987],
[0.6225 0.6457 0.6682 0.6900 0.7109]
]
[2]=[
[0.7311 0.7503 0.7685 0.7858 0.8022],
[0.8176 0.8320 0.8455 0.8581 0.8699],
[0.8808 0.8909 0.9002 0.9089 0.9168],
[0.9241 0.9309 0.9370 0.9427 0.9478]
]
The text was updated successfully, but these errors were encountered: