CPU上での処理とInferentia上での処理との比較

章5.1、章5.2で実行した推論用スクリプトに変更を加え、CPU上で推論処理を実行する場合と、Neuronコア上に推論処理をオフロードする場合との性能比較を行います。

Step 1. 推論スクリプトの変更

以下の内容でinfer_bert_perf.py というファイル名の推論実行 Python スクリプトを作成します。

章5.2で生成した TorchScriptモデル bert_cpu.ptbert_neuron.pt を再利用しますので、同じディレクトリ内で作業を行ってください。

import os
import time
import torch
import torch.neuron
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# Build tokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased-finetuned-mrpc")

# Load TorchScript back
model_cpu = torch.jit.load('bert_cpu.pt')
model_neuron = torch.jit.load('bert_neuron.pt')

# Setup some example inputs
sequence_0 = "The company HuggingFace is based in New York City"
sequence_1 = "Apples are especially bad for your health"
sequence_2 = "HuggingFace's headquarters are situated in Manhattan"

paraphrase = tokenizer.encode_plus(sequence_0, sequence_2, max_length=128, padding='max_length', truncation=True, return_tensors="pt")

# Convert example inputs to a format that is compatible with TorchScript tracing
example_inputs_paraphrase = paraphrase['input_ids'], paraphrase['attention_mask'], paraphrase['token_type_ids']

num_inferences = 1000

# warmup
paraphrase_classification_logits = model_cpu(*example_inputs_paraphrase)
paraphrase_classification_logits = model_neuron(*example_inputs_paraphrase)

# Run inference on CPUs, Display results
start = time.time()
for _ in range(num_inferences):
    paraphrase_classification_logits = model_cpu(*example_inputs_paraphrase)
elapsed_time = time.time() - start
print('By CPU         - num_inferences:{:>6}[sentences], elapsed_time:{:6.2f}[sec], Throughput:{:8.2f}[sentences/sec]'.format(num_inferences, elapsed_time, num_inferences / elapsed_time))

# Run inference on Neuron Cores, Display results
start = time.time()
for _ in range(num_inferences):
    paraphrase_classification_logits = model_neuron(*example_inputs_paraphrase)
elapsed_time = time.time() - start
print('By Neuron Core - num_inferences:{:>6}[sentences], elapsed_time:{:6.2f}[sec], Throughput:{:8.2f}[sentences/sec]'.format(num_inferences, elapsed_time, num_inferences / elapsed_time))

Step 2. 推論スクリプトを実行

推論実行スクリプトinfer_bert_perf.pyを実行し、CPU上での処理とNeuronコア上での処理との比較を行います。

python infer_bert_perf.py

次の結果が取得されます。CPU上で推論処理を実行した場合と比較し、Neuronコアによる推論処理の高速化を確認できます。

By CPU         - num_inferences:  1000[sentences], elapsed_time: 67.66[sec], Throughput:   14.78[sentences/sec]
By Neuron Core - num_inferences:  1000[sentences], elapsed_time:  7.20[sec], Throughput:  138.96[sentences/sec]

Step 3. Neuronコアの負荷測定

Neuronコア上での推論処理が実行されている間に、別のターミナル上で neuron-top ツールを使用して Neuronコアの利用率を確認します。

neuron-top

出力結果から Inferentia推論チップ上の4つの Neuron コアのうち、1つのコアのみが、90~100%程度の負荷で利用されていることが確認できます。

 NeuronCore Utilization
                  NC0                          NC1                         NC2                         NC3
 ND0  |||||||||||||||[94.57%] ||||||||||||||||||||[ 0.00%] ||||||||||||||||||||[ 0.00%] ||||||||||||||||||||[ 0.00%]

 vCPU and Memory Info
 System vCPU Usage ||||||||||||||||||||||||[31.60%, 4.19%]    Runtime vCPU Usage |||||||||||||||||||||||[23.98%, 0.38%]
 Runtime Memory Host ||||||||||||||||||[ 781.0KB/  15.2GB]    Runtime Memory Device  179.5MB

 Loaded Models
                                                                                                    Model ID
  [+] ND 0

Step 1 のスクリプト infer_bert_perf.py のままではNeuronコア上での推論処理が数秒で終了してしまうため、繰り返し回数 num_inferences を大きくし、またCPU上での推論処理をコメントアウトして再度実行して下さい。