-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Pull Request resolved: fairinternal/fairseq-py#1002 Pull Request resolved: pytorch/translate#681 Pull Request resolved: #1524 Make fairseq MultiheadAttention scriptable. Looking for feedbacks. 1. Add types 2. Move incremental state management logic from util functions to initializers. TorchScript in general doesn't support global dict. As a result modules with multihead attention in it would assign itself fairseq_instance_id in the initializer. 3. There might be opportunities to make assertions and annotations cleaner. Reviewed By: myleott Differential Revision: D18772594 fbshipit-source-id: 377aef4bbb7ef51da5b6bac9a87a6f7b03b16fe1
- Loading branch information
1 parent
2535cab
commit 4e48c4a
Showing
14 changed files
with
178 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from fairseq import utils | ||
|
||
|
||
class FairseqIncrementalState(object): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
init_incremental_state(self) | ||
|
||
|
||
def with_incremental_state(cls): | ||
cls.__bases__ = (FairseqIncrementalState,) + tuple(b for b in cls.__bases__ if b != FairseqIncrementalState) | ||
return cls | ||
|
||
|
||
# In most cases we should register incremental states using @with_incremental_state decorator | ||
# instead of calling into this explicitly in initializer. | ||
def init_incremental_state(obj): | ||
obj.module_name = obj.__class__.__name__ | ||
utils.INCREMENTAL_STATE_INSTANCE_ID[obj.module_name] = ( | ||
utils.INCREMENTAL_STATE_INSTANCE_ID.get(obj.module_name, 0) + 1 | ||
) | ||
obj._fairseq_instance_id = utils.INCREMENTAL_STATE_INSTANCE_ID[ | ||
obj.module_name | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.