### This file is originally from: [mlcommons repo](https://github.com/mlcommons/training/tree/9947bdf21ee3f2488fa4b362eec2ce7deb2ec4dd/single_stage_detector/ssd/mlperf_logger.py)
# Copyright (c) 2018, NVIDIA CORPORATION. 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.

import torch
import numpy as np
import os
from mlperf_logging import mllog
from mlperf_logging.mllog import constants as mllog_const
mllogger = mllog.get_mllogger()
mllog.config(
    filename=(os.getenv("COMPLIANCE_FILE") or "mlperf_compliance.log"),
    root_dir=os.path.normpath(os.path.dirname(os.path.realpath(__file__))))

def ssd_print(*args, sync=True, **kwargs):
    use_cuda = os.getenv('USE_CUDA')
    if sync and use_cuda=='True':
        barrier()
    if get_rank() == 0:
        kwargs['stack_offset'] = 2
        mllogger.event(*args, **kwargs)


def barrier():
    """
    Works as a temporary distributed barrier, currently pytorch
    doesn't implement barrier for NCCL backend.
    Calls all_reduce on dummy tensor and synchronizes with GPU.
    """
    if torch.distributed.is_initialized():
        torch.distributed.all_reduce(torch.cuda.FloatTensor(1))
        torch.cuda.synchronize()


def get_rank():
    """
    Gets distributed rank or returns zero if distributed is not initialized.
    """
    if torch.distributed.is_initialized():
        rank = torch.distributed.get_rank()
    else:
        rank = os.getenv('RANK', os.getenv('LOCAL_RANK', 0))
    return rank

def broadcast_seeds(seed, device):
    if torch.distributed.is_initialized():
        seeds_tensor = torch.LongTensor([seed]).to(device)
        torch.distributed.broadcast(seeds_tensor, 0)
        seed = seeds_tensor.item()
    return seed

[(21, 'mlperf_logging.mllog.get_mllogger', 'mllog.get_mllogger', False, 'from mlperf_logging import mllog\n'), (27, 'os.getenv', 'os.getenv', False, 'import os\n'), (41, 'torch.distributed.is_initialized', 'torch.distributed.is_initialized', False, 'import torch\n'), (50, 'torch.distributed.is_initialized', 'torch.distributed.is_initialized', False, 'import torch\n'), (57, 'torch.distributed.is_initialized', 'torch.distributed.is_initialized', False, 'import torch\n'), (43, 'torch.cuda.synchronize', 'torch.cuda.synchronize', False, 'import torch\n'), (51, 'torch.distributed.get_rank', 'torch.distributed.get_rank', False, 'import torch\n'), (59, 'torch.distributed.broadcast', 'torch.distributed.broadcast', False, 'import torch\n'), (23, 'os.getenv', 'os.getenv', False, 'import os\n'), (42, 'torch.cuda.FloatTensor', 'torch.cuda.FloatTensor', False, 'import torch\n'), (53, 'os.getenv', 'os.getenv', False, 'import os\n'), (24, 'os.path.realpath', 'os.path.realpath', False, 'import os\n'), (58, 'torch.LongTensor', 'torch.LongTensor', False, 'import torch\n')]