-
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.
Use cross entropy from apex for improved memory efficiency (#1122)
Summary: Pull Request resolved: fairinternal/fairseq-py#1122 Reviewed By: ngoyal2707 Differential Revision: D20745717 Pulled By: myleott fbshipit-source-id: 877a1185f17952461ef204d8ad7f05b8d37b1fd9
- Loading branch information
1 parent
4d2efae
commit 5065077
Showing
4 changed files
with
61 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 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. | ||
|
||
import logging | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _cross_entropy_pytorch(logits, target, ignore_index=None, reduction='mean'): | ||
lprobs = F.log_softmax(logits, dim=-1, dtype=torch.float32) | ||
return F.nll_loss( | ||
lprobs, target, ignore_index=ignore_index, reduction=reduction, | ||
) | ||
|
||
|
||
try: | ||
from apex.contrib import xentropy | ||
|
||
logger.info('using fused cross entropy') | ||
|
||
def cross_entropy(logits, target, ignore_index=-100, reduction='mean'): | ||
if logits.device == torch.device('cpu'): | ||
return _cross_entropy_pytorch(logits, target, ignore_index, reduction) | ||
else: | ||
half_to_float = (logits.dtype == torch.half) | ||
losses = xentropy.SoftmaxCrossEntropyLoss.apply( | ||
logits, target, 0.0, ignore_index, half_to_float, | ||
) | ||
if reduction == 'sum': | ||
return losses.sum() | ||
elif reduction == 'mean': | ||
if ignore_index >= 0: | ||
return losses.sum() / target.ne(ignore_index).sum() | ||
else: | ||
return losses.mean() | ||
elif reduction == 'none': | ||
return losses | ||
else: | ||
raise NotImplementedError | ||
|
||
except ImportError: | ||
|
||
def cross_entropy(logits, target, ignore_index=-100, reduction='mean'): | ||
return _cross_entropy_pytorch(logits, target, ignore_index, reduction) |