显存占用 显存测试

目录

显存测试

显存占用示例

一个模型多卡占用


显存测试

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)
相关推荐
xingshanchang36 分钟前
PyTorch 不支持旧GPU的异常状态与解决方案:CUDNN_STATUS_NOT_SUPPORTED_ARCH_MISMATCH
人工智能·pytorch·python
昵称是6硬币3 小时前
YOLOv11: AN OVERVIEW OF THE KEY ARCHITECTURAL ENHANCEMENTS目标检测论文精读(逐段解析)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
费弗里4 小时前
Python全栈应用开发利器Dash 3.x新版本介绍(1)
python·dash
李少兄9 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python
heimeiyingwang9 天前
【深度学习加速探秘】Winograd 卷积算法:让计算效率 “飞” 起来
人工智能·深度学习·算法
学Linux的语莫9 天前
python基础语法
开发语言·python
匿名的魔术师9 天前
实验问题记录:PyTorch Tensor 也会出现 a = b 赋值后,修改 a 会影响 b 的情况
人工智能·pytorch·python
Ven%9 天前
PyTorch 张量(Tensors)全面指南:从基础到实战
人工智能·pytorch·python