Pretraining BERT using torchrun on 3 GPUS

Naima_Vahab: <p>I am trying to run this code <a href="https://github.com/huggingface/transformers/blob/main/examples/pytorch/language-modeling/run_mlm.py" rel="noopener nofollow ugc">mlm_pretrain.py</a> using Slurm script, torch run in 3 gpu server. Since hf Trainer will by default do the DDP, I have not changed my code. The issue is using 3 gpus the training time is 3 times more than while pretraining on single GPU. It’s throwing this warning for each device id.</p>
<blockquote>
<p>[rank0]:[W reducer.cpp:1389] Warning: find_unused_parameters=True was specified in DDP constructor, but did not find any unused parameters in the forward pass. This flag results in an extra traversal of the autograd graph every iteration,  which can adversely affect performance.</p>
</blockquote>
<p>Slurm -<br>
<span class="hashtag-raw">#SBATCH</span> --gres=gpu:3<br>
<span class="hashtag-raw">#SBATCH</span> --cpus-per-task=16<br>
<span class="hashtag-raw">#SBATCH</span> --mem=32G<br>
<span class="hashtag-raw">#SBATCH</span> --qos=normal<br>
torchrun --nproc-per-node 3 train_continual_with_loss.py</p>
<p>Detailed log</p>
<blockquote>
<p>Detected kernel version 5.4.0, which is below the recommended minimum of 5.5.0; this can cause the process to hang. It is recommended to upgrade the kernel to the minimum version or higher.<br>
0%|          | 0/307 [00:00&lt;?, ?it/s][rank0]:[W reducer.cpp:1389] Warning: find_unused_parameters=True was specified in DDP constructor, but did not find any unused parameters in the forward pass. This flag results in an extra traversal of the autograd graph every iteration,  which can adversely affect performance. If your model indeed never has any unused parameters in the forward pass, consider turning this flag off. Note that this warning may be a false positive if your model has flow control causing later iterations to have unused parameters. (function operator())<br>
[rank2]:[W reducer.cpp:1389] Warning: find_unused_parameters=True was specified in DDP constructor, but did not find any unused parameters in the forward pass. This flag results in an extra traversal of the autograd graph every iteration,  which can adversely affect performance. If your model indeed never has any unused parameters in the forward pass, consider turning this flag off. Note that this warning may be a false positive if your model has flow control causing later iterations to have unused parameters. (function operator())<br>
[rank1]:[W reducer.cpp:1389] Warning: find_unused_parameters=True was specified in DDP constructor, but did not find any unused parameters in the forward pass. This flag results in an extra traversal of the autograd graph every iteration,  which can adversely affect performance. If your model indeed never has any unused parameters in the forward pass, consider turning this flag off. Note that this warning may be a false positive if your model has flow control causing later iterations to have unused parameters. (function operator())<br>
11%|█▏        | 35/307 [43:31&lt;5:43:06, 75.69s/it</p>
</blockquote>

ptrblck: <p>Did you try to disable <code>find_unused_parameters</code> by setting it to <code>False</code> or by dropping it? If so, how did the performance improve?</p>

Naima_Vahab: <p>I have not changed any lines for distributed run in the mlm code; since HF trainer is handling those with torchrun. Since this error appeared, i tried to wrap the model in DDP with find_unused_parameters = False; but it was throwing index error, might be due to I have to wrap datasets in ddp format and have to make other code changes.<br>
What is the ideal way to set find_unused_parameters=False, when using torchrun with HF trainer with minimal code changes</p>

ptrblck: <p>I’m not familiar enough with HF’s <code>Trainer</code> class and don’t know where these arguments are set.</p>

Naima_Vahab: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a> . I have figured out that. I was doing in a wrong way by wrapping model into DDP, all I have to do was adding this argument to Trianing arguments of HF like this which has significantly reduced the time in gpus</p>
<blockquote>
<p>training_args = TrainingArguments(<br>
output_dir=output_dir,<br>
overwrite_output_dir=True,<br>
num_train_epochs=5,<br>
per_device_train_batch_size=32,<br>
per_device_eval_batch_size=32,<br>
do_eval=True,<br>
eval_strategy = ‘epoch’,<br>
gradient_accumulation_steps=4,<br>
prediction_loss_only=True,<br>
<strong>ddp_find_unused_parameters=False</strong><br>
)</p>
</blockquote>