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

Issue:
`create_huffman_tree` fails when two entries have the same counts in py3
The function uses a priority queue in its implementation. When two entries have the same counts, the queue tries to compare two tuples but it is not possible in py3.

For example, `create_huffman_tree({'x': 1, 'y': 2, 'z': 3})` fails because the function tries to compare `(3, ('x', 'y'))` and `(3, 'z')`, and compares a tuple and a str. It causes an error.



Discussion:

Resolving patch:
diff --git a/chainer/functions/hierarchical_softmax.py b/chainer/functions/hierarchical_softmax.py
--- a/chainer/functions/hierarchical_softmax.py
+++ b/chainer/functions/hierarchical_softmax.py
@@ -184,14 +184,17 @@ def create_huffman_tree(word_counts):
         raise ValueError('Empty vocabulary')
 
     q = six.moves.queue.PriorityQueue()
-    for w, c in six.iteritems(word_counts):
-        q.put((c, w))
+    # Add unique id to each entry so that we can compare two entries with same
+    # counts.
+    # Note that itreitems randomly order the entries.
+    for uid, (w, c) in enumerate(six.iteritems(word_counts)):
+        q.put((c, uid, w))
 
     while q.qsize() >= 2:
-        (count1, word1) = q.get()
-        (count2, word2) = q.get()
+        (count1, id1, word1) = q.get()
+        (count2, id2, word2) = q.get()
         count = count1 + count2
         tree = (word1, word2)
-        q.put((count, tree))
+        q.put((count, min(id1, id2), tree))
 
-    return q.get()[1]
+    return q.get()[2]


Test patch:
diff --git a/tests/functions_tests/test_hierarchical_softmax.py b/tests/functions_tests/test_hierarchical_softmax.py
--- a/tests/functions_tests/test_hierarchical_softmax.py
+++ b/tests/functions_tests/test_hierarchical_softmax.py
@@ -22,6 +22,13 @@ def test_simple(self):
         expect = (('z', 'y'), (('v', 'w'), 'x'))
         self.assertEqual(expect, tree)
 
+    def test_same_count(self):
+        tree = functions.create_huffman_tree(
+            {'x': 1, 'y': 2, 'z': 3})
+        # Order of the same items are not defined.
+        self.assertTrue((('x', 'y'), 'z') == tree or
+                        ('z', ('x', 'y')) == tree)
+
 
 class TestBinaryHierarchicalSoftmax(unittest.TestCase):
Repository: chainer/chainer

Issue:
Unbiased estimation in batch normalization function
Hi!

I'm now implementing neural network models with chainer.
I have a question regarding in the implementation of batch normalization function in chainer.
Following code is the computation of the average of means and the average of varinaces for the test mode in chainer/functions/batch_normalization.py

```
        m = ldim * rdim
        adjust = m / max(m - 1., 1.)  # unbiased estimation
        self.avg_mean *= decay
        self.avg_mean += (1 - decay) * adjust * mean
        self.avg_var *= decay
        self.avg_var += (1 - decay) * adjust * var
```

Why is the estimation of the "avg_mean" scaled by m/(m-1)?
I think only the "avg_var" needs to be scaled by m/(m-1) for unbiased estimation.
In the original paper of batch normalization:http://arxiv.org/pdf/1502.03167v3.pdf, only the average of variances is also scaled by m/(m-1), and the average of means is not scaled. 

Best,



Discussion:
Thank you for reporting the suspicious code! I agree you that the scaling is not needed for avg_mean. It seems a bug, so I will fix it.


Resolving patch:
diff --git a/chainer/functions/batch_normalization.py b/chainer/functions/batch_normalization.py
--- a/chainer/functions/batch_normalization.py
+++ b/chainer/functions/batch_normalization.py
@@ -115,7 +115,7 @@ def forward(self, x_orig):
             m = ldim * rdim
             adjust = m / max(m - 1., 1.)  # unbiased estimation
             self.avg_mean *= decay
