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

Issue:
cuda mem transfer functions is not auto initializing context 
This is the reason for the failure in https://github.com/gpuopenanalytics/pygdf/issues/53 .

The method `gpuarray.copy_to_host()` doesn't acquire a cuda context.  When it is used in a new thread without an associating cuda context, the cuda driver returns CUDA_ERROR_INVALID_CONTEXT because no context is active for the thread.


Discussion:

Resolving patch:
diff --git a/numba/cuda/cudadrv/devicearray.py b/numba/cuda/cudadrv/devicearray.py
--- a/numba/cuda/cudadrv/devicearray.py
+++ b/numba/cuda/cudadrv/devicearray.py
@@ -136,6 +136,7 @@ def device_ctypes_pointer(self):
         else:
             return self.gpu_data.device_ctypes_pointer
 
+    @devices.require_context
     def copy_to_device(self, ary, stream=0):
         """Copy `ary` to `self`.
 
@@ -153,6 +154,7 @@ def copy_to_device(self, ary, stream=0):
             sz = min(_driver.host_memory_size(ary), self.alloc_size)
             _driver.host_to_device(self, ary, sz, stream=stream)
 
+    @devices.require_context
     def copy_to_host(self, ary=None, stream=0):
         """Copy ``self`` to ``ary`` or create a new Numpy ndarray
         if ``ary`` is ``None``.
@@ -330,6 +332,7 @@ def ravel(self, order='C', stream=0):
         else:
             raise NotImplementedError("operation requires copying")
 
+    @devices.require_context
     def __getitem__(self, item):
         return self._do_getitem(item)
 


Test patch:
diff --git a/numba/cuda/tests/cudapy/test_multithreads.py b/numba/cuda/tests/cudapy/test_multithreads.py
--- a/numba/cuda/tests/cudapy/test_multithreads.py
+++ b/numba/cuda/tests/cudapy/test_multithreads.py
@@ -1,4 +1,5 @@
 import traceback
+import threading
 import multiprocessing
 import numpy as np
 from numba import cuda
@@ -68,6 +69,30 @@ def test_spawn_concurrent_compilation(self):
             raise AssertionError(err)
         self.assertEqual(p.exitcode, 0, 'test failed in child process')
 
+    def test_invalid_context_error_with_d2h(self):
+        def d2h(arr, out):
+            out[:] = arr.copy_to_host()
+
+        arr = np.arange(1, 4)
+        out = np.zeros_like(arr)
+        darr = cuda.to_device(arr)
+        th = threading.Thread(target=d2h, args=[darr, out])
+        th.start()
+        th.join()
+        np.testing.assert_equal(arr, out)
+
+    def test_invalid_context_error_with_d2d(self):
+        def d2d(dst, src):
+            dst.copy_to_device(src)
+
+        arr = np.arange(100)
+        common = cuda.to_device(arr)
+        darr = cuda.to_device(np.zeros(common.shape, dtype=common.dtype))
+        th = threading.Thread(target=d2d, args=[darr, common])
+        th.start()
+        th.join()
+        np.testing.assert_equal(darr.copy_to_host(), arr)
+
 
 if __name__ == '__main__':
     unittest.main()
Repository: numba/numba

Issue:
xoroshiro128p initialization is very slow
The time required to initialize the RNG state for 10,000 threads is 473 ms on my GeForce 750M:
```
import numba.cuda.random
%timeit numba.cuda.random.create_xoroshiro128p_states(10000, 1)
```
This is because the initialization is done serially on the GPU, which is not necessary for a function called from the CPU.  The initialization could be done on the CPU and transferred to the GPU.  It would still be serial, but the integer instruction throughput on the CPU is much higher than the GPU in all cases.


Discussion:

Resolving patch:
diff --git a/numba/cuda/random.py b/numba/cuda/random.py
--- a/numba/cuda/random.py
+++ b/numba/cuda/random.py
@@ -212,27 +212,23 @@ def xoroshiro128p_normal_float64(states, index):
     return z0
 
 
