Using the .data attribute is not recommended and might yield unwanted side effects, so this line of code might be dangerous:
weight.data = self._pos(weight).data

You could probably use .copy_ and wrap it in a with torch.no_grad() block, if Autograd raises an issue.
Besides that I cannot see any obvious errors.