├── cupy ├── __init__.pxd ├── core │ ├── __init__.pxd │ ├── _dtype.pxd │ ├── include │ │ └── cupy │ │ │ ├── complex │ │ │ ├── README.md │ │ │ ├── cpow.h │ │ │ └── cproj.h │ │ │ ├── _cuda │ │ │ └── README.md │ │ │ └── atomics.cuh │ ├── raw.pxd │ ├── dlpack.pxd │ ├── _routines_logic.pxd │ ├── _kernel.pxd │ ├── _routines_sorting.pxd │ ├── _errors.py │ ├── _scalar.pxd │ ├── _routines_statistics.pxd │ ├── _routines_indexing.pxd │ ├── flags.pyx │ ├── _routines_math.pxd │ ├── _routines_logic.pyx │ ├── _ufuncs.py │ ├── _routines_manipulation.pxd │ ├── _dtype.pyx │ ├── internal.pxd │ └── raw.pyx ├── cuda │ ├── __init__.pxd │ ├── memory_hook.pxd │ ├── stream.pxd │ ├── profiler.pxd │ ├── memory_hooks │ │ └── __init__.py │ ├── cupy_common.h │ ├── common.pxd │ ├── cufft.pxd │ ├── device.pxd │ ├── nccl.pxd │ ├── cupy_cuComplex.h │ ├── function.pxd │ ├── nvrtc.pxd │ ├── pinned_memory.pxd │ ├── curand.pxd │ ├── cupy_nvrtc.h │ ├── cusparse.pxd │ ├── cupy_thrust.h │ ├── profiler.pyx │ └── memory.pxd ├── lib │ ├── __init__.py │ └── stride_tricks.py ├── _version.py ├── fft │ ├── config.py │ └── __init__.py ├── ext │ ├── __init__.py │ └── scatter.py ├── internal.py ├── prof │ └── __init__.py ├── io │ ├── rawfile.py │ ├── __init__.py │ ├── text.py │ └── formatting.py ├── padding │ └── __init__.py ├── sparse │ ├── linalg │ │ └── __init__.py │ └── __init__.py ├── binary │ ├── __init__.py │ └── elementwise.py ├── sorting │ ├── __init__.py │ └── count.py ├── indexing │ ├── __init__.py │ └── indexing.py ├── creation │ └── __init__.py ├── statistics │ └── __init__.py ├── logic │ ├── __init__.py │ ├── content.py │ ├── ops.py │ └── truth.py ├── math │ ├── special.py │ ├── __init__.py │ ├── ufunc.py │ ├── hyperbolic.py │ ├── floating.py │ ├── explog.py │ └── rounding.py ├── manipulation │ ├── __init__.py │ └── kind.py ├── random │ └── permutations.py ├── linalg │ ├── __init__.py │ └── util.py └── testing │ └── attr.py ├── install ├── __init__.py └── utils.py ├── tests ├── cupy_tests │ ├── __init__.py │ ├── ext_tests │ │ ├── __init__.py │ │ └── test_scatter.py │ ├── io_tests │ │ ├── __init__.py │ │ ├── test_text.py │ │ ├── test_rawfile.py │ │ ├── test_formatting.py │ │ ├── test_base_n.py │ │ └── test_npz.py │ ├── binary_tests │ │ ├── __init__.py │ │ ├── test_elementwise.py │ │ └── test_packing.py │ ├── core_tests │ │ ├── __init__.py │ │ ├── test_ndarray_contiguity.py │ │ ├── test_ndarray_owndata.py │ │ ├── test_raw.py │ │ ├── test_ndarray_conversion.py │ │ ├── test_array_function.py │ │ ├── test_iter.py │ │ ├── test_ndarray_math.py │ │ ├── test_function.py │ │ ├── test_dlpack.py │ │ ├── test_scan.py │ │ ├── test_core.py │ │ └── test_flags.py │ ├── creation_tests │ │ └── __init__.py │ ├── cuda_tests │ │ ├── __init__.py │ │ ├── memory_hooks_tests │ │ │ ├── __init__.py │ │ │ ├── test_line_profile.py │ │ │ └── test_debug_print.py │ │ ├── test_cusolver.py │ │ ├── test_nvtx.py │ │ ├── test_profile.py │ │ ├── test_nccl.py │ │ ├── test_driver.py │ │ └── test_curand.py │ ├── indexing_tests │ │ └── __init__.py │ ├── linalg_tests │ │ └── __init__.py │ ├── logic_tests │ │ ├── __init__.py │ │ ├── test_content.py │ │ └── test_ops.py │ ├── math_tests │ │ ├── __init__.py │ │ ├── test_window.py │ │ ├── test_special.py │ │ ├── test_hyperbolic.py │ │ └── test_explog.py │ ├── padding_tests │ │ └── __init__.py │ ├── prof_tests │ │ └── __init__.py │ ├── random_tests │ │ ├── __init__.py │ │ └── test_random.py │ ├── sorting_tests │ │ └── __init__.py │ ├── statics_tests │ │ └── __init__.py │ ├── testing_tests │ │ ├── __init__.py │ │ └── test_parameterized.py │ ├── manipulation_tests │ │ ├── __init__.py │ │ ├── test_add_remove.py │ │ └── test_kind.py │ ├── lib_tests │ │ └── test_strided_tricks.py │ └── test_numpy_interop.py ├── example_tests │ ├── __init__.py │ ├── test_gemm.py │ ├── example_test.py │ ├── test_gmm.py │ └── test_kmeans.py ├── install_tests │ ├── __init__.py │ ├── test_utils.py │ └── test_build.py └── cupyx_tests │ ├── scipy_tests │ ├── sparse_tests │ │ ├── __init__.py │ │ └── test_linalg.py │ ├── test_get_array_module.py │ ├── special_tests │ │ ├── test_statistics.py │ │ ├── test_gamma.py │ │ ├── test_gammaln.py │ │ ├── test_zeta.py │ │ ├── test_bessel.py │ │ └── test_digamma.py │ └── linalg_tests │ │ └── test_decomp_lu.py │ ├── test_scatter.py │ ├── test_rsqrt.py │ ├── __init__.py │ └── test_runtime.py ├── docs ├── source │ ├── .gitignore │ ├── tutorial │ │ └── index.rst │ ├── reference │ │ ├── memoize.rst │ │ ├── prof.rst │ │ ├── generic.rst │ │ ├── kernel.rst │ │ ├── pad.rst │ │ ├── ext.rst │ │ ├── comparison.rst │ │ ├── scipy.rst │ │ ├── routines.rst │ │ ├── indexing.rst │ │ ├── index.rst │ │ ├── io.rst │ │ ├── binary.rst │ │ ├── sorting.rst │ │ ├── statistics.rst │ │ ├── ndimage.rst │ │ ├── creation.rst │ │ ├── logic.rst │ │ ├── special.rst │ │ ├── linalg.rst │ │ ├── fftpack.rst │ │ ├── cuda.rst │ │ └── ndarray.rst │ ├── index.rst │ ├── _templates │ │ └── autosummary │ │ │ └── class.rst │ └── _static │ │ └── css │ │ └── modified_theme.css ├── requirements.txt └── image │ └── cupy_logo_1000px.png ├── cupyx ├── linalg │ ├── __init__.py │ └── sparse │ │ ├── __init__.py │ │ └── solve.py ├── scipy │ ├── linalg │ │ └── __init__.py │ ├── sparse │ │ ├── linalg │ │ │ └── __init__.py │ │ ├── sputils.py │ │ ├── util.py │ │ └── __init__.py │ ├── special │ │ ├── statistics.py │ │ ├── gammaln.py │ │ ├── gamma.py │ │ ├── polygamma.py │ │ ├── __init__.py │ │ ├── bessel.py │ │ └── erf.py │ ├── ndimage │ │ └── __init__.py │ ├── fftpack │ │ └── __init__.py │ └── __init__.py ├── rsqrt.py └── __init__.py ├── .github ├── CONTRIBUTING.md └── ISSUE_TEMPLATE.md ├── .flake8.cython ├── readthedocs.yml ├── .coveragerc ├── MANIFEST.in ├── codecov.yml ├── CODE_OF_CONDUCT.md ├── .gitignore ├── examples ├── stream │ ├── curand.py │ ├── thrust.py │ ├── cupy_kernel.py │ ├── cufft.py │ ├── cublas.py │ ├── cudnn.py │ ├── cusolver.py │ ├── cusparse.py │ ├── cupy_event.py │ ├── map_reduce.py │ └── cupy_memcpy.py ├── gemm │ └── utils.py ├── gmm │ └── README.md └── kmeans │ └── README.md ├── docker ├── python2 │ └── Dockerfile └── python3 │ └── Dockerfile ├── .travis.yml ├── LICENSE └── setup.cfg /cupy/__init__.pxd: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cupy/core/__init__.pxd: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cupy/cuda/__init__.pxd: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cupy/lib/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /install/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/example_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/install_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/ext_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/io_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/binary_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/creation_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/cuda_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/indexing_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/linalg_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/logic_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/math_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/padding_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/prof_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/random_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/sorting_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/statics_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupy_tests/testing_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cupy/_version.py: -------------------------------------------------------------------------------- 1 | __version__ = '6.0.0rc1' 2 | -------------------------------------------------------------------------------- /tests/cupy_tests/manipulation_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/source/.gitignore: -------------------------------------------------------------------------------- 1 | comparison_table.rst.inc 2 | -------------------------------------------------------------------------------- /tests/cupy_tests/cuda_tests/memory_hooks_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/cupyx_tests/scipy_tests/sparse_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cupy/fft/config.py: -------------------------------------------------------------------------------- 1 | 2 | global enable_nd_planning 3 | enable_nd_planning = True 4 | -------------------------------------------------------------------------------- /cupy/core/_dtype.pxd: -------------------------------------------------------------------------------- 1 | cpdef get_dtype(t) 2 | cpdef tuple get_dtype_with_itemsize(t) 3 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | Cython==0.28.3 2 | numpy >=1.15, <1.16 3 | scipy >=1.1, <1.2 4 | -------------------------------------------------------------------------------- /cupy/cuda/memory_hook.pxd: -------------------------------------------------------------------------------- 1 | cpdef bint _has_memory_hooks() 2 | cpdef get_memory_hooks() 3 | -------------------------------------------------------------------------------- /cupy/cuda/stream.pxd: -------------------------------------------------------------------------------- 1 | cdef size_t get_current_stream_ptr() 2 | cpdef get_current_stream() 3 | -------------------------------------------------------------------------------- /cupy/ext/__init__.py: -------------------------------------------------------------------------------- 1 | # "NOQA" to suppress flake8 warning 2 | from cupy.ext import scatter # NOQA 3 | -------------------------------------------------------------------------------- /cupyx/linalg/__init__.py: -------------------------------------------------------------------------------- 1 | # "NOQA" to suppress flake8 warning 2 | from cupyx.linalg import sparse # NOQA 3 | -------------------------------------------------------------------------------- /cupy/internal.py: -------------------------------------------------------------------------------- 1 | def prod(args, init=1): 2 | for arg in args: 3 | init *= arg 4 | return init 5 | -------------------------------------------------------------------------------- /docs/image/cupy_logo_1000px.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imos/cupy/master/docs/image/cupy_logo_1000px.png -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Please refer to [our Contribution Guide](https://docs-cupy.chainer.org/en/stable/contribution.html). 2 | 3 | -------------------------------------------------------------------------------- /cupyx/linalg/sparse/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | # "NOQA" to suppress flake8 warning 3 | from cupyx.linalg.sparse.solve import lschol # NOQA 4 | -------------------------------------------------------------------------------- /docs/source/tutorial/index.rst: -------------------------------------------------------------------------------- 1 | Tutorial 2 | ======== 3 | 4 | .. toctree:: 5 | :maxdepth: 1 6 | 7 | basic 8 | kernel 9 | -------------------------------------------------------------------------------- /.flake8.cython: -------------------------------------------------------------------------------- 1 | [flake8] 2 | filename = *.pyx,*.px* 3 | exclude = .eggs,*.egg,build,docs,.git 4 | ignore = W503,W504,E225,E226,E227,E999 5 | -------------------------------------------------------------------------------- /cupy/core/include/cupy/complex/README.md: -------------------------------------------------------------------------------- 1 | These files are copied from thrust project and are modified. 2 | 3 | http://thrust.github.io/ -------------------------------------------------------------------------------- /cupy/prof/__init__.py: -------------------------------------------------------------------------------- 1 | from cupy.prof.time_range import time_range # NOQA 2 | from cupy.prof.time_range import TimeRangeDecorator # NOQA 3 | -------------------------------------------------------------------------------- /cupy/core/raw.pxd: -------------------------------------------------------------------------------- 1 | cdef class RawKernel: 2 | 3 | cdef: 4 | readonly str code 5 | readonly str name 6 | readonly tuple options 7 | -------------------------------------------------------------------------------- /readthedocs.yml: -------------------------------------------------------------------------------- 1 | name: chainer 2 | type: sphinx 3 | base: docs/source 4 | requirements_file: docs/requirements.txt 5 | python: 6 | setup_py_install: true 7 | -------------------------------------------------------------------------------- /.coveragerc: -------------------------------------------------------------------------------- 1 | [run] 2 | plugins = Cython.Coverage 3 | include = cupy/*,cupyx/*,examples/* 4 | 5 | [report] 6 | exclude_lines = 7 | if __name__ == .__main__.: 8 | -------------------------------------------------------------------------------- /cupy/io/rawfile.py: -------------------------------------------------------------------------------- 1 | # flake8: NOQA 2 | # "flake8: NOQA" to suppress warning "H104 File contains nothing but comments" 3 | 4 | # TODO(okuta): Implement fromfile 5 | -------------------------------------------------------------------------------- /cupyx/scipy/linalg/__init__.py: -------------------------------------------------------------------------------- 1 | from cupyx.scipy.linalg.solve_triangular import solve_triangular # NOQA 2 | from cupyx.scipy.linalg.decomp_lu import lu_factor, lu_solve # NOQA 3 | -------------------------------------------------------------------------------- /tests/cupy_tests/io_tests/test_text.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestText(unittest.TestCase): 8 | 9 | pass 10 | -------------------------------------------------------------------------------- /tests/cupy_tests/io_tests/test_rawfile.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestRawfile(unittest.TestCase): 8 | 9 | pass 10 | -------------------------------------------------------------------------------- /cupy/core/dlpack.pxd: -------------------------------------------------------------------------------- 1 | # DLPACK_VERSION: 010 2 | 3 | from cupy.core.core cimport ndarray 4 | 5 | cpdef object toDlpack(ndarray array) except + 6 | cpdef ndarray fromDlpack(object dltensor) except + 7 | -------------------------------------------------------------------------------- /cupy/core/_routines_logic.pxd: -------------------------------------------------------------------------------- 1 | from cupy.core.core cimport ndarray 2 | 3 | 4 | cdef ndarray _ndarray_all(ndarray self, axis, out, keepdims) 5 | cdef ndarray _ndarray_any(ndarray self, axis, out, keepdims) 6 | -------------------------------------------------------------------------------- /cupy/core/_kernel.pxd: -------------------------------------------------------------------------------- 1 | cpdef create_ufunc(name, ops, routine=*, preamble=*, doc=*, 2 | default_casting=*, loop_prep=*) 3 | cpdef create_reduction_func(name, ops, routine=*, identity=*, preamble=*) 4 | -------------------------------------------------------------------------------- /docs/source/reference/memoize.rst: -------------------------------------------------------------------------------- 1 | Kernel binary memoization 2 | ========================= 3 | 4 | .. autosummary:: 5 | :toctree: generated/ 6 | :nosignatures: 7 | 8 | cupy.memoize 9 | cupy.clear_memo 10 | -------------------------------------------------------------------------------- /cupy/padding/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.padding.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.padding import pad # NOQA 6 | -------------------------------------------------------------------------------- /cupy/core/include/cupy/_cuda/README.md: -------------------------------------------------------------------------------- 1 | These files are copied from CUDA Toolkit Distribution and redistributed under the following license: 2 | 3 | https://docs.nvidia.com/cuda/archive/9.2/eula/#nvidia-cuda-toolkit-license-agreement 4 | -------------------------------------------------------------------------------- /cupyx/rsqrt.py: -------------------------------------------------------------------------------- 1 | from cupy.core.core import create_ufunc 2 | 3 | rsqrt = create_ufunc( 4 | 'cupy_rsqrt', 5 | ('e->e', 'f->f', 'd->d', 'F->F', 'D->D'), 6 | 'out0 = rsqrt(in0)', 7 | doc='''Returns the reciprocal square root.''') 8 | -------------------------------------------------------------------------------- /tests/example_tests/test_gemm.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from example_tests import example_test 4 | 5 | 6 | class TestGEMM(unittest.TestCase): 7 | 8 | def test_sgemm(self): 9 | example_test.run_example('gemm/sgemm.py') 10 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include cupy *.h *.hpp 2 | recursive-exclude cupy *.pyx *.pxd *.pxi 3 | include cupy_setup_build.py 4 | recursive-include install *.py 5 | include LICENSE 6 | include docs/LICENSE_THIRD_PARTY 7 | include docs/source/license.rst 8 | -------------------------------------------------------------------------------- /docs/source/reference/prof.rst: -------------------------------------------------------------------------------- 1 | Profiling 2 | ========= 3 | 4 | time range 5 | ---------- 6 | 7 | .. autosummary:: 8 | :toctree: generated/ 9 | :nosignatures: 10 | 11 | cupy.prof.TimeRangeDecorator 12 | cupy.prof.time_range 13 | -------------------------------------------------------------------------------- /cupy/sparse/linalg/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following SciPy document 2 | # https://docs.scipy.org/doc/scipy/reference/sparse.linalg.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupyx.scipy.sparse.linalg.solve import lsqr # NOQA 6 | -------------------------------------------------------------------------------- /cupyx/scipy/sparse/linalg/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following SciPy document 2 | # https://docs.scipy.org/doc/scipy/reference/sparse.linalg.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupyx.scipy.sparse.linalg.solve import lsqr # NOQA 6 | -------------------------------------------------------------------------------- /docs/source/reference/generic.rst: -------------------------------------------------------------------------------- 1 | NumPy-CuPy Generic Code Support 2 | =============================== 3 | 4 | .. autosummary:: 5 | :toctree: generated/ 6 | :nosignatures: 7 | 8 | cupy.get_array_module 9 | cupyx.scipy.get_array_module 10 | -------------------------------------------------------------------------------- /docs/source/reference/kernel.rst: -------------------------------------------------------------------------------- 1 | Custom kernels 2 | ============== 3 | 4 | .. autosummary:: 5 | :toctree: generated/ 6 | :nosignatures: 7 | 8 | cupy.ElementwiseKernel 9 | cupy.ReductionKernel 10 | cupy.RawKernel 11 | cupy.fuse 12 | -------------------------------------------------------------------------------- /docs/source/reference/pad.rst: -------------------------------------------------------------------------------- 1 | Padding 2 | ================================ 3 | 4 | .. https://docs.scipy.org/doc/numpy/reference/routines.padding.html 5 | 6 | .. autosummary:: 7 | :toctree: generated/ 8 | :nosignatures: 9 | 10 | cupy.pad 11 | -------------------------------------------------------------------------------- /cupy/binary/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.bitwise.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.binary import elementwise # NOQA 6 | from cupy.binary import packing # NOQA 7 | -------------------------------------------------------------------------------- /cupyx/__init__.py: -------------------------------------------------------------------------------- 1 | # "NOQA" to suppress flake8 warning 2 | from cupyx.rsqrt import rsqrt # NOQA 3 | from cupyx.runtime import get_runtime_info # NOQA 4 | from cupyx.scatter import scatter_add # NOQA 5 | 6 | from cupyx import linalg # NOQA 7 | from cupyx import scipy # NOQA 8 | -------------------------------------------------------------------------------- /cupy/core/_routines_sorting.pxd: -------------------------------------------------------------------------------- 1 | from cupy.core.core cimport ndarray 2 | 3 | 4 | cdef _ndarray_sort(ndarray self, int axis) 5 | cdef ndarray _ndarray_argsort(ndarray self, axis) 6 | cdef _ndarray_partition(ndarray self, kth, int axis) 7 | cdef ndarray _ndarray_argpartition(self, kth, axis) 8 | -------------------------------------------------------------------------------- /cupy/cuda/profiler.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from *: 2 | ctypedef int OutputMode 'cudaOutputMode_t' 3 | 4 | 5 | cpdef enum: 6 | cudaKeyValuePair = 0 7 | cudaCSV = 1 8 | 9 | cpdef initialize(str config_file, str output_file, int output_mode) 10 | cpdef start() 11 | cpdef stop() 12 | -------------------------------------------------------------------------------- /cupyx/scipy/sparse/sputils.py: -------------------------------------------------------------------------------- 1 | import cupy 2 | 3 | 4 | def isdense(x): 5 | return isinstance(x, cupy.ndarray) 6 | 7 | 8 | def isscalarlike(x): 9 | """Is x either a scalar, an array scalar, or a 0-dim array?""" 10 | return cupy.isscalar(x) or (isdense(x) and x.ndim == 0) 11 | -------------------------------------------------------------------------------- /cupy/sorting/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.sort.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.sorting import count # NOQA 6 | from cupy.sorting import search # NOQA 7 | from cupy.sorting import sort # NOQA 8 | -------------------------------------------------------------------------------- /docs/source/reference/ext.rst: -------------------------------------------------------------------------------- 1 | .. module:: cupyx 2 | 3 | CuPy-specific Functions 4 | ======================= 5 | 6 | CuPy-specific functions are placed under ``cupyx`` namespace. 7 | 8 | .. autosummary:: 9 | :toctree: generated/ 10 | :nosignatures: 11 | 12 | cupyx.rsqrt 13 | cupyx.scatter_add 14 | -------------------------------------------------------------------------------- /cupy/binary/elementwise.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | 4 | bitwise_and = core.bitwise_and 5 | 6 | 7 | bitwise_or = core.bitwise_or 8 | 9 | 10 | bitwise_xor = core.bitwise_xor 11 | 12 | 13 | invert = core.invert 14 | 15 | 16 | left_shift = core.left_shift 17 | 18 | 19 | right_shift = core.right_shift 20 | -------------------------------------------------------------------------------- /tests/cupy_tests/cuda_tests/test_cusolver.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import cuda 4 | 5 | 6 | class TestCusolver(unittest.TestCase): 7 | 8 | def test_cusolver_enabled(self): 9 | self.assertEqual(cuda.runtime.runtimeGetVersion() >= 8000, 10 | cuda.cusolver_enabled) 11 | -------------------------------------------------------------------------------- /cupy/indexing/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.indexing.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.indexing import generate # NOQA 6 | from cupy.indexing import indexing # NOQA 7 | from cupy.indexing import insert # NOQA 8 | -------------------------------------------------------------------------------- /cupyx/scipy/special/statistics.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | 4 | ndtr = core.create_ufunc( 5 | 'cupyx_scipy_ndtr', ('f->f', 'd->d'), 6 | 'out0 = normcdf(in0)', 7 | doc='''Cumulative distribution function of normal distribution. 8 | 9 | .. seealso:: :meth:`scipy.special.ndtr` 10 | 11 | ''') 12 | -------------------------------------------------------------------------------- /cupy/io/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.io.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.io import formatting # NOQA 6 | from cupy.io import npz # NOQA 7 | from cupy.io import rawfile # NOQA 8 | from cupy.io import text # NOQA 9 | -------------------------------------------------------------------------------- /cupy/cuda/memory_hooks/__init__.py: -------------------------------------------------------------------------------- 1 | from cupy.cuda.memory_hooks import debug_print # NOQA 2 | from cupy.cuda.memory_hooks import line_profile # NOQA 3 | 4 | # import class and function 5 | from cupy.cuda.memory_hooks.debug_print import DebugPrintHook # NOQA 6 | from cupy.cuda.memory_hooks.line_profile import LineProfileHook # NOQA 7 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | 3 | coverage: 4 | status: 5 | # Disable coverage measurement for overall codebase. 6 | project: off 7 | # Enable coverage measurement for diff introduced in the pull-request, 8 | # but do not mark "X" on commit status for now. 9 | patch: 10 | default: 11 | target: '0%' 12 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Chainer Code of Conduct 2 | 3 | CuPy follows the [NumFOCUS Code of Conduct][homepage] available at https://numfocus.org/code-of-conduct. 4 | 5 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at chainer@preferred.jp. 6 | 7 | [homepage]: https://numfocus.org/ 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.egg/ 3 | *.pyc 4 | *.pyo 5 | *.pyd 6 | *.cpp 7 | *.so 8 | build 9 | \#*\# 10 | .\#* 11 | .coverage 12 | .eggs/ 13 | _readthedocs_build 14 | /TAGS 15 | /docs/source/reference/**/generated 16 | /tags 17 | cupy.egg-info/ 18 | dist/ 19 | htmlcov/ 20 | .idea/ 21 | .cache/ 22 | .pytest_cache/ 23 | cupy/_lib/ 24 | cupy/cuda/thrust.h 25 | -------------------------------------------------------------------------------- /cupy/core/_errors.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | 3 | 4 | try: 5 | _AxisError = numpy.AxisError 6 | except AttributeError: 7 | class IndexOrValueError(IndexError, ValueError): 8 | 9 | def __init__(self, *args, **kwargs): 10 | super(IndexOrValueError, self).__init__(*args, **kwargs) 11 | 12 | _AxisError = IndexOrValueError 13 | -------------------------------------------------------------------------------- /docs/source/reference/comparison.rst: -------------------------------------------------------------------------------- 1 | Comparison Table 2 | ================ 3 | 4 | Here is a list of NumPy / SciPy APIs and its corresponding CuPy implementations. 5 | 6 | ``-`` in CuPy column denotes that CuPy implementation is not provided yet. 7 | We welcome contributions for these functions. 8 | 9 | .. include:: comparison_table.rst.inc 10 | -------------------------------------------------------------------------------- /examples/stream/curand.py: -------------------------------------------------------------------------------- 1 | # nvprof --print-gpu-trace python examples/stream/curand.py 2 | import cupy 3 | 4 | rand = cupy.random.generator.RandomState() 5 | 6 | stream = cupy.cuda.stream.Stream() 7 | with stream: 8 | y = rand.lognormal(size=(1, 3)) 9 | 10 | stream = cupy.cuda.stream.Stream() 11 | stream.use() 12 | y = rand.lognormal(size=(1, 3)) 13 | -------------------------------------------------------------------------------- /cupyx/scipy/ndimage/__init__.py: -------------------------------------------------------------------------------- 1 | from cupyx.scipy.ndimage.interpolation import affine_transform # NOQA 2 | from cupyx.scipy.ndimage.interpolation import map_coordinates # NOQA 3 | from cupyx.scipy.ndimage.interpolation import rotate # NOQA 4 | from cupyx.scipy.ndimage.interpolation import shift # NOQA 5 | from cupyx.scipy.ndimage.interpolation import zoom # NOQA 6 | -------------------------------------------------------------------------------- /cupy/creation/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.creation import basic # NOQA 6 | from cupy.creation import from_data # NOQA 7 | from cupy.creation import matrix # NOQA 8 | from cupy.creation import ranges # NOQA 9 | -------------------------------------------------------------------------------- /cupy/io/text.py: -------------------------------------------------------------------------------- 1 | # flake8: NOQA 2 | # "flake8: NOQA" to suppress warning "H104 File contains nothing but comments" 3 | 4 | 5 | # TODO(okuta): Implement loadtxt 6 | 7 | 8 | # TODO(okuta): Implement savetxt 9 | 10 | 11 | # TODO(okuta): Implement genfromtxt 12 | 13 | 14 | # TODO(okuta): Implement fromregex 15 | 16 | 17 | # TODO(okuta): Implement fromstring 18 | -------------------------------------------------------------------------------- /docker/python2/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nvidia/cuda:9.2-cudnn7-devel 2 | 3 | RUN apt-get update -y && \ 4 | apt-get install -y --no-install-recommends \ 5 | python-dev \ 6 | python-pip \ 7 | python-wheel \ 8 | python-setuptools && \ 9 | rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* 10 | 11 | RUN pip install --no-cache-dir cupy-cuda92==6.0.0rc1 12 | -------------------------------------------------------------------------------- /cupy/statistics/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.statistics.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.statistics import correlation # NOQA 6 | from cupy.statistics import histogram # NOQA 7 | from cupy.statistics import meanvar # NOQA 8 | from cupy.statistics import order # NOQA 9 | -------------------------------------------------------------------------------- /docker/python3/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nvidia/cuda:9.2-cudnn7-devel 2 | 3 | RUN apt-get update -y && \ 4 | apt-get install -y --no-install-recommends \ 5 | python3-dev \ 6 | python3-pip \ 7 | python3-wheel \ 8 | python3-setuptools && \ 9 | rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* 10 | 11 | RUN pip3 install --no-cache-dir cupy-cuda92==6.0.0rc1 12 | -------------------------------------------------------------------------------- /cupy/logic/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.logic.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.logic import comparison # NOQA 6 | from cupy.logic import content # NOQA 7 | from cupy.logic import ops # NOQA 8 | from cupy.logic import truth # NOQA 9 | from cupy.logic import type_test # NOQA 10 | -------------------------------------------------------------------------------- /tests/install_tests/test_utils.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from install import utils 4 | 5 | 6 | class TestPrintWarning(unittest.TestCase): 7 | 8 | def test_print_warning(self): 9 | utils.print_warning('This is a test.') 10 | 11 | 12 | class TestSearchOnPath(unittest.TestCase): 13 | 14 | def test_exec_not_found(self): 15 | self.assertIsNone(utils.search_on_path(['no_such_exec'])) 16 | -------------------------------------------------------------------------------- /cupy/core/_scalar.pxd: -------------------------------------------------------------------------------- 1 | from libc.stdint cimport int8_t 2 | 3 | from cupy.cuda.function cimport CPointer 4 | 5 | 6 | cdef class CScalar(CPointer): 7 | 8 | cdef: 9 | char kind 10 | int8_t size 11 | 12 | cpdef apply_dtype(self, dtype) 13 | cpdef get_numpy_type(self) 14 | 15 | 16 | cpdef str get_typename(dtype) 17 | cpdef get_scalar_from_numpy(x, dtype) 18 | cpdef convert_scalar(x, bint use_c_scalar) 19 | -------------------------------------------------------------------------------- /cupy/ext/scatter.py: -------------------------------------------------------------------------------- 1 | import warnings 2 | 3 | import cupyx 4 | 5 | 6 | def scatter_add(a, slices, value): 7 | """Adds given values to specified elements of an array. 8 | 9 | .. deprecated:: 4.0 10 | Use :func:`cupyx.scatter_add` instead. 11 | 12 | """ 13 | warnings.warn( 14 | 'cupy.scatter_add is deprecated. Use cupyx.scatter_add instead.', 15 | DeprecationWarning) 16 | cupyx.scatter_add(a, slices, value) 17 | -------------------------------------------------------------------------------- /tests/cupy_tests/math_tests/test_window.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | @testing.parameterize( 7 | *testing.product({ 8 | 'm': [0, 1, -1, 1024], 9 | 'name': ['blackman', 'hamming', 'hanning'], 10 | }) 11 | ) 12 | class TestWindow(unittest.TestCase): 13 | 14 | @testing.numpy_cupy_allclose(atol=1e-5) 15 | def test_window(self, xp): 16 | return getattr(xp, self.name)(self.m) 17 | -------------------------------------------------------------------------------- /docs/source/reference/scipy.rst: -------------------------------------------------------------------------------- 1 | ------------------------- 2 | SciPy-compatible Routines 3 | ------------------------- 4 | 5 | The following pages describe SciPy-compatible routines. 6 | These functions cover a subset of 7 | `SciPy routines `_. 8 | 9 | 10 | .. module:: cupyx.scipy 11 | 12 | .. toctree:: 13 | :maxdepth: 2 14 | 15 | fftpack 16 | ndimage 17 | sparse 18 | special 19 | -------------------------------------------------------------------------------- /cupy/cuda/cupy_common.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_GUARD_CUPY_CUDA_COMMON_H 2 | #define INCLUDE_GUARD_CUPY_CUDA_COMMON_H 3 | 4 | typedef char cpy_byte; 5 | typedef unsigned char cpy_ubyte; 6 | typedef short cpy_short; 7 | typedef unsigned short cpy_ushort; 8 | typedef int cpy_int; 9 | typedef unsigned int cpy_uint; 10 | typedef long long cpy_long; 11 | typedef unsigned long long cpy_ulong; 12 | typedef float cpy_float; 13 | typedef double cpy_double; 14 | 15 | #endif // INCLUDE_GUARD_CUPY_CUDA_COMMON_H 16 | -------------------------------------------------------------------------------- /cupy/cuda/common.pxd: -------------------------------------------------------------------------------- 1 | # distutils: language = c++ 2 | 3 | cdef extern from '../cuda/cupy_common.h': # thru parent to import in core 4 | ctypedef char cpy_byte 5 | ctypedef unsigned char cpy_ubyte 6 | ctypedef short cpy_short 7 | ctypedef unsigned short cpy_ushort 8 | ctypedef int cpy_int 9 | ctypedef unsigned int cpy_uint 10 | ctypedef long long cpy_long 11 | ctypedef unsigned long long cpy_ulong 12 | ctypedef float cpy_float 13 | ctypedef double cpy_double 14 | -------------------------------------------------------------------------------- /cupy/cuda/cufft.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from *: 2 | ctypedef float Float 'cufftReal' 3 | ctypedef double Double 'cufftDoubleReal' 4 | ctypedef int Result 'cufftResult_t' 5 | ctypedef size_t Handle 'cufftHandle' 6 | ctypedef int Type 'cufftType_t' 7 | 8 | 9 | cpdef enum: 10 | CUFFT_C2C = 0x29 11 | CUFFT_R2C = 0x2a 12 | CUFFT_C2R = 0x2c 13 | CUFFT_Z2Z = 0x69 14 | CUFFT_D2Z = 0x6a 15 | CUFFT_Z2D = 0x6c 16 | 17 | CUFFT_FORWARD = -1 18 | CUFFT_INVERSE = 1 19 | -------------------------------------------------------------------------------- /cupyx/scipy/fftpack/__init__.py: -------------------------------------------------------------------------------- 1 | from cupyx.scipy.fftpack.fft import fft # NOQA 2 | from cupyx.scipy.fftpack.fft import fft2 # NOQA 3 | from cupyx.scipy.fftpack.fft import fftn # NOQA 4 | from cupyx.scipy.fftpack.fft import ifft # NOQA 5 | from cupyx.scipy.fftpack.fft import ifft2 # NOQA 6 | from cupyx.scipy.fftpack.fft import ifftn # NOQA 7 | from cupyx.scipy.fftpack.fft import irfft # NOQA 8 | from cupyx.scipy.fftpack.fft import rfft # NOQA 9 | from cupyx.scipy.fftpack.fft import get_fft_plan # NOQA 10 | -------------------------------------------------------------------------------- /examples/stream/thrust.py: -------------------------------------------------------------------------------- 1 | # nvprof --print-gpu-trace python examples/stream/thrust.py 2 | import cupy 3 | 4 | x = cupy.array([1, 3, 2]) 5 | expected = x.sort() 6 | cupy.cuda.Device().synchronize() 7 | 8 | stream = cupy.cuda.stream.Stream() 9 | with stream: 10 | y = x.sort() 11 | stream.synchronize() 12 | cupy.testing.assert_array_equal(y, expected) 13 | 14 | stream = cupy.cuda.stream.Stream() 15 | stream.use() 16 | y = x.sort() 17 | stream.synchronize() 18 | cupy.testing.assert_array_equal(y, expected) 19 | -------------------------------------------------------------------------------- /cupy/cuda/device.pxd: -------------------------------------------------------------------------------- 1 | cpdef int get_device_id() except? -1 2 | cpdef size_t get_cublas_handle() except? 0 3 | cpdef size_t get_cusolver_handle() except? 0 4 | cpdef size_t get_cusparse_handle() except? 0 5 | cpdef str get_compute_capability() 6 | 7 | cdef class Handle: 8 | cdef: 9 | public size_t handle 10 | object _destroy_func 11 | 12 | cdef class Device: 13 | cdef: 14 | public int id 15 | list _device_stack 16 | 17 | cpdef use(self) 18 | cpdef synchronize(self) 19 | -------------------------------------------------------------------------------- /tests/cupyx_tests/test_scatter.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import cupy 6 | from cupy import testing 7 | import cupyx 8 | 9 | 10 | @testing.gpu 11 | class TestScatter(unittest.TestCase): 12 | 13 | def test_scatter_add(self): 14 | a = cupy.zeros((3,), dtype=numpy.float32) 15 | i = cupy.array([1, 1], numpy.int32) 16 | v = cupy.array([2., 1.], dtype=numpy.float32) 17 | cupyx.scatter_add(a, i, v) 18 | testing.assert_array_equal(a, cupy.array([0, 3, 0])) 19 | -------------------------------------------------------------------------------- /docs/source/reference/routines.rst: -------------------------------------------------------------------------------- 1 | -------- 2 | Routines 3 | -------- 4 | 5 | The following pages describe NumPy-compatible routines. 6 | These functions cover a subset of 7 | `NumPy routines `_. 8 | 9 | .. module:: cupy 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | creation 15 | manipulation 16 | binary 17 | fft 18 | indexing 19 | io 20 | linalg 21 | logic 22 | math 23 | pad 24 | random 25 | sorting 26 | statistics 27 | ext 28 | -------------------------------------------------------------------------------- /cupy/math/special.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | from cupy.math import ufunc 3 | 4 | 5 | i0 = ufunc.create_math_ufunc( 6 | 'cyl_bessel_i0', 1, 'cupy_i0', 7 | '''Modified Bessel function of the first kind, order 0. 8 | 9 | .. seealso:: :func:`numpy.i0` 10 | 11 | ''') 12 | 13 | 14 | sinc = core.create_ufunc( 15 | 'cupy_sinc', ('e->e', 'f->f', 'd->d'), 16 | 'out0 = abs(in0) > 1e-9 ? sinpi(in0) / (M_PI * in0) : 1', 17 | doc='''Elementwise sinc function. 18 | 19 | .. seealso:: :func:`numpy.sinc` 20 | 21 | ''') 22 | -------------------------------------------------------------------------------- /docs/source/reference/indexing.rst: -------------------------------------------------------------------------------- 1 | Indexing Routines 2 | ================= 3 | 4 | .. https://docs.scipy.org/doc/numpy/reference/routines.indexing.html 5 | 6 | .. autosummary:: 7 | :toctree: generated/ 8 | :nosignatures: 9 | 10 | cupy.c_ 11 | cupy.r_ 12 | cupy.nonzero 13 | cupy.where 14 | cupy.indices 15 | cupy.ix_ 16 | cupy.unravel_index 17 | cupy.take 18 | cupy.choose 19 | cupy.diag 20 | cupy.diagonal 21 | cupy.lib.stride_tricks.as_strided 22 | cupy.place 23 | cupy.put 24 | cupy.fill_diagonal 25 | -------------------------------------------------------------------------------- /cupy/cuda/nccl.pxd: -------------------------------------------------------------------------------- 1 | """ 2 | Wrapper for NCCL: Optimized primiteive for collective multi-GPU communication 3 | """ 4 | cpdef enum: 5 | NCCL_SUM = 0 6 | NCCL_PROD = 1 7 | NCCL_MAX = 2 8 | NCCL_MIN = 3 9 | 10 | NCCL_INT8 = 0 11 | NCCL_CHAR = 0 12 | NCCL_UINT8 = 1 13 | NCCL_INT32 = 2 14 | NCCL_INT = 2 15 | NCCL_UINT32 = 3 16 | NCCL_INT64 = 4 17 | NCCL_UINT64 = 5 18 | NCCL_FLOAT16 = 6 19 | NCCL_HALF = 6 20 | NCCL_FLOAT32 = 7 21 | NCCL_FLOAT = 7 22 | NCCL_FLOAT64 = 8 23 | NCCL_DOUBLE = 8 24 | -------------------------------------------------------------------------------- /examples/stream/cupy_kernel.py: -------------------------------------------------------------------------------- 1 | # nvprof --print-gpu-trace python examples/stream/cupy_kernel.py 2 | import cupy 3 | 4 | x = cupy.array([1, 2, 3]) 5 | expected = cupy.linalg.norm(x) 6 | cupy.cuda.Device().synchronize() 7 | 8 | stream = cupy.cuda.stream.Stream() 9 | with stream: 10 | y = cupy.linalg.norm(x) 11 | stream.synchronize() 12 | cupy.testing.assert_array_equal(y, expected) 13 | 14 | stream = cupy.cuda.stream.Stream() 15 | stream.use() 16 | y = cupy.linalg.norm(x) 17 | stream.synchronize() 18 | cupy.testing.assert_array_equal(y, expected) 19 | -------------------------------------------------------------------------------- /cupy/math/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.math.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.math import arithmetic # NOQA 6 | from cupy.math import explog # NOQA 7 | from cupy.math import floating # NOQA 8 | from cupy.math import hyperbolic # NOQA 9 | from cupy.math import misc # NOQA 10 | from cupy.math import rounding # NOQA 11 | from cupy.math import special # NOQA 12 | from cupy.math import sumprod # NOQA 13 | from cupy.math import trigonometric # NOQA 14 | -------------------------------------------------------------------------------- /examples/stream/cufft.py: -------------------------------------------------------------------------------- 1 | # nvprof --print-gpu-trace python examples/stream/cufft.py 2 | import cupy 3 | 4 | x = cupy.array([1, 0, 3, 0, 5, 0, 7, 0, 9], dtype=float) 5 | expected_f = cupy.fft.fft(x) 6 | cupy.cuda.Device().synchronize() 7 | 8 | stream = cupy.cuda.stream.Stream() 9 | with stream: 10 | f = cupy.fft.fft(x) 11 | stream.synchronize() 12 | cupy.testing.assert_array_equal(f, expected_f) 13 | 14 | stream = cupy.cuda.stream.Stream() 15 | stream.use() 16 | f = cupy.fft.fft(x) 17 | stream.synchronize() 18 | cupy.testing.assert_array_equal(f, expected_f) 19 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_ndarray_contiguity.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestArrayContiguity(unittest.TestCase): 8 | 9 | def test_is_contiguous(self): 10 | a = testing.shaped_arange((2, 3, 4)) 11 | self.assertTrue(a.flags.c_contiguous) 12 | b = a.transpose(2, 0, 1) 13 | self.assertFalse(b.flags.c_contiguous) 14 | c = a[::-1] 15 | self.assertFalse(c.flags.c_contiguous) 16 | d = a[:, :, ::2] 17 | self.assertFalse(d.flags.c_contiguous) 18 | -------------------------------------------------------------------------------- /examples/stream/cublas.py: -------------------------------------------------------------------------------- 1 | # nvprof --print-gpu-trace python examples/stream/cublas.py 2 | import cupy 3 | 4 | x = cupy.array([1, 2, 3]) 5 | y = cupy.array([[1], [2], [3]]) 6 | expected = cupy.matmul(x, y) 7 | cupy.cuda.Device().synchronize() 8 | 9 | stream = cupy.cuda.stream.Stream() 10 | with stream: 11 | z = cupy.matmul(x, y) 12 | stream.synchronize() 13 | cupy.testing.assert_array_equal(z, expected) 14 | 15 | stream = cupy.cuda.stream.Stream() 16 | stream.use() 17 | z = cupy.matmul(x, y) 18 | stream.synchronize() 19 | cupy.testing.assert_array_equal(z, expected) 20 | -------------------------------------------------------------------------------- /tests/cupy_tests/ext_tests/test_scatter.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import cupy 6 | from cupy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestScatter(unittest.TestCase): 11 | 12 | def test_scatter_add(self): 13 | a = cupy.zeros((3,), dtype=numpy.float32) 14 | i = cupy.array([1, 1], numpy.int32) 15 | v = cupy.array([2., 1.], dtype=numpy.float32) 16 | with testing.assert_warns(DeprecationWarning): 17 | cupy.scatter_add(a, i, v) 18 | testing.assert_array_equal(a, cupy.array([0, 3, 0])) 19 | -------------------------------------------------------------------------------- /tests/cupy_tests/random_tests/test_random.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import random 4 | from cupy import testing 5 | 6 | 7 | @testing.gpu 8 | class TestResetSeed(unittest.TestCase): 9 | 10 | @testing.for_float_dtypes(no_float16=True) 11 | def test_reset_seed(self, dtype): 12 | rs = random.get_random_state() 13 | rs.seed(0) 14 | l1 = rs.rand(10, dtype=dtype) 15 | 16 | rs = random.get_random_state() 17 | rs.seed(0) 18 | l2 = rs.rand(10, dtype=dtype) 19 | 20 | testing.assert_array_equal(l1, l2) 21 | -------------------------------------------------------------------------------- /cupy/core/_routines_statistics.pxd: -------------------------------------------------------------------------------- 1 | from cupy.core.core cimport ndarray 2 | 3 | 4 | cdef ndarray _ndarray_max(ndarray self, axis, out, dtype, keepdims) 5 | cdef ndarray _ndarray_min(ndarray self, axis, out, dtype, keepdims) 6 | cdef ndarray _ndarray_argmax(ndarray self, axis, out, dtype, keepdims) 7 | cdef ndarray _ndarray_argmin(ndarray self, axis, out, dtype, keepdims) 8 | cdef ndarray _ndarray_mean(ndarray self, axis, dtype, out, keepdims) 9 | cdef ndarray _ndarray_var(ndarray self, axis, dtype, out, ddof, keepdims) 10 | cdef ndarray _ndarray_std(ndarray self, axis, dtype, out, ddof, keepdims) 11 | -------------------------------------------------------------------------------- /tests/example_tests/example_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | import subprocess 3 | import sys 4 | 5 | 6 | def run_example(path, *args): 7 | examples_path = os.path.join( 8 | os.path.dirname(__file__), '..', '..', 'examples') 9 | fullpath = os.path.join(examples_path, path) 10 | 11 | try: 12 | return subprocess.check_output( 13 | (sys.executable, fullpath) + args, 14 | stderr=subprocess.STDOUT) 15 | except subprocess.CalledProcessError as e: 16 | print('Original error message:') 17 | print(e.output.decode('utf-8')) 18 | raise 19 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_ndarray_owndata.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import core 4 | from cupy import testing 5 | 6 | 7 | @testing.gpu 8 | class TestArrayOwndata(unittest.TestCase): 9 | 10 | def setUp(self): 11 | self.a = core.ndarray(()) 12 | 13 | def test_original_array(self): 14 | self.assertTrue(self.a.flags.owndata) 15 | 16 | def test_view_array(self): 17 | v = self.a.view() 18 | self.assertFalse(v.flags.owndata) 19 | 20 | def test_reshaped_array(self): 21 | r = self.a.reshape(()) 22 | self.assertFalse(r.flags.owndata) 23 | -------------------------------------------------------------------------------- /tests/cupyx_tests/test_rsqrt.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import cupy 6 | from cupy import testing 7 | import cupyx 8 | 9 | 10 | @testing.gpu 11 | class TestRsqrt(unittest.TestCase): 12 | 13 | @testing.for_all_dtypes(no_complex=True) 14 | @testing.with_requires('numpy>=1.11.2') 15 | def test_rsqrt(self, dtype): 16 | # Adding 1.0 to avoid division by zero. 17 | a = testing.shaped_arange((2, 3), numpy, dtype) + 1.0 18 | out = cupyx.rsqrt(cupy.array(a)) 19 | # numpy.sqrt is broken in numpy<1.11.2 20 | testing.assert_allclose(out, 1.0 / numpy.sqrt(a)) 21 | -------------------------------------------------------------------------------- /cupy/core/_routines_indexing.pxd: -------------------------------------------------------------------------------- 1 | from cupy.core.core cimport ndarray 2 | 3 | 4 | cdef ndarray _ndarray_getitem(ndarray self, slices) 5 | cdef _ndarray_setitem(ndarray self, slices, value) 6 | cdef tuple _ndarray_nonzero(ndarray self) 7 | cdef _ndarray_scatter_add(ndarray self, slices, value) 8 | cdef ndarray _ndarray_take(ndarray self, indices, axis, out) 9 | cdef ndarray _ndarray_put(ndarray self, indices, values, mode) 10 | cdef ndarray _ndarray_choose(ndarray self, choices, out, mode) 11 | cdef ndarray _ndarray_diagonal(ndarray self, offset, axis1, axis2) 12 | 13 | cdef ndarray _simple_getitem(ndarray a, list slice_list) 14 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | ============================================ 2 | CuPy -- NumPy-like API accelerated with CUDA 3 | ============================================ 4 | 5 | This is the `CuPy `_ documentation. 6 | 7 | .. module:: cupy 8 | 9 | .. toctree:: 10 | :maxdepth: 1 11 | 12 | overview 13 | tutorial/index 14 | reference/index 15 | 16 | .. toctree:: 17 | :maxdepth: 1 18 | :caption: Development 19 | 20 | compatibility 21 | contribution 22 | 23 | .. toctree:: 24 | :maxdepth: 1 25 | :caption: Misc Notes 26 | 27 | install 28 | upgrade 29 | license 30 | -------------------------------------------------------------------------------- /cupyx/scipy/special/gammaln.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | 4 | gammaln = core.create_ufunc( 5 | 'cupyx_scipy_gammaln', ('f->f', 'd->d'), 6 | ''' 7 | if (isinf(in0) && in0 < 0) { 8 | out0 = -1.0 / 0.0; 9 | } else { 10 | out0 = lgamma(in0); 11 | } 12 | ''', 13 | doc="""Logarithm of the absolute value of the Gamma function. 14 | 15 | Args: 16 | x (cupy.ndarray): Values on the real line at which to compute 17 | ``gammaln``. 18 | 19 | Returns: 20 | cupy.ndarray: Values of ``gammaln`` at x. 21 | 22 | .. seealso:: :data:`scipy.special.gammaln` 23 | 24 | """) 25 | -------------------------------------------------------------------------------- /tests/cupyx_tests/__init__.py: -------------------------------------------------------------------------------- 1 | # TODO(kmaehashi) this is to avoid DeprecationWarning raised during import 2 | # not being filtered in Python 3.4. Remove this when we drop Python 3.4. 3 | 4 | import sys 5 | import warnings 6 | 7 | 8 | ver = sys.version_info 9 | if ver.major == 3 and ver.minor == 4: 10 | with warnings.catch_warnings(): 11 | warnings.simplefilter('ignore') 12 | 13 | # This imports `numpy.testing.*` module, which emits 14 | # DeprecationWarning during import (in NumPy 1.15+). 15 | try: 16 | from scipy import stats # NOQA 17 | except ImportError: 18 | pass 19 | -------------------------------------------------------------------------------- /cupyx/scipy/special/gamma.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | 4 | gamma = core.create_ufunc( 5 | 'cupyx_scipy_gamma', ('f->f', 'd->d'), 6 | ''' 7 | if (isinf(in0) && in0 < 0) { 8 | out0 = -1.0 / 0.0; 9 | } else if (in0 < 0. && in0 == floor(in0)) { 10 | out0 = 1.0 / 0.0; 11 | } else { 12 | out0 = tgamma(in0); 13 | } 14 | ''', 15 | doc="""Gamma function. 16 | 17 | Args: 18 | z (cupy.ndarray): The input of gamma function. 19 | 20 | Returns: 21 | cupy.ndarray: Computed value of gamma function. 22 | 23 | .. seealso:: :data:`scipy.special.gamma` 24 | 25 | """) 26 | -------------------------------------------------------------------------------- /install/utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | 4 | def print_warning(*lines): 5 | print('**************************************************') 6 | for line in lines: 7 | print('*** WARNING: %s' % line) 8 | print('**************************************************') 9 | 10 | 11 | def get_path(key): 12 | return os.environ.get(key, '').split(os.pathsep) 13 | 14 | 15 | def search_on_path(filenames): 16 | for p in get_path('PATH'): 17 | for filename in filenames: 18 | full = os.path.join(p, filename) 19 | if os.path.exists(full): 20 | return os.path.abspath(full) 21 | return None 22 | -------------------------------------------------------------------------------- /tests/cupy_tests/io_tests/test_formatting.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import cupy 6 | from cupy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestFormatting(unittest.TestCase): 11 | 12 | def test_array_repr(self): 13 | a = testing.shaped_arange((2, 3, 4), cupy) 14 | b = testing.shaped_arange((2, 3, 4), numpy) 15 | self.assertEqual(cupy.array_repr(a), numpy.array_repr(b)) 16 | 17 | def test_array_str(self): 18 | a = testing.shaped_arange((2, 3, 4), cupy) 19 | b = testing.shaped_arange((2, 3, 4), numpy) 20 | self.assertEqual(cupy.array_str(a), numpy.array_str(b)) 21 | -------------------------------------------------------------------------------- /cupy/math/ufunc.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | 4 | def create_math_ufunc(math_name, nargs, name, doc, support_complex=True): 5 | assert 1 <= nargs <= 2 6 | if nargs == 1: 7 | types = ('e->e', 'f->f', 'd->d') 8 | if support_complex: 9 | types += ('F->F', 'D->D') 10 | return core.create_ufunc( 11 | name, types, 'out0 = %s(in0)' % math_name, doc=doc) 12 | else: 13 | types = ('ee->e', 'ff->f', 'dd->d') 14 | if support_complex: 15 | types += ('FF->F', 'DD->D') 16 | return core.create_ufunc( 17 | name, types, 'out0 = %s(in0, in1)' % math_name, doc=doc) 18 | -------------------------------------------------------------------------------- /docs/source/reference/index.rst: -------------------------------------------------------------------------------- 1 | .. _cupy_reference: 2 | 3 | **************** 4 | Reference Manual 5 | **************** 6 | 7 | This is the official reference of CuPy, a multi-dimensional array on CUDA with a subset of NumPy interface. 8 | 9 | * :ref:`genindex` 10 | * :ref:`modindex` 11 | 12 | ---- 13 | 14 | .. module:: cupy 15 | 16 | .. toctree:: 17 | :maxdepth: 2 18 | 19 | ndarray 20 | ufunc 21 | routines 22 | scipy 23 | sparse 24 | ndimage 25 | generic 26 | memory 27 | cuda 28 | memoize 29 | kernel 30 | interoperability 31 | testing 32 | prof 33 | environment 34 | difference 35 | comparison 36 | -------------------------------------------------------------------------------- /examples/gemm/utils.py: -------------------------------------------------------------------------------- 1 | import cupy as cp 2 | 3 | 4 | def read_code(code_filename, params): 5 | with open(code_filename, 'r') as f: 6 | code = f.read() 7 | for k, v in params.items(): 8 | code = '#define ' + k + ' ' + str(v) + '\n' + code 9 | return code 10 | 11 | 12 | def benchmark(func, args, n_run): 13 | times = [] 14 | for _ in range(n_run): 15 | start = cp.cuda.Event() 16 | end = cp.cuda.Event() 17 | start.record() 18 | func(*args) 19 | end.record() 20 | end.synchronize() 21 | times.append(cp.cuda.get_elapsed_time(start, end)) # milliseconds 22 | return times 23 | -------------------------------------------------------------------------------- /examples/stream/cudnn.py: -------------------------------------------------------------------------------- 1 | # nvprof --print-gpu-trace python examples/stream/cudnn.py 2 | import cupy 3 | import cupy.cudnn 4 | 5 | x = cupy.array([1.0, 2.0, 3.0]) 6 | mode = cupy.cuda.cudnn.CUDNN_ACTIVATION_RELU 7 | expected = cupy.cudnn.activation_forward(x, mode) 8 | cupy.cuda.Device().synchronize() 9 | 10 | stream = cupy.cuda.stream.Stream() 11 | with stream: 12 | y = cupy.cudnn.activation_forward(x, mode) 13 | stream.synchronize() 14 | cupy.testing.assert_array_equal(y, expected) 15 | 16 | stream = cupy.cuda.stream.Stream() 17 | stream.use() 18 | y = cupy.cudnn.activation_forward(x, mode) 19 | stream.synchronize() 20 | cupy.testing.assert_array_equal(y, expected) 21 | -------------------------------------------------------------------------------- /cupy/manipulation/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.manipulation import add_remove # NOQA 6 | from cupy.manipulation import basic # NOQA 7 | from cupy.manipulation import dims # NOQA 8 | from cupy.manipulation import join # NOQA 9 | from cupy.manipulation import kind # NOQA 10 | from cupy.manipulation import rearrange # NOQA 11 | from cupy.manipulation import shape # NOQA 12 | from cupy.manipulation import split # NOQA 13 | from cupy.manipulation import tiling # NOQA 14 | from cupy.manipulation import transpose # NOQA 15 | -------------------------------------------------------------------------------- /cupyx/scipy/special/polygamma.py: -------------------------------------------------------------------------------- 1 | import cupy 2 | from cupyx.scipy.special import digamma 3 | from cupyx.scipy.special import gamma 4 | from cupyx.scipy.special import zeta 5 | 6 | 7 | def polygamma(n, x): 8 | """Polygamma function n. 9 | 10 | Args: 11 | n (cupy.ndarray): The order of the derivative of `psi`. 12 | x (cupy.ndarray): Where to evaluate the polygamma function. 13 | 14 | Returns: 15 | cupy.ndarray: The result. 16 | 17 | .. seealso:: :data:`scipy.special.polygamma` 18 | 19 | """ 20 | n, x = cupy.broadcast_arrays(n, x) 21 | fac2 = (-1.0)**(n+1) * gamma.gamma(n+1.0) * zeta.zeta(n+1.0, x) 22 | return cupy.where(n == 0, digamma.digamma(x), fac2) 23 | -------------------------------------------------------------------------------- /examples/gmm/README.md: -------------------------------------------------------------------------------- 1 | # GMM example 2 | 3 | This example contains implementation of Gaussian Mixture Model (GMM). 4 | 5 | 6 | ### How to demo 7 | The demo contains a script that partitions data into groups using Gaussian Mixture Model. 8 | The demo can be run by the following command. 9 | 10 | ``` 11 | python gmm.py [--gpu-id GPU_ID] [--num NUM] [--dim DIM] 12 | [--max-iter MAX_ITER] [--tol TOL] [--output-image OUTPUT] 13 | ``` 14 | 15 | If you run this script on environment without matplotlib renderers (e.g., non-GUI environment), setting the environmental variable `MPLBACKEND` to `Agg` may be required to use `matplotlib`. For example, 16 | 17 | ``` 18 | MPLBACKEND=Agg python gmm.py ... 19 | ``` 20 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_raw.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import cupy 4 | 5 | 6 | _test_source = r''' 7 | extern "C" __global__ 8 | void test_sum(const float* x1, const float* x2, float* y) { 9 | int tid = blockDim.x * blockIdx.x + threadIdx.x; 10 | y[tid] = x1[tid] + x2[tid]; 11 | } 12 | ''' 13 | 14 | 15 | class TestRaw(unittest.TestCase): 16 | def test_basic(self): 17 | kern = cupy.RawKernel(_test_source, 'test_sum') 18 | x1 = cupy.arange(100, dtype=cupy.float32).reshape(10, 10) 19 | x2 = cupy.ones((10, 10), dtype=cupy.float32) 20 | y = cupy.zeros((10, 10), dtype=cupy.float32) 21 | kern((10,), (10,), (x1, x2, y)) 22 | assert (y == x1 + x2).all() 23 | -------------------------------------------------------------------------------- /cupy/manipulation/kind.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | 4 | # TODO(okuta): Implement asfarray 5 | 6 | 7 | def asfortranarray(a, dtype=None): 8 | """Return an array laid out in Fortran order in memory. 9 | 10 | Args: 11 | a (~cupy.ndarray): The input array. 12 | dtype (str or dtype object, optional): By default, the data-type is 13 | inferred from the input data. 14 | 15 | Returns: 16 | ~cupy.ndarray: The input `a` in Fortran, or column-major, order. 17 | 18 | .. seealso:: :func:`numpy.asfortranarray` 19 | 20 | """ 21 | return core.asfortranarray(a, dtype) 22 | 23 | 24 | # TODO(okuta): Implement asarray_chkfinite 25 | 26 | 27 | # TODO(okuta): Implement require 28 | -------------------------------------------------------------------------------- /cupy/cuda/cupy_cuComplex.h: -------------------------------------------------------------------------------- 1 | // This file is a stub header file of cuda for Read the Docs. 2 | 3 | #ifndef INCLUDE_GUARD_CUPY_COMPLEX_H 4 | #define INCLUDE_GUARD_CUPY_COMPLEX_H 5 | 6 | #ifndef CUPY_NO_CUDA 7 | 8 | #include 9 | 10 | #else // #ifndef CUPY_NO_CUDA 11 | 12 | extern "C" { 13 | 14 | /////////////////////////////////////////////////////////////////////////////// 15 | // cuComplex.h 16 | /////////////////////////////////////////////////////////////////////////////// 17 | 18 | struct cuComplex{ 19 | float x, y; 20 | }; 21 | 22 | struct cuDoubleComplex{ 23 | double x, y; 24 | }; 25 | 26 | } // extern "C" 27 | 28 | #endif // #ifndef CUPY_NO_CUDA 29 | #endif // #ifndef INCLUDE_GUARD_CUPY_COMPLEX_H 30 | -------------------------------------------------------------------------------- /examples/stream/cusolver.py: -------------------------------------------------------------------------------- 1 | # nvprof --print-gpu-trace python examples/stream/cusolver.py 2 | import cupy 3 | 4 | x = cupy.array([[1, 0, 3], [0, 5, 0], [7, 0, 9]], float) 5 | expected_w, expected_v = cupy.linalg.eigh(x, UPLO='U') 6 | cupy.cuda.Device().synchronize() 7 | 8 | stream = cupy.cuda.stream.Stream() 9 | with stream: 10 | w, v = cupy.linalg.eigh(x, UPLO='U') 11 | stream.synchronize() 12 | cupy.testing.assert_array_equal(w, expected_w) 13 | cupy.testing.assert_array_equal(v, expected_v) 14 | 15 | stream = cupy.cuda.stream.Stream() 16 | stream.use() 17 | w, v = cupy.linalg.eigh(x, UPLO='U') 18 | stream.synchronize() 19 | cupy.testing.assert_array_equal(w, expected_w) 20 | cupy.testing.assert_array_equal(v, expected_v) 21 | -------------------------------------------------------------------------------- /tests/cupy_tests/cuda_tests/test_nvtx.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import cuda 4 | from cupy.testing import attr 5 | 6 | 7 | @unittest.skipUnless(cuda.nvtx_enabled, 'nvtx is not installed') 8 | class TestNVTX(unittest.TestCase): 9 | 10 | @attr.gpu 11 | def test_Mark(self): 12 | cuda.nvtx.Mark('test:Mark', 0) 13 | 14 | @attr.gpu 15 | def test_MarkC(self): 16 | cuda.nvtx.MarkC('test:MarkC', 0xFF000000) 17 | 18 | @attr.gpu 19 | def test_RangePush(self): 20 | cuda.nvtx.RangePush('test:RangePush', 1) 21 | cuda.nvtx.RangePop() 22 | 23 | @attr.gpu 24 | def test_RangePushC(self): 25 | cuda.nvtx.RangePushC('test:RangePushC', 0xFF000000) 26 | cuda.nvtx.RangePop() 27 | -------------------------------------------------------------------------------- /docs/source/reference/io.rst: -------------------------------------------------------------------------------- 1 | Input and Output 2 | ================ 3 | 4 | .. https://docs.scipy.org/doc/numpy/reference/routines.io.html 5 | 6 | NumPy binary files (NPY, NPZ) 7 | ----------------------------- 8 | 9 | .. autosummary:: 10 | :toctree: generated/ 11 | :nosignatures: 12 | 13 | cupy.load 14 | cupy.save 15 | cupy.savez 16 | cupy.savez_compressed 17 | 18 | 19 | String formatting 20 | ----------------- 21 | 22 | .. autosummary:: 23 | :toctree: generated/ 24 | :nosignatures: 25 | 26 | cupy.array_repr 27 | cupy.array_str 28 | 29 | 30 | Base-n representations 31 | ---------------------- 32 | 33 | .. autosummary:: 34 | :toctree: generated/ 35 | :nosignatures: 36 | 37 | cupy.binary_repr 38 | cupy.base_repr 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for using and contributing to CuPy! 2 | 3 | If your issue is a **request for support in using CuPy**, please post it on [Stack Overflow](https://stackoverflow.com/questions/tagged/cupy) or Google Groups ([en](https://groups.google.com/forum/#!forum/cupy) / [ja](https://groups.google.com/forum/#!forum/cupy-ja)). 4 | Developer team members are monitoring questions on these channels. 5 | 6 | If it is a bug report, please include the following information: 7 | 8 | * Conditions (you can just paste the output of `python -c 'import cupy; cupy.show_config()'`) 9 | - CuPy version 10 | - OS/Platform 11 | - CUDA version 12 | - cuDNN/NCCL version (if applicable) 13 | * Code to reproduce 14 | * Error messages, stack traces, or logs 15 | -------------------------------------------------------------------------------- /examples/kmeans/README.md: -------------------------------------------------------------------------------- 1 | # kmeans example 2 | 3 | This example contains implementation of K-means clustering. 4 | 5 | 6 | ### How to demo 7 | The demo contains a script that partitions data into groups using K-means clustering. 8 | The demo can be run by the following command. 9 | 10 | ``` 11 | python kmeans.py [--gpu-id GPU_ID] [--n-clusters N_CLUSTERS] [--num NUM] 12 | [--max-iter MAX_ITER] [--use-custom-kernel] 13 | [--output-image OUTPUT_IMAGE] 14 | ``` 15 | 16 | If you run this script on environment without matplotlib renderers (e.g., non-GUI environment), setting the environmental variable `MPLBACKEND` to `Agg` may be required to use `matplotlib`. For example, 17 | 18 | ``` 19 | MPLBACKEND=Agg python kmeans.py ... 20 | ``` 21 | -------------------------------------------------------------------------------- /docs/source/reference/binary.rst: -------------------------------------------------------------------------------- 1 | Binary Operations 2 | ================= 3 | 4 | .. https://docs.scipy.org/doc/numpy/reference/routines.bitwise.html 5 | 6 | Elementwise bit operations 7 | -------------------------- 8 | 9 | .. autosummary:: 10 | :toctree: generated/ 11 | :nosignatures: 12 | 13 | cupy.bitwise_and 14 | cupy.bitwise_or 15 | cupy.bitwise_xor 16 | cupy.invert 17 | cupy.left_shift 18 | cupy.right_shift 19 | 20 | 21 | Bit packing 22 | ----------- 23 | 24 | .. autosummary:: 25 | :toctree: generated/ 26 | :nosignatures: 27 | 28 | cupy.packbits 29 | cupy.unpackbits 30 | 31 | 32 | Output formatting 33 | ----------------- 34 | 35 | .. autosummary:: 36 | :toctree: generated/ 37 | :nosignatures: 38 | 39 | cupy.binary_repr 40 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_ndarray_conversion.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | @testing.parameterize( 7 | {'shape': ()}, 8 | {'shape': (1,)}, 9 | {'shape': (1, 1, 1)}, 10 | ) 11 | class TestNdarrayItem(unittest.TestCase): 12 | 13 | @testing.for_all_dtypes() 14 | @testing.numpy_cupy_equal() 15 | def test_item(self, xp, dtype): 16 | a = xp.full(self.shape, 3, dtype) 17 | return a.item() 18 | 19 | 20 | @testing.parameterize( 21 | {'shape': (0,)}, 22 | {'shape': (2, 3)}, 23 | {'shape': (1, 0, 1)}, 24 | ) 25 | class TestNdarrayItemRaise(unittest.TestCase): 26 | 27 | @testing.numpy_cupy_raises() 28 | def test_item(self, xp): 29 | a = testing.shaped_arange(self.shape, xp, xp.float32) 30 | a.item() 31 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | cache: 4 | - pip 5 | - ccache 6 | 7 | python: 8 | - "2.7" 9 | - "3.4" 10 | - "3.5" 11 | 12 | install: 13 | - pip install -U pip wheel 14 | - pip install cython 15 | - READTHEDOCS=True travis_wait pip install -U -e .[travis] 16 | 17 | script: 18 | - flake8 19 | - flake8 --config=.flake8.cython 20 | - autopep8 -r . --diff | tee check_autopep8 21 | - test ! -s check_autopep8 22 | # To workaround Travis issue (https://github.com/travis-ci/travis-ci/issues/7261), 23 | # ignore DeprecationWarning raised in `site.py`. 24 | - python -Werror::DeprecationWarning -Wignore::DeprecationWarning:site -m compileall -f -q cupy cupyx examples tests docs 25 | - pip install scipy 26 | - pushd docs 27 | - SPHINXOPTS=-W make html 28 | - popd 29 | 30 | sudo: false 31 | -------------------------------------------------------------------------------- /cupy/cuda/function.pxd: -------------------------------------------------------------------------------- 1 | from libc.stdint cimport intptr_t 2 | 3 | 4 | cdef class CPointer: 5 | cdef void* ptr 6 | 7 | 8 | cdef class Function: 9 | 10 | cdef: 11 | public Module module 12 | public intptr_t ptr 13 | 14 | cpdef linear_launch(self, size_t size, args, size_t shared_mem=*, 15 | size_t block_max_size=*, stream=*) 16 | 17 | 18 | cdef class Module: 19 | 20 | cdef: 21 | public intptr_t ptr 22 | 23 | cpdef load_file(self, filename) 24 | cpdef load(self, bytes cubin) 25 | cpdef get_global_var(self, name) 26 | cpdef get_function(self, name) 27 | 28 | 29 | cdef class LinkState: 30 | 31 | cdef: 32 | public intptr_t ptr 33 | 34 | cpdef add_ptr_data(self, unicode data, unicode name) 35 | cpdef bytes complete(self) 36 | -------------------------------------------------------------------------------- /cupy/fft/__init__.py: -------------------------------------------------------------------------------- 1 | from cupy.fft.fft import fft # NOQA 2 | from cupy.fft.fft import fft2 # NOQA 3 | from cupy.fft.fft import fftfreq # NOQA 4 | from cupy.fft.fft import fftn # NOQA 5 | from cupy.fft.fft import fftshift # NOQA 6 | from cupy.fft.fft import hfft # NOQA 7 | from cupy.fft.fft import ifft # NOQA 8 | from cupy.fft.fft import ifft2 # NOQA 9 | from cupy.fft.fft import ifftn # NOQA 10 | from cupy.fft.fft import ifftshift # NOQA 11 | from cupy.fft.fft import ihfft # NOQA 12 | from cupy.fft.fft import irfft # NOQA 13 | from cupy.fft.fft import irfft2 # NOQA 14 | from cupy.fft.fft import irfftn # NOQA 15 | from cupy.fft.fft import rfft # NOQA 16 | from cupy.fft.fft import rfft2 # NOQA 17 | from cupy.fft.fft import rfftfreq # NOQA 18 | from cupy.fft.fft import rfftn # NOQA 19 | from cupy.fft import config # NOQA 20 | -------------------------------------------------------------------------------- /docs/source/reference/sorting.rst: -------------------------------------------------------------------------------- 1 | Sorting, Searching, and Counting 2 | ================================ 3 | 4 | .. https://docs.scipy.org/doc/numpy/reference/routines.sort.html 5 | 6 | Sorting 7 | ------- 8 | 9 | .. autosummary:: 10 | :toctree: generated/ 11 | :nosignatures: 12 | 13 | cupy.sort 14 | cupy.lexsort 15 | cupy.argsort 16 | cupy.msort 17 | cupy.partition 18 | cupy.argpartition 19 | 20 | .. seealso:: 21 | :func:`cupy.ndarray.sort` 22 | 23 | Searching 24 | --------- 25 | 26 | .. autosummary:: 27 | :toctree: generated/ 28 | :nosignatures: 29 | 30 | cupy.argmax 31 | cupy.argmin 32 | cupy.nonzero 33 | cupy.flatnonzero 34 | cupy.where 35 | 36 | Counting 37 | -------- 38 | 39 | .. autosummary:: 40 | :toctree: generated/ 41 | :nosignatures: 42 | 43 | cupy.count_nonzero 44 | -------------------------------------------------------------------------------- /tests/cupy_tests/math_tests/test_special.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import cupy 4 | from cupy import testing 5 | 6 | 7 | @testing.gpu 8 | class TestSpecial(unittest.TestCase): 9 | 10 | @testing.for_dtypes(['e', 'f', 'd']) 11 | @testing.numpy_cupy_allclose(rtol=1e-3) 12 | def test_i0(self, xp, dtype): 13 | a = testing.shaped_random((2, 3), xp, dtype) 14 | return xp.i0(a) 15 | 16 | @testing.for_dtypes(['e', 'f', 'd']) 17 | @testing.numpy_cupy_allclose(atol=1e-3) 18 | def test_sinc(self, xp, dtype): 19 | a = testing.shaped_random((2, 3), xp, dtype) 20 | return xp.sinc(a) 21 | 22 | @testing.for_dtypes(['e', 'f', 'd']) 23 | def test_sinc_zero(self, dtype): 24 | a = cupy.sinc(cupy.zeros(1, dtype=dtype)) 25 | testing.assert_array_equal(a, cupy.ones(1, dtype=dtype)) 26 | -------------------------------------------------------------------------------- /cupy/cuda/nvrtc.pxd: -------------------------------------------------------------------------------- 1 | 2 | ############################################################################### 3 | # Types 4 | ############################################################################### 5 | 6 | cdef extern from *: 7 | ctypedef int Result 'nvrtcResult' 8 | 9 | ctypedef void* Program 'nvrtcProgram' 10 | 11 | 12 | cpdef check_status(int status) 13 | 14 | cpdef tuple getVersion() 15 | 16 | ############################################################################### 17 | # Program 18 | ############################################################################### 19 | 20 | cpdef size_t createProgram(unicode src, unicode name, headers, 21 | include_names) except? 0 22 | cpdef destroyProgram(size_t prog) 23 | cpdef compileProgram(size_t prog, options) 24 | cpdef unicode getPTX(size_t prog) 25 | cpdef unicode getProgramLog(size_t prog) 26 | -------------------------------------------------------------------------------- /examples/stream/cusparse.py: -------------------------------------------------------------------------------- 1 | # nvprof --print-gpu-trace python examples/stream/cusparse.py 2 | import cupy 3 | 4 | 5 | def _make(xp, sp, dtype): 6 | data = xp.array([0, 1, 3, 2], dtype) 7 | indices = xp.array([0, 0, 2, 1], 'i') 8 | indptr = xp.array([0, 1, 2, 3, 4], 'i') 9 | # 0, 1, 0, 0 10 | # 0, 0, 0, 2 11 | # 0, 0, 3, 0 12 | return sp.csc_matrix((data, indices, indptr), shape=(3, 4)) 13 | 14 | 15 | x = _make(cupy, cupy.sparse, float) 16 | expected = cupy.cusparse.cscsort(x) 17 | cupy.cuda.Device().synchronize() 18 | 19 | stream = cupy.cuda.stream.Stream() 20 | with stream: 21 | y = cupy.cusparse.cscsort(x) 22 | stream.synchronize() 23 | cupy.testing.assert_array_equal(y, expected) 24 | 25 | stream = cupy.cuda.stream.Stream() 26 | stream.use() 27 | y = cupy.cusparse.cscsort(x) 28 | stream.synchronize() 29 | cupy.testing.assert_array_equal(y, expected) 30 | -------------------------------------------------------------------------------- /tests/cupy_tests/testing_tests/test_parameterized.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | class TestParameterize(unittest.TestCase): 7 | def test_skip(self): 8 | # Skipping the test case should not report error. 9 | self.skipTest('skip') 10 | 11 | 12 | @testing.parameterize( 13 | {'actual': {'a': [1, 2], 'b': [3, 4, 5]}, 14 | 'expect': [{'a': 1, 'b': 3}, {'a': 1, 'b': 4}, {'a': 1, 'b': 5}, 15 | {'a': 2, 'b': 3}, {'a': 2, 'b': 4}, {'a': 2, 'b': 5}]}, 16 | {'actual': {'a': [1, 2]}, 'expect': [{'a': 1}, {'a': 2}]}, 17 | {'actual': {'a': [1, 2], 'b': []}, 'expect': []}, 18 | {'actual': {'a': []}, 'expect': []}, 19 | {'actual': {}, 'expect': [{}]}) 20 | class ProductTest(unittest.TestCase): 21 | 22 | def test_product(self): 23 | self.assertListEqual(testing.product(self.actual), self.expect) 24 | -------------------------------------------------------------------------------- /examples/stream/cupy_event.py: -------------------------------------------------------------------------------- 1 | # nvprof --print-gpu-trace python examples/stream/cupy_event.py 2 | import cupy 3 | 4 | x = cupy.array([1, 2, 3]) 5 | 6 | start_event = cupy.cuda.stream.Event() 7 | stop_event = cupy.cuda.stream.Event() 8 | 9 | 10 | def _norm_with_elapsed_time(x): 11 | start_event.record() 12 | y = cupy.linalg.norm(x) 13 | stop_event.record() 14 | stop_event.synchronize() 15 | print(cupy.cuda.get_elapsed_time(start_event, stop_event)) 16 | return y 17 | 18 | 19 | expected = _norm_with_elapsed_time(x) 20 | cupy.cuda.Device().synchronize() 21 | 22 | stream = cupy.cuda.stream.Stream() 23 | with stream: 24 | y = _norm_with_elapsed_time(x) 25 | stream.synchronize() 26 | cupy.testing.assert_array_equal(y, expected) 27 | 28 | stream = cupy.cuda.stream.Stream() 29 | stream.use() 30 | y = _norm_with_elapsed_time(x) 31 | stream.synchronize() 32 | cupy.testing.assert_array_equal(y, expected) 33 | -------------------------------------------------------------------------------- /docs/source/reference/statistics.rst: -------------------------------------------------------------------------------- 1 | Statistical Functions 2 | ===================== 3 | 4 | .. https://docs.scipy.org/doc/scipy/reference/stats.html 5 | 6 | Order statistics 7 | ---------------- 8 | 9 | .. autosummary:: 10 | :toctree: generated/ 11 | :nosignatures: 12 | 13 | cupy.amin 14 | cupy.amax 15 | cupy.nanmin 16 | cupy.nanmax 17 | cupy.percentile 18 | 19 | 20 | Means and variances 21 | ------------------- 22 | 23 | .. autosummary:: 24 | :toctree: generated/ 25 | :nosignatures: 26 | 27 | cupy.average 28 | cupy.mean 29 | cupy.var 30 | cupy.std 31 | 32 | 33 | Histograms 34 | ---------- 35 | 36 | .. autosummary:: 37 | :toctree: generated/ 38 | :nosignatures: 39 | 40 | cupy.histogram 41 | cupy.bincount 42 | 43 | 44 | Correlations 45 | ------------ 46 | 47 | .. autosummary:: 48 | :toctree: generated/ 49 | :nosignatures: 50 | 51 | cupy.corrcoef 52 | cupy.cov 53 | -------------------------------------------------------------------------------- /cupy/cuda/pinned_memory.pxd: -------------------------------------------------------------------------------- 1 | from libc.stdint cimport intptr_t 2 | 3 | 4 | cdef class PinnedMemoryPointer: 5 | 6 | cdef: 7 | readonly object mem 8 | readonly intptr_t ptr 9 | Py_ssize_t _shape[1] 10 | Py_ssize_t _strides[1] 11 | 12 | cpdef size_t size(self) 13 | 14 | 15 | cpdef _add_to_watch_list(event, obj) 16 | 17 | 18 | cpdef PinnedMemoryPointer alloc_pinned_memory(size_t size) 19 | 20 | 21 | cpdef set_pinned_memory_allocator(allocator=*) 22 | 23 | 24 | cdef class PinnedMemoryPool: 25 | 26 | cdef: 27 | object _alloc 28 | dict _in_use 29 | object _free 30 | object __weakref__ 31 | object _weakref 32 | object _lock 33 | size_t _allocation_unit_size 34 | 35 | cpdef PinnedMemoryPointer malloc(self, size_t size) 36 | cpdef free(self, intptr_t ptr, size_t size) 37 | cpdef free_all_blocks(self) 38 | cpdef n_free_blocks(self) 39 | -------------------------------------------------------------------------------- /cupyx/scipy/__init__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | from cupy.core import ndarray 4 | from cupyx.scipy.sparse.base import spmatrix 5 | 6 | 7 | try: 8 | import scipy 9 | _scipy_available = True 10 | except ImportError: 11 | _scipy_available = False 12 | 13 | 14 | _cupyx_scipy = sys.modules[__name__] 15 | 16 | 17 | def get_array_module(*args): 18 | """Returns the array module for arguments. 19 | 20 | This function is used to implement CPU/GPU generic code. If at least one of 21 | the arguments is a :class:`cupy.ndarray` object, the :mod:`cupyx.scipy` 22 | module is returned. 23 | 24 | Args: 25 | args: Values to determine whether NumPy or CuPy should be used. 26 | 27 | Returns: 28 | module: :mod:`cupyx.scipy` or :mod:`scipy` is returned based on the 29 | types of the arguments. 30 | 31 | """ 32 | for arg in args: 33 | if isinstance(arg, (ndarray, spmatrix)): 34 | return _cupyx_scipy 35 | return scipy 36 | -------------------------------------------------------------------------------- /cupy/random/permutations.py: -------------------------------------------------------------------------------- 1 | from cupy.random import generator 2 | import six 3 | 4 | 5 | def shuffle(a): 6 | """Shuffles an array. 7 | 8 | Args: 9 | a (cupy.ndarray): The array to be shuffled. 10 | 11 | .. seealso:: :func:`numpy.random.shuffle` 12 | 13 | """ 14 | rs = generator.get_random_state() 15 | return rs.shuffle(a) 16 | 17 | 18 | def permutation(a): 19 | """Returns a permuted range or a permutation of an array. 20 | 21 | Args: 22 | a (int or cupy.ndarray): The range or the array to be shuffled. 23 | 24 | Returns: 25 | cupy.ndarray: If `a` is an integer, it is permutation range between 0 26 | and `a` - 1. 27 | Otherwise, it is a permutation of `a`. 28 | 29 | .. seealso:: :func:`numpy.random.permutation` 30 | """ 31 | rs = generator.get_random_state() 32 | if isinstance(a, six.integer_types): 33 | return rs.permutation(a) 34 | else: 35 | return a[rs.permutation(len(a))] 36 | -------------------------------------------------------------------------------- /cupyx/scipy/special/__init__.py: -------------------------------------------------------------------------------- 1 | from cupyx.scipy.special.bessel import i0 # NOQA 2 | from cupyx.scipy.special.bessel import i1 # NOQA 3 | from cupyx.scipy.special.bessel import j0 # NOQA 4 | from cupyx.scipy.special.bessel import j1 # NOQA 5 | from cupyx.scipy.special.bessel import y0 # NOQA 6 | from cupyx.scipy.special.bessel import y1 # NOQA 7 | 8 | from cupyx.scipy.special.polygamma import polygamma # NOQA 9 | 10 | from cupyx.scipy.special.digamma import digamma # NOQA 11 | from cupyx.scipy.special.gamma import gamma # NOQA 12 | from cupyx.scipy.special.gammaln import gammaln # NOQA 13 | from cupyx.scipy.special.zeta import zeta # NOQA 14 | 15 | from cupyx.scipy.special.statistics import ndtr # NOQA 16 | 17 | from cupyx.scipy.special.erf import erf # NOQA 18 | from cupyx.scipy.special.erf import erfc # NOQA 19 | from cupyx.scipy.special.erf import erfcx # NOQA 20 | from cupyx.scipy.special.erf import erfinv # NOQA 21 | from cupyx.scipy.special.erf import erfcinv # NOQA 22 | -------------------------------------------------------------------------------- /tests/cupy_tests/logic_tests/test_content.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | from cupy import testing 6 | 7 | 8 | @testing.gpu 9 | class TestContent(unittest.TestCase): 10 | 11 | @testing.for_dtypes('efFdD') 12 | @testing.numpy_cupy_array_equal() 13 | def check_unary_inf(self, name, xp, dtype): 14 | a = xp.array([-3, numpy.inf, -1, -numpy.inf, 0, 1, 2], 15 | dtype=dtype) 16 | return getattr(xp, name)(a) 17 | 18 | @testing.for_dtypes('efFdD') 19 | @testing.numpy_cupy_array_equal() 20 | def check_unary_nan(self, name, xp, dtype): 21 | a = xp.array( 22 | [-3, numpy.NAN, -1, numpy.NAN, 0, numpy.NAN, numpy.inf], 23 | dtype=dtype) 24 | return getattr(xp, name)(a) 25 | 26 | def test_isfinite(self): 27 | self.check_unary_inf('isfinite') 28 | 29 | def test_isinf(self): 30 | self.check_unary_inf('isinf') 31 | 32 | def test_isnan(self): 33 | self.check_unary_nan('isnan') 34 | -------------------------------------------------------------------------------- /tests/cupy_tests/cuda_tests/test_profile.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import mock 4 | 5 | from cupy import cuda 6 | 7 | 8 | class TestProfile(unittest.TestCase): 9 | 10 | def test_profile(self): 11 | start_patch = mock.patch('cupy.cuda.profiler.start') 12 | stop_patch = mock.patch('cupy.cuda.profiler.stop') 13 | with start_patch as start, stop_patch as stop: 14 | with cuda.profile(): 15 | pass 16 | start.assert_called_once_with() 17 | stop.assert_called_once_with() 18 | 19 | def test_err_case(self): 20 | start_patch = mock.patch('cupy.cuda.profiler.start') 21 | stop_patch = mock.patch('cupy.cuda.profiler.stop') 22 | with start_patch as start, stop_patch as stop: 23 | try: 24 | with cuda.profile(): 25 | raise Exception() 26 | except Exception: 27 | # ignore 28 | pass 29 | start.assert_called_once_with() 30 | stop.assert_called_once_with() 31 | -------------------------------------------------------------------------------- /tests/cupyx_tests/scipy_tests/test_get_array_module.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | import cupyx.scipy.special 5 | 6 | 7 | @testing.gpu 8 | @testing.with_requires('scipy') 9 | class TestSpecial(unittest.TestCase): 10 | 11 | @testing.for_dtypes(['f', 'd']) 12 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 13 | def test_get_array_module(self, xp, scp, dtype): 14 | import scipy.special # NOQA 15 | 16 | a = testing.shaped_arange((2, 3), xp, dtype) 17 | module = cupyx.scipy.get_array_module(a) 18 | assert module is scp 19 | return module.special.j0(a) 20 | 21 | @testing.for_dtypes(['f', 'd']) 22 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 23 | def test_get_array_module_multiple_parameters(self, xp, scp, dtype): 24 | import scipy.special # NOQA 25 | 26 | a = testing.shaped_arange((2, 3), xp, dtype) 27 | module = cupyx.scipy.get_array_module(a, a) 28 | assert module is scp 29 | return module.special.j1(a) 30 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_array_function.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | import six 5 | 6 | import cupy 7 | from cupy import testing 8 | 9 | 10 | @testing.gpu 11 | class TestArrayFunction(unittest.TestCase): 12 | 13 | @testing.with_requires('numpy>=1.17.0') 14 | def test_array_function(self): 15 | a = numpy.random.randn(100, 100) 16 | a_cpu = numpy.asarray(a) 17 | a_gpu = cupy.asarray(a) 18 | 19 | # The numpy call for both CPU and GPU arrays is intentional to test the 20 | # __array_function__ protocol 21 | qr_cpu = numpy.linalg.qr(a_cpu) 22 | qr_gpu = numpy.linalg.qr(a_gpu) 23 | 24 | if isinstance(qr_cpu, tuple): 25 | for b_cpu, b_gpu in six.moves.zip(qr_cpu, qr_gpu): 26 | self.assertEqual(b_cpu.dtype, b_gpu.dtype) 27 | cupy.testing.assert_allclose(b_cpu, b_gpu, atol=1e-4) 28 | else: 29 | self.assertEqual(qr_cpu.dtype, qr_gpu.dtype) 30 | cupy.testing.assert_allclose(qr_cpu, qr_gpu, atol=1e-4) 31 | -------------------------------------------------------------------------------- /tests/cupy_tests/logic_tests/test_ops.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestOps(unittest.TestCase): 8 | 9 | @testing.for_all_dtypes(no_complex=True) 10 | @testing.numpy_cupy_allclose(atol=1e-5) 11 | def check_unary(self, name, xp, dtype): 12 | a = testing.shaped_arange((2, 3), xp, dtype) 13 | return getattr(xp, name)(a) 14 | 15 | @testing.for_all_dtypes(no_complex=True) 16 | @testing.numpy_cupy_allclose(atol=1e-5) 17 | def check_binary(self, name, xp, dtype): 18 | a = testing.shaped_arange((2, 3), xp, dtype) 19 | b = testing.shaped_reverse_arange((2, 3), xp, dtype) 20 | return getattr(xp, name)(a, b) 21 | 22 | def test_logical_and(self): 23 | self.check_binary('logical_and') 24 | 25 | def test_logical_or(self): 26 | self.check_binary('logical_or') 27 | 28 | def test_logical_xor(self): 29 | self.check_binary('logical_xor') 30 | 31 | def test_logical_not(self): 32 | self.check_unary('logical_not') 33 | -------------------------------------------------------------------------------- /cupy/logic/content.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | 4 | def _create_float_test_ufunc(name, doc): 5 | return core.create_ufunc( 6 | 'cupy_' + name, 7 | ('e->?', 'f->?', 'd->?', 'F->?', 'D->?', 8 | ), 'out0 = %s(in0)' % name, 9 | doc=doc) 10 | 11 | 12 | isfinite = _create_float_test_ufunc( 13 | 'isfinite', 14 | '''Tests finiteness elementwise. 15 | 16 | Each element of returned array is ``True`` only if the corresponding 17 | element of the input is finite (i.e. not an infinity nor NaN). 18 | 19 | .. seealso:: :data:`numpy.isfinite` 20 | 21 | ''') 22 | 23 | 24 | isinf = _create_float_test_ufunc( 25 | 'isinf', 26 | '''Tests if each element is the positive or negative infinity. 27 | 28 | .. seealso:: :data:`numpy.isinf` 29 | 30 | ''') 31 | 32 | 33 | isnan = _create_float_test_ufunc( 34 | 'isnan', 35 | '''Tests if each element is a NaN. 36 | 37 | .. seealso:: :data:`numpy.isnan` 38 | 39 | ''') 40 | 41 | 42 | # TODO(okuta): Implement isneginf 43 | 44 | 45 | # TODO(okuta): Implement isposinf 46 | -------------------------------------------------------------------------------- /docs/source/reference/ndimage.rst: -------------------------------------------------------------------------------- 1 | ---------------------------------- 2 | Multi-dimensional image processing 3 | ---------------------------------- 4 | 5 | CuPy provides multi-dimensional image processing functions. 6 | It supports a subset of :mod:`scipy.ndimage` interface. 7 | 8 | .. module:: cupyx.scipy.ndimage 9 | 10 | .. https://docs.scipy.org/doc/scipy/reference/ndimage.html 11 | 12 | Interpolation 13 | ------------- 14 | 15 | .. autosummary:: 16 | :toctree: generated/ 17 | :nosignatures: 18 | 19 | cupyx.scipy.ndimage.affine_transform 20 | cupyx.scipy.ndimage.map_coordinates 21 | cupyx.scipy.ndimage.rotate 22 | cupyx.scipy.ndimage.shift 23 | cupyx.scipy.ndimage.zoom 24 | 25 | 26 | OpenCV mode 27 | ----------- 28 | :mod:`cupyx.scipy.ndimage` supports additional mode, ``opencv``. 29 | If it is given, the function performs like `cv2.warpAffine `_ or `cv2.resize `_. 30 | -------------------------------------------------------------------------------- /cupy/core/flags.pyx: -------------------------------------------------------------------------------- 1 | # distutils: language = c++ 2 | 3 | 4 | class Flags(object): 5 | 6 | def __init__(self, c_contiguous, f_contiguous, owndata): 7 | self.c_contiguous = c_contiguous 8 | self.f_contiguous = f_contiguous 9 | self.owndata = owndata 10 | 11 | @property 12 | def fnc(self): 13 | return self.f_contiguous and not self.c_contiguous 14 | 15 | @property 16 | def forc(self): 17 | return self.f_contiguous or self.c_contiguous 18 | 19 | def __getitem__(self, name): 20 | if name == 'C_CONTIGUOUS': 21 | return self.c_contiguous 22 | elif name == 'F_CONTIGUOUS': 23 | return self.f_contiguous 24 | elif name == 'OWNDATA': 25 | return self.owndata 26 | else: 27 | raise KeyError('%s is not defined for cupy.ndarray.flags' % name) 28 | 29 | def __repr__(self): 30 | t = ' %s : %s' 31 | ret = [] 32 | for name in 'C_CONTIGUOUS', 'F_CONTIGUOUS', 'OWNDATA': 33 | ret.append(t % (name, self[name])) 34 | return '\n'.join(ret) 35 | -------------------------------------------------------------------------------- /cupy/core/_routines_math.pxd: -------------------------------------------------------------------------------- 1 | from cupy.core.core cimport ndarray 2 | 3 | 4 | cdef ndarray _ndarray_conj(ndarray self) 5 | cdef ndarray _ndarray_real_getter(ndarray self) 6 | cdef ndarray _ndarray_real_setter(ndarray self, value) 7 | cdef ndarray _ndarray_imag_getter(ndarray self) 8 | cdef ndarray _ndarray_imag_setter(ndarray self, value) 9 | cdef ndarray _ndarray_prod(ndarray self, axis, dtype, out, keepdims) 10 | cdef ndarray _ndarray_sum(ndarray self, axis, dtype, out, keepdims) 11 | cdef ndarray _ndarray_cumsum(ndarray self, axis, dtype, out) 12 | cdef ndarray _ndarray_cumprod(ndarray self, axis, dtype, out) 13 | cdef ndarray _ndarray_clip(ndarray self, a_min, a_max, out) 14 | 15 | cdef ndarray scan(ndarray a, ndarray out=*) 16 | cdef object _sum_auto_dtype 17 | cdef object _add 18 | cdef object _conj 19 | cdef object _angle 20 | cdef object _real 21 | cdef object _imag 22 | cdef object _negative 23 | cdef object _multiply 24 | cdef object _divide 25 | cdef object _power 26 | cdef object _subtract 27 | cdef object _true_divide 28 | cdef object _floor_divide 29 | cdef object _remainder 30 | cdef object _absolute 31 | cdef object _sqrt 32 | -------------------------------------------------------------------------------- /tests/cupy_tests/cuda_tests/test_nccl.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import cuda 4 | from cupy.testing import attr 5 | 6 | 7 | @unittest.skipUnless(cuda.nccl_enabled, 'nccl is not installed') 8 | class TestNCCL(unittest.TestCase): 9 | 10 | @attr.gpu 11 | def test_single_proc_ring(self): 12 | id = cuda.nccl.get_unique_id() 13 | comm = cuda.nccl.NcclCommunicator(1, id, 0) 14 | assert 0 == comm.rank_id() 15 | comm.destroy() 16 | 17 | @attr.gpu 18 | @unittest.skipUnless(cuda.nccl_enabled and 19 | cuda.nccl.get_version() >= 2400, 'Using old NCCL') 20 | def test_abort(self): 21 | id = cuda.nccl.get_unique_id() 22 | comm = cuda.nccl.NcclCommunicator(1, id, 0) 23 | comm.abort() 24 | 25 | @attr.gpu 26 | @unittest.skipUnless(cuda.nccl_enabled and 27 | cuda.nccl.get_version() >= 2400, 'Using old NCCL') 28 | def test_check_async_error(self): 29 | id = cuda.nccl.get_unique_id() 30 | comm = cuda.nccl.NcclCommunicator(1, id, 0) 31 | comm.check_async_error() 32 | comm.destroy() 33 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_iter.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | @testing.gpu 7 | @testing.parameterize(*testing.product( 8 | {'shape': [(3,), (2, 3, 4), (0,), (0, 2), (3, 0)]}, 9 | )) 10 | class TestIter(unittest.TestCase): 11 | 12 | @testing.for_all_dtypes() 13 | @testing.numpy_cupy_array_list_equal() 14 | def test_list(self, xp, dtype): 15 | x = testing.shaped_arange(self.shape, xp, dtype) 16 | return list(x) 17 | 18 | @testing.for_all_dtypes() 19 | @testing.numpy_cupy_equal() 20 | def test_len(self, xp, dtype): 21 | x = testing.shaped_arange(self.shape, xp, dtype) 22 | return len(x) 23 | 24 | 25 | @testing.gpu 26 | class TestIterInvalid(unittest.TestCase): 27 | 28 | @testing.for_all_dtypes() 29 | @testing.numpy_cupy_raises() 30 | def test_iter(self, xp, dtype): 31 | x = testing.shaped_arange((), xp, dtype) 32 | iter(x) 33 | 34 | @testing.for_all_dtypes() 35 | @testing.numpy_cupy_raises() 36 | def test_len(self, xp, dtype): 37 | x = testing.shaped_arange((), xp, dtype) 38 | len(x) 39 | -------------------------------------------------------------------------------- /cupy/core/_routines_logic.pyx: -------------------------------------------------------------------------------- 1 | from cupy.core._kernel import create_reduction_func 2 | 3 | from cupy.core.core cimport ndarray 4 | 5 | 6 | cdef ndarray _ndarray_all(ndarray self, axis, out, keepdims): 7 | return _all(self, axis=axis, out=out, keepdims=keepdims) 8 | 9 | 10 | cdef ndarray _ndarray_any(ndarray self, axis, out, keepdims): 11 | return _any(self, axis=axis, out=out, keepdims=keepdims) 12 | 13 | 14 | cdef _all = create_reduction_func( 15 | 'cupy_all', 16 | ('?->?', 'B->?', 'h->?', 'H->?', 'i->?', 'I->?', 'l->?', 'L->?', 17 | 'q->?', 'Q->?', 'e->?', 'f->?', 'd->?', 'F->?', 'D->?'), 18 | ('in0 != type_in0_raw(0)', 'a & b', 'out0 = a', 'bool'), 19 | 'true', '') 20 | 21 | 22 | cdef _any = create_reduction_func( 23 | 'cupy_any', 24 | ('?->?', 'B->?', 'h->?', 'H->?', 'i->?', 'I->?', 'l->?', 'L->?', 25 | 'q->?', 'Q->?', 'e->?', 'f->?', 'd->?', 'F->?', 'D->?'), 26 | ('in0 != type_in0_raw(0)', 'a | b', 'out0 = a', 'bool'), 27 | 'false', '') 28 | 29 | 30 | # Variables to expose to Python 31 | # (cythonized data cannot be exposed to Python, even with cpdef.) 32 | 33 | 34 | all = _all 35 | any = _any 36 | -------------------------------------------------------------------------------- /cupy/core/_ufuncs.py: -------------------------------------------------------------------------------- 1 | from cupy.core._kernel import create_ufunc 2 | 3 | 4 | _complex_cast_copy = ''' 5 | template 6 | __device__ void cast_copy(const U& x, T& y) {y = T(x);} 7 | template 8 | __device__ void cast_copy(const complex& x, complex& y) { 9 | y = complex(x); 10 | } 11 | template 12 | __device__ void cast_copy(const complex& x, T& y) {y = T(x.real());} 13 | ''' 14 | 15 | 16 | elementwise_copy = create_ufunc( 17 | 'cupy_copy', 18 | ('?->?', 'b->b', 'B->B', 'h->h', 'H->H', 'i->i', 'I->I', 'l->l', 'L->L', 19 | 'q->q', 'Q->Q', 'e->e', 'f->f', 'd->d', 'F->F', 'D->D'), 20 | 'cast_copy(in0, out0)', 21 | preamble=_complex_cast_copy, default_casting='unsafe') 22 | 23 | 24 | elementwise_copy_where = create_ufunc( 25 | 'cupy_copy_where', 26 | ('??->?', 'b?->b', 'B?->B', 'h?->h', 'H?->H', 'i?->i', 'I?->I', 'l?->l', 27 | 'L?->L', 'q?->q', 'Q?->Q', 'e?->e', 'f?->f', 'd?->d', 'F?->F', 'D?->D'), 28 | 'if (in1) cast_copy(in0, out0)', 29 | preamble=_complex_cast_copy, default_casting='unsafe') 30 | # complex numbers requires out0 = complex(in0) 31 | -------------------------------------------------------------------------------- /cupy/logic/ops.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | logical_and = core.create_comparison( 4 | 'logical_and', '&&', 5 | '''Computes the logical AND of two arrays. 6 | 7 | .. seealso:: :data:`numpy.logical_and` 8 | 9 | ''') 10 | 11 | 12 | logical_or = core.create_comparison( 13 | 'logical_or', '||', 14 | '''Computes the logical OR of two arrays. 15 | 16 | .. seealso:: :data:`numpy.logical_or` 17 | 18 | ''') 19 | 20 | 21 | logical_not = core.create_ufunc( 22 | 'cupy_logical_not', 23 | ('?->?', 'b->?', 'B->?', 'h->?', 'H->?', 'i->?', 'I->?', 'l->?', 'L->?', 24 | 'q->?', 'Q->?', 'e->?', 'f->?', 'd->?'), 25 | 'out0 = !in0', 26 | doc='''Computes the logical NOT of an array. 27 | 28 | .. seealso:: :data:`numpy.logical_not` 29 | 30 | ''') 31 | 32 | 33 | logical_xor = core.create_ufunc( 34 | 'cupy_logical_xor', 35 | ('??->?', 'bb->?', 'BB->?', 'hh->?', 'HH->?', 'ii->?', 'II->?', 'll->?', 36 | 'LL->?', 'qq->?', 'QQ->?', 'ee->?', 'ff->?', 'dd->?'), 37 | 'out0 = !in0 != !in1', 38 | doc='''Computes the logical XOR of two arrays. 39 | 40 | .. seealso:: :data:`numpy.logical_xor` 41 | 42 | ''') 43 | -------------------------------------------------------------------------------- /tests/cupy_tests/cuda_tests/test_driver.py: -------------------------------------------------------------------------------- 1 | import threading 2 | import unittest 3 | 4 | import cupy 5 | from cupy.cuda import driver 6 | 7 | 8 | class TestDriver(unittest.TestCase): 9 | def test_ctxGetCurrent(self): 10 | # Make sure to create context. 11 | cupy.arange(1) 12 | self.assertNotEqual(0, driver.ctxGetCurrent()) 13 | 14 | def test_ctxGetCurrent_thread(self): 15 | # Make sure to create context in main thread. 16 | cupy.arange(1) 17 | 18 | def f(self): 19 | self._result0 = driver.ctxGetCurrent() 20 | cupy.arange(1) 21 | self._result1 = driver.ctxGetCurrent() 22 | 23 | self._result0 = None 24 | self._result1 = None 25 | t = threading.Thread(target=f, args=(self,)) 26 | t.daemon = True 27 | t.start() 28 | t.join() 29 | 30 | # The returned context pointer must be NULL on sub thread 31 | # without valid context. 32 | self.assertEqual(0, self._result0) 33 | 34 | # After the context is created, it should return the valid 35 | # context pointer. 36 | self.assertNotEqual(0, self._result1) 37 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_ndarray_math.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | from cupy import testing 6 | 7 | 8 | @testing.parameterize(*testing.product({ 9 | 'decimals': [-2, -1, 0, 1, 2], 10 | })) 11 | class TestRound(unittest.TestCase): 12 | 13 | shape = (20,) 14 | 15 | @testing.for_all_dtypes() 16 | @testing.numpy_cupy_allclose(atol=1e-5) 17 | def test_round(self, xp, dtype): 18 | if dtype == numpy.bool_: 19 | # avoid cast problem 20 | a = testing.shaped_random(self.shape, xp, scale=10, dtype=dtype) 21 | return a.round(0) 22 | if dtype == numpy.float16: 23 | # avoid accuracy problem 24 | a = testing.shaped_random(self.shape, xp, scale=10, dtype=dtype) 25 | return a.round(0) 26 | a = testing.shaped_random(self.shape, xp, scale=100, dtype=dtype) 27 | return a.round(self.decimals) 28 | 29 | @testing.numpy_cupy_array_equal() 30 | def test_round_out(self, xp): 31 | a = testing.shaped_random(self.shape, xp, scale=100, dtype='d') 32 | out = xp.empty_like(a) 33 | a.round(self.decimals, out) 34 | return out 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Preferred Infrastructure, Inc. 2 | Copyright (c) 2015 Preferred Networks, Inc. 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /tests/example_tests/test_gmm.py: -------------------------------------------------------------------------------- 1 | import os 2 | import shutil 3 | import tempfile 4 | import unittest 5 | 6 | import six 7 | 8 | from cupy import testing 9 | 10 | from example_tests import example_test 11 | 12 | 13 | os.environ['MPLBACKEND'] = 'Agg' 14 | 15 | 16 | @testing.with_requires('matplotlib') 17 | class TestGMM(unittest.TestCase): 18 | 19 | def test_gmm(self): 20 | output = example_test.run_example('gmm/gmm.py', '--num', '10') 21 | six.assertRegex( 22 | self, output.decode('utf-8'), 23 | r'Running CPU\.\.\.\s+train_accuracy : [0-9\.]+\s+' + 24 | r'test_accuracy : [0-9\.]+\s+CPU : [0-9\.]+ sec\s+' + 25 | r'Running GPU\.\.\.\s+train_accuracy : [0-9\.]+\s+' + 26 | r'test_accuracy : [0-9\.]+\s+GPU : [0-9\.]+ sec') 27 | 28 | def test_output_image(self): 29 | dir_path = tempfile.mkdtemp() 30 | try: 31 | image_path = os.path.join(dir_path, 'gmm.png') 32 | example_test.run_example( 33 | 'gmm/gmm.py', '--num', '10', '-o', image_path) 34 | self.assertTrue(os.path.exists(image_path)) 35 | finally: 36 | shutil.rmtree(dir_path, ignore_errors=True) 37 | -------------------------------------------------------------------------------- /cupy/cuda/curand.pxd: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Types 3 | ############################################################################### 4 | 5 | cdef extern from *: 6 | ctypedef int Ordering 'curandOrdering_t' 7 | ctypedef int RngType 'curandRngType_t' 8 | 9 | ctypedef void* Generator 'curandGenerator_t' 10 | 11 | 12 | ############################################################################### 13 | # Enum 14 | ############################################################################### 15 | 16 | cpdef enum: 17 | CURAND_RNG_PSEUDO_DEFAULT = 100 18 | CURAND_RNG_PSEUDO_XORWOW = 101 19 | CURAND_RNG_PSEUDO_MRG32K3A = 121 20 | CURAND_RNG_PSEUDO_MTGP32 = 141 21 | CURAND_RNG_PSEUDO_MT19937 = 142 22 | CURAND_RNG_PSEUDO_PHILOX4_32_10 = 161 23 | CURAND_RNG_QUASI_DEFAULT = 200 24 | CURAND_RNG_QUASI_SOBOL32 = 201 25 | CURAND_RNG_QUASI_SCRAMBLED_SOBOL32 = 202 26 | CURAND_RNG_QUASI_SOBOL64 = 203 27 | CURAND_RNG_QUASI_SCRAMBLED_SOBOL64 = 204 28 | 29 | CURAND_ORDERING_PSEUDO_BEST = 100 30 | CURAND_ORDERING_PSEUDO_DEFAULT = 101 31 | CURAND_ORDERING_PSEUDO_SEEDED = 102 32 | CURAND_ORDERING_QUASI_DEFAULT = 201 33 | -------------------------------------------------------------------------------- /cupyx/scipy/sparse/util.py: -------------------------------------------------------------------------------- 1 | import cupy 2 | from cupy.core import core 3 | 4 | 5 | def isdense(x): 6 | return isinstance(x, core.ndarray) 7 | 8 | 9 | def isintlike(x): 10 | try: 11 | return bool(int(x) == x) 12 | except (TypeError, ValueError): 13 | return False 14 | 15 | 16 | def isscalarlike(x): 17 | return cupy.isscalar(x) or (isdense(x) and x.ndim == 0) 18 | 19 | 20 | def isshape(x): 21 | if not isinstance(x, tuple) or len(x) != 2: 22 | return False 23 | m, n = x 24 | return isintlike(m) and isintlike(n) 25 | 26 | 27 | def validateaxis(axis): 28 | if axis is not None: 29 | axis_type = type(axis) 30 | 31 | if axis_type == tuple: 32 | raise TypeError( 33 | 'Tuples are not accepted for the \'axis\' ' 34 | 'parameter. Please pass in one of the ' 35 | 'following: {-2, -1, 0, 1, None}.') 36 | 37 | if not cupy.issubdtype(cupy.dtype(axis_type), cupy.integer): 38 | raise TypeError('axis must be an integer, not {name}' 39 | .format(name=axis_type.__name__)) 40 | 41 | if not (-2 <= axis <= 1): 42 | raise ValueError('axis out of range') 43 | -------------------------------------------------------------------------------- /cupy/linalg/__init__.py: -------------------------------------------------------------------------------- 1 | # Functions from the following NumPy document 2 | # https://docs.scipy.org/doc/numpy/reference/routines.linalg.html 3 | 4 | # "NOQA" to suppress flake8 warning 5 | from cupy.linalg import decomposition # NOQA 6 | from cupy.linalg import eigenvalue # NOQA 7 | from cupy.linalg import einsum # NOQA 8 | from cupy.linalg import norms # NOQA 9 | from cupy.linalg.norms import det # NOQA 10 | from cupy.linalg.norms import matrix_rank # NOQA 11 | from cupy.linalg.norms import norm # NOQA 12 | from cupy.linalg.norms import slogdet # NOQA 13 | from cupy.linalg import product # NOQA 14 | from cupy.linalg import solve # NOQA 15 | 16 | from cupy.linalg.decomposition import cholesky # NOQA 17 | from cupy.linalg.decomposition import qr # NOQA 18 | from cupy.linalg.decomposition import svd # NOQA 19 | 20 | from cupy.linalg.eigenvalue import eigh # NOQA 21 | from cupy.linalg.eigenvalue import eigvalsh # NOQA 22 | 23 | from cupy.linalg.solve import inv # NOQA 24 | from cupy.linalg.solve import pinv # NOQA 25 | from cupy.linalg.solve import solve # NOQA 26 | from cupy.linalg.solve import tensorinv # NOQA 27 | from cupy.linalg.solve import tensorsolve # NOQA 28 | 29 | from cupy.linalg.product import matrix_power # NOQA 30 | -------------------------------------------------------------------------------- /tests/cupy_tests/binary_tests/test_elementwise.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestElementwise(unittest.TestCase): 8 | 9 | @testing.for_int_dtypes() 10 | @testing.numpy_cupy_array_equal() 11 | def check_unary_int(self, name, xp, dtype): 12 | a = xp.array([-3, -2, -1, 0, 1, 2, 3], dtype=dtype) 13 | return getattr(xp, name)(a) 14 | 15 | @testing.for_int_dtypes() 16 | @testing.numpy_cupy_array_equal() 17 | def check_binary_int(self, name, xp, dtype): 18 | a = xp.array([-3, -2, -1, 0, 1, 2, 3], dtype=dtype) 19 | b = xp.array([0, 1, 2, 3, 4, 5, 6], dtype=dtype) 20 | return getattr(xp, name)(a, b) 21 | 22 | def test_bitwise_and(self): 23 | self.check_binary_int('bitwise_and') 24 | 25 | def test_bitwise_or(self): 26 | self.check_binary_int('bitwise_or') 27 | 28 | def test_bitwise_xor(self): 29 | self.check_binary_int('bitwise_xor') 30 | 31 | def test_invert(self): 32 | self.check_unary_int('invert') 33 | 34 | def test_left_shift(self): 35 | self.check_binary_int('left_shift') 36 | 37 | def test_right_shift(self): 38 | self.check_binary_int('right_shift') 39 | -------------------------------------------------------------------------------- /cupy/sorting/count.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | 4 | def count_nonzero(a, axis=None): 5 | """Counts the number of non-zero values in the array. 6 | 7 | .. note:: 8 | 9 | :func:`numpy.count_nonzero` returns `int` value when `axis=None`, 10 | but :func:`cupy.count_nonzero` returns zero-dimensional array to reduce 11 | CPU-GPU synchronization. 12 | 13 | Args: 14 | a (cupy.ndarray): The array for which to count non-zeros. 15 | axis (int or tuple, optional): Axis or tuple of axes along which to 16 | count non-zeros. Default is None, meaning that non-zeros will be 17 | counted along a flattened version of ``a`` 18 | Returns: 19 | cupy.ndarray of int: Number of non-zero values in the array 20 | along a given axis. Otherwise, the total number of non-zero values 21 | in the array is returned. 22 | """ 23 | 24 | return _count_nonzero(a, axis=axis) 25 | 26 | 27 | _count_nonzero = core.create_reduction_func( 28 | 'cupy_count_nonzero', 29 | ('?->l', 'B->l', 'h->l', 'H->l', 'i->l', 'I->l', 'l->l', 'L->l', 30 | 'q->l', 'Q->l', 'e->l', 'f->l', 'd->l', 'F->l', 'D->l'), 31 | ('in0 != type_in0_raw(0)', 'a + b', 'out0 = a', None), 0) 32 | -------------------------------------------------------------------------------- /tests/cupy_tests/math_tests/test_hyperbolic.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestHyperbolic(unittest.TestCase): 8 | 9 | @testing.for_all_dtypes() 10 | @testing.numpy_cupy_allclose(atol=1e-5) 11 | def check_unary(self, name, xp, dtype): 12 | a = testing.shaped_arange((2, 3), xp, dtype) 13 | return getattr(xp, name)(a) 14 | 15 | @testing.for_dtypes(['e', 'f', 'd']) 16 | @testing.numpy_cupy_allclose(atol=1e-5) 17 | def check_unary_unit(self, name, xp, dtype): 18 | a = xp.array([0.2, 0.4, 0.6, 0.8], dtype=dtype) 19 | return getattr(xp, name)(a) 20 | 21 | def test_sinh(self): 22 | self.check_unary('sinh') 23 | 24 | def test_cosh(self): 25 | self.check_unary('cosh') 26 | 27 | def test_tanh(self): 28 | self.check_unary('tanh') 29 | 30 | def test_arcsinh(self): 31 | self.check_unary('arcsinh') 32 | 33 | @testing.for_dtypes(['e', 'f', 'd']) 34 | @testing.numpy_cupy_allclose(atol=1e-5) 35 | def test_arccosh(self, xp, dtype): 36 | a = xp.array([1, 2, 3], dtype=dtype) 37 | return xp.arccosh(a) 38 | 39 | def test_arctanh(self): 40 | self.check_unary_unit('arctanh') 41 | -------------------------------------------------------------------------------- /tests/cupyx_tests/scipy_tests/special_tests/test_statistics.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import cupy 4 | from cupy import testing 5 | import cupyx.scipy.special # NOQA 6 | 7 | 8 | class _TestBase(object): 9 | 10 | def test_ndtr(self): 11 | self.check_unary('ndtr') 12 | 13 | 14 | @testing.gpu 15 | @testing.with_requires('scipy') 16 | class TestSpecial(unittest.TestCase, _TestBase): 17 | 18 | @testing.for_dtypes(['e', 'f', 'd']) 19 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 20 | def check_unary(self, name, xp, scp, dtype): 21 | import scipy.special # NOQA 22 | 23 | a = testing.shaped_arange((2, 3), xp, dtype) 24 | return getattr(scp.special, name)(a) 25 | 26 | 27 | @testing.gpu 28 | @testing.with_requires('scipy') 29 | class TestFusionSpecial(unittest.TestCase, _TestBase): 30 | 31 | @testing.for_dtypes(['e', 'f', 'd']) 32 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 33 | def check_unary(self, name, xp, scp, dtype): 34 | import scipy.special # NOQA 35 | 36 | a = testing.shaped_arange((2, 3), xp, dtype) 37 | 38 | @cupy.fuse() 39 | def f(x): 40 | return getattr(scp.special, name)(x) 41 | 42 | return f(a) 43 | -------------------------------------------------------------------------------- /tests/install_tests/test_build.py: -------------------------------------------------------------------------------- 1 | from distutils import ccompiler 2 | from distutils import sysconfig 3 | import unittest 4 | 5 | import pytest 6 | 7 | from install import build 8 | 9 | 10 | class TestCheckVersion(unittest.TestCase): 11 | 12 | def setUp(self): 13 | self.compiler = ccompiler.new_compiler() 14 | sysconfig.customize_compiler(self.compiler) 15 | self.settings = build.get_compiler_setting() 16 | 17 | @pytest.mark.gpu 18 | def test_check_cuda_version(self): 19 | with self.assertRaises(RuntimeError): 20 | build.get_cuda_version() 21 | self.assertTrue(build.check_cuda_version( 22 | self.compiler, self.settings)) 23 | self.assertIsInstance(build.get_cuda_version(), int) 24 | self.assertIsInstance(build.get_cuda_version(True), str) 25 | 26 | @pytest.mark.gpu 27 | @pytest.mark.cudnn 28 | def test_check_cudnn_version(self): 29 | with self.assertRaises(RuntimeError): 30 | build.get_cudnn_version() 31 | self.assertTrue(build.check_cudnn_version( 32 | self.compiler, self.settings)) 33 | self.assertIsInstance(build.get_cudnn_version(), int) 34 | self.assertIsInstance(build.get_cudnn_version(True), str) 35 | -------------------------------------------------------------------------------- /docs/source/reference/creation.rst: -------------------------------------------------------------------------------- 1 | Array Creation Routines 2 | ======================= 3 | 4 | .. https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html 5 | 6 | Basic creation routines 7 | ----------------------- 8 | 9 | .. autosummary:: 10 | :toctree: generated/ 11 | :nosignatures: 12 | 13 | cupy.empty 14 | cupy.empty_like 15 | cupy.eye 16 | cupy.identity 17 | cupy.ones 18 | cupy.ones_like 19 | cupy.zeros 20 | cupy.zeros_like 21 | cupy.full 22 | cupy.full_like 23 | 24 | 25 | Creation from other data 26 | ------------------------ 27 | 28 | .. autosummary:: 29 | :toctree: generated/ 30 | :nosignatures: 31 | 32 | cupy.array 33 | cupy.asarray 34 | cupy.asanyarray 35 | cupy.ascontiguousarray 36 | cupy.copy 37 | 38 | 39 | Numerical ranges 40 | ---------------- 41 | 42 | .. autosummary:: 43 | :toctree: generated/ 44 | :nosignatures: 45 | 46 | cupy.arange 47 | cupy.linspace 48 | cupy.logspace 49 | cupy.meshgrid 50 | cupy.mgrid 51 | cupy.ogrid 52 | 53 | 54 | Matrix creation 55 | --------------- 56 | 57 | .. autosummary:: 58 | :toctree: generated/ 59 | :nosignatures: 60 | 61 | cupy.diag 62 | cupy.diagflat 63 | cupy.tri 64 | cupy.tril 65 | cupy.triu 66 | -------------------------------------------------------------------------------- /cupy/cuda/cupy_nvrtc.h: -------------------------------------------------------------------------------- 1 | // This file is a stub header file of nvrtc for Read the Docs. 2 | 3 | #ifndef INCLUDE_GUARD_CUPY_NVRTC_H 4 | #define INCLUDE_GUARD_CUPY_NVRTC_H 5 | 6 | #ifndef CUPY_NO_CUDA 7 | 8 | #include 9 | 10 | #else // #ifndef CUPY_NO_CUDA 11 | 12 | extern "C" { 13 | 14 | typedef enum { 15 | NVRTC_SUCCESS = 0, 16 | } nvrtcResult; 17 | 18 | typedef struct _nvrtcProgram *nvrtcProgram; 19 | 20 | const char *nvrtcGetErrorString(...) { 21 | return NULL; 22 | } 23 | 24 | nvrtcResult nvrtcVersion(...) { 25 | return NVRTC_SUCCESS; 26 | } 27 | 28 | nvrtcResult nvrtcCreateProgram(...) { 29 | return NVRTC_SUCCESS; 30 | } 31 | 32 | nvrtcResult nvrtcDestroyProgram(...) { 33 | return NVRTC_SUCCESS; 34 | } 35 | 36 | nvrtcResult nvrtcCompileProgram(...) { 37 | return NVRTC_SUCCESS; 38 | } 39 | 40 | nvrtcResult nvrtcGetPTXSize(...) { 41 | return NVRTC_SUCCESS; 42 | } 43 | 44 | nvrtcResult nvrtcGetPTX(...) { 45 | return NVRTC_SUCCESS; 46 | } 47 | 48 | nvrtcResult nvrtcGetProgramLogSize(...) { 49 | return NVRTC_SUCCESS; 50 | } 51 | 52 | nvrtcResult nvrtcGetProgramLog(...) { 53 | return NVRTC_SUCCESS; 54 | } 55 | 56 | } 57 | 58 | #endif // #ifndef CUPY_NO_CUDA 59 | 60 | #endif // #ifndef INCLUDE_GUARD_CUPY_NVRTC_H 61 | -------------------------------------------------------------------------------- /cupy/math/hyperbolic.py: -------------------------------------------------------------------------------- 1 | from cupy.math import ufunc 2 | 3 | 4 | sinh = ufunc.create_math_ufunc( 5 | 'sinh', 1, 'cupy_sinh', 6 | '''Elementwise hyperbolic sine function. 7 | 8 | .. seealso:: :data:`numpy.sinh` 9 | 10 | ''') 11 | 12 | 13 | cosh = ufunc.create_math_ufunc( 14 | 'cosh', 1, 'cupy_cosh', 15 | '''Elementwise hyperbolic cosine function. 16 | 17 | .. seealso:: :data:`numpy.cosh` 18 | 19 | ''') 20 | 21 | 22 | tanh = ufunc.create_math_ufunc( 23 | 'tanh', 1, 'cupy_tanh', 24 | '''Elementwise hyperbolic tangent function. 25 | 26 | .. seealso:: :data:`numpy.tanh` 27 | 28 | ''') 29 | 30 | 31 | arcsinh = ufunc.create_math_ufunc( 32 | 'asinh', 1, 'cupy_arcsinh', 33 | '''Elementwise inverse of hyperbolic sine function. 34 | 35 | .. seealso:: :data:`numpy.arcsinh` 36 | 37 | ''') 38 | 39 | 40 | arccosh = ufunc.create_math_ufunc( 41 | 'acosh', 1, 'cupy_arccosh', 42 | '''Elementwise inverse of hyperbolic cosine function. 43 | 44 | .. seealso:: :data:`numpy.arccosh` 45 | 46 | ''') 47 | 48 | 49 | arctanh = ufunc.create_math_ufunc( 50 | 'atanh', 1, 'cupy_arctanh', 51 | '''Elementwise inverse of hyperbolic tangent function. 52 | 53 | .. seealso:: :data:`numpy.arctanh` 54 | 55 | ''') 56 | -------------------------------------------------------------------------------- /docs/source/_templates/autosummary/class.rst: -------------------------------------------------------------------------------- 1 | {{ fullname }} 2 | {{ underline }} 3 | 4 | .. currentmodule:: {{ module }} 5 | 6 | .. autoclass:: {{ objname }} 7 | 8 | .. 9 | Methods 10 | 11 | {% block methods %} 12 | 13 | .. rubric:: Methods 14 | 15 | .. 16 | Special methods 17 | 18 | {% for item in ('__call__', '__enter__', '__exit__', '__getitem__', '__setitem__', '__len__', '__next__', '__iter__', '__copy__') %} 19 | {% if item in all_methods or item in all_attributes %} 20 | .. automethod:: {{ item }} 21 | {% endif %} 22 | {%- endfor %} 23 | 24 | .. 25 | Ordinary methods 26 | 27 | {% for item in methods %} 28 | {% if item not in ('__init__',) %} 29 | .. automethod:: {{ item }} 30 | {% endif %} 31 | {%- endfor %} 32 | 33 | .. 34 | Special methods 35 | 36 | {% for item in ('__eq__', '__ne__', '__lt__', '__le__', '__gt__', '__ge__', '__nonzero__', '__bool__') %} 37 | {% if item in all_methods %} 38 | .. automethod:: {{ item }} 39 | {% endif %} 40 | {%- endfor %} 41 | {% endblock %} 42 | 43 | .. 44 | Atributes 45 | 46 | {% block attributes %} {% if attributes %} 47 | 48 | .. rubric:: Attributes 49 | 50 | {% for item in attributes %} 51 | .. autoattribute:: {{ item }} 52 | {%- endfor %} 53 | {% endif %} {% endblock %} 54 | -------------------------------------------------------------------------------- /cupy/cuda/cusparse.pxd: -------------------------------------------------------------------------------- 1 | cdef extern from *: 2 | ctypedef int IndexBase 'cusparseIndexBase_t' 3 | ctypedef int Status 'cusparseStatus_t' 4 | 5 | ctypedef void* Handle 'cusparseHandle_t' 6 | 7 | ctypedef void* MatDescr 'cusparseMatDescr_t' 8 | 9 | ctypedef int Direction 'cusparseDirection_t' 10 | 11 | ctypedef int MatrixType 'cusparseMatrixType_t' 12 | 13 | ctypedef int Operation 'cusparseOperation_t' 14 | 15 | ctypedef int PointerMode 'cusparsePointerMode_t' 16 | 17 | ctypedef int Action 'cusparseAction_t' 18 | 19 | 20 | cpdef enum: 21 | CUSPARSE_POINTER_MODE_HOST = 0 22 | CUSPARSE_POINTER_MODE_DEVICE = 1 23 | 24 | CUSPARSE_ACTION_SYMBOLIC = 0 25 | CUSPARSE_ACTION_NUMERIC = 1 26 | 27 | CUSPARSE_INDEX_BASE_ZERO = 0 28 | CUSPARSE_INDEX_BASE_ONE = 1 29 | 30 | CUSPARSE_MATRIX_TYPE_GENERAL = 0 31 | CUSPARSE_MATRIX_TYPE_SYMMETRIC = 1 32 | CUSPARSE_MATRIX_TYPE_HERMITIAN = 2 33 | CUSPARSE_MATRIX_TYPE_TRIANGULAR = 3 34 | 35 | CUSPARSE_OPERATION_NON_TRANSPOSE = 0 36 | CUSPARSE_OPERATION_TRANSPOSE = 1 37 | CUSPARSE_OPERATION_CONJUGATE_TRANSPOSE = 2 38 | 39 | CUSPARSE_DIRECTION_ROW = 0 40 | CUSPARSE_DIRECTION_COLUMN = 1 41 | 42 | 43 | cpdef size_t create() except? 0 44 | cpdef destroy(size_t handle) 45 | -------------------------------------------------------------------------------- /tests/cupy_tests/manipulation_tests/test_add_remove.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | 5 | 6 | @testing.gpu 7 | class TestUnique(unittest.TestCase): 8 | 9 | @testing.for_all_dtypes(no_float16=True, no_bool=True, no_complex=True) 10 | @testing.numpy_cupy_array_equal() 11 | def test_unique(self, xp, dtype): 12 | a = testing.shaped_random((100, 100), xp, dtype) 13 | return xp.unique(a) 14 | 15 | @testing.for_all_dtypes(no_float16=True, no_bool=True, no_complex=True) 16 | @testing.numpy_cupy_array_equal() 17 | def test_unique_index(self, xp, dtype): 18 | a = testing.shaped_random((100, 100), xp, dtype) 19 | return xp.unique(a, return_index=True)[1] 20 | 21 | @testing.for_all_dtypes(no_float16=True, no_bool=True, no_complex=True) 22 | @testing.numpy_cupy_array_equal() 23 | def test_unique_inverse(self, xp, dtype): 24 | a = testing.shaped_random((100, 100), xp, dtype) 25 | return xp.unique(a, return_inverse=True)[1] 26 | 27 | @testing.for_all_dtypes(no_float16=True, no_bool=True, no_complex=True) 28 | @testing.numpy_cupy_array_equal() 29 | def test_unique_counts(self, xp, dtype): 30 | a = testing.shaped_random((100, 100), xp, dtype) 31 | return xp.unique(a, return_counts=True)[1] 32 | -------------------------------------------------------------------------------- /cupy/cuda/cupy_thrust.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_GUARD_CUPY_CUDA_THRUST_H 2 | #define INCLUDE_GUARD_CUPY_CUDA_THRUST_H 3 | 4 | #ifndef CUPY_NO_CUDA 5 | 6 | namespace cupy { 7 | 8 | namespace thrust { 9 | 10 | template 11 | void _sort(void *, size_t *, const std::vector&, size_t, void *); 12 | 13 | template 14 | void _lexsort(size_t *, void *, size_t, size_t, size_t, void *); 15 | 16 | template 17 | void _argsort(size_t *, void *, void *, const std::vector&, size_t, 18 | void *); 19 | 20 | } // namespace thrust 21 | 22 | } // namespace cupy 23 | 24 | #else // CUPY_NO_CUDA 25 | 26 | #include "cupy_common.h" 27 | 28 | namespace cupy { 29 | 30 | namespace thrust { 31 | 32 | template 33 | void _sort(void *, size_t *, const std::vector&, size_t, void *) { 34 | return; 35 | } 36 | 37 | template 38 | void _lexsort(size_t *, void *, size_t, size_t, size_t, void *) { 39 | return; 40 | } 41 | 42 | template 43 | void _argsort(size_t *, void *, void *, const std::vector&, size_t, 44 | void *) { 45 | return; 46 | } 47 | 48 | } // namespace thrust 49 | 50 | } // namespace cupy 51 | 52 | #endif // #ifndef CUPY_NO_CUDA 53 | 54 | #endif // INCLUDE_GUARD_CUPY_CUDA_THRUST_H 55 | -------------------------------------------------------------------------------- /docs/source/reference/logic.rst: -------------------------------------------------------------------------------- 1 | Logic Functions 2 | =============== 3 | 4 | .. https://docs.scipy.org/doc/numpy/reference/routines.logic.html 5 | 6 | Truth value testing 7 | ------------------- 8 | 9 | .. autosummary:: 10 | :toctree: generated/ 11 | :nosignatures: 12 | 13 | cupy.all 14 | cupy.any 15 | 16 | 17 | Infinities and NaNs 18 | ------------------- 19 | 20 | .. autosummary:: 21 | :toctree: generated/ 22 | :nosignatures: 23 | 24 | cupy.isfinite 25 | cupy.isinf 26 | cupy.isnan 27 | 28 | 29 | Array type testing 30 | ------------------ 31 | 32 | .. autosummary:: 33 | :toctree: generated/ 34 | :nosignatures: 35 | 36 | cupy.iscomplex 37 | cupy.iscomplexobj 38 | cupy.isfortran 39 | cupy.isreal 40 | cupy.isrealobj 41 | cupy.isscalar 42 | 43 | 44 | Logic operations 45 | ---------------- 46 | 47 | .. autosummary:: 48 | :toctree: generated/ 49 | :nosignatures: 50 | 51 | cupy.logical_and 52 | cupy.logical_or 53 | cupy.logical_not 54 | cupy.logical_xor 55 | 56 | 57 | Comparison 58 | ---------- 59 | 60 | .. autosummary:: 61 | :toctree: generated/ 62 | :nosignatures: 63 | 64 | cupy.allclose 65 | cupy.isclose 66 | cupy.greater 67 | cupy.greater_equal 68 | cupy.less 69 | cupy.less_equal 70 | cupy.equal 71 | cupy.not_equal 72 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | exclude = .eggs,*.egg,build,docs,.git 3 | 4 | [pep8] 5 | exclude = .eggs,*.egg,build,.git 6 | 7 | [tool:pytest] 8 | filterwarnings= ignore::FutureWarning 9 | ignore::UserWarning 10 | error::DeprecationWarning 11 | error::PendingDeprecationWarning 12 | error::numpy.ComplexWarning 13 | # importing old SciPy is warned because it tries to 14 | # import nose via numpy.testing 15 | ignore::DeprecationWarning:scipy\._lib\._numpy_compat 16 | # importing stats from old SciPy is warned because it tries to 17 | # import numpy.testing.decorators 18 | ignore::DeprecationWarning:scipy\.stats\.morestats 19 | # Using `scipy.sparse` against NumPy 1.15+ raises warning 20 | # as it uses `np.matrix` which is pending deprecation. 21 | # Also exclude `numpy.matrixlib.defmatrix` as SciPy and our 22 | # test code uses `np.asmatrix`, which internally calls 23 | # `np.matrix`. 24 | ignore::PendingDeprecationWarning:scipy\.sparse\.\w+ 25 | ignore::PendingDeprecationWarning:numpy\.matrixlib\.defmatrix 26 | 27 | [metadata] 28 | license_file = docs/source/license.rst 29 | -------------------------------------------------------------------------------- /docs/source/_static/css/modified_theme.css: -------------------------------------------------------------------------------- 1 | @import url('theme.css'); 2 | 3 | /* Main background color modification */ 4 | .btn-info, .wy-menu-vertical a:active, .wy-side-nav-search, .wy-side-nav-search img, .wy-nav .wy-menu-vertical a:hover, .wy-nav-top img, .wy-tray-item-info, .wy-side-nav-search, .wy-dropdown-menu > dd > a:hover, .wy-dropdown-menu a:hover, .wy-nav-top { 5 | background-color: #70c162 !important; 6 | } 7 | 8 | .wy-side-nav-search input[type=text] { 9 | border-color: #72a424 !important; 10 | } 11 | 12 | .rst-content .note .admonition-title { 13 | background-color: #c9c9c9 !important; 14 | } 15 | 16 | .rst-content .note { 17 | background-color: #e3e3e3 !important; 18 | } 19 | 20 | 21 | /* code style */ 22 | .rst-content table.longtable.docutils code { 23 | font-size: 100% !important; 24 | background-color: transparent !important; 25 | border: none !important; 26 | } 27 | 28 | .rst-content a.reference code { 29 | color: #3399cc !important; 30 | } 31 | 32 | .rst-content a.reference code:hover { 33 | text-decoration: underline !important; 34 | color: #3366cc !important; 35 | } 36 | 37 | 38 | /* rubric */ 39 | .rst-content .class .rubric { 40 | color: #333333; 41 | background-color: #aaeeaa; 42 | padding: 0.2em 0 0.2em 1em; 43 | border-top: solid 0.3em #66cc66; 44 | } 45 | -------------------------------------------------------------------------------- /cupy/lib/stride_tricks.py: -------------------------------------------------------------------------------- 1 | import cupy 2 | 3 | 4 | def as_strided(x, shape=None, strides=None): 5 | """ 6 | Create a view into the array with the given shape and strides. 7 | .. warning:: This function has to be used with extreme care, see notes. 8 | 9 | Parameters 10 | ---------- 11 | x : ndarray 12 | Array to create a new. 13 | shape : sequence of int, optional 14 | The shape of the new array. Defaults to ``x.shape``. 15 | strides : sequence of int, optional 16 | The strides of the new array. Defaults to ``x.strides``. 17 | 18 | Returns 19 | ------- 20 | view : ndarray 21 | 22 | See also 23 | -------- 24 | numpy.lib.stride_tricks.as_strided 25 | reshape : reshape an array. 26 | 27 | Notes 28 | ----- 29 | ``as_strided`` creates a view into the array given the exact strides 30 | and shape. This means it manipulates the internal data structure of 31 | ndarray and, if done incorrectly, the array elements can point to 32 | invalid memory and can corrupt results or crash your program. 33 | """ 34 | shape = x.shape if shape is None else tuple(shape) 35 | strides = x.strides if strides is None else tuple(strides) 36 | 37 | return cupy.ndarray(shape=shape, dtype=x.dtype, 38 | memptr=x.data, strides=strides) 39 | -------------------------------------------------------------------------------- /tests/cupy_tests/io_tests/test_base_n.py: -------------------------------------------------------------------------------- 1 | from cupy import testing 2 | 3 | 4 | class TestBinaryRepr(testing.NumpyAliasBasicTestBase): 5 | 6 | func = 'binary_repr' 7 | 8 | 9 | @testing.parameterize( 10 | *testing.product({ 11 | 'args': [ 12 | (0,), (3,), (-3,), 13 | (0, 0), 14 | (3, 5), 15 | (-3, 5), 16 | # TODO(unno): Insuffisicent width is deprecated in numpy>=1.13. 17 | # We need to check if it cause a warning, and maybe it causes an 18 | # error in the future. 19 | # (3, 0), 20 | # (-3, 0), 21 | ]})) 22 | class TestBinaryReprValues(testing.NumpyAliasValuesTestBase): 23 | 24 | func = 'binary_repr' 25 | 26 | 27 | class TestBaseRepr(testing.NumpyAliasBasicTestBase): 28 | 29 | func = 'base_repr' 30 | 31 | 32 | @testing.parameterize( 33 | *testing.product({ 34 | 'args': [ 35 | (0,), (5,), (-5,), 36 | (0, 2), (0, 10), (0, 36), 37 | (5, 2), (5, 10), (5, 36), 38 | (-5, 2), (-5, 10), (-5, 36), 39 | (-5, 2, 0), 40 | (-5, 2, 2), 41 | (-5, 2, 10), 42 | (5, 2, 0), 43 | (5, 2, 2), 44 | (5, 2, 10), 45 | ]})) 46 | class TestBaseReprValues(testing.NumpyAliasValuesTestBase): 47 | 48 | func = 'base_repr' 49 | -------------------------------------------------------------------------------- /cupy/linalg/util.py: -------------------------------------------------------------------------------- 1 | from numpy import linalg 2 | 3 | import cupy 4 | from cupy import core 5 | 6 | 7 | def _assert_cupy_array(*arrays): 8 | for a in arrays: 9 | if not isinstance(a, cupy.core.ndarray): 10 | raise linalg.LinAlgError( 11 | 'cupy.linalg only supports cupy.core.ndarray') 12 | 13 | 14 | def _assert_rank2(*arrays): 15 | for a in arrays: 16 | if a.ndim != 2: 17 | raise linalg.LinAlgError( 18 | '{}-dimensional array given. Array must be ' 19 | 'two-dimensional'.format(a.ndim)) 20 | 21 | 22 | def _assert_nd_squareness(*arrays): 23 | for a in arrays: 24 | if max(a.shape[-2:]) != min(a.shape[-2:]): 25 | raise linalg.LinAlgError( 26 | 'Last 2 dimensions of the array must be square') 27 | 28 | 29 | _tril_kernel = core.ElementwiseKernel( 30 | 'int64 k', 'S x', 31 | 'x = (_ind.get()[1] - _ind.get()[0] <= k) ? x : 0', 32 | 'tril_kernel', 33 | reduce_dims=False 34 | ) 35 | 36 | 37 | def _tril(x, k=0): 38 | _tril_kernel(k, x) 39 | return x 40 | 41 | 42 | _triu_kernel = core.ElementwiseKernel( 43 | 'int64 k', 'S x', 44 | 'x = (_ind.get()[1] - _ind.get()[0] >= k) ? x : 0', 45 | 'triu_kernel', 46 | reduce_dims=False 47 | ) 48 | 49 | 50 | def _triu(x, k=0): 51 | _triu_kernel(k, x) 52 | return x 53 | -------------------------------------------------------------------------------- /cupy/core/_routines_manipulation.pxd: -------------------------------------------------------------------------------- 1 | from libcpp cimport vector 2 | 3 | from cupy.core.core cimport ndarray 4 | 5 | 6 | cdef class broadcast: 7 | cdef: 8 | readonly tuple values 9 | readonly tuple shape 10 | readonly Py_ssize_t size 11 | readonly Py_ssize_t nd 12 | 13 | cdef tuple _broadcast_core(arrays) 14 | cdef _ndarray_shape_setter(ndarray self, newshape) 15 | cdef ndarray _ndarray_reshape(ndarray self, tuple shape, order) 16 | cdef ndarray _ndarray_transpose(ndarray self, tuple axes) 17 | cdef ndarray _ndarray_swapaxes( 18 | ndarray self, Py_ssize_t axis1, Py_ssize_t axis2) 19 | cdef ndarray _ndarray_flatten(ndarray self) 20 | cdef ndarray _ndarray_ravel(ndarray self, order) 21 | cdef ndarray _ndarray_squeeze(ndarray self, axis) 22 | cdef ndarray _ndarray_repeat(ndarray self, repeats, axis) 23 | 24 | cpdef ndarray moveaxis(ndarray a, source, destination) 25 | cpdef ndarray rollaxis(ndarray a, Py_ssize_t axis, Py_ssize_t start=*) 26 | cpdef ndarray broadcast_to(ndarray array, shape) 27 | cpdef ndarray _reshape(ndarray self, 28 | const vector.vector[Py_ssize_t] &shape_spec) 29 | cpdef ndarray _T(ndarray self) 30 | cpdef ndarray _transpose(ndarray self, const vector.vector[Py_ssize_t] &axes) 31 | cpdef ndarray _concatenate(list arrays, Py_ssize_t axis, tuple shape, dtype) 32 | cpdef ndarray concatenate_method(tup, int axis) 33 | -------------------------------------------------------------------------------- /tests/example_tests/test_kmeans.py: -------------------------------------------------------------------------------- 1 | import os 2 | import shutil 3 | import tempfile 4 | import unittest 5 | 6 | import six 7 | 8 | from cupy import testing 9 | 10 | from example_tests import example_test 11 | 12 | 13 | os.environ['MPLBACKEND'] = 'Agg' 14 | 15 | 16 | @testing.with_requires('matplotlib') 17 | class TestKmeans(unittest.TestCase): 18 | 19 | def test_default(self): 20 | output = example_test.run_example( 21 | 'kmeans/kmeans.py', '-m', '1', '--num', '10') 22 | six.assertRegex( 23 | self, output.decode('utf-8'), 24 | r' CPU : [0-9\.]+ sec\s+GPU : [0-9\.]+ sec') 25 | 26 | def test_custom_kernel(self): 27 | output = example_test.run_example( 28 | 'kmeans/kmeans.py', '-m', '1', '--num', '10', 29 | '--use-custom-kernel') 30 | six.assertRegex( 31 | self, output.decode('utf-8'), 32 | r' CPU : [0-9\.]+ sec\s+GPU : [0-9\.]+ sec') 33 | 34 | def test_result_image(self): 35 | dir_path = tempfile.mkdtemp() 36 | try: 37 | image_path = os.path.join(dir_path, 'kmeans.png') 38 | example_test.run_example( 39 | 'kmeans/kmeans.py', '-m', '1', '--num', '10', '-o', image_path) 40 | self.assertTrue(os.path.exists(image_path)) 41 | finally: 42 | shutil.rmtree(dir_path, ignore_errors=True) 43 | -------------------------------------------------------------------------------- /tests/cupy_tests/cuda_tests/test_curand.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import cupy 6 | from cupy.cuda import curand 7 | 8 | 9 | class TestGenerateNormal(unittest.TestCase): 10 | 11 | def setUp(self): 12 | self.generator = curand.createGenerator( 13 | curand.CURAND_RNG_PSEUDO_DEFAULT) 14 | 15 | def test_invalid_argument_normal_float(self): 16 | out = cupy.empty((1,), dtype=numpy.float32) 17 | with self.assertRaises(ValueError): 18 | curand.generateNormal( 19 | self.generator, out.data.ptr, 1, 0.0, 1.0) 20 | 21 | def test_invalid_argument_normal_double(self): 22 | out = cupy.empty((1,), dtype=numpy.float64) 23 | with self.assertRaises(ValueError): 24 | curand.generateNormalDouble( 25 | self.generator, out.data.ptr, 1, 0.0, 1.0) 26 | 27 | def test_invalid_argument_log_normal_float(self): 28 | out = cupy.empty((1,), dtype=numpy.float32) 29 | with self.assertRaises(ValueError): 30 | curand.generateLogNormal( 31 | self.generator, out.data.ptr, 1, 0.0, 1.0) 32 | 33 | def test_invalid_argument_log_normal_double(self): 34 | out = cupy.empty((1,), dtype=numpy.float64) 35 | with self.assertRaises(ValueError): 36 | curand.generateLogNormalDouble( 37 | self.generator, out.data.ptr, 1, 0.0, 1.0) 38 | -------------------------------------------------------------------------------- /docs/source/reference/special.rst: -------------------------------------------------------------------------------- 1 | Special Functions 2 | ================= 3 | 4 | .. https://docs.scipy.org/doc/scipy/reference/special.html 5 | 6 | Bessel Functions 7 | ---------------- 8 | 9 | .. autosummary:: 10 | :toctree: generated/ 11 | :nosignatures: 12 | 13 | cupyx.scipy.special.j0 14 | cupyx.scipy.special.j1 15 | cupyx.scipy.special.y0 16 | cupyx.scipy.special.y1 17 | cupyx.scipy.special.i0 18 | cupyx.scipy.special.i1 19 | 20 | 21 | Gamma and Related Functions 22 | --------------------------- 23 | 24 | .. autosummary:: 25 | :toctree: generated/ 26 | :nosignatures: 27 | 28 | cupyx.scipy.special.gamma 29 | cupyx.scipy.special.gammaln 30 | cupyx.scipy.special.polygamma 31 | cupyx.scipy.special.digamma 32 | 33 | 34 | Raw Statistical Functions 35 | ------------------------- 36 | 37 | .. autosummary:: 38 | :toctree: generated/ 39 | :nosignatures: 40 | 41 | cupyx.scipy.special.ndtr 42 | 43 | 44 | Error Function 45 | -------------- 46 | 47 | .. autosummary:: 48 | :toctree: generated/ 49 | :nosignatures: 50 | 51 | cupyx.scipy.special.erf 52 | cupyx.scipy.special.erfc 53 | cupyx.scipy.special.erfcx 54 | cupyx.scipy.special.erfinv 55 | cupyx.scipy.special.erfcinv 56 | 57 | 58 | Other Special Functions 59 | ----------------------- 60 | 61 | .. autosummary:: 62 | :toctree: generated/ 63 | :nosignatures: 64 | 65 | cupyx.scipy.special.zeta 66 | -------------------------------------------------------------------------------- /cupyx/scipy/special/bessel.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | 4 | j0 = core.create_ufunc( 5 | 'cupyx_scipy_j0', ('f->f', 'd->d'), 6 | 'out0 = j0(in0)', 7 | doc='''Bessel function of the first kind of order 0. 8 | 9 | .. seealso:: :meth:`scipy.special.j0` 10 | 11 | ''') 12 | 13 | 14 | j1 = core.create_ufunc( 15 | 'cupyx_scipy_j1', ('f->f', 'd->d'), 16 | 'out0 = j1(in0)', 17 | doc='''Bessel function of the first kind of order 1. 18 | 19 | .. seealso:: :meth:`scipy.special.j1` 20 | 21 | ''') 22 | 23 | 24 | y0 = core.create_ufunc( 25 | 'cupyx_scipy_y0', ('f->f', 'd->d'), 26 | 'out0 = y0(in0)', 27 | doc='''Bessel function of the second kind of order 0. 28 | 29 | .. seealso:: :meth:`scipy.special.y0` 30 | 31 | ''') 32 | 33 | 34 | y1 = core.create_ufunc( 35 | 'cupyx_scipy_y1', ('f->f', 'd->d'), 36 | 'out0 = y1(in0)', 37 | doc='''Bessel function of the second kind of order 1. 38 | 39 | .. seealso:: :meth:`scipy.special.y1` 40 | 41 | ''') 42 | 43 | 44 | i0 = core.create_ufunc( 45 | 'cupyx_scipy_i0', ('f->f', 'd->d'), 46 | 'out0 = cyl_bessel_i0(in0)', 47 | doc='''Modified Bessel function of order 0. 48 | 49 | .. seealso:: :meth:`scipy.special.i0` 50 | 51 | ''') 52 | 53 | 54 | i1 = core.create_ufunc( 55 | 'cupyx_scipy_i1', ('f->f', 'd->d'), 56 | 'out0 = cyl_bessel_i1(in0)', 57 | doc='''Modified Bessel function of order 1. 58 | 59 | .. seealso:: :meth:`scipy.special.i1` 60 | 61 | ''') 62 | -------------------------------------------------------------------------------- /examples/stream/map_reduce.py: -------------------------------------------------------------------------------- 1 | import cupy 2 | import time 3 | 4 | device = cupy.cuda.Device() 5 | memory_pool = cupy.cuda.MemoryPool() 6 | cupy.cuda.set_allocator(memory_pool.malloc) 7 | rand = cupy.random.generator.RandomState(seed=1) 8 | 9 | n = 10 10 | zs = [] 11 | map_streams = [] 12 | stop_events = [] 13 | reduce_stream = cupy.cuda.stream.Stream() 14 | for i in range(n): 15 | map_streams.append(cupy.cuda.stream.Stream()) 16 | 17 | start_time = time.time() 18 | 19 | # Map 20 | for stream in map_streams: 21 | with stream: 22 | x = rand.normal(size=(1, 1024 * 256)) 23 | y = rand.normal(size=(1024 * 256, 1)) 24 | z = cupy.matmul(x, y) 25 | zs.append(z) 26 | stop_event = stream.record() 27 | stop_events.append(stop_event) 28 | 29 | # Block the `reduce_stream` until all events occur. This does not block host. 30 | # This is not required when reduction is performed in the default (Stream.null) 31 | # stream unless streams are created with `non_blocking=True` flag. 32 | for i in range(n): 33 | reduce_stream.wait_event(stop_events[i]) 34 | 35 | # Reduce 36 | with reduce_stream: 37 | z = sum(zs) 38 | 39 | device.synchronize() 40 | elapsed_time = time.time() - start_time 41 | print('elapsed time', elapsed_time) 42 | print('total bytes', memory_pool.total_bytes()) 43 | 44 | # Free all blocks in the memory pool of streams 45 | for stream in map_streams: 46 | memory_pool.free_all_blocks(stream=stream) 47 | print('total bytes', memory_pool.total_bytes()) 48 | -------------------------------------------------------------------------------- /docs/source/reference/linalg.rst: -------------------------------------------------------------------------------- 1 | Linear Algebra 2 | ============== 3 | 4 | .. https://docs.scipy.org/doc/numpy/reference/routines.linalg.html 5 | 6 | Matrix and vector products 7 | -------------------------- 8 | 9 | .. autosummary:: 10 | :toctree: generated/ 11 | :nosignatures: 12 | 13 | cupy.dot 14 | cupy.vdot 15 | cupy.inner 16 | cupy.outer 17 | cupy.matmul 18 | cupy.tensordot 19 | cupy.einsum 20 | cupy.linalg.matrix_power 21 | cupy.kron 22 | 23 | Decompositions 24 | -------------- 25 | 26 | .. autosummary:: 27 | :toctree: generated/ 28 | :nosignatures: 29 | 30 | cupy.linalg.cholesky 31 | cupy.linalg.qr 32 | cupy.linalg.svd 33 | 34 | Matrix eigenvalues 35 | ------------------ 36 | 37 | .. autosummary:: 38 | :toctree: generated/ 39 | :nosignatures: 40 | 41 | cupy.linalg.eigh 42 | cupy.linalg.eigvalsh 43 | 44 | Norms etc. 45 | ---------- 46 | 47 | .. autosummary:: 48 | :toctree: generated/ 49 | :nosignatures: 50 | 51 | cupy.linalg.det 52 | cupy.linalg.norm 53 | cupy.linalg.matrix_rank 54 | cupy.linalg.slogdet 55 | cupy.trace 56 | 57 | 58 | Solving linear equations 59 | -------------------------- 60 | 61 | .. autosummary:: 62 | :toctree: generated/ 63 | :nosignatures: 64 | 65 | cupy.linalg.solve 66 | cupy.linalg.tensorsolve 67 | cupy.linalg.inv 68 | cupy.linalg.pinv 69 | cupy.linalg.tensorinv 70 | 71 | cupyx.scipy.linalg.lu_factor 72 | cupyx.scipy.linalg.lu_solve 73 | cupyx.scipy.linalg.solve_triangular 74 | -------------------------------------------------------------------------------- /cupy/core/include/cupy/atomics.cuh: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #if __CUDA_ARCH__ < 600 4 | 5 | __device__ double atomicAdd(double *address, double val) 6 | { 7 | unsigned long long int* address_as_ull = (unsigned long long int*)address; 8 | unsigned long long int old = *address_as_ull; 9 | unsigned long long int assumed; 10 | do { 11 | assumed = old; 12 | old = atomicCAS(address_as_ull, assumed, 13 | __double_as_longlong(val + __longlong_as_double(assumed))); 14 | } while (assumed != old); 15 | return __longlong_as_double(old); 16 | } 17 | 18 | #endif 19 | 20 | __device__ float16 atomicAdd(float16* address, float16 val) { 21 | unsigned int *aligned = (unsigned int*)((size_t)address - ((size_t)address & 2)); 22 | unsigned int old = *aligned; 23 | unsigned int assumed; 24 | unsigned short old_as_us; 25 | do { 26 | assumed = old; 27 | old_as_us = (unsigned short)((size_t)address & 2 ? old >> 16 : old & 0xffff); 28 | #if __CUDACC_VER_MAJOR__ >= 9 29 | half sum = __float2half_rn(__half2float(__ushort_as_half(old_as_us)) + float(val)); 30 | unsigned short sum_as_us = __half_as_ushort(sum); 31 | #else 32 | unsigned short sum_as_us = __float2half_rn(__half2float(old_as_us) + float(val)); 33 | #endif 34 | unsigned int sum_as_ui = (size_t)address & 2 ? (sum_as_us << 16) | (old & 0xffff) 35 | : (old & 0xffff0000) | sum_as_us; 36 | old = atomicCAS(aligned, assumed, sum_as_ui); 37 | } while(assumed != old); 38 | __half_raw raw = {old_as_us}; 39 | return float16(raw); 40 | }; 41 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_function.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import cupy 6 | from cupy.cuda import compiler 7 | from cupy import testing 8 | 9 | 10 | def _compile_func(kernel_name, code): 11 | mod = compiler.compile_with_cache(code) 12 | return mod.get_function(kernel_name) 13 | 14 | 15 | @testing.gpu 16 | class TestFunction(unittest.TestCase): 17 | 18 | def test_python_scalar(self): 19 | code = ''' 20 | extern "C" __global__ void test_kernel(const double* a, double b, double* x) { 21 | int i = threadIdx.x; 22 | x[i] = a[i] + b; 23 | } 24 | ''' 25 | 26 | a_cpu = numpy.arange(24, dtype=numpy.float64).reshape((4, 6)) 27 | a = cupy.array(a_cpu) 28 | b = float(2) 29 | x = cupy.empty_like(a) 30 | 31 | func = _compile_func('test_kernel', code) 32 | 33 | func.linear_launch(a.size, (a, b, x)) 34 | 35 | expected = a_cpu + b 36 | testing.assert_array_equal(x, expected) 37 | 38 | def test_numpy_scalar(self): 39 | code = ''' 40 | extern "C" __global__ void test_kernel(const double* a, double b, double* x) { 41 | int i = threadIdx.x; 42 | x[i] = a[i] + b; 43 | } 44 | ''' 45 | 46 | a_cpu = numpy.arange(24, dtype=numpy.float64).reshape((4, 6)) 47 | a = cupy.array(a_cpu) 48 | b = numpy.float64(2) 49 | x = cupy.empty_like(a) 50 | 51 | func = _compile_func('test_kernel', code) 52 | 53 | func.linear_launch(a.size, (a, b, x)) 54 | 55 | expected = a_cpu + b 56 | testing.assert_array_equal(x, expected) 57 | -------------------------------------------------------------------------------- /tests/cupyx_tests/scipy_tests/special_tests/test_gamma.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | import cupyx.scipy.special # NOQA 5 | import numpy 6 | 7 | 8 | @testing.gpu 9 | @testing.with_requires('scipy') 10 | class TestGamma(unittest.TestCase): 11 | 12 | @testing.for_all_dtypes(no_complex=True) 13 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 14 | def test_arange(self, xp, scp, dtype): 15 | import scipy.special # NOQA 16 | 17 | a = testing.shaped_arange((2, 3), xp, dtype) 18 | return scp.special.gamma(a) 19 | 20 | @testing.for_all_dtypes(no_complex=True, no_bool=True) 21 | @testing.numpy_cupy_allclose(atol=1e-5, rtol=1e-5, scipy_name='scp') 22 | def test_linspace(self, xp, scp, dtype): 23 | import scipy.special # NOQA 24 | 25 | a = numpy.linspace(-30, 30, 1000, dtype=dtype) 26 | a = xp.asarray(a) 27 | return scp.special.gamma(a) 28 | 29 | @testing.for_all_dtypes(no_complex=True) 30 | @testing.numpy_cupy_allclose(atol=1e-2, rtol=1e-3, scipy_name='scp') 31 | def test_scalar(self, xp, scp, dtype): 32 | import scipy.special # NOQA 33 | 34 | return scp.special.gamma(dtype(1.5)) 35 | 36 | @testing.for_all_dtypes(no_complex=True) 37 | @testing.numpy_cupy_allclose(atol=1e-2, rtol=1e-3, scipy_name='scp') 38 | def test_inf_and_nan(self, xp, scp, dtype): 39 | import scipy.special # NOQA 40 | 41 | a = numpy.array([-numpy.inf, numpy.nan, numpy.inf]).astype(dtype) 42 | a = xp.asarray(a) 43 | return scp.special.gamma(a) 44 | -------------------------------------------------------------------------------- /cupyx/scipy/special/erf.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | 3 | 4 | erf = core.create_ufunc( 5 | 'cupyx_scipy_erf', ('f->f', 'd->d'), 6 | 'out0 = erf(in0)', 7 | doc='''Error function. 8 | 9 | .. seealso:: :meth:`scipy.special.erf` 10 | 11 | ''') 12 | 13 | 14 | erfc = core.create_ufunc( 15 | 'cupyx_scipy_erfc', ('f->f', 'd->d'), 16 | 'out0 = erfc(in0)', 17 | doc='''Complementary error function. 18 | 19 | .. seealso:: :meth:`scipy.special.erfc` 20 | 21 | ''') 22 | 23 | 24 | erfcx = core.create_ufunc( 25 | 'cupyx_scipy_erfcx', ('f->f', 'd->d'), 26 | 'out0 = erfcx(in0)', 27 | doc='''Scaled complementary error function. 28 | 29 | .. seealso:: :meth:`scipy.special.erfcx` 30 | 31 | ''') 32 | 33 | 34 | erfinv = core.create_ufunc( 35 | 'cupyx_scipy_erfinv', ('f->f', 'd->d'), 36 | ''' 37 | if (in0 < -1) { 38 | out0 = -1.0 / 0.0; 39 | } else if (in0 > 1) { 40 | out0 = 1.0 / 0.0; 41 | } else { 42 | out0 = erfinv(in0); 43 | } 44 | ''', 45 | doc='''Inverse function of error function. 46 | 47 | .. seealso:: :meth:`scipy.special.erfinv` 48 | 49 | ''') 50 | 51 | 52 | erfcinv = core.create_ufunc( 53 | 'cupyx_scipy_erfcinv', ('f->f', 'd->d'), 54 | ''' 55 | if (in0 < 0) { 56 | out0 = 1.0 / 0.0; 57 | } else if (in0 > 2) { 58 | out0 = -1.0 / 0.0; 59 | } else { 60 | out0 = erfcinv(in0); 61 | } 62 | ''', 63 | doc='''Inverse function of complementary error function. 64 | 65 | .. seealso:: :meth:`scipy.special.erfcinv` 66 | 67 | ''') 68 | -------------------------------------------------------------------------------- /tests/cupyx_tests/scipy_tests/special_tests/test_gammaln.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | import cupyx.scipy.special # NOQA 5 | import numpy 6 | 7 | 8 | @testing.gpu 9 | @testing.with_requires('scipy') 10 | class TestGammaln(unittest.TestCase): 11 | 12 | @testing.for_all_dtypes(no_complex=True) 13 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 14 | def test_arange(self, xp, scp, dtype): 15 | import scipy.special # NOQA 16 | 17 | a = testing.shaped_arange((2, 3), xp, dtype) 18 | return scp.special.gammaln(a) 19 | 20 | @testing.for_all_dtypes(no_complex=True, no_bool=True) 21 | @testing.numpy_cupy_allclose(atol=1e-4, rtol=1e-5, scipy_name='scp') 22 | def test_linspace(self, xp, scp, dtype): 23 | import scipy.special # NOQA 24 | 25 | a = numpy.linspace(-30, 30, 1000, dtype=dtype) 26 | a = xp.asarray(a) 27 | return scp.special.gammaln(a) 28 | 29 | @testing.for_all_dtypes(no_complex=True) 30 | @testing.numpy_cupy_allclose(atol=1e-2, rtol=1e-3, scipy_name='scp') 31 | def test_scalar(self, xp, scp, dtype): 32 | import scipy.special # NOQA 33 | 34 | return scp.special.gammaln(dtype(1.5)) 35 | 36 | @testing.for_all_dtypes(no_complex=True) 37 | @testing.numpy_cupy_allclose(atol=1e-2, rtol=1e-3, scipy_name='scp') 38 | def test_inf_and_nan(self, xp, scp, dtype): 39 | import scipy.special # NOQA 40 | 41 | a = numpy.array([-numpy.inf, numpy.nan, numpy.inf]).astype(dtype) 42 | a = xp.asarray(a) 43 | return scp.special.gammaln(a) 44 | -------------------------------------------------------------------------------- /tests/cupy_tests/binary_tests/test_packing.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | import unittest 3 | 4 | from cupy import testing 5 | 6 | 7 | @testing.gpu 8 | class TestPacking(unittest.TestCase): 9 | 10 | @testing.with_requires('numpy>=1.10') 11 | @testing.for_int_dtypes() 12 | @testing.numpy_cupy_array_equal() 13 | def check_packbits(self, data, xp, dtype): 14 | # Note numpy <= 1.9 raises an Exception when an input array is bool. 15 | # See https://github.com/numpy/numpy/issues/5377 16 | a = xp.array(data, dtype=dtype) 17 | return xp.packbits(a) 18 | 19 | @testing.numpy_cupy_array_equal() 20 | def check_unpackbits(self, data, xp): 21 | a = xp.array(data, dtype=xp.uint8) 22 | return xp.unpackbits(a) 23 | 24 | def test_packbits(self): 25 | self.check_packbits([0]) 26 | self.check_packbits([1]) 27 | self.check_packbits([0, 1]) 28 | self.check_packbits([1, 0, 1, 1, 0, 1, 1, 1]) 29 | self.check_packbits([1, 0, 1, 1, 0, 1, 1, 1, 1]) 30 | self.check_packbits(numpy.arange(24).reshape((2, 3, 4)) % 2) 31 | 32 | @testing.with_requires('numpy>=1.12') 33 | def test_packbits_empty(self): 34 | # Note packbits of numpy <= 1.11 has a bug against empty arrays. 35 | # See https://github.com/numpy/numpy/issues/8324 36 | self.check_packbits([]) 37 | 38 | def test_unpackbits(self): 39 | self.check_unpackbits([]) 40 | self.check_unpackbits([0]) 41 | self.check_unpackbits([1]) 42 | self.check_unpackbits([255]) 43 | self.check_unpackbits([100, 200, 123, 213]) 44 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_dlpack.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import cupy 4 | from cupy import testing 5 | 6 | 7 | @testing.parameterize(*testing.product({ 8 | 'dtype': [ 9 | cupy.uint8, cupy.uint16, cupy.uint32, cupy.uint64, 10 | cupy.int8, cupy.int16, cupy.int32, cupy.int64, 11 | cupy.float16, cupy.float32, cupy.float64], 12 | })) 13 | class TestDLPackConversion(unittest.TestCase): 14 | 15 | def setUp(self): 16 | if cupy.issubdtype(self.dtype, cupy.unsignedinteger): 17 | self.array = cupy.random.randint( 18 | 0, 10, size=(2, 3)).astype(self.dtype) 19 | elif cupy.issubdtype(self.dtype, cupy.integer): 20 | self.array = cupy.random.randint( 21 | -10, 10, size=(2, 3)).astype(self.dtype) 22 | elif cupy.issubdtype(self.dtype, cupy.floating): 23 | self.array = cupy.random.rand( 24 | 2, 3).astype(self.dtype) 25 | 26 | def test_conversion(self): 27 | tensor = self.array.toDlpack() 28 | array = cupy.fromDlpack(tensor) 29 | testing.assert_array_equal(self.array, array) 30 | testing.assert_array_equal(self.array.data.ptr, array.data.ptr) 31 | 32 | 33 | class TestDLTensorMemory(unittest.TestCase): 34 | 35 | def test_deleter(self): 36 | pool = cupy.get_default_memory_pool() 37 | pool.free_all_blocks() 38 | array = cupy.empty(10) 39 | tensor = array.toDlpack() 40 | assert pool.n_free_blocks() == 0 41 | del array 42 | assert pool.n_free_blocks() == 0 43 | del tensor 44 | assert pool.n_free_blocks() == 1 45 | -------------------------------------------------------------------------------- /docs/source/reference/fftpack.rst: -------------------------------------------------------------------------------- 1 | .. module:: cupyx.scipy.fftpack 2 | 3 | Discrete Fourier transforms 4 | =========================== 5 | 6 | 7 | Fast Fourier Transforms 8 | ----------------------- 9 | 10 | .. autosummary:: 11 | :toctree: generated/ 12 | :nosignatures: 13 | 14 | cupyx.scipy.fftpack.fft 15 | cupyx.scipy.fftpack.ifft 16 | cupyx.scipy.fftpack.fft2 17 | cupyx.scipy.fftpack.ifft2 18 | cupyx.scipy.fftpack.fftn 19 | cupyx.scipy.fftpack.ifftn 20 | cupyx.scipy.fftpack.rfft 21 | cupyx.scipy.fftpack.irfft 22 | cupyx.scipy.fftpack.get_fft_plan 23 | 24 | 25 | Code compatibility features 26 | --------------------------- 27 | 1. The ``get_fft_plan`` function has no counterpart in ``scipy.fftpack``. It returns a cuFFT plan that can be passed to the FFT functions in this module (using the argument ``plan``) to accelarate the computation. The argument ``plan`` is currently experimental and the interface may be changed in the future version. 28 | 29 | 2. The boolean switch ``cupy.fft.config.enable_nd_planning`` also affects the FFT functions in this module, see :doc:`./fft`. This switch is neglected when planning manually using ``get_fft_plan``. 30 | 31 | 3. Like in ``scipy.fftpack``, all FFT functions in this module have an optional argument ``overwrite_x`` (default is ``False``), which has the same semantics as in ``scipy.fftpack``: when it is set to ``True``, the input array ``x`` *can* (not *will*) be destroyed and replaced by the output. For this reason, when an in-place FFT is desired, the user should always reassign the input in the following manner: ``x = cupyx.scipy.fftpack.fft(x, ..., overwrite_x=True, ...)``. 32 | -------------------------------------------------------------------------------- /cupy/io/formatting.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | 3 | import cupy 4 | 5 | 6 | def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): 7 | """Returns the string representation of an array. 8 | 9 | Args: 10 | arr (array_like): Input array. It should be able to feed to 11 | :func:`cupy.asnumpy`. 12 | max_line_width (int): The maximum number of line lengths. 13 | precision (int): Floating point precision. It uses the current printing 14 | precision of NumPy. 15 | suppress_small (bool): If ``True``, very small numbers are printed as 16 | zeros 17 | 18 | Returns: 19 | str: The string representation of ``arr``. 20 | 21 | .. seealso:: :func:`numpy.array_repr` 22 | 23 | """ 24 | return numpy.array_repr(cupy.asnumpy(arr), max_line_width, precision, 25 | suppress_small) 26 | 27 | 28 | def array_str(arr, max_line_width=None, precision=None, suppress_small=None): 29 | """Returns the string representation of the content of an array. 30 | 31 | Args: 32 | arr (array_like): Input array. It should be able to feed to 33 | :func:`cupy.asnumpy`. 34 | max_line_width (int): The maximum number of line lengths. 35 | precision (int): Floating point precision. It uses the current printing 36 | precision of NumPy. 37 | suppress_small (bool): If ``True``, very small number are printed as 38 | zeros. 39 | 40 | .. seealso:: :func:`numpy.array_str` 41 | 42 | """ 43 | return numpy.array_str(cupy.asnumpy(arr), max_line_width, precision, 44 | suppress_small) 45 | -------------------------------------------------------------------------------- /docs/source/reference/cuda.rst: -------------------------------------------------------------------------------- 1 | Low-Level CUDA Support 2 | ====================== 3 | 4 | Device management 5 | ----------------- 6 | 7 | .. autosummary:: 8 | :toctree: generated/ 9 | :nosignatures: 10 | 11 | cupy.cuda.Device 12 | 13 | 14 | Memory management 15 | ----------------- 16 | 17 | .. autosummary:: 18 | :toctree: generated/ 19 | :nosignatures: 20 | 21 | cupy.get_default_memory_pool 22 | cupy.get_default_pinned_memory_pool 23 | cupy.cuda.Memory 24 | cupy.cuda.PinnedMemory 25 | cupy.cuda.MemoryPointer 26 | cupy.cuda.PinnedMemoryPointer 27 | cupy.cuda.alloc 28 | cupy.cuda.alloc_pinned_memory 29 | cupy.cuda.set_allocator 30 | cupy.cuda.set_pinned_memory_allocator 31 | cupy.cuda.MemoryPool 32 | cupy.cuda.PinnedMemoryPool 33 | 34 | 35 | Memory hook 36 | ----------- 37 | 38 | .. autosummary:: 39 | :toctree: generated/ 40 | :nosignatures: 41 | 42 | cupy.cuda.MemoryHook 43 | cupy.cuda.memory_hooks.DebugPrintHook 44 | cupy.cuda.memory_hooks.LineProfileHook 45 | 46 | Streams and events 47 | ------------------ 48 | 49 | .. autosummary:: 50 | :toctree: generated/ 51 | :nosignatures: 52 | 53 | cupy.cuda.Stream 54 | cupy.cuda.get_current_stream 55 | cupy.cuda.Event 56 | cupy.cuda.get_elapsed_time 57 | 58 | 59 | Profiler 60 | -------- 61 | 62 | .. autosummary:: 63 | :toctree: generated/ 64 | :nosignatures: 65 | 66 | cupy.cuda.profile 67 | cupy.cuda.profiler.initialize 68 | cupy.cuda.profiler.start 69 | cupy.cuda.profiler.stop 70 | cupy.cuda.nvtx.Mark 71 | cupy.cuda.nvtx.MarkC 72 | cupy.cuda.nvtx.RangePush 73 | cupy.cuda.nvtx.RangePushC 74 | cupy.cuda.nvtx.RangePop 75 | -------------------------------------------------------------------------------- /examples/stream/cupy_memcpy.py: -------------------------------------------------------------------------------- 1 | # nvprof --print-gpu-trace python examples/stream/cupy_memcpy.py 2 | import cupy 3 | import numpy 4 | 5 | pinned_memory_pool = cupy.cuda.PinnedMemoryPool() 6 | cupy.cuda.set_pinned_memory_allocator(pinned_memory_pool.malloc) 7 | 8 | 9 | def _pin_memory(array): 10 | mem = cupy.cuda.alloc_pinned_memory(array.nbytes) 11 | ret = numpy.frombuffer(mem, array.dtype, array.size).reshape(array.shape) 12 | ret[...] = array 13 | return ret 14 | 15 | 16 | SIZE = 1024 * 1024 17 | x_cpu_src = numpy.arange(SIZE, dtype=numpy.float32) 18 | x_gpu_src = cupy.arange(SIZE, dtype=numpy.float32) 19 | 20 | 21 | # synchronous 22 | stream = cupy.cuda.Stream.null 23 | start = stream.record() 24 | x_gpu_dst = cupy.empty(x_cpu_src.shape, x_cpu_src.dtype) 25 | x_gpu_dst.set(x_cpu_src) 26 | x_cpu_dst = x_gpu_src.get() 27 | end = stream.record() 28 | 29 | print('Synchronous Device to Host / Host to Device (ms)') 30 | print(cupy.cuda.get_elapsed_time(start, end)) 31 | 32 | 33 | # asynchronous 34 | x_gpu_dst = cupy.empty(x_cpu_src.shape, x_cpu_src.dtype) 35 | x_cpu_dst = numpy.empty(x_gpu_src.shape, x_gpu_src.dtype) 36 | 37 | x_pinned_cpu_src = _pin_memory(x_cpu_src) 38 | x_pinned_cpu_dst = _pin_memory(x_cpu_dst) 39 | 40 | with cupy.cuda.stream.Stream() as stream_htod: 41 | start = stream_htod.record() 42 | x_gpu_dst.set(x_pinned_cpu_src) 43 | with cupy.cuda.stream.Stream() as stream_dtoh: 44 | x_gpu_src.get(out=x_pinned_cpu_dst) 45 | stream_dtoh.synchronize() 46 | stream_htod.synchronize() 47 | end = stream_htod.record() 48 | 49 | print('Asynchronous Device to Host / Host to Device (ms)') 50 | print(cupy.cuda.get_elapsed_time(start, end)) 51 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_scan.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | import unittest 4 | 5 | import cupy 6 | from cupy import cuda 7 | from cupy.core._routines_math import _scan_for_test as scan 8 | from cupy import testing 9 | 10 | 11 | @testing.gpu 12 | class TestScan(unittest.TestCase): 13 | 14 | @testing.for_all_dtypes() 15 | def test_scan(self, dtype): 16 | element_num = 10000 17 | 18 | if dtype in {cupy.int8, cupy.uint8, cupy.float16}: 19 | element_num = 100 20 | 21 | a = cupy.ones((element_num,), dtype=dtype) 22 | prefix_sum = scan(a) 23 | expect = cupy.arange(start=1, stop=element_num + 1).astype(dtype) 24 | 25 | testing.assert_array_equal(prefix_sum, expect) 26 | 27 | def test_check_1d_array(self): 28 | with self.assertRaises(TypeError): 29 | a = cupy.zeros((2, 2)) 30 | scan(a) 31 | 32 | @testing.multi_gpu(2) 33 | def test_multi_gpu(self): 34 | with cuda.Device(0): 35 | a = cupy.zeros((10,)) 36 | scan(a) 37 | with cuda.Device(1): 38 | a = cupy.zeros((10,)) 39 | scan(a) 40 | 41 | @testing.for_all_dtypes() 42 | def test_scan_out(self, dtype): 43 | element_num = 10000 44 | 45 | if dtype in {cupy.int8, cupy.uint8, cupy.float16}: 46 | element_num = 100 47 | 48 | a = cupy.ones((element_num,), dtype=dtype) 49 | b = cupy.zeros_like(a) 50 | scan(a, b) 51 | expect = cupy.arange(start=1, stop=element_num + 1).astype(dtype) 52 | 53 | testing.assert_array_equal(b, expect) 54 | 55 | scan(a, a) 56 | testing.assert_array_equal(a, expect) 57 | -------------------------------------------------------------------------------- /cupy/core/_dtype.pyx: -------------------------------------------------------------------------------- 1 | cimport cython # NOQA 2 | import numpy 3 | import six 4 | 5 | 6 | all_type_chars = '?bhilqBHILQefdFD' 7 | # for c in '?bhilqBHILQefdFD': 8 | # print('#', c, '...', np.dtype(c).name) 9 | # ? ... bool 10 | # b ... int8 11 | # h ... int16 12 | # i ... int32 13 | # l ... int64 14 | # q ... int64 15 | # B ... uint8 16 | # H ... uint16 17 | # I ... uint32 18 | # L ... uint64 19 | # Q ... uint64 20 | # e ... float16 21 | # f ... float32 22 | # d ... float64 23 | # F ... complex64 24 | # D ... complex128 25 | 26 | cdef dict _dtype_dict = {} 27 | 28 | 29 | cdef _init_dtype_dict(): 30 | for i in six.integer_types + (float, bool, complex, None): 31 | dtype = numpy.dtype(i) 32 | _dtype_dict[i] = (dtype, dtype.itemsize) 33 | for i in all_type_chars: 34 | dtype = numpy.dtype(i) 35 | item = (dtype, dtype.itemsize) 36 | _dtype_dict[i] = item 37 | _dtype_dict[dtype.type] = item 38 | for i in {str(numpy.dtype(i)) for i in all_type_chars}: 39 | dtype = numpy.dtype(i) 40 | _dtype_dict[i] = (dtype, dtype.itemsize) 41 | 42 | 43 | _init_dtype_dict() 44 | 45 | 46 | @cython.profile(False) 47 | cpdef get_dtype(t): 48 | if type(t) is numpy.dtype: # Exact type check 49 | return t 50 | ret = _dtype_dict.get(t, None) 51 | if ret is None: 52 | return numpy.dtype(t) 53 | return ret[0] 54 | 55 | 56 | @cython.profile(False) 57 | cpdef tuple get_dtype_with_itemsize(t): 58 | if type(t) is numpy.dtype: # Exact type check 59 | return t, t.itemsize 60 | ret = _dtype_dict.get(t, None) 61 | if ret is None: 62 | t = numpy.dtype(t) 63 | return t, t.itemsize 64 | return ret 65 | -------------------------------------------------------------------------------- /cupy/core/internal.pxd: -------------------------------------------------------------------------------- 1 | from libcpp cimport bool as cpp_bool 2 | from libcpp cimport vector 3 | from libc.stdint cimport uint16_t 4 | 5 | 6 | cpdef Py_ssize_t prod(const vector.vector[Py_ssize_t]& args) 7 | 8 | cpdef tuple get_size(object size) 9 | 10 | cpdef bint vector_equal( 11 | vector.vector[Py_ssize_t]& x, vector.vector[Py_ssize_t]& y) 12 | 13 | cdef void get_reduced_dims( 14 | vector.vector[Py_ssize_t]& shape, vector.vector[Py_ssize_t]& strides, 15 | Py_ssize_t itemsize, vector.vector[Py_ssize_t]& reduced_shape, 16 | vector.vector[Py_ssize_t]& reduced_strides) 17 | 18 | cpdef vector.vector[Py_ssize_t] get_contiguous_strides( 19 | const vector.vector[Py_ssize_t]& shape, Py_ssize_t itemsize, 20 | bint is_c_contiguous) 21 | 22 | # Computes the contiguous strides given a shape and itemsize. 23 | # Returns the size (total number of elements). 24 | cdef Py_ssize_t set_contiguous_strides( 25 | const vector.vector[Py_ssize_t]& shape, vector.vector[Py_ssize_t]& strides, 26 | Py_ssize_t itemsize, bint is_c_contiguous) 27 | 28 | cpdef bint get_c_contiguity( 29 | vector.vector[Py_ssize_t]& shape, vector.vector[Py_ssize_t]& strides, 30 | Py_ssize_t itemsize) 31 | 32 | cpdef vector.vector[Py_ssize_t] infer_unknown_dimension( 33 | const vector.vector[Py_ssize_t]& shape, Py_ssize_t size) except * 34 | 35 | cpdef slice complete_slice(slice slc, Py_ssize_t dim) 36 | 37 | cpdef tuple complete_slice_list(list slice_list, Py_ssize_t ndim) 38 | 39 | cpdef size_t clp2(size_t x) 40 | 41 | ctypedef unsigned short _float16 42 | 43 | cpdef uint16_t to_float16(float f) 44 | 45 | cpdef float from_float16(uint16_t v) 46 | 47 | cdef int _normalize_order(order, cpp_bool allow_k=*) except? 0 48 | -------------------------------------------------------------------------------- /tests/cupyx_tests/test_runtime.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import mock 4 | 5 | import cupy 6 | import cupyx 7 | 8 | 9 | try: 10 | import cupy.cuda.cudnn as cudnn 11 | except ImportError: 12 | cudnn = None 13 | 14 | 15 | def _get_error_func(error, *args, **kwargs): 16 | def raise_error(*_args, **_kwargs): 17 | raise error(*args, **kwargs) 18 | return raise_error 19 | 20 | 21 | class TestRuntime(unittest.TestCase): 22 | def test_runtime(self): 23 | runtime = cupyx.get_runtime_info() 24 | assert cupy.__version__ == runtime.cupy_version 25 | assert cupy.__version__ in str(runtime) 26 | 27 | @unittest.skipUnless(cudnn is not None, 'cuDNN is required') 28 | def test_error(self): 29 | runtime = cupyx.get_runtime_info() 30 | assert 'Error' not in str(runtime) 31 | 32 | with mock.patch( 33 | 'cupy.cuda.runtime.driverGetVersion', 34 | side_effect=_get_error_func( 35 | cupy.cuda.runtime.CUDARuntimeError, 0)): 36 | runtime = cupyx.get_runtime_info() 37 | assert 'CUDARuntimeError' in str(runtime) 38 | 39 | with mock.patch( 40 | 'cupy.cuda.runtime.runtimeGetVersion', 41 | side_effect=_get_error_func( 42 | cupy.cuda.runtime.CUDARuntimeError, 0)): 43 | runtime = cupyx.get_runtime_info() 44 | assert 'CUDARuntimeError' in str(runtime) 45 | 46 | with mock.patch( 47 | 'cupy.cuda.cudnn.getVersion', 48 | side_effect=_get_error_func(cudnn.CuDNNError, 0)): 49 | runtime = cupyx.get_runtime_info() 50 | assert 'CuDNNError' in str(runtime) 51 | -------------------------------------------------------------------------------- /cupy/sparse/__init__.py: -------------------------------------------------------------------------------- 1 | from cupyx.scipy.sparse.base import issparse # NOQA 2 | from cupyx.scipy.sparse.base import isspmatrix # NOQA 3 | from cupyx.scipy.sparse.base import spmatrix # NOQA 4 | from cupyx.scipy.sparse.coo import coo_matrix # NOQA 5 | from cupyx.scipy.sparse.coo import isspmatrix_coo # NOQA 6 | from cupyx.scipy.sparse.csc import csc_matrix # NOQA 7 | from cupyx.scipy.sparse.csc import isspmatrix_csc # NOQA 8 | from cupyx.scipy.sparse.csr import csr_matrix # NOQA 9 | from cupyx.scipy.sparse.csr import isspmatrix_csr # NOQA 10 | from cupyx.scipy.sparse.dia import dia_matrix # NOQA 11 | from cupyx.scipy.sparse.dia import isspmatrix_dia # NOQA 12 | 13 | from cupyx.scipy.sparse.construct import eye # NOQA 14 | from cupyx.scipy.sparse.construct import identity # NOQA 15 | from cupyx.scipy.sparse.construct import rand # NOQA 16 | from cupyx.scipy.sparse.construct import random # NOQA 17 | from cupyx.scipy.sparse.construct import spdiags # NOQA 18 | 19 | # TODO(unno): implement bsr_matrix 20 | # TODO(unno): implement dok_matrix 21 | # TODO(unno): implement lil_matrix 22 | 23 | # TODO(unno): implement kron 24 | # TODO(unno): implement kronsum 25 | # TODO(unno): implement diags 26 | # TODO(unno): implement block_diag 27 | # TODO(unno): implement tril 28 | # TODO(unno): implement triu 29 | # TODO(unno): implement bmat 30 | # TODO(unno): implement hstack 31 | # TODO(unno): implement vstack 32 | 33 | # TODO(unno): implement save_npz 34 | # TODO(unno): implement load_npz 35 | 36 | # TODO(unno): implement find 37 | 38 | # TODO(unno): implement isspmatrix_bsr(x) 39 | # TODO(unno): implement isspmatrix_lil(x) 40 | # TODO(unno): implement isspmatrix_dok(x) 41 | 42 | from cupyx.scipy.sparse import linalg # NOQA 43 | -------------------------------------------------------------------------------- /cupy/math/floating.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | from cupy.math import ufunc 3 | 4 | 5 | signbit = core.create_ufunc( 6 | 'cupy_signbit', 7 | ('e->?', 'f->?', 'd->?'), 8 | 'out0 = signbit(in0)', 9 | doc='''Tests elementwise if the sign bit is set (i.e. less than zero). 10 | 11 | .. seealso:: :data:`numpy.signbit` 12 | 13 | ''') 14 | 15 | 16 | copysign = ufunc.create_math_ufunc( 17 | 'copysign', 2, 'cupy_copysign', 18 | '''Returns the first argument with the sign bit of the second elementwise. 19 | 20 | .. seealso:: :data:`numpy.copysign` 21 | 22 | ''') 23 | 24 | 25 | ldexp = core.create_ufunc( 26 | 'cupy_ldexp', 27 | ('ei->e', 'fi->f', 'el->e', 'fl->f', 'di->d', 'dq->d'), 28 | 'out0 = ldexp(in0, in1)', 29 | doc='''Computes ``x1 * 2 ** x2`` elementwise. 30 | 31 | .. seealso:: :data:`numpy.ldexp` 32 | 33 | ''') 34 | 35 | 36 | frexp = core.create_ufunc( 37 | 'cupy_frexp', 38 | ('e->ei', 'f->fi', 'd->di'), 39 | 'int nptr; out0 = frexp(in0, &nptr); out1 = nptr', 40 | doc='''Decomposes each element to mantissa and two's exponent. 41 | 42 | This ufunc outputs two arrays of the input dtype and the ``int`` dtype. 43 | 44 | .. seealso:: :data:`numpy.frexp` 45 | 46 | ''') 47 | 48 | 49 | nextafter = ufunc.create_math_ufunc( 50 | 'nextafter', 2, 'cupy_nextafter', 51 | '''Computes the nearest neighbor float values towards the second argument. 52 | 53 | .. note:: 54 | For values that are close to zero (or denormal numbers), 55 | results of :func:`cupy.nextafter` may be different from those of 56 | :func:`numpy.nextafter`, because CuPy sets ``-ftz=true``. 57 | 58 | .. seealso:: :data:`numpy.nextafter` 59 | 60 | ''') 61 | -------------------------------------------------------------------------------- /tests/cupy_tests/lib_tests/test_strided_tricks.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import cupy 6 | from cupy import testing 7 | from cupy.lib import stride_tricks 8 | 9 | 10 | @testing.gpu 11 | class TestAsStrided(unittest.TestCase): 12 | def test_as_strided(self): 13 | a = cupy.array([1, 2, 3, 4]) 14 | a_view = stride_tricks.as_strided( 15 | a, shape=(2,), strides=(2 * a.itemsize,)) 16 | expected = cupy.array([1, 3]) 17 | testing.assert_array_equal(a_view, expected) 18 | 19 | a = cupy.array([1, 2, 3, 4]) 20 | a_view = stride_tricks.as_strided( 21 | a, shape=(3, 4), strides=(0, 1 * a.itemsize)) 22 | expected = cupy.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]) 23 | testing.assert_array_equal(a_view, expected) 24 | 25 | @testing.numpy_cupy_array_equal() 26 | def test_rolling_window(self, xp): 27 | a = testing.shaped_arange((3, 4), xp) 28 | a_rolling = rolling_window(a, 2, 0) 29 | 30 | return a_rolling 31 | 32 | 33 | def rolling_window(a, window, axis=-1): 34 | """ 35 | Make an ndarray with a rolling window along axis. 36 | This function is taken from https://github.com/numpy/numpy/pull/31 37 | but slightly modified to accept axis option. 38 | """ 39 | a = numpy.swapaxes(a, axis, -1) 40 | shape = a.shape[:-1] + (a.shape[-1] - window + 1, window) 41 | strides = a.strides + (a.strides[-1],) 42 | if isinstance(a, numpy.ndarray): 43 | rolling = numpy.lib.stride_tricks.as_strided( 44 | a, shape=shape, strides=strides) 45 | elif isinstance(a, cupy.ndarray): 46 | rolling = stride_tricks.as_strided(a, shape=shape, strides=strides) 47 | return rolling.swapaxes(-2, axis) 48 | -------------------------------------------------------------------------------- /cupyx/scipy/sparse/__init__.py: -------------------------------------------------------------------------------- 1 | from cupyx.scipy.sparse.base import issparse # NOQA 2 | from cupyx.scipy.sparse.base import isspmatrix # NOQA 3 | from cupyx.scipy.sparse.base import spmatrix # NOQA 4 | from cupyx.scipy.sparse.coo import coo_matrix # NOQA 5 | from cupyx.scipy.sparse.coo import isspmatrix_coo # NOQA 6 | from cupyx.scipy.sparse.csc import csc_matrix # NOQA 7 | from cupyx.scipy.sparse.csc import isspmatrix_csc # NOQA 8 | from cupyx.scipy.sparse.csr import csr_matrix # NOQA 9 | from cupyx.scipy.sparse.csr import isspmatrix_csr # NOQA 10 | from cupyx.scipy.sparse.dia import dia_matrix # NOQA 11 | from cupyx.scipy.sparse.dia import isspmatrix_dia # NOQA 12 | 13 | from cupyx.scipy.sparse.construct import eye # NOQA 14 | from cupyx.scipy.sparse.construct import identity # NOQA 15 | from cupyx.scipy.sparse.construct import rand # NOQA 16 | from cupyx.scipy.sparse.construct import random # NOQA 17 | from cupyx.scipy.sparse.construct import spdiags # NOQA 18 | from cupyx.scipy.sparse.construct import diags # NOQA 19 | 20 | # TODO(unno): implement bsr_matrix 21 | # TODO(unno): implement dok_matrix 22 | # TODO(unno): implement lil_matrix 23 | 24 | # TODO(unno): implement kron 25 | # TODO(unno): implement kronsum 26 | # TODO(unno): implement diags 27 | # TODO(unno): implement block_diag 28 | # TODO(unno): implement tril 29 | # TODO(unno): implement triu 30 | # TODO(unno): implement bmat 31 | # TODO(unno): implement hstack 32 | # TODO(unno): implement vstack 33 | 34 | # TODO(unno): implement save_npz 35 | # TODO(unno): implement load_npz 36 | 37 | # TODO(unno): implement find 38 | 39 | # TODO(unno): implement isspmatrix_bsr(x) 40 | # TODO(unno): implement isspmatrix_lil(x) 41 | # TODO(unno): implement isspmatrix_dok(x) 42 | 43 | from cupyx.scipy.sparse import linalg # NOQA 44 | -------------------------------------------------------------------------------- /tests/cupy_tests/cuda_tests/memory_hooks_tests/test_line_profile.py: -------------------------------------------------------------------------------- 1 | import six 2 | import unittest 3 | 4 | from cupy.cuda import memory 5 | from cupy.cuda import memory_hooks 6 | from cupy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestLineProfileHook(unittest.TestCase): 11 | 12 | def setUp(self): 13 | self.pool = memory.MemoryPool() 14 | 15 | def test_print_report(self): 16 | hook = memory_hooks.LineProfileHook() 17 | p = self.pool.malloc(1000) 18 | del p 19 | with hook: 20 | p1 = self.pool.malloc(1000) 21 | p2 = self.pool.malloc(2000) 22 | del p1 23 | del p2 24 | io = six.StringIO() 25 | hook.print_report(file=io) 26 | actual = io.getvalue() 27 | expect = r'\A_root \(3\.00KB, 2\.00KB\)' 28 | six.assertRegex(self, actual, expect) 29 | expect = r'.*\.py:[0-9]+:test_print_report \(1\.00KB, 0\.00B\)' 30 | six.assertRegex(self, actual, expect) 31 | expect = r'.*\.py:[0-9]+:test_print_report \(2\.00KB, 2\.00KB\)' 32 | six.assertRegex(self, actual, expect) 33 | 34 | def test_print_report_max_depth(self): 35 | hook = memory_hooks.LineProfileHook(max_depth=1) 36 | with hook: 37 | p = self.pool.malloc(1000) 38 | del p 39 | io = six.StringIO() 40 | hook.print_report(file=io) 41 | actual = io.getvalue() 42 | self.assertEqual(2, len(actual.split('\n'))) 43 | 44 | hook = memory_hooks.LineProfileHook(max_depth=2) 45 | with hook: 46 | p = self.pool.malloc(1000) 47 | del p 48 | io = six.StringIO() 49 | hook.print_report(file=io) 50 | actual = io.getvalue() 51 | self.assertEqual(3, len(actual.split('\n'))) 52 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_core.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import cupy 4 | from cupy.core import core 5 | from cupy import testing 6 | 7 | 8 | class TestSize(unittest.TestCase): 9 | 10 | def tearDown(self): 11 | # Free huge memory for slow test 12 | cupy.get_default_memory_pool().free_all_blocks() 13 | 14 | @testing.for_all_dtypes() 15 | @testing.numpy_cupy_equal() 16 | def test_size(self, xp, dtype): 17 | a = xp.ndarray((2, 3), dtype=dtype) 18 | return xp.size(a) 19 | 20 | @testing.for_all_dtypes() 21 | @testing.numpy_cupy_equal() 22 | def test_size_axis(self, xp, dtype): 23 | a = xp.ndarray((2, 3), dtype=dtype) 24 | return xp.size(a, axis=1) 25 | 26 | @testing.for_all_dtypes() 27 | @testing.numpy_cupy_raises() 28 | def test_size_axis_error(self, xp, dtype): 29 | a = xp.ndarray((2, 3), dtype=dtype) 30 | return xp.size(a, axis=3) 31 | 32 | @testing.numpy_cupy_equal() 33 | @testing.slow 34 | def test_size_huge(self, xp): 35 | a = xp.ndarray(2 ** 32, 'b') # 4 GiB 36 | return xp.size(a) 37 | 38 | 39 | _orders = { 40 | order_arg: order_expect 41 | for order_expect, order_args in [ 42 | ('C', ['C', 'c', 'CONTIGUOUS', '', None]), 43 | ('F', ['F', 'f', 'FORTRAN']), 44 | ] 45 | for order_arg in order_args 46 | } 47 | 48 | 49 | class TestOrder(unittest.TestCase): 50 | 51 | @testing.for_orders(_orders.keys()) 52 | def test_ndarray(self, order): 53 | order_expect = _orders[order] 54 | a = core.ndarray((2, 3), order=order) 55 | expect_c = order_expect == 'C' 56 | expect_f = order_expect == 'F' 57 | assert a.flags.c_contiguous == expect_c 58 | assert a.flags.f_contiguous == expect_f 59 | -------------------------------------------------------------------------------- /tests/cupyx_tests/scipy_tests/special_tests/test_zeta.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | import cupyx.scipy.special # NOQA 5 | import numpy 6 | 7 | 8 | @testing.gpu 9 | @testing.with_requires('scipy') 10 | class TestZeta(unittest.TestCase): 11 | 12 | @testing.for_all_dtypes(no_complex=True) 13 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 14 | def test_arange(self, xp, scp, dtype): 15 | import scipy.special # NOQA 16 | 17 | a = testing.shaped_arange((2, 3), xp, dtype) 18 | b = testing.shaped_arange((2, 3), xp, dtype) 19 | return scp.special.zeta(a, b) 20 | 21 | @testing.for_all_dtypes(no_complex=True, no_bool=True) 22 | @testing.numpy_cupy_allclose(atol=1e-5, rtol=1e-6, scipy_name='scp') 23 | def test_linspace(self, xp, scp, dtype): 24 | import scipy.special # NOQA 25 | 26 | a = numpy.linspace(-30, 30, 1000, dtype=dtype) 27 | b = numpy.linspace(-30, 30, 1000, dtype=dtype) 28 | a = xp.asarray(a) 29 | b = xp.asarray(b) 30 | return scp.special.zeta(a, b) 31 | 32 | @testing.for_all_dtypes(no_complex=True) 33 | @testing.numpy_cupy_allclose(atol=1e-2, rtol=1e-3, scipy_name='scp') 34 | def test_scalar(self, xp, scp, dtype): 35 | import scipy.special # NOQA 36 | 37 | return scp.special.zeta(dtype(2.), dtype(1.5)) 38 | 39 | @testing.for_all_dtypes(no_complex=True) 40 | @testing.numpy_cupy_allclose(atol=1e-2, rtol=1e-3, scipy_name='scp') 41 | def test_inf_and_nan(self, xp, scp, dtype): 42 | import scipy.special # NOQA 43 | 44 | x = numpy.array([-numpy.inf, numpy.nan, numpy.inf]).astype(dtype) 45 | a = numpy.tile(x, 3) 46 | b = numpy.repeat(x, 3) 47 | a = xp.asarray(a) 48 | b = xp.asarray(b) 49 | return scp.special.zeta(a, b) 50 | -------------------------------------------------------------------------------- /tests/cupy_tests/math_tests/test_explog.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | from cupy import testing 6 | 7 | 8 | @testing.gpu 9 | class TestExplog(unittest.TestCase): 10 | 11 | @testing.for_all_dtypes() 12 | @testing.numpy_cupy_allclose(atol=1e-5) 13 | def check_unary(self, name, xp, dtype, no_complex=False): 14 | if no_complex: 15 | if numpy.dtype(dtype).kind == 'c': 16 | return xp.array(True) 17 | a = testing.shaped_arange((2, 3), xp, dtype) 18 | return getattr(xp, name)(a) 19 | 20 | @testing.for_all_dtypes() 21 | @testing.numpy_cupy_allclose(atol=1e-5) 22 | def check_binary(self, name, xp, dtype, no_complex=False): 23 | if no_complex: 24 | if numpy.dtype(dtype).kind == 'c': 25 | return xp.array(True) 26 | a = testing.shaped_arange((2, 3), xp, dtype) 27 | b = testing.shaped_reverse_arange((2, 3), xp, dtype) 28 | return getattr(xp, name)(a, b) 29 | 30 | def test_exp(self): 31 | self.check_unary('exp') 32 | 33 | def test_expm1(self): 34 | self.check_unary('expm1') 35 | 36 | def test_exp2(self): 37 | self.check_unary('exp2') 38 | 39 | def test_log(self): 40 | with testing.NumpyError(divide='ignore'): 41 | self.check_unary('log') 42 | 43 | def test_log10(self): 44 | with testing.NumpyError(divide='ignore'): 45 | self.check_unary('log10') 46 | 47 | def test_log2(self): 48 | with testing.NumpyError(divide='ignore'): 49 | self.check_unary('log2') 50 | 51 | def test_log1p(self): 52 | self.check_unary('log1p') 53 | 54 | def test_logaddexp(self): 55 | self.check_binary('logaddexp', no_complex=True) 56 | 57 | def test_logaddexp2(self): 58 | self.check_binary('logaddexp2', no_complex=True) 59 | -------------------------------------------------------------------------------- /cupy/testing/attr.py: -------------------------------------------------------------------------------- 1 | import os 2 | import unittest 3 | 4 | 5 | try: 6 | import pytest 7 | _error = None 8 | except ImportError as e: 9 | _error = e 10 | 11 | 12 | def is_available(): 13 | return _error is None 14 | 15 | 16 | def check_available(): 17 | if _error is not None: 18 | raise RuntimeError('''\ 19 | {} is not available. 20 | 21 | Reason: {}: {}'''.format(__name__, type(_error).__name__, _error)) 22 | 23 | 24 | def get_error(): 25 | return _error 26 | 27 | 28 | if _error is None: 29 | _gpu_limit = int(os.getenv('CUPY_TEST_GPU_LIMIT', '-1')) 30 | 31 | def cudnn(*args, **kwargs): 32 | return pytest.mark.cudnn(*args, **kwargs) 33 | 34 | def slow(*args, **kwargs): 35 | return pytest.mark.slow(*args, **kwargs) 36 | 37 | else: 38 | def _dummy_callable(*args, **kwargs): 39 | check_available() 40 | assert False # Not reachable 41 | 42 | cudnn = _dummy_callable 43 | slow = _dummy_callable 44 | 45 | 46 | def multi_gpu(gpu_num): 47 | """Decorator to indicate number of GPUs required to run the test. 48 | 49 | Tests can be annotated with this decorator (e.g., ``@multi_gpu(2)``) to 50 | declare number of GPUs required to run. When running tests, if 51 | ``CUPY_TEST_GPU_LIMIT`` environment variable is set to value greater 52 | than or equals to 0, test cases that require GPUs more than the limit will 53 | be skipped. 54 | """ 55 | 56 | check_available() 57 | return unittest.skipIf( 58 | 0 <= _gpu_limit < gpu_num, 59 | reason='{} GPUs required'.format(gpu_num)) 60 | 61 | 62 | def gpu(f): 63 | """Decorator to indicate that GPU is required to run the test. 64 | 65 | Tests can be annotated with this decorator (e.g., ``@gpu``) to 66 | declare that one GPU is required to run. 67 | """ 68 | 69 | check_available() 70 | return multi_gpu(1)(pytest.mark.gpu(f)) 71 | -------------------------------------------------------------------------------- /tests/cupyx_tests/scipy_tests/special_tests/test_bessel.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import cupy 4 | from cupy import testing 5 | import cupyx.scipy.special # NOQA 6 | 7 | 8 | @testing.gpu 9 | @testing.with_requires('scipy') 10 | class TestSpecial(unittest.TestCase): 11 | 12 | @testing.for_dtypes(['e', 'f', 'd']) 13 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 14 | def check_unary(self, name, xp, scp, dtype): 15 | import scipy.special # NOQA 16 | 17 | a = testing.shaped_arange((2, 3), xp, dtype) 18 | return getattr(scp.special, name)(a) 19 | 20 | def test_j0(self): 21 | self.check_unary('j0') 22 | 23 | def test_j1(self): 24 | self.check_unary('j1') 25 | 26 | def test_y0(self): 27 | self.check_unary('y0') 28 | 29 | def test_y1(self): 30 | self.check_unary('y1') 31 | 32 | def test_i0(self): 33 | self.check_unary('i0') 34 | 35 | def test_i1(self): 36 | self.check_unary('i1') 37 | 38 | 39 | @testing.gpu 40 | @testing.with_requires('scipy') 41 | class TestFusionSpecial(unittest.TestCase): 42 | 43 | @testing.for_dtypes(['e', 'f', 'd']) 44 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 45 | def check_unary(self, name, xp, scp, dtype): 46 | import scipy.special # NOQA 47 | 48 | a = testing.shaped_arange((2, 3), xp, dtype) 49 | 50 | @cupy.fuse() 51 | def f(x): 52 | return getattr(scp.special, name)(x) 53 | 54 | return f(a) 55 | 56 | def test_j0(self): 57 | self.check_unary('j0') 58 | 59 | def test_j1(self): 60 | self.check_unary('j1') 61 | 62 | def test_y0(self): 63 | self.check_unary('y0') 64 | 65 | def test_y1(self): 66 | self.check_unary('y1') 67 | 68 | def test_i0(self): 69 | self.check_unary('i0') 70 | 71 | def test_i1(self): 72 | self.check_unary('i1') 73 | -------------------------------------------------------------------------------- /cupy/cuda/profiler.pyx: -------------------------------------------------------------------------------- 1 | # distutils: language = c++ 2 | 3 | """Thin wrapper of cuda profiler.""" 4 | from cupy.cuda cimport runtime 5 | 6 | 7 | cdef extern from 'cupy_cuda.h' nogil: 8 | runtime.Error cudaProfilerInitialize(const char *configFile, 9 | const char *outputFile, 10 | int outputMode) 11 | runtime.Error cudaProfilerStart() 12 | runtime.Error cudaProfilerStop() 13 | 14 | 15 | cpdef initialize(str config_file, 16 | str output_file, 17 | int output_mode): 18 | """Initialize the CUDA profiler. 19 | 20 | This function initialize the CUDA profiler. See the CUDA document for 21 | detail. 22 | 23 | Args: 24 | config_file (str): Name of the configuration file. 25 | output_file (str): Name of the output file. 26 | output_mode (int): ``cupy.cuda.profiler.cudaKeyValuePair`` or 27 | ``cupy.cuda.profiler.cudaCSV``. 28 | """ 29 | cdef bytes b_config_file = config_file.encode() 30 | cdef bytes b_output_file = output_file.encode() 31 | status = cudaProfilerInitialize(b_config_file, 32 | b_output_file, 33 | output_mode) 34 | runtime.check_status(status) 35 | 36 | 37 | cpdef start(): 38 | """Enable profiling. 39 | 40 | A user can enable CUDA profiling. When an error occurs, it raises an 41 | exception. 42 | 43 | See the CUDA document for detail. 44 | """ 45 | status = cudaProfilerStart() 46 | runtime.check_status(status) 47 | 48 | 49 | cpdef stop(): 50 | """Disable profiling. 51 | 52 | A user can disable CUDA profiling. When an error occurs, it raises an 53 | exception. 54 | 55 | See the CUDA document for detail. 56 | """ 57 | status = cudaProfilerStop() 58 | runtime.check_status(status) 59 | -------------------------------------------------------------------------------- /tests/cupy_tests/core_tests/test_flags.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy.core import flags 4 | from cupy import testing 5 | 6 | 7 | class TestFlags(unittest.TestCase): 8 | 9 | def setUp(self): 10 | self.flags = flags.Flags(1, 2, 3) 11 | 12 | def test_c_contiguous(self): 13 | self.assertEqual(1, self.flags['C_CONTIGUOUS']) 14 | 15 | def test_f_contiguous(self): 16 | self.assertEqual(2, self.flags['F_CONTIGUOUS']) 17 | 18 | def test_owndata(self): 19 | self.assertEqual(3, self.flags['OWNDATA']) 20 | 21 | def test_key_error(self): 22 | with self.assertRaises(KeyError): 23 | self.flags['unknown key'] 24 | 25 | def test_repr(self): 26 | self.assertEqual(''' C_CONTIGUOUS : 1 27 | F_CONTIGUOUS : 2 28 | OWNDATA : 3''', repr(self.flags)) 29 | 30 | 31 | @testing.parameterize( 32 | *testing.product({ 33 | 'order': ['C', 'F', 'non-contiguous'], 34 | 'shape': [(8, ), (4, 8)], 35 | }) 36 | ) 37 | class TestContiguityFlags(unittest.TestCase): 38 | 39 | def setUp(self): 40 | self.flags = None 41 | 42 | def init_flags(self, xp): 43 | if self.order == 'non-contiguous': 44 | a = xp.empty(self.shape)[::2] 45 | else: 46 | a = xp.empty(self.shape, order=self.order) 47 | self.flags = a.flags 48 | 49 | @testing.numpy_cupy_equal() 50 | def test_fnc(self, xp): 51 | self.init_flags(xp) 52 | return self.flags.fnc 53 | 54 | @testing.numpy_cupy_equal() 55 | def test_forc(self, xp): 56 | self.init_flags(xp) 57 | return self.flags.forc 58 | 59 | @testing.numpy_cupy_equal() 60 | def test_f_contiguous(self, xp): 61 | self.init_flags(xp) 62 | return self.flags.f_contiguous 63 | 64 | @testing.numpy_cupy_equal() 65 | def test_c_contiguous(self, xp): 66 | self.init_flags(xp) 67 | return self.flags.c_contiguous 68 | -------------------------------------------------------------------------------- /tests/cupyx_tests/scipy_tests/sparse_tests/test_linalg.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | try: 5 | import scipy.sparse 6 | import scipy.stats 7 | scipy_available = True 8 | except ImportError: 9 | scipy_available = False 10 | 11 | from cupy import cuda 12 | from cupy import testing 13 | from cupy.testing import condition 14 | 15 | 16 | @testing.parameterize(*testing.product({ 17 | 'dtype': [numpy.float32, numpy.float64], 18 | })) 19 | @unittest.skipUnless( 20 | cuda.cusolver_enabled, 'Only cusolver in CUDA 8.0 is supported') 21 | @unittest.skipUnless(scipy_available, 'requires scipy') 22 | class TestLsqr(unittest.TestCase): 23 | 24 | def setUp(self): 25 | rvs = scipy.stats.randint(0, 15).rvs 26 | self.A = scipy.sparse.random(50, 50, density=0.2, data_rvs=rvs) 27 | self.b = numpy.random.randint(15, size=50) 28 | 29 | @testing.numpy_cupy_raises(sp_name='sp') 30 | def test_size(self, xp, sp): 31 | A = sp.csr_matrix(self.A, dtype=self.dtype) 32 | b = xp.array(numpy.append(self.b, [1]), dtype=self.dtype) 33 | sp.linalg.lsqr(A, b) 34 | 35 | @testing.numpy_cupy_raises(sp_name='sp') 36 | def test_shape(self, xp, sp): 37 | A = sp.csr_matrix(self.A, dtype=self.dtype) 38 | b = xp.array(numpy.tile(self.b, (2, 1)), dtype=self.dtype) 39 | sp.linalg.lsqr(A, b) 40 | 41 | @condition.retry(10) 42 | @testing.numpy_cupy_allclose(atol=1e-1, sp_name='sp') 43 | def test_csrmatrix(self, xp, sp): 44 | A = sp.csr_matrix(self.A, dtype=self.dtype) 45 | b = xp.array(self.b, dtype=self.dtype) 46 | x = sp.linalg.lsqr(A, b) 47 | return x[0] 48 | 49 | @condition.retry(10) 50 | @testing.numpy_cupy_allclose(atol=1e-1, sp_name='sp') 51 | def test_ndarray(self, xp, sp): 52 | A = xp.array(self.A.A, dtype=self.dtype) 53 | b = xp.array(self.b, dtype=self.dtype) 54 | x = sp.linalg.lsqr(A, b) 55 | return x[0] 56 | -------------------------------------------------------------------------------- /tests/cupy_tests/test_numpy_interop.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import cupy 6 | from cupy import testing 7 | 8 | 9 | try: 10 | import scipy.sparse 11 | scipy_available = True 12 | except ImportError: 13 | scipy_available = False 14 | 15 | 16 | @testing.gpu 17 | class TestGetArrayModule(unittest.TestCase): 18 | 19 | def test_get_array_module_1(self): 20 | n1 = numpy.array([2], numpy.float32) 21 | c1 = cupy.array([2], numpy.float32) 22 | csr1 = cupy.sparse.csr_matrix((5, 3), dtype=numpy.float32) 23 | 24 | self.assertIs(numpy, cupy.get_array_module()) 25 | self.assertIs(numpy, cupy.get_array_module(n1)) 26 | self.assertIs(cupy, cupy.get_array_module(c1)) 27 | self.assertIs(cupy, cupy.get_array_module(csr1)) 28 | 29 | self.assertIs(numpy, cupy.get_array_module(n1, n1)) 30 | self.assertIs(cupy, cupy.get_array_module(c1, c1)) 31 | self.assertIs(cupy, cupy.get_array_module(csr1, csr1)) 32 | 33 | self.assertIs(cupy, cupy.get_array_module(n1, csr1)) 34 | self.assertIs(cupy, cupy.get_array_module(csr1, n1)) 35 | self.assertIs(cupy, cupy.get_array_module(c1, n1)) 36 | self.assertIs(cupy, cupy.get_array_module(n1, c1)) 37 | self.assertIs(cupy, cupy.get_array_module(c1, csr1)) 38 | self.assertIs(cupy, cupy.get_array_module(csr1, c1)) 39 | 40 | if scipy_available: 41 | csrn1 = scipy.sparse.csr_matrix((5, 3), dtype=numpy.float32) 42 | 43 | self.assertIs(numpy, cupy.get_array_module(csrn1)) 44 | self.assertIs(cupy, cupy.get_array_module(csrn1, csr1)) 45 | self.assertIs(cupy, cupy.get_array_module(csr1, csrn1)) 46 | self.assertIs(cupy, cupy.get_array_module(c1, csrn1)) 47 | self.assertIs(cupy, cupy.get_array_module(csrn1, c1)) 48 | self.assertIs(numpy, cupy.get_array_module(n1, csrn1)) 49 | self.assertIs(numpy, cupy.get_array_module(csrn1, n1)) 50 | -------------------------------------------------------------------------------- /cupy/core/include/cupy/complex/cpow.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace thrust { 6 | 7 | template 8 | __device__ inline complex pow(const complex& z, 9 | const complex& exponent) { 10 | const T absz = abs(z); 11 | if (absz == 0) { 12 | return complex(0, 0); 13 | } 14 | const T real = exponent.real(); 15 | const T imag = exponent.imag(); 16 | const T argz = arg(z); 17 | T r = ::pow(absz, real); 18 | T theta = real * argz; 19 | if (imag != 0) { 20 | r *= ::exp(-imag * argz); 21 | theta += imag * ::log(absz); 22 | } 23 | return complex(r * cos(theta), r * sin(theta)); 24 | } 25 | 26 | template 27 | __device__ inline complex pow(const complex& z, const T& exponent) { 28 | return pow(z, complex(exponent)); 29 | } 30 | 31 | template 32 | __device__ inline complex pow(const T& x, const complex& exponent) { 33 | return pow(complex(x), exponent); 34 | } 35 | 36 | template 37 | __device__ inline complex::type> pow( 38 | const complex& z, const complex& exponent) { 39 | typedef typename _select_greater_type::type PromotedType; 40 | return pow(complex(z), complex(exponent)); 41 | } 42 | 43 | template 44 | __device__ inline complex::type> pow( 45 | const complex& z, const U& exponent) { 46 | typedef typename _select_greater_type::type PromotedType; 47 | return pow(complex(z), PromotedType(exponent)); 48 | } 49 | 50 | template 51 | __device__ inline complex::type> pow( 52 | const T& x, const complex& exponent) { 53 | typedef typename _select_greater_type::type PromotedType; 54 | return pow(PromotedType(x), complex(exponent)); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /tests/cupy_tests/io_tests/test_npz.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import six 4 | 5 | import cupy 6 | from cupy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestNpz(unittest.TestCase): 11 | 12 | @testing.for_all_dtypes() 13 | def test_save_load(self, dtype): 14 | a = testing.shaped_arange((2, 3, 4), dtype=dtype) 15 | sio = six.BytesIO() 16 | cupy.save(sio, a) 17 | s = sio.getvalue() 18 | sio.close() 19 | 20 | sio = six.BytesIO(s) 21 | b = cupy.load(sio) 22 | sio.close() 23 | 24 | testing.assert_array_equal(a, b) 25 | 26 | @testing.for_all_dtypes() 27 | def check_savez(self, savez, dtype): 28 | a1 = testing.shaped_arange((2, 3, 4), dtype=dtype) 29 | a2 = testing.shaped_arange((3, 4, 5), dtype=dtype) 30 | 31 | sio = six.BytesIO() 32 | savez(sio, a1, a2) 33 | s = sio.getvalue() 34 | sio.close() 35 | 36 | sio = six.BytesIO(s) 37 | with cupy.load(sio) as d: 38 | b1 = d['arr_0'] 39 | b2 = d['arr_1'] 40 | sio.close() 41 | 42 | testing.assert_array_equal(a1, b1) 43 | testing.assert_array_equal(a2, b2) 44 | 45 | def test_savez(self): 46 | self.check_savez(cupy.savez) 47 | 48 | def test_savez_compressed(self): 49 | self.check_savez(cupy.savez_compressed) 50 | 51 | @testing.for_all_dtypes() 52 | def test_pickle(self, dtype): 53 | a = testing.shaped_arange((2, 3, 4), dtype=dtype) 54 | s = six.moves.cPickle.dumps(a) 55 | b = six.moves.cPickle.loads(s) 56 | testing.assert_array_equal(a, b) 57 | 58 | @testing.for_all_dtypes() 59 | def test_dump(self, dtype): 60 | a = testing.shaped_arange((2, 3, 4), dtype=dtype) 61 | 62 | sio = six.BytesIO() 63 | a.dump(sio) 64 | s = sio.getvalue() 65 | sio.close() 66 | 67 | sio = six.BytesIO(s) 68 | b = cupy.load(sio) 69 | sio.close() 70 | 71 | testing.assert_array_equal(a, b) 72 | -------------------------------------------------------------------------------- /cupy/indexing/indexing.py: -------------------------------------------------------------------------------- 1 | def take(a, indices, axis=None, out=None): 2 | """Takes elements of an array at specified indices along an axis. 3 | 4 | This is an implementation of "fancy indexing" at single axis. 5 | 6 | This function does not support ``mode`` option. 7 | 8 | Args: 9 | a (cupy.ndarray): Array to extract elements. 10 | indices (int or array-like): Indices of elements that this function 11 | takes. 12 | axis (int): The axis along which to select indices. The flattened input 13 | is used by default. 14 | out (cupy.ndarray): Output array. If provided, it should be of 15 | appropriate shape and dtype. 16 | 17 | Returns: 18 | cupy.ndarray: The result of fancy indexing. 19 | 20 | .. seealso:: :func:`numpy.take` 21 | 22 | """ 23 | # TODO(okuta): check type 24 | return a.take(indices, axis, out) 25 | 26 | 27 | def choose(a, choices, out=None, mode='raise'): 28 | return a.choose(choices, out, mode) 29 | 30 | 31 | # TODO(okuta): Implement compress 32 | 33 | 34 | def diagonal(a, offset=0, axis1=0, axis2=1): 35 | """Returns specified diagonals. 36 | 37 | This function extracts the diagonals along two specified axes. The other 38 | axes are not changed. This function returns a writable view of this array 39 | as NumPy 1.10 will do. 40 | 41 | Args: 42 | a (cupy.ndarray): Array from which the diagonals are taken. 43 | offset (int): Index of the diagonals. Zero indicates the main 44 | diagonals, a positive value upper diagonals, and a negative value 45 | lower diagonals. 46 | axis1 (int): The first axis to take diagonals from. 47 | axis2 (int): The second axis to take diagonals from. 48 | 49 | Returns: 50 | cupy.ndarray: A view of the diagonals of ``a``. 51 | 52 | .. seealso:: :func:`numpy.diagonal` 53 | 54 | """ 55 | # TODO(okuta): check type 56 | return a.diagonal(offset, axis1, axis2) 57 | 58 | 59 | # TODO(okuta): Implement select 60 | -------------------------------------------------------------------------------- /tests/cupy_tests/cuda_tests/memory_hooks_tests/test_debug_print.py: -------------------------------------------------------------------------------- 1 | import json 2 | import six 3 | import unittest 4 | 5 | import cupy.cuda 6 | from cupy.cuda import memory 7 | from cupy.cuda import memory_hooks 8 | from cupy import testing 9 | 10 | 11 | @testing.gpu 12 | class TestDebugPrintHook(unittest.TestCase): 13 | 14 | def setUp(self): 15 | self.io = six.StringIO() 16 | self.hook = memory_hooks.DebugPrintHook(file=self.io) 17 | self.pool = memory.MemoryPool() 18 | 19 | def test_print(self): 20 | device_id = 0 21 | size = 1 22 | unit = 512 23 | with cupy.cuda.Device(device_id): 24 | with self.hook: 25 | mem = self.pool.malloc(size) 26 | ptr1, pmem1 = mem.ptr, id(mem.mem) 27 | del mem 28 | mem = self.pool.malloc(size) 29 | ptr2, pmem2 = mem.ptr, id(mem.mem) 30 | del mem 31 | actual_lines = self.io.getvalue().splitlines() 32 | 33 | expect = {'hook': 'alloc', 'device_id': device_id, 34 | 'mem_size': unit, 'mem_ptr': ptr1} 35 | self.assertEqual(expect, json.loads(actual_lines[0])) 36 | 37 | expect = {'hook': 'malloc', 'device_id': device_id, 'size': size, 38 | 'mem_size': unit, 'mem_ptr': ptr1, 'pmem_id': hex(pmem1)} 39 | self.assertEqual(expect, json.loads(actual_lines[1])) 40 | 41 | expect = {'hook': 'free', 'device_id': device_id, 42 | 'mem_size': unit, 'mem_ptr': ptr1, 'pmem_id': hex(pmem1)} 43 | self.assertEqual(expect, json.loads(actual_lines[2])) 44 | 45 | expect = {'hook': 'malloc', 'device_id': device_id, 'size': size, 46 | 'mem_size': unit, 'mem_ptr': ptr2, 'pmem_id': hex(pmem2)} 47 | self.assertEqual(expect, json.loads(actual_lines[3])) 48 | 49 | expect = {'hook': 'free', 'device_id': device_id, 50 | 'mem_size': unit, 'mem_ptr': ptr2, 'pmem_id': hex(pmem2)} 51 | self.assertEqual(expect, json.loads(actual_lines[4])) 52 | -------------------------------------------------------------------------------- /cupy/core/include/cupy/complex/cproj.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008-2013 NVIDIA Corporation 3 | * Copyright 2013 Filipe RNC Maia 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #pragma once 19 | 20 | #include 21 | #include 22 | 23 | namespace thrust { 24 | namespace detail { 25 | namespace complex { 26 | 27 | using thrust::complex; 28 | 29 | __device__ inline complex cprojf(const complex& z) { 30 | if (!isinf(z.real()) && !isinf(z.imag())) { 31 | return z; 32 | } else { 33 | // std::numeric_limits::infinity() doesn't run on the GPU 34 | return complex(infinity(), copysignf(0.0, z.imag())); 35 | } 36 | } 37 | 38 | __device__ inline complex cproj(const complex& z) { 39 | if (!isinf(z.real()) && !isinf(z.imag())) { 40 | return z; 41 | } else { 42 | // numeric_limits::infinity() doesn't run on the GPU 43 | return complex(infinity(), copysign(0.0, z.imag())); 44 | } 45 | } 46 | } 47 | } 48 | 49 | template 50 | __device__ inline thrust::complex proj(const thrust::complex& z) { 51 | return detail::complex::cproj(z); 52 | } 53 | 54 | template <> 55 | __device__ inline thrust::complex proj( 56 | const thrust::complex& z) { 57 | return detail::complex::cproj(z); 58 | } 59 | 60 | template <> 61 | __device__ inline thrust::complex proj(const thrust::complex& z) { 62 | return detail::complex::cprojf(z); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /docs/source/reference/ndarray.rst: -------------------------------------------------------------------------------- 1 | Multi-Dimensional Array (ndarray) 2 | ================================= 3 | 4 | :class:`cupy.ndarray` is the CuPy counterpart of NumPy :class:`numpy.ndarray`. 5 | It provides an intuitive interface for a fixed-size multidimensional array which resides 6 | in a CUDA device. 7 | 8 | For the basic concept of ``ndarray``\s, please refer to the `NumPy documentation `_. 9 | 10 | 11 | .. autosummary:: 12 | :toctree: generated/ 13 | :nosignatures: 14 | 15 | cupy.ndarray 16 | 17 | 18 | Code compatibility features 19 | --------------------------- 20 | 21 | :class:`cupy.ndarray` is designed to be interchangeable with :class:`numpy.ndarray` in terms of code compatibility as much as possible. 22 | But occasionally, you will need to know whether the arrays you're handling are :class:`cupy.ndarray` or :class:`numpy.ndarray`. 23 | One example is when invoking module-level functions such as :func:`cupy.sum` or :func:`numpy.sum`. 24 | In such situations, :func:`cupy.get_array_module` can be used. 25 | 26 | .. autosummary:: 27 | :toctree: generated/ 28 | :nosignatures: 29 | 30 | cupy.get_array_module 31 | cupyx.scipy.get_array_module 32 | 33 | 34 | Conversion to/from NumPy arrays 35 | ------------------------------- 36 | 37 | :class:`cupy.ndarray` and :class:`numpy.ndarray` are not implicitly convertible to each other. 38 | That means, NumPy functions cannot take :class:`cupy.ndarray`\s as inputs, and vice versa. 39 | 40 | - To convert :class:`numpy.ndarray` to :class:`cupy.ndarray`, use :func:`cupy.array` or :func:`cupy.asarray`. 41 | - To convert :class:`cupy.ndarray` to :class:`numpy.ndarray`, use :func:`cupy.asnumpy` or :meth:`cupy.ndarray.get`. 42 | 43 | Note that converting between :class:`cupy.ndarray` and :class:`numpy.ndarray` incurs data transfer between 44 | the host (CPU) device and the GPU device, which is costly in terms of performance. 45 | 46 | 47 | .. autosummary:: 48 | :toctree: generated/ 49 | :nosignatures: 50 | 51 | cupy.array 52 | cupy.asarray 53 | cupy.asnumpy 54 | -------------------------------------------------------------------------------- /cupy/math/explog.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | from cupy.math import ufunc 3 | 4 | 5 | exp = ufunc.create_math_ufunc( 6 | 'exp', 1, 'cupy_exp', 7 | '''Elementwise exponential function. 8 | 9 | .. seealso:: :data:`numpy.exp` 10 | 11 | ''') 12 | 13 | 14 | expm1 = ufunc.create_math_ufunc( 15 | 'expm1', 1, 'cupy_expm1', 16 | '''Computes ``exp(x) - 1`` elementwise. 17 | 18 | .. seealso:: :data:`numpy.expm1` 19 | 20 | ''') 21 | 22 | 23 | exp2 = core.create_ufunc( 24 | 'cupy_exp2', 25 | ('e->e', 'f->f', 'd->d', 'F->F', 'D->D'), 26 | 'out0 = pow(in0_type(2), in0)', 27 | doc='''Elementwise exponentiation with base 2. 28 | 29 | .. seealso:: :data:`numpy.exp2` 30 | 31 | ''') 32 | 33 | 34 | log = ufunc.create_math_ufunc( 35 | 'log', 1, 'cupy_log', 36 | '''Elementwise natural logarithm function. 37 | 38 | .. seealso:: :data:`numpy.log` 39 | 40 | ''') 41 | 42 | 43 | log10 = ufunc.create_math_ufunc( 44 | 'log10', 1, 'cupy_log10', 45 | '''Elementwise common logarithm function. 46 | 47 | .. seealso:: :data:`numpy.log10` 48 | 49 | ''') 50 | 51 | 52 | log2 = ufunc.create_math_ufunc( 53 | 'log2', 1, 'cupy_log2', 54 | '''Elementwise binary logarithm function. 55 | 56 | .. seealso:: :data:`numpy.log2` 57 | 58 | ''') 59 | 60 | 61 | log1p = ufunc.create_math_ufunc( 62 | 'log1p', 1, 'cupy_log1p', 63 | '''Computes ``log(1 + x)`` elementwise. 64 | 65 | .. seealso:: :data:`numpy.log1p` 66 | 67 | ''') 68 | 69 | 70 | logaddexp = core.create_ufunc( 71 | 'cupy_logaddexp', 72 | ('ee->e', 'ff->f', 'dd->d'), 73 | 'out0 = fmax(in0, in1) + log1p(exp(-fabs(in0 - in1)))', 74 | doc='''Computes ``log(exp(x1) + exp(x2))`` elementwise. 75 | 76 | .. seealso:: :data:`numpy.logaddexp` 77 | 78 | ''') 79 | 80 | 81 | logaddexp2 = core.create_ufunc( 82 | 'cupy_logaddexp2', 83 | ('ee->e', 'ff->f', 'dd->d'), 84 | 'out0 = fmax(in0, in1) + log2(1 + exp2(-fabs(in0 - in1)))', 85 | doc='''Computes ``log2(exp2(x1) + exp2(x2))`` elementwise. 86 | 87 | .. seealso:: :data:`numpy.logaddexp2` 88 | 89 | ''') 90 | -------------------------------------------------------------------------------- /cupy/core/raw.pyx: -------------------------------------------------------------------------------- 1 | import cupy 2 | from cupy import util 3 | 4 | import six 5 | 6 | 7 | cdef class RawKernel: 8 | 9 | """User-defined custom kernel. 10 | 11 | This class can be used to define a custom kernel using raw CUDA source. 12 | 13 | The kernel is compiled at an invocation of the :meth:`~RawKernel.__call__` 14 | method, which is cached for each device. 15 | The compiled binary is also cached into a file under the 16 | ``$HOME/.cupy/kernel_cache/`` directory with a hashed file name. The cached 17 | binary is reused by other processes. 18 | 19 | Args: 20 | code (str): CUDA source code. 21 | name (str): Name of the kernel function. 22 | options (str): Compile options passed to NVRTC. For details, see 23 | https://docs.nvidia.com/cuda/nvrtc/index.html#group__options. 24 | 25 | """ 26 | 27 | def __init__(self, code, name, options=()): 28 | if isinstance(code, six.binary_type): 29 | code = code.decode('UTF-8') 30 | if isinstance(name, six.binary_type): 31 | name = name.decode('UTF-8') 32 | self.code = code 33 | self.name = name 34 | self.options = options 35 | 36 | def __call__(self, grid, block, args, **kwargs): 37 | """__call__(self, grid, block, args, *, shared_mem=0) 38 | 39 | Compiles and invokes the kernel. 40 | 41 | The compilation runs only if the kernel is not cached. 42 | 43 | Args: 44 | grid (tuple): Size of grid in blocks. 45 | block (tuple): Dimensions of each thread block. 46 | args (tuple): Arguments of the kernel. 47 | shared_mem (int): Dynamic shared-memory size per thread block in 48 | bytes. 49 | 50 | """ 51 | kern = _get_raw_kernel(self.code, self.name, self.options) 52 | kern(grid, block, args, **kwargs) 53 | 54 | 55 | @cupy.util.memoize(for_each_device=True) 56 | def _get_raw_kernel(code, name, options=()): 57 | module = cupy.core.core.compile_with_cache( 58 | code, options, prepend_cupy_headers=False) 59 | return module.get_function(name) 60 | -------------------------------------------------------------------------------- /tests/cupyx_tests/scipy_tests/linalg_tests/test_decomp_lu.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | import cupy 5 | from cupy import cuda 6 | from cupy import testing 7 | import cupyx.scipy.linalg 8 | if cupyx.scipy._scipy_available: 9 | import scipy.linalg 10 | 11 | 12 | @testing.gpu 13 | @testing.parameterize(*testing.product({ 14 | 'shape': [(1, 1), (2, 2), (3, 3), (5, 5)], 15 | })) 16 | @testing.fix_random() 17 | @unittest.skipUnless( 18 | cuda.cusolver_enabled, 'Only cusolver in CUDA 8.0 is supported') 19 | @testing.with_requires('scipy') 20 | class TestLUFactor(unittest.TestCase): 21 | 22 | @testing.for_float_dtypes(no_float16=True) 23 | def test_lu_factor(self, dtype): 24 | array = numpy.random.randn(*self.shape) 25 | a_cpu = numpy.asarray(array, dtype=dtype) 26 | a_gpu = cupy.asarray(array, dtype=dtype) 27 | result_cpu = scipy.linalg.lu_factor(a_cpu) 28 | result_gpu = cupyx.scipy.linalg.lu_factor(a_gpu) 29 | self.assertEqual(len(result_cpu), len(result_gpu)) 30 | self.assertEqual(result_cpu[0].dtype, result_gpu[0].dtype) 31 | self.assertEqual(result_cpu[1].dtype, result_gpu[1].dtype) 32 | cupy.testing.assert_allclose(result_cpu[0], result_gpu[0], atol=1e-5) 33 | cupy.testing.assert_array_equal(result_cpu[1], result_gpu[1]) 34 | 35 | 36 | @testing.gpu 37 | @testing.parameterize(*testing.product({ 38 | 'trans': [0, 1, 2], 39 | 'shapes': [((4, 4), (4,)), ((5, 5), (5, 2))], 40 | })) 41 | @testing.fix_random() 42 | @unittest.skipUnless( 43 | cuda.cusolver_enabled, 'Only cusolver in CUDA 8.0 is supported') 44 | @testing.with_requires('scipy') 45 | class TestLUSolve(unittest.TestCase): 46 | 47 | @testing.for_float_dtypes(no_float16=True) 48 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 49 | def test_lu_solve(self, xp, scp, dtype): 50 | a_shape, b_shape = self.shapes 51 | A = testing.shaped_random(a_shape, xp, dtype=dtype) 52 | b = testing.shaped_random(b_shape, xp, dtype=dtype) 53 | lu = scp.linalg.lu_factor(A) 54 | return scp.linalg.lu_solve(lu, b, trans=self.trans) 55 | -------------------------------------------------------------------------------- /tests/cupyx_tests/scipy_tests/special_tests/test_digamma.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from cupy import testing 4 | import cupyx.scipy.special # NOQA 5 | import numpy 6 | 7 | 8 | @testing.gpu 9 | @testing.with_requires('scipy') 10 | class TestDigamma(unittest.TestCase): 11 | 12 | @testing.with_requires('scipy>=1.1.0') 13 | @testing.for_all_dtypes(no_complex=True) 14 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 15 | def test_arange(self, xp, scp, dtype): 16 | import scipy.special # NOQA 17 | 18 | a = testing.shaped_arange((2, 3), xp, dtype) 19 | return scp.special.digamma(a) 20 | 21 | @testing.with_requires('scipy>=1.1.0') 22 | @testing.for_all_dtypes(no_complex=True) 23 | @testing.numpy_cupy_allclose(atol=1e-5, scipy_name='scp') 24 | def test_linspace_positive(self, xp, scp, dtype): 25 | import scipy.special # NOQA 26 | 27 | a = numpy.linspace(0, 30, 1000, dtype=dtype) 28 | a = xp.asarray(a) 29 | return scp.special.digamma(a) 30 | 31 | @testing.with_requires('scipy>=1.1.0') 32 | @testing.for_all_dtypes(no_complex=True) 33 | @testing.numpy_cupy_allclose(atol=1e-2, rtol=1e-3, scipy_name='scp') 34 | def test_linspace_negative(self, xp, scp, dtype): 35 | import scipy.special # NOQA 36 | 37 | a = numpy.linspace(-30, 0, 1000, dtype=dtype) 38 | a = xp.asarray(a) 39 | return scp.special.digamma(a) 40 | 41 | @testing.for_all_dtypes(no_complex=True) 42 | @testing.numpy_cupy_allclose(atol=1e-2, rtol=1e-3, scipy_name='scp') 43 | def test_scalar(self, xp, scp, dtype): 44 | import scipy.special # NOQA 45 | 46 | return scp.special.digamma(dtype(1.5)) 47 | 48 | @testing.with_requires('scipy>=1.1.0') 49 | @testing.for_all_dtypes(no_complex=True) 50 | @testing.numpy_cupy_allclose(atol=1e-2, rtol=1e-3, scipy_name='scp') 51 | def test_inf_and_nan(self, xp, scp, dtype): 52 | import scipy.special # NOQA 53 | 54 | a = numpy.array([-numpy.inf, numpy.nan, numpy.inf]).astype(dtype) 55 | a = xp.asarray(a) 56 | return scp.special.digamma(a) 57 | -------------------------------------------------------------------------------- /cupy/cuda/memory.pxd: -------------------------------------------------------------------------------- 1 | cimport cython # NOQA 2 | 3 | from libc.stdint cimport intptr_t 4 | from libcpp cimport vector 5 | from libcpp cimport map 6 | 7 | from cupy.cuda cimport device 8 | 9 | 10 | @cython.no_gc 11 | cdef class BaseMemory: 12 | 13 | cdef: 14 | public intptr_t ptr 15 | public size_t size 16 | public int device_id 17 | 18 | 19 | @cython.final 20 | cdef class MemoryPointer: 21 | 22 | cdef: 23 | readonly intptr_t ptr 24 | readonly int device_id 25 | readonly BaseMemory mem 26 | 27 | cdef _init(self, BaseMemory mem, ptrdiff_t offset) 28 | 29 | cpdef copy_from_device(self, MemoryPointer src, size_t size) 30 | cpdef copy_from_device_async(self, MemoryPointer src, size_t size, 31 | stream=?) 32 | cpdef copy_from_host(self, mem, size_t size) 33 | cpdef copy_from_host_async(self, mem, size_t size, stream=?) 34 | cpdef copy_from(self, mem, size_t size) 35 | cpdef copy_from_async(self, mem, size_t size, stream=?) 36 | cpdef copy_to_host(self, mem, size_t size) 37 | cpdef copy_to_host_async(self, mem, size_t size, stream=?) 38 | cpdef memset(self, int value, size_t size) 39 | cpdef memset_async(self, int value, size_t size, stream=?) 40 | 41 | 42 | cpdef MemoryPointer alloc(size) 43 | 44 | 45 | cpdef set_allocator(allocator=*) 46 | 47 | 48 | cdef class MemoryPool: 49 | 50 | cdef: 51 | object _pools 52 | 53 | cpdef MemoryPointer malloc(self, size_t size) 54 | cpdef free_all_blocks(self, stream=?) 55 | cpdef free_all_free(self) 56 | cpdef size_t n_free_blocks(self) 57 | cpdef size_t used_bytes(self) 58 | cpdef size_t free_bytes(self) 59 | cpdef size_t total_bytes(self) 60 | 61 | 62 | @cython.no_gc 63 | cdef class CFunctionAllocatorMemory(BaseMemory): 64 | 65 | cdef: 66 | intptr_t _param 67 | intptr_t _free_func 68 | 69 | 70 | cdef class CFunctionAllocator: 71 | 72 | cdef: 73 | intptr_t _param 74 | intptr_t _malloc_func 75 | intptr_t _free_func 76 | object _owner 77 | 78 | cpdef MemoryPointer malloc(self, size_t size) 79 | -------------------------------------------------------------------------------- /cupy/logic/truth.py: -------------------------------------------------------------------------------- 1 | import cupy 2 | from cupy.core import _routines_logic as _logic 3 | from cupy.core import fusion 4 | 5 | 6 | def all(a, axis=None, out=None, keepdims=False): 7 | """Tests whether all array elements along a given axis evaluate to True. 8 | 9 | Args: 10 | a (cupy.ndarray): Input array. 11 | axis (int or tuple of ints): Along which axis to compute all. 12 | The flattened array is used by default. 13 | out (cupy.ndarray): Output array. 14 | keepdims (bool): If ``True``, the axis is remained as an axis of 15 | size one. 16 | 17 | Returns: 18 | cupy.ndarray: An array reduced of the input array along the axis. 19 | 20 | .. seealso:: :func:`numpy.all` 21 | 22 | """ 23 | if fusion._is_fusing(): 24 | if keepdims: 25 | raise NotImplementedError( 26 | 'cupy.all does not support `keepdims` in fusion yet.') 27 | return fusion._call_reduction( 28 | _logic.all, a, axis=axis, out=out) 29 | 30 | assert isinstance(a, cupy.ndarray) 31 | return a.all(axis=axis, out=out, keepdims=keepdims) 32 | 33 | 34 | def any(a, axis=None, out=None, keepdims=False): 35 | """Tests whether any array elements along a given axis evaluate to True. 36 | 37 | Args: 38 | a (cupy.ndarray): Input array. 39 | axis (int or tuple of ints): Along which axis to compute all. 40 | The flattened array is used by default. 41 | out (cupy.ndarray): Output array. 42 | keepdims (bool): If ``True``, the axis is remained as an axis of 43 | size one. 44 | 45 | Returns: 46 | cupy.ndarray: An array reduced of the input array along the axis. 47 | 48 | .. seealso:: :func:`numpy.any` 49 | 50 | """ 51 | if fusion._is_fusing(): 52 | if keepdims: 53 | raise NotImplementedError( 54 | 'cupy.any does not support `keepdims` in fusion yet.') 55 | return fusion._call_reduction( 56 | _logic.any, a, axis=axis, out=out) 57 | 58 | assert isinstance(a, cupy.ndarray) 59 | return a.any(axis=axis, out=out, keepdims=keepdims) 60 | -------------------------------------------------------------------------------- /cupy/math/rounding.py: -------------------------------------------------------------------------------- 1 | from cupy import core 2 | from cupy.core import fusion 3 | from cupy.math import ufunc 4 | 5 | 6 | def around(a, decimals=0, out=None): 7 | """Rounds to the given number of decimals. 8 | 9 | Args: 10 | a (cupy.ndarray): The source array. 11 | decimals (int): umber of decimal places to round to (default: 0). 12 | If decimals is negative, it specifies the number of positions to 13 | the left of the decimal point. 14 | out (cupy.ndarray): Output array. 15 | 16 | Returns: 17 | cupy.ndarray: Rounded array. 18 | 19 | .. seealso:: :func:`numpy.around` 20 | 21 | """ 22 | if fusion._is_fusing(): 23 | return fusion._call_ufunc(core.core._round_ufunc, a, decimals, out=out) 24 | a = core.array(a, copy=False) 25 | return a.round(decimals, out=out) 26 | 27 | 28 | def round_(a, decimals=0, out=None): 29 | return around(a, decimals, out=out) 30 | 31 | 32 | rint = ufunc.create_math_ufunc( 33 | 'rint', 1, 'cupy_rint', 34 | '''Rounds each element of an array to the nearest integer. 35 | 36 | .. seealso:: :data:`numpy.rint` 37 | 38 | ''') 39 | 40 | 41 | floor = ufunc.create_math_ufunc( 42 | 'floor', 1, 'cupy_floor', 43 | '''Rounds each element of an array to its floor integer. 44 | 45 | .. seealso:: :data:`numpy.floor` 46 | 47 | ''', support_complex=False) 48 | 49 | 50 | ceil = ufunc.create_math_ufunc( 51 | 'ceil', 1, 'cupy_ceil', 52 | '''Rounds each element of an array to its ceiling integer. 53 | 54 | .. seealso:: :data:`numpy.ceil` 55 | 56 | ''', support_complex=False) 57 | 58 | 59 | trunc = ufunc.create_math_ufunc( 60 | 'trunc', 1, 'cupy_trunc', 61 | '''Rounds each element of an array towards zero. 62 | 63 | .. seealso:: :data:`numpy.trunc` 64 | 65 | ''', support_complex=False) 66 | 67 | 68 | fix = core.create_ufunc( 69 | 'cupy_fix', ('e->e', 'f->f', 'd->d'), 70 | 'out0 = (in0 >= 0.0) ? floor(in0): ceil(in0)', 71 | doc='''If given value x is positive, it return floor(x). 72 | Else, it return ceil(x). 73 | 74 | .. seealso:: :func:`numpy.fix` 75 | 76 | ''') 77 | -------------------------------------------------------------------------------- /tests/cupy_tests/manipulation_tests/test_kind.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy 4 | 5 | import cupy 6 | from cupy import testing 7 | 8 | 9 | @testing.gpu 10 | class TestKind(unittest.TestCase): 11 | 12 | @testing.for_all_dtypes() 13 | def test_asfortranarray1(self, dtype): 14 | def func(xp): 15 | x = xp.zeros((2, 3), dtype) 16 | ret = xp.asfortranarray(x) 17 | self.assertTrue(x.flags.c_contiguous) 18 | self.assertTrue(ret.flags.f_contiguous) 19 | return ret.strides 20 | self.assertEqual(func(numpy), func(cupy)) 21 | 22 | @testing.for_all_dtypes() 23 | def test_asfortranarray2(self, dtype): 24 | def func(xp): 25 | x = xp.zeros((2, 3, 4), dtype) 26 | ret = xp.asfortranarray(x) 27 | self.assertTrue(x.flags.c_contiguous) 28 | self.assertTrue(ret.flags.f_contiguous) 29 | return ret.strides 30 | self.assertEqual(func(numpy), func(cupy)) 31 | 32 | @testing.for_all_dtypes() 33 | def test_asfortranarray3(self, dtype): 34 | def func(xp): 35 | x = xp.zeros((2, 3, 4), dtype) 36 | ret = xp.asfortranarray(xp.asfortranarray(x)) 37 | self.assertTrue(x.flags.c_contiguous) 38 | self.assertTrue(ret.flags.f_contiguous) 39 | return ret.strides 40 | self.assertEqual(func(numpy), func(cupy)) 41 | 42 | @testing.for_all_dtypes() 43 | def test_asfortranarray4(self, dtype): 44 | def func(xp): 45 | x = xp.zeros((2, 3), dtype) 46 | x = xp.transpose(x, (1, 0)) 47 | ret = xp.asfortranarray(x) 48 | self.assertTrue(ret.flags.f_contiguous) 49 | return ret.strides 50 | self.assertEqual(func(numpy), func(cupy)) 51 | 52 | @testing.for_all_dtypes() 53 | def test_asfortranarray5(self, dtype): 54 | def func(xp): 55 | x = testing.shaped_arange((2, 3), xp, dtype) 56 | ret = xp.asfortranarray(x) 57 | self.assertTrue(x.flags.c_contiguous) 58 | self.assertTrue(ret.flags.f_contiguous) 59 | return ret.strides 60 | self.assertEqual(func(numpy), func(cupy)) 61 | -------------------------------------------------------------------------------- /cupyx/linalg/sparse/solve.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | 3 | import cupy 4 | from cupy import cuda 5 | from cupy.cuda import device 6 | from cupy.linalg import util 7 | import cupy.sparse 8 | 9 | 10 | if cuda.cusolver_enabled: 11 | from cupy.cuda import cusolver 12 | 13 | 14 | def lschol(A, b): 15 | """Solves linear system with cholesky decomposition. 16 | 17 | Find the solution to a large, sparse, linear system of equations. 18 | The function solves ``Ax = b``. Given two-dimensional matrix ``A`` is 19 | decomposed into ``L * L^*``. 20 | 21 | Args: 22 | A (cupy.ndarray or cupy.sparse.csr_matrix): The input matrix with 23 | dimension ``(N, N)``. Must be positive-definite input matrix. 24 | Only symmetric real matrix is supported currently. 25 | b (cupy.ndarray): Right-hand side vector. 26 | 27 | Returns: 28 | ret (cupy.ndarray): The solution vector ``x``. 29 | 30 | """ 31 | 32 | if not cuda.cusolver_enabled: 33 | raise RuntimeError('Current cupy only supports cusolver in CUDA 8.0') 34 | 35 | if not cupy.sparse.isspmatrix_csr(A): 36 | A = cupy.sparse.csr_matrix(A) 37 | util._assert_nd_squareness(A) 38 | util._assert_cupy_array(b) 39 | m = A.shape[0] 40 | if b.ndim != 1 or len(b) != m: 41 | raise ValueError('b must be 1-d array whose size is same as A') 42 | 43 | # Cast to float32 or float64 44 | if A.dtype == 'f' or A.dtype == 'd': 45 | dtype = A.dtype 46 | else: 47 | dtype = numpy.find_common_type((A.dtype, 'f'), ()) 48 | 49 | handle = device.get_cusolver_sp_handle() 50 | nnz = A.nnz 51 | tol = 1.0 52 | reorder = 1 53 | x = cupy.empty(m, dtype=dtype) 54 | singularity = numpy.empty(1, numpy.int32) 55 | 56 | if dtype == 'f': 57 | csrlsvchol = cusolver.scsrlsvchol 58 | else: 59 | csrlsvchol = cusolver.dcsrlsvchol 60 | csrlsvchol( 61 | handle, m, nnz, A._descr.descriptor, A.data.data.ptr, 62 | A.indptr.data.ptr, A.indices.data.ptr, b.data.ptr, tol, reorder, 63 | x.data.ptr, singularity.ctypes.data) 64 | 65 | # The return type of SciPy is always float64. 66 | x = x.astype(numpy.float64) 67 | 68 | return x 69 | --------------------------------------------------------------------------------