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

Issue:
inconsistency to use example module
I found some inconsistent behavior to import classes.

To use SOME_CLASS in chainer/iterators, the following code works.
```importIterator.py
import chainer
some_class = chainer.iterators.SOME_CLASS() # OK
```
However, to use SOME_CLASS in chainer/training/extensions, the following code does not work.
```importExtension.py
import chainer
some_class = chainer.training.extensions.SOME_CLASS() #NG
```

I feel some inconsistency here, because `chainer/__init__.py` contains both of `import iterator` and `import iterators` but `chaner/training/__init__.py` contains only `import extension` and not `import extensions`.

Is there any reason about this inconsistency?

* Conditions
  - Chainer version
    chainer v2.0.1
  - OS/Platform
    ubuntu 16.04
* Code to reproduce
```importModule.py
import chainer
iteratorExamples = chainer.iterators # OK

import chainer
extensionExamples = chainer.training.extensions #NG
```

* Error messages
```error.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'chainer.training' has no attribute 'extensions'
```





Discussion:
Thank you for your report. I feel it's good to fix the inconsistency by importing `extensions` as well as `iterators`.

Resolving patch:
diff --git a/chainer/training/__init__.py b/chainer/training/__init__.py
--- a/chainer/training/__init__.py
+++ b/chainer/training/__init__.py
@@ -1,7 +1,11 @@
 from chainer.training import extension  # NOQA
+from chainer.training import extensions  # NOQA
 from chainer.training import trainer  # NOQA
 from chainer.training import trigger  # NOQA
+from chainer.training import triggers  # NOQA
 from chainer.training import updater  # NOQA
+from chainer.training import updaters  # NOQA
+from chainer.training import util  # NOQA
 
 
 # import class and function
diff --git a/chainer/training/extensions/parameter_statistics.py b/chainer/training/extensions/parameter_statistics.py
--- a/chainer/training/extensions/parameter_statistics.py
+++ b/chainer/training/extensions/parameter_statistics.py
@@ -2,8 +2,8 @@
 import six
 
 from chainer import reporter
-from chainer import training
 from chainer.training import extension
+from chainer.training import trigger as trigger_module
 
 
 class ParameterStatistics(extension.Extension):
