<commit_before>'''
The Device Array API is not implemented in the simulator. This module provides
stubs to allow tests to import correctly.
'''
from contextlib import contextmanager
from warnings import warn

import numpy as np

from numba.core import types
from numba.np import numpy_support

DeviceRecord = None
from_record_like = None


def is_cuda_ndarray(obj):
    return getattr(obj, '__cuda_ndarray__', False)


errmsg_contiguous_buffer = ("Array contains non-contiguous buffer and cannot "
                            "be transferred as a single memory region. Please "
                            "ensure contiguous buffer with numpy "
                            ".ascontiguousarray()")


class FakeShape(tuple):
    '''
    The FakeShape class is used to provide a shape which does not allow negative
    indexing, similar to the shape in CUDA Python. (Numpy shape arrays allow
    negative indexing)
    '''

    def __getitem__(self, k):
        if isinstance(k, int) and k < 0:
            raise IndexError('tuple index out of range')
        return super(FakeShape, self).__getitem__(k)


class FakeKernelCUDAArray(object):
    def __init__(self, item):
        assert isinstance(item, FakeCUDAArray)
        self.__dict__['_item'] = item

    def __wrap_if_fake(self, item):
        return FakeKernelCUDAArray(item) if isinstance(item, FakeCUDAArray) else item

    def __getattr__(self, attrname):
        if attrname in dir(self.__dict__['_item']._ary):  # For, eg, array size.
            return self.__wrap_if_fake(getattr(self.__dict__['_item']._ary, attrname))
        else:
            return self.__wrap_if_fake(self._item.__getitem__(attrname))

    def __setattr__(self, nm, val):
        self._item.__setitem__(nm, val)

    def __getitem__(self, idx):
        return self.__wrap_if_fake(self._item.__getitem__(idx))

    def __setitem__(self, idx, val):
        self._item.__setitem__(idx, val)

    def __len__(self):
        return len(self.__dict__['_item'])

