efficientvit icon indicating copy to clipboard operation
efficientvit copied to clipboard

FP16 training will occurs NaN problem after last code updates

Open xyzacademic opened this issue 2 years ago • 3 comments

I noticed that you remove the code

        if qkv.dtype == torch.float16:
            qkv = qkv.float()

in relu_linear_att and relu_quadratic_att function.

But it might have Nan output after state 4

xyzacademic avatar May 20 '24 05:05 xyzacademic

Hi Yunzhe,

You can use BF16 for mixed precision training. It does not have the NAN issue.

If your machines do not support BF16, you can add the code back to avoid the NAN issue:

 if qkv.dtype == torch.float16:
     qkv = qkv.float()

In addition, the following code can be removed in such case https://github.com/mit-han-lab/efficientvit/blob/1ccfc0caa4360f20e20fb948aab2d5d1992608eb/efficientvit/models/nn/ops.py#L427-L428

han-cai avatar May 20 '24 12:05 han-cai

Hi Yunzhe,

You can use BF16 for mixed precision training. It does not have the NAN issue.

If your machines do not support BF16, you can add the code back to avoid the NAN issue:

 if qkv.dtype == torch.float16:
     qkv = qkv.float()

In addition, the following code can be removed in such case

https://github.com/mit-han-lab/efficientvit/blob/1ccfc0caa4360f20e20fb948aab2d5d1992608eb/efficientvit/models/nn/ops.py#L427-L428

Thank you so much for clarifying it.

Would it be better to reserve the code that converts qkv to float in fp16 and out to float if out is bfloat16?

By the way, which precision the pre-trained model trained on? I guess it is fp16 mixed precision, right?

xyzacademic avatar May 20 '24 19:05 xyzacademic

Good idea! I have updated the code.

L models were trained with fp16. B models were trained with fp32.

han-cai avatar May 20 '24 20:05 han-cai