forked from PaddlePaddle/PaddleVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
52 lines (42 loc) · 1.79 KB
/
base.py
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
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
from abc import abstractmethod
import paddle
from paddlevideo.utils import get_dist_info
from .registry import METRIC
class BaseMetric(object):
def __init__(self, data_size, batch_size, log_interval=1, **kwargs):
self.data_size = data_size
self.batch_size = batch_size
_, self.world_size = get_dist_info()
self.log_interval = log_interval
def gather_from_gpu(self,
gather_object: paddle.Tensor,
concat_axis=0) -> paddle.Tensor:
"""gather Tensor from all gpus into a list and concatenate them on `concat_axis`.
Args:
gather_object (paddle.Tensor): gather object Tensor
concat_axis (int, optional): axis for concatenation. Defaults to 0.
Returns:
paddle.Tensor: gatherd & concatenated Tensor
"""
gather_object_list = []
paddle.distributed.all_gather(gather_object_list, gather_object.cuda())
return paddle.concat(gather_object_list, axis=concat_axis)
@abstractmethod
def update(self):
raise NotImplementedError(
"'update' method must be implemented in subclass")
@abstractmethod
def accumulate(self):
raise NotImplementedError(
"'accumulate' method must be implemented in subclass")