We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Lexsort output is incorrect
The output of cupy.lexsort is incorrect. I can't see any pattern as to what is wrong with the output.

### Code to reproduce
```
import cupy
import numpy

# numpy output is correct:
a = (numpy.random.random((10,2)) * 10).astype(int)  # random ints between 0 and 9
print(a[numpy.lexsort(a.T[::-1])])                  # sorted by first column, not last

# cupy output is unsorted:
b = cupy.array(a)
print(b[cupy.lexsort(b.T[::-1])])
```
The same thing happens for floats, so that's not the reason.

### Conditions
(output of `python -c 'import cupy; cupy.show_config()'`):

CuPy Version          : 7.1.1
CUDA Root             : /opt/cuda
CUDA Build Version    : 10020
CUDA Driver Version   : 10020
CUDA Runtime Version  : 10020
cuBLAS Version        : 10202
cuFFT Version         : 10102
cuRAND Version        : 10102
cuSOLVER Version      : (10, 3, 0)
cuSPARSE Version      : 10301
NVRTC Version         : (10, 2)
cuDNN Build Version   : 7605
cuDNN Version         : 7605
NCCL Build Version    : 2507
NCCL Runtime Version  : 2507

--- 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/sort.py`
Content:
```
1 import cupy
2 import numpy
3 
4 if cupy.cuda.thrust_enabled:
5     from cupy.cuda import thrust
6 
7 
8 def sort(a, axis=-1):
9     """Returns a sorted copy of an array with a stable sorting algorithm.
10 
11     Args:
12         a (cupy.ndarray): Array to be sorted.
13         axis (int or None): Axis along which to sort. Default is -1, which
14             means sort along the last axis. If None is supplied, the array is
15             flattened before sorting.
16 
17     Returns:
18         cupy.ndarray: Array of the same type and shape as ``a``.
19 
20     .. note::
21        For its implementation reason, ``cupy.sort`` currently does not support
22        ``kind`` and ``order`` parameters that ``numpy.sort`` does
23        support.
24 
25     .. seealso:: :func:`numpy.sort`
26 
27     """
28     if axis is None:
29         ret = a.flatten()
30         axis = -1
31     else:
32         ret = a.copy()
33     ret.sort(axis=axis)
34     return ret
35 
36 
37 def lexsort(keys):
38     """Perform an indirect sort using an array of keys.
39 
40     Args:
41         keys (cupy.ndarray): ``(k, N)`` array containing ``k`` ``(N,)``-shaped
42             arrays. The ``k`` different "rows" to be sorted. The last row is
43             the primary sort key.
44 
45     Returns:
46         cupy.ndarray: Array of indices that sort the keys.
47 
48     .. note::
49         For its implementation reason, ``cupy.lexsort`` currently supports only
50         keys with their rank of one or two and does not support ``axis``
51         parameter that ``numpy.lexsort`` supports.
52 
53     .. seealso:: :func:`numpy.lexsort`
54 
55     """
56 
57     # TODO(takagi): Support axis argument.
58 
59     if not cupy.cuda.thrust_enabled:
60         raise RuntimeError('Thrust is needed to use cupy.lexsort. Please '
61                            'install CUDA Toolkit with Thrust then reinstall '
62                            'CuPy after uninstalling it.')
63 
64     if keys.ndim == ():
65         # as numpy.lexsort() raises
66         raise TypeError('need sequence of keys with len > 0 in lexsort')
67 
68     if keys.ndim == 1:
69         return 0
70 
71     # TODO(takagi): Support ranks of three or more.
72     if keys.ndim > 2:
73         raise NotImplementedError('Keys with the rank of three or more is not '
74                                   'supported in lexsort')
75 
76     idx_array = cupy.ndarray(keys._shape[1:], dtype=numpy.intp)
77     k = keys._shape[0]
78     n = keys._shape[1]
79     thrust.lexsort(keys.dtype, idx_array.data.ptr, keys.data.ptr, k, n)
80 
81     return idx_array
82 
83 
84 def argsort(a, axis=-1):
85     """Returns the indices that would sort an array with a stable sorting.
86 
87     Args:
88         a (cupy.ndarray): Array to sort.
89         axis (int or None): Axis along which to sort. Default is -1, which
90             means sort along the last axis. If None is supplied, the array is
91             flattened before sorting.
92 
93     Returns:
94         cupy.ndarray: Array of indices that sort ``a``.
95 
96     .. note::
97         For its implementation reason, ``cupy.argsort`` does not support
98         ``kind`` and ``order`` parameters.
99 
100     .. seealso:: :func:`numpy.argsort`
101 
102     """
103     return a.argsort(axis=axis)
104 
105 
106 def msort(a):
107     """Returns a copy of an array sorted along the first axis.
108 
109     Args:
110         a (cupy.ndarray): Array to be sorted.
111 
112     Returns:
113         cupy.ndarray: Array of the same type and shape as ``a``.
114 
115     .. note:
116         ``cupy.msort(a)``, the CuPy counterpart of ``numpy.msort(a)``, is
117         equivalent to ``cupy.sort(a, axis=0)``.
118 
119     .. seealso:: :func:`numpy.msort`
120 
121     """
122 
123     # TODO(takagi): Support float16 and bool.
124     return sort(a, axis=0)
125 
126 
127 # TODO(okuta): Implement sort_complex
128 
129 
130 def partition(a, kth, axis=-1):
131     """Returns a partitioned copy of an array.
132 
133     Creates a copy of the array whose elements are rearranged such that the
134     value of the element in k-th position would occur in that position in a
135     sorted array. All of the elements before the new k-th element are less
136     than or equal to the elements after the new k-th element.
137 
138     Args:
139         a (cupy.ndarray): Array to be sorted.
140         kth (int or sequence of ints): Element index to partition by. If
141             supplied with a sequence of k-th it will partition all elements
142             indexed by k-th of them into their sorted position at once.
143         axis (int or None): Axis along which to sort. Default is -1, which
144             means sort along the last axis. If None is supplied, the array is
145             flattened before sorting.
146 
147     Returns:
148         cupy.ndarray: Array of the same type and shape as ``a``.
149 
150     .. seealso:: :func:`numpy.partition`
151 
152     """
153     if axis is None:
154         ret = a.flatten()
155         axis = -1
156     else:
157         ret = a.copy()
158     ret.partition(kth, axis=axis)
159     return ret
160 
161 
162 def argpartition(a, kth, axis=-1):
163     """Returns the indices that would partially sort an array.
164 
165     Args:
166         a (cupy.ndarray): Array to be sorted.
167         kth (int or sequence of ints): Element index to partition by. If
168             supplied with a sequence of k-th it will partition all elements
169             indexed by k-th of them into their sorted position at once.
170         axis (int or None): Axis along which to sort. Default is -1, which
171             means sort along the last axis. If None is supplied, the array is
172             flattened before sorting.
173 
174     Returns:
175         cupy.ndarray: Array of the same type and shape as ``a``.
176 
177     .. note::
178         For its implementation reason, `cupy.argpartition` fully sorts the
179         given array as `cupy.argsort` does. It also does not support ``kind``
180         and ``order`` parameters that ``numpy.argpartition`` supports.
181 
182     .. seealso:: :func:`numpy.argpartition`
183 
184     """
185     return a.argpartition(kth, axis=axis)
186
```
--- 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/sort.py b/cupy/_sorting/sort.py
--- a/cupy/_sorting/sort.py
+++ b/cupy/_sorting/sort.py
@@ -73,6 +73,10 @@
         raise NotImplementedError('Keys with the rank of three or more is not '
                                   'supported in lexsort')
 
+    # thrust.lexsort() assumes a C-contiguous array
+    if not keys.flags.c_contiguous:
+        keys = keys.copy('C')
+
     idx_array = cupy.ndarray(keys._shape[1:], dtype=numpy.intp)
     k = keys._shape[0]
     n = keys._shape[1]
@@ -120,7 +124,6 @@
 
     """
 
-    # TODO(takagi): Support float16 and bool.
     return sort(a, axis=0)

{"golden_diff": "diff --git a/cupy/_sorting/sort.py b/cupy/_sorting/sort.py\n--- a/cupy/_sorting/sort.py\n+++ b/cupy/_sorting/sort.py\n@@ -73,6 +73,10 @@\n         raise NotImplementedError('Keys with the rank of three or more is not '\n                                   'supported in lexsort')\n \n+    # thrust.lexsort() assumes a C-contiguous array\n+    if not keys.flags.c_contiguous:\n+        keys = keys.copy('C')\n+\n     idx_array = cupy.ndarray(keys._shape[1:], dtype=numpy.intp)\n     k = keys._shape[0]\n     n = keys._shape[1]\n@@ -120,7 +124,6 @@\n \n     \"\"\"\n \n-    # TODO(takagi): Support float16 and bool.\n     return sort(a, axis=0)\n", "issue": "Lexsort output is incorrect\nThe output of cupy.lexsort is incorrect. I can't see any pattern as to what is wrong with the output.\r\n\r\n### Code to reproduce\r\n```\r\nimport cupy\r\nimport numpy\r\n\r\n# numpy output is correct:\r\na = (numpy.random.random((10,2)) * 10).astype(int)  # random ints between 0 and 9\r\nprint(a[numpy.lexsort(a.T[::-1])])                  # sorted by first column, not last\r\n\r\n# cupy output is unsorted:\r\nb = cupy.array(a)\r\nprint(b[cupy.lexsort(b.T[::-1])])\r\n```\r\nThe same thing happens for floats, so that's not the reason.\r\n\r\n### Conditions\r\n(output of `python -c 'import cupy; cupy.show_config()'`):\r\n\r\nCuPy Version          : 7.1.1\r\nCUDA Root             : /opt/cuda\r\nCUDA Build Version    : 10020\r\nCUDA Driver Version   : 10020\r\nCUDA Runtime Version  : 10020\r\ncuBLAS Version        : 10202\r\ncuFFT Version         : 10102\r\ncuRAND Version        : 10102\r\ncuSOLVER Version      : (10, 3, 0)\r\ncuSPARSE Version      : 10301\r\nNVRTC Version         : (10, 2)\r\ncuDNN Build Version   : 7605\r\ncuDNN Version         : 7605\r\nNCCL Build Version    : 2507\r\nNCCL Runtime Version  : 2507\n", "before_files": [{"content": "import cupy\nimport numpy\n\nif cupy.cuda.thrust_enabled:\n    from cupy.cuda import thrust\n\n\ndef sort(a, axis=-1):\n    \"\"\"Returns a sorted copy of an array with a stable sorting algorithm.\n\n    Args:\n        a (cupy.ndarray): Array to be sorted.\n        axis (int or None): Axis along which to sort. Default is -1, which\n            means sort along the last axis. If None is supplied, the array is\n            flattened before sorting.\n\n    Returns:\n        cupy.ndarray: Array of the same type and shape as ``a``.\n\n    .. note::\n       For its implementation reason, ``cupy.sort`` currently does not support\n       ``kind`` and ``order`` parameters that ``numpy.sort`` does\n       support.\n\n    .. seealso:: :func:`numpy.sort`\n\n    \"\"\"\n    if axis is None:\n        ret = a.flatten()\n        axis = -1\n    else:\n        ret = a.copy()\n    ret.sort(axis=axis)\n    return ret\n\n\ndef lexsort(keys):\n    \"\"\"Perform an indirect sort using an array of keys.\n\n    Args:\n        keys (cupy.ndarray): ``(k, N)`` array containing ``k`` ``(N,)``-shaped\n            arrays. The ``k`` different \"rows\" to be sorted. The last row is\n            the primary sort key.\n\n    Returns:\n        cupy.ndarray: Array of indices that sort the keys.\n\n    .. note::\n        For its implementation reason, ``cupy.lexsort`` currently supports only\n        keys with their rank of one or two and does not support ``axis``\n        parameter that ``numpy.lexsort`` supports.\n\n    .. seealso:: :func:`numpy.lexsort`\n\n    \"\"\"\n\n    # TODO(takagi): Support axis argument.\n\n    if not cupy.cuda.thrust_enabled:\n        raise RuntimeError('Thrust is needed to use cupy.lexsort. Please '\n                           'install CUDA Toolkit with Thrust then reinstall '\n                           'CuPy after uninstalling it.')\n\n    if keys.ndim == ():\n        # as numpy.lexsort() raises\n        raise TypeError('need sequence of keys with len > 0 in lexsort')\n\n    if keys.ndim == 1:\n        return 0\n\n    # TODO(takagi): Support ranks of three or more.\n    if keys.ndim > 2:\n        raise NotImplementedError('Keys with the rank of three or more is not '\n                                  'supported in lexsort')\n\n    idx_array = cupy.ndarray(keys._shape[1:], dtype=numpy.intp)\n    k = keys._shape[0]\n    n = keys._shape[1]\n    thrust.lexsort(keys.dtype, idx_array.data.ptr, keys.data.ptr, k, n)\n\n    return idx_array\n\n\ndef argsort(a, axis=-1):\n    \"\"\"Returns the indices that would sort an array with a stable sorting.\n\n    Args:\n        a (cupy.ndarray): Array to sort.\n        axis (int or None): Axis along which to sort. Default is -1, which\n            means sort along the last axis. If None is supplied, the array is\n            flattened before sorting.\n\n    Returns:\n        cupy.ndarray: Array of indices that sort ``a``.\n\n    .. note::\n        For its implementation reason, ``cupy.argsort`` does not support\n        ``kind`` and ``order`` parameters.\n\n    .. seealso:: :func:`numpy.argsort`\n\n    \"\"\"\n    return a.argsort(axis=axis)\n\n\ndef msort(a):\n    \"\"\"Returns a copy of an array sorted along the first axis.\n\n    Args:\n        a (cupy.ndarray): Array to be sorted.\n\n    Returns:\n        cupy.ndarray: Array of the same type and shape as ``a``.\n\n    .. note:\n        ``cupy.msort(a)``, the CuPy counterpart of ``numpy.msort(a)``, is\n        equivalent to ``cupy.sort(a, axis=0)``.\n\n    .. seealso:: :func:`numpy.msort`\n\n    \"\"\"\n\n    # TODO(takagi): Support float16 and bool.\n    return sort(a, axis=0)\n\n\n# TODO(okuta): Implement sort_complex\n\n\ndef partition(a, kth, axis=-1):\n    \"\"\"Returns a partitioned copy of an array.\n\n    Creates a copy of the array whose elements are rearranged such that the\n    value of the element in k-th position would occur in that position in a\n    sorted array. All of the elements before the new k-th element are less\n    than or equal to the elements after the new k-th element.\n\n    Args:\n        a (cupy.ndarray): Array to be sorted.\n        kth (int or sequence of ints): Element index to partition by. If\n            supplied with a sequence of k-th it will partition all elements\n            indexed by k-th of them into their sorted position at once.\n        axis (int or None): Axis along which to sort. Default is -1, which\n            means sort along the last axis. If None is supplied, the array is\n            flattened before sorting.\n\n    Returns:\n        cupy.ndarray: Array of the same type and shape as ``a``.\n\n    .. seealso:: :func:`numpy.partition`\n\n    \"\"\"\n    if axis is None:\n        ret = a.flatten()\n        axis = -1\n    else:\n        ret = a.copy()\n    ret.partition(kth, axis=axis)\n    return ret\n\n\ndef argpartition(a, kth, axis=-1):\n    \"\"\"Returns the indices that would partially sort an array.\n\n    Args:\n        a (cupy.ndarray): Array to be sorted.\n        kth (int or sequence of ints): Element index to partition by. If\n            supplied with a sequence of k-th it will partition all elements\n            indexed by k-th of them into their sorted position at once.\n        axis (int or None): Axis along which to sort. Default is -1, which\n            means sort along the last axis. If None is supplied, the array is\n            flattened before sorting.\n\n    Returns:\n        cupy.ndarray: Array of the same type and shape as ``a``.\n\n    .. note::\n        For its implementation reason, `cupy.argpartition` fully sorts the\n        given array as `cupy.argsort` does. It also does not support ``kind``\n        and ``order`` parameters that ``numpy.argpartition`` supports.\n\n    .. seealso:: :func:`numpy.argpartition`\n\n    \"\"\"\n    return a.argpartition(kth, axis=axis)\n", "path": "cupy/_sorting/sort.py"}], "after_files": [{"content": "import cupy\nimport numpy\n\nif cupy.cuda.thrust_enabled:\n    from cupy.cuda import thrust\n\n\ndef sort(a, axis=-1):\n    \"\"\"Returns a sorted copy of an array with a stable sorting algorithm.\n\n    Args:\n        a (cupy.ndarray): Array to be sorted.\n        axis (int or None): Axis along which to sort. Default is -1, which\n            means sort along the last axis. If None is supplied, the array is\n            flattened before sorting.\n\n    Returns:\n        cupy.ndarray: Array of the same type and shape as ``a``.\n\n    .. note::\n       For its implementation reason, ``cupy.sort`` currently does not support\n       ``kind`` and ``order`` parameters that ``numpy.sort`` does\n       support.\n\n    .. seealso:: :func:`numpy.sort`\n\n    \"\"\"\n    if axis is None:\n        ret = a.flatten()\n        axis = -1\n    else:\n        ret = a.copy()\n    ret.sort(axis=axis)\n    return ret\n\n\ndef lexsort(keys):\n    \"\"\"Perform an indirect sort using an array of keys.\n\n    Args:\n        keys (cupy.ndarray): ``(k, N)`` array containing ``k`` ``(N,)``-shaped\n            arrays. The ``k`` different \"rows\" to be sorted. The last row is\n            the primary sort key.\n\n    Returns:\n        cupy.ndarray: Array of indices that sort the keys.\n\n    .. note::\n        For its implementation reason, ``cupy.lexsort`` currently supports only\n        keys with their rank of one or two and does not support ``axis``\n        parameter that ``numpy.lexsort`` supports.\n\n    .. seealso:: :func:`numpy.lexsort`\n\n    \"\"\"\n\n    # TODO(takagi): Support axis argument.\n\n    if not cupy.cuda.thrust_enabled:\n        raise RuntimeError('Thrust is needed to use cupy.lexsort. Please '\n                           'install CUDA Toolkit with Thrust then reinstall '\n                           'CuPy after uninstalling it.')\n\n    if keys.ndim == ():\n        # as numpy.lexsort() raises\n        raise TypeError('need sequence of keys with len > 0 in lexsort')\n\n    if keys.ndim == 1:\n        return 0\n\n    # TODO(takagi): Support ranks of three or more.\n    if keys.ndim > 2:\n        raise NotImplementedError('Keys with the rank of three or more is not '\n                                  'supported in lexsort')\n\n    # thrust.lexsort() assumes a C-contiguous array\n    if not keys.flags.c_contiguous:\n        keys = keys.copy('C')\n\n    idx_array = cupy.ndarray(keys._shape[1:], dtype=numpy.intp)\n    k = keys._shape[0]\n    n = keys._shape[1]\n    thrust.lexsort(keys.dtype, idx_array.data.ptr, keys.data.ptr, k, n)\n\n    return idx_array\n\n\ndef argsort(a, axis=-1):\n    \"\"\"Returns the indices that would sort an array with a stable sorting.\n\n    Args:\n        a (cupy.ndarray): Array to sort.\n        axis (int or None): Axis along which to sort. Default is -1, which\n            means sort along the last axis. If None is supplied, the array is\n            flattened before sorting.\n\n    Returns:\n        cupy.ndarray: Array of indices that sort ``a``.\n\n    .. note::\n        For its implementation reason, ``cupy.argsort`` does not support\n        ``kind`` and ``order`` parameters.\n\n    .. seealso:: :func:`numpy.argsort`\n\n    \"\"\"\n    return a.argsort(axis=axis)\n\n\ndef msort(a):\n    \"\"\"Returns a copy of an array sorted along the first axis.\n\n    Args:\n        a (cupy.ndarray): Array to be sorted.\n\n    Returns:\n        cupy.ndarray: Array of the same type and shape as ``a``.\n\n    .. note:\n        ``cupy.msort(a)``, the CuPy counterpart of ``numpy.msort(a)``, is\n        equivalent to ``cupy.sort(a, axis=0)``.\n\n    .. seealso:: :func:`numpy.msort`\n\n    \"\"\"\n\n    return sort(a, axis=0)\n\n\n# TODO(okuta): Implement sort_complex\n\n\ndef partition(a, kth, axis=-1):\n    \"\"\"Returns a partitioned copy of an array.\n\n    Creates a copy of the array whose elements are rearranged such that the\n    value of the element in k-th position would occur in that position in a\n    sorted array. All of the elements before the new k-th element are less\n    than or equal to the elements after the new k-th element.\n\n    Args:\n        a (cupy.ndarray): Array to be sorted.\n        kth (int or sequence of ints): Element index to partition by. If\n            supplied with a sequence of k-th it will partition all elements\n            indexed by k-th of them into their sorted position at once.\n        axis (int or None): Axis along which to sort. Default is -1, which\n            means sort along the last axis. If None is supplied, the array is\n            flattened before sorting.\n\n    Returns:\n        cupy.ndarray: Array of the same type and shape as ``a``.\n\n    .. seealso:: :func:`numpy.partition`\n\n    \"\"\"\n    if axis is None:\n        ret = a.flatten()\n        axis = -1\n    else:\n        ret = a.copy()\n    ret.partition(kth, axis=axis)\n    return ret\n\n\ndef argpartition(a, kth, axis=-1):\n    \"\"\"Returns the indices that would partially sort an array.\n\n    Args:\n        a (cupy.ndarray): Array to be sorted.\n        kth (int or sequence of ints): Element index to partition by. If\n            supplied with a sequence of k-th it will partition all elements\n            indexed by k-th of them into their sorted position at once.\n        axis (int or None): Axis along which to sort. Default is -1, which\n            means sort along the last axis. If None is supplied, the array is\n            flattened before sorting.\n\n    Returns:\n        cupy.ndarray: Array of the same type and shape as ``a``.\n\n    .. note::\n        For its implementation reason, `cupy.argpartition` fully sorts the\n        given array as `cupy.argsort` does. It also does not support ``kind``\n        and ``order`` parameters that ``numpy.argpartition`` supports.\n\n    .. seealso:: :func:`numpy.argpartition`\n\n    \"\"\"\n    return a.argpartition(kth, axis=axis)\n", "path": "cupy/_sorting/sort.py"}]}