Skip to content

Commit

Permalink
Merge pull request #4577 from kmaehashi/fix-variable-to-intel64
Browse files Browse the repository at this point in the history
Fix to_intel64 to check if it is suitable for iDeep
  • Loading branch information
niboshi committed Apr 10, 2018
2 parents 5c106ab + 4b4cc6a commit d61deb2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
6 changes: 4 additions & 2 deletions chainer/link.py
Expand Up @@ -389,9 +389,11 @@ def to_intel64(self):
value = d[name]
if isinstance(value, cuda.ndarray):
value = value.get() # to numpy.ndarray
if isinstance(value, numpy.ndarray):
d[name] = intel64.ideep.array(
if (isinstance(value, numpy.ndarray) and
intel64.inputs_all_ready((value,))):
value = intel64.ideep.array(
value, itype=intel64.ideep.wgt_array)
d[name] = value
self._cpu = True
self._device_id = None
return self
Expand Down
20 changes: 10 additions & 10 deletions chainer/variable.py
Expand Up @@ -776,16 +776,16 @@ def to_intel64(self):
intel64.check_ideep_available()
data = self.data
if data is not None:
if isinstance(data, numpy.ndarray):
# numpy.ndarray to ideep
self._data = [
intel64.ideep.array(
data, itype=intel64.ideep.wgt_array)]
elif isinstance(data, cuda.ndarray):
# cupy.ndarray to ideep
self._data = [
intel64.ideep.array(
data.get(), itype=intel64.ideep.wgt_array)]
if isinstance(data, cuda.ndarray):
# cupy.ndarray to numpy.ndarray
data = data.get()
if (isinstance(data, numpy.ndarray) and
intel64.inputs_all_ready((data,))):
# numpy.ndarray to ideep.mdarray
data = intel64.ideep.array(
data, itype=intel64.ideep.wgt_array)
self._data = [data]

if self._grad_var is not None:
self._grad_var.to_intel64()
# ensure that the node tracks the device migration
Expand Down
19 changes: 18 additions & 1 deletion tests/chainer_tests/test_link.py
Expand Up @@ -1152,7 +1152,7 @@ class TestIntel64(unittest.TestCase):

def setUp(self):
self.link = chainer.Link()
shape = (2,)
shape = (2, 2)
dtype = numpy.float32
y_array = numpy.random.rand(*shape).astype(dtype)
pa_array = numpy.random.rand(*shape).astype(dtype)
Expand Down Expand Up @@ -1284,5 +1284,22 @@ def test_intel64_to_cpu(self):
# Persistent scalar
assert link.ps == self.ps_scalar

def test_cpu_to_intel64_unsupported(self):
# Test for persistents that cannot be transferred to iDeep.
with self.link.init_scope():
self.link.no_ideep = numpy.ones((2, 2, 2), numpy.float32)
self.link.register_persistent('no_ideep')
self.link.to_intel64()
assert isinstance(self.link.no_ideep, numpy.ndarray)

@attr.gpu
def test_gpu_to_intel64_unsupported(self):
# Test for persistents that cannot be transferred to iDeep.
with self.link.init_scope():
self.link.no_ideep = cuda.cupy.ones((2, 2, 2), numpy.float32)
self.link.register_persistent('no_ideep')
self.link.to_intel64()
assert isinstance(self.link.no_ideep, numpy.ndarray)


testing.run_module(__name__, __file__)
32 changes: 30 additions & 2 deletions tests/chainer_tests/test_variable.py
Expand Up @@ -1714,8 +1714,7 @@ def test_loss_scale_gpu(self):


@testing.parameterize(*testing.product({
# TODO(niboshi): shape () is not supported
'shape': [(0,), (3, 2)],
'shape': [(3, 2), (2, 3, 4, 3)],
'dtype': [
np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32,
np.uint64, np.float16, np.float32, np.float64],
Expand Down Expand Up @@ -1791,6 +1790,35 @@ def test_intel64_to_cpu(self):
self._check_variable_shape_and_dtype(x)


@testing.parameterize(*testing.product({
'shape': [(), (0,), (3, 2, 3), (4, 4, 3, 2, 3)],
'dtype': [
np.int8, np.int16, np.int32, np.int64,
np.uint8, np.uint16, np.uint32, np.uint64,
np.float16, np.float32, np.float64,
],
}))
@attr.ideep
class TestIntel64Unsupported(unittest.TestCase):

"""Tests for arrays that should not be converted to iDeep array."""

def setUp(self):
self.x_data = np.random.uniform(-1, 1, self.shape).astype(self.dtype)

def test_cpu_to_intel64(self):
x = chainer.Variable(self.x_data)
x.to_intel64()
assert isinstance(x.data, np.ndarray)

@attr.gpu
def test_gpu_to_intel64(self):
x = chainer.Variable(self.x_data)
x.to_gpu()
x.to_intel64()
assert isinstance(x.data, np.ndarray)


@testing.parameterize(*testing.product({
'shape': [(3,), (3, 2), (3, 2, 2), (3, 2, 2, 3)],
'dtype': [
Expand Down

0 comments on commit d61deb2

Please sign in to comment.