提交 eba58007 authored 作者: Mohammad Pezeshki's avatar Mohammad Pezeshki

only ADDED ones are now checked.

上级 9552e36c
...@@ -1699,7 +1699,7 @@ def local_useless_alloc(node): ...@@ -1699,7 +1699,7 @@ def local_useless_alloc(node):
# Check if alloc adds a broadcastable dimension with shape 1. # Check if alloc adds a broadcastable dimension with shape 1.
output_shape = node.inputs[1:] output_shape = node.inputs[1:]
num_dims_with_size_1_added_to_left = 0 num_dims_with_size_1_added_to_left = 0
for i in range(len(output_shape)): for i in range(len(output_shape) - input.ndim):
if extract_constant(output_shape[i], only_process_constants=True) == 1: if extract_constant(output_shape[i], only_process_constants=True) == 1:
num_dims_with_size_1_added_to_left += 1 num_dims_with_size_1_added_to_left += 1
else: else:
...@@ -1708,7 +1708,7 @@ def local_useless_alloc(node): ...@@ -1708,7 +1708,7 @@ def local_useless_alloc(node):
if num_dims_with_size_1_added_to_left > 0 and len(new_output_shape) >= input.ndim: if num_dims_with_size_1_added_to_left > 0 and len(new_output_shape) >= input.ndim:
inner = op(*([input] + new_output_shape)) inner = op(*([input] + new_output_shape))
dimshuffle_new_order = (['x'] * num_dims_with_size_1_added_to_left + dimshuffle_new_order = (['x'] * num_dims_with_size_1_added_to_left +
range(len(new_output_shape))) list(xrange(len(new_output_shape))))
return [DimShuffle(inner.type.broadcastable, dimshuffle_new_order)(inner)] return [DimShuffle(inner.type.broadcastable, dimshuffle_new_order)(inner)]
......
...@@ -3552,31 +3552,55 @@ class Test_local_useless_alloc(unittest.TestCase): ...@@ -3552,31 +3552,55 @@ class Test_local_useless_alloc(unittest.TestCase):
alloc_lift = out2in(local_useless_alloc) alloc_lift = out2in(local_useless_alloc)
x = shared(self.rng.randn(2,)) x = shared(self.rng.randn(2,))
y = shared(self.rng.randn()) y = shared(self.rng.randn())
z = shared(self.rng.randn(1, 1))
w = shared(self.rng.randn(1, 1))
alloc_x = tensor.alloc(x, 1, 3, 2) alloc_x = tensor.alloc(x, 1, 3, 2)
alloc_y = tensor.alloc(y, 1, 1) alloc_y = tensor.alloc(y, 1, 1)
alloc_z = tensor.alloc(z, 1, 1, 2)
alloc_w = tensor.alloc(w, 1, 2)
g = FunctionGraph([x, y], [alloc_x, alloc_y]) g = FunctionGraph([x, y, z, w], [alloc_x, alloc_y, alloc_z, alloc_w])
self.assertTrue(str(g) == ("[Alloc(<TensorType(float64, vector)>, " self.assertTrue(str(g) == ("[Alloc(<TensorType(float64, vector)>, "
"TensorConstant{1}, " "TensorConstant{1}, "
"TensorConstant{3}, " "TensorConstant{3}, "
"TensorConstant{2}), " "TensorConstant{2}), "
"Alloc(<TensorType(float64, scalar)>, " "Alloc(<TensorType(float64, scalar)>, "
"TensorConstant{1}, " "TensorConstant{1}, "
"TensorConstant{1})]")) "TensorConstant{1}), "
"Alloc(<TensorType(float64, matrix)>, "
"TensorConstant{1}, "
"TensorConstant{1}, "
"TensorConstant{2}), "
"Alloc(<TensorType(float64, matrix)>, "
"TensorConstant{1}, "
"TensorConstant{2})]"))
alloc_lift.optimize(g) alloc_lift.optimize(g)
self.assertTrue(str(g) == "[DimShuffle{x,0,1}" self.assertTrue(str(g) == "[DimShuffle{x,0,1}"
"(Alloc(<TensorType(float64, vector)>, " "(Alloc(<TensorType(float64, vector)>, "
"TensorConstant{3}, " "TensorConstant{3}, "
"TensorConstant{2})), " "TensorConstant{2})), "
"DimShuffle{x,x}" "DimShuffle{x,x}"
"(<TensorType(float64, scalar)>)]") "(<TensorType(float64, scalar)>), "
"DimShuffle{x,0,1}"
"(Alloc(<TensorType(float64, matrix)>, "
"TensorConstant{1}, "
"TensorConstant{2})), "
"Alloc(<TensorType(float64, matrix)>, "
"TensorConstant{1}, "
"TensorConstant{2})]")
# Check stacktrace was copied over correctly after opt was applied # Check stacktrace was copied over correctly after opt was applied
# self._verify_stack_trace(g)
self.assertTrue(hasattr(g.outputs[0].tag, 'trace')) self.assertTrue(hasattr(g.outputs[0].tag, 'trace'))
class Test_local_useless_inc_subtensor_alloc(unittest.TestCase): class Test_local_useless_inc_subtensor_alloc(unittest.TestCase):
opt_name = 'local_useless_inc_subtensor_alloc' opt_name = 'local_useless_inc_subtensor_alloc'
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论