Skip to content

process_num_alloc_configs

Configuration processing method.

It processes the provided information about the provided configuration information, including the total number, allocation of these numbers, and the list of configurations.

For the RPN layer and RPN model, they may contain multi-head, and multi-layer. To provide more flexibility in their initialization, the tinyBIG toolkit allows users to provide the configuration information in different ways:

  • provide "total number" n, "num_alloc" [1, 2, 1, ..., 1], and a list of "configs" [config1, config2, ..., confign]
  • only provide "total number" n, and a list of "configs" [config1, config2, ..., confign], we will auto complete the num_alloc to be [1, 1, 1, ..., 1]
  • only provide "total number" n, and only one "configs" either in a list "[config1]" or just "config1", we will auto complete the num_alloc to be [n]
  • only provide "num_alloc" [1, 2, 1, 3, ...., 1], and a list of configs [config1, config2, ..., configk], we will auto complete the "total num" to be sum(num_alloc)
  • other cases, we will report value errors

Therefore, this method may need to process such provided parameters to figure out the intended configurations of the RPN heads and RPN layers.

Parameters:

Name Type Description Default
num int

Total number of the configurations.

None
num_alloc int | list

The allocation of the configuration number.

None
configs dict | list

The list/dict of the configurations.

None

Returns:

Type Description
tuple | pair

The processed num, num_alloc, configs tuple.

Source code in tinybig/util/util.py
def process_num_alloc_configs(num: int = None, num_alloc: int | list = None, configs: dict | list = None):
    """
    Configuration processing method.

    It processes the provided information about the provided configuration information, including the total number,
    allocation of these numbers, and the list of configurations.

    For the RPN layer and RPN model, they may contain multi-head, and multi-layer.
    To provide more flexibility in their initialization, the tinyBIG toolkit allows users to provide the configuration
    information in different ways:

    * provide "total number" n, "num_alloc" [1, 2, 1, ..., 1], and a list of "configs" [config1, config2, ..., confign]
    * only provide "total number" n, and a list of "configs" [config1, config2, ..., confign], we will auto complete the num_alloc to be [1, 1, 1, ..., 1]
    * only provide "total number" n, and only one "configs" either in a list "[config1]" or just "config1", we will auto complete the num_alloc to be [n]
    * only provide "num_alloc" [1, 2, 1, 3, ...., 1], and a list of configs [config1, config2, ..., configk], we will auto complete the "total num" to be sum(num_alloc)
    * other cases, we will report value errors

    Therefore, this method may need to process such provided parameters to figure out the intended configurations of
    the RPN heads and RPN layers.

    Parameters
    ----------
    num: int, default = None
        Total number of the configurations.
    num_alloc: int | list, default = None
        The allocation of the configuration number.
    configs: dict | list, default = None
        The list/dict of the configurations.

    Returns
    -------
    tuple | pair
        The processed num, num_alloc, configs tuple.
    """
    if num_alloc is None:
        if type(configs) is not list:
            configs = [configs]
        if len(configs) == num:
            num_alloc = [1] * num
        else:
            if num is None:
                if configs is None:
                    raise ValueError(
                        "Neither total num, num_alloc or configs has been provided...")
                else:
                    num = len(configs)
                    num_alloc = [1] * len(configs)
                    warnings.warn(
                        "Neither total num or num_alloc is provided, which will be inferred from the config...".format(
                            len(configs)))
            else:
                if len(configs) == 1:
                    # only one config is provided, repeat the identical config for all heads
                    warnings.warn(
                        "The provided total number {} and config number {} are inconsistent, we will repeat the config {} times by default...".format(
                            num, len(configs), num), UserWarning)
                    num_alloc = [num]
                else:
                    # multiple configs provided but the numbers are inconsistent with number, cannot infer the configs for each head
                    raise ValueError(
                        "The provided total number {} and config number {} are inconsistent and num_alloc parameter is None... please also provide the num_alloc as well...".format(
                            num, len(configs)))
    else:
        # check variable consistency
        if type(num_alloc) is not list:
            num_alloc = [num_alloc]
        if type(configs) is not list:
            configs = [configs]
        if num is None:
            num = sum(num_alloc)

    return num, num_alloc, configs