显存占用 显存测试

目录

显存测试

显存占用示例

一个模型多卡占用


显存测试

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)
相关推荐
用户28628100934013 分钟前
使用 PyTorch 进行模型训练train
pytorch
困死,根本不会42 分钟前
蓝桥杯python备赛笔记之(十)数论基础 & 日期问题
笔记·python·蓝桥杯
輕華1 小时前
Python 命令行参数处理:sys.argv 与 argparse 深度对比
python
王侯相将1 小时前
Claude Code 是什么?
人工智能·深度学习
湘美书院--湘美谈教育1 小时前
湘美书院主理人:AI时代的文雅智能,赏花赏月赏秋香
人工智能·深度学习·神经网络·机器学习·ai写作
清水白石0081 小时前
Python 内存陷阱深度解析——浅拷贝、深拷贝与对象复制的正确姿势
开发语言·python
国家二级编程爱好者1 小时前
删除typora文档没有引用的资源文件
git·python
进击的雷神1 小时前
邮箱编码解码、国际电话验证、主办方过滤、多页面深度爬取——柬埔寨塑料展爬虫四大技术难关攻克纪实
爬虫·python
深蓝电商API2 小时前
多线程 vs 异步 vs 多进程爬虫性能对比
爬虫·python
Dfreedom.2 小时前
从“阅读小说”到循环网络:一篇搞懂循环神经网络(RNN)
人工智能·rnn·深度学习·神经网络