Python chainer.get_device() Examples

The following are 30 code examples of chainer.get_device(). 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 , or try the search function .
Example #1
Source File: backend.py    From chainer with MIT License 6 votes vote down vote up
def using_device(device_spec):
    """Context manager to apply the thread-local device state.

    Args:
        device_spec (object): Device specifier. See :func:`chainer.get_device`
            for details.

    .. admonition:: Example

        .. testcode::
           :skipif: doctest_helper.skipif_not_enough_cuda_devices(2)

           with chainer.using_device('@cupy:1'):
               a = cupy.empty((3, 2))

           assert a.device.id == 1

    """

    # TODO(niboshi): Set default device (once this concept is introduced in
    # Chainer).
    device = get_device(device_spec)
    return device.create_context() 
Example #2
Source File: chainer_compiler_test.py    From chainer-compiler with MIT License 6 votes vote down vote up
def test_multi_in_outs(device_name, translator):
    device = chainer.get_device(device_name)
    device.use()

    model = MultiInOuts()
    model.to_device(device)

    inputs = [np.array(3, dtype=np.float32), np.array(39, dtype=np.float32)]

    expected = model(*inputs)

    model = chainer_compiler.compile(model, inputs, translator=translator)
    model.to_device(device)

    actual = model(*inputs)

    assert len(expected) == len(actual)
    for e, a in zip(expected, actual):
        e = _array(e)
        a = _array(a)
        assert _get_device(e) == _get_device(a)
        _assert_allclose(e, a) 
Example #3
Source File: chainer_compiler_test.py    From chainer-compiler with MIT License 6 votes vote down vote up
def test_const_mul(device_name, translator):
    device = chainer.get_device(device_name)
    device.use()

    # This checks if the default ChainerX device is set properly by
    # Constant op, whose result will be placed on the default device.
    model = ConstMul()
    model.to_device(device)

    inputs = [np.array(3, dtype=np.float32)]

    expected = model(*inputs)

    model = chainer_compiler.compile(model, inputs, translator=translator)
    model.to_device(device)

    actual = model(*inputs)

    e = _array(expected)
    a = _array(actual)
    assert _get_device(e) == _get_device(a)
    _assert_allclose(e, a) 
Example #4
Source File: chainer_compiler_test.py    From chainer-compiler with MIT License 6 votes vote down vote up
def test_sequence(device_name, translator):
    device = chainer.get_device(device_name)
    device.use()

    model = Sequence()
    model.to_device(device)

    xs = [device.xp.array(i + 1, dtype=np.float32) for i in range(3)]
    expected = model(xs)

    model = chainer_compiler.compile(model, [xs], translator=translator)
    model.to_device(device)

    xs = [device.xp.array(i + 1, dtype=np.float32) for i in range(3)]
    actual = model(xs)

    assert len(expected) == len(actual)
    for e, a in zip(expected, actual):
        e = _array(e)
        a = _array(a)
        assert _get_device(e) == _get_device(a)
        _assert_allclose(e, a) 
Example #5
Source File: test_gradient_check.py    From chainer with MIT License 6 votes vote down vote up
def test_sample_unit_vector(self, backend_config):
        size = self.size
        device = backend_config.device

        # _sample_unit_vector uses the current device
        with chainer.using_device(device):
            y = gradient_check._CheckBackward._sample_unit_vector(
                size, device.xp)
        assert device.is_array_supported(y)
        assert y.shape == (size,)

        y_cpu = chainer.get_device('@numpy').send(y)
        if size >= 1:
            numpy.testing.assert_allclose(numpy.square(y_cpu).sum(), 1.0)
            assert numpy.min(abs(y_cpu)) >= 0.1 / numpy.sqrt(size)
        if size >= 64:
            assert numpy.min(y_cpu) < 0 < numpy.max(y_cpu) 
Example #6
Source File: test_base.py    From chainer-chemistry with MIT License 6 votes vote down vote up
def _test_save_load_pickle(device, tmpdir):
    model = DummyForwardModel(device=device, dummy_str='hoge')

    filepath = os.path.join(str(tmpdir), 'model.pkl')
    model.save_pickle(filepath)
    model_load = DummyForwardModel.load_pickle(filepath, device=device)

    # --- check model class ---
    assert isinstance(model_load, DummyForwardModel)
    # --- check model attribute is same ---
    assert model_load.dummy_str == model.dummy_str
    assert model_load.dummy_str == 'hoge'
    assert model_load.device == chainer.get_device(device)

    # --- check model parameter is same ---
    params = model.namedparams()
    params_load = dict(model_load.namedparams())
    for k, v in params:
        v_load = params_load[k]
        assert cuda.get_device_from_array(v_load.data).id == device
        assert numpy.allclose(cuda.to_cpu(v.data), cuda.to_cpu(v_load.data)) 
