英特尔 GPU 入门¶
硬件先决条件¶
经过验证的硬件 |
支持的操作系统 |
---|---|
英特尔® 数据中心 GPU Max 系列 |
Linux |
英特尔客户端 GPU |
Windows/Linux |
英特尔 GPU 支持(Beta 版)已在 PyTorch* 2.5 中为 Linux 和 Windows 上的英特尔® 数据中心 GPU Max 系列和英特尔® 客户端 GPU 做好准备,这将英特尔 GPU 和 SYCL* 软件堆栈带入了官方 PyTorch 堆栈,提供一致的用户体验,以拥抱更多 AI 应用场景。
安装¶
二进制文件¶
平台 Linux¶
现在我们已经安装了所有必需的软件包并激活了环境。使用以下命令在 Linux 上安装 pytorch
、torchvision
、torchaudio
。
对于发行版轮子
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/xpu
对于 nightly 轮子
pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/xpu
平台 Windows¶
现在我们已经安装了所有必需的软件包并激活了环境。使用以下命令在 Windows 上安装 pytorch
,从源代码构建 torchvision
和 torchaudio
。
对于发行版轮子
pip3 install torch --index-url https://download.pytorch.org/whl/xpu
对于 nightly 轮子
pip3 install --pre torch --index-url https://download.pytorch.org/whl/nightly/xpu
从源代码¶
从源代码构建 torch
,请参考 PyTorch 安装从源代码构建。
从源代码构建 torchvision
,请参考 Torchvision 安装从源代码构建。
从源代码构建 torchaudio
,请参考 Torchaudio 安装从源代码构建。
检查英特尔 GPU 的可用性¶
要检查英特尔 GPU 是否可用,您通常会使用以下代码
import torch
torch.xpu.is_available() # torch.xpu is the API for Intel GPU support
如果输出为 False
,请仔细检查以下步骤。
英特尔 GPU 驱动程序安装
英特尔支持包安装
环境设置
最少代码更改¶
如果您要将代码从 cuda
迁移,您需要将 cuda
的引用更改为 xpu
。例如
# CUDA CODE
tensor = torch.tensor([1.0, 2.0]).to("cuda")
# CODE for Intel GPU
tensor = torch.tensor([1.0, 2.0]).to("xpu")
以下几点概述了 PyTorch 与英特尔 GPU 的支持和局限性
支持训练和推理工作流程。
支持急切模式和
torch.compile
。支持 FP32、BF16、FP16 和自动混合精度 (AMP) 等数据类型。
示例¶
本节包含推理和训练工作流程的使用示例。
推理示例¶
这里有一些推理工作流程示例。
使用 FP32 进行推理¶
import torch
import torchvision.models as models
model = models.resnet50(weights="ResNet50_Weights.DEFAULT")
model.eval()
data = torch.rand(1, 3, 224, 224)
model = model.to("xpu")
data = data.to("xpu")
with torch.no_grad():
model(data)
print("Execution finished")
使用 AMP 进行推理¶
import torch
import torchvision.models as models
model = models.resnet50(weights="ResNet50_Weights.DEFAULT")
model.eval()
data = torch.rand(1, 3, 224, 224)
model = model.to("xpu")
data = data.to("xpu")
with torch.no_grad():
d = torch.rand(1, 3, 224, 224)
d = d.to("xpu")
# set dtype=torch.bfloat16 for BF16
with torch.autocast(device_type="xpu", dtype=torch.float16, enabled=True):
model(data)
print("Execution finished")
使用 torch.compile
进行推理¶
import torch
import torchvision.models as models
import time
model = models.resnet50(weights="ResNet50_Weights.DEFAULT")
model.eval()
data = torch.rand(1, 3, 224, 224)
ITERS = 10
model = model.to("xpu")
data = data.to("xpu")
for i in range(ITERS):
start = time.time()
with torch.no_grad():
model(data)
torch.xpu.synchronize()
end = time.time()
print(f"Inference time before torch.compile for iteration {i}: {(end-start)*1000} ms")
model = torch.compile(model)
for i in range(ITERS):
start = time.time()
with torch.no_grad():
model(data)
torch.xpu.synchronize()
end = time.time()
print(f"Inference time after torch.compile for iteration {i}: {(end-start)*1000} ms")
print("Execution finished")
训练示例¶
这里有一些训练工作流程示例。
使用 FP32 进行训练¶
import torch
import torchvision
LR = 0.001
DOWNLOAD = True
DATA = "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)
loss.backward()
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")
使用 AMP 进行训练¶
import torch
import torchvision
LR = 0.001
DOWNLOAD = True
DATA = "datasets/cifar10/"
use_amp=True
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)
scaler = torch.amp.GradScaler(enabled=use_amp)
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")
# set dtype=torch.bfloat16 for BF16
with torch.autocast(device_type="xpu", dtype=torch.float16, enabled=use_amp):
output = model(data)
loss = criterion(output, target)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
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")
使用 torch.compile
进行训练¶
import torch
import torchvision
LR = 0.001
DOWNLOAD = True
DATA = "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")
model = torch.compile(model)
print(f"Initiating training with torch compile")
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)
loss.backward()
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")