If you put your code between two lines with just three backticks, like this:
```
your python code goes here
```

then it will get formatted nicely.
Edited to add: I thought what you quoted was code, but it looks like it is not. I am not sure how to interpret all the non-prose text in your post. Perhaps you could explain what those lines mean?
Also, I think gradients cannot pass across torch.abs(), but I may be remembering this incorrectly.