system.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright 2025 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. """Contains commands to print information about the environment and version.
  15. Usage:
  16. transformers env
  17. transformers version
  18. """
  19. import contextlib
  20. import io
  21. import os
  22. import platform
  23. from typing import Annotated
  24. import huggingface_hub
  25. import typer
  26. from .. import __version__
  27. from ..integrations.deepspeed import is_deepspeed_available
  28. from ..utils import (
  29. is_accelerate_available,
  30. is_torch_available,
  31. is_torch_hpu_available,
  32. is_torch_npu_available,
  33. is_torch_xpu_available,
  34. )
  35. def env(
  36. accelerate_config_file: Annotated[
  37. str | None,
  38. typer.Argument(help="The accelerate config file to use for the default values in the launching script."),
  39. ] = None,
  40. ) -> None:
  41. """Print information about the environment."""
  42. import safetensors
  43. safetensors_version = safetensors.__version__
  44. accelerate_version = "not installed"
  45. accelerate_config = accelerate_config_str = "not found"
  46. if is_accelerate_available():
  47. import accelerate
  48. from accelerate.commands.config import default_config_file, load_config_from_file
  49. accelerate_version = accelerate.__version__
  50. # Get the default from the config file.
  51. if accelerate_config_file is not None or os.path.isfile(default_config_file):
  52. accelerate_config = load_config_from_file(accelerate_config_file).to_dict()
  53. accelerate_config_str = (
  54. "\n".join([f"\t- {prop}: {val}" for prop, val in accelerate_config.items()])
  55. if isinstance(accelerate_config, dict)
  56. else f"\t{accelerate_config}"
  57. )
  58. pt_version = "not installed"
  59. pt_cuda_available = "NA"
  60. pt_accelerator = "NA"
  61. if is_torch_available():
  62. import torch
  63. pt_version = torch.__version__
  64. pt_cuda_available = torch.cuda.is_available()
  65. pt_xpu_available = is_torch_xpu_available()
  66. pt_npu_available = is_torch_npu_available()
  67. pt_hpu_available = is_torch_hpu_available()
  68. if pt_cuda_available:
  69. pt_accelerator = "CUDA"
  70. elif pt_xpu_available:
  71. pt_accelerator = "XPU"
  72. elif pt_npu_available:
  73. pt_accelerator = "NPU"
  74. elif pt_hpu_available:
  75. pt_accelerator = "HPU"
  76. deepspeed_version = "not installed"
  77. if is_deepspeed_available():
  78. # Redirect command line output to silence deepspeed import output.
  79. with contextlib.redirect_stdout(io.StringIO()):
  80. import deepspeed
  81. deepspeed_version = deepspeed.__version__
  82. info = {
  83. "`transformers` version": __version__,
  84. "Platform": platform.platform(),
  85. "Python version": platform.python_version(),
  86. "Huggingface_hub version": huggingface_hub.__version__,
  87. "Safetensors version": f"{safetensors_version}",
  88. "Accelerate version": f"{accelerate_version}",
  89. "Accelerate config": f"{accelerate_config_str}",
  90. "DeepSpeed version": f"{deepspeed_version}",
  91. "PyTorch version (accelerator?)": f"{pt_version} ({pt_accelerator})",
  92. "Using distributed or parallel set-up in script?": "<fill in>",
  93. }
  94. if is_torch_available():
  95. if pt_cuda_available:
  96. info["Using GPU in script?"] = "<fill in>"
  97. info["GPU type"] = torch.cuda.get_device_name()
  98. elif pt_xpu_available:
  99. info["Using XPU in script?"] = "<fill in>"
  100. info["XPU type"] = torch.xpu.get_device_name()
  101. elif pt_hpu_available:
  102. info["Using HPU in script?"] = "<fill in>"
  103. info["HPU type"] = torch.hpu.get_device_name()
  104. elif pt_npu_available:
  105. info["Using NPU in script?"] = "<fill in>"
  106. info["NPU type"] = torch.npu.get_device_name()
  107. info["CANN version"] = torch.version.cann
  108. print("\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n")
  109. print(_format_dict(info))
  110. return info
  111. def version() -> None:
  112. """Print CLI version."""
  113. print(__version__)
  114. def _format_dict(d: dict) -> str:
  115. return "\n".join([f"- {prop}: {val}" for prop, val in d.items()]) + "\n"