提交 03f55128 authored 作者: Olivier Breuleux's avatar Olivier Breuleux

added opt.InplaceOptimizer

上级 8e856888
from gof import opt
from elemwise2 import Broadcast
class InplaceOptimizer(opt.OpSpecificOptimizer):
opclass = Broadcast
def apply_on_op(self, env, op):
baseline = op.inplace_pattern
candidate_outputs = [i for i in xrange(len(op.outputs)) if i not in baseline]
candidate_inputs = [i for i in xrange(len(op.inputs)) if i not in baseline.values()]
for candidate_output in candidate_outputs:
for candidate_input in candidate_inputs:
inplace_pattern = dict(baseline, **{candidate_output: candidate_input})
try:
new_op = Broadcast(op.scalar_opclass, op.inputs, inplace_pattern)
env.replace_all(dict(zip(op.outputs, new_op.outputs)))
except:
continue
candidate_inputs.remove(candidate_input)
op = new_op
break
inplace_optimizer = InplaceOptimizer()
"""
This variable is used in compile.prog as the optimizer for all programs built
......
......@@ -60,12 +60,6 @@ class Second(BinaryScalarOp):
def grad(self, (x, y), (gz, )):
return None, gz
# class SquareDiff(BinaryScalarOp):
# def impl(self, x, y):
# diff = (x - y)
# return diff * diff
# def c_code(self, (x, y), (z, ), sub):
# return "%(z)s = %(x)s - %(y)s; %(z)s *= %(z)s;" % locals()
class Identity(UnaryScalarOp):
def impl(self, x):
......
......@@ -327,7 +327,6 @@ Div, div, DivInplace, div_inplace = broadcast(scal.Div, 'Div')
Pow, pow, PowInplace, pow_inplace = broadcast(scal.Pow, 'Pow')
#########################
# Linalg : Dot
#########################
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论