Repository: pytorch
Issue: When calculating the loss, the input data does not contain NaN, but the output contains NaN.
Labels: triaged,module: NaNs and Infs

### 🐛 Describe the bug

Please specify `cuda:0` at the very beginning.

```python
import torch
import numpy as np
import os


if "CONTEXT_DEVICE_TARGET" in os.environ and os.environ['CONTEXT_DEVICE_TARGET'] == 'GPU':
    devices = os.environ['CUDA_VISIBLE_DEVICES'].split(",")
    device = devices[-2]
    final_device = "cuda:" + device
else:
    final_device = 'cpu'


def loss_yolo_torch():
    from yolov4.yolov4_pytorch import yolov4loss_torch
    return yolov4loss_torch()

y_true_0 = np.load('./yolo_out[0][0].npy')
yolo_out1 = torch.from_numpy(y_true_0).to(final_device)
y_true_0 = np.load('./yolo_out[0][1].npy')
yolo_out2 = torch.from_numpy(y_true_0).to(final_device)
y_true_0 = np.load('./yolo_out[0][2].npy')
yolo_out3 = torch.from_numpy(y_true_0).to(final_device)
y_true_0 = np.load('./yolo_out[1][0].npy')
yolo_out4 = torch.from_numpy(y_true_0).to(final_device)
y_true_0 = np.load('./yolo_out[1][1].npy')
yolo_out5 = torch.from_numpy(y_true_0).to(final_device)
y_true_0 = np.load('./yolo_out[1][2].npy')
yolo_out6 = torch.from_numpy(y_true_0).to(final_device)
y_true_0 = np.load('./yolo_out[2][0].npy')
yolo_out7 = torch.from_numpy(y_true_0).to(final_device)
y_true_0 = np.load('./yolo_out[2][1].npy')
yolo_out8 = torch.from_numpy(y_true_0).to(final_device)
y_true_0 = np.load('./yolo_out[2][2].npy')
yolo_out9 = torch.from_numpy(y_true_0).to(final_device)

yolo_out = ((yolo_out1,yolo_out2,yolo_out3),(yolo_out4,yolo_out5,yolo_out6),(yolo_out7,yolo_out8,yolo_out9))


y_true_0 = np.load('./y_true_0.npy')
y_true_0 = torch.from_numpy(y_true_0).to(final_device)

y_true_1 = np.load('./y_true_1.npy')
y_true_1 = torch.from_numpy(y_true_1).to(final_device)

y_true_2 = np.load('./y_true_2.npy')

y_true_2 = torch.from_numpy(y_true_2).to(final_device)


gt_0 = np.load('./gt_0.npy')
gt_0 = torch.from_numpy(gt_0).to(final_device)

gt_1 = np.load('./gt_1.npy')
gt_1 = torch.from_numpy(gt_1).to(final_device)


gt_2 = np.load('./gt_2.npy')
gt_2 = torch.from_numpy(gt_2).to(final_device)

input_shape_t = np.load('./input_shape_t.npy')
input_shape_t = torch.from_numpy(input_shape_t).to(final_device)

loss_torch = loss_yolo_torch()


loss_torch_result = loss_torch(yolo_out, y_true_0, y_true_1, y_true_2, gt_0, gt_1, gt_2, input_shape_t)


yolo_out1 = torch.isnan(yolo_out1).any()
print('yolo_out1;',yolo_out1)  
yolo_out2 = torch.isnan(yolo_out2).any()
print('yolo_out2;',yolo_out2) 
yolo_out3 = torch.isnan(yolo_out3).any()
print('yolo_out3;',yolo_out3)  
yolo_out4 = torch.isnan(yolo_out4).any()
print('yolo_out4;',yolo_out4) 
yolo_out5 = torch.isnan(yolo_out5).any()
print('yolo_out5;',yolo_out5)  
yolo_out6 = torch.isnan(yolo_out6).any()
print('yolo_out6;',yolo_out6) 
yolo_out7 = torch.isnan(yolo_out7).any()
print('yolo_out7;',yolo_out7)
yolo_out8 = torch.isnan(yolo_out8).any()
print('yolo_out8;',yolo_out8) 
yolo_out9 = torch.isnan(yolo_out9).any()
print('yolo_out9;',yolo_out9) 
y_true_0 = torch.isnan(y_true_0).any()
print('y_true_0;',y_true_0) 
y_true_1 = torch.isnan(y_true_1).any()
print('y_true_1;',y_true_1)  
y_true_2 = torch.isnan(y_true_2).any()
print('y_true_2;',y_true_2) 
gt_0 = torch.isnan(gt_0).any()
print('gt_0;',gt_0) 
gt_1 = torch.isnan(gt_1).any()
print('gt_1;',gt_1) 
gt_2 = torch.isnan(gt_2).any()
print('gt_2;',gt_2) 
input_shape_t = torch.isnan(input_shape_t).any()
print('input_shape_t;',input_shape_t)  

loss_torch_result = torch.isnan(loss_torch_result).any()
print('loss_torch_result;',loss_torch_result)  

```
![屏幕截图 2024-09-13 202932](https://github.com/user-attachments/assets/6bfb465d-1923-42ce-866d-30e190bb77f9)


### Versions

Code and data links:https://drive.google.com/file/d/1MT_0Tq1koITqg9pnn-po1zVE8bDCxWvj/view?usp=sharing