class_docstr: 实现 ``fairscale`` 功能的 ``Driver`` 。
  
  :param model: 传入给 ``Trainer`` 的 ``model`` 参数。
  :param parallel_device: 用于分布式训练的 ``gpu`` 设备。
  :param is_pull_by_torch_run: 标志当前的脚本的启动是否由 ``python -m torch.distributed.launch`` 启动的。
  :param fp16: 是否开启 fp16 训练。
  :param fairscale_kwargs:
  
      * *oss_kwargs* --
      * *sdp_kwargs* --
      * *fsdp_kwargs* --
      * *ddp_kwargs* --
      * *set_grad_to_none* -- 是否在训练过程中在每一次 optimizer 更新后将 grad 置为 ``None``
      * *non_blocking* -- 表示用于 :meth:`torch.Tensor.to` 方法的参数 non_blocking
      * *gradscaler_kwargs* -- 用于 ``fp16=True`` 时，提供给 :class:`torch.amp.cuda.GradScaler` 的参数
  :kwargs:
      * *wo_auto_param_call* (``bool``) -- 是否关闭在训练时调用我们的 ``auto_param_call`` 函数来自动匹配 batch 和前向函数的参数的行为
  
      .. note::
  
          关于该参数的详细说明，请参见 :class:`~fastNLP.core.controllers.Trainer` 中的描述；函数 ``auto_param_call`` 详见 :func:`fastNLP.core.utils.auto_param_call`。
class_docstr_tok: ["\u5b9e\u73b0","``","fairscale","``","\u529f\u80fd\u7684","``","Driver","``","\u3002",":","param","model",":","\u4f20\u5165\u7ed9","``","Trainer","``","\u7684","``","model","``","\u53c2\u6570\u3002",":","param","parallel_device",":","\u7528\u4e8e\u5206\u5e03\u5f0f\u8bad\u7ec3\u7684","``","gpu","``","\u8bbe\u5907\u3002",":","param","is_pull_by_torch_run",":","\u6807\u5fd7\u5f53\u524d\u7684\u811a\u672c\u7684\u542f\u52a8\u662f\u5426\u7531","``","python","-m","torch.distributed.launch","``","\u542f\u52a8\u7684\u3002",":","param","fp16",":","\u662f\u5426\u5f00\u542f","fp16","\u8bad\u7ec3\u3002",":","param","fairscale_kwargs",":","*","*","oss_kwargs","*","--","*","*","sdp_kwargs","*","--","*","*","fsdp_kwargs","*","--","*","*","ddp_kwargs","*","--","*","*","set_grad_to_none","*","--","\u662f\u5426\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u5728\u6bcf\u4e00\u6b21","optimizer","\u66f4\u65b0\u540e\u5c06","grad","\u7f6e\u4e3a","``","None","``","*","*","non_blocking","*","--","\u8868\u793a\u7528\u4e8e",":","meth",":","`","torch.Tensor.to","`","\u65b9\u6cd5\u7684\u53c2\u6570","non_blocking","*","*","gradscaler_kwargs","*","--","\u7528\u4e8e","``","fp16=True","``","\u65f6\uff0c\u63d0\u4f9b\u7ed9",":","class",":","`","torch.amp.cuda.GradScaler","`","\u7684\u53c2\u6570",":","kwargs",":","*","*","wo_auto_param_call","*","(","``","bool","``",")","--","\u662f\u5426\u5173\u95ed\u5728\u8bad\u7ec3\u65f6\u8c03\u7528\u6211\u4eec\u7684","``","auto_param_call","``","\u51fd\u6570\u6765\u81ea\u52a8\u5339\u914d","batch","\u548c\u524d\u5411\u51fd\u6570\u7684\u53c2\u6570\u7684\u884c\u4e3a","..","note",":",":","\u5173\u4e8e\u8be5\u53c2\u6570\u7684\u8be6\u7ec6\u8bf4\u660e\uff0c\u8bf7\u53c2\u89c1",":","class",":","`","~fastNLP.core.controllers.Trainer","`","\u4e2d\u7684\u63cf\u8ff0\uff1b\u51fd\u6570","``","auto_param_call","``","\u8be6\u89c1",":","func",":","`","fastNLP.core.utils.auto_param_call","`","\u3002"]
code: def configure_ddp(self):
      model = _DDPWrappingModel(self.model)
      if self.fs_type == "ddp":
          self.model = DistributedDataParallel(
              # 注意这里的 self.model_device 是 `torch.device` type，因此 self.model_device.index；
              model,
              device_ids=[self.model_device.index],
              **self._ddp_kwargs,
          )
      elif self.fs_type == "sdp":
          sdp_kwargs = self._sdp_kwargs
          sdp_kwargs = {**sdp_kwargs, "module": model}
          sdp_kwargs["reduce_fp16"] = sdp_kwargs.get(
              "reduce_fp16", self.fp16
          )
          oss_lst = []
          for optimizer in self.optimizers:
              oss = OSS(
                  optimizer.param_groups,
                  optim=type(optimizer),
                  **optimizer.defaults,
              )
              oss_lst.append(oss)
          sdp_kwargs["sharded_optimizer"] = oss_lst
          sdp_kwargs[
              "warn_on_trainable_params_changed"
          ] = sdp_kwargs.get("warn_on_trainable_params_changed", False)
          self.model = ShardedDataParallel(**sdp_kwargs)
          self.optimizers = oss_lst
      else:
          assert (
              len(self.optimizers) == 1
          ), "When fs_type='fsdp', only one optimizer is allowed."
          optimizer = self.optimizers[0]
          assert (
              len(optimizer.param_groups) == 1
          ), "Cannot assign parameter specific optimizer parameter for 'fsdp'."
          fsdp_kwargs = self._fdsp_kwargs
          fsdp_kwargs["mixed_precision"] = self.fp16
          fsdp_kwargs["state_dict_on_rank_0_only"] = fsdp_kwargs.get(
              "state_dict_on_rank_0_only", True
          )
          fsdp_kwargs["state_dict_device"] = fsdp_kwargs.get(
              "state_dict_device", torch.device("cpu")
          )
          fsdp_kwargs["compute_device"] = fsdp_kwargs.get(
              "compute_device", self.model_device
          )
          optimizer = self.optimizers[0]
          # wrap_policy = functools.partial(default_auto_wrap_policy, min_num_params=1e6)
          # with enable_wrap(wrapper_cls=FullyShardedDataParallel, auto_wrap_policy=wrap_policy,
          #                  **fsdp_kwargs):
          #     model = auto_wrap(model)
          fsdp_kwargs = {**fsdp_kwargs, "module": model}
          self.model = None  # 释放掉
          self.model = FullyShardedDataParallel(**fsdp_kwargs).to(
              self.model_device
          )
          self.optimizers = type(optimizer)(
              self.model.parameters(), **optimizer.defaults
          )
  
      self._has_ddpwrapped = True
  
imports: from typing import List, Sequence, Union, Dict, Mapping
  from pathlib import Path
  import os
  import functools
  from fastNLP.envs.imports import _NEED_IMPORT_FAIRSCALE
  from ...log import logger
  from .utils import _DDPWrappingModel
  from .ddp import TorchDDPDriver
  from .torch_driver import TorchDriver
  from .utils import _build_fp16_env
  from ....envs.distributed import all_rank_call_context
  from fastNLP.envs import FASTNLP_DISTRIBUTED_CHECK
  from .utils import optimizer_state_to_device
label_desc: Use image node_id 3 for calling the FairScaleDriver obj's underlying member method code with example usage: obj.configure_ddp() without return types