当前位置: 首页>>代码示例>>Python>>正文


Python cuda.cupy方法代码示例

本文整理汇总了Python中chainer.backends.cuda.cupy方法的典型用法代码示例。如果您正苦于以下问题:Python cuda.cupy方法的具体用法?Python cuda.cupy怎么用?Python cuda.cupy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chainer.backends.cuda的用法示例。


在下文中一共展示了cuda.cupy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: setUp

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def setUp(self):
        x_shape_0 = 2
        x_shape_1 = numpy.int64(3)
        self.link = chainer.Link(x=((x_shape_0, x_shape_1), 'd'),
                                 u=(None, 'd'))
        with self.link.init_scope():
            self.link.y = chainer.Parameter(shape=(2,))
            self.link.v = chainer.Parameter()
        self.p = numpy.array([1, 2, 3], dtype='f')
        self.link.add_persistent('p', self.p)
        self.link.name = 'a'
        self.link.x.update_rule = chainer.UpdateRule()
        self.link.x.update_rule.enabled = False
        self.link.u.update_rule = chainer.UpdateRule()
        if cuda.available:
            self.current_device_id = cuda.cupy.cuda.get_device_id() 
开发者ID:chainer,项目名称:chainer,代码行数:18,代码来源:test_link.py

示例2: test_copy_and_to_gpu_uninit_multi_gpu

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def test_copy_and_to_gpu_uninit_multi_gpu(self):
        cupy = cuda.cupy
        l0 = self.link
        l1 = l0.copy()
        l2 = l0.copy()
        self.assertIsNone(l0.u.data)
        self.assertIsNone(l1.u.data)
        self.assertIsNone(l2.u.data)
        with testing.assert_warns(DeprecationWarning):
            l1.to_gpu()
        l1.u.initialize((2, 3))
        with testing.assert_warns(DeprecationWarning):
            l2.to_gpu()
        l2.u.initialize((2, 3))
        self.assertIsNone(l0.u.data)
        self.assertIsInstance(l1.u.data, cupy.ndarray)
        self.assertIsInstance(l2.u.data, cupy.ndarray)
        self.assertNotEqual(l1.u.data.data, l2.u.data.data) 
开发者ID:chainer,项目名称:chainer,代码行数:20,代码来源:test_link.py

示例3: test_to_chx

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def test_to_chx(self, backend_config):
        self.link.to_device(backend_config.device)
        self.link.to_chx()

        source_device = backend_config.device

        self.check_param_init('x', (2, 3), 'd')
        self.check_param_init('y', (2,), 'f')
        self.check_param_uninit('u')

        if source_device.xp is chainerx:
            expected_device = source_device
        elif source_device.xp is numpy:
            expected_device = backend.ChainerxDevice(
                chainerx.get_device('native', 0))
        elif source_device.xp is cuda.cupy:
            expected_device = backend.ChainerxDevice(
                chainerx.get_device('cuda', source_device.device.id))
        else:
            assert False

        self.assertEqual(self.link.device, expected_device) 
开发者ID:chainer,项目名称:chainer,代码行数:24,代码来源:test_link.py

示例4: test_to_gpu

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def test_to_gpu(self):
        self.set_count_parameters()
        cupy = cuda.cupy
        with testing.assert_warns(DeprecationWarning):
            self.c2.to_gpu()
        self.assertIs(self.c2.xp, cupy)
        self.assertIs(self.c1.xp, cupy)
        self.assertIs(self.l1.xp, cupy)
        self.assertIs(self.l2.xp, cupy)
        self.assertIs(self.l3.xp, cupy)
        self.assertIsInstance(self.l1.x.data, cupy.ndarray)
        self.assertIsInstance(self.l1.x.grad, cupy.ndarray)
        self.assertIsInstance(self.l2.x.data, cupy.ndarray)
        self.assertIsInstance(self.l2.x.grad, cupy.ndarray)
        self.assertIsNone(self.l3.x.data)
        self.assertIsNone(self.l3.x.grad)

        self.l3.x.initialize(3)
        self.assertIsInstance(self.l3.x.data, cupy.ndarray)
        self.assertIsInstance(self.l3.x.grad, cupy.ndarray) 
开发者ID:chainer,项目名称:chainer,代码行数:22,代码来源:test_link.py

