code: def deepspeed_init(
      trainer, num_training_steps, resume_from_checkpoint=None
  ):
      """
      Init DeepSpeed, after updating the DeepSpeed configuration with any relevant Trainer's args.
  
      If ``resume_from_checkpoint`` was passed then an attempt to resume from a previously saved checkpoint will be made.
  
      Args:
          trainer: Trainer object
          num_training_steps: per single gpu
          resume_from_checkpoint: path to a checkpoint if to resume from after normal DeepSpeedEngine load
  
      Returns: model, optimizer, lr_scheduler
  
      """
      import deepspeed
      from deepspeed.utils import logger as ds_logger
  
      model = trainer.model
      args = trainer.args
  
      hf_deepspeed_config = args.hf_deepspeed_config
      hf_deepspeed_config.trainer_config_finalize(
          args, model, num_training_steps
      )
  
      # resume config update - some bits like `model` and `num_training_steps` only become available during train
      config = hf_deepspeed_config.config
  
      # Optimizer + Scheduler
      # Currently supported combos:
      # 1. DS scheduler + DS optimizer: Yes
      # 2. HF scheduler + HF optimizer: Yes
      # 3. DS scheduler + HF optimizer: Yes
      # 4. HF scheduler + DS optimizer: Yes
      #
      # Unless Offload is enabled in which case it's:
      # 1. DS scheduler + DS optimizer: Yes
      # 2. HF scheduler + HF optimizer: Mostly*
      # 3. DS scheduler + HF optimizer: Mostly*
      # 4. HF scheduler + DS optimizer: Yes
      #
      # Mostly*: All non-native DeepSpeed optimizers that have both CPU and GPU implementation should work (except LAMB)
  
      optimizer = None
      if "optimizer" in config:
          if args.adafactor:
              raise ValueError(
                  "--adafactor was passed, but also found `optimizer` configured in the DeepSpeed config. "
                  "Only one optimizer can be configured."
              )
      else:
          if hf_deepspeed_config.is_offload():
              logger.info(
                  "Detected ZeRO Offload and non-DeepSpeed optimizers: This combination should work as long as the custom optimizer has both CPU and GPU implementation (except LAMB)"
              )
  
          # ds supports Adam, OneBitAdam, and Lamb optimizers and can import other optimizers from torch.
          # But trainer uses AdamW by default.
          optimizer = trainer.create_optimizer()
          # To use other optimizers requires voiding warranty with: `zero_allow_untested_optimizer`
          config["zero_allow_untested_optimizer"] = True
  
      def _lr_scheduler_callable(optimizer):
          return trainer.create_scheduler(
              num_training_steps=num_training_steps, optimizer=optimizer
          )
  
      lr_scheduler = None
      if "scheduler" not in config:
          if optimizer is None:
              # Optimizer is not available, so use callable to defer lr_scheduler creation to DS init
              lr_scheduler = _lr_scheduler_callable
          else:
              lr_scheduler = trainer.create_scheduler(
                  num_training_steps=num_training_steps,
                  optimizer=optimizer,
              )
  
      # keep for quick debug:
      # from pprint import pprint; pprint(config)
  
      # set the Deepspeed log level consistent with the trainer
      ds_logger.setLevel(args.get_process_log_level())
  
      model_parameters = filter(
          lambda p: p.requires_grad, model.parameters()
      )
  
      model, optimizer, _, lr_scheduler = deepspeed.initialize(
          model=model,
          model_parameters=model_parameters,
          config_params=config,
          optimizer=optimizer,
          lr_scheduler=lr_scheduler,
      )
  
      if resume_from_checkpoint is not None:
          # it's possible that the user is trying to resume from model_path, which doesn't necessarily
          # contain a deepspeed checkpoint. e.g. examples just check if the dir exists and assume it's
          # a resume from a checkpoint and not just a local pretrained weight. So we check here if the
          # path contains what looks like a deepspeed checkpoint
          import glob
  
          deepspeed_checkpoint_dirs = sorted(
              glob.glob(f"{resume_from_checkpoint}/global_step*")
          )
  
          if len(deepspeed_checkpoint_dirs) > 0:
              logger.info(
                  f"Attempting to resume from {resume_from_checkpoint}"
              )
              # this magically updates self.optimizer and self.lr_scheduler
              load_path, _ = model.load_checkpoint(
                  resume_from_checkpoint,
                  load_optimizer_states=True,
                  load_lr_scheduler_states=True,
              )
              if load_path is None:
                  raise ValueError(
                      f"[deepspeed] failed to resume from checkpoint {resume_from_checkpoint}"
                  )
          else:
              logger.info(
                  f"{resume_from_checkpoint} doesn't have deepspeed checkpoints, doing nothing"
              )
  
      return model, optimizer, lr_scheduler
  
imports: import importlib.util
  import io
  import json
  import weakref
  from copy import deepcopy
  from functools import partialmethod
  from .utils.versions import require_version
  from fastNLP.envs.imports import _NEED_IMPORT_TORCH
  from fastNLP.core.log import logger
label_desc: Use image node_id 5 for calling a global function with example usage: deepspeed_init(trainer, num_training_steps, resume_from_checkpoint) and returns: model,  optimizer,  lr_scheduler, trainer