code: def get_trainer(
      num_workers: int = 4,
      use_gpu: bool = False,
      config: Optional[Dict] = None,
  ):
      """Get the trainer to be used across train and tune to ensure consistency."""
      from torch_benchmark import train_func
  
      def train_loop(config):
          train_func(use_ray=True, config=config)
  
      # We are using STRICT_PACK here to do an apples to apples comparison.
      # PyTorch defaults to using multithreading, so if the workers are spread,
      # they are able to utilize more resources. We would effectively be comparing
      # X tune runs with 2 CPUs per worker vs. 1 tune run with up to 8 CPUs per
      # worker. Using STRICT_PACK avoids this by forcing all workers to be
      # co-located.
  
      config = config or CONFIG
  
      trainer = TorchTrainer(
          train_loop_per_worker=train_loop,
          train_loop_config=config,
          scaling_config=ScalingConfig(
              num_workers=num_workers,
              resources_per_worker={"CPU": 2},
              trainer_resources={"CPU": 0},
              use_gpu=use_gpu,
              placement_strategy="STRICT_PACK",
          ),
      )
      return trainer
  
file_path: ray/release/air_tests/air_benchmarks/workloads/tune_torch_benchmark.py
imports: import json
  import os
  import time
  import timeit
  from typing import Optional, Dict
  import click
  import numpy
  import ray
  from ray.train import ScalingConfig
  from ray.train.torch import TorchTrainer
label_desc: Use image node_id 2 for calling a global function with example usage: get_trainer(num_workers, use_gpu, config) and returns: trainer