I am not sure if I understand correctly what you want to do. You have a loss value, and you want to change this loss value due to a rule, and then update the weights of the layers.
The update of the weights of the layers should be based on this new loss value ?
If this “rule” is a differentiable function it is no problem, especially if it is based on functions already implemented in pytorch. I would then suggest to just write your own custom loss function.