示例5: test_intel64_to_gpu

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def test_intel64_to_gpu(self):
        link = self.link
        with testing.assert_warns(DeprecationWarning):
            link.to_intel64()
        assert isinstance(link.device, backend.Intel64Device)
        with testing.assert_warns(DeprecationWarning):
            link.to_gpu()
        assert link.device.device == cuda.Device(0)

        # Arrays should be converted to cupy.ndarray

        # Initialized parameter
        assert isinstance(link.y.data, cuda.cupy.ndarray)
        _assert_variable_array_equal(link.y, self.y_array)
        # Uninitialized parameter
        assert link.v.data is None
        # Persistent ndarray
        assert isinstance(link.pa, cuda.ndarray)
        _assert_arrays_equal(link.pa, self.pa_array)
        # Persistent scalar
        assert link.ps == self.ps_scalar 
开发者ID:chainer,项目名称:chainer,代码行数:23,代码来源:test_link.py

示例6: _dummy_func

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def _dummy_func(self, bwd_return_data):
        # Create a dummy func that returns `bwd_return_data` in the
        # `backward` method.

        def one(xp):
            return xp.array(1, numpy.float32)

        class DummyFunc(chainer.FunctionNode):
            def forward_cpu(self, inputs):
                return one(numpy),

            def forward_gpu(self, inputs):
                return one(cuda.cupy),

            def backward(self, indexes, grad_outputs):
                return bwd_return_data

        return DummyFunc() 
开发者ID:chainer,项目名称:chainer,代码行数:20,代码来源:test_function_node.py

示例7: check_to_cpu_to_gpu

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def check_to_cpu_to_gpu(self, c, h):
        self.link.c = c
        self.link.h = h
        with testing.assert_warns(DeprecationWarning):
            self.link.to_gpu()
        self.assertIs(self.link.xp, cuda.cupy)
        self.assertIsInstance(self.link.c.data, self.link.xp.ndarray)
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        with testing.assert_warns(DeprecationWarning):
            self.link.to_gpu()
        self.assertIs(self.link.xp, cuda.cupy)
        self.assertIsInstance(self.link.c.data, self.link.xp.ndarray)
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        with testing.assert_warns(DeprecationWarning):
            self.link.to_cpu()
        self.assertIs(self.link.xp, numpy)
        self.assertIsInstance(self.link.c.data, self.link.xp.ndarray)
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray)
        with testing.assert_warns(DeprecationWarning):
            self.link.to_gpu()
        self.assertIs(self.link.xp, cuda.cupy)
        self.assertIsInstance(self.link.c.data, self.link.xp.ndarray)
        self.assertIsInstance(self.link.h.data, self.link.xp.ndarray) 
开发者ID:chainer,项目名称:chainer,代码行数:25,代码来源:test_zoneoutlstm.py

示例8: __call__

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def __call__(self, inputs, device=None):
        """Convert DALI arrays to Numpy/CuPy arrays"""

        xp = chainer.backend.get_array_module(self.perturbation)
        if xp is not cuda.cupy:
            self.perturbation = cuda.to_gpu(self.perturbation, device)

        outputs = []
        for i in range(len(inputs)):
            x = inputs[i].as_tensor()
            if (isinstance(x, dali.backend_impl.TensorCPU)):
                x = np.array(x)
                if x.ndim == 2 and x.shape[1] == 1:
                    x = x.squeeze(axis=1)
                if device is not None and device >= 0:
                    x = cuda.to_gpu(x, device)
            elif (isinstance(x, dali.backend_impl.TensorGPU)):
                x_cupy = cuda.cupy.empty(shape=x.shape(), dtype=x.dtype())
                # Synchronization is necessary here to avoid data corruption
                # because DALI and CuPy will use different CUDA streams.
                cuda.cupy.cuda.runtime.deviceSynchronize()
                # copy data from DALI array to CuPy array
                x.copy_to_external(ctypes.c_void_p(x_cupy.data.ptr))
                cuda.cupy.cuda.runtime.deviceSynchronize()
                x = x_cupy.astype(chainer.get_dtype())
                if self.perturbation is not None:
                    x = x - self.perturbation
                if device is not None and device < 0:
                    x = cuda.to_cpu(x)
            else:
                raise ValueError('Unexpected object')
            outputs.append(x)
        return tuple(outputs) 
开发者ID:chainer,项目名称:chainer,代码行数:35,代码来源:dali_util.py

示例9: dali_converter

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def dali_converter(inputs, device=None):
    """Convert DALI arrays to Numpy/CuPy arrays"""

    outputs = []
    for i in range(len(inputs)):
        x = inputs[i].as_tensor()
        if (isinstance(x, dali.backend_impl.TensorCPU)):
            x = np.array(x)
            if x.ndim == 2 and x.shape[1] == 1:
                x = x.squeeze(axis=1)
            if device is not None and device >= 0:
                x = cuda.to_gpu(x, device)
        elif (isinstance(x, dali.backend_impl.TensorGPU)):
            x_cupy = cuda.cupy.empty(shape=x.shape(), dtype=x.dtype())
            # Synchronization is necessary here to avoid data corruption
            # because DALI and CuPy will use different CUDA streams.
            cuda.cupy.cuda.runtime.deviceSynchronize()
            # copy data from DALI array to CuPy array
            x.copy_to_external(ctypes.c_void_p(x_cupy.data.ptr))
            cuda.cupy.cuda.runtime.deviceSynchronize()
            x = x_cupy.astype(chainer.get_dtype())
            if device is not None and device < 0:
                x = cuda.to_cpu(x)
        else:
            raise ValueError('Unexpected object')
        outputs.append(x)
    return tuple(outputs) 
开发者ID:chainer,项目名称:chainer,代码行数:29,代码来源:dali_util.py

示例10: get_mean_and_var

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def get_mean_and_var(self, axis, gamma, x, xp, interm_dtype):
        tmp = xp.empty(gamma.size * 2, dtype=gamma.dtype)
        x.mean(axis=axis, out=tmp[:gamma.size], dtype=gamma.dtype)
        xp.square(x).mean(axis=axis, out=tmp[gamma.size:], dtype=gamma.dtype)
        if xp is cuda.cupy:
            chainer.cuda.Stream.null.synchronize()
        self.comm._multi_node_mean(None, tmp)
        mean = tmp[:gamma.size]
        sqmean = tmp[gamma.size:]
        var = sqmean - xp.square(mean)
        return mean, var 
开发者ID:chainer,项目名称:chainer,代码行数:13,代码来源:batch_normalization.py

示例11: get_ggamma_and_gbeta

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def get_ggamma_and_gbeta(self, axis, gamma, gy, x_hat, xp):
        tmp = xp.empty(gamma.size * 2, dtype=gamma.dtype)
        gy.sum(axis=axis, out=tmp[:gamma.size], dtype=gamma.dtype)
        (gy * x_hat).sum(axis=axis, out=tmp[gamma.size:], dtype=gamma.dtype)
        if xp is cuda.cupy:
            chainer.cuda.Stream.null.synchronize()
        self.comm._multi_node_mean(None, tmp)
        gbeta = tmp[:gamma.size]
        ggamma = tmp[gamma.size:]
        return gbeta, ggamma 
开发者ID:chainer,项目名称:chainer,代码行数:12,代码来源:batch_normalization.py

示例12: test_cupy

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def test_cupy(self):
        xp = cuda.cupy
        self.summary.add({'cupy': xp.array(3, 'f')})
        self.summary.add({'cupy': xp.array(1, 'f')})
        self.summary.add({'cupy': xp.array(2, 'f')})
        self.summary.add({'cupy': xp.array(3, 'f')})

        self.check(self.summary, {'cupy': (3., 1., 2., 3.)}) 
开发者ID:chainer,项目名称:chainer,代码行数:10,代码来源:test_reporter.py

示例13: tearDown

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def tearDown(self):
        if cuda.available \
                and cuda.cupy.cuda.get_device_id() != self.current_device_id:
            cuda.Device(self.current_device_id).use() 
开发者ID:chainer,项目名称:chainer,代码行数:6,代码来源:test_link.py

示例14: test_copy_and_to_gpu_init

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def test_copy_and_to_gpu_init(self):
        cupy = cuda.cupy
        l0 = self.link
        l1 = l0.copy()
        self.assertIs(l0.x.data, l1.x.data)
        with testing.assert_warns(DeprecationWarning):
            l1.to_gpu()
        self.assertIsNot(l0.x.data, l1.x.data)
        self.assertIsInstance(l0.x.data, numpy.ndarray)
        self.assertIsInstance(l1.x.data, cupy.ndarray) 
开发者ID:chainer,项目名称:chainer,代码行数:12,代码来源:test_link.py

示例15: test_copy_and_to_gpu_uninit

# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import cupy [as 别名]
def test_copy_and_to_gpu_uninit(self):
        cupy = cuda.cupy
        l0 = self.link
        l1 = l0.copy()
        self.assertIs(l0.device.xp, numpy)
        self.assertIsNone(l0.u.data)
        self.assertIsNone(l1.u.data)
        with testing.assert_warns(DeprecationWarning):
            l1.to_gpu()
        self.assertIs(l0.device.xp, numpy)
        self.assertIsNone(l0.u.data)
        l1.u.initialize((2, 3))
        self.assertIsNone(l0.u.data)
        self.assertIsInstance(l1.u.data, cupy.ndarray) 
开发者ID:chainer,项目名称:chainer,代码行数:16,代码来源:test_link.py


注:本文中的chainer.backends.cuda.cupy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。