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

Issue:
cuda.to_cpu does not move a Variable to CPU
It even does not raises an error. It silently returns the original variable without any modification. This behavior is confusing.



Discussion:

Resolving patch:
diff --git a/chainer/cuda.py b/chainer/cuda.py
--- a/chainer/cuda.py
+++ b/chainer/cuda.py
@@ -190,7 +190,7 @@ def to_cpu(array, stream=None):
     """Copies the given GPU array to host CPU.
 
     Args:
-        array: Array to be sent to GPU.
+        array: Array to be sent to CPU.
         stream (cupy.cuda.Stream): CUDA stream.
 
     Returns:
@@ -200,11 +200,13 @@ def to_cpu(array, stream=None):
         ``array`` without performing any copy.
 
     """
-    assert stream is None  # TODO(beam2d): FIX IT
     if isinstance(array, ndarray):
-        return array.get()
-    else:
+        return array.get(stream)
+    elif isinstance(array, numpy.ndarray):
         return array
+    else:
+        raise TypeError(
+            'The array sent to cpu must be numpy.ndarray or cupy.ndarray')
 
 
 def empty(shape, dtype=numpy.float32):


Test patch:
diff --git a/tests/chainer_tests/test_cuda.py b/tests/chainer_tests/test_cuda.py
--- a/tests/chainer_tests/test_cuda.py
+++ b/tests/chainer_tests/test_cuda.py
@@ -2,8 +2,10 @@
 
 import numpy
 
+import chainer
 from chainer import cuda
 from chainer import testing
+from chainer.testing import attr
 
 
 class TestCuda(unittest.TestCase):
@@ -30,4 +32,27 @@ def test_empy_like_unavailable(self):
                 cuda.empty_like(x)
 
 
+class TestToCPU(unittest.TestCase):
+
+    def setUp(self):
+        self.x = numpy.random.uniform(-1, 1, (2, 3))
+
+    def test_numpy_array(self):
+        y = cuda.to_cpu(self.x)
+        self.assertIs(self.x, y)  # Do not copy
+
+    @attr.gpu
+    def test_cupy_array(self):
+        x = cuda.to_gpu(self.x)
+        y = cuda.to_cpu(x)
+        self.assertIsInstance(y, numpy.ndarray)
+        numpy.testing.assert_array_equal(self.x, y)
+
+    def test_variable(self):
+        x = numpy.random.uniform(-1, 1, (2, 3))
+        x = chainer.Variable(x)
+        with self.assertRaises(TypeError):
+            cuda.to_cpu(x)
+
+
 testing.run_module(__name__, __file__)
Repository: chainer/chainer

Issue:
Use `utils.type_check.prod`
`utils.type_check.prod` is available. Use it in type check.



Discussion:

Resolving patch:
diff --git a/chainer/functions/connection/bilinear.py b/chainer/functions/connection/bilinear.py
--- a/chainer/functions/connection/bilinear.py
+++ b/chainer/functions/connection/bilinear.py
@@ -159,10 +159,9 @@ def check_type_forward(self, in_types):
         )
 
         in_sizes = type_check.Variable(self.in_sizes, 'in_sizes')
-        type_check_prod = type_check.Variable(numpy.prod, 'prod')
         type_check.expect(
-            type_check_prod(e1_type.shape[1:]) == in_sizes[0],
-            type_check_prod(e2_type.shape[1:]) == in_sizes[1]
+            type_check.prod(e1_type.shape[1:]) == in_sizes[0],
+            type_check.prod(e2_type.shape[1:]) == in_sizes[1]
         )
 
     def zero_grads(self):
