<jupyter_start><jupyter_text>Utils> get system info<jupyter_code>#export
def get_env(name):
    "Return env var value if it's defined and not an empty string, or return Unknown"
    res = os.environ.get(name,'')
    return res if len(res) else "Unknown"
#export
def try_import(module):
    "Try to import `module`. Returns module's object on success, None on failure"
    try: return importlib.import_module(module)
    except: return None
#export
def show_install(show_nvidia_smi:bool=False):
    "Print user's setup information"

    import platform

    rep = []
    opt_mods = []

    rep.append(["=== Software ===", None])
    rep.append(["python", platform.python_version()])
    rep.append(["fastai", fastai.__version__])
    rep.append(["fastprogress", fastprogress.__version__])
    rep.append(["torch",  torch.__version__])

    # nvidia-smi
    cmd = "nvidia-smi"
    have_nvidia_smi = False
    try: result = subprocess.run(cmd.split(), shell=False, check=False, stdout=subprocess.PIPE)
    except: pass
    else:
        if result.returncode == 0 and result.stdout: have_nvidia_smi = True

    # XXX: if nvidia-smi is not available, another check could be:
    # /proc/driver/nvidia/version on most systems, since it's the
    # currently active version

    if have_nvidia_smi:
        smi = result.stdout.decode('utf-8')
        # matching: "Driver Version: 396.44"
        match = re.findall(r'Driver Version: +(\d+\.\d+)', smi)
        if match: rep.append(["nvidia driver", match[0]])

    available = "available" if torch.cuda.is_available() else "**Not available** "
    rep.append(["torch cuda", f"{torch.version.cuda} / is {available}"])

    # no point reporting on cudnn if cuda is not available, as it
    # seems to be enabled at times even on cpu-only setups
    if torch.cuda.is_available():
        enabled = "enabled" if torch.backends.cudnn.enabled else "**Not enabled** "
        rep.append(["torch cudnn", f"{torch.backends.cudnn.version()} / is {enabled}"])

    rep.append(["\n=== Hardware ===", None])

    # it's possible that torch might not see what nvidia-smi sees?
    gpu_total_mem = []
    nvidia_gpu_cnt = 0
    if have_nvidia_smi:
        try:
            cmd = "nvidia-smi --query-gpu=memory.total --format=csv,nounits,noheader"
            result = subprocess.run(cmd.split(), shell=False, check=False, stdout=subprocess.PIPE)
        except:
            print("have nvidia-smi, but failed to query it")
        else:
            if result.returncode == 0 and result.stdout:
                output = result.stdout.decode('utf-8')
                gpu_total_mem = [int(x) for x in output.strip().split('\n')]
                nvidia_gpu_cnt = len(gpu_total_mem)


    if nvidia_gpu_cnt: rep.append(["nvidia gpus", nvidia_gpu_cnt])

    torch_gpu_cnt = torch.cuda.device_count()
    if torch_gpu_cnt:
        rep.append(["torch devices", torch_gpu_cnt])
        # information for each gpu
        for i in range(torch_gpu_cnt):
            rep.append([f"  - gpu{i}", (f"{gpu_total_mem[i]}MB | " if gpu_total_mem else "") + torch.cuda.get_device_name(i)])
    else:
        if nvidia_gpu_cnt:
            rep.append([f"Have {nvidia_gpu_cnt} GPU(s), but torch can't use them (check nvidia driver)", None])
        else:
            rep.append([f"No GPUs available", None])


    rep.append(["\n=== Environment ===", None])

    rep.append(["platform", platform.platform()])

    if platform.system() == 'Linux':
        distro = try_import('distro')
        if distro:
            # full distro info
            rep.append(["distro", ' '.join(distro.linux_distribution())])
        else:
            opt_mods.append('distro');
            # partial distro info
            rep.append(["distro", platform.uname().version])

    rep.append(["conda env", get_env('CONDA_DEFAULT_ENV')])
    rep.append(["python", sys.executable])
    rep.append(["sys.path", "\n".join(sys.path)])

    keylen = max([len(e[0]) for e in rep if e[1] is not None])
    for e in rep:
        print(f"{e[0]:{keylen}}", (f": {e[1]}" if e[1] is not None else ""))

    if have_nvidia_smi:
        if show_nvidia_smi: print(f"\n{smi}")
    else:
        if torch_gpu_cnt: print("no nvidia-smi is found")
        else: print("no supported gpus found on this system")
show_install()<jupyter_output>=== Software === 
python        : 3.8.5
fastai        : 2.0.14
fastprogress  : 0.2.7
torch         : 1.6.0
torch cuda    : 10.2 / is available
torch cudnn   : 7605 / is enabled

=== Hardware === 
torch devices : 2
  - gpu0      : Quadro RTX 8000
  - gpu1      : GeForce RTX 2070 SUPER

=== Environment === 
platform      : Linux-5.4.0-42-generic-x86_64-with-glibc2.10
distro        : #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020
conda env     : fastai
python        : /home/tcapelle/miniconda3/envs/fastai/bin/python
sys.path      : /home/tcapelle/SteadySun/fastbench
/home/tcapelle/miniconda3/envs/fastai/lib/python38.zip
/home/tcapelle/miniconda3/envs/fastai/lib/python3.8
/home/tcapelle/miniconda3/envs/fastai/lib/python3.8/lib-dynload

/home/tcapelle/miniconda3/envs/fastai/lib/python3.8/site-packages
/home/tcapelle/Apps/fastai
/home/tcapelle/Apps/nbdev
/home/tcapelle/miniconda3/envs/fastai/lib/python3.8/site-packages/IPython/extensions
/home/tcapelle/.ipython
no nvidia-smi is found<jupyter_text>Reporting> some functions to create reports<jupyter_code>from fastbench.vision import train_imagenette
learn = train_imagenette(gpu=0, epochs=1, runs=1)<jupyter_output>arch='xresnet50'; opt='ranger'; epochs=1; bs=64; fp16=0; runs=1
 Run: 0<jupyter_text>Export -<jupyter_code>#hide
from nbdev.export import notebook2script
notebook2script()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13928508540235957, 'fraction_numerical': 0.0183130832893115, 'mean_word_length': 3.098124098124098, 'pattern_counts': {'":': 1, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '32639', 'license': 'Apache-2.0', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1970, 'n_words': 442, 'path': 'nbs/09_utils.ipynb', 'repo_name': 'tcapelle/fastbench', 'size': '5679'}