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
def copy(x, dst): """Copies the input variable onto the specified device. If the input ``x`` already resides on the device specified by ``dst``, no copy will actually take place and the returned variable will hold a view of the input. In other cases, the input will be copied to ``dst``. When ``dst == -1``, the array is copied to the host memory. This function supports copies from host to host, from host to device, from device to device and from device to host. Args: x (:class:`~chainer.Variable` or :ref:`ndarray`): Variable to be copied. dst: Target device specifier. Returns: ~chainer.Variable: Output variable. .. admonition:: Example >>> import chainer.backends.cuda as cuda >>> x_arr = np.random.uniform(-1, 1, (5, 10)) >>> x = chainer.Variable(x_arr) >>> x.device <CpuDevice (numpy)> >>> y = F.copy(x, '@cupy:0') # from CPU (NumPy) to GPU 0 (CuPy) >>> y.device <GpuDevice (cupy):0> .. note:: Copies between non-ChainerX devices and ChainerX devices are not supported. """ # For backward compatibility if dst is cuda.DummyDevice: dst = chainer.get_device('@numpy') in_device = backend.get_device_from_array( x.array if isinstance(x, chainer.Variable) else x) out_device = chainer.get_device(dst) is_chainerx = in_device.xp is chainerx if is_chainerx != (out_device.xp is chainerx): raise RuntimeError( 'F.copy does not support copies between non-ChainerX devices and ' 'ChainerX devices.\n' 'From: {}\n' 'To: {}'.format(in_device, out_device)) y, = Copy(in_device, out_device).apply((x,)) return y
def device(self): if self._device is None: if self.use_cuda: device = chainer.get_device( chainer.backends.cuda.Device(self.cuda_device)) elif self.use_chainerx: device = chainer.get_device(self.chainerx_device) elif self.use_ideep != 'never': device = backend.Intel64Device() else: device = backend.CpuDevice() self._device = device return self._device
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"))
def parse_device(args): gpu = None if args.gpu is not None: gpu = args.gpu elif re.match(r'(-|\+|)[0-9]+$', args.device): gpu = int(args.device) if gpu is not None: if gpu < 0: return chainer.get_device(numpy) else: import cupy return chainer.get_device((cupy, gpu)) return chainer.get_device(args.device)
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"))
def test(): model = SimpleNetChild() dataset = [((numpy.ones((2, 5, 5)) * i).astype(numpy.float32), numpy.int32(0)) for i in range(100)] batch_size = 5 devices = tuple([chainer.get_device(d) for d in sys.argv[1].split(',')]) iters = [chainer.iterators.SerialIterator(i, batch_size) for i in chainer.datasets.split_dataset_n_random( dataset, len(devices))] optimizer = chainer.optimizers.SGD(lr=1.0) optimizer.setup(model) # Initialize CUDA context. cuda.cupy.cuda.runtime.runtimeGetVersion() try: mpu.MultiprocessParallelUpdater(iters, optimizer, devices=devices) except RuntimeError as e: if sys.argv[2] == 'fork': assert 'CUDA context' in str(e) return updater = mpu.MultiprocessParallelUpdater( iters, optimizer, devices=devices) trainer = chainer.training.Trainer(updater, (1, 'epoch'), '/tmp') trainer.run() assert sys.argv[2] != 'fork'
def copy(array, out=None, out_device=None, stream=None): """Copies a :class:`cupy.ndarray` object using the default stream. This function can copy the device array to the destination array on another device. Args: array (cupy.ndarray): Array to be copied. out (cupy.ndarray): Destination array. If it is not ``None``, then ``out_device`` argument is ignored. out_device: Destination device specifier. Actual device object is obtained by passing this value to :func:`get_device`. stream (cupy.cuda.Stream): CUDA stream. Returns: cupy.ndarray: Copied array. If ``out`` is not specified, then the array is allocated on the device specified by ``out_device`` argument. """ # TODO(niboshi): Update docstring not to mention deprecated `get_device` check_cuda_available() assert stream is None # TODO(beam2d): FIX IT if out is None: if out_device is None: out_device = array with chainer.get_device(out_device): out = cupy.empty_like(array) with get_device_from_array(array): cupy.copyto(out, array) return out
def main(): device = chainer.get_device(DEVICE_ID) device.use() _, test = datasets.get_cifar10() test_data, test_label = dataset.concat_examples(test[:1000]) results = [] for pruning_rate in range(0, 100, 1): model = load("initial.model") model.to_device(device) masks = util.create_masks("initial.model", "final.model", pruning_rates=pruning_rate) model.prune_connection(masks) results.append( [pruning_rate, get_accuracy(model, test_data, test_label)]) do_cr = (pruning_rate + 1) % 10 print(".", end="" if do_cr else "\n") df = pd.DataFrame(results, columns=['pruning_rate', 'accuracy']) df.to_csv(os.path.join(REPORT_DIR, 'pruning_accuracy.csv')) df.set_index('pruning_rate')['accuracy'].plot() plt.savefig(os.path.join(REPORT_DIR, 'pruning_accuracy.png')) plt.close('all')
def translate(self, xs, max_length=100): batch = len(xs) with chainer.no_backprop_mode(), chainer.using_config('train', False): xs = [x[::-1] for x in xs] exs = sequence_embed(self.embed_x, xs) h, c, _ = self.encoder(None, None, exs) ys = self.xp.full(batch, EOS, numpy.int32) result = [] for i in range(max_length): eys = self.embed_y(ys) eys = F.split_axis(eys, batch, 0) h, c, ys = self.decoder(h, c, eys) cys = F.concat(ys, axis=0) wy = self.W(cys) ys = self.xp.argmax(wy.array, axis=1).astype(numpy.int32) result.append(ys) # Using `xp.concatenate(...)` instead of `xp.stack(result)` here to # support NumPy 1.9. result = chainer.get_device(numpy).send( self.xp.concatenate([x[None, :] for x in result]).T) # Remove EOS taggs outs = [] for y in result: inds = numpy.argwhere(y == EOS) if len(inds) > 0: y = y[:inds[0, 0]] outs.append(y) return outs
def evaluate_image( self, image: numpy.ndarray, return_boxes: bool = False) -> Union[bool, Tuple[bool, list]]: if self.needs_patches: patches, bboxes = self.prediction_helper.create_sliding_window( image) else: patches = image[numpy.newaxis, ...] network = self.network device = chainer.get_device(network.device) xp = numpy with chainer.using_device(device), chainer.configuration.using_config( 'train', False): predicted_patches = [] for patch in patches: batch = [{'image': patch}] batch = concat_examples(batch, device) xp = get_array_module(batch['image']) predictions = network(**batch) predicted_patches.append(xp.argmax(predictions.array, axis=1)) predicted_patches = xp.stack(predicted_patches, axis=0) contains_handwriting = chainer.backends.cuda.to_cpu( predicted_patches == 1) if return_boxes: assert self.needs_patches, "Can not return boxes if we do not need patches" return contains_handwriting, bboxes else: return contains_handwriting.any()
def test(): model = chainer.Link() dataset = [((numpy.ones( (2, 5, 5)) * i).astype(numpy.float32), numpy.int32(0)) for i in range(100)] batch_size = 5 devices = (chainer.get_device('@cupy:0'), ) iters = [ chainer.iterators.SerialIterator(i, batch_size) for i in chainer.datasets.split_dataset_n_random( dataset, len(devices)) ] optimizer = chainer.optimizers.SGD(lr=1.0) optimizer.setup(model) # Initialize CUDA context. cuda.cupy.cuda.runtime.runtimeGetVersion() try: mpu.MultiprocessParallelUpdater(iters, optimizer, devices=devices) except RuntimeError as e: assert 'CUDA context' in str(e) return assert False
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
def test_sequence_grad(device_name, translator): device = chainer.get_device(device_name) device.use() seq_length = 4 batch_size = 2 n_units = 3 model = SequenceGrad(n_units) model.to_device(device) xs = aranges(device.xp, seq_length, batch_size, n_units) xs = [device.xp.array(x) for x in xs] expected_ys, expected_grads = _run_fwd_bwd(model, [xs]) model = chainer_compiler.compile(model, [xs], translator=translator) model.to_device(device) actual_ys, actual_grads = _run_fwd_bwd(model, [xs]) assert len(expected_ys) == len(actual_ys) for e, a in zip(expected_ys, actual_ys): e = _array(e) a = _array(a) assert _get_device(e) == _get_device(a) _assert_allclose(e, a, rtol=1e-4) assert len(expected_grads) == len(actual_grads) for (e_name, e_grad), (a_name, a_grad) in zip(expected_grads, actual_grads): assert e_name == a_name assert e_grad is not None, e_name assert a_grad is not None, a_name _assert_allclose(e_grad, a_grad, rtol=1e-4)
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]
def test_to_cpu(self): link = self.create_link() link.to_device((cuda.cupy, 0)) self.assertEqual( link.sampler.device, chainer.get_device((cuda.cupy, 0))) link.to_device(numpy) self.assertEqual(link.sampler.device, backend.CpuDevice())
def translate(self, xs, max_length=100): batch = len(xs) with chainer.no_backprop_mode(), chainer.using_config('train', False): xs = [x[::-1] for x in xs] exs = sequence_embed(self.embed_x, xs) h, c, _ = self.encoder(None, None, exs) ys = self.xp.full(batch, EOS, numpy.int32) result = [] for i in range(max_length): eys = self.embed_y(ys) eys = F.split_axis(eys, batch, 0) h, c, ys = self.decoder(h, c, eys) cys = F.concat(ys, axis=0) wy = self.W(cys) ys = self.xp.argmax(wy.array, axis=1).astype(numpy.int32) result.append(ys) # Using `xp.concatenate(...)` instead of `xp.stack(result)` here to # support NumPy 1.9. result = chainer.get_device('@numpy').send( self.xp.concatenate([x[None, :] for x in result]).T) # Remove EOS taggs outs = [] for y in result: inds = numpy.argwhere(y == EOS) if len(inds) > 0: y = y[:inds[0, 0]] outs.append(y) return outs
def __init__(self,data,batch_size=20,bproplen = 35, epoch = 39,device = -1, grad_clip = 5, out = lm.lm_dir + 'rnn/',resume = '',test=False,unit = 650, model_filename = 'model.npz'): ''' class to train rnn model batch_size number of examples in a mini batch bproplen number or words in each minibatch epoch number of sweeps over the dataset device device specifier. Either chainerX device specifier or an integer. If non-negative integer, CuPy array with specified device id are used. If negative integer NumPy arrays are used gradclip gradient norm threshold to clip out directory to output the result resume resume training from snap shot unit number of LSTM units in each layer model_filename model filename to serialize ''' self.data = data self.batch_size = batch_size self.bproplen = bproplen self.epoch = epoch self.device = chainer.get_device(device) self.device.use() self.grad_clip = grad_clip self.out = out self.resume = resume self.test = test self.unit = unit self.model_filename = model_filename self.nvocab = self.data.nvocab if self.data.nvocab else max(self.data.train) + 1
def translate(self, source_seq, max_length=100): batch = len(source_seq) with chainer.no_backprop_mode(), chainer.using_config('train', False): source_seq_emb = sequence_embed(self.embed_source, source_seq) h, c, _ = self.lstm(None, None, source_seq_emb) out_seq = self.xp.full(batch, EOS, numpy.int32) result = [] for i in range(max_length): out_seq_emb = self.embed_target(out_seq) out_seq_emb = F.split_axis(out_seq_emb, batch, 0) h, c, out_seq = self.lstm(h, c, out_seq_emb) hid_seq = F.concat(out_seq, axis=0) tmp = self.lin(hid_seq) out_seq = self.xp.argmax(tmp.array, axis=1).astype(numpy.int32) result.append(out_seq) # Using `xp.concatenate(...)` instead of `xp.stack(result)` here to # support NumPy 1.9. result = chainer.get_device('@numpy').send( self.xp.concatenate([x[None, :] for x in result]).T) # Remove EOS taggs outs = [] for y in result: inds = numpy.argwhere(y == EOS) if len(inds) > 0: y = y[:inds[0, 0]] outs.append(y) return outs
def __init__(self, prediction_config_path="prediction_config.json", gpu=-1): self.gpu = gpu with open(prediction_config_path) as prediction_config_file: prediction_config = json.load(prediction_config_file) classes = sorted(prediction_config["classes"]) long_class_label_dict = { "alpha_num": "Alphanumeric", "alphanum": "Alphanumeric", "date": "Date", "num": "Number", "plz": "Zip Code", "text": "Word" } self.idx_to_label_map = {i: long_class_label_dict[label] for i, label in enumerate(classes)} self.input_image_size = prediction_config["input_image_size"] self.base_model = PooledResNet(prediction_config["resnet_size"]) self.model = CrossEntropyClassifier(self.base_model, len(classes)) with numpy.load(prediction_config["model_path"]) as f: chainer.serializers.NpzDeserializer(f, strict=True).load(self.model) if int(self.gpu) >= 0: with chainer.using_device(chainer.get_device(self.gpu)): self.base_model.to_device(self.gpu) self.model.to_device(self.gpu)
def test_param_layout_to_device(self, backend_config): with chainer.using_config('compute_mode', 'cudnn_fast'): link = self.create_link() assert link.W.device == chainer.get_device('@numpy') link.to_device(backend_config.device) assert link.W.device == backend_config.device assert link.W.layout == memory_layouts.CUDNN_CHANNEL_LAST_W
def main(): # Parse the arguments. args = parse_arguments() if args.label: labels = args.label class_num = len(labels) if isinstance(labels, list) else 1 else: raise ValueError('No target label was specified.') # Dataset preparation. Postprocessing is required for the regression task. def postprocess_label(label_list): return numpy.asarray(label_list, dtype=numpy.float32) # Apply a preprocessor to the dataset. 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'] # Scale the label values, if necessary. if args.scale == 'standardize': scaler = StandardScaler() scaler.fit(dataset.get_datasets()[-1]) else: scaler = None # Split the dataset into training and validation. train_data_size = int(len(dataset) * args.train_data_ratio) train, _ = split_dataset_random(dataset, train_data_size, args.seed) # Set up the predictor. predictor = set_up_predictor( args.method, args.unit_num, args.conv_layers, class_num, label_scaler=scaler) # Set up the regressor. device = chainer.get_device(args.device) metrics_fun = {'mae': F.mean_absolute_error, 'rmse': rmse} regressor = Regressor(predictor, lossfun=F.mean_squared_error, metrics_fun=metrics_fun, device=device) print('Training...') run_train(regressor, train, valid=None, batch_size=args.batchsize, epoch=args.epoch, out=args.out, extensions_list=None, device=device, converter=concat_mols, resume_path=None) # Save the regressor's parameters. model_path = os.path.join(args.out, args.model_filename) print('Saving the trained model to {}...'.format(model_path)) # TODO(nakago): ChainerX array cannot be sent to numpy array when internal # state has gradients. if hasattr(regressor.predictor.graph_conv, 'reset_state'): regressor.predictor.graph_conv.reset_state() regressor.save_pickle(model_path, protocol=args.protocol)
def test_sequence(device_name, translator): if translator == 'onnx_chainer': if device_name == 'native:0' or device_name == 'cuda:0': pytest.skip() 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]) 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)
def main(): #データの読み込み train_batch_size = 128 test_batch_size = 1000 train_data_size, valid_data_size, train_loader, val_loader, test_loader = DataSet( train_batch_size=train_batch_size, test_batch_size=test_batch_size) print("train data size:", train_data_size, " valid data size:", valid_data_size) model = CNN() if chainer.cuda.available: device = chainer.get_device(0) print("use gpu") else: device = -1 print("use cpu") model.to_device(device) #device.use opt = optimizers.Adam() opt.setup(model) epoch_num = 10 train_loss_log = [] train_acc_log = [] val_loss_log = [] val_acc_log = [] print("epoch:", epoch_num, "batch size:", train_batch_size) print("start train.") import time for epoch in range(epoch_num): start_time = time.perf_counter() avg_train_loss, avg_train_acc = TrainBatch(train_data_size, train_batch_size, train_loader, device, model, opt) end_time = time.perf_counter() s_val_time = time.perf_counter() avg_val_loss, avg_val_acc = ValidBatch(valid_data_size, train_batch_size, val_loader, device, model) e_val_time = time.perf_counter() proc_time = end_time - start_time val_time = e_val_time - s_val_time print("Epoch[{}/{}], train loss: {loss:.4f}, valid loss: {val_loss:.4f}, valid acc: {val_acc:.4f}, "\ "train time: {proc_time:.4f}sec, valid time: {val_time:.4f}sec"\ .format(epoch+1, epoch_num, loss=avg_train_loss, val_loss=avg_val_loss, val_acc=avg_val_acc, proc_time=proc_time, val_time=val_time)) train_loss_log.append(avg_train_loss) train_acc_log.append(avg_train_acc) val_loss_log.append(avg_val_loss) val_acc_log.append(avg_val_acc) ViewGraph(epoch_num, train_loss_log, train_acc_log, val_loss_log, val_acc_log)
def to_chainer_device(device): """Create a chainer device from a given torch device. Args: device (torch.device): Device to be converted. Returns: A ``chainer.device`` object corresponding to the given input. """ if not isinstance(device, torch.device): raise TypeError('The argument should be torch device.') if device.type == 'cpu': return chainer.get_device('@numpy') if device.type == 'cuda': device_index = 0 if device.index is None else device.index return chainer.get_device('@cupy:{}'.format(device_index)) raise ValueError('{} is not supported.'.format(device.type))
def _to_device(self, device, skip_between_cupy_devices=False): # Overrides Link._to_device # TODO(niboshi): Avoid forcing concrete links to override _to_device device = chainer.get_device(device) self._func._to_device( device, skip_between_cupy_devices=skip_between_cupy_devices) return super(BinaryHierarchicalSoftmax, self)._to_device( device, skip_between_cupy_devices=skip_between_cupy_devices)
def save3x3(x, filename): numpy_device = chainer.get_device('@numpy') fig, ax = plt.subplots(3, 3, figsize=(9, 9), dpi=100) for ai, xi in zip(ax.flatten(), x): im = xi.reshape(28, 28) im = numpy_device.send(im) ai.imshow(im) fig.savefig(filename)
def test_forward_chainerx_cuda(self): # TODO(niboshi): Support it if self.dtype == numpy.float16: raise unittest.SkipTest('ChainerX does not support float16') device = chainer.get_device('cuda:0') self.link.to_device(device) self.check_forward(device.send(self.x), device.send(self.t))
def to_intel64(self): # type: () -> 'DeviceResident' """Copies parameter variables and persistent values to CPU.""" intel64.check_ideep_available() visitor = _ToDeviceVisitor(chainer.get_device(intel64.Intel64Device()), entry_method_info=('to_intel64', {})) self.__to_device(visitor) return self
def to_intel64(self): # type: () -> 'DeviceResident' """Copies parameter variables and persistent values to CPU.""" intel64.check_ideep_available() visitor = _ToDeviceVisitor( chainer.get_device(intel64.Intel64Device()), entry_method_info=('to_intel64', {})) self.__to_device(visitor) return self
def _to_device(self, device, skip_between_cupy_devices=False): # Overrides Link._to_device # TODO(niboshi): Avoid forcing concrete links to override _to_device device = chainer.get_device(device) if not (skip_between_cupy_devices and device.xp is cuda.cupy and isinstance(self.sampler, cuda.ndarray)): self.sampler.to_device(device) return super(BlackOut, self)._to_device( device, skip_between_cupy_devices=skip_between_cupy_devices)
def _to_device(self, device, skip_between_cupy_devices=False): # TODO(niboshi): Avoid forcing concrete implementations to handle # skip_between_cupy_devices device = chainer.get_device(device) if not (skip_between_cupy_devices and device.xp is cuda.cupy and isinstance(self.paths, cuda.ndarray)): self.paths = device.send(self.paths) self.codes = device.send(self.codes) self.begins = device.send(self.begins)
def test_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 device = backend.GpuDevice.from_array(arr) assert isinstance(device, backend.GpuDevice) assert (device == chainer.get_device((cuda.cupy, backend_config.cuda_device)))
def _to_device(self, device, skip_between_cupy_devices=False): # Overrides Link._to_device device = chainer.get_device(device) super(ChainList, self)._to_device( device, skip_between_cupy_devices=skip_between_cupy_devices) for link in self._children: link._to_device( device, skip_between_cupy_devices=skip_between_cupy_devices) return self
def _to_device(self, device, skip_between_cupy_devices=False): # Overrides Link._to_device device = chainer.get_device(device) super(Chain, self)._to_device( device, skip_between_cupy_devices=skip_between_cupy_devices) d = self.__dict__ for name in self._children: d[name]._to_device( device, skip_between_cupy_devices=skip_between_cupy_devices) return self
def _to_device(self, device, skip_between_cupy_devices=False): # Overrides Link._to_device # TODO(niboshi): Avoid forcing concrete links to override _to_device device = chainer.get_device(device) super(StatefulGRU, self)._to_device( device, skip_between_cupy_devices=skip_between_cupy_devices) if self.h is not None: if not (skip_between_cupy_devices and device.xp is cuda.cupy and isinstance(self.h, cuda.ndarray)): self.h.to_device(device) return self
def _to_device(self, device, skip_between_cupy_devices=False): # type: (types.DeviceSpec, bool) -> 'ChainList' # Overrides Link._to_device device_obj = chainer.get_device(device) super(ChainList, self)._to_device( device_obj, skip_between_cupy_devices=skip_between_cupy_devices) for link in self._children: link._to_device( device_obj, skip_between_cupy_devices=skip_between_cupy_devices) return self
def _to_device(self, device, skip_between_cupy_devices=False): # type: (types.DeviceSpec, bool) -> 'Chain' # Overrides Link._to_device backend_device = chainer.get_device(device) super(Chain, self)._to_device( backend_device, skip_between_cupy_devices=skip_between_cupy_devices) d = self.__dict__ for name in self._children: d[name]._to_device( backend_device, skip_between_cupy_devices=skip_between_cupy_devices) return self
def train(train_data_path, test_data_path, args): device = chainer.get_device(args.device) device.use() vocab = collections.defaultdict(lambda: len(vocab)) vocab['<unk>'] = 0 train_data = babi.read_data(vocab, train_data_path) test_data = babi.read_data(vocab, test_data_path) print('Training data: %s: %d' % (train_data_path, len(train_data))) print('Test data: %s: %d' % (test_data_path, len(test_data))) train_data = memnn.convert_data(train_data, args.max_memory) test_data = memnn.convert_data(test_data, args.max_memory) encoder = memnn.make_encoder(args.sentence_repr) network = memnn.MemNN( args.unit, len(vocab), encoder, args.max_memory, args.hop) model = chainer.links.Classifier(network, label_key='answer') opt = chainer.optimizers.Adam() model.to_device(device) opt.setup(model) train_iter = chainer.iterators.SerialIterator( train_data, args.batchsize) test_iter = chainer.iterators.SerialIterator( test_data, args.batchsize, repeat=False, shuffle=False) updater = chainer.training.StandardUpdater(train_iter, opt, device=device) trainer = chainer.training.Trainer(updater, (args.epoch, 'epoch')) @chainer.training.make_extension() def fix_ignore_label(trainer): network.fix_ignore_label() trainer.extend(fix_ignore_label) trainer.extend(extensions.Evaluator(test_iter, model, device=device)) trainer.extend(extensions.LogReport()) trainer.extend(extensions.PrintReport( ['epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy'])) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.run() if args.model: memnn.save_model(args.model, model, vocab)
def _to_device(self, device, skip_between_cupy_devices=False): # `skip_between_cupy_devices` argument is a workaround # for `Link.to_gpu` which does not transfer cupy parameters to # a different CUDA device. device = chainer.get_device(device) d = self.__dict__ for name in self._params: if not (skip_between_cupy_devices and device.xp is cuda.cupy and d[name].device.xp is cuda.cupy): d[name].to_device(device) for name in self._persistent: if not numpy.isscalar(d[name]): if not (skip_between_cupy_devices and device.xp is cuda.cupy and isinstance(d[name], cuda.ndarray)): d[name] = device.send(d[name]) self._device = device return self
def to_device( self, device # type: types.DeviceSpec ): # type: (...) -> '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
def _to_device(self, device, skip_between_cupy_devices=False): # type: (types.DeviceSpec, bool) -> 'Link' # `skip_between_cupy_devices` argument is a workaround # for `Link.to_gpu` which does not transfer cupy parameters to # a different CUDA device. backend_device = chainer.get_device(device) d = self.__dict__ # type: tp.Dict[str, chainer.Parameter] for name in self._params: if not (skip_between_cupy_devices and backend_device.xp is cuda.cupy and d[name].device.xp is cuda.cupy): d[name].to_device(backend_device) for name in self._persistent: if not numpy.isscalar(d[name]): if not (skip_between_cupy_devices and backend_device.xp is cuda.cupy and isinstance(d[name], cuda.ndarray)): d[name] = backend_device.send(d[name]) self._device = backend_device return self
def test_repr_str_cupy_device(self): device = chainer.get_device('@cupy:0') assert str(device) == '<GpuDevice (cupy):0>'
def test_repr_tuple_cupy_device(self): device = chainer.get_device((cuda.cupy, 0)) assert str(device) == '<GpuDevice (cupy):0>'
def test_repr_tuple_chainerx_device(self): device = chainer.get_device(('native', 0)) assert str(device) == '<ChainerxDevice native:0>'
def test_repr_module_numpy(self): device = chainer.get_device(numpy) assert str(device) == '<CpuDevice (numpy)>'
predict_batch(batch) batch = [] if batch: predict_batch(batch) if __name__ == '__main__': parser = argparse.ArgumentParser( description='Chainer example: Text Classification') parser.add_argument('--device', '-d', type=str, default='-1', help='Device specifier. Either ChainerX device ' 'specifier or an integer. If non-negative integer, ' 'CuPy arrays with specified device id are used. If ' 'negative integer, NumPy arrays are used') parser.add_argument('--model-setup', required=True, help='Model setup dictionary.') group = parser.add_argument_group('deprecated arguments') group.add_argument('--gpu', '-g', dest='device', type=int, nargs='?', const=0, help='GPU ID (negative value indicates CPU)') args = parser.parse_args() device = chainer.get_device(args.device) device.use() model, vocab, setup = setup_model(device, args.model_setup) if device.xp is numpy: run_online(device) else: run_batch(device)
def test_repr_str_numpy(self): device = chainer.get_device('@numpy') assert str(device) == '<CpuDevice (numpy)>'
def test_repr_str_chainerx_device(self): device = chainer.get_device('native:0') assert str(device) == '<ChainerxDevice native:0>'
def test_repr_tuple_intel64_device(self): device = chainer.get_device(intel64) assert str(device) == '<Intel64Device>'
def main(): parser = argparse.ArgumentParser(description='Chainer example: DCGAN') parser.add_argument('--batchsize', '-b', type=int, default=50, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=1000, help='Number of sweeps over the dataset to train') parser.add_argument('--device', '-d', type=str, default='-1', help='Device specifier. Either ChainerX device ' 'specifier or an integer. If non-negative integer, ' 'CuPy arrays with specified device id are used. If ' 'negative integer, NumPy arrays are used') parser.add_argument('--dataset', '-i', default='', help='Directory of image files. Default is cifar-10.') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--resume', '-r', type=str, help='Resume the training from snapshot') parser.add_argument('--n_hidden', '-n', type=int, default=100, help='Number of hidden units (z)') parser.add_argument('--seed', type=int, default=0, help='Random seed of z at visualization stage') parser.add_argument('--snapshot_interval', type=int, default=1000, help='Interval of snapshot') parser.add_argument('--display_interval', type=int, default=100, help='Interval of displaying log to console') group = parser.add_argument_group('deprecated arguments') group.add_argument('--gpu', '-g', dest='device', type=int, nargs='?', const=0, help='GPU ID (negative value indicates CPU)') args = parser.parse_args() device = chainer.get_device(args.device) device.use() print('Device: {}'.format(device)) print('# Minibatch-size: {}'.format(args.batchsize)) print('# n_hidden: {}'.format(args.n_hidden)) print('# epoch: {}'.format(args.epoch)) print('') # Set up a neural network to train gen = Generator(n_hidden=args.n_hidden) dis = Discriminator() gen.to_device(device) # Copy the model to the device dis.to_device(device) # Setup an optimizer def make_optimizer(model, alpha=0.0002, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook( chainer.optimizer_hooks.WeightDecay(0.0001), 'hook_dec') return optimizer opt_gen = make_optimizer(gen) opt_dis = make_optimizer(dis) if args.dataset == '': # Load the CIFAR10 dataset if args.dataset is not specified train, _ = chainer.datasets.get_cifar10(withlabel=False, scale=255.) else: all_files = os.listdir(args.dataset) image_files = [f for f in all_files if ('png' in f or 'jpg' in f)] print('{} contains {} image files' .format(args.dataset, len(image_files))) train = chainer.datasets\ .ImageDataset(paths=image_files, root=args.dataset) # Setup an iterator train_iter = chainer.iterators.SerialIterator(train, args.batchsize) # Setup an updater updater = DCGANUpdater( models=(gen, dis), iterator=train_iter, optimizer={ 'gen': opt_gen, 'dis': opt_dis}, device=device) # Setup a trainer trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) snapshot_interval = (args.snapshot_interval, 'iteration') display_interval = (args.display_interval, 'iteration') trainer.extend( extensions.snapshot(filename='snapshot_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( gen, 'gen_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( dis, 'dis_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.LogReport(trigger=display_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend( out_generated_image( gen, dis, 10, 10, args.seed, args.out), trigger=snapshot_interval) if args.resume is not None: # Resume from a snapshot chainer.serializers.load_npz(args.resume, trainer) # Run the training trainer.run()
def test_repr_str_intel64_device(self): device = chainer.get_device('@intel64') assert str(device) == '<Intel64Device>'