This code creates a simple model, wraps it with DataParallel if multiple GPUs are available, and runs one training step with dummy data.
import torch
import torch.nn as nn
import torch.optim as optim
# Simple model
class SimpleModel(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(10, 2)
def forward(self, x):
return self.linear(x)
# Check if multiple GPUs are available
if torch.cuda.device_count() > 1:
print(f"Using {torch.cuda.device_count()} GPUs")
else:
print("Using single GPU or CPU")
# Create model and move to GPUs
model = SimpleModel()
if torch.cuda.is_available():
model = model.cuda()
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
# Dummy data
inputs = torch.randn(32, 10)
labels = torch.randint(0, 2, (32,))
if torch.cuda.is_available():
inputs = inputs.cuda()
labels = labels.cuda()
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
# Forward pass
outputs = model(inputs)
loss = criterion(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f"Loss: {loss.item():.4f}")