# Pelatihan pada CPU Intel

## Cara Kerjanya Untuk optimasi pelatihan di CPU

Accelerate memiliki dukungan penuh untuk CPU Intel, yang Anda butuhkan hanyalah mengaktifkannya melalui konfigurasi.

**Skenario 1**: Percepatan pelatihan CPU tanpa distribusi

Jalankan accelerate config di mesin Anda:

```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
```
Hal ini akan menghasilkan file konfigurasi yang akan digunakan secara otomatis untuk mengatur opsi default dengan benar saat melakukan

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

Sebagai contoh, berikut adalah cara menjalankan contoh NLP `examples/nlp_example.py` (dari root repo) dengan `default_config.yaml` yang dihasilkan oleh `accelerate config`

```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` saat ini hanya dapat menangani persiapan beberapa model secara bersamaan (tanpa optimizer) ATAU satu pasangan model-optimizer untuk pelatihan. Percobaan lain (misalnya, dua pasangan model-optimizer) akan memicu error yang detail. Untuk mengatasi keterbatasan ini, pertimbangkan untuk menggunakan `accelerator.prepare` secara terpisah untuk setiap pasangan model-optimizer.

**Skenario 2**: Percepatan pelatihan CPU terdistribusi. Kami menggunakan Intel oneCCL untuk komunikasi, dikombinasikan dengan pustaka Intel® MPI untuk menyediakan pengiriman pesan kluster yang fleksibel, efisien, dan dapat diskalakan pada arsitektur Intel®. Anda dapat merujuk [di sini](https://huggingface.co/docs/transformers/perf_train_cpu_many) untuk panduan instalasi.

Jalankan accelerate config di mesin Anda (node0):

```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
```
Sebagai contoh, berikut adalah cara menjalankan contoh NLP `examples/nlp_example.py` (dari root repo) untuk pelatihan CPU terdistribusi.

`default_config.yaml` yang dibuat oleh `accelerate config`
```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
```

Set variabel lingkungan berikut dan gunakan intel MPI untuk meluncurkan pelatihan

Di `node0`, Anda perlu membuat file konfigurasi yang berisi alamat IP dari setiap node (misalnya hostfile) dan meneruskan path file konfigurasi tersebut sebagai argumen.

Jika Anda memilih agar Accelerate meluncurkan `mpirun`, pastikan lokasi hostfile Anda sesuai dengan path di config.

```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
```

Anda juga dapat langsung meluncurkan pelatihan terdistribusi dengan perintah `mpirun`, Anda perlu menjalankan perintah berikut di node0 dan **16DDP** akan diaktifkan di node0,node1,node2,node3 dengan presisi campuran BF16. Saat menggunakan metode ini, skrip python, lingkungan python, dan file konfigurasi accelerate harus tersedia di semua mesin yang digunakan untuk pelatihan multi-CPU.

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

