Solved by leowalkling in post #3 
              
              
                For reference, I’ve come up with a solution by imitating the generated kernels and the code generation 
from /tools/autograd. 
But it seems to me that it is a very intricate task, given that there is little documentation, so I made sure to follow the original code of builtin operations closely. 
The…