Skip to content

mpi_rank

kedro_azureml_pipeline.distributed.mpi_rank()

Return the 0-based rank of this process in an MPI or PyTorch step, else 0.

Reads the variables in :data:RANK_VARS order. A variable that is set but does not hold an integer is skipped in favour of the next one, and 0 is returned when none parses, so a single-instance run and a malformed launcher variable both read as rank 0 instead of failing the step. Use it for anything that varies per rank, such as a seed offset or a log marker.

Returns

Type Description
int

The rank parsed from the first of :data:RANK_VARS that holds an integer, or 0.

See Also

Source Code

Source code in src/kedro_azureml_pipeline/distributed/utils.py
def mpi_rank() -> int:
    """Return the 0-based rank of this process in an MPI or PyTorch step, else 0.

    Reads the variables in :data:`RANK_VARS` order. A variable that is set but
    does not hold an integer is skipped in favour of the next one, and 0 is
    returned when none parses, so a single-instance run and a malformed
    launcher variable both read as rank 0 instead of failing the step. Use it
    for anything that varies per rank, such as a seed offset or a log marker.

    Returns
    -------
    int
        The rank parsed from the first of :data:`RANK_VARS` that holds an
        integer, or 0.

    See Also
    --------
    [is_distributed_master_node][kedro_azureml_pipeline.distributed.utils.is_distributed_master_node] : Checks master rank, including the TensorFlow case.
    [is_distributed_environment][kedro_azureml_pipeline.distributed.utils.is_distributed_environment] : Detects distributed context.
    """
    for var in RANK_VARS:
        value = os.environ.get(var)
        if value is None:
            continue
        try:
            return int(value)
        except ValueError:
            continue
    return 0