diff --git a/chainer/functions/connection/linear.py b/chainer/functions/connection/linear.py
--- a/chainer/functions/connection/linear.py
+++ b/chainer/functions/connection/linear.py
@@ -97,7 +97,7 @@ def check_type_forward(self, in_types):
         type_check.expect(
             x_type.dtype == numpy.float32,
             x_type.ndim >= 2,
-            (type_check.Variable(numpy.prod, 'prod')(x_type.shape[1:]) ==
+            (type_check.prod(x_type.shape[1:]) ==
              type_check.Variable(self.W.shape[1], 'W.shape[1]')),
         )
 
@@ -137,13 +137,12 @@ def check_type_forward(self, in_types):
         x_type = in_types[0]
         w_type = in_types[1]
 
-        prod = type_check.Variable(numpy.prod, 'prod')
         type_check.expect(
             x_type.dtype == numpy.float32,
             w_type.dtype == numpy.float32,
             x_type.ndim >= 2,
             w_type.ndim == 2,
-            prod(x_type.shape[1:]) == w_type.shape[1],
+            type_check.prod(x_type.shape[1:]) == w_type.shape[1],
         )
         if in_types.size().eval() == 3:
             b_type = in_types[2]


Test patch:
diff --git a/tests/chainer_tests/utils_tests/test_type_check.py b/tests/chainer_tests/utils_tests/test_type_check.py
--- a/tests/chainer_tests/utils_tests/test_type_check.py
+++ b/tests/chainer_tests/utils_tests/test_type_check.py
@@ -337,4 +337,13 @@ def test_nest_list_str(self):
         self.assertEqual('[[0]]', T._repr([[T.Constant(0)]]))
 
 
+class TestProd(unittest.TestCase):
+
+    def test_name(self):
+        self.assertEqual(T.prod.name, 'prod')
+
+    def test_value(self):
+        self.assertIs(T.prod.value, numpy.prod)
+
+
 testing.run_module(__name__, __file__)
Repository: chainer/chainer

Issue:
Error type mismatch when broadcast fails
When broadcast fails, numpy causes `ValueError`, but cupy causes `RuntimeError`.



Discussion:

Resolving patch:
diff --git a/cupy/manipulation/dims.py b/cupy/manipulation/dims.py
--- a/cupy/manipulation/dims.py
+++ b/cupy/manipulation/dims.py
@@ -149,7 +149,7 @@ def __init__(self, *arrays):
                 in six_zip(r_shape, a._shape[rev], a._strides[rev])]
 
             if None in r_strides:
-                raise RuntimeError('Broadcasting failed')
+                raise ValueError('Broadcasting failed')
 
             offset = (0,) * (ndim - len(r_strides))
 


Test patch:
diff --git a/cupy/testing/__init__.py b/cupy/testing/__init__.py
--- a/cupy/testing/__init__.py
+++ b/cupy/testing/__init__.py
@@ -18,6 +18,7 @@
 numpy_cupy_array_equal = helper.numpy_cupy_array_equal
 numpy_cupy_array_list_equal = helper.numpy_cupy_array_list_equal
 numpy_cupy_array_less = helper.numpy_cupy_array_less
+numpy_cupy_raises = helper.numpy_cupy_raises
 for_dtypes = helper.for_dtypes
 for_all_dtypes = helper.for_all_dtypes
 for_float_dtypes = helper.for_float_dtypes
diff --git a/cupy/testing/helper.py b/cupy/testing/helper.py
--- a/cupy/testing/helper.py
+++ b/cupy/testing/helper.py
@@ -131,6 +131,30 @@ def test_func(self, *args, **kw):
     return decorator
 
 
+def numpy_cupy_raises(name='xp'):
+    def decorator(impl):
+        @functools.wraps(impl)
+        def test_func(self, *args, **kw):
+            kw[name] = cupy
+            try:
+                impl(self, *args, **kw)
+                cupy_error = None
+            except Exception as e:
+                cupy_error = e
+
+            kw[name] = numpy
+            try:
+                impl(self, *args, **kw)
+                numpy_error = None
+            except Exception as e:
+                numpy_error = e
+
+            self.assertIs(type(cupy_error), type(numpy_error))
+            self.assertIsNotNone(cupy_error)
+        return test_func
+    return decorator
+
+
 def for_dtypes(dtypes, name='dtype'):
     def decorator(impl):
         @functools.wraps(impl)
diff --git a/tests/cupy_tests/manipulation_tests/test_dims.py b/tests/cupy_tests/manipulation_tests/test_dims.py
--- a/tests/cupy_tests/manipulation_tests/test_dims.py
+++ b/tests/cupy_tests/manipulation_tests/test_dims.py
@@ -59,6 +59,12 @@ def test_broadcast(self):
         self.assertEqual(2 * 3 * 3 * 4, bc.size)
         self.assertEqual(4, bc.nd)
 
+    @testing.numpy_cupy_raises()
+    def test_broadcast_fail(self, xp):
+        a = xp.zeros((2, 3))
+        b = xp.zeros((3, 2))
+        xp.broadcast(a, b)
+
     @testing.numpy_cupy_array_equal()
     def test_expand_dims0(self, xp):
         a = testing.shaped_arange((2, 3), xp)
