getkeops / keops

KErnel OPerationS, on CPUs and GPUs, with autodiff and without memory overflows
https://www.kernel-operations.io
MIT License
1.04k stars 64 forks source link

Odd behavior : parameters in reduction formula with symbolic variables ? #282

Closed AdrienWohrer closed 1 year ago

AdrienWohrer commented 1 year ago

Hi, and first of all thank you for this wonderful tool you have designed !

I am new to KeOps. When playing with pykeops, I tried to implement the following sum_reduction, over the gradient of a Gaussian kernel (by the way, I realize that it could be done more easily with autograd, but it's not my point here) :

def GaussGradKernel(gam,D): x, y = Vi(0, D), Vj(1, D) D2 = x.sqdist(y) K = (-D2*gam/2).exp() yo = -gam*K*(x-y) return yo.sum_reduction(axis=1)

Here, gam is a regular Python float, and D a regular Python int. When I call G=GaussGradKernel(1.0,3), it succesfully creates a reduction formula, that I can then apply to any inputs x and y : G(x,y).

Now to my question. In the following code, the only difference is line 4, the additional parenthesis over gam/2.

def GaussGradKernel(gam,D): x, y = Vi(0, D), Vj(1, D) D2 = x.sqdist(y) K = (-D2*(gam/2)).exp() yo = -gam*K*(x-y) return yo.sum_reduction(axis=1)

Oddly, this new code produces an error when I call the function G(x,y) :

Traceback (most recent call last): File "........./tests.py", line 165, in res=G(xt,yt) File "/home/adrien/.local/lib/python3.9/site-packages/pykeops/common/lazy_tensor.py", line 919, in call self.callfun = self.Genred( File "/home/adrien/.local/lib/python3.9/site-packages/pykeops/torch/generic/generic_red.py", line 485, in init self.aliases = complete_aliases( File "/home/adrien/.local/lib/python3.9/site-packages/pykeops/common/parse_type.py", line 40, in complete_aliases aliases[newpos[i]] = extravars[newind[i]] IndexError: list assignment index out of range

Where does this error come from ? I think that it happens only with symbolic variables. When x and y are basic, numeric LazyTensors with an actual content, the same code succeeds.

Thanks in advance for the explanations, Adrien

AdrienWohrer commented 1 year ago

Thanks, your update fixed all the issues I had with parameters ! (Here and at other places.)