internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: Theano/Theano

Issue:
Stop cropping output of profiling
At least, make that an option.



Discussion:
Currently, we have theano flags profiling.n_ops and profiling.n_apply which control this, the first for Ops and the second for Apply nodes. They both have a default value of 20. How about simply adding a message to the profiling output to inform the user of these flags (and possibly also increasing the default values for those flags)?

This isn't what this ticket is about.

This ticket is about each line length being cut to 140 caractere. I would
raise this to 512 by default and make a new flags.

Can you do it?

On Wed, Sep 10, 2014 at 2:54 PM, carriepl notifications@github.com wrote:

> Currently, we have theano flags profiling.n_ops and profiling.n_apply
> which control this, the first for Ops and the second for Apply nodes. They
> both have a default value of 20. How about simply adding a message to the
> profiling output to inform the user of these flags (and possibly also
> increasing the default values for those flags)?
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/Theano/Theano/issues/2080#issuecomment-55164563.

Sure, I'll do it.


Resolving patch:
diff --git a/theano/compile/profiling.py b/theano/compile/profiling.py
--- a/theano/compile/profiling.py
+++ b/theano/compile/profiling.py
@@ -50,6 +50,11 @@
              IntParam(20, lambda i: i > 0),
              in_c_key=False)
 
+AddConfigVar('profiling.output_line_width',
+             "Max line width for the profiling output",
+             IntParam(512, lambda i: i > 0),
+             in_c_key=False)
+
 AddConfigVar('profiling.min_memory_size',
              """For the memory profile, do not print Apply nodes if the size
              of their outputs (in bytes) is lower than this threshold""",
@@ -175,7 +180,7 @@ class ProfileStats(object):
     linker_time = 0.0
     # time spent linking graph (FunctionMaker.create)
 
-    line_width = 140
+    line_width = config.profiling.output_line_width
 
     optimizer_profile = None
     # None or tuple (the optimizer, the profile it returned)
@@ -360,7 +365,7 @@ def summary_class(self, file=sys.stderr, N=None):
         es += [' %4d  ']
 
         upto_length = numpy.sum([len(x) for x in hs]) + len(hs)
-        maxlen = self.line_width - upto_length
+        maxlen = max(self.line_width - upto_length, 0)
         hs += ['<Class name>']
         es += ['%s']
         header_str = ' '.join(hs)
@@ -443,7 +448,7 @@ def summary_ops(self, file=sys.stderr, N=None):
         es += ['  %4d  ']
 
         upto_length = numpy.sum([len(x) for x in hs]) + len(hs)
-        maxlen = self.line_width - upto_length
+        maxlen = max(self.line_width - upto_length, 0)
         hs += ['<Op name>']
         es += ['%s']
         header_str = ' '.join(hs)
@@ -510,7 +515,7 @@ def summary_nodes(self, file=sys.stderr, N=None):
             hs += ['<Mflops>', '<Gflops/s>']
 
         upto_length = numpy.sum([len(x) for x in hs]) + len(hs)
-        maxlen = self.line_width - upto_length
+        maxlen = max(self.line_width - upto_length, 0)
         hs += ['<Apply name>']
         es += ['%s']
 


Test patch:
Repository: Theano/Theano

Issue:
Remove equality checks between arrays and None
There is a warning in NumPy 1.9 that the result of such comparisons will be an array instead of a Boolean in the future:

```
/Library/Python/2.7/site-packages/theano/tensor/opt.py:2165: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
```



Discussion:

Resolving patch:
diff --git a/theano/compile/function_module.py b/theano/compile/function_module.py
--- a/theano/compile/function_module.py
+++ b/theano/compile/function_module.py
@@ -1069,7 +1069,7 @@ def removeAllFgraph(remove):
                             if hasattr(remove, 'fgraph'):
                                 del remove.fgraph
                             if hasattr(remove, 'owner'):
-                                if remove.owner == None:
+                                if remove.owner is None:
                                     pass
                                 else:
                                     if hasattr(remove.owner, 'fgraph'):
diff --git a/theano/sparse/basic.py b/theano/sparse/basic.py
--- a/theano/sparse/basic.py
+++ b/theano/sparse/basic.py
@@ -1627,7 +1627,7 @@ def make_node(self, x):
         return gof.Apply(self, [x], [z])
 
     def perform(self, node, (x,), (z,)):
-        if self.axis == None:
+        if self.axis is None:
             z[0] = numpy.asarray(x.sum())
         else:
             z[0] = numpy.asarray(x.sum(self.axis)).ravel()
diff --git a/theano/tensor/nlinalg.py b/theano/tensor/nlinalg.py
--- a/theano/tensor/nlinalg.py
+++ b/theano/tensor/nlinalg.py
@@ -670,7 +670,7 @@ def norm(x,ord):
     if ndim == 0:
         raise ValueError("'axis' entry is out of bounds.")
     elif ndim == 1:
-        if ord == None:
+        if ord is None:
             return tensor.sum(x**2)**0.5
         elif ord == 'inf':
             return tensor.max(abs(x))
@@ -685,7 +685,7 @@ def norm(x,ord):
                 raise ValueError("Invalid norm order for vectors.")
             return z
     elif ndim == 2:
-        if ord == None or ord == 'fro':
+        if ord is None or ord == 'fro':
             return tensor.sum(abs(x**2))**(0.5)
         elif ord == 'inf':
             return tensor.max(tensor.sum(abs(x), 1))


Test patch:
Repository: Theano/Theano

Issue:
Random error in Test with python 3
https://s3.amazonaws.com/archive.travis-ci.org/jobs/46914094/log.txt

This happen on travis. See the real log.

I restart the failed jobs to have a clean travis history.



Discussion:
I have no idea what error do you mean.  This log seems clean.

I restarted the corresponding travis part and this override the original output. I taught they where keeping all of them...

When there is a new one, I'll copy the error here.

Is it the "Apply() < Apply()" one?  Because I've seen it a couple of times.

This would be an example: https://s3.amazonaws.com/archive.travis-ci.org/jobs/47025241/log.txt, if it is indeed the Apply() problem.


Resolving patch:
diff --git a/theano/compile/profiling.py b/theano/compile/profiling.py
--- a/theano/compile/profiling.py
+++ b/theano/compile/profiling.py
@@ -573,8 +573,7 @@ def summary_nodes(self, file=sys.stderr, N=None):
                 self.apply_callcount[a]))
         del topos
 
