显存占用 显存测试

目录

显存测试

显存占用示例

一个模型多卡占用


显存测试

python 复制代码
import torch

# 计算张量的大小(例如:每个 float 占用 4 字节)
# 40GB = 40 * 1024 * 1024 * 1024 字节
# 每个 float 4 字节,因此需要的 float 数量为 (40 * 1024 * 1024 * 1024) / 4
num_elements = (40 * 1024 * 1024 * 1024) // 4

# 创建一个在 GPU 上的张量
tensor = torch.empty(num_elements, dtype=torch.float32, device='cuda')

print(tensor)

显存占用示例

42G和62G显存

python 复制代码
import time

import torch

# 设置张量的大小
num_elements = (10 * 1024 * 1024 * 1024) // 4  # 42GB 大小
# num_elements = (15 * 1024 * 1024 * 1024) // 4  # 62GB 大小

# 创建两个随机数张量,存放在 GPU 上
tensor_a = torch.rand(num_elements, dtype=torch.float32, device='cuda:3')
tensor_b = torch.rand(num_elements, dtype=torch.float32, device='cuda:3')

# 创建一个用于存储结果的张量
# result_tensor = torch.empty(num_elements, dtype=torch.float32, device='cuda')
index=0
while True:
    result_tensor=tensor_a + tensor_b
    # tensor_a + tensor_b
    time.sleep(0.01)
    index+=1
    print(index)

一个模型多卡占用

python 复制代码
import time

import torch
import torch.nn as nn

# 设置张量的大小
num_elements = (6 * 1024 * 1024 * 1024) // 4  # 40GB 大小

# 确保有两个可用的 GPU
if torch.cuda.device_count() < 2:
    raise RuntimeError("至少需要两块 GPU")

# 创建两个随机数张量,存放在 GPU 上
tensor_a = torch.rand(num_elements , dtype=torch.float32, device='cuda:0')
tensor_b = torch.rand(num_elements , dtype=torch.float32, device='cuda:0')

# 创建一个用于存储结果的张量
result_tensor = torch.empty(num_elements , dtype=torch.float32, device='cuda:1')

class AddModel(nn.Module):
    def forward(self, tensor_a, tensor_b):
        return tensor_a + tensor_b

# 实例化模型并使用 DataParallel
model = AddModel().cuda()
model = nn.DataParallel(model)

index=0
# 不断相加的循环
while True:
    # 使用 DataParallel 进行加法
    result_tensor = model(tensor_a, tensor_b)

    # 将结果存储在第一个 GPU 上
    result_tensor = result_tensor.to('cuda:1')

    time.sleep(0.01)
    index += 1
    print(index)
相关推荐
财经资讯数据_灵砚智能几秒前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月28日
大数据·人工智能·python·信息可视化·自然语言处理·ai编程·灵砚智能
m沐沐几秒前
【机器学习】聚类算法-K-means聚类
人工智能·python·算法·机器学习·pycharm·kmeans·聚类
若鱼文化创意7 分钟前
品牌设计CI规划使用后交付偏差先分项核对验收标准
python·ci/cd
水木流年追梦8 分钟前
大模型入门-大模型优化方法3
人工智能·分布式·python·深度学习·机器学习
悟乙己13 分钟前
因果推断方法实践:Python实现合成控制法
开发语言·python
武子康13 分钟前
调查研究-148 Deepseek-V4-Flash 生成式AI十大高频业务场景落地指南
大数据·人工智能·深度学习·ai·chatgpt·deepseek
lulu121654407815 分钟前
Claude钩子系统架构设计:从执行时序到扩展机制
java·人工智能·python·ai编程
极光代码工作室18 分钟前
基于Spark的电商用户点击流分析系统
大数据·python·数据分析·spark·数据可视化
DreamLife☼22 分钟前
OpenBCI-Python与OpenBCI:实时脑电信号采集实战
开发语言·python·硬件·选型·openbci·cyton·ganglion
AI行业学习24 分钟前
CC-Switch 下载、安装与使用配置指南【2026.5.29】
java·开发语言·vscode·python·eclipse·laravel