开发者的福音:PyTorch 2.5现已支持英特尔独立显卡训练

openlab_96bf3613 更新于 27天前

《PyTorch 2.5重磅更新:性能优化+新特性》中的一个新特性就是:正式支持在英特尔®独立显卡上训练模型!

本文将在Intel®Core™ Ultra 7 155H自带的Arc™集成显卡上展示使用Pytorch2.5搭建并训练AI模型的全流程。

一. 搭建开发环境

首先,请安装显卡驱动,参考指南:

https://dgpu-docs.intel.com/driver/client/overview.html

然后,请下载并安装Anaconda,链接↓↓

https://www.anaconda.com/download

并用下面的命令创建并激活名为pytorch_arc的虚拟环境:

conda create -n pytorch_arc python=3.11    #创建虚拟环境conda activate pytorch_arc                 #激活虚拟环境python -m pip install --upgrade pip        #升级pip到最新版本

接着,安装Pytorch XPU版;

pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/xpu

滑动查看更多

最后,执行命令,验证安装。看到返回结果为“True”,证明环境搭建成功!

>>> import torch>>> torch.xpu.is_available()

二. 训练ResNet模型

执行下载的训练代码,实现在Intel® Arc™集成显卡上训练ResNet50模型。代码下载链接:

https://gitee.com/Pauntech/Pytorch-2.5
import torchimport torchvisionLR = 0.001DOWNLOAD = TrueDATA = "datasets/cifar10/"transform = torchvision.transforms.Compose(    [        torchvision.transforms.Resize((224, 224)),        torchvision.transforms.ToTensor(),        torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),    ])train_dataset = torchvision.datasets.CIFAR10(    root=DATA,    train=True,    transform=transform,    download=DOWNLOAD,)train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=128)train_len = len(train_loader)model = torchvision.models.resnet50()criterion = torch.nn.CrossEntropyLoss()optimizer = torch.optim.SGD(model.parameters(), lr=LR, momentum=0.9)model.train()model = model.to("xpu")criterion = criterion.to("xpu")print(f"Initiating training")for batch_idx, (data, target) in enumerate(train_loader):    data = data.to("xpu")    target = target.to("xpu")    optimizer.zero_grad()    output = model(data)    loss = criterion(output, target)    los***ackward()    optimizer.step()    if (batch_idx + 1) % 10 == 0:         iteration_loss = loss.item()         print(f"Iteration [{batch_idx+1}/{train_len}], Loss: {iteration_loss:.4f}")torch.save(    {        "model_state_dict": model.state_dict(),        "optimizer_state_dict": optimizer.state_dict(),    },    "checkpoint.pth",)print("Execution finished")

三. 总结

使用PyTorch在英特尔独立显卡上训练模型将为AI行业新增计算硬件选择!

0个评论