1# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2
3import math
4from typing import List
5
6
7def set_attributes(self, params: List[object] = None) -> None:
8    """
9    An utility function used in classes to set attributes from the input list of parameters.
10    Args:
11        params (list): list of parameters.
12    """
13    if params:
14        for k, v in params.items():
15            if k != "self":
16                setattr(self, k, v)
17
18
19def round_width(width, multiplier, min_width=8, divisor=8, ceil=False):
20    """
21    Round width of filters based on width multiplier
22    Args:
23        width (int): the channel dimensions of the input.
24        multiplier (float): the multiplication factor.
25        min_width (int): the minimum width after multiplication.
26        divisor (int): the new width should be dividable by divisor.
27        ceil (bool): If True, use ceiling as the rounding method.
28    """
29    if not multiplier:
30        return width
31
32    width *= multiplier
33    min_width = min_width or divisor
34    if ceil:
35        width_out = max(min_width, int(math.ceil(width / divisor)) * divisor)
36    else:
37        width_out = max(min_width, int(width + divisor / 2) // divisor * divisor)
38    if width_out < 0.9 * width:
39        width_out += divisor
40    return int(width_out)
41
42
43def round_repeats(repeats, multiplier):
44    """
45    Round number of layers based on depth multiplier.
46    """
47    if not multiplier:
48        return repeats
49    return int(math.ceil(multiplier * repeats))
50