code: def _set_backend():
      """
      根据环境变量或者默认配置文件设置 backend 。
  
       backend 为 paddle 时，我们还将设置部分环境变量以使得 paddle 能够在 fastNLP 中正确运行。
       backend 为 jittor 时，我们将设置 log_silent:1
  
      :return:
      """
      backend = ""
      if FASTNLP_BACKEND in os.environ:
          backend = os.environ[FASTNLP_BACKEND]
      else:
          # 从文件中读取的
          conda_env = os.environ.get("CONDA_DEFAULT_ENV", None)
          if conda_env is None:
              conda_env = "default"
          env_folder = os.path.join(
              os.path.expanduser("~"), ".fastNLP", "envs"
          )
          env_path = os.path.join(env_folder, conda_env + ".json")
          if os.path.exists(env_path):
              try:
                  with open(env_path, "r", encoding="utf8") as f:
                      envs = json.load(f)
                      # print(json.dumps(envs))
                  if FASTNLP_BACKEND in envs:
                      backend = envs[FASTNLP_BACKEND]
                      os.environ[FASTNLP_BACKEND] = backend
                      if (
                          int(os.environ.get(FASTNLP_GLOBAL_RANK, 0))
                          == 0
                      ):
                          print(
                              f"Set fastNLP backend as {backend} based on {env_path}."
                          )
              except BaseException as e:
                  raise e
  
      if backend:
          assert backend in SUPPORT_BACKENDS, (
              f"Right now fastNLP only support the following backends:{SUPPORT_BACKENDS}, "
              f"instead of `{backend}`"
          )
  
      if backend == "paddle":
          assert _module_available(
              backend
          ), f"You must have {backend} available to use {backend} backend."
          assert (
              "paddle" not in sys.modules
          ), "You have to use `set_backend()` before `import paddle`."
          user_visible_devices = os.getenv(USER_CUDA_VISIBLE_DEVICES)
          cuda_visible_devices = os.getenv("CUDA_VISIBLE_DEVICES")
          if (
              "PADDLE_RANK_IN_NODE" in os.environ
              and "FLAGS_selected_gpus" in os.environ
          ):
              # 在分布式子进程下，根据 USER_VISIBLE_DEVICES 得到进程真正占有的设备
              selected_gpus = os.environ["FLAGS_selected_gpus"].split(
                  ","
              )
              if user_visible_devices is not None:
                  # 用户使用 fastNLP 启动了分布式训练
                  # 此时经过 set_backend，用户的设置会保存在 USER_CUDA_VISIBLE_DEVICES 中
                  # 我们需要从中转换为用户找到真正使用的设备编号
                  user_visible_devices = user_visible_devices.split(",")
                  selected_gpus = [
                      user_visible_devices[int(i)]
                      for i in selected_gpus
                  ]
              # 没有找到 USER_CUDA_VISIBLE_DEVICES，说明用户是直接用 launch 启动的
              elif cuda_visible_devices:
                  # 用户设置了可见设备，需要进行转换
                  # 如 CUDA_VISIBLE_DEVICES = 0,2,3 --gpus=0,2,3
                  # 在 rank1 中此时 selected_gpus = ['1']，需要转换为设备 2
                  os.environ[
                      USER_CUDA_VISIBLE_DEVICES
                  ] = cuda_visible_devices
                  cuda_visible_devices = cuda_visible_devices.split(",")
                  selected_gpus = [
                      cuda_visible_devices[int(i)]
                      for i in selected_gpus
                  ]
              else:
                  # 用户没有设置可见设备，则赋值成所有的设备
                  os.environ[USER_CUDA_VISIBLE_DEVICES] = ",".join(
                      map(str, list(range(get_gpu_count())))
                  )
              os.environ["CUDA_VISIBLE_DEVICES"] = ",".join(
                  selected_gpus
              )
              os.environ["FLAGS_selected_gpus"] = ",".join(
                  [str(g) for g in range(len(selected_gpus))]
              )
              os.environ["FLAGS_selected_accelerators"] = ",".join(
                  [str(g) for g in range(len(selected_gpus))]
              )
  
          elif "CUDA_VISIBLE_DEVICES" in os.environ:
              # 主进程中，用户设置了 CUDA_VISIBLE_DEVICES
              # 将用户设置的 CUDA_VISIBLE_DEVICES hack 掉
              CUDA_VISIBLE_DEVICES = os.environ["CUDA_VISIBLE_DEVICES"]
              os.environ[
                  USER_CUDA_VISIBLE_DEVICES
              ] = CUDA_VISIBLE_DEVICES
              os.environ[
                  "CUDA_VISIBLE_DEVICES"
              ] = CUDA_VISIBLE_DEVICES.split(",")[0]
          else:
              # 没有设置的话限制在单卡上，防止多进程时占用别的卡
              os.environ["CUDA_VISIBLE_DEVICES"] = "0"
              os.environ[USER_CUDA_VISIBLE_DEVICES] = ",".join(
                  map(str, list(range(get_gpu_count())))
              )
  
      elif backend == "jittor":
          assert _module_available(
              backend
          ), f"You must have {backend} available to use {backend} backend."
          if "log_silent" not in os.environ:
              os.environ["log_silent"] = "1"
          if "CUDA_VISIBLE_DEVICES" in os.environ:
              os.environ["use_cuda"] = "1"
  
      elif backend == "torch":
          assert _module_available(
              backend
          ), f"You must have {backend} available to use {backend} backend."
  
      if (
          "PADDLE_RANK_IN_NODE" in os.environ
          and "FLAGS_selected_gpus" in os.environ
          and "USER_CUDA_VISIBLE_DEVICES" not in os.environ
      ):
          # 当用户没有设置 backend 并且使用 launch 启动了多卡，应该提醒用户进行设置
          raise RuntimeError(
              "To run paddle distributed training, please set `FASTNLP_BACKEND` to 'paddle' before using FastNLP."
          )
  
file_path: fastNLP/fastNLP/envs/set_backend.py
imports: import os
  import json
  import sys
  from collections import defaultdict
  from fastNLP.envs.env import FASTNLP_BACKEND, FASTNLP_GLOBAL_RANK, USER_CUDA_VISIBLE_DEVICES, FASTNLP_GLOBAL_SEED
  from fastNLP.envs.utils import _module_available, get_gpu_count
label_desc: Use image node_id 1 for calling a global function with example usage: _set_backend() without return types