We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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)

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `cupy/sorting/search.py`
Content:
```
1 from cupy import core
2 
3 
4 def argmax(a, axis=None, dtype=None, out=None, keepdims=False):
5     """Returns the indices of the maximum along an axis.
6 
7     Args:
8         a (cupy.ndarray): Array to take argmax.
9         axis (int): Along which axis to find the maximum. ``a`` is flattened by
10             default.
11         dtype: Data type specifier.
12         out (cupy.ndarray): Output array.
13         keepdims (bool): If ``True``, the axis ``axis`` is preserved as an axis
14             of length one.
15 
16     Returns:
17         cupy.ndarray: The indices of the maximum of ``a`` along an axis.
18 
19     .. seealso:: :func:`numpy.argmax`
20 
21     """
22     # TODO(okuta): check type
23     return a.argmax(axis=axis, dtype=dtype, out=out, keepdims=keepdims)
24 
25 
26 # TODO(okuta): Implement nanargmax
27 
28 
29 def argmin(a, axis=None, dtype=None, out=None, keepdims=False):
30     """Returns the indices of the minimum along an axis.
31 
32     Args:
33         a (cupy.ndarray): Array to take argmin.
34         axis (int): Along which axis to find the minimum. ``a`` is flattened by
35             default.
36         dtype: Data type specifier.
37         out (cupy.ndarray): Output array.
38         keepdims (bool): If ``True``, the axis ``axis`` is preserved as an axis
39             of length one.
40 
41     Returns:
42         cupy.ndarray: The indices of the minimum of ``a`` along an axis.
43 
44     .. seealso:: :func:`numpy.argmin`
45 
46     """
47     # TODO(okuta): check type
48     return a.argmin(axis=axis, dtype=dtype, out=out, keepdims=keepdims)
49 
50 
51 # TODO(okuta): Implement nanargmin
52 
53 
54 # TODO(okuta): Implement argwhere
55 
56 
57 def nonzero(a):
58     """Return the indices of the elements that are non-zero.
59 
60     Returns a tuple of arrays, one for each dimension of a,
61     containing the indices of the non-zero elements in that dimension.
62 
63     Args:
64         a (cupy.ndarray): array
65 
66     Returns:
67         tuple of arrays: Indices of elements that are non-zero.
68 
69     .. seealso:: :func:`numpy.nonzero`
70 
71     """
72     return a.nonzero()
73 
74 
75 def flatnonzero(a):
76     """Return indices that are non-zero in the flattened version of a.
77 
78     This is equivalent to a.ravel().nonzero()[0].
79 
80     Args:
81         a (cupy.ndarray): input array
82 
83     Returns:
84         cupy.ndarray: Output array,
85         containing the indices of the elements of a.ravel() that are non-zero.
86 
87     .. seealso:: :func:`numpy.flatnonzero`
88     """
89     return a.ravel().nonzero()[0]
90 
91 
92 def where(condition, x=None, y=None):
93     """Return elements, either from x or y, depending on condition.
94 
95     If only condition is given, return ``condition.nonzero()``.
96 
97     Args:
98         condition (cupy.ndarray): When True, take x, otherwise take y.
99         x (cupy.ndarray): Values from which to choose on ``True``.
100         y (cupy.ndarray): Values from which to choose on ``False``.
101 
102     Returns:
103         cupy.ndarray: Each element of output contains elements of ``x`` when
104             ``condition`` is ``True``, otherwise elements of ``y``. If only
105             ``condition`` is given, return the tuple ``condition.nonzero()``,
106             the indices where ``condition`` is True.
107 
108     .. seealso:: :func:`numpy.where`
109 
110     """
111 
112     missing = (x is None, y is None).count(True)
113 
114     if missing == 1:
115         raise ValueError("Must provide both 'x' and 'y' or neither.")
116     if missing == 2:
117         return nonzero(condition)
118 
119     return _where_ufunc(condition.astype('?'), x, y)
120 
121 
122 _where_ufunc = core.create_ufunc(
123     'cupy_where',
124     ('???->?', '?bb->b', '?BB->B', '?hh->h', '?HH->H', '?ii->i', '?II->I',
125      '?ll->l', '?LL->L', '?qq->q', '?QQ->Q', '?ee->e', '?ff->f',
126      # On CUDA 6.5 these combinations don't work correctly (on CUDA >=7.0, it
127      # works).
128      # See issue #551.
129      '?hd->d', '?Hd->d',
130      '?dd->d'),
131     'out0 = in0 ? in1 : in2')
132 
133 
134 # TODO(okuta): Implement searchsorted
135 
136 
137 # TODO(okuta): Implement extract
138
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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 @@
     .. seealso:: :func:`numpy.nonzero`
 
     """
+    assert isinstance(a, core.ndarray)
     return a.nonzero()
 
 
@@ -86,6 +87,7 @@
 
     .. seealso:: :func:`numpy.flatnonzero`
     """
+    assert isinstance(a, core.ndarray)
     return a.ravel().nonzero()[0]

{"golden_diff": "diff --git a/cupy/sorting/search.py b/cupy/sorting/search.py\n--- a/cupy/sorting/search.py\n+++ b/cupy/sorting/search.py\n@@ -69,6 +69,7 @@\n     .. seealso:: :func:`numpy.nonzero`\n \n     \"\"\"\n+    assert isinstance(a, core.ndarray)\n     return a.nonzero()\n \n \n@@ -86,6 +87,7 @@\n \n     .. seealso:: :func:`numpy.flatnonzero`\n     \"\"\"\n+    assert isinstance(a, core.ndarray)\n     return a.ravel().nonzero()[0]\n", "issue": "cupy.nonzero fails with some corner-case inputs\nCode:\r\n```\r\nimport sys\r\nimport cupy, numpy\r\n\r\nshapes = [\r\n    (),\r\n    (0,),\r\n    (1,),\r\n    (0,2),\r\n    (0,0,2,0),\r\n]\r\n\r\nf = sys.stdout\r\nfor xp in (numpy, cupy):\r\n    print(xp.__name__)\r\n    for shape in shapes:\r\n        a = xp.ones(shape)\r\n        f.write('shape={:<15} => '.format(str(shape)))\r\n\r\n        try:\r\n            b = xp.nonzero(a)\r\n            f.write('{}\\n'.format(b))\r\n        except Exception as e:\r\n            f.write('FAIL: {}\\n'.format(e))\r\n\r\n# get stack trace\r\ncupy.nonzero(cupy.ones((0,)))\r\n```\r\n\r\nResult:\r\n```\r\nnumpy\r\nshape=()              => (array([0]),)\r\nshape=(0,)            => (array([], dtype=int64),)\r\nshape=(1,)            => (array([0]),)\r\nshape=(0, 2)          => (array([], dtype=int64), array([], dtype=int64))\r\nshape=(0, 0, 2, 0)    => (array([], dtype=int64), array([], dtype=int64), array([], dtype=int64), array([], dtype=int64))\r\ncupy\r\nshape=()              => (array([0]),)\r\nshape=(0,)            => FAIL: CUDA_ERROR_INVALID_VALUE: invalid argument\r\nshape=(1,)            => (array([0]),)\r\nshape=(0, 2)          => FAIL: CUDA_ERROR_INVALID_VALUE: invalid argument\r\nshape=(0, 0, 2, 0)    => FAIL: CUDA_ERROR_INVALID_VALUE: invalid argument\r\nTraceback (most recent call last):\r\n  File \"test-nonzero.py\", line 26, in <module>\r\n    cupy.nonzero(cupy.ones((0,)))\r\n  File \"/niboshi/repos/cupy/cupy/sorting/search.py\", line 72, in nonzero\r\n    return a.nonzero()\r\n  File \"cupy/core/core.pyx\", line 810, in cupy.core.core.ndarray.nonzero (cupy/core/core.cpp:16210)\r\n    scan_index = scan(condition.astype(dtype).ravel())\r\n  File \"cupy/core/core.pyx\", line 3883, in cupy.core.core.scan (cupy/core/core.cpp:83826)\r\n    kern_scan(grid=((a.size - 1) // (2 * block_size) + 1,),\r\n  File \"cupy/cuda/function.pyx\", line 118, in cupy.cuda.function.Function.__call__ (cupy/cuda/function.cpp:3794)\r\n    _launch(\r\n  File \"cupy/cuda/function.pyx\", line 100, in cupy.cuda.function._launch (cupy/cuda/function.cpp:3431)\r\n    driver.launchKernel(\r\n  File \"cupy/cuda/driver.pyx\", line 170, in cupy.cuda.driver.launchKernel (cupy/cuda/driver.cpp:3262)\r\n    check_status(status)\r\n  File \"cupy/cuda/driver.pyx\", line 70, in cupy.cuda.driver.check_status (cupy/cuda/driver.cpp:1481)\r\n    raise CUDADriverError(status)\r\ncupy.cuda.driver.CUDADriverError: CUDA_ERROR_INVALID_VALUE: invalid argument\r\n```\r\nCuPy version: latest master(v2.0.0a1)\n", "before_files": [{"content": "from cupy import core\n\n\ndef argmax(a, axis=None, dtype=None, out=None, keepdims=False):\n    \"\"\"Returns the indices of the maximum along an axis.\n\n    Args:\n        a (cupy.ndarray): Array to take argmax.\n        axis (int): Along which axis to find the maximum. ``a`` is flattened by\n            default.\n        dtype: Data type specifier.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis ``axis`` is preserved as an axis\n            of length one.\n\n    Returns:\n        cupy.ndarray: The indices of the maximum of ``a`` along an axis.\n\n    .. seealso:: :func:`numpy.argmax`\n\n    \"\"\"\n    # TODO(okuta): check type\n    return a.argmax(axis=axis, dtype=dtype, out=out, keepdims=keepdims)\n\n\n# TODO(okuta): Implement nanargmax\n\n\ndef argmin(a, axis=None, dtype=None, out=None, keepdims=False):\n    \"\"\"Returns the indices of the minimum along an axis.\n\n    Args:\n        a (cupy.ndarray): Array to take argmin.\n        axis (int): Along which axis to find the minimum. ``a`` is flattened by\n            default.\n        dtype: Data type specifier.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis ``axis`` is preserved as an axis\n            of length one.\n\n    Returns:\n        cupy.ndarray: The indices of the minimum of ``a`` along an axis.\n\n    .. seealso:: :func:`numpy.argmin`\n\n    \"\"\"\n    # TODO(okuta): check type\n    return a.argmin(axis=axis, dtype=dtype, out=out, keepdims=keepdims)\n\n\n# TODO(okuta): Implement nanargmin\n\n\n# TODO(okuta): Implement argwhere\n\n\ndef nonzero(a):\n    \"\"\"Return the indices of the elements that are non-zero.\n\n    Returns a tuple of arrays, one for each dimension of a,\n    containing the indices of the non-zero elements in that dimension.\n\n    Args:\n        a (cupy.ndarray): array\n\n    Returns:\n        tuple of arrays: Indices of elements that are non-zero.\n\n    .. seealso:: :func:`numpy.nonzero`\n\n    \"\"\"\n    return a.nonzero()\n\n\ndef flatnonzero(a):\n    \"\"\"Return indices that are non-zero in the flattened version of a.\n\n    This is equivalent to a.ravel().nonzero()[0].\n\n    Args:\n        a (cupy.ndarray): input array\n\n    Returns:\n        cupy.ndarray: Output array,\n        containing the indices of the elements of a.ravel() that are non-zero.\n\n    .. seealso:: :func:`numpy.flatnonzero`\n    \"\"\"\n    return a.ravel().nonzero()[0]\n\n\ndef where(condition, x=None, y=None):\n    \"\"\"Return elements, either from x or y, depending on condition.\n\n    If only condition is given, return ``condition.nonzero()``.\n\n    Args:\n        condition (cupy.ndarray): When True, take x, otherwise take y.\n        x (cupy.ndarray): Values from which to choose on ``True``.\n        y (cupy.ndarray): Values from which to choose on ``False``.\n\n    Returns:\n        cupy.ndarray: Each element of output contains elements of ``x`` when\n            ``condition`` is ``True``, otherwise elements of ``y``. If only\n            ``condition`` is given, return the tuple ``condition.nonzero()``,\n            the indices where ``condition`` is True.\n\n    .. seealso:: :func:`numpy.where`\n\n    \"\"\"\n\n    missing = (x is None, y is None).count(True)\n\n    if missing == 1:\n        raise ValueError(\"Must provide both 'x' and 'y' or neither.\")\n    if missing == 2:\n        return nonzero(condition)\n\n    return _where_ufunc(condition.astype('?'), x, y)\n\n\n_where_ufunc = core.create_ufunc(\n    'cupy_where',\n    ('???->?', '?bb->b', '?BB->B', '?hh->h', '?HH->H', '?ii->i', '?II->I',\n     '?ll->l', '?LL->L', '?qq->q', '?QQ->Q', '?ee->e', '?ff->f',\n     # On CUDA 6.5 these combinations don't work correctly (on CUDA >=7.0, it\n     # works).\n     # See issue #551.\n     '?hd->d', '?Hd->d',\n     '?dd->d'),\n    'out0 = in0 ? in1 : in2')\n\n\n# TODO(okuta): Implement searchsorted\n\n\n# TODO(okuta): Implement extract\n", "path": "cupy/sorting/search.py"}], "after_files": [{"content": "from cupy import core\n\n\ndef argmax(a, axis=None, dtype=None, out=None, keepdims=False):\n    \"\"\"Returns the indices of the maximum along an axis.\n\n    Args:\n        a (cupy.ndarray): Array to take argmax.\n        axis (int): Along which axis to find the maximum. ``a`` is flattened by\n            default.\n        dtype: Data type specifier.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis ``axis`` is preserved as an axis\n            of length one.\n\n    Returns:\n        cupy.ndarray: The indices of the maximum of ``a`` along an axis.\n\n    .. seealso:: :func:`numpy.argmax`\n\n    \"\"\"\n    # TODO(okuta): check type\n    return a.argmax(axis=axis, dtype=dtype, out=out, keepdims=keepdims)\n\n\n# TODO(okuta): Implement nanargmax\n\n\ndef argmin(a, axis=None, dtype=None, out=None, keepdims=False):\n    \"\"\"Returns the indices of the minimum along an axis.\n\n    Args:\n        a (cupy.ndarray): Array to take argmin.\n        axis (int): Along which axis to find the minimum. ``a`` is flattened by\n            default.\n        dtype: Data type specifier.\n        out (cupy.ndarray): Output array.\n        keepdims (bool): If ``True``, the axis ``axis`` is preserved as an axis\n            of length one.\n\n    Returns:\n        cupy.ndarray: The indices of the minimum of ``a`` along an axis.\n\n    .. seealso:: :func:`numpy.argmin`\n\n    \"\"\"\n    # TODO(okuta): check type\n    return a.argmin(axis=axis, dtype=dtype, out=out, keepdims=keepdims)\n\n\n# TODO(okuta): Implement nanargmin\n\n\n# TODO(okuta): Implement argwhere\n\n\ndef nonzero(a):\n    \"\"\"Return the indices of the elements that are non-zero.\n\n    Returns a tuple of arrays, one for each dimension of a,\n    containing the indices of the non-zero elements in that dimension.\n\n    Args:\n        a (cupy.ndarray): array\n\n    Returns:\n        tuple of arrays: Indices of elements that are non-zero.\n\n    .. seealso:: :func:`numpy.nonzero`\n\n    \"\"\"\n    assert isinstance(a, core.ndarray)\n    return a.nonzero()\n\n\ndef flatnonzero(a):\n    \"\"\"Return indices that are non-zero in the flattened version of a.\n\n    This is equivalent to a.ravel().nonzero()[0].\n\n    Args:\n        a (cupy.ndarray): input array\n\n    Returns:\n        cupy.ndarray: Output array,\n        containing the indices of the elements of a.ravel() that are non-zero.\n\n    .. seealso:: :func:`numpy.flatnonzero`\n    \"\"\"\n    assert isinstance(a, core.ndarray)\n    return a.ravel().nonzero()[0]\n\n\ndef where(condition, x=None, y=None):\n    \"\"\"Return elements, either from x or y, depending on condition.\n\n    If only condition is given, return ``condition.nonzero()``.\n\n    Args:\n        condition (cupy.ndarray): When True, take x, otherwise take y.\n        x (cupy.ndarray): Values from which to choose on ``True``.\n        y (cupy.ndarray): Values from which to choose on ``False``.\n\n    Returns:\n        cupy.ndarray: Each element of output contains elements of ``x`` when\n            ``condition`` is ``True``, otherwise elements of ``y``. If only\n            ``condition`` is given, return the tuple ``condition.nonzero()``,\n            the indices where ``condition`` is True.\n\n    .. seealso:: :func:`numpy.where`\n\n    \"\"\"\n\n    missing = (x is None, y is None).count(True)\n\n    if missing == 1:\n        raise ValueError(\"Must provide both 'x' and 'y' or neither.\")\n    if missing == 2:\n        return nonzero(condition)\n\n    return _where_ufunc(condition.astype('?'), x, y)\n\n\n_where_ufunc = core.create_ufunc(\n    'cupy_where',\n    ('???->?', '?bb->b', '?BB->B', '?hh->h', '?HH->H', '?ii->i', '?II->I',\n     '?ll->l', '?LL->L', '?qq->q', '?QQ->Q', '?ee->e', '?ff->f',\n     # On CUDA 6.5 these combinations don't work correctly (on CUDA >=7.0, it\n     # works).\n     # See issue #551.\n     '?hd->d', '?Hd->d',\n     '?dd->d'),\n    'out0 = in0 ? in1 : in2')\n\n\n# TODO(okuta): Implement searchsorted\n\n\n# TODO(okuta): Implement extract\n", "path": "cupy/sorting/search.py"}]}