What is @staticmethod?

In the docs it is recommended that when implementing forward and backward passes for a custom function, they be marked as @staticmethod. For example: https://pytorch.org/docs/stable/notes/extending.html.

What does the annotation @staticmethod do? I can’t find an explanation in the docs.

Static methods are methods not attached to a particular instance - so they do take a self as first argument. They’re not PyTorch-specific but a general Python thing:

PyTorch’s autograd Functions store state in a special context object (passed as the first argument) rather than the Function object, that is why they’re static. (In ancient times before PyTorch 0.2 this used to be different.)

Best regards


1 Like