Example #7
Source File: device_resident.py    From chainer with MIT License 6 votes vote down vote up
def to_device(
            self,
            device: types.DeviceSpec
    ) -> 'DeviceResident':
        """Copies parameter variables and persistent values to the specified \
device.

        This method does not handle non-registered attributes. If some of such
        attributes must be copied to the device, the link implementation must
        override this method to do so.

        Args:
            device: Target device specifier. See
                :func:`~chainer.get_device` for available values.

        Returns: self

        """
        device = chainer.get_device(device)
        self.__to_device(_ToDeviceVisitor(device))
        return self 
Example #8
Source File: variable.py    From chainer with MIT License 6 votes vote down vote up
def to_chx(self):
        if not chainerx.is_available():
            raise RuntimeError('ChainerX is not available.')

        # Derive the target ChainerX device from the array if it is
        # initialized. Otherwise, from the current initial device.
        if self.array is not None:
            device = backend.get_device_from_array(self.array)
        else:
            device = self._initial_device

        if device.xp is numpy:
            self._initial_device = backend.ChainerxDevice(
                chainerx.get_device('native:0'))
        elif device.xp is cuda.cupy:
            self._initial_device = backend.ChainerxDevice(
                chainerx.get_device('cuda:{}'.format(device.device.id)))

        super(Parameter, self)._to_chx(allow_unchaining=True) 
Example #9
Source File: test_chainer.py    From delira with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_keyword_arguments_different_batchsize(self):
        import numpy as np
        import chainer

        # test batchsize smaller than, equal to and greater than number devices
        for batchsize in [1, 2, 3]:
            with self.subTest(batchsize=batchsize):
                input_kwargs = {
                    "x": np.random.rand(batchsize, 3).astype(np.float32)
                }

                pred = self.model(**input_kwargs)
                self.assertTupleEqual(pred.shape,
                                      (batchsize, 2))
                self.assertEqual(chainer.get_device(pred.device),
                                 chainer.get_device("@numpy"))

    # test with positional arguments 
Example #10
Source File: test_chainer.py    From delira with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_positional_arguments(self):
        import numpy as np
        import chainer

        # test batchsize smaller than, equal to and greater than number devices
        for batchsize in [1, 2, 3]:
            with self.subTest(batchsize=batchsize):
                input_args = [
                    np.random.rand(batchsize, 3).astype(np.float32)
                ]

                pred = self.model(*input_args)
                self.assertTupleEqual(pred.shape,
                                      (batchsize, 2))

                self.assertEqual(chainer.get_device(pred.device),
                                 chainer.get_device("@numpy")) 
Example #11
Source File: test_function_node.py    From chainer with MIT License 6 votes vote down vote up
def test_backward_default_device(self):
        # Default device in backward should be determined by arrays,
        # otherwise, creation routines in backward do not create new arrays
        # on the proper device.

        device = chainerx.get_device('cuda:0')
        shape = (2, 3)
        dtype = numpy.float32
        x1 = chainerx.full(shape, 3, dtype, device=device)
        x2 = chainerx.full(shape, 5, dtype, device=device).require_grad()

        backward_call_new_array = []

        def backward_call_callback(call_arg):
            backward_call_new_array.append(chainerx.empty(shape, dtype))

        with chainerx.using_device('native:0'):
            # forward
            func = self.SimpleFunctionNode(backward_call_callback)
            y1, y2 = func.apply((x1, x2))

            # backward
            y2.backward()

        assert backward_call_new_array[0].device is device 
Example #12
Source File: base_graph_dataset.py    From chainer-chemistry with MIT License 6 votes vote down vote up
def converter(self, batch, device=None):
        """Converter

        add `self.batch`, which represents the index of the graph each node
        belongs to.

        Args:
            batch (list[BaseGraphData]): list of graph data
            device (int, optional): specifier of device. Defaults to None.

        Returns:
            self sent to `device`
        """
        data = super(SparseGraphDataset, self).converter(batch, device=device)
        if not isinstance(device, Device):
            device = chainer.get_device(device)
        data.batch = numpy.concatenate([
            numpy.full((data.x.shape[0]), i, dtype=numpy.int)
            for i, data in enumerate(batch)
        ])
        data.batch = device.send(data.batch)
        return data

    # for experiment
    # use converter for the normal use 
