If you want to train the gradient to be low by minimizing it as an objective, retrieve the gradient by torch.autograd.grad then minimize its norm.
But if you want to block gradients for certain positions, then you should detach the positions where you don’t want the gradient to be accumulated.
I don’t know why you need to block the gradient for the same token, since the embedding layer is designed to receive different information from multiple positions.