code: def _run_binary_scaling(
      trainer: "pl.Trainer",
      new_size: int,
      batch_arg_name: str,
      max_trials: int,
      params: Dict[str, Any],
  ) -> int:
      """Batch scaling mode where the size is initially is doubled at each iteration until an OOM error is encountered.
  
      Hereafter, the batch size is further refined using a binary search
  
      """
      low = 1
      high = None
      count = 0
      while True:
          garbage_collection_cuda()
  
          # reset after each try
          _reset_progress(trainer)
  
          try:
              # run loop
              _try_loop_run(trainer, params)
              count += 1
              if count > max_trials:
                  break
              # Double in size
              low = new_size
              if high:
                  if high - low <= 1:
                      break
                  midval = (high + low) // 2
                  new_size, changed = _adjust_batch_size(
                      trainer,
                      batch_arg_name,
                      value=midval,
                      desc="succeeded",
                  )
              else:
                  new_size, changed = _adjust_batch_size(
                      trainer,
                      batch_arg_name,
                      factor=2.0,
                      desc="succeeded",
                  )
  
              if not changed:
                  break
  
              # Force the train dataloader to reset as the batch size has changed
              _reset_dataloaders(trainer)
  
          except RuntimeError as exception:
              # Only these errors should trigger an adjustment
              if is_oom_error(exception):
                  # If we fail in power mode, half the size and return
                  garbage_collection_cuda()
  
                  high = new_size
                  midval = (high + low) // 2
                  new_size, _ = _adjust_batch_size(
                      trainer,
                      batch_arg_name,
                      value=midval,
                      desc="failed",
                  )
  
                  # Force the train dataloader to reset as the batch size has changed
                  _reset_dataloaders(trainer)
  
                  if high - low <= 1:
                      break
              else:
                  raise  # some other error not memory related
  
      return new_size
  
file_path: pytorch-lightning/src/lightning/pytorch/tuner/batch_size_scaling.py
imports: import logging
  import os
  import uuid
  from copy import deepcopy
  from typing import Any, Dict, Optional, Tuple
  import lightning.pytorch
  from lightning.pytorch.utilities.memory import garbage_collection_cuda, is_oom_error
  from lightning.pytorch.utilities.parsing import lightning_getattr, lightning_setattr
  from lightning.pytorch.utilities.rank_zero import rank_zero_info, rank_zero_warn
label_desc: Use image node_id 6 for calling a global function with example usage: _run_binary_scaling(trainer, new_size, batch_arg_name, max_trials, params) and returns: new_size