Hi,
If you have a that f, x1 and x2 defined (and requiring gradients), you can do:
out = f(x1, x2)
df_dx1, df_dx2 = autograd.grad(out, (x1, x2), create_graph=True)

loss = some_criterion(df_dx1 + 2 * df_dx2)
loss.backward()

Note that out has to be a single value for this to work, otherwise getting the full matrix of all the gradients will be much more expensive.