To answer my own question, I compiled libtorch from source and reverse engineered the autogenerated functions (eg. torch::pow() which uses e.g. the PowBackward0 backward function).
Doing this I came up with the minimal working example:

  
      gist.github.com
  
  
    https://gist.github.com/mikehamer/df0af5ec7ff98d3cae487975d0c921df 76
pytorch_custom_backward.cpp
// An example of using the PyTorch C++ API to implement a custom forward and backward function

#include <iostream>
#include <vector>

#include <torch/torch.h>
#include <torch/csrc/autograd/variable.h>
#include <torch/csrc/autograd/function.h>
#include <torch/csrc/autograd/VariableTypeUtils.h>
#include <torch/csrc/autograd/functions/utils.h>
This file has been truncated. show original




  
  
    
    
  
  


To show a clean example, I cut out a lot of JIT stuff from the auto-generated examples, as well as checks and balances (asserts, error handling logic if wrong types are passed, etc).
I’d still be interested in someone’s input on how the whole autograd infrastructure works in C++. But in the meantime, I hope this example helps someone.