Hi,
I am not sure what you are trying to do here, but mixing of .data (which is not a thing anymore) and detaching things inside a custom function is quite dangerous.
You should never use .data as a general rule. If you want to get a new Tensor with no history, you should use .detach().
save_for_backward should only be called with either inputs or outputs to the Function.
History is not tracked through the save_for_backward/saved_tensors, so you cannot do this and expect the grad call in your backward to work.
What are you trying to do here? Only to set some gradients for binary_gates even though it was not used in the run_func?