Expert guidance for PyTorch development covering Deep Reinforcement Learning and NLP Transformers. This skill provides comprehensive knowledge for building RL agents with TorchRL (DQN, PPO) and NLP...
Expert guidance for PyTorch 2.7+ development covering Deep Reinforcement Learning with TorchRL and NLP with HuggingFace Transformers.
| Library | Version | Notes |
|---|---|---|
| PyTorch | 2.9.1+ | Use 2.7+ minimum, CUDA 12.4 recommended |
| TorchRL | 0.10.x | GymEnv, SyncDataCollector, ClipPPOLoss |
| HuggingFace Transformers | 4.56.2+ | AutoTokenizer, Trainer, pipeline |
| Gymnasium | 1.0.0+ | OpenAI Gym is DEPRECATED |
| PEFT | Current | LoRA fine-tuning |
| PettingZoo | Current | Multi-agent RL |
ALWAYS avoid these deprecated patterns:
| Deprecated | Use Instead |
|---|---|
import gym |
import gymnasium as gym |
evaluation_strategy in Trainer |
eval_strategy |
| CUDA < 12.1 | CUDA 12.4 |
env.step() returning 4 values |
Use 5 values: obs, reward, terminated, truncated, info |
import torch
device = (
torch.device("cuda") if torch.cuda.is_available() else
torch.device("mps") if torch.backends.mps.is_available() else
torch.device("xpu") if hasattr(torch.backends, 'xpu') and torch.backends.xpu.is_available() else
torch.device("cpu")
)
model = model.to(device)
model = torch.compile(model) # Optimize for speed
from torchrl.envs import GymEnv
from torchrl.collectors import SyncDataCollector
from torchrl.data import ReplayBuffer, LazyTensorStorage
from torchrl.objectives import DQNLoss, HardUpdate
env = GymEnv("CartPole-v1", device=device)
collector = SyncDataCollector(env, policy, frames_per_batch=128, total_frames=100_000)
replay_buffer = ReplayBuffer(storage=LazyTensorStorage(10_000))
loss_module = DQNLoss(value_network=qnet, loss_function="smooth_l1", delay_value=True)
from torchrl.objectives import ClipPPOLoss
from torchrl.objectives.value import GAE
loss_fn = ClipPPOLoss(actor_network=actor, critic_network=critic, clip_epsilon=0.2, entropy_coef=0.01)
advantage_fn = GAE(value_network=critic, gamma=0.99, lmbda=0.95)
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
# Simple inference
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
result = classifier("I love PyTorch!")
# Fine-tuning
from transformers import Trainer, TrainingArguments
training_args = TrainingArguments(
output_dir="./results", eval_strategy="epoch", # NOT evaluation_strategy
learning_rate=2e-5, per_device_train_batch_size=8, num_train_epochs=2, fp16=True
)
trainer = Trainer(model=model, args=training_args, train_dataset=train_ds, eval_dataset=val_ds)
trainer.train()
For comprehensive coverage, load the appropriate guide:
| Topic | Guide | When to Load |
|---|---|---|
| Tensors, Autograd, nn.Module | references/pytorch-fundamentals.md |
PyTorch basics, device management |
| TorchRL, DQN, PPO | references/reinforcement-learning.md |
RL algorithms, environments |
| HuggingFace, BERT, Fine-tuning | references/nlp-transformers.md |
NLP tasks, transformer models |
| torch.compile, Quantization, DDP | references/optimization-deployment.md |
Production, performance |
| CLIP, RLHF, Ethics | references/advanced-topics.md |
Multi-modal, responsible AI |
import gymnasium as gym
env = gym.make("CartPole-v1")
obs, info = env.reset()
while True:
action = env.action_space.sample()
obs, reward, terminated, truncated, info = env.step(action) # 5 values!
done = terminated or truncated
if done:
break
from torch.cuda.amp import autocast, GradScaler
scaler = GradScaler()
for batch in dataloader:
optimizer.zero_grad()
with autocast():
loss = compute_loss(batch)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
from peft import LoraConfig, get_peft_model
lora_config = LoraConfig(r=8, lora_alpha=16, target_modules=["q_proj", "v_proj"])
model = get_peft_model(model, lora_config)
# Now fine-tune with much fewer parameters
# Create virtual environment
python -m venv .venv && source .venv/bin/activate
# PyTorch with CUDA 12.4
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
# RL libraries
pip install torchrl gymnasium pettingzoo
# NLP libraries
pip install transformers datasets peft accelerate
# Experiment tracking
pip install tensorboard wandb
PyTorch MPS backend enables GPU acceleration on Apple Silicon Macs.
import torch
import os
# Enable MPS fallback for unsupported operations
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
device = torch.device("mps") if torch.backends.mps.is_available() else torch.device("cpu")
model = model.to(device)