-        atimes.sort()
-        atimes.reverse()
+        atimes.sort(reverse=True, key=lambda t: (t[1], t[3]))
         tot = 0
         for (f, t, a, nd_id, nb_call) in atimes[:N]:
             tot += t


Test patch:
Repository: Theano/Theano

Issue:
Fix gradient of advincsubtensor1 if increment is broadcasted
The fix should be similar to the one in #2363, but for advincsubtensor1 instead.



Discussion:

Resolving patch:
diff --git a/theano/tensor/subtensor.py b/theano/tensor/subtensor.py
--- a/theano/tensor/subtensor.py
+++ b/theano/tensor/subtensor.py
@@ -1971,6 +1971,7 @@ def grad(self, inputs, grads):
             else:
                 gx = g_output
             gy = advanced_subtensor1(g_output, idx_list)
+            gy = _sum_grad_over_bcasted_dims(y, gy)
 
         return [gx, gy] + [DisconnectedType()()]
 


Test patch:
diff --git a/theano/tensor/tests/test_subtensor.py b/theano/tensor/tests/test_subtensor.py
--- a/theano/tensor/tests/test_subtensor.py
+++ b/theano/tensor/tests/test_subtensor.py
@@ -1347,6 +1347,28 @@ def test_inc_adv_subtensor_with_broadcasting(self):
                   [.5, .3 + 2.1, .15]]), aval
         assert numpy.allclose(gval, 3.0), gval
 
