Unsure how to write Gradient for this Method

I am trying to implement the backward / gradient pass for the following kernel.

It’s basically an image tensor of [3, H, W] which is split up as org_cloud_x_ptr, org_cloud_y_ptr, org_cloud_z_ptr. And I am iterating through it, checking its neighbourhood, and computing 6 additional tensors of which combined is [6, H, W] which is split up as dxdz_tensor.., which is basically the output of the method. The method is something like below:

  // Iterate Image
  for (auto yy = 0; yy < height; yy++) {
    for (auto xx = 0; xx < width; xx++) {

      const float bad_point = std::numeric_limits<float>::quiet_NaN();
      const int index = yy * width + xx;
      const float px = org_cloud_x_ptr[index];
      const float py = org_cloud_y_ptr[index];
      const float pz = org_cloud_z_ptr[index];

      // Kernel Local Neighbourhood search
      for (auto v = yy - search_size; v <= yy + search_size; v += step_size) {
        for (auto u = xx - search_size; u <= xx + search_size; u += step_size) {

          if (u < 0 || u >= width || v < 0 || v >= height)
            continue;

          const int index2 = v * width + u;

          const float qx = org_cloud_x_ptr[index2];
          const float qy = org_cloud_y_ptr[index2];
          const float qz = org_cloud_z_ptr[index2];

          if (std::isnan(qx) || qz == 0)
            continue;

          const float dz = qz - pz;
          const float dx = qx - px;
          const float dy = qy - py;


          dxdx_sum += f * dx * dx;
          dxdy_sum += f * dx * dy;
          dydy_sum += f * dy * dy;
          dxdz_sum += f * dx * dz;
          dydz_sum += f * dy * dz;
        }
      }

      // Set
      dxdz_tensor[index] =  dxdx_sum
      ...

    }
  }

How would I even start to think about how to write a backward pass / gradient for this?