Server - 测试 GPU 的显卡使用率与张量之间的关系

本文介绍了NVIDIAA100GPU,该GPU基于Ampere架构,适用于AI训练、数据分析和HPC。其特性包括支持TF32精度,提供20倍于V100的训练速度,采用MIG技术实现资源灵活分配,以及结构稀疏性提升推理性能。文章还展示了使用40GB版本的GPU进行测试,实际占用接近58GB显存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

欢迎关注我的CSDN:https://spike.blog.csdn.net/
本文地址:https://blog.csdn.net/caroline_wendy/article/details/131331049

GPU
NVIDIA A100 是一款基于 Ampere 架构的高性能 GPU,专为 AI、数据分析和高性能计算等应用场景设计。NVIDIA A100 具有以下特点:

  • 支持 Tensor Float (TF32) 精度,可在不修改代码的情况下提供比前代 V100 高 20 倍的 AI 训练速度。
  • 支持多实例 GPU (MIG) 技术,可将一个 A100 分割为最多七个独立的 GPU 实例,实现灵活的资源分配和利用率提升。
  • 支持结构稀疏性,可在保持精度的同时将推理性能提升高达两倍。
  • 提供 40GB 和 80GB 显存两种版本,可处理超大规模的模型和数据集。
  • 配合 NVIDIA 的软件、网络和硬件解决方案,可构建强大的端到端 AI 和 HPC 数据中心平台。

当前张量占用:10000 * 10000 * 150 = 15 ^ 9,一个float占用4位,约60G。

测试:

nohup python3 run_model.py -s 10000 > nohup.run.out &

NVIDIA A100 实际占用 58169M = 58G,相差不多:

Wed Jun 21 08:55:41 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.57.02    Driver Version: 470.57.02    CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA A100-SXM...  On   | 00000000:62:00.0 Off |                    0 |
| N/A   32C    P0    77W / 400W |  58169MiB / 81251MiB |      0%      Default |
|                               |                      |             Disabled |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
+-----------------------------------------------------------------------------+

源码:

import argparse
import math
import time

import torch


def run(size=10000, nums=None):
    # 获取可用的GPU数量
    # num_gpus = torch.cuda.device_count()
    size = size * 2048 * 1024
    if not nums:
        num_gpus = [i for i in range(torch.cuda.device_count())]
    else:
        num_gpus = nums
    print(f"[Info] num_gpus: {num_gpus}")
    # 创建一个空的列表,用来存储分配给每个GPU的张量
    tensors = []
    x = int(math.sqrt(size))
    y = 128
    print(f"[Info] size: {x * x * y * 4 / 1024 / 1024:0.4f} G")
    print("[Info] test start")
    # 设置一个无限循环
    while True:
        # 对每个GPU进行循环
        for i in num_gpus:
            # 设置当前设备为第i个GPU
            torch.cuda.set_device(i)
            for j in range(y):
                # 创建一个很大的随机张量,占用大量的显存
                tensor = torch.randn(x, x).cuda(i)
                # 将张量添加到列表中
                tensors.append(tensor)
        # 等待所有的张量计算完成
        torch.cuda.synchronize()
        # 清空列表,释放显存
        tensors.clear()
        # 等待一秒钟,防止过度占用CPU资源
        time.sleep(1)


def main():
    print(f"[Info] torch version: {torch.__version__}")
    print(f"[Info] is_available: {torch.cuda.is_available()}")
    # for i in range(torch.cuda.device_count()):
    # print(torch.cuda.get_device_properties(i).name)

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-s",
        "--size",
        type=int,
        help="size of tensor (G)",
        default=50,
        required=False,
    )
    parser.add_argument(
        "-n",
        "--nums",
        type=str,
        default="0",
        required=True,
    )
    args = parser.parse_args()
    size = int(args.size)
    nums = [int(i) for i in str(args.nums).split(",")]
    print(f"[Info] size: {size} nums: {nums}")
    run(size, nums)
    print(f"[Info] over")


if __name__ == '__main__':
    main()

运行:

nohup python run_cuda.py -s 60 -n 1,2,3,4,5,6,7 > nohup.run.out &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ManonLegrand

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值