+    def test_inc_adv_subtensor1_with_broadcasting(self):
+        if inplace_increment is None:
+            raise inplace_increment_missing
+
+        inc = dscalar()
+        a = inc_subtensor(self.m[self.ix1], inc)
+        g_inc = tensor.grad(a.sum(), inc)
+
+        assert a.type == self.m.type, (a.type, self.m.type)
+        f = theano.function([self.m, self.ix1, inc], [a, g_inc],
+                            allow_input_downcast=True)
+        aval, gval = f([[.4, .9, .1],
+                        [5, 6, 7],
+                        [.5, .3, .15]],
+                       [0, 1, 0],
+                       2.1)
+        assert numpy.allclose(aval,
+                [[.4 + 2.1 * 2, .9  + 2.1 * 2, .1 + 2.1 * 2],
+                  [5 + 2.1, 6 + 2.1, 7 + 2.1],
+                  [.5, .3, .15]]), aval
+        assert numpy.allclose(gval, 9.0), gval
+
     def test_inc_adv_subtensor_with_index_broadcasting(self):
         if inplace_increment is None:
             raise inplace_increment_missing
Repository: Theano/Theano

Issue:
Broadcastable pattern of tensordot output.
@bartvm [says](https://github.com/Theano/Theano/issues/2594):

>  If I take the dot product of a 3D matrix, where the first dimension is broadcastable, with a 2D matrix, the resulting matrix is no longer broadcastable in the first dimension.



Discussion:

Resolving patch:
diff --git a/theano/tensor/basic.py b/theano/tensor/basic.py
--- a/theano/tensor/basic.py
+++ b/theano/tensor/basic.py
@@ -4997,6 +4997,7 @@ def tensordot(a, b, axes=2):
                              'of b (b.ndim=%i, axes=%i)' % (b.ndim, axes))
 
         outshape = concatenate([a.shape[:a.ndim - axes], b.shape[axes:]])
+        outbcast = a.broadcastable[:a.ndim - axes] + b.broadcastable[axes:]
         outndim = a.ndim + b.ndim - (2 * axes)
 
         a_shape_0 = b_shape_0 = a_shape_1 = b_shape_1 = 1
@@ -5012,7 +5013,10 @@ def tensordot(a, b, axes=2):
         a_reshaped = a.reshape((a_shape_0, a_shape_1), ndim=2)
         b_reshaped = b.reshape((b_shape_0, b_shape_1), ndim=2)
 
-        return _dot(a_reshaped, b_reshaped).reshape(outshape, outndim)
+        out = _dot(a_reshaped, b_reshaped).reshape(outshape, outndim)
+        # Make sure the broadcastable pattern of the result is correct,
+        # since some shape information can be lost in the reshapes.
+        return patternbroadcast(out, outbcast)
 
     # if 'axes' is a list, transpose a and b such that the summed axes of a
     # are last and the summed axes of b are first.


Test patch:
diff --git a/theano/tensor/tests/test_basic.py b/theano/tensor/tests/test_basic.py
--- a/theano/tensor/tests/test_basic.py
+++ b/theano/tensor/tests/test_basic.py
@@ -5529,6 +5529,29 @@ def test_scalar0(self):
                                        f3(aval, bval)))
         utt.verify_grad(self.TensorDot(axes), [aval, bval])
 
+    def test_broadcastable1(self):
+        x = TensorType(dtype=floatX, broadcastable=(True, False, False))('x')
+        y = tensor3('y')
+        z = tensordot(x, y)
+        assert z.broadcastable == (True, False)
+        f = inplace_func([x, y], z)
+        xv = rand(1, 3, 4)
+        yv = rand(3, 4, 5)
+        zv = f(xv, yv)
+        self.assertTrue(numpy.allclose(numpy.tensordot(xv, yv), zv))
+
+    def test_broadcastable2(self):
+        x = TensorType(dtype=floatX, broadcastable=(True, False, False))('x')
+        y = tensor3('y')
+        axes = [[2, 1], [0, 1]]
+        z = tensordot(x, y, axes=axes)
+        assert z.broadcastable == (True, False)
+        f = inplace_func([x, y], z)
+        xv = rand(1, 3, 4)
+        yv = rand(4, 3, 5)
+        zv = f(xv, yv)
+        self.assertTrue(numpy.allclose(numpy.tensordot(xv, yv, axes=axes), zv))
+
 
 def test_smallest_stack():
     sx, sy = dscalar(), dscalar()
