You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AttributeError: 'NoneType' object has no attribute 'type' with overlap_comm=True and zero 2 #684
When I train the model with zero 2 and overlap_comm=True on one single P4d instance, I received the following error:
10.0.89.152: File "/usr/local/lib64/python3.7/site-packages/deepspeed/runtime/engine.py", line 850, in backward
10.0.89.152: self.allreduce_gradients()
10.0.89.152: File "/usr/local/lib64/python3.7/site-packages/deepspeed/runtime/engine.py", line 770, in allreduce_gradients
10.0.89.152: self.optimizer.overlapping_partition_gradients_reduce_epilogue()
10.0.89.152: File "/usr/local/lib64/python3.7/site-packages/deepspeed/runtime/zero/stage2.py", line 581, in overlapping_partition_gradients_reduce_epilogue
10.0.89.152: self.independent_gradient_partition_epilogue()
10.0.89.152: File "/usr/local/lib64/python3.7/site-packages/deepspeed/runtime/zero/stage2.py", line 470, in independent_gradient_partition_epilogue
10.0.89.152: self.reduce_ipg_grads()
10.0.89.152: File "/usr/local/lib64/python3.7/site-packages/deepspeed/runtime/zero/stage2.py", line 954, in reduce_ipg_grads
10.0.89.152: elements_per_buffer=self.elements_in_ipg_bucket)
10.0.89.152: File "/usr/local/lib64/python3.7/site-packages/deepspeed/runtime/zero/stage2.py", line 1123, in buffered_reduce_fallback
10.0.89.152: split_buckets = split_half_float_double(grads)
10.0.89.152: File "/usr/local/lib64/python3.7/site-packages/deepspeed/runtime/zero/stage2.py", line 40, in split_half_float_double
10.0.89.152: bucket = [t for t in tensors if t.type() == dtype]
10.0.89.152: File "/usr/local/lib64/python3.7/site-packages/deepspeed/runtime/zero/stage2.py", line 40, in <listcomp>
10.0.89.152: bucket = [t for t in tensors if t.type() == dtype]
10.0.89.152: AttributeError: 'NoneType' object has no attribute 'type'
After some debugging, I found that it is due to that deepspeed does not handle tied parameters properly. In my example, I use tied weight for the embedding and output layers. There are two issues:
If I am right about what I mentioned above. This also results in wrong aggregated gradients when we use zero 2 and gradient accumulation. Suppose that the size of gradient accumulation is 3, then the final aggregated gradient is 1/n^3\sum_ig_{t-2, i} + 1/n^2\sum_ig_{t-1, i} + 1/n\sum_ig_{t, i}.
@szhengac, apologies for not responding to this earlier. I certainly see the potential issues you have raised with ZeRO-2. Specifically, the handling of tied weights. Can you share repro steps on this as well?
@tjruwase Apart from the tied weights, this also seems problematic to me when we use gradient accumulation. As I mentioned above the final aggregated gradient is 1/n^3\sum_ig_{t-2, i} + 1/n^2\sum_ig_{t-1, i} + 1/n\sum_ig_{t, i} for an accumulation size of 3 based on the current zero-2 logic. We don't have the problem without the gradient partition just like what apex is doing.
@szhengac I definitely also see the issue with gradient accumulation, which is a challenging for ZeRO-2 in general. But I think we should start first with tied weights issue.
@tjruwase To reproduce the Nonetype error for the tied parameters, you can use bing_bert in DeepSpeedExamples on a single node. I use script ds_train_bert_nvidia_data_bsz64k_seq128.sh and add
@szhengac, thanks for keeping this alive. I suspect the same issues with weight tying applies to ZeRO3. With the release completed, I am now looking into deepspeed's support for tied weights.
@szhengac, unfortunately I am not able to easily repro this AttributeError with the bing_bert even after reverting #728 in my branch. More specifically, my run has completed 1 epoch without the error, which means the multiple gradient reduction steps has already occurred.
Can you give more details on your bing_bert repro? In my zero_optimization config, I set only stage=2 and overlap_comm=True
@szhengac, just to follow up. The AttributeError is not due to tied weights. Rather, it is due to the fact that when overlap_comm=True, and we are overlapping reduction with backward, we delay the clearing of the currently-reducing gradients of other partitions until the next reduction operation. But, we were failing to ensure that the last set of reduced gradients are cleared before the start of the next backward. The bug is then triggered by the right (or wrong) combination of reduce_bucket_size and model size. It is now fixed by #856.
However, I am not sure if this address your other concerns regarding the general handling of tied weights, including gradient accumulation. So could you please check again whether they are now addressed? If not perhaps, you can open a new issue, to keep things nicely separated. Thanks.
@tjruwase Thanks for getting back to me. I just read your PR. So what you observed is that the error occurs in the next iteration rather than the current iteration where the hook is called the second time?
@tjruwase I just read the zero2 code carefully again. I realized there is an additional averaged_gradients buffer. With this, we do not have problem with gradient accumulation. But for tied weights, I am not sure yet.
@tjruwase I just did some testing. It seems that Pytorch defers the hook call for tied parameters to the first time they are used in the computational graph, so hook is actually only called once. I am closing this issue. Thanks for providing the fix.
Activity
szhengac commentedon Jan 21, 2021
After some debugging, I found that it is due to that deepspeed does not handle tied parameters properly. In my example, I use tied weight for the embedding and output layers. There are two issues:
when the tied parameters are also in
self.previous_reduced_gradsand their gradients are set to None as inhttps://github.com/microsoft/DeepSpeed/blob/e59ba12d4df2e61e85abbd2c82a6097ac3722d7a/deepspeed/runtime/zero/stage2.py#L1089.
szhengac commentedon Jan 26, 2021
If I am right about what I mentioned above. This also results in wrong aggregated gradients when we use zero 2 and gradient accumulation. Suppose that the size of gradient accumulation is 3, then the final aggregated gradient is
1/n^3\sum_ig_{t-2, i} + 1/n^2\sum_ig_{t-1, i} + 1/n\sum_ig_{t, i}.szhengac commentedon Jan 26, 2021
@ShadenSmith Hi Shaden, would anyone in the deepspeed team be able to confirm if I am right about it? Thanks.
tjruwase commentedon Feb 17, 2021
@szhengac, apologies for not responding to this earlier. I certainly see the potential issues you have raised with ZeRO-2. Specifically, the handling of tied weights. Can you share repro steps on this as well?
szhengac commentedon Feb 17, 2021
@tjruwase Apart from the tied weights, this also seems problematic to me when we use gradient accumulation. As I mentioned above the final aggregated gradient is
1/n^3\sum_ig_{t-2, i} + 1/n^2\sum_ig_{t-1, i} + 1/n\sum_ig_{t, i}for an accumulation size of 3 based on the current zero-2 logic. We don't have the problem without the gradient partition just like what apex is doing.tjruwase commentedon Feb 17, 2021
@szhengac I definitely also see the issue with gradient accumulation, which is a challenging for ZeRO-2 in general. But I think we should start first with tied weights issue.
szhengac commentedon Feb 19, 2021
@tjruwase To reproduce the Nonetype error for the tied parameters, you can use bing_bert in DeepSpeedExamples on a single node. I use script
ds_train_bert_nvidia_data_bsz64k_seq128.shand addto
deepspeed_bsz64k_lamb_config_seq128.json. I use the following model config inbert_large_lamb_nvidia_data.jsonAlso, you may want to notice a recent PR for fixing Nonetype in a wrong way: #728. With such fix, we will lose the gradient for the embedding layers.
tjruwase commentedon Feb 19, 2021
@szhengac Thanks for sharing these repro steps. I will look into it.
Also, thanks for sharing feedback on #728, I will check into it.
szhengac commentedon Mar 9, 2021
@tjruwase I realized that ZeRO 3 has come out. I wonder if the issues mentioned here also exist in ZeRO 3?
tjruwase commentedon Mar 9, 2021
@szhengac, thanks for keeping this alive. I suspect the same issues with weight tying applies to ZeRO3. With the release completed, I am now looking into deepspeed's support for tied weights.
tjruwase commentedon Mar 9, 2021
@szhengac, unfortunately I am not able to easily repro this AttributeError with the bing_bert even after reverting #728 in my branch. More specifically, my run has completed 1 epoch without the error, which means the multiple gradient reduction steps has already occurred.
Can you give more details on your bing_bert repro? In my zero_optimization config, I set only stage=2 and overlap_comm=True
szhengac commentedon Mar 9, 2021
@tjruwase Do you use the model config that has
"num_hidden_layers": 38I give to you?tjruwase commentedon Mar 9, 2021
@szhengac , oops, sorry I missed that part. I will do so now.
tjruwase commentedon Mar 9, 2021
@szhengac, I have repro'd successfully. Thanks so much.
tjruwase commentedon Mar 12, 2021
@szhengac, just to follow up. The
AttributeErroris not due to tied weights. Rather, it is due to the fact that when overlap_comm=True, and we are overlapping reduction with backward, we delay the clearing of the currently-reducing gradients of other partitions until the next reduction operation. But, we were failing to ensure that the last set of reduced gradients are cleared before the start of the next backward. The bug is then triggered by the right (or wrong) combination ofreduce_bucket_sizeand model size. It is now fixed by #856.However, I am not sure if this address your other concerns regarding the general handling of tied weights, including gradient accumulation. So could you please check again whether they are now addressed? If not perhaps, you can open a new issue, to keep things nicely separated. Thanks.
szhengac commentedon Mar 12, 2021
@tjruwase Thanks for getting back to me. I just read your PR. So what you observed is that the error occurs in the next iteration rather than the current iteration where the hook is called the second time?
tjruwase commentedon Mar 12, 2021
That is correct.
szhengac commentedon Mar 13, 2021
@tjruwase I just read the zero2 code carefully again. I realized there is an additional
averaged_gradientsbuffer. With this, we do not have problem with gradient accumulation. But for tied weights, I am not sure yet.szhengac commentedon Mar 13, 2021
@tjruwase I just did some testing. It seems that Pytorch defers the hook call for tied parameters to the first time they are used in the computational graph, so hook is actually only called once. I am closing this issue. Thanks for providing the fix.