forked from huggingface/optimum-habana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable HPU graphs for distributed runs and generation (huggingface#179)
- Loading branch information
Showing
5 changed files
with
116 additions
and
65 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 @@ | ||
from .modeling_t5 import _gaudi_relative_position_bucket |
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,37 @@ | ||
import math | ||
|
||
import torch | ||
|
||
|
||
@staticmethod | ||
def _gaudi_relative_position_bucket(relative_position, bidirectional=True, num_buckets=32, max_distance=128): | ||
""" | ||
Adapted from Transformers: https://github.com/huggingface/transformers/blob/ae54e3c3b18bac0832ad62ea9b896dfd52a09850/src/transformers/models/t5/modeling_t5.py#L426 | ||
The only difference is that the arguments of `torch.where` are casted to int32 to avoid an error with HPU Graphs. | ||
""" | ||
relative_buckets = 0 | ||
if bidirectional: | ||
num_buckets //= 2 | ||
relative_buckets += (relative_position > 0).to(torch.long) * num_buckets | ||
relative_position = torch.abs(relative_position) | ||
else: | ||
relative_position = -torch.min(relative_position, torch.zeros_like(relative_position)) | ||
# now relative_position is in the range [0, inf) | ||
|
||
# half of the buckets are for exact increments in positions | ||
max_exact = num_buckets // 2 | ||
is_small = relative_position < max_exact | ||
|
||
# The other half of the buckets are for logarithmically bigger bins in positions up to max_distance | ||
relative_position_if_large = max_exact + ( | ||
torch.log(relative_position.float() / max_exact) | ||
/ math.log(max_distance / max_exact) | ||
* (num_buckets - max_exact) | ||
).to(torch.long) | ||
relative_position_if_large = torch.min( | ||
relative_position_if_large, torch.full_like(relative_position_if_large, num_buckets - 1) | ||
) | ||
|
||
# TODO: delete this method when SynapseAI 1.9 is released | ||
relative_buckets += torch.where(is_small, relative_position.int(), relative_position_if_large.int()) | ||
return relative_buckets |
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