@register_specialize
@gof.local_optimizer([T.Sum, T.elemwise.Prod])
def local_opt_alloc(node):
    """
    sum(alloc(constant,shapes...)) => constant*prod(shapes)
    or
    prod(alloc(constant,shapes...)) => constant**prod(shapes)

    """
    if isinstance(node.op, T.Sum) or isinstance(node.op, T.elemwise.Prod):
        node_inps, = node.inputs
        if node_inps.owner and isinstance(node_inps.owner.op, T.Alloc):
            input = node_inps.owner.inputs[0]
            shapes = node_inps.owner.inputs[1:]
            if (node.op.axis is None or
                    node.op.axis == tuple(range(input.ndim))):
                try:
                    val = get_scalar_constant_value(input)
                    assert val.size == 1
                    # check which type of op
                    if isinstance(node.op, T.Sum):
                        val = val.reshape(1)[0] * T.mul(*shapes)
                    else:
                        val = val.reshape(1)[0] ** T.mul(*shapes)
                    return [T.cast(val, dtype=node.outputs[0].dtype)]

                except NotScalarConstantError:
                    pass
            else:
                try:
                    val = get_scalar_constant_value(input)
                    assert val.size == 1
                    val = val.reshape(1)[0]
                    to_prod = [shapes[i] for i in xrange(len(shapes))
                               if node.op.axis in i]
                    if to_prod:
                        if isinstance(node.op, T.Sum):
                            val *= T.mul(*to_prod)
                        else:
                            val = val ** T.mul(*to_prod)
                    return [T.alloc(T.cast(val, dtype=node.outputs[0].dtype),
                                    *[shapes[i] for i in xrange(len(shapes))
                                      if i not in node.op.axis])]
                except NotScalarConstantError:
                    pass
dataset/ETHPy150Open rizar/attention-lvcsr/libs/Theano/theano/tensor/opt.py/local_opt_alloc/swapped operands of `in`