code: def _init_context(
      client: experimental.Determined,
      defaults: Optional[DefaultConfig] = None,
      unmanaged: Optional[UnmanagedConfig] = None,
      distributed: Optional[core.DistributedContext] = None,
      checkpoint_storage: Optional[Union[str, Dict[str, Any]]] = None,
      preempt_mode: core.PreemptMode = core.PreemptMode.WorkersAskChief,
      tensorboard_mode: core.TensorboardMode = core.TensorboardMode.AUTO,
  ) -> core.Context:
      info = determined.get_cluster_info()
      if info is not None and info.task_type == "TRIAL":
          # Managed trials.
          _context = core_v2._make_v2_context(
              distributed=distributed,
              checkpoint_storage=checkpoint_storage,
              preempt_mode=preempt_mode,
              tensorboard_mode=tensorboard_mode,
              client=client,
          )
          return _context
  
      # Unmanaged trials.
      if defaults is None:
          raise NotImplementedError(
              "either specify `defaults`, or run as a managed determined experiment"
          )
  
      # Construct the config.
      defaulted_config = defaults or DefaultConfig()
      unmanaged_config = unmanaged or UnmanagedConfig()
      checkpoint_storage = (
          checkpoint_storage or defaulted_config.checkpoint_storage
      )
  
      config = {
          "name": defaulted_config.name
          or f"unmanaged-{uuid.uuid4().hex[:8]}",
          "data": defaulted_config.data,
          "description": defaulted_config.description,
          "labels": defaulted_config.labels,
          "searcher": defaulted_config.searcher
          or {
              "name": "single",
              "metric": "unmanaged",
              "max_length": 100000000,
          },
          "workspace": unmanaged_config.workspace,
          "project": unmanaged_config.project,
      }
  
      config_text = util.yaml_safe_dump(config)
      assert config_text is not None
  
      unmanaged_info = core_v2._get_or_create_experiment_and_trial(
          client,
          config_text=config_text,
          experiment_id=unmanaged_config.external_experiment_id,
          trial_id=unmanaged_config.external_trial_id,
          distributed=distributed,
          hparams=defaulted_config.hparams,
      )
  
      _context = core_v2._make_v2_context(
          distributed=distributed,
          checkpoint_storage=checkpoint_storage,
          preempt_mode=preempt_mode,
          tensorboard_mode=tensorboard_mode,
          unmanaged_info=unmanaged_info,
          client=client,
      )
      return _context
  
file_path: determined/harness/determined/experimental/core_v2/_core_v2.py
imports: import atexit
  import dataclasses
  import logging
  import uuid
  from typing import Any, Dict, List, Optional, Union
  import determined
  from determined import core, experimental
  from determined.common import util
  from determined.experimental import core_v2
label_desc: Use image node_id 2 for calling a global function with example usage: _init_context(client, defaults, unmanaged, distributed, checkpoint_storage, preempt_mode, tensorboard_mode) and returns: _context, _context