Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move all weight initializations from private methods to constructors #5331

Merged
merged 7 commits into from
Feb 2, 2022

Conversation

datumbox
Copy link
Contributor

@datumbox datumbox commented Feb 1, 2022

The main idiom for weight initialisation in TorchVision is to do it within constructors. Nevertheless, there is limited use of another idiom of placing the initialization in a separate private method and calling it from constructor. Though the two can be considered equivalent (the method is private after all), the use of the latter idiom can lead to gotchas.

It's worth noting that TorchVision users have the appetite for being able to re-initialize the weights (see #3410) and thus some might be tempted to call the private method directly. Unfortunately having such a method currently stumbles upon fundamental FX limitations and can lead to inheritance issues. Thus there are cases (for example after using TorchVision's Feature Extraction util) where if one calls the private method to reinitialize the model, the code won't work or even break. By moving the initialization within the constructor, we make it impossible for someone to shoot themselves in the foot. Until proper support is added for this feature in PyTorch (see pytorch/pytorch#71404), TorchVision should use idioms that minimize the chance of gotchas.

@facebook-github-bot
Copy link

facebook-github-bot commented Feb 1, 2022

💊 CI failures summary and remediations

As of commit 49037a2 (more details on the Dr. CI page):


  • 1/1 failures introduced in this PR

1 failure not recognized by patterns:

Job Step Action
CircleCI cmake_macos_cpu curl -o conda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
sh conda.sh -b
source $HOME/miniconda3/bin/activate
conda install -yq conda-build cmake
packaging/build_cmake.sh
🔁 rerun

This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@datumbox datumbox marked this pull request as draft February 1, 2022 15:55
@datumbox datumbox marked this pull request as ready for review February 1, 2022 17:50
Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@datumbox datumbox merged commit 4ae20e5 into pytorch:main Feb 2, 2022
@datumbox datumbox deleted the models/weight_init_in_constructor branch February 2, 2022 14:19
facebook-github-bot pushed a commit that referenced this pull request Feb 11, 2022
…tructors (#5331)

Summary:
* Move weight initialization in constructors.

* Fixing mypy for ViT.

* remove unnecessary import

Reviewed By: NicolasHug

Differential Revision: D34140261

fbshipit-source-id: e9fd49cd5d7d39aa8d557d5f6efb9fbf92b80f94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants