Python chainer.backends.cuda.get_device_from_array() Examples

The following are 12 code examples of chainer.backends.cuda.get_device_from_array(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer.backends.cuda , or try the search function .
Example #1
Source File: theano_function.py    From chainer with MIT License 6 votes vote down vote up
def backward(self, inputs, grads):
        gpu = backend.get_array_module(*inputs) is cuda.cupy

        # TODO(unno): We can remove redundant gpu-cpu copy using
        # theano.sandbox.cuda.basic_ops.gpu_from_host
        args = [cuda.to_cpu(x) for x in inputs + grads]

        outputs = self.backward_func(*args)
        assert len(outputs) == len(inputs)

        if gpu:
            # TODO(unno): We can remove redundant gpu-cpu copy using
            # theano.sandbox.cuda.CudaNdarray.gpudata
            device = cuda.get_device_from_array(inputs)
            outputs = [cuda.to_gpu(x, device) for x in outputs]

        results = []
        for o, i in zip(outputs, inputs):
            if i.dtype.kind != 'f':
                o = None
            elif o.dtype != i.dtype:
                o = o.astype(i.dtype)
            results.append(o)
        return tuple(results) 
Example #2
Source File: test_cuda.py    From chainer with MIT License 5 votes vote down vote up
def test_get_device_from_array_for_numpy_int(self):
        assert cuda.get_device_from_array(numpy.int64(0)) is cuda.DummyDevice 
Example #3
Source File: test_cuda.py    From chainer with MIT License 5 votes vote down vote up
def test_get_device_for_empty_array(self):
        x = cuda.get_device_from_array(cuda.cupy.array([]).reshape((0, 10)))
        # TODO(okuta): Only check `assert x == cuda.Device(0)`
        #              when cupy/cupy#946 is merged
        assert x == cuda.Device(0) or x == cuda.DummyDevice 
Example #4
Source File: test_cuda.py    From chainer with MIT License 5 votes vote down vote up
def test_get_device_warning(self):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            cuda.get_device(cuda.cupy.array([1]))

        assert len(w) == 1
        assert w[0].category is DeprecationWarning
        assert ('get_device is deprecated. Please use get_device_from_id'
                ' or get_device_from_array instead.' in str(w[0].message)) 
Example #5
Source File: test_cuda.py    From chainer with MIT License 5 votes vote down vote up
def test_get_device_from_array(self):
        arr = cuda.cupy.array([0])
        assert cuda.get_device_from_array(arr) == cuda.Device(0) 
Example #6
Source File: test_cuda.py    From chainer with MIT License 5 votes vote down vote up
def test_get_device_from_array(self, backend_config):
        with cuda.Device(backend_config.cuda_device):
            arr = cuda.ndarray((), numpy.float32)
        # Test precondition check
        assert arr.device.id == backend_config.cuda_device

        expected_device = backend_config.device

        device = backend.GpuDevice.from_array(arr)
        self.check_device(device, backend_config)
        assert device == expected_device

        device = backend.get_device_from_array(arr)
        self.check_device(device, backend_config)
        assert device == expected_device 
Example #7
Source File: parameter.py    From chainer with MIT License 5 votes vote down vote up
def __init__(self, array):
        super(Parameter, self).__init__()
        self.add_param('W', array.shape, dtype=array.dtype)
        self.W.array = array
        if isinstance(array, cuda.ndarray):
            self.to_gpu(cuda.get_device_from_array(array)) 
Example #8
Source File: theano_function.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs):
        gpu = backend.get_array_module(*inputs) is cuda.cupy
        inputs = [cuda.to_cpu(x) for x in inputs]

        outputs = self.forward_func(*inputs)

        if gpu:
            # TODO(unno): We can remove redundant gpu-cpu copy using
            # theano.sandbox.cuda.CudaNdarray.gpudata
            device = cuda.get_device_from_array(inputs)
            outputs = [cuda.to_gpu(x, device) for x in outputs]

        return tuple(outputs) 
Example #9
Source File: eval.py    From models with MIT License 5 votes vote down vote up
def concat_arrays(arrays):
    # Convert `arrays` to numpy.ndarray or cupy.ndarray
    xp = cuda.get_array_module(arrays[0])
    with cuda.get_device_from_array(arrays[0]):
        return xp.concatenate(arrays) 
Example #10
Source File: gradient_scaling.py    From chainercv with MIT License 5 votes vote down vote up
def __call__(self, rule, param):
        g = param.grad
        with cuda.get_device_from_array(g):
            g *= self.rate 
Example #11
Source File: radam.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def init_state(self, param):
        xp = backend.get_array_module(param.data)
        with cuda.get_device_from_array(param.data):
            self.state['m'] = xp.zeros_like(param.data)
            self.state['v'] = xp.zeros_like(param.data)

        # For iDeep
        if isinstance(param.data, intel64.mdarray):
            self.state['m'] = intel64.ideep.array(
                self.state['m'], itype=intel64.ideep.wgt_array)
            self.state['v'] = intel64.ideep.array(
                self.state['v'], itype=intel64.ideep.wgt_array) 
Example #12
Source File: _utility.py    From pytorch-sso with MIT License 5 votes vote down vote up
def _check_array(array, name):
    xp = cuda.get_array_module(array)
    with cuda.get_device_from_array(array):
        if not array.dtype == xp.float32:
            warnings.warn('non FP32 dtype detected in {}'.format(name))
            array = array.astype(xp.float32)
        if not (array.flags.c_contiguous or array.flags.f_contiguous):
            warnings.warn('non contiguous array detected in {}'.format(name))
            array = xp.ascontiguousarray(array)
    return array