Repository: Theano/Theano

Issue:
DisconnectedGrad should have a connection_pattern() method.



Discussion:

Resolving patch:
diff --git a/theano/gradient.py b/theano/gradient.py
--- a/theano/gradient.py
+++ b/theano/gradient.py
@@ -1945,6 +1945,8 @@ class DisconnectedGrad(ViewOp):
     def grad(self, args, g_outs):
         return [disconnected_type() for g_out in g_outs]
 
+    def connection_pattern(self, node):
+        return [[False]]
 
 disconnected_grad_ = DisconnectedGrad()
 


Test patch:
diff --git a/theano/tests/test_gradient.py b/theano/tests/test_gradient.py
--- a/theano/tests/test_gradient.py
+++ b/theano/tests/test_gradient.py
@@ -719,6 +719,14 @@ def test_grad(self):
 
             assert np.allclose(f(a), f2(a))
 
+    def test_connection_pattern(self):
+        T = theano.tensor
+        x = T.matrix('x')
+        y = gradient.disconnected_grad(x)
+
+        connection_pattern = y.owner.op.connection_pattern(y.owner)
+        assert connection_pattern == [[False]]
+
     def test_disconnected_paths(self):
         # Test that taking gradient going through a disconnected
         # path rasises an exception
Repository: Theano/Theano

Issue:
Identify random states of MRG generators
For the moment, random states of MRG are regular matrices, either `TensorType(int32, matrix)` or `CudaNdarrayType(float32, matrix)`.
It would be useful to have a way of differenciating them from model parameters, for instance.
@bartvm proposed a tag like `.is_RNG`.
Something else that could be useful would be a weak reference to the `MRG_RandomStreams` instance that created them.



Discussion:
Why a weakref, specifically?

The `MRG_RandomStreams` has a dictionary of all `shared state -> update expression` that it created, so I think it may make the work of the garbage collector easier not to have a cycle.
If this is not a concern, a full reference is fine by me as well.

