Hi,
The first link your posted is about extending the autograd engine for things it does not support.
Everything you need here is supported, so no need for you to implement any backward 
Usually, given a batch of samples, your model will give you one output score (?) for each sample (the oi).
Then you can write a function that given these, will compute the scalar loss on that batch.
For the first one, that would be:
def my_loss(outs):
  return (outs.max() - out.max()) ** 2

Then you can do as usual:
outs = model(inp)
loss = my_loss(outs)
opt.zero_grad()
loss.backward()
opt.step()