Hi,
I get the correct last output grad_x(z) = [2, 0, 0, 1].
I tested both the versions I had installed (~git checkout 2017-06-2x) and installed a fresh checkout of the master git (torch.__version__ is  0.1.12+4d5d9de from today, 2017-07-19).
I am on python3.5, though.
What is your torch.__version__? Maybe you had an unlucky checkout…
Best regards
Thomas