If you want to normalize a vector as a part of a model, this should do it:
assume q is the tensor to be L2 normalized, along dim 1
qn = torch.norm(q, p=2, dim=1).detach()
q = q.div(qn.expand_as(q))
detach(), that is essential for the gradients to work correctly. I'm assuming you want the norm to be treated as a constant while dividing the Tensor with it.