2021-12-27 07:04:32 +00:00
|
|
|
import torch
|
2022-04-02 08:12:04 +00:00
|
|
|
from colossalai.nn.layer.parallel_2p5d import reduce_by_batch_2p5d, split_batch_2p5d
|
2021-12-27 07:04:32 +00:00
|
|
|
from torch import nn
|
|
|
|
|
|
|
|
from ._utils import calc_acc
|
|
|
|
|
|
|
|
|
|
|
|
class Accuracy2p5D(nn.Module):
|
2022-01-21 02:44:30 +00:00
|
|
|
"""Accuracy for 2p5D parallelism
|
2022-01-10 10:05:58 +00:00
|
|
|
"""
|
2022-10-17 09:08:31 +00:00
|
|
|
|
2021-12-27 07:04:32 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def forward(self, logits, targets):
|
2022-01-21 02:44:30 +00:00
|
|
|
"""Calculate the accuracy of predicted labels.
|
|
|
|
|
2022-03-25 05:02:39 +00:00
|
|
|
Args:
|
|
|
|
logits (:class:`torch.tensor`): Predicted labels.
|
|
|
|
targets (:class:`torch.tensor`): True labels from data.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
float: the accuracy of prediction.
|
2022-01-21 02:44:30 +00:00
|
|
|
"""
|
2021-12-27 07:04:32 +00:00
|
|
|
with torch.no_grad():
|
2022-04-02 08:12:04 +00:00
|
|
|
targets = split_batch_2p5d(targets)
|
2021-12-27 07:04:32 +00:00
|
|
|
correct = calc_acc(logits, targets)
|
2022-02-14 03:15:02 +00:00
|
|
|
correct = reduce_by_batch_2p5d(correct)
|
2021-12-27 07:04:32 +00:00
|
|
|
return correct
|