Skip to content

[consistent use] F vs. nn.functional #11600

@stas00

Description

@stas00

We use 3 different ways of doing the same:

  1. F.foo()
  2. nn.functional.foo()
  3. torch.nn.functional.foo()

and these could also be imported:

  1. from torch.nn.functional import foo; foo()

Asking others it appears that F is not quite liked, so it's 2, 3 or 4.

2 and 3 often lead to longer lines which autoformatter wraps, leading to 3 lines of code instead of 1 and which gives less readable code.

So it seems that option 4 might be the best outcome.

For 2, the global update would be easy:

find . -type d -name ".git" -prune -o -type f -exec perl -pi -e 's|from torch.nn import functional as F||'  {} \;
find . -type d -name ".git" -prune -o -type f -exec perl -pi -e 's|import torch.nn.functional as F||'  {} \;
find . -type d -name ".git" -prune -o -type f -exec perl -pi -e 's| F\.| nn.functional.|g'  {} \;
make fixup

For 4, it will take much more work, but can be semi-automated.

@LysandreJik, @sgugger, @patrickvonplaten

Metadata

Metadata

Assignees

Labels

WIPLabel your PR/Issue with WIP for some long outstanding Issues/PRs that are work in progress

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions