Hi Alpha!



 AlphaBetaGamma96:

I’ve noticed that it actually scales worse than using torch.linalg.det


I doubt that your nested-loop version scales worse that det().  Your
version scales in time as n**2 (just count the number of times through
the loop), while det() scales as n**3.  Of course for moderate values
of n your python-loop version is indeed likely to be much slower than
det() because of the python, non-pipelined overhead.


Is there a way to parallelize the nested loop?


Yes.  With a little bit of monkeying around, you can perform the n**2
computation with pure, non-loop tensor operations:
>>> import torch
>>> torch.__version__
'1.9.0'
>>>
>>> _ = torch.manual_seed (2021)
>>>
>>> n = 5
>>>
>>> x = torch.randn (n)   # random x
>>>
>>> V = x.unsqueeze (1).expand (n, n)**torch.arange (n)   # vandermonde matrix
>>>
>>> torch.det (V)   # vandermonde determinant
tensor(2.2679)
>>>
>>> d = x - x.unsqueeze (1)   # "outer difference" -- x_j - x_i
>>>
>>> (torch.ones (n, n).tril() + d.triu (1)).prod()   # product of upper triangle of d  =  det (V)
tensor(2.2679)

Best.
K. Frank