mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-13 05:01:44 +00:00
* [legacy] move communication to legacy (#4640) * [legacy] refactor logger and clean up legacy codes (#4654) * [legacy] make logger independent to gpc * [legacy] make optim independent to registry * [legacy] move test engine to legacy * [legacy] move nn to legacy (#4656) * [legacy] move nn to legacy * [checkpointio] fix save hf config * [test] remove useledd rpc pp test * [legacy] fix nn init * [example] skip tutorial hybriad parallel example * [devops] test doc check * [devops] test doc check
31 lines
806 B
Python
31 lines
806 B
Python
import torch
|
|
from torch import nn
|
|
|
|
from colossalai.legacy.nn.layer.parallel_2p5d import reduce_by_batch_2p5d, split_batch_2p5d
|
|
|
|
from ._utils import calc_acc
|
|
|
|
|
|
class Accuracy2p5D(nn.Module):
|
|
"""Accuracy for 2p5D parallelism
|
|
"""
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def forward(self, logits, targets):
|
|
"""Calculate the accuracy of predicted labels.
|
|
|
|
Args:
|
|
logits (:class:`torch.tensor`): Predicted labels.
|
|
targets (:class:`torch.tensor`): True labels from data.
|
|
|
|
Returns:
|
|
float: the accuracy of prediction.
|
|
"""
|
|
with torch.no_grad():
|
|
targets = split_batch_2p5d(targets)
|
|
correct = calc_acc(logits, targets)
|
|
correct = reduce_by_batch_2p5d(correct)
|
|
return correct
|