code: def init(
      *,
      hparams: Optional[Dict] = None,
      exp_conf: Optional[Dict[str, Any]] = None,
      distributed: Optional[core.DistributedContext] = None,
      aggregation_frequency: int = 1,
      enable_tensorboard_logging: bool = True,
  ) -> Iterator[pytorch.PyTorchTrialContext]:
      """
      Creates a PyTorchTrialContext for use with a PyTorchTrial. All trainer.* calls must be within
      the scope of this context because there are resources started in __enter__ that must be
      cleaned up in __exit__.
  
      Arguments:
          hparams: (Optional) instance of hyperparameters for the trial
          exp_conf: (Optional) for local-training mode. If unset, calling
              context.get_experiment_config() will fail.
          distributed: (Optional) custom distributed training configuration
          aggregation_frequency: number of batches before gradients are exchanged in distributed
              training. This value is configured here because it is used in context.wrap_optimizer.
          enable_tensorboard_logging: Configures if upload to tensorboard is enabled
      """
      cluster_info = det.get_cluster_info()
      local_training = (
          cluster_info is None or cluster_info.task_type != "TRIAL"
      )
  
      # Pre-execute steps: initialize distributed backend and random seeds.
      distributed_context = distributed
  
      if not local_training:
          distributed_context = _initialize_distributed_backend()
  
      # Initialize default values.
      if local_training:
          trial_seed = None
          steps_completed = 0
          managed_training = True
          debug_enabled = False
          num_gpus = len(gpu.get_gpu_uuids())
      else:
          assert cluster_info, "Unable to detect cluster info"
  
          trial_seed = cluster_info.trial.trial_seed
          exp_conf = cluster_info.trial._config
          steps_completed = cluster_info.trial._steps_completed
          managed_training = True
          num_gpus = len(cluster_info.gpu_uuids)
          debug_enabled = cluster_info.trial._debug
  
          _set_random_seeds(trial_seed)
  
      with core.init(
          distributed=distributed_context,
          preempt_mode=core.PreemptMode.WorkersAskChief,
          tensorboard_mode=core.TensorboardMode.MANUAL,
      ) as core_context:
          context = pytorch.PyTorchTrialContext(
              core_context=core_context,
              trial_seed=trial_seed,
              hparams=hparams,
              slots_per_trial=core_context.distributed.get_size(),
              num_gpus=num_gpus,
              exp_conf=exp_conf,
              aggregation_frequency=aggregation_frequency,
              steps_completed=steps_completed,
              managed_training=managed_training,
              debug_enabled=debug_enabled,
              enable_tensorboard_logging=enable_tensorboard_logging,
          )
  
          yield context
  
file_path: determined/harness/determined/pytorch/_trainer.py
imports: import contextlib
  import logging
  import random
  import sys
  from typing import Any, Dict, Iterator, Optional
  import numpy
  import torch
  import torch.distributed
  import determined
  from determined import core, gpu, horovod, profiler, pytorch
  from determined.horovod import hvd
label_desc: Use image node_id 3 for calling a global function with example usage: init() without return types