Factorials that will work with cuda?

Is there a way to do factorials that will work with cuda? I can use scipy.special.factorial if using the CPU, but it gives an error when using cuda (because it’s using numpy under the hood).

Hi ccook!

Yes. The Euler gamma function is essentially the factorial function.
Because it grows so fast we often use the logarithm of the gamma
function for numerical work. Pytorch provides torch.lgamma(), a
full-featured, cuda-enabled, autograd-enabled pytorch tensor function.

Best.

K. Frank

Wow, excellent – this worked perfectly. Thank you.

(One note for future readers is that it’s torch.lgamma(k+1).exp() ~= factorial(k) – the +1 is needed).