Example #13
Source File: base_graph_dataset.py    From chainer-chemistry with MIT License 6 votes vote down vote up
def converter(self, batch, device=None):
        """Converter

        Args:
            batch (list[BaseGraphData]): list of graph data
            device (int, optional): specifier of device. Defaults to None.

        Returns:
            self sent to `device`
        """
        if not isinstance(device, Device):
            device = chainer.get_device(device)
        batch = [method(name, batch, device=device) for name, method in
                 zip(self._feature_entries, self._feature_batch_method)]
        data = BaseGraphData(
            **{key: value for key, value in zip(self._feature_entries, batch)})
        return data 
Example #14
Source File: predict_chainer.py    From treasure-boxes with MIT License 5 votes vote down vote up
def predict_chainer(database, input_table, output_table, device_num=-1):
    device = chainer.get_device(device_num)
    device.use()

    model_setup = download_model()
    logger.info(f"model setup path: {model_setup}")
    model, vocab, setup = setup_model(device, model_setup)
    run_batch(
        database, input_table, output_table, device, model, vocab, setup, batchsize=64
    ) 
Example #15
Source File: base.py    From chainer-chemistry with MIT License 5 votes vote down vote up
def update_device(self, device=-1):
        if not isinstance(device, chainer._backend.Device):
            device = chainer.get_device(device)  # type: chainerx.Device

        if self.device != device:
            device.use()
            # reset current state
            self.to_cpu()
            # update the model to specified device
            self.to_device(device) 
Example #16
Source File: test_computational_cost_hook.py    From chainer_computational_cost with MIT License 5 votes vote down vote up
def test_chainerx():
    x = np.random.randn(1, 3, 32, 32).astype(np.float32)

    # Get non-ChainerX based version
    net = SimpleConvNet()
    with chainer.using_config('train', False):
        with ComputationalCostHook() as cost:
            net(x)
            correct_report = cost.layer_report

    # Get ChainerX based report
    device = chainer.get_device('-1')
    net = SimpleConvNet()
    net.to_device(device)
    device.use()

    with chainer.using_config('train', False):
        with ComputationalCostHook() as cost:
            net(x)
            chainerx_report = cost.layer_report

    # Compare
    assert sorted(correct_report.keys()) == sorted(chainerx_report.keys())
    for layer, correct in correct_report.items():
        if layer == 'total':
            assert chainerx_report['total'] == correct
        else:
            for key in ReportColumns.ALL:
                assert chainerx_report[layer][key] == correct[key] 
Example #17
Source File: test_black_out.py    From chainer with MIT License 5 votes vote down vote up
def test_forward_chainerx_native(self):
        device = chainer.get_device('native:0')
        self.link.to_device(device)
        self.check_forward(device.send(self.x), device.send(self.t)) 
Example #18
Source File: test_train_utils.py    From chainer-chemistry with MIT License 5 votes vote down vote up
def test_run_train_chainerx_cuda0(model, train_data, valid_data):
    device = chainer.get_device('cuda:0')
    model.to_device(device)
    run_train(model, train_data, valid=valid_data, epoch=1, batch_size=8,
              device=device) 
Example #19
Source File: test_train_utils.py    From chainer-chemistry with MIT License 5 votes vote down vote up
def test_run_train_chainerx_native(model, train_data, valid_data):
    device = chainer.get_device('native')
    model.to_device(device)
    run_train(model, train_data, valid=valid_data, epoch=1, batch_size=8,
              device=device) 
Example #20
Source File: predict_own_dataset.py    From chainer-chemistry with MIT License 5 votes vote down vote up
def main():
    # Parse the arguments.
    args = parse_arguments()

    if args.label:
        labels = args.label
    else:
        raise ValueError('No target label was specified.')

    # Dataset preparation.
    def postprocess_label(label_list):
        return numpy.asarray(label_list, dtype=numpy.float32)

    print('Preprocessing dataset...')
    preprocessor = preprocess_method_dict[args.method]()
    parser = CSVFileParser(preprocessor, postprocess_label=postprocess_label,
                           labels=labels, smiles_col='SMILES')
    dataset = parser.parse(args.datafile)['dataset']

    test = dataset

    print('Predicting...')
    # Set up the regressor.
    device = chainer.get_device(args.device)
    model_path = os.path.join(args.in_dir, args.model_filename)
    regressor = Regressor.load_pickle(model_path, device=device)

    # Perform the prediction.
    print('Evaluating...')
    converter = converter_method_dict[args.method]
    test_iterator = SerialIterator(test, 16, repeat=False, shuffle=False)
    eval_result = Evaluator(test_iterator, regressor, converter=converter,
                            device=device)()
    print('Evaluation result: ', eval_result)

    save_json(os.path.join(args.in_dir, 'eval_result.json'), eval_result) 
