Chainer v2 released: difference from v1

 

Chainer version 2 has been released on 2017 June 1, 

This post is a summary of what you need to change in your code for your chainer development. Detail change is written in official document.

 

Installation change

CuPy module becomes independent. Reason is that CuPy is GPU version of numpy, it can be used for many types of linear calculation, not specific for chainer.

To setup Chainer,

  1. If you are using only CPU, this is enough as previous
  2. If you want to get a benefit of GPU, you need to setup CUDA and need to install CuPy separately.

    [NOTE] Also, is you have multiple GPU, you can install NCCL before install chainer and cupy to use MultiProcessParallelUpdater

Important to note that NO source code change is necessary for your Chainer development. Chainer will import CuPy only when it is installed in your environment.

 

Global configuration is introduced

Global config chainer.global_config and thread local config chainer.config is introduced to control the chainer behavior.

Its config includes these flags,

  • chainer.config.cudnn_deterministic
  • chainer.config.debug
  • chainer.config.enable_backprop
  • chainer.config.keep_graph_on_report
  • chainer.config.train
  • chainer.config.type_check
  • chainer.config.use_cudnn

See official document for details.

I think train flag and enable_backprop flag is important to remember.

chainer.config.train

Function behavior can be controlled by using chainer.config.train flag instead of writing it in function argument. I will just cite above official doc for the example,

Example

Consider the following model definition and the code to call it in test mode written for Chainer v1.

In Chainer v2, it should be updated into the following code:

 

 

chainer.config.enable_backprop

volatile flag of Variable class, used in Chainer v1, is removed in v2.

Instead you can use chainer.config.enable_backprop flag to control ON/OFF of backpropagation.

When disable backprop, there is util function chainer.no_backprop_mode(),

Input size of the Link can be omitted

Let me just show the example,

In Chainer v1

 

In Chainer v2 it can be also written as, (writing in Chainer v1 notation is also possible)

 

This is available with following links,

 

init_scope closure can be used for Link, Chain initialization

When you define your own Link or Chain class, init_scope() can be used to initialize Parameter or Link,

This writing style is recommended because of IDE (PyCharm etc) can enhance the local variable indexing and show type hinting. But you can still use conventional (chainer v1) initialization as well.

Below is an example of defining Chain class, from official doc,

Example

For example, the following chain initialization code

 

is recommended to be updated as follows.

 

Function spec change (GRU, LSTM etc)

This change affects those who are working with NLP (Natural Language Processing) field.

GRU and LSTM function behavior has changed.

 

Optimizer spec change

Some of the deprecated optimizer function, e.g. zero_grads(), is removed.

 

Internal Change for better performance

This does not affect to your development code change, but just good to know for your knowledge:

 

Memory efficiency enhancement

When creating a computational graph for back propagation, Function does not keep the Variable’s array data itself but only keep the reference of it. 

 

Speed enhancement

Lazy type check is introduced to speed up type check.

 

Summary

  • CuPy module becomes independent package: need to install separately if using GPU.
  • Global config, chainer.config is introduced
    • Function, Chain call behavior is switched by chainer.config.train flag.
    • volatile flag of Variable is removed, use chainer.config.enable_backprop flag instead.

       

  • Your custom class of Link and Chain can be initialized by with self.init_scope(): sentence.

 

I noticed that many with statement is used in Chainer v2 code.

Sponsored Links

Leave a Reply

Your email address will not be published.