# Intel CPU üzerinde eğitim

## Eğitim İçin Çalışma Prensibi CPU'da Optimizasyon

Accelerate, Intel CPU için tam destek sunar; yapmanız gereken, yapılandırmadan bunu etkinleştirmektir.

**Senaryo 1**: Dağıtık olmayan CPU eğitiminde hızlandırma

Makinenizde accelerate config komutunu çalıştırı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
```
Bu, … sırasında varsayılan seçenekleri doğru şekilde ayarlamak için otomatik olarak kullanılacak bir yapılandırma dosyası oluşturur.

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

Örneğin, `accelerate config` tarafından oluşturulan `default_config.yaml` ile NLP örneği olan `examples/nlp_example.py` dosyasını (deponun kökünden) şu şekilde çalıştırırsınız

```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` şu anda yalnızca birden fazla modelin (ve optimizatsız) eşzamanlı olarak hazırlanmasını veya eğitim için tek bir model-optimizör çiftini işleyebilir. Diğer denemeler (ör. iki model-optimizör çifti) ayrıntılı bir hata oluşturur. Bu kısıtlamayı aşmak için, her model-optimizör çifti için `accelerator.prepare` işlevini ayrı ayrı kullanmayı düşünün.

**Senaryo 2**: Dağıtık CPU eğitiminde hızlandırma için iletişim amacıyla Intel oneCCL kullanıyoruz; Intel® MPI kütüphanesiyle birleştirilerek Intel® mimarisinde esnek, verimli ve ölçeklenebilir küme mesajlaşması sağlanır. Kurulum kılavuzu için [buraya](https://huggingface.co/docs/transformers/perf_train_cpu_many) bakabilirsiniz.

Makinenizde (node0) accelerate config komutunu çalıştırın:

```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
```
Örneğin, dağıtık CPU eğitimi için `examples/nlp_example.py` (deponun kökünden) NLP örneğini nasıl çalıştıracağınız aşağıdaki gibidir.

`accelerate config` tarafından oluşturulan `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
```

Aşağıdaki ortam değişkenlerini ayarlayın ve eğitimi başlatmak için intel MPI kullanın

`node0` içinde, her düğümün IP adreslerini içeren bir yapılandırma dosyası (örneğin hostfile) oluşturmanız ve bu yapılandırma dosyasının yolunu bir argüman olarak iletmeniz gerekir.

Accelerate'in `mpirun` başlatmasına izin verdiyseniz, hostfile konumunun yapılandırma dosyasındaki yolla eşleştiğinden emin olun.

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

Distributed training'i doğrudan `mpirun` komutuyla da başlatabilirsiniz; bunun için node0'da aşağıdaki komutu çalıştırmanız gerekir ve **16DDP**, BF16 mixed precision ile node0, node1, node2 ve node3'te etkinleştirilecektir. Bu yöntemi kullanırken, python scripti, python ortamı ve accelerate config dosyasının, multi-CPU training için kullanılan tüm makinelerde mevcut olması gerekir.

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

