Variables have requires_grad=True by default.
What you want to do, if you’re doing MNIST classification, is take the output and labels and compute the CrossEntropyLoss. This will be a scalar variable. You can now do loss.backward() and it will backprop i.e. compute gradients back through to the inputs.
If you want gradOut or gradInp of a layer check out register_hook function.