pytorch-lightning
88 строк · 2.7 Кб
1# Copyright The Lightning AI team.
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"""Diagnose your system and show basic information.
15
16This server mainly to get detail info for better bug reporting.
17
18"""
19
20import os21import platform22import sys23
24import pkg_resources25import torch26
27sys.path += [os.path.abspath(".."), os.path.abspath("")]28
29
30LEVEL_OFFSET = "\t"31KEY_PADDING = 2032
33
34def info_system() -> dict:35return {36"OS": platform.system(),37"architecture": platform.architecture(),38"version": platform.version(),39"release": platform.release(),40"processor": platform.processor(),41"python": platform.python_version(),42}43
44
45def info_cuda() -> dict:46return {47"GPU": [torch.cuda.get_device_name(i) for i in range(torch.cuda.device_count())] or None,48"available": torch.cuda.is_available(),49"version": torch.version.cuda,50}51
52
53def info_packages() -> dict:54"""Get name and version of all installed packages."""55packages = {}56for dist in pkg_resources.working_set:57package = dist.as_requirement()58packages[package.key] = package.specs[0][1]59return packages60
61
62def nice_print(details: dict, level: int = 0) -> list:63lines = []64for k in sorted(details):65key = f"* {k}:" if level == 0 else f"- {k}:"66if isinstance(details[k], dict):67lines += [level * LEVEL_OFFSET + key]68lines += nice_print(details[k], level + 1)69elif isinstance(details[k], (set, list, tuple)):70lines += [level * LEVEL_OFFSET + key]71lines += [(level + 1) * LEVEL_OFFSET + "- " + v for v in details[k]]72else:73template = "{:%is} {}" % KEY_PADDING74key_val = template.format(key, details[k])75lines += [(level * LEVEL_OFFSET) + key_val]76return lines77
78
79def main() -> None:80details = {"System": info_system(), "CUDA": info_cuda(), "Packages": info_packages()}81details["Lightning"] = {k: v for k, v in details["Packages"].items() if "torch" in k or "lightning" in k}82lines = nice_print(details)83text = os.linesep.join(lines)84print(f"<details>\n <summary>Current environment</summary>\n\n{text}\n\n</details>")85
86
87if __name__ == "__main__":88main()89