PyTorch
ssl-aasist
custom_code
File size: 1,886 Bytes
d28af7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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 random
import numpy as np
import torch

from .shardedtensor import *
from .load_config import *


def set_seed(seed=43211):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    if torch.backends.cudnn.enabled:
        torch.backends.cudnn.benchmark = False
        torch.backends.cudnn.deterministic = True


def get_world_size():
    if torch.distributed.is_initialized():
        world_size = torch.distributed.get_world_size()
    else:
        world_size = 1
    return world_size


def get_local_rank():
    return torch.distributed.get_rank() \
        if torch.distributed.is_initialized() else 0


def print_on_rank0(func):
    local_rank = get_local_rank()
    if local_rank == 0:
        print("[INFO]", func)


class RetriMeter(object):
    """
    Statistics on whether retrieval yields a better pair.
    """
    def __init__(self, freq=1024):
        self.freq = freq
        self.total = 0
        self.replace = 0
        self.updates = 0

    def __call__(self, data):
        if isinstance(data, np.ndarray):
            self.replace += data.shape[0] - int((data[:, 0] == -1).sum())
            self.total += data.shape[0]
        elif torch.is_tensor(data):
            self.replace += int(data.sum())
            self.total += data.size(0)
        else:
            raise ValueError("unsupported RetriMeter data type.", type(data))

        self.updates += 1
        if get_local_rank() == 0 and self.updates % self.freq == 0:
            print("[INFO]", self)

    def __repr__(self):
        return "RetriMeter (" + str(self.replace / self.total) \
            + "/" + str(self.replace) + "/" + str(self.total) + ")"