Git Product home page Git Product logo

Comments (7)

sniklaus avatar sniklaus commented on July 17, 2024

The commit doesn't actually change the behavior, it just makes the code easier to read. rbot0 and rbot1 will be modified in place, so even though in the previous version we put the save_for_backward before populating rbot0 and rbot1, the saved tensors weren't just zeros. You can verify this using gradcheck for example. That is, change all float to double in the correlation.py and run the following code. Then change the location of save_for_backward and try again. The gradcheck will pass both times. As for why I don't use comments, I have the philosophic that if your code requires comments then the code itself isn't written well enough (just my opinion though, I am well aware that the readability could still be improved with comments).

import torch
import sys; sys.path.insert(0, './correlation'); import correlation

class Network(torch.nn.Module):
    def __init__(self):
        super().__init__()
    # end

    def forward(self, tenOne, tenTwo):
        return correlation.FunctionCorrelation(tenOne=tenOne, tenTwo=tenTwo)
    # end
# end

netTest = Network().cuda()

tenOne = torch.randn(2, 4, 10, 10).double().cuda().requires_grad_()
tenTwo = torch.randn(2, 4, 10, 10).double().cuda().requires_grad_()

torch.autograd.gradcheck(func=netTest, inputs=tuple([tenOne, tenTwo]))

from pytorch-pwc.

Etienne66 avatar Etienne66 commented on July 17, 2024

I'm not quite buying that theory that save_for_backward lets you modify the values afterwards. The reason I say this is that my epochs were taking 29 hours but after this change they are now taking 30 hours. Something is taking longer to calculate and this was the only change I made.

from pytorch-pwc.

sniklaus avatar sniklaus commented on July 17, 2024

I'm not quite buying that theory that save_for_backward lets you modify the values afterwards.

If you don't believe me then I encourage you to give it a try yourself. Print the average rbot0 in the backwards function and play around with the implementation. rbot0 (and rbot1) will be nonzero regardless of where save_for_backward is called but if you instead remove the two calls to kernel_Correlation_rearrange then rbot0 (and rbot1) will be zero.

from pytorch-pwc.

Etienne66 avatar Etienne66 commented on July 17, 2024

It isn't so much that I don't believe you. I know you have more experience with this than I do. I just don't understand why that change would be more computational if the value was allowed to be modified after the save_for_backward plus the default for new_zeros is requires_grad = False which is what is declared right before the original save_for_backward so a grad check on that tensor wouldn't show anything. Besides I always thought in place variable changes were the ones being passed in not on ones being passed out. It seems like save_for_backward would retain the value at the time it was called but I can't find a lot of documentation on it.

from pytorch-pwc.

Etienne66 avatar Etienne66 commented on July 17, 2024

the one thing I do see is that mark_dirty must be used to mark any input that is modified inplace by the forward function. It makes more sense if that is supposed to be used on rbot0 and rbot1. I didn't really understand the use of mark_dirty and I could not find a single example.

from pytorch-pwc.

sniklaus avatar sniklaus commented on July 17, 2024

I am afraid that I don't know what happens in PyTorch internally. Both versions of save_for_backward produce the same result though, so if one is faster then the other for you there is nothing that should hold you back form using the faster one. 🙂

from pytorch-pwc.

Etienne66 avatar Etienne66 commented on July 17, 2024

Well as long as you are sure. I still wonder why the computations are taking longer though. It certainly seemed like it made a big difference in my loss total as well. I'm a PL/SQL developer in my professional life and I'm still very new at Python and Pytorch. I appreciate all of the information @sniklaus

from pytorch-pwc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.