Thank you for your reply!
Here is code:
import torch.multiprocessing as mp
import torch.nn.functional as F
from torch.cuda.amp import autocast, GradScaler

def task():
    for epoch in range(0, 100):
        trainer = Trainer()
        trainer.training()

class Trainer():
    def training(self, model, dataloader):
        train_loss = 0.0
        for i, (images, target) in enumerate(dataloader):
            with autocast():
                output = model(images)
                loss = criterion(output, target)
                train_loss += loss.item()
            GradScaler.scale(loss).backward()
            GradScaler.step(optimizer)
            GradScaler.update()

def criterion(predict, target):
    loss_0 = torch.tensor(0.0, device='0, 1')
    n, c, h, w = predict.size()
    for i in range(n):
        loss = F.nll_loss(predict, target, reduction='None').view(-1)
        loss = loss.clamp(min=0.0, max=1000.0) #RuntimeError: CUDA error: an illegal memory access was encountered
        loss_hard, _ = loss.topk(int(0.1 * h * w))
        loss_0 += loss_hard
    return loss_0 / n

if __name__ == '__main__':
    mp.spawn(task, nprocs, args)

My pytorch version is 1.7 and cuda version is 11.0.The reason I don’t update to the latest release is because loss function used to work fine until I trained with another larger dataset and this error shows accidently during training.
So is this about the scale of dataset? If not, may be other parts of  my codes is wrong.
Thank you!