Here, you have defined a model architecture with forward pass, but also added the loss function in the forward function. It is usually better to separate these two from each for code-readability. Once you define the forward function, PyTorch (autograd) will take care of the backward function.
A better way to do this, is to make one call that computes the output of your model, and then compute the loss based on that output.
Another thing, are you sure that you want loss=w*|x-y|, and not loss=|w*x - y|?