-            self.avg_mean += (1 - decay) * adjust * mean
+            self.avg_mean += (1 - decay) * mean
             self.avg_var *= decay
             self.avg_var += (1 - decay) * adjust * var
 


Test patch:
Repository: chainer/chainer

Issue:
cupy.cudnn.reset_handles() fails for atexit run time
- chainer (fc79c3caf0a5ebea1bcbb1570b48c057577cf1ad) with GPU
- cuDNN not installed
- python (using pyenv)

```
Python 2.7.10 (default, Jun 19 2015, 20:41:53)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import chainer
>>> exit()
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "****/2.7.10/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "cupy/cudnn.py", line 35, in reset_handles
    for handle in six.itervalues(handles):
AttributeError: 'NoneType' object has no attribute 'itervalues'
Error in sys.exitfunc:
Traceback (most recent call last):
  File "***/2.7.10/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "cupy/cudnn.py", line 35, in reset_handles
    for handle in six.itervalues(handles):
AttributeError: 'NoneType' object has no attribute 'itervalues'
```

I can see the problem as follows
1. cupy/cudnn.py:10 `from cupy.cuda import cudnn` fails because cuDNN is not installed.
2. However, `import cupy.cudnn` fails because `cudnn` is used in cupy/cudnn.py (name 'cudnn' is not defined)
3. The handling of the global variable(`cupy.cudnn._handles`) of this time seems to be different from python3.3~ in python2.

test_code.py

```
import atexit

global_variable = []

@atexit.register
def exit_func():
    global global_variable
    print("global_variable is ", global_variable)


fail_code # this is fail code
```

python 2.7.10

```
Python 2.7.10 (default, Jun 19 2015, 20:41:53)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test_code
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test_code.py", line 11, in <module>
    fail_code # this is fail code
NameError: name 'fail_code' is not defined
>>> exit()
('global_variable is ', None)
```

python 3.4.3

```
Python 3.4.3 (default, Aug 13 2015, 16:03:08)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import test_code
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test_code.py", line 11, in <module>
    fail_code # this is fail code
NameError: name 'fail_code' is not defined
>>> exit()
global_variable is  []
```

The result is `[]` in the python3.3.x~, but becomes `None` in python2.
But python3.0.1, 3.1.5, 3.2.6 result is 'None', python 2.7.0 is [].



Discussion:
Thank you for the detailed report! I notice that I mistook to handle the availability of cuDNN. `cupy.cudnn` should not be importable if cuDNN is not available. I'll fix it.


Resolving patch:
diff --git a/cupy/cudnn.py b/cupy/cudnn.py
--- a/cupy/cudnn.py
+++ b/cupy/cudnn.py
@@ -5,12 +5,7 @@
 import six
 
 from cupy import cuda
-
-try:
-    from cupy.cuda import cudnn
-    available = True
-except Exception:
-    available = False
+from cupy.cuda import cudnn
 
 
 _handles = {}


Test patch:
Repository: chainer/chainer

Issue:
Clarify library version
Chainer doesn't work with old version of numpy. We need to clarify minimum versions of libraries.



Discussion:
Chainer doesn't work with numpy 1.8, but 1.9 is OK.


Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -33,7 +33,7 @@
         'cupy': ['carray.cuh'],
     },
     install_requires=['nose',
-                      'numpy',
+                      'numpy>=1.9.0',
                       'protobuf',
                       'six>=1.9.0'],
     tests_require=['mock',


Test patch:
Repository: chainer/chainer

Issue:
XConstant functions whose value is integer cannot access to label attributes
Functions in basic_math which involve operations on constant values (namely, `AddConstant`, `SubFromConstant`, `MulConstant`, `DivFromConstant`, `PowConstVar`, and `PowVarConst`) admit that the constant value is integer. But when we do access their label properties, `ValueError` is raised because `_convert_value_to_string`, which is called when creating labels, forbids the value being integer. We should allow broader constant cases.

In the first place, it could be inappropriate to check the type of the constant value in `constant_value_to_string`. I think we should check the type in instantiation.



Discussion:

Resolving patch:
diff --git a/chainer/functions/basic_math.py b/chainer/functions/basic_math.py
--- a/chainer/functions/basic_math.py
+++ b/chainer/functions/basic_math.py
@@ -15,13 +15,16 @@ def _convert_value_to_string(value):
     if isinstance(value, variable.Variable):
         value = value.data
 
-    if isinstance(value, float):
-        return str(value)
+    if numpy.isscalar(value):
+        if value < 0:
+            return '({})'.format(value)
+        else:
+            return str(value)
     elif isinstance(value, (numpy.ndarray, cuda.ndarray)):
         return 'constant array'
     else:
         raise ValueError(
-            'value must be float, ndarray, or Variable')
+            'value must be scalar, ndarray, or Variable')
 
 
 def _check_constant_type(value):


