Thanks for the answer K. Frank.
I will try to tinker with this solution in a few hours.
Do you happen to know if torch.where breaks the gradent tree? I am looking for a differentiable function even if I know that for large values of p, the gradient will be strangled.