libtorchに於いてleaf-tensorのgrad()を呼び出すと怒られる

 libtorchの公式サンプルをいじっていて、ネットワークのリーフノードの勾配を取得したらどうなるか試した所、以下のようなWarningが出た。

y.grad() = [W TensorBody.h:494] Warning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won't be populated during autograd.backward(). If you indeed want the .grad field to be populated for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. If you access the non-leaf Tensor by mistake, make sure you access the leaf Tensor instead. See github.com/pytorch/pytorch/pull/30531 for more informations. (function grad)

 ネットワークの終端でスカラー出力の場合、その勾配は1なので、1が出力されることを期待したのだがそうはならんらしい。
 backward()呼び出しの際にいい感じで処理するようにできているようだ。