8.3_auto-parallelism

该段内容展示了PyTorch如何实现自动并行计算。通过在不同的GPU上执行矩阵乘法任务,验证了当两个计算任务一起执行时,总执行时间小于分开执行的总和,证明了PyTorch在不同设备上的并行计算效率。
部署运行你感兴趣的模型镜像

8.3 自动并行计算

上一节提到,默认情况下,GPU 操作是异步的。当调用一个使用 GPU 的函数时,这些操作会在特定的设备上排队,但不一定会在稍后执行。这允许我们并行更多的计算,包括 CPU 或其他 GPU 上的操作。
下面看一个简单的例子。

首先导入本节中实验所需的包或模块。注意,需要至少2块GPU才能运行本节实验。

import torch
import time

assert torch.cuda.device_count() >= 2

我们先实现一个简单的计时类。

class Benchmark():  # 本类已保存在d2lzh_pytorch包中方便以后使用
    def __init__(self, prefix=None):
        self.prefix = prefix + ' ' if prefix else ''

    def __enter__(self):
        self.start = time.time()

    def __exit__(self, *args):
        print('%stime: %.4f sec' % (self.prefix, time.time() - self.start))

再定义run函数,令它做20000次矩阵乘法。

def run(x):
    for _ in range(20000):
        y = torch.mm(x, x)

接下来,分别在两块GPU上创建Tensor

x_gpu1 = torch.rand(size=(100, 100), device='cuda:0')
x_gpu2 = torch.rand(size=(100, 100), device='cuda:1')

然后,分别使用它们运行run函数并打印运行所需时间。

with Benchmark('Run on GPU1.'):
    run(x_gpu1)
    torch.cuda.synchronize()

with Benchmark('Then run on GPU2.'):
    run(x_gpu2)
    torch.cuda.synchronize()

输出:

Run on GPU1. time: 0.2989 sec
Then run on GPU2. time: 0.3518 sec

尝试系统能自动并行这两个任务:

with Benchmark('Run on both GPU1 and GPU2 in parallel.'):
    run(x_gpu1)
    run(x_gpu2)
    torch.cuda.synchronize()

输出:

Run on both GPU1 and GPU2 in parallel. time: 0.5076 sec

可以看到,当两个计算任务一起执行时,执行总时间小于它们分开执行的总和。这表明,PyTorch能有效地实现在不同设备上自动并行计算。


注:本节与原书有很多不同,原书传送门

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

mysql_sync_database --warehouse hdfs://hdfs-cluster/user/hive/warehouse/ --database ods --table_prefix ods_r2_ --table_suffix _paimon --catalog_conf metastore=hive --catalog_conf uri=thrift://master-1-1.c-dcd118dc4de41971.us-west-1.emr.aliyuncs.com:9083,thrift://master-1-2.c-dcd118dc4de41971.us-west-1.emr.aliyuncs.com:9083,thrift://master-1-3.c-dcd118dc4de41971.us-west-1.emr.aliyuncs.com:9083 --mysql_conf hostname=rr-2evv848982cw96i13.mysql.rds.aliyuncs.com --mysql_conf username=bigdata_reader --mysql_conf password='21Rzhm9IpRa!' --mysql_conf database-name='repay' --mysql_conf server-time-zone='America/Cancun' --mysql_conf scan.startup.mode='initial' --table_conf bucket=10 --table_conf changelog-producer=lookup --table_conf sink.parallelism=10 --table_conf metastore.partitioned-table=true --table_conf file.format=parquet --metadata_column op_ts --ignore_incompatible true --table_conf num-sorted-run.stop-trigger=2147483647 --table_conf sort-spill-threshold=10 --table_conf snapshot.time-retained='24h' --mysql_conf debezium.min.row.count.to.stream.results=50000 --mysql_conf debezium.tasks.max=10 --mysql_conf debezium.max.queue.size=81920 --mysql_conf debezium.max.batch.size=20480 --mysql_conf debezium.event.deserialization.failure.handling.mode='skip' --mysql_conf debezium.binlog.buffer.size=20480 --mysql_conf debezium.snapshot.fetch.size=40000 --mysql_conf debezium.snapshot.mode='initial' --mysql_conf debezium.database.serverTimezone='America/Cancun' --type_mapping tinyint1-not-bool,to-nullable --mysql_conf scan.incremental.snapshot.enabled=true --mysql_conf scan.incremental.snapshot.chunk.size=40000 --mysql_conf scan.snapshot.fetch.size=100000 --mode combined --mysql_conf server-id='19700-19740' --mysql_conf debezium.deserialization.skip.parse.errors=true 加了debezium.deserialization.skip.parse.errors,但还是报错
11-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

给算法爸爸上香

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

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

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

打赏作者

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

抵扣说明:

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

余额充值