Phase 00 - Lesson 03

Configuración de GPU y Nube

Entrenar en CPU está bien para aprender. Entrenar en serio requiere una GPU.

Tipo: Build Lenguajes: Python Prerrequisitos: Fase 0, Lección 01 Tiempo: ~45 minutos

Objetivos de Aprendizaje

  • Verificar la disponibilidad de la GPU local usando nvidia-smi y la API CUDA de PyTorch
  • Configurar Google Colab con una GPU T4 para experimentos gratuitos basados en la nube
  • Hacer benchmark de la multiplicación de matrices en CPU vs GPU y medir la aceleración
  • Estimar el modelo más grande que cabe en tu VRAM usando la regla práctica de fp16

El Problema

La mayoría de las lecciones en las fases 1-3 funcionan bien en CPU. Pero cuando empiezas a entrenar CNNs, transformers o LLMs (fases 4+), necesitas aceleración por GPU. Una ejecución de entrenamiento que toma 8 horas en CPU toma 10 minutos en GPU.

Tienes tres opciones: GPU local, GPU en la nube o Google Colab (gratis).

El Concepto

Your options:

1. Local NVIDIA GPU
   Cost: $0 (you already have it)
   Setup: Install CUDA + cuDNN
   Best for: Regular use, large datasets

2. Google Colab (free tier)
   Cost: $0
   Setup: None
   Best for: Quick experiments, no GPU at home

3. Cloud GPU (Lambda, RunPod, Vast.ai)
   Cost: $0.20-2.00/hr
   Setup: SSH + install
   Best for: Serious training, large models

Build It

Opción 1: GPU NVIDIA Local

Verifica si tienes una:

nvidia-smi

Instala PyTorch con CUDA:

import torch

print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA version: {torch.version.cuda}")
if torch.cuda.is_available():
    print(f"GPU: {torch.cuda.get_device_name(0)}")
    print(f"Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")

Opción 2: Google Colab

  1. Ve a colab.research.google.com
  2. Runtime > Change runtime type > T4 GPU
  3. Ejecuta !nvidia-smi para verificar

Sube notebooks de este curso directamente a Colab.

Opción 3: GPU en la Nube

Para Lambda Labs, RunPod o Vast.ai:

ssh user@your-gpu-instance

pip install torch torchvision torchaudio
python -c "import torch; print(torch.cuda.get_device_name(0))"

¿Sin GPU? Sin problema.

La mayoría de las lecciones funcionan en CPU. Las que necesitan GPU lo indicarán e incluirán enlaces de Colab.

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using: {device}")

Build It: benchmark GPU vs CPU

import torch
import time

size = 5000

a_cpu = torch.randn(size, size)
b_cpu = torch.randn(size, size)

start = time.time()
c_cpu = a_cpu @ b_cpu
cpu_time = time.time() - start
print(f"CPU: {cpu_time:.3f}s")

if torch.cuda.is_available():
    a_gpu = a_cpu.to("cuda")
    b_gpu = b_cpu.to("cuda")

    torch.cuda.synchronize()
    start = time.time()
    c_gpu = a_gpu @ b_gpu
    torch.cuda.synchronize()
    gpu_time = time.time() - start
    print(f"GPU: {gpu_time:.3f}s")
    print(f"Speedup: {cpu_time / gpu_time:.0f}x")

Ejercicios

  1. Ejecuta el benchmark anterior y compara los tiempos de CPU vs GPU
  2. Si no tienes una GPU, ejecútalo en Google Colab y compara
  3. Revisa cuánta memoria de GPU tienes y estima el modelo más grande que puedes acomodar (regla práctica: 2 bytes por parámetro para fp16)

Términos Clave

Término Lo que dice la gente Lo que realmente significa
CUDA "Programación de GPU" Plataforma de computación paralela de NVIDIA que permite ejecutar código en la GPU
VRAM "Memoria de la GPU" RAM de video en la GPU, separada de la RAM del sistema. Limita el tamaño del modelo.
fp16 "Media precisión" Punto flotante de 16 bits, usa la mitad de la memoria de fp32 con una pérdida mínima de precisión
Tensor Core "Hardware de matrices rápido" Núcleos de GPU especializados para la multiplicación de matrices, 4-8x más rápidos que los núcleos normales
0 lifetime access. Curriculum based on AI Engineering from Scratch by Rohit Ghumare (MIT, used under attribution).