-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
_cpu.py
63 lines (48 loc) · 1.72 KB
/
_cpu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy
from chainer import _backend
# TODO(kmaehashi): `from chainer.backends import cuda` causes circular imports.
# Surprisingly, `import chianer.backends` works as a workaround to avoid, but
# we should fix circular dependencies themselves around `chainer.backends.*`.
import chainer.backends
import chainerx
class CpuDevice(_backend.Device):
"""Device for CPU (NumPy) backend"""
@property
def xp(self):
return numpy
@property
def supported_array_types(self):
return (numpy.ndarray,)
@staticmethod
def from_array(array):
if isinstance(array, numpy.ndarray):
return CpuDevice()
return None
def __eq__(self, other):
return isinstance(other, CpuDevice)
def __repr__(self):
return '<{} (numpy)>'.format(self.__class__.__name__)
def __str__(self):
return '@numpy'
def send_array(self, array):
return _array_to_cpu(array)
def _to_cpu(array):
"""Converts an array or arrays to NumPy."""
return _backend._convert_arrays(array, _array_to_cpu)
def _array_to_cpu(array):
if array is None:
return None
if isinstance(array, numpy.ndarray):
return array
if isinstance(array, chainer.backends.intel64.mdarray):
return numpy.asarray(array)
if isinstance(array, chainerx.ndarray):
return chainerx.to_numpy(array, copy=False)
if isinstance(array, chainer.backends.cuda.ndarray):
with chainer.backends.cuda.get_device_from_array(array):
return array.get()
if numpy.isscalar(array):
return numpy.asarray(array)
raise TypeError(
'Array cannot be converted into an numpy.ndarray'
'\nActual type: {0}.'.format(type(array)))