I met the same question as you do ,but I solved this problem by this way
ids, count = nms(boxes.detach(), scores.detach(), nms_thresh, top_k)

in this way ,you can use it in training and testing without revising the nms function