Because the gradient is zero almost everywhere and undefined otherwise. It’s the same reason why ceil(x) gives all zero gradients.



 Zichun_Zhang:

how actually did the back end kernel calculate the grad for each grid point? I just cant figure out, much thx


In forward, each output pixel is a linear interpolation (weighted sum) of input pixels, where the interpolation weights are computed using the mode and grid values. In most cases, this computation is simple and differentiable, e.g., bilinear. So on a high level, it goes like grad_output -> grad_interpolation_weights -> grad grid. (In real code there are a lot of other detailed optimizations.) If you are interested, CPU kernel is at https://github.com/pytorch/pytorch/blob/b039a715ce4e9cca82ae3bf72cb84652957b2844/aten/src/ATen/native/cpu/GridSamplerKernel.cpp 114 and GPU kernel is at https://github.com/pytorch/pytorch/blob/b039a715ce4e9cca82ae3bf72cb84652957b2844/aten/src/ATen/native/cuda/GridSampler.cu 84.