@@ -75,7 +75,7 @@ def __init__(self, links, statistics=default_statistics,
         self._attrs = attrs
 
         self._prefix = prefix
-        self._trigger = training.trigger.get_trigger(trigger)
+        self._trigger = trigger_module.get_trigger(trigger)
         self._summary = reporter.DictSummary()
 
     def __call__(self, trainer):


Test patch:
Repository: chainer/chainer

Issue:
Add order='C' option to `cupy.copy` calls using default order value.
Merge https://github.com/cupy/cupy/pull/159 first.

In https://github.com/cupy/cupy/pull/159, the default value of `cupy.copy`'s `order` argument is changed with breaking compatibility. We need to add `order='C'` option to `cupy.copy` calls with default `order` value in Chainer so that Chainer can work as currently it does.

`cupy.ndarray.copy` is not affected by https://github.com/cupy/cupy/pull/159, so it needs nothing.




Discussion:

Resolving patch:
diff --git a/chainer/functions/math/sqrt.py b/chainer/functions/math/sqrt.py
--- a/chainer/functions/math/sqrt.py
+++ b/chainer/functions/math/sqrt.py
@@ -24,7 +24,7 @@ def forward(self, x):
 
     def backward(self, x, gy):
         xp = cuda.get_array_module(*gy)
-        gx = xp.copy(self.output_data[0])
+        gx = self.output_data[0].copy()
         gx *= 2.0
         xp.reciprocal(gx, out=gx)
         gx *= gy[0]
diff --git a/chainer/gradient_check.py b/chainer/gradient_check.py
--- a/chainer/gradient_check.py
+++ b/chainer/gradient_check.py
@@ -12,7 +12,7 @@
 
 def _copy_arrays(xs):
     xp = cuda.get_array_module(*xs)
-    return [xp.copy(x) for x in xs]
+    return [xp.copy(x, order='C') for x in xs]
 
 
 def numerical_grad(f, inputs, grad_outputs, eps=1e-3):


Test patch:
Repository: chainer/chainer

Issue:
ChainList doesn't warn self.init_scope()
The following code has a bug.  Chainer doesn't warn the usage of `with self.init_scope()` in the subclass of ChainList.  Could you add a warning message for such mistakes?

* Code to reproduce
```python
import chainer
from chainer import Chain, ChainList
import chainer.functions as F
import chainer.links as L
import numpy as np


class C(ChainList):  # should be 'class C(Chain)'
    def __init__(self):
        super().__init__()
        with self.init_scope():
            self.l1 = L.Linear(5, 5)
            self.l2 = L.Linear(5, 5)

    def __call__(self, x):
        return self.l2(F.relu(self.l1(x)))


c = C()
print(c.l1.W.data)

opt = chainer.optimizers.SGD()
opt.setup(c)
x = np.arange(10).reshape((2, 5)).astype(np.float32)
loss = F.sum(c(x))
opt.update(lambda: loss)
print(c.l1.W.data)
```

output:
```
[[ 0.22224635  0.13709065 -0.0590423   0.31734523  0.76646286]
 [-0.09569775 -0.00810872  0.72896075 -0.50144166 -0.23909038]
 [ 0.24655567 -0.59849507  0.05945947 -0.06633393 -0.05738653]
 [-0.85418522  0.56111503 -0.4280332  -0.19436245 -0.09941436]
 [-0.06522682 -0.43800679  0.7132498   0.49363273 -0.2827867 ]]
[[ 0.22224635  0.13709065 -0.0590423   0.31734523  0.76646286]
 [-0.09569775 -0.00810872  0.72896075 -0.50144166 -0.23909038]
 [ 0.24655567 -0.59849507  0.05945947 -0.06633393 -0.05738653]
 [-0.85418522  0.56111503 -0.4280332  -0.19436245 -0.09941436]
 [-0.06522682 -0.43800679  0.7132498   0.49363273 -0.2827867 ]]
```
No update is taken because the link l1 is not registered.


Discussion:

Resolving patch:
diff --git a/chainer/link.py b/chainer/link.py
--- a/chainer/link.py
+++ b/chainer/link.py
@@ -820,6 +820,13 @@ def __init__(self, *links):
         for link in links:
             self.add_link(link)
 
+    def __setattr__(self, name, value):
+        if self.within_init_scope and isinstance(value, Link):
+            raise TypeError(
+                'cannot register a new link'
+                ' within a "with chainlist.init_scope():" block.')
+        super(ChainList, self).__setattr__(name, value)
+
     def __getitem__(self, index):
         """Returns the child at given index.
 


Test patch:
diff --git a/tests/chainer_tests/test_link.py b/tests/chainer_tests/test_link.py
--- a/tests/chainer_tests/test_link.py
+++ b/tests/chainer_tests/test_link.py
@@ -744,6 +744,18 @@ def test_append(self):
         self.assertIs(self.c2[1], self.l3)
         self.assertEqual(self.l3.name, '1')
 
+    def test_assign_param_in_init_scope(self):
+        p = chainer.Parameter()
+        with self.c1.init_scope():
+            self.c1.p = p
+        self.assertIn(p, self.c1.params())
+
+    def test_assign_link_in_init_scope(self):
+        l = chainer.Link()
+        with self.c1.init_scope():
+            with self.assertRaises(TypeError):
+                self.c1.l = l
+
     def test_iter(self):
         links = list(self.c2)
         self.assertEqual(2, len(links))
Repository: chainer/chainer

Issue:
document bug report: chainer.Link page has incorrect code.
https://docs.chainer.org/en/stable/reference/core/generated/chainer.Link.html?highlight=Link
there is an example:
```
 import chainer
import chainer.functions as F
from chainer import initializers
import numpy as np

class LinearLayer(chainer.Link):

    def __init__(self, n_in, n_out):
        super(LinearLayer, self).__init__()
        with self.init_scope():
            self.W = chainer.Parameter(
                (n_out, n_in), initializers.Normal())
            self.b = chainer.Parameter(
                (n_out,), initializers.Zero())

    def __call__(self, x):
        return F.linear(x, self.W, self.b)
```
but chainer.Parameter constructor function is define as :
class chainer.Parameter(**initializer=None, shape=None, name=None**)
note that the order of argument.
so if we omit the parameter keyword , this is incorrect code.


Discussion:

Resolving patch:
diff --git a/chainer/link.py b/chainer/link.py
--- a/chainer/link.py
+++ b/chainer/link.py
@@ -102,9 +102,9 @@ def __init__(self, n_in, n_out):
                   super(LinearLayer, self).__init__()
                   with self.init_scope():
                       self.W = chainer.Parameter(
-                          (n_out, n_in), initializers.Normal())
+                          initializers.Normal(), (n_out, n_in))
                       self.b = chainer.Parameter(
-                          (n_out,), initializers.Zero())
+                          initializers.Zero(), (n_out,))
 
               def __call__(self, x):
                   return F.linear(x, self.W, self.b)


Test patch:
Repository: chainer/chainer

Issue:
Fix confusing warning message
https://github.com/chainer/chainer/blob/79d6bf6f4f5c86ba705b8fd377368519bc1fd264/chainer/cuda.py#L84-L86

When cupy is not built with cudnn, one do not have to reinstall chainer but cupy.


Discussion:

Resolving patch:
diff --git a/chainer/cuda.py b/chainer/cuda.py
--- a/chainer/cuda.py
+++ b/chainer/cuda.py
@@ -82,8 +82,9 @@ def check_cuda_available():
             not getattr(check_cuda_available, '_already_warned', False)):
         warnings.warn(
             'cuDNN is not enabled.\n'
-            'Please reinstall chainer after you install cudnn\n'
-            '(see https://github.com/chainer/chainer#installation).')
+            'Please reinstall CuPy after you install cudnn\n'
+            '(see https://docs-cupy.chainer.org/en/stable/install.html'
+            '#install-cupy-with-cudnn-and-nccl).')
         check_cuda_available._already_warned = True
 
 


Test patch:
Repository: chainer/chainer

Issue:
Doc: Source links broken for functions defined with `@contextlib.contextmanager`
For example:
https://docs.chainer.org/en/v2.0.2/reference/core/generated/chainer.using_config.html


Discussion:

Resolving patch:
diff --git a/docs/source/conf.py b/docs/source/conf.py
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -430,6 +430,30 @@ def _check_object_validity(obj):
                 ''.format(obj, repr(doc)))
 
 
+def _get_sourcefile_and_linenumber(obj):
+    # Retrieve the original function wrapped by contextlib.contextmanager
+    if callable(obj):
+        closure = getattr(obj, '__closure__', None)
+        if closure is not None:
+            obj = closure[0].cell_contents
+
+    # Get the source file name and line number at which obj is defined.
+    try:
+        filename = inspect.getsourcefile(obj)
+    except TypeError:
+        # obj is not a module, class, function, ..etc.
+        return None, None
+
+    # inspect can return None for cython objects
+    if filename is None:
+        return None, None
+
+    # Get the source line number
+    _, linenum = inspect.getsourcelines(obj)
+
+    return filename, linenum
+
+
 def linkcode_resolve(domain, info):
     if domain != 'py' or not info['module']:
         return None
@@ -450,21 +474,11 @@ def linkcode_resolve(domain, info):
     if not (mod.__name__ == 'chainer' or mod.__name__.startswith('chainer.')):
         return None
 
-    # Get the source file name and line number at which obj is defined.
-    try:
-        filename = inspect.getsourcefile(obj)
-    except TypeError:
-        # obj is not a module, class, function, ..etc.
+    # Retrieve source file name and line number
+    filename, linenum = _get_sourcefile_and_linenumber(obj)
+    if filename is None or linenum is None:
         return None
 
-    # inspect can return None for cython objects
-    if filename is None:
-        return None
-
-    # Get the source line number
-    _, linenum = inspect.getsourcelines(obj)
-    assert isinstance(linenum, six.integer_types)
-
     filename = os.path.realpath(filename)
     relpath = _get_source_relative_path(filename)
 


Test patch:
Repository: chainer/chainer

Issue:
Document about initializers
Criteria for initializer arguments of links is complicated (https://github.com/chainer/chainer/pull/3259#issuecomment-325562538). We should write some dedicated documentation about that, and let each link documentation point to it. Maybe we can write on [this page](https://docs.chainer.org/en/v2.0.2/reference/initializers.html).

Also we should describe *the default initializer* (which is `LeCunNormal`).


Discussion:

Resolving patch:
diff --git a/chainer/links/connection/linear.py b/chainer/links/connection/linear.py
--- a/chainer/links/connection/linear.py
+++ b/chainer/links/connection/linear.py
@@ -24,14 +24,11 @@ class Linear(link.Link):
             data pass at which time the size will be determined.
         out_size (int): Dimension of output vectors.
         nobias (bool): If ``True``, then this function does not use the bias.
-        initialW (2-D array): Initial weight value. If ``None``, then the
-            default initializer is used.
-            May also be a callable that takes ``numpy.ndarray`` or
-            ``cupy.ndarray`` and edits its value.
-        initial_bias (1-D array): Initial bias value. If ``None``, the bias
-            vector is initialized to zero.
-            May also be a callable that takes ``numpy.ndarray`` or
-            ``cupy.ndarray`` and edits its value.
+        initialW (:ref:`initializer <initializer>`): Initializer to initialize
+            the weight.
+        initial_bias (:ref:`initializer <initializer>`): Initializer to
+            initialize the bias. If ``None``, the bias will be initialized to
+            zero.
     .. seealso:: :func:`~chainer.functions.linear`
 
     Attributes:


Test patch:
Repository: chainer/chainer

Issue:
Document deprecation of stream option in to_gpu
`stream` option in `cuda.to_gpu()` has been deprecated in #2238, but that is not documented.


Discussion:

Resolving patch:
diff --git a/chainer/cuda.py b/chainer/cuda.py
--- a/chainer/cuda.py
+++ b/chainer/cuda.py
@@ -237,8 +237,8 @@ def to_gpu(array, device=None, stream=None):
     Args:
         array: Array to be sent to GPU.
         device: Device specifier.
-        stream (cupy.cuda.Stream): CUDA stream. If not ``None``, the copy runs
-            asynchronously.
+        stream (~cupy.cuda.Stream): *(deprecated since v3.0.0)*
+            CUDA stream. If not ``None``, the copy runs asynchronously.
 
     Returns:
         cupy.ndarray: Array on GPU.


Test patch:
Repository: chainer/chainer

Issue:
Undocumented implicit SciPy dependency
The SVHN dataset helper depends on SciPy (to load MATLAB files) to work. Should this be documented somewhere? 
https://github.com/chainer/chainer/blob/master/chainer/datasets/svhn.py#L5

As a side note, although the import error is handled inside the helper, the tests strictly depend on the package being installed.


Discussion:

Resolving patch:
diff --git a/chainer/datasets/svhn.py b/chainer/datasets/svhn.py
--- a/chainer/datasets/svhn.py
+++ b/chainer/datasets/svhn.py
@@ -15,11 +15,16 @@ def get_svhn(withlabel=True, scale=1., dtype=numpy.float32,
              label_dtype=numpy.int32):
     """Gets the SVHN dataset.
 
-    `SVHN <http://ufldl.stanford.edu/housenumbers/>` is a dataset
-    similar to MNIST but composed of cropped images of house numbers.
-    The functionality is identical to the MNIST dataset,
+    `The Street View House Numbers (SVHN) dataset <http://ufldl.stanford.edu/housenumbers/>`_
+    is a dataset similar to MNIST but composed of cropped images of house
+    numbers.
+    The functionality of this function is identical to the counterpart for the
+    MNIST dataset (:func:`~chainer.datasets.get_mnist`),
     with the exception that there is no ``ndim`` argument.
 
+    .. note::
+       `SciPy <https://www.scipy.org/>`_ is required to use this feature.
+
     Args:
         withlabel (bool): If ``True``, it returns datasets with labels. In this
             case, each example is a tuple of an image and a label. Otherwise,
@@ -34,7 +39,7 @@ def get_svhn(withlabel=True, scale=1., dtype=numpy.float32,
         are :class:`~chainer.datasets.TupleDataset` instances. Otherwise, both
         datasets are arrays of images.
 
-    """
+    """  # NOQA
     if not _scipy_available:
         raise RuntimeError('scipy is not available')
 


Test patch:
Repository: chainer/chainer

Issue:
Grouped convolution/deconvolution does not work in CPU mode with NumPy 1.9
We should not use `xp.stack` which is introduced in NumPy 1.10.


Discussion:

Resolving patch:
diff --git a/chainer/functions/connection/convolution_2d.py b/chainer/functions/connection/convolution_2d.py
--- a/chainer/functions/connection/convolution_2d.py
+++ b/chainer/functions/connection/convolution_2d.py
@@ -217,9 +217,7 @@ def _forward_grouped_convolution(self, x, W, b):
                 _y = self._forward_gpu_core(_x[g, ], _W[g, ], _bg)
             _ys.append(_y)
 
-        y = xp.stack(_ys, axis=1)  # (N, G, oCg, oH, oW)
-        _, _, _, oH, oW = y.shape
-        y = y.reshape(N, oC, oH, oW)
+        y = xp.concatenate(_ys, axis=1)  # (N, oC, oH, oW)
         return y
 
     def _forward_cudnn(self, x, W, b, y):
@@ -378,8 +376,6 @@ def _forward_grouped_convolution(self, x, gy):
         # iC, iH, iW: input channels, input height, input width
         # oC, oH, oW: output channels, output height, output width
         G = self.group
-        kH = self.kh
-        kW = self.kw
         N, iC, iH, iW = x.shape
         _, oC, oH, oW = gy.shape
         iCg = int(iC / G)
@@ -403,8 +399,7 @@ def _forward_grouped_convolution(self, x, gy):
                 _gW = self._forward_gpu_core(_x[g, ], _gy[g, ])
             _gWs.append(_gW)
 
-        gW = xp.stack(_gWs)  # (G, oCg, iCg, kH, kW)
-        gW = gW.reshape(oC, iCg, kH, kW)
+        gW = xp.concatenate(_gWs)  # (oC, iCg, kH, kW)
         return gW
 
     def _forward_cudnn(self, x, gy):
diff --git a/chainer/functions/connection/deconvolution_2d.py b/chainer/functions/connection/deconvolution_2d.py
--- a/chainer/functions/connection/deconvolution_2d.py
+++ b/chainer/functions/connection/deconvolution_2d.py
@@ -212,9 +212,6 @@ def _forward_grouped_convolution(self, x, W, b):
         N, xC, xH, xW = x.shape
         xCg = int(xC / G)
         _, yCg, kH, kW = W.shape
-        yC = yCg * G
-        yH = self.outh
-        yW = self.outw
 
         xp = cuda.get_array_module(x)
 
@@ -233,8 +230,7 @@ def _forward_grouped_convolution(self, x, W, b):
                 _y = self._forward_gpu_core(_x[g, ], _W[g, ], _bg)
             _ys.append(_y)
 
-        y = xp.stack(_ys, axis=1)  # (N, G, yCg, yH, yW)
-        y = y.reshape(N, yC, yH, yW)
+        y = xp.concatenate(_ys, axis=1)  # (N, yC, yH, yW)
         return y
 
     def _forward_cudnn(self, x, W, b):


Test patch:
Repository: chainer/chainer

Issue:
Support optimization for rsqrt
CUDA provides `rsqrt` function, which is faster than doing `1.0 / sqrt(x)`.
It is better to provide custom kernel in Chainer or directly support in CuPy.
(Note that NumPy does not provide `rsqrt`)


Discussion:
CuPy already provide original functions in `cupy.ext`. It is a good idea to provide `rsqrt`.

Resolving patch:
diff --git a/chainer/functions/math/sqrt.py b/chainer/functions/math/sqrt.py
--- a/chainer/functions/math/sqrt.py
+++ b/chainer/functions/math/sqrt.py
@@ -1,3 +1,5 @@
+import numpy
+
 from chainer.backends import cuda
 from chainer import function_node
 from chainer import utils
@@ -27,6 +29,36 @@ def backward(self, indexes, grad_outputs):
         return gy / (gx * 2.0),
 
 
+class Rsqrt(function_node.FunctionNode):
+
+    @property
+    def label(self):
+        return 'rsqrt'
+
+    def check_type_forward(self, in_types):
+        type_check.expect(
+            in_types.size() == 1,
+            in_types[0].dtype.kind == 'f',
+        )
+
+    def forward(self, inputs):
+        self.retain_inputs((0,))
+        x, = inputs
+        xp = cuda.get_array_module(x)
+        dtype = x.dtype
+        if xp is numpy:
+            out = xp.reciprocal(xp.sqrt(x, dtype=dtype), dtype=dtype)
+        else:
+            # CuPy provides `rsqrt` which is faster than `1.0 / sqrt(x)`.
+            out = cuda.cupyx.rsqrt(x, dtype=dtype)
+        return utils.force_array(out),
+
+    def backward(self, indexes, grad_outputs):
+        x, = self.get_retained_inputs()
+        gy, = grad_outputs
+        return gy * (x ** -1.5) * -0.5,
+
+
 def sqrt(x):
     """Elementwise square root function.
 
@@ -59,4 +91,4 @@ def rsqrt(x):
 
     .. seealso:: :func:`~chainer.functions.sqrt`
     """
-    return 1.0 / sqrt(x)
+    return Rsqrt().apply((x,))[0]


Test patch:
diff --git a/tests/chainer_tests/functions_tests/math_tests/test_sqrt.py b/tests/chainer_tests/functions_tests/math_tests/test_sqrt.py
--- a/tests/chainer_tests/functions_tests/math_tests/test_sqrt.py
+++ b/tests/chainer_tests/functions_tests/math_tests/test_sqrt.py
@@ -27,15 +27,20 @@ class TestSqrt(unittest.TestCase):
 
 # rsqrt
 
-def rsqrt(x):
-    return numpy.reciprocal(numpy.sqrt(x))
+def rsqrt(x, dtype):
+    return numpy.reciprocal(numpy.sqrt(x, dtype=dtype), dtype=dtype)
 
 
+@testing.unary_math_function_unittest(
+    F.rsqrt,
+    func_expected=rsqrt,
+    make_data=make_data,
+    forward_options={'atol': 1e-2},
+    backward_options={'eps': 1e-2, 'atol': 1e-2, 'rtol': 1e-2},
+    double_backward_options={'eps': 1e-2, 'atol': 1e-1, 'rtol': 1e-1},
+)
 class TestRsqrt(unittest.TestCase):
-
-    def test_rsqrt(self):
-        x = numpy.random.uniform(0.1, 5, (3, 2)).astype(numpy.float32)
-        testing.assert_allclose(F.rsqrt(x).data, rsqrt(x))
+    pass
 
 
 testing.run_module(__name__, __file__)