Could you check the type and shape of sum(trg_loss)?
If trg_loss contains tensors, sum(trg_loss) should return another tensor, which shouldn’t raise the error on calling detach().
However, if new_loss is an int, then this won’t work and you would have to create a tensor first or use the list entries directly. Note that creating a tensor of Ints, will not contain any Autograd history, so that you wouldn’t need to call detach() at all.