惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园 - 【当耐特】
量子位
Engineering at Meta
Engineering at Meta
博客园_首页
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
V
Visual Studio Blog
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
Jina AI
Jina AI
The Cloudflare Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
博客园 - 聂微东

StudyingLover's Blog

Diffusion Policy笔记 rwkv笔记 act笔记 nanovllm-block_manager opencode多智能体 nanobot-pre-train nanobot-rl nanobot-sft nanobot-checkpoint_manager nanobot-gpt nanobot-mid-train Vision Mamba (Vim)笔记 BPE演示 最后一遍学习Transformer YOLOv5 目标检测笔记 下载根服务器解析记录 Dynaseal A Backend-Controlled LLM API Key Distribution Scheme with Constrained Invocation Parameters 判断链表有环 王道25数据结构勘误 关于perplexity的open-sourcing-r1-1776 AI为什么不像人类一样进行多轮对话 新博客改造日记和功能测试 linuxqq只显示登陆背景图 数字设计和计算机体系结构(机械工业出版社)勘误(自制) Dynaseal:面向未来端侧llm agent的llm api key分发机制 A Definitive Guide to Markdown Style This post is using MDX, Where you can embed JSX and Astro components RT-Patch学习 pydantic实现的LLM ReAct fastapi 和 uvicorn 设置监听 ipv6
在终端绘制GPU显存使用曲线
About the Author StudyingLover · 2023-08-13 · via StudyingLover's Blog

在终端绘制GPU显存使用曲线

这个东西的灵感来自于写torch的时候想实时看到loss和gpu使用情况,突然想到可以在终端实时显示,经过与ai的一番激烈讨,最终有了这个代码。

我们首先要获取GPU的显存使用数据,先检查是否安装了nvidia-smi, 在终端输入有正常输出即可。

首先导入所有需要的库

import subprocess
import time
import asciichartpy
import platform

通过nvidia-smi 的命令获取已经使用的显存和所有现存

def get_gpu_used_memory():
	output = subprocess.check_output(['nvidia-smi', '--query-gpu=memory.used', '--format=csv,nounits'])
	output = output.decode('utf-8')
	lines = output.strip().split('\n')
	used_memory = int(lines[1])
	return used_memory
  
def get_gpu_total_memory():
	output = subprocess.check_output(['nvidia-smi', '--query-gpu=memory.total', '--format=csv,nounits'])
	output = output.decode('utf-8')
	lines = output.strip().split('\n')
	total_memory = int(lines[1])
	return total_memory

asciichartpy 是一个 Python 库,用于在终端中绘制 ASCII 图表。我们用他来在终端绘制图标。

def draw_gpu_memory(gpu_memory_history):
    used_memory = get_gpu_used_memory()
    total_memory = get_gpu_total_memory()

    used_percentage = used_memory / total_memory * 100
    gpu_memory_history.append(used_percentage)

    # 绘制字符图表
    chart = asciichartpy.plot(gpu_memory_history, {'height': 20, 'width': 10, 'timestamp': True})
    
    # 清空终端屏幕
    if platform.system() == 'Windows':
        subprocess.call('cls', shell=True)
    else:
        subprocess.call('clear', shell=True)
    
    print(chart)

最后运行上面的代码

while True:
    try:
        gpu_memory_history = []
        while True:
            draw_gpu_memory(gpu_memory_history)
            time.sleep(1)
    except KeyboardInterrupt:
        break

运行效果 image.png