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

Issue:
bug for 4.4.0 release. Can not working on multi threading.
This [merge](https://github.com/cupy/cupy/pull/1350/files) has bug when working on multi threading. 

![image](https://user-images.githubusercontent.com/6102702/44715413-55010700-aaea-11e8-8569-f6311ea7a632.png)

Appear this issue after upgrade to 4.4.0. I had to rollback my package to 4.3.0 now. 


Discussion:

Resolving patch:
diff --git a/cupy/core/fusion.py b/cupy/core/fusion.py
--- a/cupy/core/fusion.py
+++ b/cupy/core/fusion.py
@@ -16,7 +16,6 @@
 
 
 _thread_local = threading.local()
-_thread_local.history = None
 
 _kind_score = {
     'b': 0,
@@ -753,7 +752,7 @@ def __repr__(self):
         return '<Fusion \'{}\'>'.format(self.name)
 
     def __call__(self, *args, **kwargs):
-        if _thread_local.history is None:
+        if not hasattr(_thread_local, 'history'):
             func, kw = self._compile(*args, **kwargs)
             kwargs = dict(kwargs, **kw)
             return func(*args, **kwargs)
@@ -761,7 +760,7 @@ def __call__(self, *args, **kwargs):
             return self.func(*args, **kwargs)
 
     def _compile_from_dtypes(self, *dtypes):
-        assert _thread_local.history is None
+        assert not hasattr(_thread_local, 'history')
         _thread_local.history = _FusionHistory()
         try:
             key = tuple(dtypes)
@@ -770,7 +769,7 @@ def _compile_from_dtypes(self, *dtypes):
                     self.func, dtypes, self.name)
             return self._memo[key]
         finally:
-            _thread_local.history = None
+            del _thread_local.history
 
     def _compile(self, *args, **kwargs):
         if builtins.any(
@@ -853,7 +852,7 @@ def __repr__(self):
         return repr(self._cupy_op)
 
     def __call__(self, *args, **kwargs):
-        if _thread_local.history is None:
+        if not hasattr(_thread_local, 'history'):
             return self._cupy_op(*args, **kwargs)
         else:
             return _thread_local.history.call_ufunc(
@@ -983,7 +982,7 @@ def __init__(self, fusion_op, cupy_op):
         self.__doc__ = cupy_op.__doc__
 
     def __call__(self, *args, **kwargs):
-        if _thread_local.history is None:
+        if not hasattr(_thread_local, 'history'):
             return self._cupy_op(*args, **kwargs)
 
         fusion_op = self._fusion_op


Test patch:
Repository: cupy/cupy

Issue:
Some cupy.random distribution does not support `size=None`
`size=None` is different from `size=()`.
```
>>> np.random.uniform([2], [[3]])
array([[2.63150331]])
>>> np.random.uniform([2], [[3]], size=())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mtrand.pyx", line 1317, in mtrand.RandomState.uniform
  File "mtrand.pyx", line 267, in mtrand.cont2_array
ValueError: size is not compatible with inputs
>>> 
>>> cupy.random.uniform([2], [[3]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/kataoka/cupy/cupy/random/distributions.py", line 454, in uniform
    return rs.uniform(low, high, size=size, dtype=dtype)
  File "/home/kataoka/cupy/cupy/random/generator.py", line 484, in uniform
    return RandomState._scale_kernel(low, high, rand)
  File "cupy/core/_kernel.pyx", line 532, in cupy.core._kernel.ElementwiseKernel.__call__
    out_args = _get_out_args_with_params(
  File "cupy/core/_kernel.pyx", line 368, in cupy.core._kernel._get_out_args_with_params
    raise ValueError('Out shape is mismatched')
ValueError: Out shape is mismatched
```


Discussion:

Resolving patch:
diff --git a/cupy/random/generator.py b/cupy/random/generator.py
--- a/cupy/random/generator.py
+++ b/cupy/random/generator.py
@@ -183,7 +183,7 @@ def geometric(self, p, size=None, dtype=int):
 
     _laplace_kernel = core.ElementwiseKernel(
         'T x, T loc, T scale', 'T y',
-        'y = T(loc) + T(scale) * ((x < 0.5) ? log(x + x): -log(2.0 - x - x))',
+        'y = loc + scale * ((x <= 0.5) ? log(x + x): -log(x + x - 1.0))',
         'laplace_kernel')
 
     def laplace(self, loc=0.0, scale=1.0, size=None, dtype=float):
@@ -193,11 +193,11 @@ def laplace(self, loc=0.0, scale=1.0, size=None, dtype=float):
             :func:`cupy.random.laplace` for full documentation,
             :meth:`numpy.random.RandomState.laplace`
         """
-        x = self.random_sample(size=size, dtype=dtype)
-        if not numpy.isscalar(loc):
-            loc = cupy.asarray(loc, dtype)
-        if not numpy.isscalar(scale):
-            scale = cupy.asarray(scale, dtype)
+        loc = cupy.asarray(loc, dtype)
+        scale = cupy.asarray(scale, dtype)
+        if size is None:
+            size = cupy.broadcast(loc, scale).shape
+        x = self._random_sample_raw(size, dtype)
         RandomState._laplace_kernel(x, loc, scale, x)
         return x
 


Test patch:
Repository: cupy/cupy

Issue:
`cupy.random.rand` may return 1 for float32
While the document says the output is on "the interval ``[0, 1)``", the function may return 1.
```
>>> cupy.random.rand(10**8, dtype=cupy.float32).max()
array(1., dtype=float32)
```

This is because #bits of significand of float32 is smaller than #bits of RNG output.


Discussion:
Though not checked in detail, the following patch worked.

```patch
diff --git a/cupy/random/generator.py b/cupy/random/generator.py
index cfdf79059..e5e9db452 100644
--- a/cupy/random/generator.py
+++ b/cupy/random/generator.py
@@ -491,8 +491,9 @@ class RandomState(object):
                             % ', '.join(kwarg.keys()))
         return self.normal(size=size, dtype=dtype)
 
-    _1m_kernel = core.ElementwiseKernel(
-        '', 'T x', 'x = 1 - x', 'cupy_random_1_minus_x')
+    _deceps_max0_kernel = core.ElementwiseKernel(
+        'T eps', 'T x', 'x = max(x - eps, (T)0)',
+        'cupy_random_x_decrement_eps_max_0')
 
     def _random_sample_raw(self, size, dtype):
         dtype = _check_and_get_dtype(dtype)
@@ -513,7 +514,7 @@ class RandomState(object):
 
         """
         out = self._random_sample_raw(size, dtype)
-        RandomState._1m_kernel(out)
+        RandomState._deceps_max0_kernel(numpy.finfo(dtype).eps, out)
         return out
 
     def rayleigh(self, scale=1.0, size=None, dtype=float):
```
Sorry, `numpy.finfo(dtype).epsneg` should be used, instead of `numpy.finfo(dtype).eps`. Using `eps` which is `2 * epsneg` would cause unnecessary bias towards 0.

Note that [the document of NumPy](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.finfo.html) is [technically incorrect](https://github.com/numpy/numpy/issues/6940).

Resolving patch:
diff --git a/cupy/random/generator.py b/cupy/random/generator.py
--- a/cupy/random/generator.py
+++ b/cupy/random/generator.py
@@ -491,8 +491,8 @@ def randn(self, *size, **kwarg):
                             % ', '.join(kwarg.keys()))
         return self.normal(size=size, dtype=dtype)
 
-    _1m_kernel = core.ElementwiseKernel(
-        '', 'T x', 'x = 1 - x', 'cupy_random_1_minus_x')
+    _mod1_kernel = core.ElementwiseKernel(
+        '', 'T x', 'x = (x == (T)1) ? 0 : x', 'cupy_random_x_mod_1')
 
     def _random_sample_raw(self, size, dtype):
         dtype = _check_and_get_dtype(dtype)
@@ -513,7 +513,7 @@ def random_sample(self, size=None, dtype=float):
 
         """
         out = self._random_sample_raw(size, dtype)
-        RandomState._1m_kernel(out)
+        RandomState._mod1_kernel(out)
         return out
 
     def rayleigh(self, scale=1.0, size=None, dtype=float):


Test patch:
Repository: cupy/cupy

Issue:
Complex scalar is not correctly casted
```
>>> cupy.arange(3, dtype='e') + 1j
array([0.+1.j, 1.+1.j, 2.+1.j])
>>> numpy.arange(3, dtype='e') + 1j
array([0.+1.j, 1.+1.j, 2.+1.j], dtype=complex64)
```

No problem with floats:
```
>>> cupy.arange(3, dtype='e') + 1e30
array([1.e+30, 1.e+30, 1.e+30], dtype=float32)
>>> cupy.arange(3, dtype='e') + 1e100
array([1.e+100, 1.e+100, 1.e+100])
```
Test complex in TestArrayElementwiseOp



Discussion:

Resolving patch:
diff --git a/cupy/core/fusion.py b/cupy/core/fusion.py
--- a/cupy/core/fusion.py
+++ b/cupy/core/fusion.py
@@ -19,7 +19,7 @@
     'u': 1,
     'i': 1,
     'f': 2,
-    'c': 3,
+    'c': 2,
 }
 
 _dtype_to_ctype = {


Test patch:
diff --git a/tests/cupy_tests/core_tests/test_fusion.py b/tests/cupy_tests/core_tests/test_fusion.py
--- a/tests/cupy_tests/core_tests/test_fusion.py
+++ b/tests/cupy_tests/core_tests/test_fusion.py
@@ -1435,23 +1435,23 @@ def func_a1(x, y, z):
 @testing.gpu
 class TestFusionPythonConstant(unittest.TestCase):
 
-    @testing.for_all_dtypes()
+    @testing.for_all_dtypes_combination(names=['dtype1', 'dtype2'])
     @testing.numpy_cupy_array_equal()
-    def test_python_scalar(self, xp, dtype):
+    def test_python_scalar(self, xp, dtype1, dtype2):
 
         @cupy.fuse()
         def f(x):
-            return x * numpy.asscalar(dtype(1))
-        return f(testing.shaped_arange((1,), xp, dtype))
+            return x * numpy.asscalar(dtype2(1))
+        return f(testing.shaped_arange((1,), xp, dtype1))
 
-    @testing.for_all_dtypes()
+    @testing.for_all_dtypes_combination(names=['dtype1', 'dtype2'])
     @testing.numpy_cupy_array_equal()
-    def test_numpy_scalar(self, xp, dtype):
+    def test_numpy_scalar(self, xp, dtype1, dtype2):
 
         @cupy.fuse()
         def f(x):
-            return x * dtype(1)
-        return f(testing.shaped_arange((1,), xp, dtype))
+            return x * dtype2(1)
+        return f(testing.shaped_arange((1,), xp, dtype1))
 
 
 @testing.gpu
diff --git a/tests/cupy_tests/core_tests/test_ndarray_elementwise_op.py b/tests/cupy_tests/core_tests/test_ndarray_elementwise_op.py
--- a/tests/cupy_tests/core_tests/test_ndarray_elementwise_op.py
+++ b/tests/cupy_tests/core_tests/test_ndarray_elementwise_op.py
@@ -11,8 +11,7 @@
 @testing.gpu
 class TestArrayElementwiseOp(unittest.TestCase):
 
-    @testing.for_all_dtypes_combination(names=['x_type', 'y_type'],
-                                        no_complex=True)
+    @testing.for_all_dtypes_combination(names=['x_type', 'y_type'])
     @testing.numpy_cupy_allclose(rtol=1e-6, accept_error=TypeError)
     def check_array_scalar_op(self, op, xp, x_type, y_type, swap=False,
                               no_bool=False, no_complex=False):
Repository: cupy/cupy

Issue:
Bundle header files for fp16
CUDA 9.2 or later allows redistribution of `cuda_fp16.h` and `cuda_fp16.hpp`.
https://docs.nvidia.com/cuda/archive/9.2/eula/#attachment-a

Let's bundle them into the repository and use it to avoid `CUDA_PATH`-based header discovery at runtime.


Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -107,6 +107,8 @@ def reduce_requirements(key):
         'core/include/cupy/carray.cuh',
         'core/include/cupy/complex.cuh',
         'core/include/cupy/atomics.cuh',
+        'core/include/cupy/_cuda/cuda-*/*.h',
+        'core/include/cupy/_cuda/cuda-*/*.hpp',
         'cuda/cupy_thrust.cu',
     ],
 }


Test patch:
Repository: cupy/cupy

Issue:
[WIP] Fix assigning from complex to float (only test)
When a user assign complex value to float array, it causes an error.


Discussion:

Resolving patch:
diff --git a/cupy/core/_ufuncs.py b/cupy/core/_ufuncs.py
--- a/cupy/core/_ufuncs.py
+++ b/cupy/core/_ufuncs.py
@@ -1,8 +1,30 @@
 from cupy.core._kernel import create_ufunc
 
+
+_complex_cast_copy = '''
+template<typename T, typename U>
+__device__ void cast_copy(const U& x, T& y) {y = T(x);}
+template<typename T, typename U>
+__device__ void cast_copy(const complex<U>& x, complex<T>& y) {
+    y = complex<T>(x);
+}
+template<typename T, typename U>
+__device__ void cast_copy(const complex<U>& x, T& y) {y = T(x.real());}
+'''
+
+
 elementwise_copy = create_ufunc(
     'cupy_copy',
     ('?->?', 'b->b', 'B->B', 'h->h', 'H->H', 'i->i', 'I->I', 'l->l', 'L->L',
      'q->q', 'Q->Q', 'e->e', 'f->f', 'd->d', 'F->F', 'D->D'),
-    'out0 = out0_type(in0)', default_casting='unsafe')
+    'cast_copy(in0, out0)',
+    preamble=_complex_cast_copy, default_casting='unsafe')
+
+
+elementwise_copy_where = create_ufunc(
+    'cupy_copy_where',
+    ('??->?', 'b?->b', 'B?->B', 'h?->h', 'H?->H', 'i?->i', 'I?->I', 'l?->l',
+     'L?->L', 'q?->q', 'Q?->Q', 'e?->e', 'f?->f', 'd?->d', 'F?->F', 'D?->D'),
+    'if (in1) cast_copy(in0, out0)',
+    preamble=_complex_cast_copy, default_casting='unsafe')
 # complex numbers requires out0 = complex<T>(in0)


Test patch:
diff --git a/tests/cupy_tests/core_tests/test_ndarray_indexing.py b/tests/cupy_tests/core_tests/test_ndarray_indexing.py
--- a/tests/cupy_tests/core_tests/test_ndarray_indexing.py
+++ b/tests/cupy_tests/core_tests/test_ndarray_indexing.py
@@ -1,4 +1,7 @@
 import unittest
+import warnings
+
+import numpy
 
 from cupy import testing
 
@@ -137,6 +140,16 @@ def test_setitem_copy(self, xp, dtype):
         a[:] = b
         return a
 
+    @testing.for_all_dtypes_combination(('src_type', 'dst_type'))
+    @testing.numpy_cupy_array_equal()
+    def test_setitem_different_type(self, xp, src_type, dst_type):
+        a = xp.zeros((2, 3, 4), dtype=dst_type)
+        b = testing.shaped_arange((2, 3, 4), xp, src_type)
+        with warnings.catch_warnings():
+            warnings.simplefilter('ignore', numpy.ComplexWarning)
+            a[:] = b
+        return a
+
     @testing.for_all_dtypes()
     @testing.numpy_cupy_array_equal()
     def test_setitem_partial_copy(self, xp, dtype):
Repository: cupy/cupy

Issue:
cupy.ascontiguousarray behavior different from numpy
```py
>>> np.ascontiguousarray(np.array(3))
array([3])
>>> cp.ascontiguousarray(cp.array(3))
array(3)
```


Discussion:

Resolving patch:
diff --git a/cupy/core/__init__.py b/cupy/core/__init__.py
--- a/cupy/core/__init__.py
+++ b/cupy/core/__init__.py
@@ -33,6 +33,8 @@
 from cupy.core._routines_math import true_divide  # NOQA
 from cupy.core._routines_statistics import nanmax  # NOQA
 from cupy.core._routines_statistics import nanmin  # NOQA
+from cupy.core.core import _internal_ascontiguousarray  # NOQA
+from cupy.core.core import _internal_asfortranarray  # NOQA
 from cupy.core.core import array  # NOQA
 from cupy.core.core import ascontiguousarray  # NOQA
 from cupy.core.core import asfortranarray  # NOQA
diff --git a/cupy/statistics/order.py b/cupy/statistics/order.py
--- a/cupy/statistics/order.py
+++ b/cupy/statistics/order.py
@@ -240,4 +240,4 @@ def percentile(a, q, axis=None, out=None, interpolation='linear',
             keepdim = (-1,) + keepdim
         ret = ret.reshape(keepdim)
 
-    return cupy.ascontiguousarray(ret)
+    return core._internal_ascontiguousarray(ret)


Test patch:
diff --git a/tests/cupy_tests/creation_tests/test_from_data.py b/tests/cupy_tests/creation_tests/test_from_data.py
--- a/tests/cupy_tests/creation_tests/test_from_data.py
+++ b/tests/cupy_tests/creation_tests/test_from_data.py
@@ -228,6 +228,16 @@ def test_ascontiguousarray_on_contiguous_array(self):
         b = cupy.ascontiguousarray(a)
         assert a is b
 
+    @testing.numpy_cupy_array_equal()
+    def test_asarray_cuda_array_zero_dim(self, xp):
+        a = xp.ones(())
+        return xp.ascontiguousarray(a)
+
+    @testing.numpy_cupy_array_equal()
+    def test_asarray_cuda_array_zero_dim_dtype(self, xp):
+        a = xp.ones((), dtype=numpy.float64)
+        return xp.ascontiguousarray(a, dtype=numpy.int64)
+
     @testing.for_CF_orders()
     @testing.for_all_dtypes()
     @testing.numpy_cupy_array_equal()
@@ -254,6 +264,19 @@ def test_copy_order(self, xp, order):
         b = xp.copy(a)
         return (b.flags.c_contiguous, b.flags.f_contiguous)
 
+    @testing.numpy_cupy_array_equal()
+    def test_asfortranarray_cuda_array_zero_dim(self, xp):
+        a = xp.ones(())
+        return xp.asfortranarray(a)
+
+    @testing.for_all_dtypes_combination(['dtype_a', 'dtype_b'],
+                                        no_complex=True)
+    @testing.numpy_cupy_array_equal()
+    def test_asfortranarray_cuda_array_zero_dim_dtype(
+            self, xp, dtype_a, dtype_b):
+        a = xp.ones((), dtype=dtype_a)
+        return xp.asfortranarray(a, dtype=dtype_b)
+
 
 class DummyObjectWithCudaArrayInterface(object):
Repository: cupy/cupy

Issue:
Support for Nvidia Tesla T4
Feature request

Current Cupy build doesn't include Tesla T4. 
https://github.com/cupy/cupy/blob/master/cupy_setup_build.py#L633. It would be great if we have support for T4 as well. I believe this architecture is more for inference than training but still a good use case to consider.
I believe it has to accommodate Tesla T4 by supporting cuda compute ability 7.5



Discussion:

Resolving patch:
diff --git a/cupy_setup_build.py b/cupy_setup_build.py
--- a/cupy_setup_build.py
+++ b/cupy_setup_build.py
@@ -636,7 +636,13 @@ def _nvcc_gencode_options(cuda_version):
     arch_list = ['compute_30',
                  'compute_50']
 
-    if cuda_version >= 9000:
+    if cuda_version >= 10000:
+        arch_list += [('compute_60', 'sm_60'),
+                      ('compute_61', 'sm_61'),
+                      ('compute_70', 'sm_70'),
+                      ('compute_75', 'sm_75'),
+                      'compute_70']
+    elif cuda_version >= 9000:
         arch_list += [('compute_60', 'sm_60'),
                       ('compute_61', 'sm_61'),
                       ('compute_70', 'sm_70'),


Test patch:
Repository: cupy/cupy

Issue:
nvcc absence warning could be removed
On installing cupy from source, it is warned about absence of nvcc in `PATH`.
But it is actually not required both during installation and on runtime.
```
    **************************************************
    *** WARNING: nvcc not in path.
    *** WARNING: Please set path to nvcc.
    **************************************************
```
It's rather confusing for troubleshooting.


Discussion:

Resolving patch:
diff --git a/install/build.py b/install/build.py
--- a/install/build.py
+++ b/install/build.py
@@ -41,10 +41,7 @@ def get_cuda_path():
 
     nvcc_path = utils.search_on_path(('nvcc', 'nvcc.exe'))
     cuda_path_default = None
-    if nvcc_path is None:
-        utils.print_warning('nvcc not in path.',
-                            'Please set path to nvcc.')
-    else:
+    if nvcc_path is not None:
         cuda_path_default = os.path.normpath(
             os.path.join(os.path.dirname(nvcc_path), '..'))
 


Test patch:
Repository: cupy/cupy

Issue:
Incorrect behavior of in-place operation between ndarrays share same memory space
Incorrect behavior of in-place operation between ndarrays which
- share same memory space and
- have different strides.

Test code:
```py
import numpy
import cupy

for xp in (numpy, cupy):
    x = xp.arange(10000).reshape(100, 100)
    x += x.T
    print(xp.__name__, ':', x.sum())  # Expected: 99990000 = 10000 * (10000 - 1)
```
Output:
```py
numpy : 99990000
cupy : 100026222
```


Discussion:
Looks like this is a different manifestation of the same problem #2405? 
To resolve this issue, we need to support `cupy.shares_memory` or `cupy.may_share_memory` and copy inputs before the operation.

Resolving patch:
diff --git a/cupy/misc/memory_ranges.py b/cupy/misc/memory_ranges.py
--- a/cupy/misc/memory_ranges.py
+++ b/cupy/misc/memory_ranges.py
@@ -1,40 +1,16 @@
-def _get_bound(array):
-    left = array.data.ptr
-    right = left
-
-    for dim, stride in zip(array.shape, array.strides):
-        right += (dim - 1) * stride
-
-    if left > right:
-        left, right = right, left
-
-    return left, right + array.itemsize
-
-
-def _may_share_bounds(a, b):
-    a_data, b_data = a.data, b.data
-
-    if (a_data.device_id != b_data.device_id
-            or a_data.mem != b_data.mem
-            or a.size == 0 or b.size == 0):
-        return False
-
-    a_left, a_right = _get_bound(a)
-    b_left, b_right = _get_bound(b)
-
-    return a_left < b_right and b_left < a_right
+from cupy.core import _memory_range
 
 
 def may_share_memory(a, b, max_work=None):
     if max_work is None:
-        return _may_share_bounds(a, b)
+        return _memory_range.may_share_bounds(a, b)
 
     raise NotImplementedError('Only supported for `max_work` is `None`')
 
 
 def shares_memory(a, b, max_work=None):
     if max_work == 'MAY_SHARE_BOUNDS':
-        return _may_share_bounds(a, b)
+        return _memory_range.may_share_bounds(a, b)
 
     raise NotImplementedError(
         'Only supported for `max_work` is MAY_SHARE_BOUNDS')
diff --git a/cupy_setup_build.py b/cupy_setup_build.py
--- a/cupy_setup_build.py
+++ b/cupy_setup_build.py
@@ -34,6 +34,7 @@
         'file': [
             'cupy.core._dtype',
             'cupy.core._kernel',
+            'cupy.core._memory_range',
             'cupy.core._routines_indexing',
             'cupy.core._routines_logic',
             'cupy.core._routines_manipulation',


Test patch:
diff --git a/tests/cupy_tests/core_tests/test_ndarray_ufunc.py b/tests/cupy_tests/core_tests/test_ndarray_ufunc.py
--- a/tests/cupy_tests/core_tests/test_ndarray_ufunc.py
+++ b/tests/cupy_tests/core_tests/test_ndarray_ufunc.py
@@ -87,3 +87,11 @@ def test_indexing(self, xp):
         a = cupy.testing.shaped_arange((3, 1), xp)[:, :, None]
         b = cupy.testing.shaped_arange((3, 2), xp)[:, None, :]
         return a * b
+
+    @testing.with_requires('numpy>=1.11')
+    @testing.numpy_cupy_array_equal()
+    def test_shares_memory(self, xp):
+        a = cupy.testing.shaped_arange((1000, 1000), xp, 'int64')
+        b = xp.transpose(a)
+        a += b
+        return a