code: def train_model_distributed(
      config, metric_channels: Optional[List[Channel]]
  ):
      assert (
          config.use_cuda_if_available and torch.cuda.is_available()
      ) or config.distributed_world_size == 1, (
          "distributed training is only available for GPU training"
      )
      assert (
          config.distributed_world_size == 1
          or config.distributed_world_size <= torch.cuda.device_count()
      ), (
          f"Only {torch.cuda.device_count()} GPUs are available, "
          f"{config.distributed_world_size} GPUs were requested"
      )
  
      print(
          f"\n=== Starting training, World size is {config.distributed_world_size}"
      )
      if (
          not config.use_cuda_if_available
          or not torch.cuda.is_available()
      ):
          run_single(
              rank=0,
              config_json=config_to_json(PyTextConfig, config),
              world_size=1,
              dist_init_method=None,
              metadata=None,
              metric_channels=metric_channels,
          )
      else:
          with tempfile.NamedTemporaryFile(
              delete=False, suffix=".dist_sync"
          ) as sync_file:
              dist_init_method = "file://" + sync_file.name
              metadata = prepare_task_metadata(config)
              spawn(
                  run_single,
                  (
                      config_to_json(PyTextConfig, config),
                      config.distributed_world_size,
                      dist_init_method,
                      metadata,
                      [],
                  ),
                  config.distributed_world_size,
              )
  
imports: import json
  import pprint
  import sys
  import tempfile
  from importlib import import_module
  from pydoc import locate
  from typing import Dict, List, Optional, Union
  import click
  import torch
  from pytext import create_predictor
  from pytext.builtin_task import add_include
  from pytext.common.utils import eprint
  from pytext.config import ExportConfig, LATEST_VERSION, PyTextConfig
  from pytext.config.component import register_tasks
  from pytext.config.config_adapter import upgrade_to_latest
  from pytext.config.serialize import config_from_json, config_to_json, parse_config, pytext_config_from_json
  from pytext.config.utils import find_param, replace_param
  from pytext.data.data_handler import CommonMetadata
  from pytext.metric_reporters.channel import Channel, TensorBoardChannel
  from pytext.PreprocessingMap.ttypes import ModelType
  from pytext.task import load
  from pytext.utils.documentation import find_config_class, get_subclasses, pretty_print_config_class, replace_components, ROOT_CONFIG
  from pytext.utils.file_io import PathManager
  from pytext.workflow import export_saved_model_to_caffe2, export_saved_model_to_torchscript, get_logits, prepare_task_metadata, save_pytext_snapshot, test_model_from_snapshot_path, train_model
  from torch.multiprocessing.spawn import spawn
label_desc: Use image node_id 3 for calling a global function with example usage: train_model_distributed(config, metric_channels) without return types