env.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Copyright 2020 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import contextlib
  15. import io
  16. import os
  17. import platform
  18. from argparse import ArgumentParser
  19. import huggingface_hub
  20. from .. import __version__ as version
  21. from ..integrations.deepspeed import is_deepspeed_available
  22. from ..utils import (
  23. is_accelerate_available,
  24. is_flax_available,
  25. is_tf_available,
  26. is_torch_available,
  27. is_torch_hpu_available,
  28. is_torch_npu_available,
  29. is_torch_xpu_available,
  30. )
  31. from . import BaseTransformersCLICommand
  32. def info_command_factory(_):
  33. return EnvironmentCommand()
  34. def download_command_factory(args):
  35. return EnvironmentCommand(args.accelerate_config_file)
  36. class EnvironmentCommand(BaseTransformersCLICommand):
  37. @staticmethod
  38. def register_subcommand(parser: ArgumentParser):
  39. download_parser = parser.add_parser("env")
  40. download_parser.set_defaults(func=info_command_factory)
  41. download_parser.add_argument(
  42. "--accelerate-config_file",
  43. default=None,
  44. help="The accelerate config file to use for the default values in the launching script.",
  45. )
  46. download_parser.set_defaults(func=download_command_factory)
  47. def __init__(self, accelerate_config_file, *args) -> None:
  48. self._accelerate_config_file = accelerate_config_file
  49. def run(self):
  50. import safetensors
  51. safetensors_version = safetensors.__version__
  52. accelerate_version = "not installed"
  53. accelerate_config = accelerate_config_str = "not found"
  54. if is_accelerate_available():
  55. import accelerate
  56. from accelerate.commands.config import default_config_file, load_config_from_file
  57. accelerate_version = accelerate.__version__
  58. # Get the default from the config file.
  59. if self._accelerate_config_file is not None or os.path.isfile(default_config_file):
  60. accelerate_config = load_config_from_file(self._accelerate_config_file).to_dict()
  61. accelerate_config_str = (
  62. "\n".join([f"\t- {prop}: {val}" for prop, val in accelerate_config.items()])
  63. if isinstance(accelerate_config, dict)
  64. else f"\t{accelerate_config}"
  65. )
  66. pt_version = "not installed"
  67. pt_cuda_available = "NA"
  68. pt_accelerator = "NA"
  69. if is_torch_available():
  70. import torch
  71. pt_version = torch.__version__
  72. pt_cuda_available = torch.cuda.is_available()
  73. pt_xpu_available = is_torch_xpu_available()
  74. pt_npu_available = is_torch_npu_available()
  75. pt_hpu_available = is_torch_hpu_available()
  76. if pt_cuda_available:
  77. pt_accelerator = "CUDA"
  78. elif pt_xpu_available:
  79. pt_accelerator = "XPU"
  80. elif pt_npu_available:
  81. pt_accelerator = "NPU"
  82. elif pt_hpu_available:
  83. pt_accelerator = "HPU"
  84. tf_version = "not installed"
  85. tf_cuda_available = "NA"
  86. if is_tf_available():
  87. import tensorflow as tf
  88. tf_version = tf.__version__
  89. try:
  90. # deprecated in v2.1
  91. tf_cuda_available = tf.test.is_gpu_available()
  92. except AttributeError:
  93. # returns list of devices, convert to bool
  94. tf_cuda_available = bool(tf.config.list_physical_devices("GPU"))
  95. deepspeed_version = "not installed"
  96. if is_deepspeed_available():
  97. # Redirect command line output to silence deepspeed import output.
  98. with contextlib.redirect_stdout(io.StringIO()):
  99. import deepspeed
  100. deepspeed_version = deepspeed.__version__
  101. flax_version = "not installed"
  102. jax_version = "not installed"
  103. jaxlib_version = "not installed"
  104. jax_backend = "NA"
  105. if is_flax_available():
  106. import flax
  107. import jax
  108. import jaxlib
  109. flax_version = flax.__version__
  110. jax_version = jax.__version__
  111. jaxlib_version = jaxlib.__version__
  112. jax_backend = jax.lib.xla_bridge.get_backend().platform
  113. info = {
  114. "`transformers` version": version,
  115. "Platform": platform.platform(),
  116. "Python version": platform.python_version(),
  117. "Huggingface_hub version": huggingface_hub.__version__,
  118. "Safetensors version": f"{safetensors_version}",
  119. "Accelerate version": f"{accelerate_version}",
  120. "Accelerate config": f"{accelerate_config_str}",
  121. "DeepSpeed version": f"{deepspeed_version}",
  122. "PyTorch version (accelerator?)": f"{pt_version} ({pt_accelerator})",
  123. "Tensorflow version (GPU?)": f"{tf_version} ({tf_cuda_available})",
  124. "Flax version (CPU?/GPU?/TPU?)": f"{flax_version} ({jax_backend})",
  125. "Jax version": f"{jax_version}",
  126. "JaxLib version": f"{jaxlib_version}",
  127. "Using distributed or parallel set-up in script?": "<fill in>",
  128. }
  129. if is_torch_available():
  130. if pt_cuda_available:
  131. info["Using GPU in script?"] = "<fill in>"
  132. info["GPU type"] = torch.cuda.get_device_name()
  133. elif pt_xpu_available:
  134. info["Using XPU in script?"] = "<fill in>"
  135. info["XPU type"] = torch.xpu.get_device_name()
  136. elif pt_hpu_available:
  137. info["Using HPU in script?"] = "<fill in>"
  138. info["HPU type"] = torch.hpu.get_device_name()
  139. elif pt_npu_available:
  140. info["Using NPU in script?"] = "<fill in>"
  141. info["NPU type"] = torch.npu.get_device_name()
  142. info["CANN version"] = torch.version.cann
  143. print("\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n")
  144. print(self.format_dict(info))
  145. return info
  146. @staticmethod
  147. def format_dict(d):
  148. return "\n".join([f"- {prop}: {val}" for prop, val in d.items()]) + "\n"