Repository: chainer/chainer

Issue:
with chainer.cuda.get_device(x) fails when x is 0-size cupy.ndarray
```
In [48]: x
Out[48]: array([], shape=(0, 10), dtype=float32)

In [49]: with chainer.cuda.get_device(b) as i:
   ....:     i
   ....:     
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-49-d8b685ac1ab0> in <module>()
----> 1 with chainer.cuda.get_device(x) as i:
      2     i
      3 

AttributeError: __exit__
```



Discussion:
One possible solution is to check if `cuda.ndarray.device` is `None` in `get_device`.

Do you have a minimal test code to check this bug?

Sorry I failed to edit the code that reproduces the error.

```
In [1]: import cupy
In [2]: x = cupy.array([]).reshape((0, 10))
In [3]: import chainer
In [4]: with chainer.cuda.get_device(x) as i:
   ...:     pass
   ...: 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-fa05828b6a46> in <module>()
----> 1 with chainer.cuda.get_device(x) as i:
      2     pass
      3 

AttributeError: __exit__
```

More simply:

```
>>> x=cupy.ndarray((0,))
>>> with x.device as i:
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __exit__
```

In `cupy.cuda.memory`, when size is zero, `Memory` doesn't set `_device` member.
https://github.com/pfnet/chainer/blob/master/cupy/cuda/memory.py#L23


Resolving patch:
diff --git a/chainer/cuda.py b/chainer/cuda.py
--- a/chainer/cuda.py
+++ b/chainer/cuda.py
@@ -153,6 +153,8 @@ def get_device(*args):
         if not isinstance(arg, numpy.ndarray):
             check_cuda_available()
             if isinstance(arg, cupy.ndarray):
+                if arg.device is None:
+                    continue
                 return arg.device
             else:
                 return Device(arg)


Test patch:
diff --git a/tests/chainer_tests/test_cuda.py b/tests/chainer_tests/test_cuda.py
--- a/tests/chainer_tests/test_cuda.py
+++ b/tests/chainer_tests/test_cuda.py
@@ -14,6 +14,11 @@ def test_get_dummy_device(self):
         if not cuda.available:
             self.assertIs(cuda.get_device(), cuda.DummyDevice)
 
+    @attr.gpu
+    def test_get_dummy_device_for_empty_array(self):
+        x = cuda.cupy.array([]).reshape((0, 10))
+        self.assertIs(cuda.get_device(x), cuda.DummyDevice)
+
     def test_to_gpu_unavailable(self):
         x = numpy.array([1])
         if not cuda.available:
Repository: chainer/chainer

Issue:
where doesn't work with int16 and float64 on cuda6.5
On only cuda 6.5, this code doesn't work:

```
x = cupy.array([1,2,3], dtype=cupy.int16)
y = cupy.array([1,2,3], dtype=cupy.float64)
c = cupy.array([1,0,1], dtype=cupy.bool_)
cupy.where(c, x, y)
```

Other combinations such as (int16, float32) and (int32, float64) correctly work.
Maybe this is a bug on cuda 6.5, and fixed on 7.0.

Note that `cupy.where(c, y, x)` can work.



Discussion:
`bool, int8, uint8, int16, uint16` has the same problem.


Resolving patch:
diff --git a/cupy/sorting/search.py b/cupy/sorting/search.py
--- a/cupy/sorting/search.py
+++ b/cupy/sorting/search.py
@@ -93,7 +93,12 @@ def where(condition, x=None, y=None):
 _where_ufunc = elementwise.create_ufunc(
     'cupy_where',
     ('???->?', '?bb->b', '?BB->B', '?hh->h', '?HH->H', '?ii->i', '?II->I',
-     '?ll->l', '?LL->L', '?qq->q', '?QQ->Q', '?ee->e', '?ff->f', '?dd->d'),
+     '?ll->l', '?LL->L', '?qq->q', '?QQ->Q', '?ee->e', '?ff->f',
+     # On CUDA 6.5 these combinations don't work correctly (on CUDA >=7.0, it
+     # works).
+     # See issue #551.
+     '?hd->d', '?Hd->d',
+     '?dd->d'),
     'out0 = in0 ? in1 : in2')
 
 


Test patch:
Repository: chainer/chainer