Example #21
Source File: train_mnist.py    From models with MIT License 5 votes vote down vote up
def evaluate_optimizer(args, train, optimizer):
    if isinstance(optimizer, nets.optnets.LSTMOptNet):
        optimizer.release_all()
    device = chainer.get_device(args.gpu)
    device.use()
    n_evaluation_runs = args.evaluation_runs  # 5?
    max_iter_of_meta = args.iter_meta  # 100

    all_losses = []
    for _ in range(n_evaluation_runs):
        train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
        losses = []
        model = nets.images.MLPforMNIST()
        model.to_device(device)
        optimizer.setup(model)

        iteration = 0
        while iteration < max_iter_of_meta:
            # routine
            iteration += 1
            batch = train_iter.next()
            batch = convert.concat_examples(batch, device=device)
            x, t = batch
            with chainer.using_config('train', True):
                loss, acc = model(x, t, get_accuracy=True)
            model.cleargrads()
            loss.backward(retain_grad=False)  # False
            optimizer.update(train_optnet=False)
            losses.append(loss.item())  # log
        all_losses.append(losses)
    # TODO: use losses in only last half iterations?
    last10_mean = np.mean([losses[-10:] for losses in all_losses])
    return last10_mean, all_losses 
Example #22
Source File: device_resident.py    From chainer with MIT License 5 votes vote down vote up
def to_intel64(self) -> 'DeviceResident':
        """Copies parameter variables and persistent values to CPU.

         .. deprecated:: v7.0.0
            Use :meth:`to_device` instead.

        """
        intel64.check_ideep_available()
        visitor = _ToDeviceVisitor(
            chainer.get_device(intel64.Intel64Device()),
            entry_method_info=('to_intel64', {}),
            starting_device_resident=self)
        self.__to_device(visitor)
        return self 
Example #23
Source File: convert.py    From chainer with MIT License 5 votes vote down vote up
def _get_device(device_spec):
    # Converts device specificer to a chainer.Device instance.
    # Additionally to chainer.get_device, this function supports None
    if device_spec is None:
        return None
    return backend.get_device(device_spec)


# TODO(hvy): Write unit tests where batch elements contain Python lists. 
Example #24
Source File: test_cuda.py    From chainer with MIT License 5 votes vote down vote up
def test_from_device_id(self):
        device = backend.GpuDevice.from_device_id(self.device_id)
        assert isinstance(device, backend.GpuDevice)
        device_spec = '@cupy:{}'.format(self.device_id)
        assert device == chainer.get_device(device_spec)
        assert device.device.id == int(self.device_id) 
Example #25
Source File: test_cuda.py    From chainer with MIT License 5 votes vote down vote up
def test_get_device_for_builtin_int(self):
        # builtins.int is from future package and it is different
        # from builtin int/long on Python 2.
        with testing.assert_warns(DeprecationWarning):
            device = cuda.get_device(builtins.int(0))
        assert device == cuda.Device(0) 
Example #26
Source File: test_cuda.py    From chainer with MIT License 5 votes vote down vote up
def test_get_device_for_int(self):
        with testing.assert_warns(DeprecationWarning):
            device = cuda.get_device(0)
        assert device == cuda.Device(0) 
Example #27
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 #28
Source File: test_chainerx.py    From chainer with MIT License 5 votes vote down vote up
def test_use(self, backend_config):
        device = chainer.get_device(backend_config.chainerx_device)
        with chainerx.using_device('native:1'):
            device.use()
            assert device.device is chainerx.get_default_device() 
Example #29
Source File: test_chainerx.py    From chainer with MIT License 5 votes vote down vote up
def test_from_fallback_device(self, backend_config):
        # Preparation: it depends on ChainerxDevice.fallback_device
        tmp_device = backend.ChainerxDevice(
            chainerx.get_device(backend_config.chainerx_device))
        fallback_device = tmp_device.fallback_device

        # Test
        device = backend.ChainerxDevice.from_fallback_device(fallback_device)
        self.check_device(device, backend_config)
        assert device.fallback_device == fallback_device 
Example #30
Source File: test_batch_normalization.py    From chainer with MIT License 5 votes vote down vote up
def test_param_layout_to_device(self, backend_config):
        with chainer.using_config('compute_mode', 'cudnn_fast'):
            link = self.create_link()
        assert link.gamma.device == chainer.get_device('@numpy')
        assert link.beta.device == chainer.get_device('@numpy')
        link.to_device(backend_config.device)
        assert link.gamma.device == backend_config.device
        assert link.beta.device == backend_config.device
        assert link.gamma.layout is None
        assert link.beta.layout is None