-@cuda.jit
-def init_xoroshiro128p_states_kernel(states, seed, subsequence_start):
+@jit
+def init_xoroshiro128p_states_cpu(states, seed, subsequence_start):
+    n = states.shape[0]
     seed = uint64(seed)
     subsequence_start = uint64(subsequence_start)
 
-    # Only run this with a single thread and block
-    n = states.shape[0]
-
-    if n < 1:
-        return  # assuming at least 1 state going forward
+    if n >= 1:
+        init_xoroshiro128p_state(states, 0, seed)
 
-    init_xoroshiro128p_state(states, 0, seed)
+        # advance to starting subsequence number
+        for _ in range(subsequence_start):
+            xoroshiro128p_jump(states, 0)
 
-    # advance to starting subsequence number
-    for _ in range(subsequence_start):
-        xoroshiro128p_jump(states, 0)
-
-    # populate the rest of the array
-    for i in range(1, n):
-        states[i] = states[i - 1]  # take state of previous generator
-        xoroshiro128p_jump(states, i)  # and jump forward 2**64 steps
+        # populate the rest of the array
+        for i in range(1, n):
+            states[i] = states[i - 1]  # take state of previous generator
+            xoroshiro128p_jump(states, i)  # and jump forward 2**64 steps
 
 
 def init_xoroshiro128p_states(states, seed, subsequence_start=0, stream=0):