Test patch:
diff --git a/tests/chainer_tests/functions_tests/test_basic_math.py b/tests/chainer_tests/functions_tests/test_basic_math.py
--- a/tests/chainer_tests/functions_tests/test_basic_math.py
+++ b/tests/chainer_tests/functions_tests/test_basic_math.py
@@ -1134,6 +1134,45 @@ def test_nonzero(self):
                 pass
 
 
+class ConvertValueToStringTest(unittest.TestCase):
+
+    def _check_scalar(self, value, string):
+        self.assertEqual(basic_math._convert_value_to_string(value), string)
+
+    def test_integer_positive(self):
+        self._check_scalar(2, '2')
+
+    def test_integer_zero(self):
+        self._check_scalar(0, '0')
+
+    def test_integer_negative(self):
+        self._check_scalar(-2, '(-2)')
+
+    def test_float_positive(self):
+        self._check_scalar(2.0, '2.0')
+
+    def test_float_zero(self):
+        self._check_scalar(0.0, '0.0')
+
+    def test_float_negative(self):
+        self._check_scalar(-2.0, '(-2.0)')
+
+    def test_numpy_scalar(self):
+        self._check_scalar(numpy.float32(2), '2.0')
+
+    def _check_array(self, value, string):
+        self.assertEqual(basic_math._convert_value_to_string(value), string)
+        value = chainer.Variable(value)
+        self.assertEqual(basic_math._convert_value_to_string(value), string)
+
+    def test_array_cpu(self):
+        self._check_array(numpy.array([1, 2]), 'constant array')
+
+    @attr.gpu
+    def test_array_gpu(self):
+        self._check_array(cuda.ndarray([1, 2]), 'constant array')
+
+
 class TestLabel(unittest.TestCase):
 
     def test_neg(self):
Repository: chainer/chainer

Issue:
Make a method to disable type check
Type checking is large overhead. We need to make a method to disable it.
related to #333 



Discussion:

Resolving patch:
diff --git a/chainer/function.py b/chainer/function.py
--- a/chainer/function.py
+++ b/chainer/function.py
@@ -1,4 +1,5 @@
 import copy
+import os
 import weakref
 
 import numpy
@@ -92,10 +93,15 @@ class Function(object):
             or otherwise it should override :meth:`parameters` property.
         gradient_names: A tuple or list of names of gradient attributes. The
             detail is same as :data:`parameter_names`.
+        type_check_enable: When it is ``True``, the function checks types of
+            input arguments. Set ``CHAINER_TYPE_CHECK`` environment variable
+            ``0`` to disable type check, or set the variable directly in
+            your own program.
 
     """
     parameter_names = ()
     gradient_names = ()
+    type_check_enable = int(os.environ.get('CHAINER_TYPE_CHECK', '1')) != 0
 
     def __init__(self):
         self.inputs = None
@@ -134,7 +140,8 @@ def __call__(self, *inputs):
             assert all(x.volatile for x in inputs)
 
             in_data = tuple(x.data for x in inputs)
-            self._check_data_type_forward(in_data)
+            if self.type_check_enable:
+                self._check_data_type_forward(in_data)
             with cuda.get_device(*in_data):
                 out_data = self.forward(in_data)
             assert type(out_data) == tuple
@@ -161,7 +168,8 @@ def __call__(self, *inputs):
             self.rank = 0
 
         in_data = tuple(x.data for x in self.inputs)
-        self._check_data_type_forward(in_data)
+        if self.type_check_enable:
+            self._check_data_type_forward(in_data)
         with cuda.get_device(*in_data):
             outputs = self.forward(in_data)
         assert type(outputs) == tuple


Test patch:
Repository: chainer/chainer

Issue:
Make a test for code snippets in the document
https://docs.python.org/2/library/doctest.html



Discussion:
I found a cool extension: http://sphinx-doc.org/ext/doctest.html#module-sphinx.ext.doctest


Resolving patch:
diff --git a/docs/source/conf.py b/docs/source/conf.py
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -23,14 +23,16 @@
 
 sys.path.insert(0, '../..')
 
-# Replace LoadLibrary to mock for importing CuPy on ReadTheDocs.
-class MockObject(object):
+on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
+if on_rtd:
+    # Replace LoadLibrary to mock for importing CuPy on ReadTheDocs.
+    class MockObject(object):
 
-    def __getattr__(self, key): return MockObject()
-    def __setattr__(self, key, value): pass
-    def __call__(self, *a, **k): return MockObject()
+        def __getattr__(self, key): return MockObject()
+        def __setattr__(self, key, value): pass
+        def __call__(self, *a, **k): return MockObject()
 
-ctypes.cdll.LoadLibrary = MockObject()
+    ctypes.cdll.LoadLibrary = MockObject()
 
 
 # If extensions (or modules to document with autodoc) are in another directory,
@@ -48,6 +50,7 @@ def __call__(self, *a, **k): return MockObject()
 # ones.
 extensions = ['sphinx.ext.autodoc',
               'sphinx.ext.autosummary',
+              'sphinx.ext.doctest',
               'sphinx.ext.intersphinx',
               'sphinx.ext.mathjax',
               'sphinx.ext.napoleon',
@@ -324,3 +327,12 @@ def __call__(self, *a, **k): return MockObject()
     'python': ('https://docs.python.org/2/', None),
     'numpy': ('http://docs.scipy.org/doc/numpy/', None),
 }
+
+doctest_global_setup = '''
+import numpy as np
+import cupy
+from chainer import cuda, Function, FunctionSet, gradient_check, Variable, optimizers, utils
+import chainer.functions as F
+np.random.seed(0)
+'''
+doctest_test_doctest_blocks = None


Test patch:
Repository: chainer/chainer

Issue:
cupy.ndarray.__nonzero__ returns ndarray
`numpy.ndarray.__nonzero__` raises `ValueError`, but `cupy.ndarray.__nonzero__` returns `ndarray`, and then it causes `TypeError`.

```
>>> bool(numpy.array([1, 2]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> bool(cupy.array([1, 2]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __nonzero__ should return bool or int, returned ndarray
```

When an array represents a scalar, `numpy.ndarray` returns a boolean value, but `cupy.ndarray` causes an error.

```
>>> bool(numpy.array(1))
True
>>> bool(cupy.array(1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __nonzero__ should return bool or int, returned ndarray
```

https://github.com/pfnet/chainer/blob/master/cupy/__init__.py#L666



Discussion:

Resolving patch:
diff --git a/cupy/__init__.py b/cupy/__init__.py
--- a/cupy/__init__.py
+++ b/cupy/__init__.py
@@ -664,7 +664,17 @@ def __ne__(self, other):
     # Truth value of an array (bool):
 
     def __nonzero__(self):
-        return self != 0
+        if self._size == 0:
+            return False
+        elif self._size == 1:
+            return bool(self.get())
+        else:
+            msg = 'The truth value of an array with more than one element is ' \
+                  'ambiguous. Use a.get().any() or a.get().all()'
+            raise ValueError(msg)
+
+    def __bool__(self):
+        return self.__nonzero__()
 
     # Unary operations:
 


Test patch:
diff --git a/tests/cupy_tests/test_ndarray_unary_op.py b/tests/cupy_tests/test_ndarray_unary_op.py
new file mode 100644
--- /dev/null
+++ b/tests/cupy_tests/test_ndarray_unary_op.py
@@ -0,0 +1,39 @@
+import unittest
+
+import numpy
+
+import cupy
+from cupy import testing
+
+
+@testing.gpu
+class TestArrayBoolOp(unittest.TestCase):
+
+    _multiprocess_can_split_ = True
+
+    @testing.for_all_dtypes()
+    def test_bool_empty(self, dtype):
+        self.assertFalse(bool(cupy.array((), dtype=dtype)))
+
+    def test_bool_scalar_bool(self):
+        self.assertTrue(bool(cupy.array(True, dtype=numpy.bool)))
+        self.assertFalse(bool(cupy.array(False, dtype=numpy.bool)))
+
+    @testing.for_all_dtypes()
+    def test_bool_scalar(self, dtype):
+        self.assertTrue(bool(cupy.array(1, dtype=dtype)))
+        self.assertFalse(bool(cupy.array(0, dtype=dtype)))
+
+    def test_bool_one_element_bool(self):
+        self.assertTrue(bool(cupy.array([True], dtype=numpy.bool)))
+        self.assertFalse(bool(cupy.array([False], dtype=numpy.bool)))
+
+    @testing.for_all_dtypes()
+    def test_bool_one_element(self, dtype):
+        self.assertTrue(bool(cupy.array([1], dtype=dtype)))
+        self.assertFalse(bool(cupy.array([0], dtype=dtype)))
+
+    @testing.for_all_dtypes()
+    def test_bool_two_elements(self, dtype):
+        with self.assertRaises(ValueError):
+            bool(cupy.array([1, 2], dtype=dtype))
Repository: chainer/chainer

Issue:
Seed cannot be fixed via an environment variable
Past version of Chainer supported the CHAINER_SEED environment variable, which is (maybe accidentally) missed in v1.3.0.



Discussion:

Resolving patch:
diff --git a/cupy/random/generator.py b/cupy/random/generator.py
--- a/cupy/random/generator.py
+++ b/cupy/random/generator.py
@@ -226,7 +226,7 @@ def get_random_state():
     dev = cuda.Device()
     rs = _random_states.get(dev.id, None)
     if rs is None:
-        rs = RandomState()
+        rs = RandomState(os.getenv('CHAINER_SEED'))
         _random_states[dev.id] = rs
     return rs
 


Test patch:
diff --git a/tests/cupy_tests/random_tests/test_generator.py b/tests/cupy_tests/random_tests/test_generator.py
--- a/tests/cupy_tests/random_tests/test_generator.py
+++ b/tests/cupy_tests/random_tests/test_generator.py
@@ -1,4 +1,5 @@
 import mock
+import os
 import unittest
 
 import numpy
@@ -144,6 +145,10 @@ class TestGetRandomState(unittest.TestCase):
 
     def setUp(self):
         self.device_id = cuda.Device().id
+        self.rs_tmp = generator._random_states
+
+    def tearDown(self, *args):
+        generator._random_states = self.rs_tmp
 
     def test_get_random_state_initialize(self):
         generator._random_states = {}
@@ -159,6 +164,30 @@ def test_get_random_state_memoized(self):
         self.assertEqual('expected', rs)
 
 
+@testing.gpu
+class TestGetRandomState2(unittest.TestCase):
+
+    def setUp(self):
+        self.rs_tmp = generator.RandomState
+        generator.RandomState = mock.Mock()
+        self.rs_dict = generator._random_states
+        generator._random_states = {}
+
+    def tearDown(self, *args):
+        generator.RandomState = self.rs_tmp
+        generator._random_states = self.rs_dict
+
+    def test_get_random_state_no_chainer_seed(self):
+        os.unsetenv('CHAINER_SEED')
+        generator.get_random_state()
+        generator.RandomState.assert_called_with(None)
+
+    def test_get_random_state_with_chainer_seed(self):
+        os.environ['CHAINER_SEED'] = '1'
+        generator.get_random_state()
+        generator.RandomState.assert_called_with('1')
+
+
 class TestGetSize(unittest.TestCase):
 
     def test_none(self):
Repository: chainer/chainer

Issue:
Add tests on cupy.asnumpy
All of decorators in `cupy.testing.array` check the equivalence of two `cupy.ndarray`s by converting them into `numpy.ndarray` with `cupy.asnumpy`. Since these decorators are used in many unit tests, we should test `cupy.asnumpy` to validate the correctness of this function.

`cupy.asnumpy` is essentially same as `cupy.ndarray.get`. So we will also have to test the latter one.



Discussion:

Resolving patch:
diff --git a/cupy/cuda/memory.py b/cupy/cuda/memory.py
--- a/cupy/cuda/memory.py
+++ b/cupy/cuda/memory.py
@@ -198,8 +198,8 @@ def copy_to_host_async(self, mem, size, stream):
 
         """
         if size > 0:
