# Intel CPUでの学習

## CPUでのトレーニング最適化の仕組み

Accelerate は Intel CPU を完全にサポートしています。設定で有効にするだけです。

**シナリオ 1**: 分散なし CPU トレーニングの高速化

マシンの上でaccelerate configを実行します:

```bash
$ accelerate config
-----------------------------------------------------------------------------------------------------------------------------------------------------------
In which compute environment are you running?
This machine
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Which type of machine are you using?
No distributed training
Do you want to run your training on CPU only (even if a GPU / Apple Silicon device is available)? [yes/NO]:yes
Do you wish to optimize your script with torch dynamo?[yes/NO]:NO
Do you want to use DeepSpeed? [yes/NO]: NO
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Do you wish to use FP16 or BF16 (mixed precision)?
bf16
```
これにより設定ファイルが生成され、doing を実行する際にデフォルトのオプションが正しく設定されるように自動的に使用されます。

```bash
accelerate launch my_script.py --args_to_my_script
```

例えば、リポジトリのルートから、`accelerate config` によって生成される `default_config.yaml` を使用して、NLPの例である `examples/nlp_example.py` を実行するには、次のようにします。

```bash
compute_environment: LOCAL_MACHINE
distributed_type: 'NO'
downcast_bf16: 'no'
machine_rank: 0
main_training_function: main
mixed_precision: bf16
num_machines: 1
num_processes: 1
rdzv_backend: static
same_network: true
tpu_env: []
tpu_use_cluster: false
tpu_use_sudo: false
use_cpu: true
```
```bash
accelerate launch examples/nlp_example.py
```

> [!CAUTION]
> `accelerator.prepare`は現在、複数のモデルを同時に準備すること（オプティマイザーなし）か、学習用のモデルとオプティマイザーのペアを1つだけ準備することのみに対応しています。それ以外の試み（例：モデルとオプティマイザーのペアを2つ）は、詳細なエラーを発生させます。この制限を回避するには、各モデルとオプティマイザーのペアに対して`accelerator.prepare`を個別に使用することを検討してください。

**シナリオ2**: 分散CPUトレーニングの高速化。Intel oneCCLを通信に使用し、Intel® MPIライブラリと組み合わせることで、Intel®アーキテクチャ上で柔軟で効率的かつスケーラブルなクラスターメッセージングを実現します。インストールガイドについては、[こちら](https://huggingface.co/docs/transformers/perf_train_cpu_many)を参照してください。

マシンの(node0)でaccelerate configを実行します:

```bash
$ accelerate config
-----------------------------------------------------------------------------------------------------------------------------------------------------------
In which compute environment are you running?
This machine
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Which type of machine are you using?
multi-CPU
How many different machines will you use (use more than 1 for multi-node training)? [1]: 4
-----------------------------------------------------------------------------------------------------------------------------------------------------------
What is the rank of this machine?
0
What is the IP address of the machine that will host the main process? 36.112.23.24
What is the port you will use to communicate with the main process? 29500
Are all the machines on the same local network? Answer `no` if nodes are on the cloud and/or on different network hosts [YES/no]: yes
Do you want accelerate to launch mpirun? [yes/NO]: yes
Please enter the path to the hostfile to use with mpirun [~/hostfile]: ~/hostfile
Enter the number of oneCCL worker threads [1]: 1
Do you wish to optimize your script with torch dynamo?[yes/NO]:NO
How many processes should be used for distributed training? [1]:16
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Do you wish to use FP16 or BF16 (mixed precision)?
bf16
```
例えば、リポジトリのルートから分散CPUトレーニングでNLPの例`examples/nlp_example.py`を実行するには、次のようにします。

`accelerate config` によって生成される `default_config.yaml`
```bash
compute_environment: LOCAL_MACHINE
distributed_type: MULTI_CPU
downcast_bf16: 'no'
machine_rank: 0
main_process_ip: 36.112.23.24
main_process_port: 29500
main_training_function: main
mixed_precision: bf16
mpirun_config:
  mpirun_hostfile: /home/user/hostfile
num_machines: 4
num_processes: 16
rdzv_backend: static
same_network: true
tpu_env: []
tpu_use_cluster: false
tpu_use_sudo: false
use_cpu: true
```

以下の環境変数を設定し、Intel MPI を使用してトレーニングを起動します

`node0`では、各ノードの IP アドレス（例えば hostfile など）を含む設定ファイルを作成し、その設定ファイルのパスを引数として渡す必要があります。

Accelerate による `mpirun` の起動を選択した場合、ホストファイルの場所が設定内のパスと一致していることを確認してください。

```bash
$ cat hostfile
xxx.xxx.xxx.xxx #node0 ip
xxx.xxx.xxx.xxx #node1 ip
xxx.xxx.xxx.xxx #node2 ip
xxx.xxx.xxx.xxx #node3 ip
```

```bash
accelerate launch examples/nlp_example.py
```

`mpirun` コマンドを使用して分散学習を直接起動することもできます。この場合、node0 で以下のコマンドを実行すると、node0、node1、node2、node3 で BF16 混合精度が有効になった **16DDP** が有効になります。この方法を使用する場合、マルチ CPU 学習に使用されるすべてのマシンで、Python スクリプト、Python 環境、accelerate 設定ファイルが利用可能である必要があります。

```bash
export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip
mpirun -f hostfile -n 16 -ppn 4 accelerate launch examples/nlp_example.py
```