![](https://i.imgur.com/1XR0bEF.png)

Sounds good to me.


Resolving patch:
diff --git a/theano/sandbox/rng_mrg.py b/theano/sandbox/rng_mrg.py
--- a/theano/sandbox/rng_mrg.py
+++ b/theano/sandbox/rng_mrg.py
@@ -6,6 +6,7 @@
 
 """
 import warnings
+import weakref
 
 import numpy
 
@@ -1220,6 +1221,8 @@ def uniform(self, size, low=0.0, high=1.0, ndim=None, dtype=None,
             u = self.pretty_return(node_rstate,
                                    *mrg_uniform.new(node_rstate,
                                                     ndim, dtype, size))
+        # Add a reference to distinguish from other shared variables
+        node_rstate.rng_owner = weakref.ref(self)
         r = u * (high - low) + low
 
         if u.type.broadcastable != r.type.broadcastable:
diff --git a/theano/tensor/shared_randomstreams.py b/theano/tensor/shared_randomstreams.py
--- a/theano/tensor/shared_randomstreams.py
+++ b/theano/tensor/shared_randomstreams.py
@@ -5,6 +5,7 @@
 
 import copy
 import numpy
+import weakref
 
 from theano.compile.sharedvalue import (SharedVariable, shared_constructor,
                                         shared)
@@ -132,6 +133,8 @@ def gen(self, op, *args, **kwargs):
         """
         seed = int(self.gen_seedgen.randint(2 ** 30))
         random_state_variable = shared(numpy.random.RandomState(seed))
+        # Add a reference to distinguish from other shared variables
+        random_state_variable.rng_owner = weakref.ref(self)
         new_r, out = op(random_state_variable, *args, **kwargs)
         out.rng = random_state_variable
         out.update = (random_state_variable, new_r)


Test patch:
Repository: Theano/Theano

Issue:
Make an optimization that remove Split with only 1 split



Discussion:

Resolving patch:
diff --git a/theano/tensor/opt.py b/theano/tensor/opt.py
--- a/theano/tensor/opt.py
+++ b/theano/tensor/opt.py
@@ -3077,6 +3077,25 @@ def local_useless_tile(node):
             return
 
 
+##############
+# Split Opts #
+##############
+@register_canonicalize
+@register_specialize
+@gof.local_optimizer([T.Split])
+def local_useless_split(node):
+    """ Split{n_splits=1}(x, y) -> x
+
+    Remove Split with only 1 split.
+    """
+    if isinstance(node.op, T.Split):
+        if node.op.len_splits == 1:
+            x, axis, splits = node.inputs
+            out = assert_op(x, T.eq(splits.shape[0], 1))
+            out = assert_op(out, T.eq(x.shape[axis], splits[0]))
+            return [out]
+
+
 ################
 # Flatten Opts #
 ################


Test patch:
diff --git a/theano/tensor/tests/test_opt.py b/theano/tensor/tests/test_opt.py
--- a/theano/tensor/tests/test_opt.py
+++ b/theano/tensor/tests/test_opt.py
@@ -5013,6 +5013,26 @@ def test_local_div_to_inv():
     assert numpy.allclose(out_val, 0.5)
 
 
+def test_local_useless_split():
+    x = tensor.matrix('x')
+    splits = tensor.ivector('splits')
+    opt = tensor.split(x, splits, n_splits=1)
+    nonopt = tensor.split(x, splits, n_splits=3)
+
+    mode = compile.get_default_mode().including("local_useless_split")
+    f_opt = theano.function([x, splits], opt, mode=mode)
+    f_nonopt = theano.function([x, splits], nonopt, mode=mode)
+
+    f_opt(numpy.random.rand(4,4).astype(config.floatX), [4])
+    f_nonopt(numpy.random.rand(4,4).astype(config.floatX), [1,2,1])
+    graph_opt = f_opt.maker.fgraph.toposort()
+    graph_nonopt = f_nonopt.maker.fgraph.toposort()
+
+    assert isinstance(graph_opt[-1].op, DeepCopyOp)
+    assert len(graph_nonopt)==1
+    assert isinstance(graph_nonopt[0].op, tensor.Split)
+
+
 def test_local_flatten_lift():
     for i in range(1, 4):
         op = tensor.Flatten(i)
Repository: Theano/Theano

Issue:
Opt generalize reshape(reshape(x, ...), shapes...) -> reshape(x, shapes) to the GPU.



Discussion:

Resolving patch:
diff --git a/theano/sandbox/cuda/basic_ops.py b/theano/sandbox/cuda/basic_ops.py
--- a/theano/sandbox/cuda/basic_ops.py
+++ b/theano/sandbox/cuda/basic_ops.py
@@ -2445,6 +2445,8 @@ class GpuReshape(tensor.Reshape, GpuOp):
 
     # __hash__, __eq__, __str__ come from tensor.Subtensor
     def make_node(self, x, shp):
+        x = as_cuda_ndarray_variable(x)
+        shp = tensor.as_tensor_variable(shp)
         host_reshaped = host_from_gpu(x).reshape(shp, ndim=self.ndim)
         return Apply(self, [x, shp],
                      [CudaNdarrayType(host_reshaped.broadcastable)()])
diff --git a/theano/sandbox/cuda/opt.py b/theano/sandbox/cuda/opt.py
--- a/theano/sandbox/cuda/opt.py
+++ b/theano/sandbox/cuda/opt.py
@@ -944,6 +944,35 @@ def local_gpu_reshape(node):
     return False
 
 
+@local_optimizer([GpuReshape])
+def local_gpu_reshape_chain(node):
+    """
+    GuReshape(GpuReshape(shape1),shape2) -> GpuReshape(shape2)
+
+    """
+    if not tensor.opt.opt.check_chain(node, GpuReshape, GpuReshape):
+        return False
+
+    # TODO: this can permit a failing program to run by eliminating
+    #       the lower reshape
+    rval = node.op(node.inputs[0].owner.inputs[0], node.inputs[1])
+    # It might happen that the desired output of this node has a broadcastable
+    # pattern that does not match that of 'rval'. This is when originally, we
+    # were able to figure out that one of the dimensions of the reshape is one,
+    # but some other transformation replaced the shape by one for which this
+    # cannot be guessed.
+    # We should try to figure out why we lost the information about this
+    # constant value... but in the meantime, better not apply this
+    # optimization.
+    if rval.broadcastable == node.outputs[0].broadcastable:
+        return [rval]
+    else:
+        return False
+gpu_cut_copies.register('cut_local_gpu_reshape_chain',
+                        local_gpu_reshape_chain,
+                        'fast_run', 'gpu')
+
+
 @register_opt()
 @local_optimizer([gpu_from_host, tensor.Flatten])
 def local_gpu_flatten(node):


Test patch:
diff --git a/theano/sandbox/cuda/tests/test_opt.py b/theano/sandbox/cuda/tests/test_opt.py
--- a/theano/sandbox/cuda/tests/test_opt.py
+++ b/theano/sandbox/cuda/tests/test_opt.py
@@ -819,6 +819,16 @@ def __init__(self, name):
               self).__init__(name)
 
 