class FakeCUDAArray(object):
    '''
    Implements the interface of a DeviceArray/DeviceRecord, but mostly just
    wraps a NumPy array.
    '''

    __cuda_ndarray__ = True  # There must be gpu_data attribute

    def __init__(self, ary, stream=0):
        if ary.ndim == 0:
            self._ary = ary.reshape(1)
            self._ary_access = self._ary[0]
        else:
            self._ary = ary
            self._ary_access = self._ary
        self.stream = stream

    @property
    def alloc_size(self):
        return self._ary.nbytes

    @property
    def nbytes(self):
        # return nbytes -- FakeCUDAArray is a wrapper around NumPy
        return self._ary.nbytes

    def __getattr__(self, attrname):
        try:
            attr = getattr(self._ary, attrname)
            return attr
        except AttributeError as e:
            msg = "Wrapped array has no attribute '%s'" % attrname
            raise AttributeError(msg) from e

    def bind(self, stream=0):
        return FakeCUDAArray(self._ary, stream)

    @property
    def T(self):
        return self.transpose()

    def transpose(self, axes=None):
        return FakeCUDAArray(np.transpose(self._ary, axes=axes))

    def __getitem__(self, idx):
        ret = self._ary.__getitem__(idx)
        if np.issubdtype(type(ret), np.number) or np.issubdtype(type(ret), np.bool_):
            return ret
        else:
            return FakeCUDAArray(self._ary_access.__getitem__(idx), stream=self.stream)

    def __setitem__(self, idx, val):
        return self._ary_access.__setitem__(idx, val)

    def copy_to_host(self, ary=None, stream=0):
        if ary is None:
            ary = np.empty_like(self._ary)
        else:
            check_array_compatibility(self, ary)
        np.copyto(ary, self._ary)
        return ary

    def copy_to_device(self, ary, stream=0):
        '''
        Copy from the provided array into this array.

        This may be less forgiving than the CUDA Python implementation, which
        will copy data up to the length of the smallest of the two arrays,
        whereas this expects the size of the arrays to be equal.
        '''
        sentry_contiguous(self)
        self_core, ary_core = array_core(self), array_core(ary)
        if isinstance(ary, FakeCUDAArray):
            sentry_contiguous(ary)
            check_array_compatibility(self_core, ary_core)
        else:
            ary_core = np.array(
                ary_core,
                order='C' if self_core.flags['C_CONTIGUOUS'] else 'F',<commit_msg>a minor oops.<commit_after>'''
The Device Array API is not implemented in the simulator. This module provides
stubs to allow tests to import correctly.
'''
from contextlib import contextmanager
from warnings import warn

import numpy as np

from numba.core import types
from numba.np import numpy_support

DeviceRecord = None
from_record_like = None


def is_cuda_ndarray(obj):
    return getattr(obj, '__cuda_ndarray__', False)


errmsg_contiguous_buffer = ("Array contains non-contiguous buffer and cannot "
                            "be transferred as a single memory region. Please "
                            "ensure contiguous buffer with numpy "
                            ".ascontiguousarray()")


class FakeShape(tuple):
    '''
    The FakeShape class is used to provide a shape which does not allow negative
    indexing, similar to the shape in CUDA Python. (Numpy shape arrays allow
    negative indexing)
    '''

    def __getitem__(self, k):
        if isinstance(k, int) and k < 0:
            raise IndexError('tuple index out of range')
        return super(FakeShape, self).__getitem__(k)


class FakeKernelCUDAArray(object):
    def __init__(self, item):
        assert isinstance(item, FakeCUDAArray)
        self.__dict__['_item'] = item

    def __wrap_if_fake(self, item):
        return FakeKernelCUDAArray(item) if isinstance(item, FakeCUDAArray) else item

    def __getattr__(self, attrname):
        if attrname in dir(self.__dict__['_item']._ary):  # For, eg, array size.
            return self.__wrap_if_fake(getattr(self.__dict__['_item']._ary, attrname))
        else:
            return self.__wrap_if_fake(self._item.__getitem__(attrname))

    def __setattr__(self, nm, val):
        self._item.__setitem__(nm, val)

    def __getitem__(self, idx):
        return self.__wrap_if_fake(self._item.__getitem__(idx))

    def __setitem__(self, idx, val):
        self._item.__setitem__(idx, val)

    def __len__(self):
        return len(self.__dict__['_item'])

class FakeCUDAArray(object):
    '''
    Implements the interface of a DeviceArray/DeviceRecord, but mostly just
    wraps a NumPy array.
    '''

    __cuda_ndarray__ = True  # There must be gpu_data attribute

    def __init__(self, ary, stream=0):
        if ary.ndim == 0:
            self._ary = ary.reshape(1)
            self._ary_access = self._ary[0]
        else:
            self._ary = ary
            self._ary_access = self._ary
        self.stream = stream

    @property
    def alloc_size(self):
        return self._ary.nbytes

    @property
    def nbytes(self):
        # return nbytes -- FakeCUDAArray is a wrapper around NumPy
        return self._ary.nbytes

    def __getattr__(self, attrname):
        try:
            attr = getattr(self._ary, attrname)
            return attr
        except AttributeError as e:
            msg = "Wrapped array has no attribute '%s'" % attrname
            raise AttributeError(msg) from e

    def bind(self, stream=0):
        return FakeCUDAArray(self._ary, stream)

    @property
    def T(self):
        return self.transpose()

    def transpose(self, axes=None):
        return FakeCUDAArray(np.transpose(self._ary, axes=axes))

    def __getitem__(self, idx):
        ret = self._ary_access.__getitem__(idx)
        if np.issubdtype(type(ret), np.number) or np.issubdtype(type(ret), np.bool_):
            return ret
        else:
            return FakeCUDAArray(ret, stream=self.stream)

    def __setitem__(self, idx, val):
        return self._ary_access.__setitem__(idx, val)

    def copy_to_host(self, ary=None, stream=0):
        if ary is None:
            ary = np.empty_like(self._ary)
        else:
            check_array_compatibility(self, ary)
        np.copyto(ary, self._ary)
        return ary

    def copy_to_device(self, ary, stream=0):
        '''
        Copy from the provided array into this array.

        This may be less forgiving than the CUDA Python implementation, which
        will copy data up to the length of the smallest of the two arrays,
        whereas this expects the size of the arrays to be equal.
        '''
        sentry_contiguous(self)
        self_core, ary_core = array_core(self), array_core(ary)
        if isinstance(ary, FakeCUDAArray):
            sentry_contiguous(ary)
            check_array_compatibility(self_core, ary_core)
        else:
            ary_core = np.array(
                ary_core,
                order='C' if self_core.flags['C_CONTIGUOUS'] else 'F',
{'fraction_non_alphanumeric': 0.08756780693014503, 'fraction_numerical': 0.001771283073176132, 'mean_word_length': 2.4586523736600308, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3822231', 'n_tokens_mistral': 2737, 'n_tokens_neox': 2700, 'n_words': 777}