Hello,
apparently the problem is that I have to create a new instance of the custom function every time before applying to an input - otherwise, something in the backward computation goes wrong. So instead of saving the function as
self.m = multiply(s)
and calling the forward as
return self.m(input)
we should do directly
return multiply(s)(input)
Now it works!