Debian Codegraph @

docs/source/usage_guides/intel_cpu.md

raw
1 <!--Copyright 2022 The HuggingFace Team. All rights reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4 the License. You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9 an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10 specific language governing permissions and limitations under the License.
11
12 ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
13 rendered properly in your Markdown viewer.
14 -->
15
16 # Training on Intel CPU
17
18 ## How It Works For Training optimization in CPU
19
20 Accelerate has full support for Intel CPU, all you need to do is enabling it through the config.
21
22 **Scenario 1**: Acceleration of No distributed CPU training
23
24 Run <u>accelerate config</u> on your machine:
25
26 ```bash
27 $ accelerate config
28 -----------------------------------------------------------------------------------------------------------------------------------------------------------
29 In which compute environment are you running?
30 This machine
31 -----------------------------------------------------------------------------------------------------------------------------------------------------------
32 Which type of machine are you using?
33 No distributed training
34 Do you want to run your training on CPU only (even if a GPU / Apple Silicon device is available)? [yes/NO]:yes
35 Do you wish to optimize your script with torch dynamo?[yes/NO]:NO
36 Do you want to use DeepSpeed? [yes/NO]: NO
37 -----------------------------------------------------------------------------------------------------------------------------------------------------------
38 Do you wish to use FP16 or BF16 (mixed precision)?
39 bf16
40 ```
41 This will generate a config file that will be used automatically to properly set the
42 default options when doing
43
44 ```bash
45 accelerate launch my_script.py --args_to_my_script
46 ```
47
48 For instance, here is how you would run the NLP example `examples/nlp_example.py` (from the root of the repo) with `default_config.yaml` which is generated by `accelerate config`
49
50 ```bash
51 compute_environment: LOCAL_MACHINE
52 distributed_type: 'NO'
53 downcast_bf16: 'no'
54 machine_rank: 0
55 main_training_function: main
56 mixed_precision: bf16
57 num_machines: 1
58 num_processes: 1
59 rdzv_backend: static
60 same_network: true
61 tpu_env: []
62 tpu_use_cluster: false
63 tpu_use_sudo: false
64 use_cpu: true
65 ```
66 ```bash
67 accelerate launch examples/nlp_example.py
68 ```
69
70 > [!CAUTION]
71 > `accelerator.prepare` can currently only handle simultaneously preparing multiple models (and no optimizer) OR a single model-optimizer pair for training. Other attempts (e.g., two model-optimizer pairs) will raise a verbose error. To work around this limitation, consider separately using `accelerator.prepare` for each model-optimizer pair.
72
73 **Scenario 2**: Acceleration of distributed CPU training
74 we use Intel oneCCL for communication, combined with Intel® MPI library to deliver flexible, efficient, scalable cluster messaging on Intel® architecture. you could refer the [here](https://huggingface.co/docs/transformers/perf_train_cpu_many) for the installation guide
75
76 Run <u>accelerate config</u> on your machine(node0):
77
78 ```bash
79 $ accelerate config
80 -----------------------------------------------------------------------------------------------------------------------------------------------------------
81 In which compute environment are you running?
82 This machine
83 -----------------------------------------------------------------------------------------------------------------------------------------------------------
84 Which type of machine are you using?
85 multi-CPU
86 How many different machines will you use (use more than 1 for multi-node training)? [1]: 4
87 -----------------------------------------------------------------------------------------------------------------------------------------------------------
88 What is the rank of this machine?
89 0
90 What is the IP address of the machine that will host the main process? 36.112.23.24
91 What is the port you will use to communicate with the main process? 29500
92 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
93 Do you want accelerate to launch mpirun? [yes/NO]: yes
94 Please enter the path to the hostfile to use with mpirun [~/hostfile]: ~/hostfile
95 Enter the number of oneCCL worker threads [1]: 1
96 Do you wish to optimize your script with torch dynamo?[yes/NO]:NO
97 How many processes should be used for distributed training? [1]:16
98 -----------------------------------------------------------------------------------------------------------------------------------------------------------
99 Do you wish to use FP16 or BF16 (mixed precision)?
100 bf16
101 ```
102 For instance, here is how you would run the NLP example `examples/nlp_example.py` (from the root of the repo) for distributed CPU training.
103
104 `default_config.yaml` which is generated by `accelerate config`
105 ```bash
106 compute_environment: LOCAL_MACHINE
107 distributed_type: MULTI_CPU
108 downcast_bf16: 'no'
109 machine_rank: 0
110 main_process_ip: 36.112.23.24
111 main_process_port: 29500
112 main_training_function: main
113 mixed_precision: bf16
114 mpirun_config:
115 mpirun_hostfile: /home/user/hostfile
116 num_machines: 4
117 num_processes: 16
118 rdzv_backend: static
119 same_network: true
120 tpu_env: []
121 tpu_use_cluster: false
122 tpu_use_sudo: false
123 use_cpu: true
124 ```
125
126 Set following env and using intel MPI to launch the training
127
128 In `node0`, you need to create a configuration file which contains the IP addresses of each node (for example hostfile) and pass that configuration file path as an argument.
129
130 If you selected to let Accelerate launch `mpirun`, ensure that the location of your hostfile matches the path in the config.
131
132 ```bash
133 $ cat hostfile
134 xxx.xxx.xxx.xxx #node0 ip
135 xxx.xxx.xxx.xxx #node1 ip
136 xxx.xxx.xxx.xxx #node2 ip
137 xxx.xxx.xxx.xxx #node3 ip
138 ```
139
140 ```bash
141 accelerate launch examples/nlp_example.py
142 ```
143
144 You can also directly launch distributed training with `mpirun` command, you need to run the following command in node0 and **16DDP** will be enabled in node0,node1,node2,node3 with BF16 mixed precision. When using this method, the python script, python environment, and accelerate config file need to be available on all of the machines used for multi-CPU training.
145
146 ```bash
147 export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip
148 mpirun -f hostfile -n 16 -ppn 4 accelerate launch examples/nlp_example.py
149 ```