Issue:
cupy.count_nonzero does not work correctly
count_nonzero(x) should return the count of non-zero elements in x. The current implementation is not correct.

``` python
>>> a = cupy.eye(4)
>>> cupy.count_nonzero(a)
1
```

Note that this bug is overlooked because the test also has a bug. `testing.shaped_random` with dtype=bool almost always returns an array filled by False. It seems a bug of shaped_random(dtype=bool), since it should return a random boolean array. We should fix it, too.



Discussion:

Resolving patch:
diff --git a/cupy/reduction.py b/cupy/reduction.py
--- a/cupy/reduction.py
+++ b/cupy/reduction.py
@@ -483,11 +483,13 @@ def create_reduction_func(name, ops, routine=None, identity=None,
 __device__ min_max_st my_argmin(const min_max_st& a, const min_max_st& b) {
     if (a.index == -1) return b;
     if (b.index == -1) return a;
+    if (a.value == b.value) return min_max_st(a.value, min(a.index, b.index));
     return (a.value <= b.value) ? a : b;
 }
 __device__ min_max_st my_argmax(const min_max_st& a, const min_max_st& b) {
     if (a.index == -1) return b;
     if (b.index == -1) return a;
+    if (a.value == b.value) return min_max_st(a.value, min(a.index, b.index));
     return (a.value >= b.value) ? a : b;
 }'''
 
diff --git a/cupy/sorting/count.py b/cupy/sorting/count.py
--- a/cupy/sorting/count.py
+++ b/cupy/sorting/count.py
@@ -18,4 +18,4 @@ def count_nonzero(x):
     'cupy_count_nonzero',
     ('?->l', 'B->l', 'h->l', 'H->l', 'i->l', 'I->l', 'l->l', 'L->l',
      'q->l', 'Q->l', 'e->l', 'f->l', 'd->l'),
-    ('in0', 'a + b', 'out0 = (a != 0)', None), 0)
+    ('in0 != 0', 'a + b', 'out0 = a', None), 0)


Test patch:
diff --git a/cupy/testing/helper.py b/cupy/testing/helper.py
--- a/cupy/testing/helper.py
+++ b/cupy/testing/helper.py
@@ -232,11 +232,10 @@ def shaped_reverse_arange(shape, xp=cupy, dtype=numpy.float32):
 
 def shaped_random(shape, xp=cupy, dtype=numpy.float32, scale=10, seed=0):
     numpy.random.seed(seed)
-    a = numpy.random.rand(*shape) * scale
     if numpy.dtype(dtype).type == numpy.bool_:
-        return xp.array((a % 2 == 0))
+        return xp.asarray(numpy.random.randint(2, size=shape).astype(dtype))
     else:
-        return xp.array(a.astype(dtype))
+        return xp.asarray((numpy.random.rand(*shape) * scale).astype(dtype))
 
 
 class NumpyError(object):
Repository: chainer/chainer

Issue:
EmbedID on word sequences
The current EmbedID converts a vector of word IDs to a real matrix. If one wants an embedding of all words in sequences of length T at once, it requires T calls of EmbedID, which is really slow. It can be avoided by extending EmbedID (directly or by making another one) to accept a matrix of word IDs and outputs a real array with 3 axes.

(This issue is suggested by Nick in the user group. Thank you!)



Discussion:

Resolving patch:
diff --git a/chainer/functions/connection/embed_id.py b/chainer/functions/connection/embed_id.py
--- a/chainer/functions/connection/embed_id.py
+++ b/chainer/functions/connection/embed_id.py
@@ -39,7 +39,7 @@ def check_type_forward(self, in_types):
 
         type_check.expect(
             x_type.dtype == numpy.int32,
-            x_type.ndim == 1,
+            x_type.ndim >= 1,
         )
 
     def forward(self, x):
@@ -54,5 +54,6 @@ def backward_gpu(self, x, gy):
             'T gy, int32 x, int32 n_out', 'raw T gW',
             'int w_ind[] = {x, i % n_out}; atomicAdd(&gW[w_ind], gy)',
             'embed_id_bwd')(
-                gy[0], x[0][:, numpy.newaxis], self.gW.shape[1], self.gW)
+                gy[0], cuda.cupy.expand_dims(x[0], -1),
+                self.gW.shape[1], self.gW)
         return None,


Test patch:
diff --git a/tests/chainer_tests/functions_tests/connection_tests/test_embed_id.py b/tests/chainer_tests/functions_tests/connection_tests/test_embed_id.py
--- a/tests/chainer_tests/functions_tests/connection_tests/test_embed_id.py
+++ b/tests/chainer_tests/functions_tests/connection_tests/test_embed_id.py
@@ -1,7 +1,6 @@
 import unittest
 
 import numpy
-import six
 
 import chainer
 from chainer import cuda
@@ -12,6 +11,10 @@
 from chainer.testing import condition
 
 
+@testing.parameterize(
+    {'x_data': [0, 1, 0]},
+    {'x_data': [[0, 1, 0], [1, 0, 1]]},
+)
 class TestEmbedID(unittest.TestCase):
 
     def setUp(self):
@@ -19,8 +22,9 @@ def setUp(self):
         self.func.gW.fill(0)
 
         self.W = self.func.W.copy()  # fixed on CPU
-        self.x = numpy.array([0, 1, 0], dtype=numpy.int32)
-        self.gy = numpy.random.uniform(-1, 1, (3, 2)).astype(numpy.float32)
+        self.x = numpy.array(self.x_data, dtype=numpy.int32)
+        y_shape = self.x.shape + (2,)
+        self.gy = numpy.random.uniform(-1, 1, y_shape).astype(numpy.float32)
 
     def to_gpu(self):
         self.func.W = cuda.to_gpu(self.func.W)
@@ -32,7 +36,7 @@ def check_forward(self, x_data):
         self.assertEqual(y.data.dtype, numpy.float32)
 
         y_expect = numpy.empty_like(self.gy)
-        for i in six.moves.range(self.x.size):
+        for i in numpy.ndindex(self.x.shape):
             y_expect[i] = self.W[int(self.x[i])]
 
         gradient_check.assert_allclose(y_expect, y.data, atol=0, rtol=0)
Repository: chainer/chainer

Issue:
Cupy indexing behaviour
I mistakenly tried to perform the following indexing operation:

```
import cupy as cp
x = cp.random.randn(16, 1, 9, 9)
y = x[:, :, (2,2):(7,7), (2,2):(7,7)]
```

This results in a y with shape (16, 1, 0, 0). In numpy an "IndexError: invalid slice" is raised. Maybe the cupy indexing should check for invalid slices of this kind?



Discussion:
Thank you for reporting a bug! We'll check the specification of numpy and fix it.


Resolving patch:
diff --git a/cupy/internal.py b/cupy/internal.py
--- a/cupy/internal.py
+++ b/cupy/internal.py
@@ -106,15 +106,43 @@ def get_contiguous_strides(shape, itemsize):
 
 
 def complete_slice(slc, dim):
-    step = 1 if slc.step is None else slc.step
+    if slc.step is None:
+        step = 1
+    else:
+        try:
+            step = int(slc.step)
+        except TypeError:
+            raise IndexError(
+                'slice.step must be int or None: {}'.format(slc))
+
     if step == 0:
         raise ValueError('Slice step must be nonzero.')
-    elif step > 0:
-        start = 0 if slc.start is None else max(0, min(dim, slc.start))
-        stop = dim if slc.stop is None else max(start, min(dim, slc.stop))
+
+    if slc.start is None:
+        start = None
     else:
-        start = dim - 1 if slc.start is None else max(0, min(dim, slc.start))
-        stop = -1 if slc.stop is None else max(0, min(start, slc.stop))
+        try:
+            start = int(slc.start)
+        except TypeError:
+            raise IndexError(
+                'slice.start must be int or None: {}'.format(slc))
+
+    if slc.stop is None:
+        stop = None
+    else:
+        try:
+            stop = int(slc.stop)
+        except TypeError:
+            raise IndexError(
+                'slice.stop must be int or None: {}'.format(slc))
+
+    if step > 0:
+        start = 0 if start is None else max(0, min(dim, start))
+        stop = dim if stop is None else max(start, min(dim, stop))
+    else:
+        start = dim - 1 if start is None else max(0, min(dim, start))
+        stop = -1 if stop is None else max(0, min(start, stop))
+
     return slice(start, stop, step)
 
 


Test patch:
diff --git a/tests/cupy_tests/test_ndarray_indexing.py b/tests/cupy_tests/test_ndarray_indexing.py
--- a/tests/cupy_tests/test_ndarray_indexing.py
+++ b/tests/cupy_tests/test_ndarray_indexing.py
@@ -64,6 +64,22 @@ def test_getitem_newaxis4(self):
     def test_getitem_newaxis5(self):
         self.check_getitem((), None, (None, None))
 
+    @testing.numpy_cupy_raises()
+    def test_getitem_zero_step(self, xp):
+        xp.zeros((2, 3, 4))[0:1:0]
+
+    @testing.numpy_cupy_raises()
+    def test_getitem_invalid_slice_start(self, xp):
+        xp.zeros((2, 3, 4))[(0, 0):None:None]
+
+    @testing.numpy_cupy_raises()
+    def test_getitem_invalid_slice_stop(self, xp):
+        xp.zeros((2, 3, 4))[None:(0, 0):None]
+
+    @testing.numpy_cupy_raises()
+    def test_getitem_invalid_slice_step(self, xp):
+        xp.zeros((2, 3, 4))[None:None:(0, 0)]
+
     @testing.for_all_dtypes()
     @testing.numpy_cupy_array_equal()
     def test_setitem_constant(self, xp, dtype):
Repository: chainer/chainer

Issue:
chainer.functions.Parameter cannot accept cupy.ndarray
```
In [1]: import numpy, chainer, cupy
In [2]: p = chainer.functions.Parameter(numpy.arange(12, dtype=numpy.float32))
In [3]: p = chainer.functions.Parameter(cupy.arange(12, dtype=numpy.float32))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-3bee41ef9fca> in <module>()
----> 1 p = chainer.functions.Parameter(cupy.arange(12, dtype=numpy.float32))

/home/delta/dev/chainer2/chainer/functions/connection/parameter.py in __init__(self, array)
     21     def __init__(self, array):
     22         self.W = array
---> 23         self.gW = numpy.full_like(array, numpy.nan)
     24 
     25     def __call__(self, volatile=False):

/home/delta/.pyenv/versions/pyenv-2.7.9/lib/python2.7/site-packages/numpy/core/numeric.pyc in full_like(a, fill_value, dtype, order, subok)
    344 
    345     """
--> 346     res = empty_like(a, dtype=dtype, order=order, subok=subok)
    347     multiarray.copyto(res, fill_value, casting='unsafe')
    348     return res

ValueError: object __array__ method not producing an array
```



Discussion:

Resolving patch:
diff --git a/chainer/functions/connection/parameter.py b/chainer/functions/connection/parameter.py
--- a/chainer/functions/connection/parameter.py
+++ b/chainer/functions/connection/parameter.py
@@ -1,5 +1,6 @@
 import numpy
 
+from chainer import cuda
 from chainer import function
 from chainer.utils import type_check
 
@@ -20,7 +21,8 @@ class Parameter(function.Function):
 
     def __init__(self, array):
         self.W = array
-        self.gW = numpy.full_like(array, numpy.nan)
+        xp = cuda.get_array_module(array)
+        self.gW = xp.full_like(self.W, numpy.nan)
 
     def __call__(self, volatile=False):
         ret = super(Parameter, self).__call__()


Test patch:
diff --git a/tests/chainer_tests/functions_tests/connection_tests/test_parameter.py b/tests/chainer_tests/functions_tests/connection_tests/test_parameter.py
--- a/tests/chainer_tests/functions_tests/connection_tests/test_parameter.py
+++ b/tests/chainer_tests/functions_tests/connection_tests/test_parameter.py
@@ -103,4 +103,29 @@ def test_volatile_gpu_not_volatile(self):
         self.check_volatile(False)
 
 
+class TestInit(unittest.TestCase):
+
+    def setUp(self):
+        self.W = numpy.random.uniform(-1, 1, (2, 3)).astype(numpy.float32)
+        self.p = functions.Parameter(self.W)
+
+    def check(self, p, xp):
+        self.assertIsInstance(p.W, xp.ndarray)
+        self.assertIsInstance(p.gW, xp.ndarray)
+
+    def test_cpu(self):
+        self.check(self.p, numpy)
+
+    @attr.gpu
+    def test_gpu_initialize_by_numpy_ndarray(self):
+        self.p.to_gpu()
+        self.check(self.p, cuda.cupy)
+
+    @attr.gpu
+    def test_gpu_initialize_by_cupy_ndarray(self):
+        W = cuda.to_gpu(self.W)
+        p = functions.Parameter(W)
+        self.check(p, cuda.cupy)
+
+
 testing.run_module(__name__, __file__)