+def test_local_gpu_reshape():
+    mode = mode_with_gpu
+    a = tensor.fmatrix()
+    b = basic_ops.GpuReshape(3)(a, [2, 3, 4])
+    c = basic_ops.GpuReshape(1)(b, [24])
+    f = theano.function([a], c, mode=mode)
+    topo = f.maker.fgraph.toposort()
+    assert sum(isinstance(node.op, basic_ops.GpuReshape) for node in topo) == 1
+
+
 if __name__ == '__main__':
     test_gpualloc()
     test_opt_gpujoin_onlyajoin()
Repository: Theano/Theano

Issue:
Add a 'name' argument to '.copy' method of Theano variables.
See the discussion in https://github.com/mila-udem/blocks/issues/784. 



Discussion:
@yingzha This is your ticket for this week. Basically the `copy` method on Theano variables should have a `name` argument that sets the `.name` of the newly created variable. It will involve modifying [this method](https://github.com/Theano/Theano/blob/master/theano/tensor/var.py#L523-L524) and probably writing a small function to replace `tensor_copy` [here](https://github.com/Theano/Theano/blob/master/theano/tensor/basic.py#L2809-L2810), plus tests.

@dwf , I added .name to `copy` like this 
`def copy(self, name):
         """Copy a variable and set a name to the copy."""
         copied_variable = theano.tensor.basic.tensor_copy(self)
         copied_variable.name = name
         return copied_variable
`
Do we still need to modify `tensor_copy`?

The name argument should definitely be optional but otherwise that sounds fine.


Resolving patch:
diff --git a/theano/tensor/var.py b/theano/tensor/var.py
--- a/theano/tensor/var.py
+++ b/theano/tensor/var.py
@@ -520,8 +520,11 @@ def take(self, indices, axis=None, mode='raise'):
         return theano.tensor.subtensor.take(self, indices, axis, mode)
 
     # COPYING
-    def copy(self):
-        return theano.tensor.basic.tensor_copy(self)
+    def copy(self, name=None):
+        """Copy a variable and optionally assign a name."""
+        copied_variable = theano.tensor.basic.tensor_copy(self)
+        copied_variable.name = name
+        return copied_variable
 
     def __iter__(self):
         try:


Test patch:
diff --git a/theano/tensor/tests/test_var.py b/theano/tensor/tests/test_var.py
--- a/theano/tensor/tests/test_var.py
+++ b/theano/tensor/tests/test_var.py
@@ -1,4 +1,5 @@
 import numpy as np
+from numpy.testing import assert_equal, assert_string_equal
 
 import theano
 import theano.tensor as tt
@@ -19,3 +20,12 @@ def test_numpy_method():
         f = theano.function([x], y)
         utt.assert_allclose(np.nan_to_num(f(data)),
                             np.nan_to_num(fct(data)))
+
+
+def test_copy():
+    x = tt.dmatrix('x')
+    data = np.random.rand(5, 5)
+    y = x.copy(name='y')
+    f = theano.function([x], y)
+    assert_equal(f(data), data)
+    assert_string_equal(y.name, 'y')