class_docstr: The real implementation behind external facing ``Tuner``.
  
  The external facing ``Tuner`` multiplexes between local Tuner and remote Tuner
  depending on whether in Ray client mode.
  
  In Ray client mode, external ``Tuner`` wraps ``TunerInternal`` into a remote actor,
  which is guaranteed to be placed on head node.
  
  ``TunerInternal`` can be constructed from fresh, in which case, ``trainable`` needs
  to be provided, together with optional ``param_space``, ``tune_config`` and
  ``run_config``.
  
  It can also be restored from a previous failed run (given ``restore_path``).
  
  Args:
      restore_path: The path from where the Tuner can be restored. If provided, None
          of the rest args are needed.
      resume_config: Resume config to configure which trials to continue.
      trainable: The trainable to be tuned.
      param_space: Search space of the tuning job.
          One thing to note is that both preprocessor and dataset can be tuned here.
      tune_config: Tuning algorithm specific configs.
          Refer to ray.tune.tune_config.TuneConfig for more info.
      run_config: Runtime configuration that is specific to individual trials.
          If passed, this will overwrite the run config passed to the Trainer,
          if applicable. Refer to ray.train.RunConfig for more info.
docstr: Get tune.run arguments common for both new and resumed runs.
docstr_tok: ["Get","tune.run","arguments","common","for","both","new","and","resumed","runs","."]
code: def _get_tune_run_arguments(
      self, trainable: TrainableType
  ) -> Dict[str, Any]:
      """Get tune.run arguments common for both new and resumed runs."""
      # Avoid overwriting the originally configured checkpoint config.
      checkpoint_config = copy.deepcopy(
          self._run_config.checkpoint_config
      )
  
      if checkpoint_config.checkpoint_frequency:
          # Function trainables (and thus most of our trainers) usually don't handle
          # this argument.
          handle_checkpoint_freq = getattr(
              trainable, "_handles_checkpoint_freq", None
          )
          if handle_checkpoint_freq is False:
              # If we specifically know this trainable doesn't support the
              # argument, raise an error
              raise ValueError(
                  "You passed `checkpoint_frequency="
                  f"{checkpoint_config.checkpoint_frequency}` to your "
                  "CheckpointConfig, but this trainer does not support "
                  "this argument. If you passed in a Trainer that takes in a "
                  "custom training loop, you will need to "
                  "report a checkpoint every `checkpoint_frequency` iterations "
                  "within your training loop using "
                  "`ray.train.report(metrics=..., checkpoint=...)` "
                  "to get this behavior."
              )
          elif handle_checkpoint_freq is True:
              # If we specifically support it, it's handled in the training loop,
              # so we disable tune's bookkeeping.
              checkpoint_config.checkpoint_frequency = 0
          # Otherwise, the trainable is not a Trainer and we just keep the
          # user-supplied value.
          # Function trainables will raise a runtime error later if set > 0
      if checkpoint_config.checkpoint_at_end is not None:
          # Again, function trainables usually don't handle this argument.
          handle_cp_at_end = getattr(
              trainable, "_handles_checkpoint_at_end", None
          )
          if handle_cp_at_end is False:
              # If we specifically know we don't support it, raise an error.
              raise ValueError(
                  "You passed `checkpoint_at_end="
                  f"{checkpoint_config.checkpoint_at_end}` "
                  "to your CheckpointConfig, but this trainer does not support "
                  "this argument. If you passed in a Trainer that takes in a "
                  "custom training loop, you should include one last call to "
                  "`ray.train.report(metrics=..., checkpoint=...)` "
                  "at the end of your training loop to get this behavior."
              )
          elif handle_cp_at_end is True:
              # If we specifically support it, it's handled in the training loop,
              # so we disable tune's internal bookkeeping.
              checkpoint_config.checkpoint_at_end = False
          # If this is a user-defined trainable, just keep the value
          # Function trainables will raise a runtime error later if set to True
      else:
          # Set default to False for function trainables and True for everything else
          if is_function_trainable(trainable):
              checkpoint_config.checkpoint_at_end = False
          else:
              checkpoint_config.checkpoint_at_end = True
  
      return dict(
          storage_path=self._run_config.storage_path,
          storage_filesystem=self._run_config.storage_filesystem,
          name=self._experiment_dir_name,
          mode=self._tune_config.mode,
          metric=self._tune_config.metric,
          callbacks=self._run_config.callbacks,
          sync_config=self._run_config.sync_config,
          stop=self._run_config.stop,
          max_failures=self._run_config.failure_config.max_failures,
          checkpoint_config=checkpoint_config,
          raise_on_failed_trial=False,
          fail_fast=(self._run_config.failure_config.fail_fast),
          progress_reporter=self._run_config.progress_reporter,
          verbose=self._run_config.verbose,
          reuse_actors=self._tune_config.reuse_actors,
          max_concurrent_trials=self._tune_config.max_concurrent_trials,
          time_budget_s=self._tune_config.time_budget_s,
          trial_name_creator=self._tune_config.trial_name_creator,
          trial_dirname_creator=self._tune_config.trial_dirname_creator,
          _entrypoint=self._entrypoint,
          # TODO(justinvyu): Finalize the local_dir vs. env var API in 2.8.
          # For now, keep accepting both options.
          local_dir=self._run_config.local_dir,
          # Deprecated
          chdir_to_trial_dir=self._tune_config.chdir_to_trial_dir,
      )
  
imports: import copy
  import io
  import os
  import math
  import logging
  from pathlib import Path
  from typing import Any, Callable, Dict, List, Optional, Type, Union, TYPE_CHECKING, Tuple
  import pyarrow.fs
  import ray.cloudpickle
  from ray.util import inspect_serializability
  from ray.air._internal.uri_utils import URI
  from ray.air._internal.usage import AirEntrypoint
  from ray.air.config import RunConfig, ScalingConfig
  from ray.train._internal.storage import StorageContext, get_fs_and_path
  from ray.tune import Experiment, TuneError, ExperimentAnalysis
  from ray.tune.execution.experiment_state import _ResumeConfig
  from ray.tune.tune import _Config
  from ray.tune.registry import is_function_trainable
  from ray.tune.result import _get_defaults_results_dir
  from ray.tune.result_grid import ResultGrid
  from ray.tune.trainable import Trainable
  from ray.tune.tune import run
  from ray.tune.tune_config import TuneConfig
  from ray.tune.utils import flatten_dict
label_desc: Use image node_id 24 for calling the TunerInternal obj's underlying member method code with example usage: obj._get_tune_run_arguments(trainable) and returns: dict