tensor_cost.py 3.5 KB
Newer Older
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
#   Copyright (c) 2022 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.
# See the License for the specific language governing permissions and
# limitations under the License

from functools import reduce

import paddle
from paddle.fluid.framework import Variable
from paddle.distributed.auto_parallel.dist_tensor import DistributedTensor

from .base_cost import Cost


class TensorCost:
    def __init__(self, tensor=None, dist_tensor=None, shape=None, dtype=None):
        self._check_args(tensor, dist_tensor, shape, dtype)
        self._tensor = tensor
        self._dist_tensor = dist_tensor
        self._shape = shape
        self._dtype = dtype
        self._cost = self.calc_cost()

    @property
    def tensor(self):
        return self._tensor

    @property
    def dist_tensor(self):
        return self._dist_tensor

    @property
    def shape(self):
        return self._shape

    @property
    def dtype(self):
        return self._dtype

    def _check_args(self, tensor, dist_tensor, shape, dtype):
        if tensor is not None:
51
            assert shape is None and dist_tensor is None and dtype is None
52 53 54 55

            if not isinstance(tensor, Variable):
                raise TypeError(
                    "Please check tensor type is Variable, but got {}".format(
56 57 58
                        type(tensor)
                    )
                )
59 60

        elif dist_tensor is not None:
61
            assert tensor is None and shape is None
62 63
            if not isinstance(dist_tensor, DistributedTensor):
                raise TypeError(
64 65 66 67
                    "Please check dist_tensor type is DistributedTensor, but got {}".format(
                        type(dist_tensor)
                    )
                )
68 69

        elif shape is not None:
70
            assert tensor is None and dist_tensor is None and dtype is not None
71 72 73
            if not isinstance(shape, (list, set)):
                raise TypeError(
                    "Please check shape type is list or set, but got {}".format(
74 75 76
                        type(shape)
                    )
                )
77 78

        elif dtype is not None:
79
            assert tensor is None and dist_tensor is None and shape is not None
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

    @property
    def cost(self):
        return self._cost

    def calc_cost(self):
        dtype = None
        shape = None

        if self.dist_tensor:
            shape = self.dist_tensor.local_sizes()
            dtype = self.dist_tensor.serial_tensor.dtype
        elif self.tensor:
            shape = self.tensor.shape
            dtype = self.tensor.dtype
        elif self.shape and self.dtype:
            shape = self.shape
            dtype = self.dtype

        total_count = reduce(lambda x, y: x * y, shape)

        if dtype == paddle.float32 or dtype == paddle.int32:
            dtype_factor = 4
103
        elif dtype == paddle.int64:
104
            dtype_factor = 8
105
        elif dtype == paddle.uint8:
106 107 108 109 110 111 112 113 114
            dtype_factor = 1
        else:
            dtype_factor = 2

        memory = total_count * dtype_factor
        assert memory >= 0
        cost = Cost(memory=memory)

        return cost