Repository: Theano/Theano

Issue:
Segment fault when using fast_compile
Using latest Theano, libgpuarray, Keras, try to run keras/examples/cifar10_cnn.py, it will segfault at epoch 3 or 4 when using fast_compile, no problem using fast_run. Issue is repeatable and occurs on old gpu backend and cpu too, I think the Windows version has it as well.

OS: Ubuntu 16.04.1 LTS
GPU: Old Titan X (the issue can be reproduced without using GPU)
CUDA driver: 367.48
.theanorc:
```
[global]
device=cuda
floatX=float32
optimizer=fast_compile
[lib]
cnmem=0.1
[nvcc]
fastmath=True
```
Versions:
```
>>> theano.__version__
'0.9.0beta1.dev-9f1aaacb6e884ebcff9e249f19848db8aa6cb1b2'
>>> keras.__version__
'1.2.2'
>>> numpy.__version__
'1.12.0'
```

Using gdb to backtrace, a usual stacktrace looks like this (although sometimes it's different):
```
#0  0x0000000000000001 in ?? ()
#1  0x00000000004ad59b in PyList_SetItem ()
#2  0x00007f829b2a896f in CLazyLinker_call (_self=0x7f821d885188,
    args=0x7f829fd3e050, kwds=0x0)
    at /root/.theano/compiledir_Linux-3.13--generic-x86_64-with-Ubuntu-16.04-xenial-x86_64-2.7.12-64/lazylinker_ext/mod.cpp:859
#3  0x00000000004b0cb3 in PyObject_Call ()
#4  0x00000000004c9faf in PyEval_EvalFrameEx ()
#5  0x00000000004c2765 in PyEval_EvalCodeEx ()
#6  0x00000000004de6fe in ?? ()
#7  0x00000000004b0cb3 in PyObject_Call ()
#8  0x00000000004f492e in ?? ()
#9  0x00000000004b0cb3 in PyObject_Call ()
#10 0x0000000000553187 in ?? ()
#11 0x00000000004b0cb3 in PyObject_Call ()
#12 0x00000000004c6ad1 in PyEval_EvalFrameEx ()
#13 0x00000000004c2765 in PyEval_EvalCodeEx ()
#14 0x00000000004de6fe in ?? ()
#15 0x00000000004b0cb3 in PyObject_Call ()
#16 0x00000000004f492e in ?? ()
#17 0x00000000004b0cb3 in PyObject_Call ()
#18 0x0000000000553187 in ?? ()
#19 0x00000000004b0cb3 in PyObject_Call ()
```

Line 859 of mod.cpp is this:
`              PyList_SetItem(self->var_computed_cells[i], 0, one);
`



Discussion:
Confirmed. I'm investigating.

Resolving patch:
diff --git a/theano/gpuarray/reduction.py b/theano/gpuarray/reduction.py
--- a/theano/gpuarray/reduction.py
+++ b/theano/gpuarray/reduction.py
@@ -66,7 +66,6 @@ def c_code(self, node, name, input_names, output_names, sub):
         for (unsigned i = 0; i < %(name)s_redux_len; ++i) {
             PyObject* axis_object = PyTuple_GET_ITEM(%(axes)s, i);
             %(name)s_axes_to_reduce[i] = (unsigned) PyInt_AS_LONG(axis_object);
-            Py_XDECREF(axis_object);
         }
 
         size_t  %(name)s_input_ndim = PyGpuArray_NDIM(%(X)s);
@@ -136,3 +135,6 @@ def c_code_cleanup(self, node, name, inputs, outputs, sub):
         free(%(name)s_output_dims);
         free(%(name)s_axes_to_reduce);
         """ % {'name': name, 'X': inputs[0]}
+
+    def c_code_cache_version(self):
+        return (1,)
diff --git a/theano/tensor/basic.py b/theano/tensor/basic.py
--- a/theano/tensor/basic.py
+++ b/theano/tensor/basic.py
@@ -1259,7 +1259,6 @@ def c_code(self, node, name, inp, out, sub):
         } else if(PyTuple_GET_SIZE(%(axis)s) == 1) {
             PyObject* axis_object = PyTuple_GET_ITEM(%(axis)s, 0);
             axis = (int)PyInt_AS_LONG(axis_object);
-            Py_XDECREF(axis_object);
             if (axis > PyArray_NDIM(%(x)s)-1 || axis < -PyArray_NDIM(%(x)s)) {
                 PyErr_SetString(PyExc_ValueError,
                 "MaxAndArgmax: bad axis argument");
@@ -1308,7 +1307,7 @@ def c_code(self, node, name, inp, out, sub):
         return ret % locals()
 
     def c_code_cache_version(self):
-        return (4,)
+        return (5,)
 
     def infer_shape(self, node, shapes):
         ishape = shapes[0]


Test patch: