提交 1cf4a13c authored 作者: Frederic's avatar Frederic

local_mul_to_neg now only apply when it won't add node to the graph

上级 cc47a946
...@@ -3387,12 +3387,21 @@ def local_sum_alloc(node): ...@@ -3387,12 +3387,21 @@ def local_sum_alloc(node):
@gof.local_optimizer([T.mul]) @gof.local_optimizer([T.mul])
def local_mul_to_neg(node): def local_mul_to_neg(node):
"""
mul(-1, x) -> neg(x)
This is not done if we would add more nodes in the graph, like with:
mul(-1, x, y) -/-> neg(mul(x, y))
"""
if (node.op == T.mul and if (node.op == T.mul and
local_mul_canonizer.get_constant(node.inputs[0]) == -1.0): local_mul_canonizer.get_constant(node.inputs[0]) == -1.0):
num = node.inputs[1:] num = node.inputs[1:]
if len(num) == 1: if len(num) == 1:
other_prod = num[0] other_prod = num[0]
else: else:
# This would add extra node in the graph
return False
other_prod = local_mul_canonizer.main(*num) other_prod = local_mul_canonizer.main(*num)
if other_prod.type == node.outputs[0].type: if other_prod.type == node.outputs[0].type:
return [-other_prod] return [-other_prod]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论