So in short you have an output, you pass that output through a blackbox and you get an scalar.
What you want is to backprop that scalar.
The easiest way is trying to write that blackbox in pytorch.
Else, (i think) you can write a module which does so but you would need to be able to calculate gradients of scalar wrt output.