-            runtime.memcpyAsync(mem, self.ptr, size, stream,
-                                runtime.memcpyDeviceToHost)
+            runtime.memcpyAsync(mem, self.ptr, size,
+                                runtime.memcpyDeviceToHost, stream)
 
     def memset(self, value, size):
         """Fills a memory sequence by constant byte value.


Test patch:
diff --git a/tests/cupy_tests/test_ndarray_get.py b/tests/cupy_tests/test_ndarray_get.py
new file mode 100644
--- /dev/null
+++ b/tests/cupy_tests/test_ndarray_get.py
@@ -0,0 +1,45 @@
+import unittest
+
+import cupy
+from cupy import cuda
+from cupy import testing
+import numpy
+from numpy import testing as np_testing
+
+
+@testing.gpu
+class TestArrayGet(unittest.TestCase):
+
+    _multiprocess_can_split_ = True
+
+    def setUp(self):
+        self.stream = cuda.Stream()
+
+    def check_get(self, f, stream):
+        a_gpu = f(cupy)
+        a_cpu = f(numpy)
+        np_testing.assert_array_equal(a_gpu.get(stream), a_cpu)
+
+    @testing.for_all_dtypes()
+    def test_contiguous_array(self, dtype):
+        contiguous_array = lambda xp: testing.shaped_arange(
+            (3,), xp=xp, dtype=dtype)
+        self.check_get(contiguous_array, None)
+
+    @testing.for_all_dtypes()
+    def test_non_contiguous_array(self, dtype):
+        non_contiguous_array = lambda xp: testing.shaped_arange(
+            (3,), xp=xp, dtype=dtype)[0::2]
+        self.check_get(non_contiguous_array, None)
+
+    @testing.for_all_dtypes()
+    def test_contiguous_array_stream(self, dtype):
+        contiguous_array = lambda xp: testing.shaped_arange(
+            (3,), xp=xp, dtype=dtype)
+        self.check_get(contiguous_array, self.stream.ptr)
+
+    @testing.for_all_dtypes()
+    def test_non_contiguous_array_stream(self, dtype):
+        non_contiguous_array = lambda xp: testing.shaped_arange(
+            (3,), xp=xp, dtype=dtype)[0::2]
+        self.check_get(non_contiguous_array, self.stream.ptr)
Repository: chainer/chainer

Issue:
Restore cuda.init
Older versions of Chainer have `cuda.init`, which is missing in newest version (v1.3.2). For backward compatibility, we should add this (deprecated) API, in which actually do nothing.



Discussion:

Resolving patch:
diff --git a/chainer/cuda.py b/chainer/cuda.py
--- a/chainer/cuda.py
+++ b/chainer/cuda.py
@@ -60,6 +60,15 @@ class ndarray(object):
         _resolution_error = e
 
 
+def init(arg=None):
+    warnings.warn(
+        'chainer.cuda.init is deprecated. You need to call nothing to '
+        'initialize your environment. Call chainer.cuda.check_cuda_available '
+        'to check availability of CUDA.',
+        DeprecationWarning)
+    check_cuda_available()
+
+
 def check_cuda_available():
     """Checks if CUDA is available.
 


Test patch: