Only record grad_fn in C++ Scatter and Gather when required so (#20286)
Summary:
C++ `Scatter` and `Gather` always set autograd history for input data tensors regardless whether they require grad. This hits assertion failure in `set_history(Tensor, shared_ptr<Function> grad_fn)`
where `grad_fn` cannot be nullptr. After this PR, C++ `Scatter` and `Gather` only record `grad_fn` when required.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/20286
Differential Revision: D15266610
Pulled By: mrshenli
fbshipit-source-id: 641df0ea36e7c922b5820c8dc3f83e2a050412b5