-
Notifications
You must be signed in to change notification settings - Fork 812
Add pad transform, string to int transform #1683
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b66cecc
Allow inferred scaling in MultiheadSelfAttention for head_dim != 64
ebsmothers 6589989
Merge branch 'pytorch:main' into main
ebsmothers 54a4eef
Merge branch 'pytorch:main' into main
ebsmothers 3c11873
Merge branch 'pytorch:main' into main
ebsmothers 02b166e
Merge branch 'pytorch:main' into main
ebsmothers 003eae6
Merge branch 'pytorch:main' into main
ebsmothers 0e7630f
Add pad transform, string to int transform
ebsmothers 243c851
Add docstrings for new transforms
ebsmothers a6d8e54
Add test docstrings, fix str to int scripting
ebsmothers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -23,3 +23,8 @@ add_token | |
--------- | ||
|
||
.. autofunction:: add_token | ||
|
||
str_to_int | ||
---------- | ||
|
||
.. autofunction:: str_to_int |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
"LabelToIndex", | ||
"Truncate", | ||
"AddToken", | ||
"PadTransform", | ||
"StrToIntTransform", | ||
"GPT2BPETokenizer", | ||
"Sequential", | ||
] | ||
|
@@ -221,6 +223,50 @@ def forward(self, input: Any) -> Any: | |
return F.add_token(input, self.token, self.begin) | ||
|
||
|
||
class PadTransform(Module): | ||
"""Pad tensor to a fixed length with given padding value. | ||
|
||
:param max_length: Maximum length to pad to | ||
:type max_length: int | ||
:param pad_value: Value to pad the tensor with | ||
:type pad_value: bool | ||
""" | ||
|
||
def __init__(self, max_length: int, pad_value: int): | ||
super().__init__() | ||
self.max_length = max_length | ||
self.pad_value = pad_value | ||
|
||
def forward(self, x: Tensor) -> Tensor: | ||
""" | ||
:param x: The tensor to pad | ||
:type x: Tensor | ||
:return: Tensor padded up to max_length with pad_value | ||
:rtype: Tensor | ||
""" | ||
max_encoded_length = x.size(-1) | ||
if max_encoded_length < self.max_length: | ||
pad_amount = self.max_length - max_encoded_length | ||
x = torch.nn.functional.pad(x, (0, pad_amount), value=self.pad_value) | ||
return x | ||
|
||
|
||
class StrToIntTransform(Module): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment for doc-strings :) |
||
"""Convert string tokens to integers (either single sequence or batch).""" | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
def forward(self, input: Any) -> Any: | ||
""" | ||
:param input: sequence or batch of string tokens to convert | ||
:type input: Union[List[str], List[List[str]]] | ||
:return: sequence or batch converted into corresponding token ids | ||
:rtype: Union[List[int], List[List[int]]] | ||
""" | ||
return F.str_to_int(input) | ||
|
||
|
||
class GPT2BPETokenizer(Module): | ||
__jit_unused_properties__ = ["is_jitable"] | ||
""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a brief description of what this test does?
In the form of "(under some condition), an API produces a result that satisfies this property"