Repository navigation
Description
I want to make some testing about mix-precision, and all I did is change the following line in run_squard.py:
model, optimizer = amp.initialize(model, optimizer, opt_level="O2", keep_batchnorm_fp32=False, loss_scale="dynamic")
to:
model, optimizer = amp.initialize(model, optimizer, opt_level="O1", loss_scale="dynamic")
Then I execute the command:
bash scripts/run_squad.sh results/DLE_BERT_FP16_PyT_LAMB_92_hard_scaling_node.pt
Here is the error information I encountered:
`root@3a1199bf35c5:/workspace/bert# bash scripts/run_squad.sh results/DLE_BERT_FP16_PyT_LAMB_92_hard_scaling_node.pt
Container nvidia build = 7741716
out dir is /workspace/bert/results/SQuAD
fp16 activated!
python run_squad.py --init_checkpoint=results/DLE_BERT_FP16_PyT_LAMB_92_hard_scaling_node.pt --do_train --train_file=/workspace/bert/data/download/squad/v1.1/train-v1.1.json --train_batch_size=3 --do_predict --predict_file=/workspace/bert/data/download/squad/v1.1/dev-v1.1.json --predict_batch_size=3 --do_lower_case --bert_model=bert-large-uncased --learning_rate=3e-5 --seed=1 --num_train_epochs=2.0 --max_seq_length=384 --doc_stride=128 --output_dir=/workspace/bert/results/SQuAD --vocab_file=/workspace/bert/data/download/google_pretrained_weights/uncased_L-24_H-1024_A-16/vocab.txt --config_file=/workspace/bert/bert_config.json --max_steps=-1 --fp16 |& tee /workspace/bert/results/SQuAD/logfile.txt
11/12/2019 11:30:29 - INFO - main - device: cuda n_gpu: 1, distributed training: False, 16-bits training: True
11/12/2019 11:31:01 - INFO - main - ***** Running training *****
11/12/2019 11:31:01 - INFO - main - Num orig examples = 87599
11/12/2019 11:31:01 - INFO - main - Num split examples = 88641
11/12/2019 11:31:01 - INFO - main - Batch size = 3
11/12/2019 11:31:01 - INFO - main - Num steps = 58398
Epoch: 0%| | 0/2 [00:00<?, ?it/s]
WARNING: Output directory /workspace/bert/results/SQuAD already exists and is not empty. ['nbest_predictions.json', 'predictions.json', 'logfile.txt', 'bert_config.json', 'pytorch_model.bin']
LOADING CHECKOINT
LOADED CHECKPOINT
Selected optimization level O1: Insert automatic casts around Pytorch functions and Tensor methods.
Defaults for this optimization level are:
enabled : True
opt_level : O1
cast_model_type : None
patch_torch_functions : True
keep_batchnorm_fp32 : None
master_weights : None
loss_scale : dynamic
Processing user overrides (additional kwargs that are not None)...
After processing overrides, optimization options are:
enabled : True
opt_level : O1
cast_model_type : None
patch_torch_functions : True
keep_batchnorm_fp32 : None
master_weights : None
loss_scale : dynamic
Traceback (most recent call last):
File "run_squad.py", line 1159, in
main()
File "run_squad.py", line 1058, in main
scaled_loss.backward()
File "/opt/conda/lib/python3.6/site-packages/torch/tensor.py", line 118, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/opt/conda/lib/python3.6/site-packages/torch/autograd/init.py", line 93, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: Function torch::jit::(anonymous namespace)::DifferentiableGraphBackward returned an invalid gradient at index 1 - expected type torch.cuda.HalfTensor but got torch.cuda.FloatTensor
`
In my understanding, apex should handle all the type converting. I am confused why this error happened? Could you please help to give some insights? Thanks!
Activity
sharathts commentedon Nov 12, 2019
The FusedAdam works with O2 only at the moment. If you want to try O1(which gives you less performance gains compared to O2) you can use BertAdam or BertLAMB.
guoshzhao commentedon Nov 13, 2019
Thanks @sharathts .
I followed your suggestions and also replaced the following lines in run_squad.py:
optimizer = FusedAdam(optimizer_grouped_parameters, lr=args.learning_rate, bias_correction=False, max_grad_norm=1.0)to
optimizer = BertAdam(optimizer_grouped_parameters, lr=args.learning_rate, warmup=args.warmup_proportion, t_total=num_train_optimization_steps)or
optimizer = BertLAMB(optimizer_grouped_parameters, lr=args.learning_rate, warmup=args.warmup_proportion, t_total=num_train_optimization_steps)Unfortunately, the similar error appear again:
`root@3a1199bf35c5:/workspace/bert# bash scripts/run_squad.sh results/DLE_BERT_FP16_PyT_LAMB_92_hard_scaling_node.pt
Container nvidia build = 7741716
out dir is /workspace/bert/results/SQuAD
fp16 activated!
python run_squad.py --init_checkpoint=results/DLE_BERT_FP16_PyT_LAMB_92_hard_scaling_node.pt --do_train --train_file=/workspace/bert/data/download/squad/v1.1/train-v1.1.json --train_batch_size=3 --do_predict --predict_file=/workspace/bert/data/download/squad/v1.1/dev-v1.1.json --predict_batch_size=3 --do_lower_case --bert_model=bert-large-uncased --learning_rate=3e-5 --seed=1 --num_train_epochs=2.0 --max_seq_length=384 --doc_stride=128 --output_dir=/workspace/bert/results/SQuAD --vocab_file=/workspace/bert/data/download/google_pretrained_weights/uncased_L-24_H-1024_A-16/vocab.txt --config_file=/workspace/bert/bert_config.json --max_steps=-1 --fp16 |& tee /workspace/bert/results/SQuAD/logfile.txt
11/13/2019 03:46:11 - INFO - main - device: cuda n_gpu: 1, distributed training: False, 16-bits training: True
11/13/2019 03:46:43 - INFO - main - ***** Running training *****
11/13/2019 03:46:43 - INFO - main - Num orig examples = 87599
11/13/2019 03:46:43 - INFO - main - Num split examples = 88641
11/13/2019 03:46:43 - INFO - main - Batch size = 3
11/13/2019 03:46:43 - INFO - main - Num steps = 58398
Epoch: 0%| | 0/2 [00:00<?, ?it/s]
WARNING: Output directory /workspace/bert/results/SQuAD already exists and is not empty. ['nbest_predictions.json', 'predictions.json', 'logfile.txt', 'bert_config.json', 'pytorch_model.bin']
LOADING CHECKOINT
LOADED CHECKPOINT
Selected optimization level O1: Insert automatic casts around Pytorch functions and Tensor methods.
Defaults for this optimization level are:
enabled : True
opt_level : O1
cast_model_type : None
patch_torch_functions : True
keep_batchnorm_fp32 : None
master_weights : None
loss_scale : dynamic
Processing user overrides (additional kwargs that are not None)...
After processing overrides, optimization options are:
enabled : True
opt_level : O1
cast_model_type : None
patch_torch_functions : True
keep_batchnorm_fp32 : None
master_weights : None
loss_scale : dynamic
Traceback (most recent call last):
File "run_squad.py", line 1170, in
main()
File "run_squad.py", line 1069, in main
scaled_loss.backward()
File "/opt/conda/lib/python3.6/site-packages/torch/tensor.py", line 118, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/opt/conda/lib/python3.6/site-packages/torch/autograd/init.py", line 93, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: Function torch::jit::(anonymous namespace)::DifferentiableGraphBackward returned an invalid gradient at index 1 - expected type torch.cuda.HalfTensor but got torch.cuda.FloatTensor
`
Did I miss something?
sharathts commentedon Nov 13, 2019
You will have also have to remove
@torch.jit.scripthappening here https://github.com/NVIDIA/DeepLearningExamples/blob/master/PyTorch/LanguageModeling/BERT/modeling.py#L119-L128Note: Not doing JIT will cost additional drop in performance, around ~15%.
guoshzhao commentedon Nov 14, 2019
It works. Thanks @sharathts for the detailed instructions and the note. I am doing some performance testing about mix-precision between pytorch and tensorflow. From the principles and performance number, looks the pytorch's amp with "opt_level=O1"is similar with tensorflow's amp.