Home

Awesome

Effective Transformer

Effective Transformer is built on top of the NVIDIA open sourced project FasterTransformer with many advanced optimizations. Our experiments show Effective Transformer can significantly reduce the execution time and memory consumption, especially for large batch size cases.

Running BERT without Padding

When using BERT to encode a batch of input sequences, we usually treat the input batch as a matrix whose column number equals to the maximum length of all sequences. NVIDIA FasterTransformer can process cases that all sequences have roughly the same length very efficiently. However, if the lengths of sequences in the same batch vary a lot, padding them into the same length means a big waste of both memory and computation resources.

Consider the following case

bert_input = [["Hi"], ["Picking"], ["The", "seed", "of", "Job's", "tears"]]
bert_tokens = [[1], [2], [3,4,5,6,7]]
bert_tokens_padded = [[1, 0, 0, 0, 0], [2, 0, 0, 0, 0], [3, 4, 5, 6, 7]]
bert_tokens_mask = [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 1, 1, 1]]

this input includes 3 sequences and the maximum length is 5. If we just simply treat it as a 3x5 matrix, only 7 out of 15 values are meaningful.

In Effective Transformer, we still take the input batch as a padded matrix but padding values will be dynamically removed and restored during different calculation stages.

By calculating the prefix sum of the input mask matrix, we can access real inputs in each sequence in a matrix with no padding values. The following figure illustrates how to access valid inputs and dynamically remove and restore padding values during the calculation. All valid inputs are colored in green while padding values are colored in gray.

<img src="./images/1.png" width="50%" height="50%">

Environment requirements

Features

Performance

BERT-Base, layers=12, head_num=12, hidden_size=64

Intel(R) Xeon(R) Platinum 8260 CPU @ 2.40GHz

serquence length generated by

avg_seq_len = np.random.randint(
    low = 2 * avg_seq_len - max_seq_len,
    high = max_seq_len + 1,
    size = (batch_size),
    dtype = np.int32)

Tesla V100, float16, maximum sequence length=32, average serquence length≈20

batch_sizeXLA (in ms)Faster Transformer (in ms)Speedup over XLAEffective Transformer (in ms)Speedup over XLA
10015.0810.391.458.751.72
20028.0819.641.4315.321.83
30041.3729.651.4022.181.86
40053.6538.521.3928.311.89
50066.8648.131.3933.082.02
1000131.4695.011.3864.342.04

Tesla V100, float16, maximum sequence length=64, average serquence length≈40

batch_sizeXLA (in ms)Faster Transformer (in ms)Speedup over XLAEffective Transformer (in ms)Speedup over XLA
10028.3120.271.4016.031.77
20054.4740.081.3630.151.81
30080.5359.111.3641.271.95
400106.578.381.3654.121.97
500132.3598.031.3765.922.01
1000261.18190.911.38133.611.95

Tesla V100, float32, maximum sequence length=64, average serquence length≈40

batch_sizeXLA (in ms)Faster Transformer (in ms)Speedup over XLAEffective Transformer (in ms)Speedup over XLA
100103.1398.521.0567.451.53
200207.40198.861.04125.441.65
300304.99290.551.05197.071.55
400405.98386.041.05247.391.64
500516.88496.901.04325.371.59

Tesla T4, float16, maximum sequence length=32, average serquence length≈20

batch_sizeXLA (in ms)FasterTransformer (in ms)Speedup over XLAEffectiveTransformer (in ms)Speedup over XLA
10044.9435.071.2828.631.57
20090.0967.081.3453.841.67
300136.88100.961.3582.741.65
400184.80133.131.39109.091.69
500242.79166.541.46136.661.78

Tesla T4, float16, maximum sequence length=64, average serquence length≈40

batch_sizeXLA (in ms)FasterTransformer (in ms)Speedup over XLAEffectiveTransformer (in ms)Speedup over XLA
10087.2365.861.3052.011.68
200176.91138.531.34108.331.63
300261.25204.991.36157.841.65
400355.34272.961.33202.611.75
500452.62343.891.33250.781.80

Run demo

Using python prebuilt packege requires python3.5+ tensorflow1.15.x cuda10.0, tested on debian9.

$ cd effective_transformer
$ pip install -e python

$ python benchmark.py --help
usage: benchmark.py [-h] [-c CONFIG] [-p {fp32,fp16}] [-b BATCH_SIZE]
                    [-m MAX_SEQ_LENGTH] [-a AVG_SEQ_LENGTH]

Bert performance measuring sample.

optional arguments:
  -h, --help            show this help message and exit
  -c CONFIG, --config CONFIG
                        Bert config file.
  -p {fp32,fp16}, --precision {fp32,fp16}
                        Weight precision.
  -b BATCH_SIZE, --batch_size BATCH_SIZE
                        Batch size.
  -m MAX_SEQ_LENGTH, --max_seq_length MAX_SEQ_LENGTH
                        Max sequence length.
  -a AVG_SEQ_LENGTH, --avg_seq_length AVG_SEQ_LENGTH
                        Average sequence length.

Build from source

TF_PATH : path to libtensorflow_framework.so

$ mkdir build && cd build
$ cmake -DTF_PATH=/your/path/to/pythonx.x/site-packages/tensorflow_core/ ..
$ make
$ cp lib/libtf_effectivetransformer.so ../python/effective_transformer/libtf_effectivetransformer.so.1.15