显存占用 显存测试

目录

显存测试

显存占用示例

一个模型多卡占用


显存测试

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)
相关推荐
掘金-我是哪吒1 小时前
分布式微服务系统架构第132集:Python大模型,fastapi项目-Jeskson文档-微服务分布式系统架构
分布式·python·微服务·架构·系统架构
四口鲸鱼爱吃盐1 小时前
BMVC2023 | 多样化高层特征以提升对抗迁移性
人工智能·深度学习·cnn·vit·对抗攻击·迁移攻击
Douglassssssss2 小时前
【深度学习】使用块的网络(VGG)
网络·人工智能·深度学习
xhdll2 小时前
egpo进行train_egpo训练时,keyvalueError:“replay_sequence_length“
python·egpo
終不似少年遊*2 小时前
【从基础到模型网络】深度学习-语义分割-ROI
人工智能·深度学习·卷积神经网络·语义分割·fcn·roi
Cchaofan2 小时前
lesson01-PyTorch初见(理论+代码实战)
人工智能·pytorch·python
网络小白不怕黑2 小时前
Python Socket编程:实现简单的客户端-服务器通信
服务器·网络·python
Ronin-Lotus3 小时前
程序代码篇---python获取http界面上按钮或者数据输入
python·http
摆烂仙君3 小时前
南京邮电大学金工实习答案
人工智能·深度学习·aigc
不知道写什么的作者3 小时前
Flask快速入门和问答项目源码
后端·python·flask