The function is implemented at it’s core in these two files:

  
      github.com
  
  
    pytorch/pytorch/blob/e33df2b88a31c08567c81f49c45f1eb530cd7ef4/aten/src/THNN/generic/SpatialGridSamplerBilinear.c 32
#ifndef TH_GENERIC_FILE
#define TH_GENERIC_FILE "generic/SpatialGridSamplerBilinear.c"
#else

#undef MIN
#define MIN(a,b) ( ((a)<(b)) ? (a) : (b) )
#undef MAX
#define MAX(a,b) ( ((a)>(b)) ? (a) : (b) )

#undef MODE_BORDER
#define MODE_BORDER 1

static inline void THNN_(SpatialGridSamplerBilinear_shapeCheck)
     (THTensor *input, THTensor *grid, THTensor *gradOutput) {
  THNN_ARGCHECK(input->nDimension == 4, 2, input,
    "4D input tensor expected but got: %s");
  THNN_ARGCHECK(grid->nDimension == 4, 2, grid,
    "4D grid tensor expected but got: %s");

  int nbatch   = THTensor_(size)(input, 0);


  This file has been truncated. show original

  
  
    
    
  
  



  
      github.com
  
  
    pytorch/pytorch/blob/e33df2b88a31c08567c81f49c45f1eb530cd7ef4/aten/src/THCUNN/SpatialGridSamplerBilinear.cu 17
#include "THCUNN.h"
#include "common.h"
#include "THCDeviceTensor.cuh"
#include "THCDeviceTensorUtils.cuh"
#include "THCDeviceUtils.cuh"
#include "THCHalf.h"
#include "THCHalfAutoNumerics.cuh"
#include "THCAtomics.cuh"

#define WITHIN_BOUNDS(x, y, H, W) (x >= 0 && x < W && y >= 0 && y < H)
#define SAFE_ADD(input, x, y, n, c, H, W, value)    \
  do {    \
    if (WITHIN_BOUNDS(x, y, H, W)) {    \
      atomicAdd(&input[n][c][y][x], value);   \
    }   \
  } while(0)

#undef MIN
#define MIN(a,b) ( ((a)<(b)) ? (a) : (b) )
#undef MAX


  This file has been truncated. show original

  
  
    
    
  
  


It is a relatively small task to extend this for 3D inputs. One has to make VolumetricGridSamplerBilinear.c / cu and call them from here: https://github.com/pytorch/pytorch/blob/1c0fbd27a10b7036b1db643014e65cae7f6d0266/torch/nn/_functions/vision.py#L52 24