# Huấn luyện trên CPU Intel

## Cách hoạt động Tối ưu hóa huấn luyện trên CPU

Accelerate hỗ trợ đầy đủ cho CPU Intel, bạn chỉ cần bật tính năng này thông qua cấu hình.

**Kịch bản 1**: Tăng tốc độ huấn luyện CPU không phân tán

Chạy accelerate config trên máy của bạn:

```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
```
Điều này sẽ tạo ra một tệp cấu hình sẽ được sử dụng tự động để thiết lập đúng các tùy chọn mặc định khi thực hiện

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

Ví dụ, đây là cách bạn chạy ví dụ NLP `examples/nlp_example.py` (từ thư mục gốc của repo) với `default_config.yaml` được tạo bởi `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` hiện chỉ có thể xử lý việc chuẩn bị đồng thời nhiều model (và không có optimizer) HOẶC một cặp model-optimizer duy nhất để huấn luyện. Các nỗ lực khác (ví dụ: hai cặp model-optimizer) sẽ gây ra một lỗi chi tiết. Để khắc phục hạn chế này, hãy cân nhắc sử dụng `accelerator.prepare` riêng biệt cho từng cặp model-optimizer.

**Kịch bản 2**: Tăng tốc huấn luyện CPU phân tán. Chúng tôi sử dụng Intel oneCCL cho giao tiếp, kết hợp với thư viện Intel® MPI để cung cấp thông điệp cụm linh hoạt, hiệu quả và có khả năng mở rộng trên kiến trúc Intel®. Bạn có thể tham khảo [tại đây](https://huggingface.co/docs/transformers/perf_train_cpu_many) để xem hướng dẫn cài đặt.

Chạy accelerate config trên máy của bạn (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
```
Ví dụ, đây là cách bạn chạy ví dụ NLP `examples/nlp_example.py` (từ thư mục gốc của repo) cho huấn luyện CPU phân tán.

`default_config.yaml` được tạo bởi `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
```

Đặt các biến môi trường sau và sử dụng intel MPI để khởi động quá trình huấn luyện

Trong `node0`, bạn cần tạo một tệp cấu hình chứa địa chỉ IP của từng node (ví dụ: hostfile) và truyền đường dẫn đến tệp cấu hình đó dưới dạng một đối số.

Nếu bạn chọn để Accelerate khởi chạy `mpirun`, hãy đảm bảo rằng vị trí của hostfile khớp với đường dẫn trong cấu hình.

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

Bạn cũng có thể khởi động trực tiếp huấn luyện phân tán bằng lệnh `mpirun`, bạn cần chạy lệnh sau trên node0 và **16DDP** sẽ được kích hoạt trên node0, node1, node2, node3 với độ chính xác hỗn hợp BF16. Khi sử dụng phương pháp này, script python, môi trường python và tệp cấu hình accelerate phải khả dụng trên tất cả các máy được sử dụng cho huấn luyện đa CPU.

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

