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:
https://docs.python.org/3.5/library/functions.html#staticmethod

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

Thomas

1 Like