Python cupy.zeros() Examples
The following are 30
code examples of cupy.zeros().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cupy
, or try the search function
.
Example #1
Source File: gla_gpu.py From Deep_VoiceChanger with MIT License | 6 votes |
def auto_inverse(self, whole_spectrum): whole_spectrum = np.copy(whole_spectrum).astype(complex) whole_spectrum[whole_spectrum < 1] = 1 overwrap = self.buffer_size * 2 height = whole_spectrum.shape[0] parallel_dif = (height-overwrap) // self.parallel if height < self.parallel*overwrap: raise Exception('voice length is too small to use gpu, or parallel number is too big') spec = [self.inverse(whole_spectrum[range(i, i+parallel_dif*self.parallel, parallel_dif), :]) for i in tqdm.tqdm(range(parallel_dif+overwrap))] spec = spec[overwrap:] spec = np.concatenate(spec, axis=1) spec = spec.reshape(-1, self.wave_len) #Below code don't consider wave_len and wave_dif, I'll fix. wave = np.fft.ifft(spec, axis=1).real pad = np.zeros((wave.shape[0], 2), dtype=float) wave = np.concatenate([wave, pad], axis=1) dst = np.zeros((wave.shape[0]+3)*self.wave_dif, dtype=float) for i in range(4): w = wave[range(i, wave.shape[0], 4),:] w = w.reshape(-1) dst[i*self.wave_dif:i*self.wave_dif+len(w)] += w return dst*0.5
Example #2
Source File: gla_gpu.py From Deep_VoiceChanger with MIT License | 6 votes |
def __init__(self, parallel, wave_len=254, wave_dif=64, buffer_size=5, loop_num=5, window=np.hanning(254)): self.wave_len = wave_len self.wave_dif = wave_dif self.buffer_size = buffer_size self.loop_num = loop_num self.parallel = parallel self.window = cp.array([window for _ in range(parallel)]) self.wave_buf = cp.zeros((parallel, wave_len+wave_dif), dtype=float) self.overwrap_buf = cp.zeros((parallel, wave_dif*buffer_size+(wave_len-wave_dif)), dtype=float) self.spectrum_buffer = cp.ones((parallel, self.buffer_size, self.wave_len), dtype=complex) self.absolute_buffer = cp.ones((parallel, self.buffer_size, self.wave_len), dtype=complex) self.phase = cp.zeros((parallel, self.wave_len), dtype=complex) self.phase += cp.random.random((parallel, self.wave_len))-0.5 + cp.random.random((parallel, self.wave_len))*1j - 0.5j self.phase[self.phase == 0] = 1 self.phase /= cp.abs(self.phase)
Example #3
Source File: non_maximum_suppression.py From chainer-compiler with MIT License | 6 votes |
def _non_maximum_suppression_gpu(bbox, thresh, score=None, limit=None): if len(bbox) == 0: return cp.zeros((0,), dtype=np.int32) n_bbox = bbox.shape[0] if score is not None: order = score.argsort()[::-1].astype(np.int32) else: order = cp.arange(n_bbox, dtype=np.int32) sorted_bbox = bbox[order, :] selec, n_selec = _call_nms_kernel( sorted_bbox, thresh) selec = selec[:n_selec] selec = order[selec] if limit is not None: selec = selec[:limit] return selec
Example #4
Source File: non_maximum_suppression.py From chainer-compiler with MIT License | 6 votes |
def _call_nms_kernel(bbox, thresh): assert False, "Not supported." n_bbox = bbox.shape[0] threads_per_block = 64 col_blocks = np.ceil(n_bbox / threads_per_block).astype(np.int32) blocks = (col_blocks, col_blocks, 1) threads = (threads_per_block, 1, 1) mask_dev = cp.zeros((n_bbox * col_blocks,), dtype=np.uint64) bbox = cp.ascontiguousarray(bbox, dtype=np.float32) kern = cp.RawKernel(_nms_gpu_code, 'nms_kernel') kern(blocks, threads, args=(cp.int32(n_bbox), cp.float32(thresh), bbox, mask_dev)) mask_host = mask_dev.get() selection, n_selec = _nms_gpu_post( mask_host, n_bbox, threads_per_block, col_blocks) return selection, n_selec
Example #5
Source File: non_maximum_suppression.py From FATE with Apache License 2.0 | 6 votes |
def _non_maximum_suppression_gpu(bbox, thresh, score=None, limit=None): if len(bbox) == 0: return cp.zeros((0,), dtype=np.int32) n_bbox = bbox.shape[0] if score is not None: order = score.argsort()[::-1].astype(np.int32) else: order = cp.arange(n_bbox, dtype=np.int32) sorted_bbox = bbox[order, :] selec, n_selec = _call_nms_kernel( sorted_bbox, thresh) selec = selec[:n_selec] selec = order[selec] if limit is not None: selec = selec[:limit] return cp.asnumpy(selec)
Example #6
Source File: objectholder.py From mars with Apache License 2.0 | 6 votes |
def __init__(self, size_limit=0, device_id=None): super().__init__(size_limit=size_limit) if device_id is not None: os.environ['CUDA_VISIBLE_DEVICES'] = str(device_id) # warm up cupy try: import cupy cupy.zeros((10, 10)).sum() except ImportError: pass # warm up cudf try: import cudf import numpy as np import pandas as pd cudf.from_pandas(pd.DataFrame(np.zeros((10, 10)))) except ImportError: pass
Example #7
Source File: non_maximum_suppression.py From FATE with Apache License 2.0 | 6 votes |
def _call_nms_kernel(bbox, thresh): # PyTorch does not support unsigned long Tensor. # Doesn't matter,since it returns ndarray finally. # So I'll keep it unmodified. n_bbox = bbox.shape[0] threads_per_block = 64 col_blocks = np.ceil(n_bbox / threads_per_block).astype(np.int32) blocks = (col_blocks, col_blocks, 1) threads = (threads_per_block, 1, 1) mask_dev = cp.zeros((n_bbox * col_blocks,), dtype=np.uint64) bbox = cp.ascontiguousarray(bbox, dtype=np.float32) kern = _load_kernel('nms_kernel', _nms_gpu_code) kern(blocks, threads, args=(cp.int32(n_bbox), cp.float32(thresh), bbox, mask_dev)) mask_host = mask_dev.get() selection, n_selec = _nms_gpu_post( mask_host, n_bbox, threads_per_block, col_blocks) return selection, n_selec
Example #8
Source File: kmeans.py From cupy with MIT License | 6 votes |
def fit_custom(X, n_clusters, max_iter): assert X.ndim == 2 n_samples = len(X) pred = cupy.zeros(n_samples) initial_indexes = cupy.random.choice(n_samples, n_clusters, replace=False) centers = X[initial_indexes] for _ in range(max_iter): distances = var_kernel(X[:, None, 0], X[:, None, 1], centers[None, :, 1], centers[None, :, 0]) new_pred = cupy.argmin(distances, axis=1) if cupy.all(new_pred == pred): break pred = new_pred i = cupy.arange(n_clusters) mask = pred == i[:, None] sums = sum_kernel(X, mask[:, :, None], axis=1) counts = count_kernel(mask, axis=1).reshape((n_clusters, 1)) centers = sums / counts return centers, pred
Example #9
Source File: compressed.py From cupy with MIT License | 6 votes |
def getnnz(self, axis=None): """Returns the number of stored values, including explicit zeros. Args: axis: Not supported yet. Returns: int: The number of stored values. """ if axis is None: return self.data.size else: raise ValueError # TODO(unno): Implement sorted_indices
Example #10
Source File: dia.py From cupy with MIT License | 6 votes |
def getnnz(self, axis=None): """Returns the number of stored values, including explicit zeros. Args: axis: Not supported yet. Returns: int: The number of stored values. """ if axis is not None: raise NotImplementedError( 'getnnz over an axis is not implemented for DIA format') m, n = self.shape nnz = core.ReductionKernel( 'int32 offsets, int32 m, int32 n', 'int32 nnz', 'offsets > 0 ? min(m, n - offsets) : min(m + offsets, n)', 'a + b', 'nnz = a', '0', 'dia_nnz')(self.offsets, m, n) return int(nnz)
Example #11
Source File: dia.py From cupy with MIT License | 6 votes |
def diagonal(self, k=0): """Returns the k-th diagonal of the matrix. Args: k (int, optional): Which diagonal to get, corresponding to elements a[i, i+k]. Default: 0 (the main diagonal). Returns: cupy.ndarray : The k-th diagonal. """ rows, cols = self.shape if k <= -rows or k >= cols: return cupy.empty(0, dtype=self.data.dtype) idx, = cupy.nonzero(self.offsets == k) first_col, last_col = max(0, k), min(rows + k, cols) if idx.size == 0: return cupy.zeros(last_col - first_col, dtype=self.data.dtype) return self.data[idx[0], first_col:last_col]
Example #12
Source File: measurements.py From cupy with MIT License | 6 votes |
def _label(x, structure, y): elems = numpy.where(structure != 0) vecs = [elems[dm] - 1 for dm in range(x.ndim)] offset = vecs[0] for dm in range(1, x.ndim): offset = offset * 3 + vecs[dm] indxs = numpy.where(offset < 0)[0] dirs = [[vecs[dm][dr] for dm in range(x.ndim)] for dr in indxs] dirs = cupy.array(dirs, dtype=numpy.int32) ndirs = indxs.shape[0] y_shape = cupy.array(y.shape, dtype=numpy.int32) count = cupy.zeros(2, dtype=numpy.int32) _kernel_init()(x, y) _kernel_connect()(y_shape, dirs, ndirs, x.ndim, y, size=y.size) _kernel_count()(y, count, size=y.size) maxlabel = int(count[0]) labels = cupy.empty(maxlabel, dtype=numpy.int32) _kernel_labels()(y, count, labels, size=y.size) _kernel_finalize()(maxlabel, cupy.sort(labels), y, size=y.size) return maxlabel
Example #13
Source File: test_ndarray_cuda_array_interface.py From cupy with MIT License | 6 votes |
def test_shape_with_strides(self, dtype, order): x = cupy.zeros(self.shape, dtype=dtype, order=order) start = [s.start for s in self.slices] itemsize = cupy.dtype(dtype).itemsize dimsize = [s * itemsize for s in start] if len(self.shape) == 1: offset = start[0] * itemsize else: if order == 'C': offset = self.shape[0] * dimsize[0] + dimsize[1] else: offset = self.shape[0] * dimsize[1] + dimsize[0] cai_ptr, _ = x.__cuda_array_interface__['data'] slice_cai_ptr, _ = x[self.slices].__cuda_array_interface__['data'] cupy_data_ptr = x.data.ptr sliced_cupy_data_ptr = x[self.slices].data.ptr assert cai_ptr == cupy_data_ptr assert slice_cai_ptr == sliced_cupy_data_ptr assert slice_cai_ptr == cai_ptr+offset
Example #14
Source File: test_ndarray_scatter.py From cupy with MIT License | 6 votes |
def test_scatter_minmax_differnt_dtypes_mask(self, src_dtype, dst_dtype): shape = (2, 3) a = cupy.zeros(shape, dtype=src_dtype) value = cupy.array(1, dtype=dst_dtype) slices = (numpy.array([[True, False, False], [False, True, True]])) a.scatter_max(slices, value) numpy.testing.assert_almost_equal( a.get(), numpy.array([[1, 0, 0], [0, 1, 1]], dtype=src_dtype)) a = cupy.ones(shape, dtype=src_dtype) value = cupy.array(0, dtype=dst_dtype) a.scatter_min(slices, value) numpy.testing.assert_almost_equal( a.get(), numpy.array([[0, 1, 1], [1, 0, 0]], dtype=src_dtype))
Example #15
Source File: test_ndarray_scatter.py From cupy with MIT License | 6 votes |
def test_scatter_minmax_differnt_dtypes(self, src_dtype, dst_dtype): shape = (2, 3) a = cupy.zeros(shape, dtype=src_dtype) value = cupy.array(1, dtype=dst_dtype) slices = ([1, 1], slice(None)) a.scatter_max(slices, value) numpy.testing.assert_almost_equal( a.get(), numpy.array([[0, 0, 0], [1, 1, 1]], dtype=src_dtype)) a = cupy.ones(shape, dtype=src_dtype) value = cupy.array(0, dtype=dst_dtype) a.scatter_min(slices, value) numpy.testing.assert_almost_equal( a.get(), numpy.array([[1, 1, 1], [0, 0, 0]], dtype=src_dtype))
Example #16
Source File: test_ndarray.py From cupy with MIT License | 6 votes |
def test_cuda_array_interface_view(self): arr = cupy.zeros(shape=(10, 20), dtype=cupy.float64) view = arr[::2, ::5] iface = view.__cuda_array_interface__ assert (set(iface.keys()) == set(['shape', 'typestr', 'data', 'version', 'strides', 'descr'])) assert iface['shape'] == (5, 4) assert iface['typestr'] == '<f8' assert isinstance(iface['data'], tuple) assert len(iface['data']) == 2 assert iface['data'][0] == arr.data.ptr assert not iface['data'][1] assert iface['version'] == 2 assert iface['strides'] == (320, 40) assert iface['descr'] == [('', '<f8')]
Example #17
Source File: test_ndarray.py From cupy with MIT License | 6 votes |
def test_cuda_array_interface_zero_size(self): arr = cupy.zeros(shape=(10,), dtype=cupy.float64) view = arr[0:3:-1] iface = view.__cuda_array_interface__ assert (set(iface.keys()) == set(['shape', 'typestr', 'data', 'version', 'strides', 'descr'])) assert iface['shape'] == (0,) assert iface['typestr'] == '<f8' assert isinstance(iface['data'], tuple) assert len(iface['data']) == 2 assert iface['data'][0] == 0 assert not iface['data'][1] assert iface['version'] == 2 assert iface['strides'] is None assert iface['descr'] == [('', '<f8')]
Example #18
Source File: test_kind.py From cupy with MIT License | 5 votes |
def test_require_flag_check(self, dtype): possible_flags = [['C_CONTIGUOUS'], ['F_CONTIGUOUS']] x = cupy.zeros((2, 3, 4), dtype) for flags in possible_flags: arr = cupy.require(x, dtype, flags) for parameter in flags: assert arr.flags[parameter] assert arr.dtype == dtype
Example #19
Source File: test_raw.py From cupy with MIT License | 5 votes |
def test_grid_sync_rawmodule(self): n = self.n x1 = cupy.arange(n ** 2, dtype='float32').reshape(n, n) x2 = cupy.ones((n, n), dtype='float32') y = cupy.zeros((n, n), dtype='float32') kern = self.mod_grid_sync.get_function('test_grid_sync') kern((n,), (n,), (x1, x2, y, n ** 2)) assert cupy.allclose(y, x1 + x2)
Example #20
Source File: test_raw.py From cupy with MIT License | 5 votes |
def test_grid_sync_rawkernel(self): n = self.n x1 = cupy.arange(n ** 2, dtype='float32').reshape(n, n) x2 = cupy.ones((n, n), dtype='float32') y = cupy.zeros((n, n), dtype='float32') self.kern_grid_sync((n,), (n,), (x1, x2, y, n ** 2)) assert cupy.allclose(y, x1 + x2)
Example #21
Source File: test_raw.py From cupy with MIT License | 5 votes |
def test_dynamical_parallelism_compile_failure(self): # no option for separate compilation is given should cause an error ker = cupy.RawKernel(_test_source4, 'test_kernel', backend=self.backend) N = 10 inner_chunk = 2 x = cupy.zeros((N,), dtype=cupy.float32) if self.backend == 'nvrtc': # raised when calling ls.complete() with pytest.raises(cupy.cuda.driver.CUDADriverError): ker((1,), (N//inner_chunk,), (x, N, inner_chunk)) else: # nvcc with pytest.raises(cupy.cuda.compiler.CompileException): ker((1,), (N//inner_chunk,), (x, N, inner_chunk))
Example #22
Source File: test_raw.py From cupy with MIT License | 5 votes |
def _helper(self, kernel, dtype): N = 10 x1 = cupy.arange(N**2, dtype=dtype).reshape(N, N) x2 = cupy.ones((N, N), dtype=dtype) y = cupy.zeros((N, N), dtype=dtype) kernel((N,), (N,), (x1, x2, y, N**2)) return x1, x2, y
Example #23
Source File: test_ndarray_scatter.py From cupy with MIT License | 5 votes |
def test_scatter_minmax_cupy_arguments_mask(self, dtype): shape = (2, 3) a = cupy.zeros(shape, dtype) slices = (cupy.array([True, False]), slice(None)) a.scatter_max(slices, cupy.array(1.)) testing.assert_array_equal( a, cupy.array([[1., 1., 1.], [0., 0., 0.]], dtype)) a = cupy.ones(shape, dtype) a.scatter_min(slices, cupy.array(0.)) testing.assert_array_equal( a, cupy.array([[0., 0., 0.], [1., 1., 1.]], dtype))
Example #24
Source File: test_ndarray_scatter.py From cupy with MIT License | 5 votes |
def test_scatter_minmax_cupy_arguments(self, dtype): shape = (2, 3) a = cupy.zeros(shape, dtype) slices = (cupy.array([1, 1]), slice(None)) a.scatter_max(slices, cupy.array(1.)) testing.assert_array_equal( a, cupy.array([[0., 0., 0.], [1., 1., 1.]], dtype)) a = cupy.ones(shape, dtype) a.scatter_min(slices, cupy.array(0.)) testing.assert_array_equal( a, cupy.array([[1., 1., 1.], [0., 0., 0.]], dtype))
Example #25
Source File: test_ndarray_scatter.py From cupy with MIT License | 5 votes |
def test_scatter_add_differnt_dtypes(self, src_dtype, dst_dtype): shape = (2, 3) a = cupy.zeros(shape, dtype=src_dtype) value = cupy.array(1, dtype=dst_dtype) slices = ([1, 1], slice(None)) a.scatter_add(slices, value) numpy.testing.assert_almost_equal( a.get(), numpy.array([[0, 0, 0], [2, 2, 2]], dtype=src_dtype))
Example #26
Source File: test_ndarray_scatter.py From cupy with MIT License | 5 votes |
def test_scatter_add_cupy_arguments_mask(self, dtype): shape = (2, 3) a = cupy.zeros(shape, dtype) slices = (cupy.array([True, False]), slice(None)) a.scatter_add(slices, cupy.array(1.)) testing.assert_array_equal( a, cupy.array([[1., 1., 1.], [0., 0., 0.]], dtype))
Example #27
Source File: test_ndarray_scatter.py From cupy with MIT License | 5 votes |
def test_scatter_add_cupy_arguments(self, dtype): shape = (2, 3) a = cupy.zeros(shape, dtype) slices = (cupy.array([1, 1]), slice(None)) a.scatter_add(slices, cupy.array(1.)) testing.assert_array_equal( a, cupy.array([[0., 0., 0.], [2., 2., 2.]], dtype))
Example #28
Source File: test_ndarray_scatter.py From cupy with MIT License | 5 votes |
def test_scatter_min(self, xp, dtype): a = xp.zeros(self.shape, dtype) if xp is cupy: a.scatter_min(self.slices, self.value) else: numpy.minimum.at(a, self.slices, self.value) return a
Example #29
Source File: test_ndarray_scatter.py From cupy with MIT License | 5 votes |
def test_scatter_max(self, xp, dtype): a = xp.zeros(self.shape, dtype) if xp is cupy: a.scatter_max(self.slices, self.value) else: numpy.maximum.at(a, self.slices, self.value) return a
Example #30
Source File: test_scan.py From cupy with MIT License | 5 votes |
def test_multi_gpu(self): with cuda.Device(0): a = cupy.zeros((10,)) scan(a) with cuda.Device(1): a = cupy.zeros((10,)) scan(a)