jeanfeydy / geomloss

Geometric loss functions between point clouds, images and volumes
MIT License
598 stars 60 forks source link

Sinkhorn loss always renables gradient tracking #57

Open mattcleigh opened 2 years ago

mattcleigh commented 2 years ago

Essentially whenever I calculate the sinkhorn loss, pytorch's internal gradient tracking turns back on.

I believe this is to do with line 280 in geomloss/sinkhorn_divergence.py: torch.autograd.set_grad_enabled(True)

This is obviously not an issue during training, but it is causing my memory to explode during inference or validation as the gradients are not cleared as there typically is no backwards pass.

I think this could be fixed by using something like: cur_grad_status = torch.is_grad_enabled() ## At beginning of sinkhorn_loop() torch.autograd.set_grad_enabled(cur_grad_status) ## At end of sinkhorn_loop()

lopsided commented 1 month ago

@jeanfeydy I just spent an hour or so coming to the same conclusion as to why I'm running out of memory during testing too. Looks like this PR would be a straight forward fix. Any chance you could merge it in? Thanks!