def test_is_close_scalar_scalar(self, dtype): # cupy.isclose always returns ndarray a = cupy.dtype(cupy.dtype).type(0) b = cupy.dtype(cupy.dtype).type(0) cond = cupy.isclose(a, b) assert cond.shape == () assert bool(cond)
def randint(self, low, high=None, size=None, dtype='l'): """Returns a scalar or an array of integer values over ``[low, high)``. .. seealso:: :func:`cupy.random.randint` for full documentation, :meth:`numpy.random.RandomState.randint` """ if high is None: lo = 0 hi = low else: lo = low hi = high if lo >= hi: raise ValueError('low >= high') if lo < cupy.iinfo(dtype).min: raise ValueError('low is out of bounds for {}'.format( cupy.dtype(dtype).name)) if hi > cupy.iinfo(dtype).max + 1: raise ValueError('high is out of bounds for {}'.format( cupy.dtype(dtype).name)) diff = hi - lo - 1 if diff > cupy.iinfo(cupy.int32).max - cupy.iinfo(cupy.int32).min + 1: raise NotImplementedError( 'Sampling from a range whose extent is larger than int32 ' 'range is currently not supported') x = self._interval(diff, size).astype(dtype, copy=False) cupy.add(x, lo, out=x) return x
def randint(self, low, high=None, size=None, dtype='l'): """Returns a scalar or an array of integer values over ``[low, high)``. .. seealso:: :func:`cupy.random.randint` for full documentation, :meth:`numpy.random.RandomState.randint <numpy.random.mtrand.RandomState.randint>` """ if high is None: lo = 0 hi1 = int(low) - 1 else: lo = int(low) hi1 = int(high) - 1 if lo > hi1: raise ValueError('low >= high') if lo < cupy.iinfo(dtype).min: raise ValueError('low is out of bounds for {}'.format( cupy.dtype(dtype).name)) if hi1 > cupy.iinfo(dtype).max: raise ValueError('high is out of bounds for {}'.format( cupy.dtype(dtype).name)) diff = hi1 - lo x = self._interval(diff, size).astype(dtype, copy=False) cupy.add(x, lo, out=x) return x
def test_range_extra_dtypes(dtype_in, dt): """Test code paths that are not skipped by `test_range`""" imin, imax = dtype_range_extra[dtype_in] x = cp.linspace(imin, imax, 10).astype(dtype_in) y = _convert(x, dt) omin, omax = dtype_range_extra[dt] _verify_range("From %s to %s" % (cp.dtype(dtype_in), cp.dtype(dt)), y, omin, omax, cp.dtype(dt))
def _check_array_contiguity(ary): """ Check if array-like ary is contioguous. Parameters ---------- ary: __cuda_array_interface__ or __array_interface__ compliant array. """ if hasattr(ary, 'ndim'): if ary.ndim == 1: return True # Use contiguity flags if present if hasattr(ary, 'flags'): if ary.flags['C_CONTIGUOUS'] or ary.flags['F_CONTIGUOUS']: return True else: return False # Check contiguity from shape and strides if not else: if hasattr(ary, "__array_interface__"): ary_interface = ary.__array_interface__ elif hasattr(ary, "__cuda_array_interface__"): ary_interface = ary.__cuda_array_interface__ else: raise TypeError("No array_interface attribute detected in input. ") # if the strides are not set or none, then the array is C-contiguous if 'strides' not in ary_interface or ary_interface['strides'] is None: return True shape = ary_interface['shape'] strides = ary_interface['strides'] dtype = cp.dtype(ary_interface['typestr']) order = _strides_to_order(strides, dtype) itemsize = cp.dtype(dtype).itemsize # We check if the strides jump on the non contiguous dimension # does not correspond to the array dimension size, which indicates # this is a view to a non contiguous array. if order == 'F': if (shape[0] * itemsize) != strides[1]: return False elif order == 'C': if (shape[1] * itemsize) != strides[0]: return False return True
def _complex_dtype(dtype): """Patched version of :func:`sporco.fft.complex_dtype`.""" real_cplx = {'float128': 'complex256', 'float64': 'complex128', 'float32': 'complex64'} dt = cp.dtype(dtype) for real, cplx in real_cplx.items(): try: cpdt = cp.dtype(real) except TypeError: continue if dt == cpdt: return cp.dtype(cplx) return cp.dtype('complex64')
def test_backward_fft(self, dtype): t = dtype idtype = odtype = edtype = cupy.dtype(t) shape = self.shape length = cupy.core.internal.prod(shape[1:]) a = testing.shaped_random(shape, cupy, dtype) out = cupy.empty_like(a) plan = cufft.XtPlanNd(shape[1:], shape[1:], 1, length, idtype, shape[1:], 1, length, odtype, shape[0], edtype, order='C', last_axis=-1, last_size=None) plan.fft(a, out, cufft.CUFFT_INVERSE) if len(shape) <= 2: out_cp = cupy.fft.ifft(a) else: out_cp = cupy.fft.ifftn(a, axes=(-1, -2)) testing.assert_allclose(out / length, out_cp)
def test_chainerx_to_cupy_noncontiguous(): dtype = 'float32' a_chx = chainerx.arange( 12, dtype=dtype, device='cuda:0').reshape((2, 6))[::-1, ::2] offset = a_chx.offset # test preconditions assert offset > 0 assert not a_chx.is_contiguous a_cupy = cupy.ndarray( a_chx.shape, cupy.dtype(a_chx.dtype.name), cupy.cuda.MemoryPointer(cupy.cuda.UnownedMemory( a_chx.data_ptr, a_chx.data_size, a_chx, 0), offset), strides=a_chx.strides, ) assert a_chx.strides == a_cupy.strides chainerx.testing.assert_array_equal_ex( a_chx, a_cupy.get(), strides_check=False) a_cupy[1, 1] = 53 assert a_chx.strides == a_cupy.strides chainerx.testing.assert_array_equal_ex( a_chx, a_cupy.get(), strides_check=False)
def fit(self, y, _classes=None): """ Fit a LabelEncoder (nvcategory) instance to a set of categories Parameters ---------- y : cudf.Series Series containing the categories to be encoded. It's elements may or may not be unique _classes: int or None. Passed by the dask client when dask LabelEncoder is used. Returns ------- self : LabelEncoder A fitted instance of itself to allow method chaining """ self._validate_keywords() self.dtype = y.dtype if y.dtype != cp.dtype('O') else str if _classes is not None: self.classes_ = _classes else: self.classes_ = y.unique() # dedupe and sort self._fitted = True return self
def test_range(dtype, f_and_dt): imin, imax = dtype_range[dtype] x = cp.linspace(imin, imax, 10).astype(dtype) f, dt = f_and_dt y = f(x) omin, omax = dtype_range[dt] if imin == 0 or omin == 0: omin = 0 imin = 0 _verify_range("From %s to %s" % (cp.dtype(dtype), cp.dtype(dt)), y, omin, omax, np.dtype(dt))
def _safely_castable_to_int(dt): """Test whether the NumPy data type `dt` can be safely cast to an int.""" int_size = cupy.dtype(int).itemsize safe = ( cupy.issubdtype(dt, cupy.signedinteger) and dt.itemsize <= int_size ) or (cupy.issubdtype(dt, cupy.unsignedinteger) and dt.itemsize < int_size) return safe
def _unpack(binary, dtype, endianness): data_size = cp.dtype(dtype).itemsize // binary.dtype.itemsize out_size = binary.shape[0] // data_size out = cp.empty_like(binary, dtype=dtype, shape=out_size) if endianness == "B": little = False else: little = True threadsperblock, blockspergrid = _get_tpb_bpg() k_type = "unpack" _populate_kernel_cache(out.dtype, k_type) kernel = _get_backend_kernel( out.dtype, blockspergrid, threadsperblock, k_type, ) kernel(out_size, little, binary, out) _print_atts(kernel) # Remove binary data del binary return out
def test_chainerx_to_cupy_noncontiguous(): dtype = 'float32' a_chx = chainerx.arange(12, dtype=dtype, device='cuda:0').reshape( (2, 6))[::-1, ::2] offset = a_chx.offset # test preconditions assert offset > 0 assert not a_chx.is_contiguous a_cupy = cupy.ndarray( a_chx.shape, cupy.dtype(a_chx.dtype.name), cupy.cuda.MemoryPointer( cupy.cuda.UnownedMemory(a_chx.data_ptr, a_chx.data_size, a_chx, 0), offset), strides=a_chx.strides, ) assert a_chx.strides == a_cupy.strides chainerx.testing.assert_array_equal_ex(a_chx, a_cupy.get(), strides_check=False) a_cupy[1, 1] = 53 assert a_chx.strides == a_cupy.strides chainerx.testing.assert_array_equal_ex(a_chx, a_cupy.get(), strides_check=False)
def test_class_overriding(): with ua.set_backend(NumpyBackend, coerce=True): assert isinstance(onp.add, np.ufunc) assert isinstance(onp.dtype("float64"), np.dtype) assert np.dtype("float64") == onp.float64 assert isinstance(np.dtype("float64"), onp.dtype) assert issubclass(onp.ufunc, np.ufunc) with ua.set_backend(DaskBackend(), coerce=True): assert isinstance(da.add, np.ufunc) assert isinstance(onp.dtype("float64"), np.dtype) assert np.dtype("float64") == onp.float64 assert isinstance(np.dtype("float64"), onp.dtype) assert issubclass(da.ufunc.ufunc, np.ufunc) with ua.set_backend(SparseBackend, coerce=True): assert isinstance(onp.add, np.ufunc) assert isinstance(onp.dtype("float64"), np.dtype) assert np.dtype("float64") == onp.float64 assert isinstance(np.dtype("float64"), onp.dtype) assert issubclass(onp.ufunc, np.ufunc) if hasattr(CupyBackend, "__ua_function__"): with ua.set_backend(CupyBackend, coerce=True): assert isinstance(cp.add, np.ufunc) assert isinstance(cp.dtype("float64"), np.dtype) assert np.dtype("float64") == cp.float64 assert isinstance(np.dtype("float64"), cp.dtype) assert issubclass(cp.ufunc, np.ufunc)
def from_pycuda(pycuda_arr, device=0): """Read in gpuarray from PyCUDA and output CuPy array Parameters ---------- pycuda_arr : PyCUDA gpuarray device : int GPU Device ID Returns ------- cupy_arr : CuPy ndarray """ cupy_arr = cp.ndarray( pycuda_arr.shape, cp.dtype(pycuda_arr.dtype), cp.cuda.MemoryPointer( cp.cuda.UnownedMemory( pycuda_arr.ptr, pycuda_arr.size, pycuda_arr, device ), 0, ), strides=pycuda_arr.strides, ) return cupy_arr
def test_chainerx_to_cupy_contiguous(): dtype = 'float32' a_chx = chainerx.arange(6, dtype=dtype, device='cuda:0').reshape((2, 3)) a_cupy = cupy.ndarray( a_chx.shape, cupy.dtype(a_chx.dtype.name), cupy.cuda.MemoryPointer(cupy.cuda.UnownedMemory( a_chx.data_ptr + a_chx.offset, a_chx.data_size, a_chx, 0), 0), strides=a_chx.strides, ) assert a_cupy.device.id == 0 chainerx.testing.assert_array_equal_ex(a_chx, a_cupy.get()) # Write to a_cupy a_cupy[0, 1] = 8 chainerx.testing.assert_array_equal_ex( a_chx, numpy.array([[0, 8, 2], [3, 4, 5]], dtype)) # Write to a_chx a_chx += 1 chainerx.testing.assert_array_equal_ex( a_cupy.get(), numpy.array([[1, 9, 3], [4, 5, 6]], dtype))
def randint(low, high=None, size=None, dtype='l'): """Returns a scalar or an array of integer values over ``[low, high)``. Each element of returned values are independently sampled from uniform distribution over left-close and right-open interval ``[low, high)``. Args: low (int): If ``high`` is not ``None``, it is the lower bound of the interval. Otherwise, it is the **upper** bound of the interval and lower bound of the interval is set to ``0``. high (int): Upper bound of the interval. size (None or int or tuple of ints): The shape of returned value. dtype: Data type specifier. Returns: int or cupy.ndarray of ints: If size is ``None``, it is single integer sampled. If size is integer, it is the 1D-array of length ``size`` element. Otherwise, it is the array whose shape specified by ``size``. """ if high is None: lo = 0 hi = low else: lo = low hi = high if lo >= hi: raise ValueError('low >= high') if lo < cupy.iinfo(dtype).min: raise ValueError('low is out of bounds for {}'.format( cupy.dtype(dtype).name)) if hi > cupy.iinfo(dtype).max + 1: raise ValueError('high is out of bounds for {}'.format( cupy.dtype(dtype).name)) diff = hi - lo - 1 if diff > cupy.iinfo(cupy.int32).max - cupy.iinfo(cupy.int32).min + 1: raise NotImplementedError( 'Sampling from a range whose extent is larger than int32 range is ' 'currently not supported') rs = generator.get_random_state() x = rs.interval(diff, size).astype(dtype, copy=False) cupy.add(x, lo, out=x) return x
def random(m, n, density=0.01, format='coo', dtype=None, random_state=None, data_rvs=None): """Generates a random sparse matrix. This function generates a random sparse matrix. First it selects non-zero elements with given density ``density`` from ``(m, n)`` elements. So the number of non-zero elements ``k`` is ``k = m * n * density``. Value of each element is selected with ``data_rvs`` function. Args: m (int): Number of rows. n (int): Number of cols. density (float): Ratio of non-zero entries. format (str): Matrix format. dtype (dtype): Type of the returned matrix values. random_state (cupy.random.RandomState or int): State of random number generator. If an integer is given, the method makes a new state for random number generator and uses it. If it is not given, the default state is used. This state is used to generate random indexes for nonzero entries. data_rvs (callable): A function to generate data for a random matrix. If it is not given, `random_state.rand` is used. Returns: cupy.sparse.spmatrix: Generated matrix. .. seealso:: :func:`scipy.sparse.random` """ if density < 0 or density > 1: raise ValueError('density expected to be 0 <= density <= 1') dtype = cupy.dtype(dtype) if dtype.char not in 'fd': raise NotImplementedError('type %s not supported' % dtype) mn = m * n k = int(density * m * n) if random_state is None: random_state = cupy.random elif isinstance(random_state, (int, cupy.integer)): random_state = cupy.random.RandomState(random_state) if data_rvs is None: data_rvs = random_state.rand ind = random_state.choice(mn, size=k, replace=False) j = cupy.floor(ind * (1. / m)).astype('i') i = ind - j * m vals = data_rvs(k).astype(dtype) return cupy.sparse.coo_matrix((vals, (i, j)), shape=(m, n)).asformat(format)
def setup(self): self.dt = np.dtype([('a', 'f4', 256)]) self.A = np.zeros((), self.dt) self.B = self.A.copy() self.a = np.zeros(1, self.dt)[0] self.b = self.a.copy()
def judge_dtype(dtype): if dtype is None: dtype = common.default_dtype dtype = cp.dtype(dtype) if dtype.kind == 'f': return dtype else: raise TypeError('dtype must be floating point')
def _complex_dtype(dtype): """Patched version of :func:`mysporco.linalg.complex_dtype`.""" dt = cp.dtype(dtype) # print(dtype) # print(dt) # if dt == cp.dtype('float128'): # return cp.dtype('complex256') # elif dt == cp.dtype('float64'): # return cp.dtype('complex128') # else: # return cp.dtype('complex64') # if dt == cp.dtype('longdouble'): # return cp.dtype('clongdouble') if dt == cp.dtype('float64'): return cp.dtype('complex128') else: return cp.dtype('complex64')
def wrap_if_boxes_inside(raw_array, slow_op_name=None): if raw_array.dtype is _cp.dtype("O"): if slow_op_name: warnings.warn("{0} is slow for array inputs. " "np.concatenate() is faster.".format(slow_op_name)) return array_from_args((), {}, *raw_array.ravel()).reshape(raw_array.shape) else: return raw_array
def read_bin(file, buffer=None, dtype=cp.uint8, num_samples=None, offset=0): """ Reads binary file into GPU memory. Can be used as a building blocks for custom unpack/pack data readers/writers. Parameters ---------- file : str A string of filename to be read to GPU. buffer : ndarray, optional Pinned memory buffer to use when copying data from GPU. dtype : data-type, optional Any object that can be interpreted as a numpy data type. num_samples : int, optional Number of samples to be loaded to GPU. If set to 0, read in all samples. offset : int, optional In the file, array data starts at this offset. Since offset is measured in bytes, it should normally be a multiple of the byte-size of dtype. Returns ------- out : ndarray An 1-dimensional array containing binary data. """ # Get current stream, default or not. stream = cp.cuda.get_current_stream() # offset is measured in bytes offset *= cp.dtype(dtype).itemsize fp = np.memmap(file, mode="r", offset=offset, shape=num_samples, dtype=dtype) if buffer is not None: out = cp.empty(buffer.shape, buffer.dtype) if buffer is None: out = cp.asarray(fp) else: buffer[:] = fp[:] out.set(buffer) stream.synchronize() del fp return out
def test_float_conversion_dtype(): """Test any convertion from a float dtype to an other.""" x = cp.array([-1, 1]) # Test all combinations of dtypes convertions dtype_combin = np.array(np.meshgrid(float_dtype_list, float_dtype_list)).T.reshape(-1, 2) for dtype_in, dtype_out in dtype_combin: x = x.astype(dtype_in) y = _convert(x, dtype_out) assert y.dtype == cp.dtype(dtype_out)
def _check_nan_inf(x, dtype, neg=None): if dtype.char in 'FD': dtype = cupy.dtype(dtype.char.lower()) if dtype.char not in 'efd': x = 0 elif x is None and neg is not None: x = cupy.finfo(dtype).min if neg else cupy.finfo(dtype).max elif cupy.isnan(x): x = cupy.nan elif cupy.isinf(x): x = cupy.inf * (-1)**(x < 0) return cupy.asanyarray(x, dtype)
def __init__(self, dtype, shape): """Initialize this :class:`LinearOperator` """ if dtype is not None: dtype = cupy.dtype(dtype) shape = tuple(shape) if not _util.isshape(shape): raise ValueError('invalid shape %r (must be 2-d)' % (shape, )) self.dtype = dtype self.shape = shape
def test_float_conversion_dtype_warns(): """Test that convert issues a warning when called""" x = np.array([-1, 1]) # Test all combinations of dtypes convertions dtype_combin = np.array(np.meshgrid(float_dtype_list, float_dtype_list)).T.reshape(-1, 2) for dtype_in, dtype_out in dtype_combin: x = x.astype(dtype_in) with expected_warnings(["The use of this function is discouraged"]): y = convert(x, dtype_out) assert y.dtype == cp.dtype(dtype_out)
def _get_weights_dtype(input, weights, dtype_mode): if weights.dtype.kind == "c" or input.dtype.kind == "c": if dtype_mode == "ndimage": weights_dtype = cupy.complex128 elif dtype_mode == "float": weights_dtype = cupy.promote_types(input.real.dtype, cupy.complex64) else: if dtype_mode == "ndimage": weights_dtype = cupy.float64 elif dtype_mode == "float": weights_dtype = cupy.promote_types(input.real.dtype, cupy.float32) weights_dtype = cupy.dtype(weights_dtype) return weights_dtype
def fit_transform(self, y: cudf.Series) -> cudf.Series: """ Simultaneously fit and transform an input This is functionally equivalent to (but faster than) `LabelEncoder().fit(y).transform(y)` """ self.dtype = y.dtype if y.dtype != cp.dtype('O') else str y = y.astype('category') self.classes_ = y._column.categories self._fitted = True return cudf.Series(y._column.codes, index=y.index)
def _get_output(output, input, shape=None, complex_output=False): shape = input.shape if shape is None else shape if output is None: if complex_output: _dtype = cupy.promote_types(input.dtype, cupy.complex64) else: _dtype = input.dtype output = cupy.zeros(shape, dtype=_dtype) elif isinstance(output, (type, cupy.dtype)): if complex_output and cupy.dtype(output).kind != 'c': warnings.warn("promoting specified output dtype to complex") output = cupy.promote_types(output, cupy.complex64) output = cupy.zeros(shape, dtype=output) elif isinstance(output, str): output = numpy.typeDict[output] if complex_output and cupy.dtype(output).kind != 'c': raise RuntimeError("output must have complex dtype") output = cupy.zeros(shape, dtype=output) elif output.shape != shape: raise RuntimeError("output shape not correct") elif complex_output and output.dtype.kind != 'c': raise RuntimeError("output must have complex dtype") return output
def cuda_serialize_cupy_ndarray(x): # Making sure `x` is behaving if not (x.flags["C_CONTIGUOUS"] or x.flags["F_CONTIGUOUS"]): x = cupy.array(x, copy=True) header = x.__cuda_array_interface__.copy() header["strides"] = tuple(x.strides) frames = [ cupy.ndarray( shape=(x.nbytes,), dtype=cupy.dtype("u1"), memptr=x.data, strides=(1,) ) ] return header, frames
def pcm2float(sig, output_dtype=cp.float64): # Make sure it's a NumPy array. sig = cp.asnumpy(cp.asarray(sig)) # Check if it is an array of signed integers. assert sig.dtype.kind == 'i', "'sig' must be an array of signed integers!" # Set the array output format. Accepts string as input argument for the # desired output format (e.g. 'f'). out_dtype = cp.dtype(output_dtype) # Note that 'min' has a greater (by 1) absolute value than 'max'! # Therefore, we use 'min' here to avoid clipping. return sig.astype(out_dtype) / out_dtype.type(-cp.iinfo(sig.dtype).min)
def _complex_dtype(dtype): """Patched version of :func:`sporco.linalg.complex_dtype`.""" dt = cp.dtype(dtype) if dt == cp.dtype('float128'): return cp.dtype('complex256') elif dt == cp.dtype('float64'): return cp.dtype('complex128') else: return cp.dtype('complex64')
def test_chainerx_to_cupy_nondefault_device(): dtype = 'float32' a_chx = chainerx.arange(6, dtype=dtype, device='cuda:1').reshape((2, 3)) a_cupy = cupy.ndarray( a_chx.shape, cupy.dtype(a_chx.dtype.name), cupy.cuda.MemoryPointer(cupy.cuda.UnownedMemory( a_chx.data_ptr + a_chx.offset, a_chx.data_size, a_chx, -1), 0), strides=a_chx.strides, ) assert a_cupy.device.id == 1 chainerx.testing.assert_array_equal_ex(a_chx, a_cupy.get())
def test_chainerx_to_cupy_delete_chainerx_first(): dtype = 'float32' a_chx = chainerx.arange(6, dtype=dtype, device='cuda:0').reshape((2, 3)) a_cupy = cupy.ndarray( a_chx.shape, cupy.dtype(a_chx.dtype.name), cupy.cuda.MemoryPointer(cupy.cuda.UnownedMemory( a_chx.data_ptr + a_chx.offset, a_chx.data_size, a_chx, 0), 0), strides=a_chx.strides, ) del a_chx a_cupy += 1 chainerx.testing.assert_array_equal_ex( a_cupy.get(), numpy.array([[1, 2, 3], [4, 5, 6]], dtype))