code: def prepare_task(
      config: PyTextConfig,
      dist_init_url: str = None,
      device_id: int = 0,
      rank: int = 0,
      world_size: int = 1,
      metric_channels: Optional[List[Channel]] = None,
      metadata: CommonMetadata = None,
  ) -> Tuple[Task_Deprecated, TrainingState]:
      if world_size > 1 and config.random_seed is None:
          msg = (
              "Must set random seed when using world_size > 1, so that parameters have "
              "same initialization across workers."
          )
          raise ValueError(msg)
  
      if rank == 0:
          print("\nParameters: {}\n".format(config), flush=True)
      _set_cuda(config.use_cuda_if_available, device_id, world_size)
      _set_fp16(config.use_fp16, rank)
      _set_distributed(
          rank,
          world_size,
          dist_init_url,
          device_id,
          config.gpu_streams_for_distributed_training,
      )
  
      if config.random_seed is not None:
          set_random_seeds(
              config.random_seed, config.use_deterministic_cudnn
          )
  
      training_state = None
  
      if config.auto_resume_from_snapshot:
          # if there are existing checkpoints, resume from the latest one
          latest_snapshot_path = get_latest_checkpoint_path(
              os.path.dirname(config.save_snapshot_path)
          )
          if latest_snapshot_path:
              config.load_snapshot_path = latest_snapshot_path
  
      if config.load_snapshot_path:
          assert PathManager.isfile(config.load_snapshot_path)
          if config.use_config_from_snapshot:
              task, _, training_state = load(
                  config.load_snapshot_path,
                  rank=rank,
                  world_size=world_size,
              )
          else:
              task, _, training_state = load(
                  config.load_snapshot_path,
                  overwrite_config=config,
                  rank=rank,
                  world_size=world_size,
              )
  
          if training_state:
              training_state.rank = rank
      else:
          task = create_task(
              config.task,
              metadata=metadata,
              rank=rank,
              world_size=world_size,
          )
  
      for mc in metric_channels or []:
          task.metric_reporter.add_channel(mc)
  
      return task, training_state
  
imports: import gzip
  import json
  import os
  import sys
  import traceback
  from typing import Any, Dict, get_type_hints, IO, Iterator, List, Optional, Tuple, Union
  import torch
  from pytext.common.constants import Stage
  from pytext.config import ExportConfig, PyTextConfig, TestConfig
  from pytext.config.component import ComponentType, create_component, create_exporter
  from pytext.data.data import Batcher
  from pytext.data.data_handler import CommonMetadata
  from pytext.metric_reporters.channel import Channel
  from pytext.task import create_task, get_latest_checkpoint_path, load, NewTask, save, Task_Deprecated
  from pytext.task.disjoint_multitask import NewDisjointMultitask
  from pytext.trainers import TrainingState
  from pytext.utils import cuda, distributed, precision, round_seq, set_random_seeds, timing
  from pytext.utils.file_io import PathManager
label_desc: Use image node_id 6 for calling a global function with example usage: prepare_task(config, dist_init_url, device_id, rank, world_size, metric_channels, metadata) and returns: task,  training_state