@register_uncanonicalize
@gof.local_optimizer([DimShuffle])
def local_dimshuffle_alloc(node):
 if (isinstance(node.op, DimShuffle) and node.inputs[0].owner):
  input_ = node.inputs[0]
  if isinstance(input_.owner.op, T.Alloc):
   new_order = node.op.new_order
   expected_new_order = ((('x',) * (len(new_order) - input_.ndim)) + tuple(range(input_.ndim)))
   if (new_order != expected_new_order):
    return False
   nb_new_dims = (len(new_order) - input_.ndim)
   new_shape_input = (((1,) * nb_new_dims) + tuple(input_.owner.inputs[1:]))
   return [T.alloc(input_.owner.inputs[0], *new_shape_input)]
 return False

@ register_uncanonicalize @ gof . local_optimizer ( [ DimShuffle ] ) def local_dimshuffle_alloc ( node ) : if ( isinstance ( node . op , DimShuffle ) and node . inputs [ 0 ] . owner ) : input_ = node . inputs [ 0 ] if isinstance ( input_ . owner . op , T . Alloc ) : new_order = node . op . new_order expected_new_order = ( ( ( 'x' , ) * ( len ( new_order ) - input_ . ndim ) ) + tuple ( range ( input_ . ndim ) ) ) if ( new_order != expected_new_order ) : return False nb_new_dims = ( len ( new_order ) - input_ . ndim ) new_shape_input = ( ( ( 1 , ) * nb_new_dims ) + tuple ( input_ . owner . inputs [ 1 : ] ) ) return [ T . alloc ( input_ . owner . inputs [ 0 ] , * new_shape_input ) ] return False
if an alloc is inside a dimshuffle which only adds dimension to the left .