2021-12-27 07:04:32 +00:00
|
|
|
import torch
|
2022-02-14 03:15:02 +00:00
|
|
|
from colossalai.nn.layer.parallel_2d import reduce_by_batch_2d, split_tensor_2d
|
2021-12-27 07:04:32 +00:00
|
|
|
from torch import nn
|
|
|
|
|
|
|
|
from ._utils import calc_acc
|
|
|
|
|
|
|
|
|
|
|
|
class Accuracy2D(nn.Module):
|
2022-01-21 02:44:30 +00:00
|
|
|
"""Accuracy for 2D parallelism
|
2022-01-10 10:05:58 +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.
|
|
|
|
|
|
|
|
:param logits: Predicted labels
|
|
|
|
:param targets: True labels from data
|
|
|
|
"""
|
2021-12-27 07:04:32 +00:00
|
|
|
with torch.no_grad():
|
2022-02-14 03:15:02 +00:00
|
|
|
targets = split_tensor_2d(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_2d(correct)
|
2021-12-27 07:04:32 +00:00
|
|
|
return correct
|