class_docstr: ``TorchSingleDriver`` 是用于 cpu 和 单卡 gpu 运算的 ``driver``。
  
  .. note::
  
      如果您希望使用 ``DataParallel`` 来训练您的模型，您应当自己在 ``Trainer`` 初始化之前初始化好 ``DataParallel``，然后将其传入 ``Trainer`` 中。
  
  :param model: 传入给 ``Trainer`` 的 ``model`` 参数
  :param device: torch.device，当前进程所使用的设备
  :param fp16: 是否开启 fp16
  :param torch_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: ["``","TorchSingleDriver","``","\u662f\u7528\u4e8e","cpu","\u548c","\u5355\u5361","gpu","\u8fd0\u7b97\u7684","``","driver","``","\u3002","..","note",":",":","\u5982\u679c\u60a8\u5e0c\u671b\u4f7f\u7528","``","DataParallel","``","\u6765\u8bad\u7ec3\u60a8\u7684\u6a21\u578b\uff0c\u60a8\u5e94\u5f53\u81ea\u5df1\u5728","``","Trainer","``","\u521d\u59cb\u5316\u4e4b\u524d\u521d\u59cb\u5316\u597d","``","DataParallel","``","\uff0c\u7136\u540e\u5c06\u5176\u4f20\u5165","``","Trainer","``","\u4e2d\u3002",":","param","model",":","\u4f20\u5165\u7ed9","``","Trainer","``","\u7684","``","model","``","\u53c2\u6570",":","param","device",":","torch.device\uff0c\u5f53\u524d\u8fdb\u7a0b\u6240\u4f7f\u7528\u7684\u8bbe\u5907",":","param","fp16",":","\u662f\u5426\u5f00\u542f","fp16",":","param","torch_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 __init__(
      self,
      model,
      device: "torch.device",
      fp16: bool = False,
      torch_kwargs: Dict = None,
      **kwargs,
  ):
      if isinstance(model, DistributedDataParallel):
          raise ValueError(
              "`DistributedDataParallel` is not supported in `TorchSingleDriver`"
          )
  
      cuda_visible_devices = os.environ.get(
          "CUDA_VISIBLE_DEVICES", None
      )
      if cuda_visible_devices == "":
          device = torch.device("cpu")
          logger.info(
              "You have set `CUDA_VISIBLE_DEVICES` to '' in system environment variable, and we are gonna to"
              "use `cpu` instead of `gpu` device."
          )
  
      super(TorchSingleDriver, self).__init__(
          model, fp16=fp16, torch_kwargs=torch_kwargs, **kwargs
      )
  
      if device is None:
          logger.debug(
              "device is not set, fastNLP will try to automatically get it."
          )
          try:
              device = next(model.parameters()).device
              assert isinstance(device, torch.device)
          except:
              raise ValueError(
                  "fastNLP cannot get device automatically, please set device explicitly."
              )
  
      self.model_device = device
  
      self.local_rank = 0
      self.global_rank = 0
      self.world_size = 1
  
file_path: fastNLP/fastNLP/core/drivers/torch_driver/single_device.py
imports: import os
  from typing import Dict, Union, Callable, Tuple, Optional
  from fastNLP.envs.imports import _NEED_IMPORT_TORCH
  from .torch_driver import TorchDriver
  from fastNLP.core.drivers.torch_driver.utils import replace_sampler, replace_batch_sampler
  from fastNLP.core.utils import auto_param_call
  from fastNLP.core.utils.utils import _get_fun_msg
  from fastNLP.core.samplers import ReproducibleBatchSampler, ReproducibleSampler, re_instantiate_sampler, ReproduceBatchSampler
  from fastNLP.core.samplers import RandomSampler
  from fastNLP.core.log import logger
label_desc: Use image node_id 1 to create a new TorchSingleDriver object from inherited base classes: TorchDriver with example: obj = TorchSingleDriver(model, device, fp16, torch_kwargs)