@@ -252,7 +248,12 @@ def init_xoroshiro128p_states(states, seed, subsequence_start=0, stream=0):
     :type seed: uint64
     :param seed: starting seed for list of generators
     '''
-    init_xoroshiro128p_states_kernel[1, 1, stream](states, seed, subsequence_start)
+
+    # Initialization on CPU is much faster than the GPU
+    states_cpu = np.empty(shape=states.shape, dtype=xoroshiro128p_dtype)
+    init_xoroshiro128p_states_cpu(states_cpu, seed, subsequence_start)
+
+    states.copy_to_device(states_cpu, stream=stream)
 
 
 def create_xoroshiro128p_states(n, seed, subsequence_start=0, stream=0):


Test patch:
Repository: numba/numba

Issue:
Weird behavior on np.nanmedian
Hello,

I'm no Numba expert, however I have a weird behavior when using nanmedian on arrays containing only nans (edge case).

I'm not sure it was by design, hence this issue.

Thomas

```
import numba as nb
import numpy as np

@nb.jit(nopython=True)
def f(a):
    return np.nansum(a)

a = np.array([np.nan])
print(np.nansum(a))
print(f(a))
```
This is ok!

```
import numba as nb
import numpy as np

@nb.jit(nopython=True)
def f(a):
    return np.nanmedian(a)

a = np.array([np.nan])
print(np.nanmedian(a))
print(f(a))
```
This is not ok:
```
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-15-cb312ec8285d> in <module>()
      8 a = np.array([np.nan])
      9 print(np.nanmedian(a))
---> 10 print(f(np.array([np.nan, np.nan])))

AssertionError: 
```


Discussion:
Thanks for the report, I can reproduce. This is a bug, I have a fix that I'll try and get into the next release.

Resolving patch:
diff --git a/numba/targets/arraymath.py b/numba/targets/arraymath.py
--- a/numba/targets/arraymath.py
+++ b/numba/targets/arraymath.py
@@ -770,6 +770,11 @@ def nanmedian_impl(arry):
                 if not isnan(v):
                     temp_arry[n] = v
                     n += 1
+
+            # all NaNs
+            if n == 0:
+                return np.nan
+
             return _median_inner(temp_arry, n)
 
         return nanmedian_impl


Test patch:
diff --git a/numba/tests/test_array_reductions.py b/numba/tests/test_array_reductions.py
--- a/numba/tests/test_array_reductions.py
+++ b/numba/tests/test_array_reductions.py
@@ -345,6 +345,8 @@ def variations(a):
             yield a
             a[a % 4 >= 2] = float('nan')
             yield a
+            a[:] = float('nan')
+            yield a
 
         self.check_median_basic(pyfunc, variations)
Repository: numba/numba

Issue:
CUDA Streams Not Bound by Default
It seems like constructing a device array with an explicit stream doesn't actually bind the array stream to that stream (e.g. the below). Is this expected
````python
from numba import cuda
stream = cuda.stream()
with stream.auto_synchronize():
    arr = cuda.device_array(
        (3, 3),
        dtype=np.float64,
        stream=stream)
    assert arr.bind(stream).stream == stream
    assert arr.stream == stream # throws!
````


Discussion:
Good catch.  I think that it is a mistake that we didn't bind the stream to the array.

Resolving patch:
diff --git a/numba/cuda/cudadrv/devicearray.py b/numba/cuda/cudadrv/devicearray.py
--- a/numba/cuda/cudadrv/devicearray.py
+++ b/numba/cuda/cudadrv/devicearray.py
@@ -105,7 +105,7 @@ def __init__(self, shape, strides, dtype, stream=0, writeback=None,
         self.gpu_data = gpu_data
 
         self.__writeback = writeback    # should deprecate the use of this
-        self.stream = 0
+        self.stream = stream
 
     def bind(self, stream=0):
         """Bind a CUDA stream to this object so that all subsequent operation
@@ -164,7 +164,7 @@ def copy_to_device(self, ary, stream=0):
 
         sentry_contiguous(self)
         stream = self._default_stream(stream)
-        
+
         if _driver.is_device_memory(ary):
             sentry_contiguous(ary)
 


Test patch:
diff --git a/numba/cuda/tests/cudadrv/test_cuda_ndarray.py b/numba/cuda/tests/cudadrv/test_cuda_ndarray.py
--- a/numba/cuda/tests/cudadrv/test_cuda_ndarray.py
+++ b/numba/cuda/tests/cudadrv/test_cuda_ndarray.py
@@ -33,6 +33,17 @@ def test_devicearray(self):
 
         self.assertTrue((array == original).all())
 
+    def test_stream_bind(self):
+        stream = cuda.stream()
+        with stream.auto_synchronize():
+            arr = cuda.device_array(
+                (3, 3),
+                dtype=np.float64,
+                stream=stream)
+            self.assertEqual(arr.bind(stream).stream, stream)
+            self.assertEqual(arr.stream, stream)
+
+
     def test_devicearray_partition(self):
         N = 100
         array = np.arange(N, dtype=np.int32)
Repository: numba/numba

Issue:
simplify_CFG raising KeyError
Noticed by DataShader team with the following script:

```python
import numpy as np, datashader as ds, xarray as xr, datashader.utils as du
from datashader import transfer_functions as tf

n=50
x_r=(0.0,4.5)

def f(x,y):
    return np.sin(x**2+y**2)

ls  = np.linspace(*x_r, n)
x,y = np.meshgrid(ls, ls)
z   = f(x,y)

da = xr.DataArray(z, coords={'x': ls, 'y': ls}, dims=('y', 'x'))

cvs = ds.Canvas(plot_height=n*5, plot_width=n*5, x_range=x_r, y_range=x_r)
agg = cvs.raster(da)
img = tf.shade(agg)

du.export_image(img, "parfors")
```

with datashader=0.6.2, xarray=0.9.6, numpy=1.13.1, and numba=0.37.0dev1+25.ga8a973f.

The traceback is leading to `simplify_CFG` inside the parfor pass.
simplify_CFG raising KeyError
Noticed by DataShader team with the following script:

```python
import numpy as np, datashader as ds, xarray as xr, datashader.utils as du
from datashader import transfer_functions as tf

n=50
x_r=(0.0,4.5)

def f(x,y):
    return np.sin(x**2+y**2)

ls  = np.linspace(*x_r, n)
x,y = np.meshgrid(ls, ls)
z   = f(x,y)

da = xr.DataArray(z, coords={'x': ls, 'y': ls}, dims=('y', 'x'))

cvs = ds.Canvas(plot_height=n*5, plot_width=n*5, x_range=x_r, y_range=x_r)
agg = cvs.raster(da)
img = tf.shade(agg)

du.export_image(img, "parfors")
```

with datashader=0.6.2, xarray=0.9.6, numpy=1.13.1, and numba=0.37.0dev1+25.ga8a973f.

The traceback is leading to `simplify_CFG` inside the parfor pass.


Discussion:

Resolving patch:
diff --git a/numba/ir_utils.py b/numba/ir_utils.py
--- a/numba/ir_utils.py
+++ b/numba/ir_utils.py
@@ -1095,6 +1095,7 @@ def find_single_branch(label):
         block = blocks[label]
         return len(block.body) == 1 and isinstance(block.body[0], ir.Branch)
     single_branch_blocks = list(filter(find_single_branch, blocks.keys()))
+    marked_for_del = set()
     for label in single_branch_blocks:
         inst = blocks[label].body[0]
         predecessors = cfg.predecessors(label)
@@ -1106,7 +1107,10 @@ def find_single_branch(label):
             else:
                 delete_block = False
         if delete_block:
-            del blocks[label]
+            marked_for_del.add(label)
+    # Delete marked labels
+    for label in marked_for_del:
+        del blocks[label]
     merge_adjacent_blocks(blocks)
     return rename_labels(blocks)
 


Test patch:
Repository: numba/numba

Issue:
Memory leak isssue
Environment: Ubuntu 16.04, Numba 0.36.0.dev.
I have some codes that extensively use Numba for acceleration and have to run for very long time.
I found that the memory usage of my process kept going up, which is quite serious as my 128GB RAM can be depleted in just one day.
I have been able to reproduce this problem with a minimal code snippet as shown below.
I also found that the issue was gone or at least much less serious if I set `parallel` to `False` and used `range` instead of `prange`.
So I guess this is probably related to the parallel feature.
``` python
from numba import jit, prange
import numpy as np

@jit(nopython=True, parallel=True)
def square(A):
    out = np.empty(A.shape, dtype=np.float64)
    for r in range(A.shape[0]):
        temp = np.empty(A.shape[1], dtype=np.float64)
        for c in range(A.shape[1]):
            temp[c] = A[r, c] ** 2
        for c in range(A.shape[1]):
            out[r, c] = temp[c] ** 2
    return out

for i in range(1000000):
    x = np.random.random((2000, 2000))
    square(x)
    print(i)
```
I found a workaround that might give the same result without this issue:
``` python
@jit(nopython=True, parallel=True)
def square(A):
    out = np.empty(A.shape, dtype=np.float64)
    temp = np.empty(A.shape, dtype=np.float64)
    for r in prange(A.shape[0]):
        for c in range(A.shape[1]):
            temp[r, c] = A[r, c] ** 2
        for c in range(A.shape[1]):
            out[r, c] = temp[r, c] ** 2
    return out
```


Discussion:
Thanks for the report. I've run this using the first code sample with `for r in range` then `for r in prange`. For `range` I see <150Mb `RSS`. For `prange` I do see `RSS` per thread creeping up as time goes by, needs more investigation.
Hi @stuartarchibald , may I know if there is any progress on this issue? Thanks!

Resolving patch:
diff --git a/numba/parfor.py b/numba/parfor.py
--- a/numba/parfor.py
+++ b/numba/parfor.py
@@ -1788,7 +1788,9 @@ def lower_parfor_sequential(typingctx, func_ir, typemap, calltypes):
     dprint_func_ir(func_ir, "after parfor sequential lowering")
     simplify(func_ir, typemap, calltypes)
     dprint_func_ir(func_ir, "after parfor sequential simplify")
-
+    # add dels since simplify removes dels
+    post_proc = postproc.PostProcessor(func_ir)
+    post_proc.run()
     return
 
 


Test patch:
Repository: numba/numba

Issue:
Numba cuda incorrect fork error msg
Numba cuda incorrect fork error.  Need to add check that `self.pid is not None` at https://github.com/numba/numba/blob/3a4ae696a115871cca0fff4fa53ce518b8f34618/numba/cuda/cudadrv/driver.py#L319-L322
  
I discovered this when updating to osx 10.13.2 with a nvidia gpu driver that is too old.  This makes the cuda driver fail to initialize.  Since `pid` is `None` by default, the check will think that it is being forked as it compares the current pid to None.


Discussion:

Resolving patch:
diff --git a/numba/cuda/cudadrv/driver.py b/numba/cuda/cudadrv/driver.py
--- a/numba/cuda/cudadrv/driver.py
+++ b/numba/cuda/cudadrv/driver.py
@@ -316,7 +316,7 @@ def _check_error(self, fname, retcode):
             _logger.error(msg)
             if retcode == enums.CUDA_ERROR_NOT_INITIALIZED:
                 # Detect forking
-                if _getpid() != self.pid:
+                if self.pid is not None and _getpid() != self.pid:
                     msg = 'pid %s forked from pid %s after CUDA driver init'
                     _logger.critical(msg, _getpid(), self.pid)
                     raise CudaDriverError("CUDA initialized before forking")


Test patch:
Repository: numba/numba

Issue:
Added nextoffset and lineno arguments to ByteCodeInst.get.
Solves the bug, that nextoffset is mandatory since V0.36.


Discussion:
# [Codecov](https://codecov.io/gh/numba/numba/pull/2620?src=pr&el=h1) Report
> Merging [#2620](https://codecov.io/gh/numba/numba/pull/2620?src=pr&el=desc) into [master](https://codecov.io/gh/numba/numba/commit/9ed665ea67ce293b94c641aab0387fc846588b38?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `75%`.


```diff
@@            Coverage Diff             @@
##           master    #2620      +/-   ##
==========================================
+ Coverage   86.31%   86.31%   +<.01%     
==========================================
  Files         320      320              
  Lines       66708    66709       +1     
  Branches     7537     7537              
==========================================
+ Hits        57578    57579       +1     
  Misses       7915     7915              
  Partials     1215     1215
```



Please could you expand on what you mean in the comment? Also before merge please could associated tests be written to validate the change?

Resolving patch:
diff --git a/numba/bytecode.py b/numba/bytecode.py
--- a/numba/bytecode.py
+++ b/numba/bytecode.py
@@ -12,7 +12,6 @@
 from types import CodeType, ModuleType
 
 from numba import errors, utils
-from numba.config import PYVERSION
 
 
 opcode_info = namedtuple('opcode_info', ['argsize'])
@@ -75,10 +74,6 @@ def __init__(self, offset, opcode, arg, nextoffset):
         self.arg = arg
         self.lineno = -1  # unknown line number
 
-    @classmethod
-    def get(cls, offset, opname, arg):
-        return cls(offset, dis.opmap[opname], arg)
-
     @property
     def is_jump(self):
         return self.opcode in JUMP_OPS


Test patch:
Repository: numba/numba

Issue:
support for fill function 
`fill` function of arrays is a very handy way to zero out an existing array (like memset in C)
in regular python:
```
a = np.arange(10)
a.fill(0)
```
will zero out `a`
this is a very simple function to support and might save us from writing some unnecessary loops. 
Thanks!


Discussion:
Added as feature request. In the meanwhile would broadcast assignment help a bit?
```
a = np.arange(10)
a[:] = 0
```
I didn't realize this works.
Thanks!

Resolving patch:
diff --git a/numba/targets/arrayobj.py b/numba/targets/arrayobj.py
--- a/numba/targets/arrayobj.py
+++ b/numba/targets/arrayobj.py
@@ -4456,6 +4456,14 @@ def np_vstack_impl(arrays):
 
         return context.compile_internal(builder, np_vstack_impl, sig, args)
 
+@extending.overload_method(types.Array, 'fill')
+def arr_fill(arr, val):
+
+    def fill_impl(arr, val):
+        arr[:] = val
+        return None
+
+    return fill_impl
 
 # -----------------------------------------------------------------------------
 # Sorting


Test patch:
diff --git a/numba/tests/test_array_methods.py b/numba/tests/test_array_methods.py
--- a/numba/tests/test_array_methods.py
+++ b/numba/tests/test_array_methods.py
@@ -85,6 +85,8 @@ def array_take(arr, indices):
 def array_take_kws(arr, indices, axis):
     return arr.take(indices, axis=axis)
 
+def array_fill(arr, val):
+    return arr.fill(val)
 
 # XXX Can't pass a dtype as a Dispatcher argument for now
 def make_array_view(newtype):
@@ -797,6 +799,34 @@ def check(arr, ind):
         self.disable_leak_check()
 
 
+    def test_fill(self):
+        pyfunc = array_fill
+        cfunc = jit(nopython=True)(pyfunc)
+        def check(arr, val):
+            expected = np.copy(arr)
+            erv = pyfunc(expected, val)
+            self.assertTrue(erv is None)
+            got = np.copy(arr)
+            grv = cfunc(got, val)
+            self.assertTrue(grv is None)
+            # check mutation is the same
+            self.assertPreciseEqual(expected, got)
+
+        # scalar
+        A = np.arange(1)
+        for x in [np.float64, np.bool_]:
+            check(A, x(10))
+
+        # 2d
+        A = np.arange(12).reshape(3, 4)
+        for x in [np.float64, np.bool_]:
+            check(A, x(10))
+
+        # 4d
+        A = np.arange(48, dtype=np.complex64).reshape(2, 3, 4, 2)
+        for x in [np.float64, np.complex128, np.bool_]:
+            check(A, x(10))
+
 class TestArrayComparisons(TestCase):
 
     def test_identity(self):
Repository: numba/numba

Issue:
Typing error message incorrectly masking lowering error (CUDA)
With reference to #2205 
```python
import numpy as np
from numba import cuda

@cuda.jit('float64(float64[::1])',device=True)
def devfunc(arr):
    return 1.0;

@cuda.jit('void(float64[:])')
def kern(arr):
    toarr=cuda.local.array(10,'float64')
    for i in range(10):
        toarr[i] = arr[i]
    print(devfunc(toarr))

kern[1,1](np.zeros(10))
```
reports
```
numba.errors.TypingError: Failed at nopython (nopython frontend)
Invalid usage of Function(<DeviceFunction py_func=<function devfunc at 0x7efef3b17268> signature=(array(float64, 1d, C),) -> float64>) with parameters (array(float64, 1d, C))
Known signatures:
 * (array(float64, 1d, C),) -> float64
File "issue2205.py", line 13
[1] During: resolving callee type: Function(<DeviceFunction py_func=<function devfunc at 0x7efef3b17268> signature=(array(float64, 1d, C),) -> float64>)
[2] During: typing of call at issue2205.py (13)
```
when the actual problem is that `cuda.local.array` couldn't handle the string entered for `dtype` and it is this which should have been reported.
```python
import numpy as np
from numba import cuda

@cuda.jit('float64(float64[::1])',device=True)
def devfunc(arr):
    return 1.0;

@cuda.jit('void(float64[:])')
def kern(arr):
    toarr=cuda.local.array(10,'float64')
    for i in range(10):
        toarr[i] = arr[i]
    #print(devfunc(toarr))

kern[1,1](np.zeros(10))
```
gives
```
numba.errors.LoweringError: Failed at nopython (nopython mode backend)
cannot create weak reference to 'str' object
File "issue2205.py", line 10
[1] During: lowering "$0.6 = call ptx.lmem.alloc($const0.4, $const0.5)" at issue2205.py (10)
```



Discussion:

Resolving patch:
diff --git a/numba/typeconv/typeconv.py b/numba/typeconv/typeconv.py
--- a/numba/typeconv/typeconv.py
+++ b/numba/typeconv/typeconv.py
@@ -1,6 +1,7 @@
 from __future__ import print_function, absolute_import
 
 from . import _typeconv, castgraph, Conversion
+from .. import types
 
 
 class TypeManager(object):
@@ -23,6 +24,9 @@ def select_overload(self, sig, overloads, allow_unsafe):
                                          allow_unsafe)
 
     def check_compatible(self, fromty, toty):
+        if not isinstance(toty, types.Type):
+            raise ValueError("Specified type '%s' (%s) is not a Numba type" %
+                             (toty, type(toty)))
         name = _typeconv.check_compatible(self._ptr, fromty._code, toty._code)
         conv = Conversion[name] if name is not None else None
         assert conv is not Conversion.nil


Test patch: