class_docstr: Trainer for BigDL-Nano pytorch.
  
  This Trainer extends PyTorch Lightning Trainer by adding
  various options to accelerate pytorch training.
class_docstr_tok: ["Trainer","for","BigDL-Nano","pytorch",".","This","Trainer","extends","PyTorch","Lightning","Trainer","by","adding","various","options","to","accelerate","pytorch","training","."]
docstr: A pytorch lightning trainer that uses bigdl-nano optimization.
  
  :param num_processes: number of processes in distributed training. default: ``1``.
  :param use_ipex: whether we use ipex as accelerator for trainer. default: ``False``.
  :param distributed_backend: use which backend in distributed mode, defaults to
      ``'subprocess'``, now avaiable backends are ``'spawn'``, ``'subprocess'`` and ``'ray'``
  :param process_group_backend: use which process group backend in distributed mode, defaults
      to ``None``, means using ``'gloo'`` with CPU, while using ``'nccl'`` with GPU, now
      avaiable backends are ``None`` and ``'ccl'``.
  :param cpu_for_each_process: A list of length ``num_processes``, each containing a list of
      indices of cpus each process will be using. default: ``None``, and the cpu will be
      automatically and evenly distributed among processes.
  :param channels_last: whether convert input to channels last memory formats,
      defaults to ``False``.
  :param auto_lr: whether to scale the learning rate linearly by ``num_processes`` times.
      Defaults to ``True``.
      A dict with ``warmup_epochs`` as key is also accepted to control the number of epochs
      needed for the learning rate to be scaled by ``num_processes`` times.
      If ``auto_lr=Ture``, ``warmup_epochs`` will by default be ``max_epochs // 10``.
      If ``num_processes=1`` or other ``lr_scheduler`` is set, ``auto_lr`` will be ignored.
  :param precision: Double precision (``64``), full precision (``32``),
      half precision (``16``) or bfloat16 precision (``'bf16'``), defaults to ``32``.
      Enable ipex bfloat16 weight prepack when ``use_ipex=True`` and ``precision='bf16'``
docstr_tok: ["A","pytorch","lightning","trainer","that","uses","bigdl-nano","optimization",".",":","param","num_processes",":","number","of","processes","in","distributed","training",".","default",":","``","1","``",".",":","param","use_ipex",":","whether","we","use","ipex","as","accelerator","for","trainer",".","default",":","``","False","``",".",":","param","distributed_backend",":","use","which","backend","in","distributed","mode",",","defaults","to","``","'subprocess","'","``",",","now","avaiable","backends","are","``","'spawn","'","``",",","``","'subprocess","'","``","and","``","'ray","'","``",":","param","process_group_backend",":","use","which","process","group","backend","in","distributed","mode",",","defaults","to","``","None","``",",","means","using","``","'gloo","'","``","with","CPU",",","while","using","``","'nccl","'","``","with","GPU",",","now","avaiable","backends","are","``","None","``","and","``","'ccl","'","``",".",":","param","cpu_for_each_process",":","A","list","of","length","``","num_processes","``",",","each","containing","a","list","of","indices","of","cpus","each","process","will","be","using",".","default",":","``","None","``",",","and","the","cpu","will","be","automatically","and","evenly","distributed","among","processes",".",":","param","channels_last",":","whether","convert","input","to","channels","last","memory","formats",",","defaults","to","``","False","``",".",":","param","auto_lr",":","whether","to","scale","the","learning","rate","linearly","by","``","num_processes","``","times",".","Defaults","to","``","True","``",".","A","dict","with","``","warmup_epochs","``","as","key","is","also","accepted","to","control","the","number","of","epochs","needed","for","the","learning","rate","to","be","scaled","by","``","num_processes","``","times",".","If","``","auto_lr=Ture","``",",","``","warmup_epochs","``","will","by","default","be","``","max_epochs","\/\/","10","``",".","If","``","num_processes=1","``","or","other","``","lr_scheduler","``","is","set",",","``","auto_lr","``","will","be","ignored",".",":","param","precision",":","Double","precision","(","``","64","``",")",",","full","precision","(","``","32","``",")",",","half","precision","(","``","16","``",")","or","bfloat16","precision","(","``","'bf16","'","``",")",",","defaults","to","``","32","``",".","Enable","ipex","bfloat16","weight","prepack","when","``","use_ipex=True","``","and","``","precision='bf16","'","``"]
code: def __init__(
      self,
      num_processes: Optional[int] = None,
      use_ipex: bool = False,
      distributed_backend="subprocess",
      process_group_backend: Optional[str] = None,
      cpu_for_each_process: Optional[List[List[int]]] = None,
      use_hpo=False,
      channels_last: bool = False,
      auto_lr: Union[dict, bool] = True,
      precision: Union[str, int] = 32,
      *args: Any,
      **kwargs: Any,
  ) -> None:
      """
      A pytorch lightning trainer that uses bigdl-nano optimization.
  
      :param num_processes: number of processes in distributed training. default: ``1``.
      :param use_ipex: whether we use ipex as accelerator for trainer. default: ``False``.
      :param distributed_backend: use which backend in distributed mode, defaults to
          ``'subprocess'``, now avaiable backends are ``'spawn'``, ``'subprocess'`` and ``'ray'``
      :param process_group_backend: use which process group backend in distributed mode, defaults
          to ``None``, means using ``'gloo'`` with CPU, while using ``'nccl'`` with GPU, now
          avaiable backends are ``None`` and ``'ccl'``.
      :param cpu_for_each_process: A list of length ``num_processes``, each containing a list of
          indices of cpus each process will be using. default: ``None``, and the cpu will be
          automatically and evenly distributed among processes.
      :param channels_last: whether convert input to channels last memory formats,
          defaults to ``False``.
      :param auto_lr: whether to scale the learning rate linearly by ``num_processes`` times.
          Defaults to ``True``.
          A dict with ``warmup_epochs`` as key is also accepted to control the number of epochs
          needed for the learning rate to be scaled by ``num_processes`` times.
          If ``auto_lr=Ture``, ``warmup_epochs`` will by default be ``max_epochs // 10``.
          If ``num_processes=1`` or other ``lr_scheduler`` is set, ``auto_lr`` will be ignored.
      :param precision: Double precision (``64``), full precision (``32``),
          half precision (``16``) or bfloat16 precision (``'bf16'``), defaults to ``32``.
          Enable ipex bfloat16 weight prepack when ``use_ipex=True`` and ``precision='bf16'``
      """
      # Check keyword arguments
      if "accelerator" in kwargs:
          warning(
              f"""Accelerator will be specified by bigdl-nano,
              accelerator entered {kwargs['accelerator']} will be ignored. """
          )
  
          kwargs.pop("accelerator")
      if "plugins" in kwargs:
          warning(
              f"""Plugins will be specified by bigdl-nano,
               plugines entered {kwargs['plugins']} will be ignored. """
          )
  
          kwargs.pop("plugins")
      if cpu_for_each_process is not None:
          if len(cpu_for_each_process) != num_processes:
              invalidInputError(
                  False,
                  f"The length of `cpu_for_each_process` ("
                  f"{len(cpu_for_each_process)}) is not equal to the number of"
                  f" processes {num_processes}.",
              )
  
      if "algorithms" in kwargs:
          kwargs = self._add_algorithms(kwargs)
  
      if channels_last:
          callbacks = kwargs.get("callbacks")
          if callbacks:
              callbacks.append(ChannelsLastCallback())
          else:
              kwargs["callbacks"] = [ChannelsLastCallback()]
  
      self.use_ipex = use_ipex
      dtype = None
      if self.use_ipex and precision == "bf16":
          # Enable ipex bfloat16 weight prepack and disable pytorch-lightning native AMP
          dtype = torch.bfloat16
          precision = 32
  
      # Confirm if cpu supports avx512
      if self.use_ipex and not _avx512_checker():
          if TORCH_VERSION_LESS_1_11:
              warning(
                  "Enable ipex<=1.11 in a cpu instruction set"
                  " without avx512 will crash."
                  "Fall back to regular pytorch."
              )
              self.use_ipex = False
          elif dtype == torch.bfloat16:
              warning(
                  "Enable IPEX bfloat16 in a cpu instruction set"
                  " without avx512 will crash. "
                  "Using 32-bit precision"
              )
              dtype = None
  
      kwargs["precision"] = precision
  
      if num_processes is None and distributed_backend != "k8s":
          num_processes = 1
  
      if num_processes == 1:
          kwargs["strategy"] = (
              IPEXStrategy(dtype=dtype) if self.use_ipex else None
          )
          super().__init__(*args, **kwargs)
      else:
          invalidInputError(
              distributed_backend in distributed_backends,
              f"Distributed backends supported now are {distributed_backends},"
              f" but get {distributed_backend}.",
          )
          check_ccl(process_group_backend)
          if "checkpoint_callback" in kwargs:
              if not kwargs["checkpoint_callback"]:
                  invalidInputError(
                      False,
                      f"`checkpoint_callback` set to False. "
                      f"Currently, disable checkpoint callback make "
                      f"distributed training backend work incorrect",
                  )
  
          strategy_cls = backends_class_map[distributed_backend]
          strategy = strategy_cls(
              num_processes=num_processes,
              cpu_for_each_process=cpu_for_each_process,
              use_ipex=self.use_ipex,
              dtype=dtype,
              auto_lr=auto_lr,
              process_group_backend=process_group_backend,
          )
  
          kwargs["strategy"] = strategy
          super().__init__(*args, **kwargs)
  
      if use_hpo:
          self.hposearcher = create_hpo_searcher(
              trainer=self, num_processes=num_processes
          )
      else:
          self.hposearcher = None
  
imports: import copy
  from logging import warning
  from typing import Any, List, Optional, Union
  import pytorch_lightning
  import torch
  from torch import nn
  from torch.nn.modules.loss import _Loss
  from torch.utils.data import DataLoader
  from torchmetrics.metric import Metric
  from torch.optim.lr_scheduler import _LRScheduler
  from bigdl.nano.pytorch import InferenceOptimizer
  from bigdl.nano.utils.pytorch import TORCH_VERSION_LESS_1_11, check_ccl
  from bigdl.nano.utils.pytorch import ChannelsLastCallback
  from bigdl.nano.utils.pytorch import save_model, load_model
  from bigdl.nano.pytorch.algorithms import SelectiveBackprop
  from bigdl.nano.pytorch.lightning import LightningModule
  from bigdl.nano.pytorch.strategies import IPEXStrategy, DDPSpawnStrategy, DDPSubprocessStrategy, DDPK8sStrategy
  from bigdl.nano.deps.automl.hpo_api import create_hpo_searcher, check_hpo_status
  from bigdl.nano.deps.ray.ray_api import create_ray_strategy
  from bigdl.nano.utils.common import invalidInputError
  from bigdl.nano.utils.common import _avx512_checker
  from bigdl.nano.utils.common import deprecated
label_desc: Use image node_id 1 to create a new Trainer object from inherited base classes: pl with example: obj = Trainer(num_processes, use_ipex, distributed_backend, process_group_backend, cpu_for_each_process, use_hpo, channels_last, auto_lr, precision)