Repository: cupy/cupy

Issue:
cupy.nonzero fails with some corner-case inputs
Code:
```
import sys
import cupy, numpy

shapes = [
    (),
    (0,),
    (1,),
    (0,2),
    (0,0,2,0),
]

f = sys.stdout
for xp in (numpy, cupy):
    print(xp.__name__)
    for shape in shapes:
        a = xp.ones(shape)
        f.write('shape={:<15} => '.format(str(shape)))

        try:
            b = xp.nonzero(a)
            f.write('{}\n'.format(b))
        except Exception as e:
            f.write('FAIL: {}\n'.format(e))

# get stack trace
cupy.nonzero(cupy.ones((0,)))
```

Result:
```
numpy
shape=()              => (array([0]),)
shape=(0,)            => (array([], dtype=int64),)
shape=(1,)            => (array([0]),)
shape=(0, 2)          => (array([], dtype=int64), array([], dtype=int64))
shape=(0, 0, 2, 0)    => (array([], dtype=int64), array([], dtype=int64), array([], dtype=int64), array([], dtype=int64))
cupy
shape=()              => (array([0]),)
shape=(0,)            => FAIL: CUDA_ERROR_INVALID_VALUE: invalid argument
shape=(1,)            => (array([0]),)
shape=(0, 2)          => FAIL: CUDA_ERROR_INVALID_VALUE: invalid argument
shape=(0, 0, 2, 0)    => FAIL: CUDA_ERROR_INVALID_VALUE: invalid argument
Traceback (most recent call last):
  File "test-nonzero.py", line 26, in <module>
    cupy.nonzero(cupy.ones((0,)))
  File "/niboshi/repos/cupy/cupy/sorting/search.py", line 72, in nonzero
    return a.nonzero()
  File "cupy/core/core.pyx", line 810, in cupy.core.core.ndarray.nonzero (cupy/core/core.cpp:16210)
    scan_index = scan(condition.astype(dtype).ravel())
  File "cupy/core/core.pyx", line 3883, in cupy.core.core.scan (cupy/core/core.cpp:83826)
    kern_scan(grid=((a.size - 1) // (2 * block_size) + 1,),
  File "cupy/cuda/function.pyx", line 118, in cupy.cuda.function.Function.__call__ (cupy/cuda/function.cpp:3794)
    _launch(
  File "cupy/cuda/function.pyx", line 100, in cupy.cuda.function._launch (cupy/cuda/function.cpp:3431)
    driver.launchKernel(
  File "cupy/cuda/driver.pyx", line 170, in cupy.cuda.driver.launchKernel (cupy/cuda/driver.cpp:3262)
    check_status(status)
  File "cupy/cuda/driver.pyx", line 70, in cupy.cuda.driver.check_status (cupy/cuda/driver.cpp:1481)
    raise CUDADriverError(status)
cupy.cuda.driver.CUDADriverError: CUDA_ERROR_INVALID_VALUE: invalid argument
```
CuPy version: latest master(v2.0.0a1)


Discussion:

Resolving patch:
diff --git a/cupy/sorting/search.py b/cupy/sorting/search.py
--- a/cupy/sorting/search.py
+++ b/cupy/sorting/search.py
@@ -69,6 +69,7 @@ def nonzero(a):
     .. seealso:: :func:`numpy.nonzero`
 
     """
+    assert isinstance(a, core.ndarray)
     return a.nonzero()
 
 
@@ -86,6 +87,7 @@ def flatnonzero(a):
 
     .. seealso:: :func:`numpy.flatnonzero`
     """
+    assert isinstance(a, core.ndarray)
     return a.ravel().nonzero()[0]
 
 


Test patch:
diff --git a/tests/cupy_tests/sorting_tests/test_search.py b/tests/cupy_tests/sorting_tests/test_search.py
--- a/tests/cupy_tests/sorting_tests/test_search.py
+++ b/tests/cupy_tests/sorting_tests/test_search.py
@@ -161,6 +161,8 @@ def test_one_argument(self, xp):
     {"array": numpy.random.randn(3, 2, 4)},
     {"array": numpy.array(0)},
     {"array": numpy.array(1)},
+    {"array": numpy.zeros((0, 2))},
+    {"array": numpy.zeros((0, 2, 0))},
 )
 @testing.gpu
 class TestNonzero(unittest.TestCase):
@@ -177,6 +179,8 @@ def test_nonzero(self, xp, dtype):
     {"array": numpy.random.randn(3, 2, 4)},
     {"array": numpy.array(0)},
     {"array": numpy.array(1)},
+    {"array": numpy.zeros((0, 2))},
+    {"array": numpy.zeros((0, 2, 0))},
 )
 @testing.gpu
 class TestFlatNonzero(unittest.TestCase):