AI State of the Art
Resources/Resources/9 min
Section 307

Tools & Frameworks: PyTorch, JAX, Hugging Face Transformers

Listen as a podcast →

The Tools Without Which Nothing Gets Done

Everyone talks about GPT, Claude, Gemini... but behind every model, there's a deep learning framework. It's the invisible infrastructure. The engine under the hood. And understanding these tools means understanding how AI is built, not just how it's used.

PyTorch reigns over research and industry. JAX is Google DeepMind's secret weapon. Transformers (the Hugging Face library) is the universal API that gives access to everything. Mastering at least one of these three is essential for anyone who wants to go beyond simple API calls.

The importance of framework choice

Your framework choice influences your productivity, performance, and access to the ecosystem. PyTorch dominates research and industry, JAX is used by Google for the largest models (Gemini, AlphaFold), and Transformers is the standard interface for using pre-trained models.

PyTorch

Overview

Developed by Meta AI (FAIR), PyTorch is the most widely used deep learning framework in the world. Over 90% of AI research papers use PyTorch. It's the de facto standard.

Why? Because coding in PyTorch is coding in Python. No black magic, no weird syntax. You write normal Python code, and PyTorch handles automatic differentiation and GPU execution.

  • Dynamic graph: the computation graph is built on the fly (eager execution), making debugging trivial. You can put print() statements everywhere.
  • Pythonic API: natural Python syntax, easy to learn for anyone who knows Python
  • Autograd: transparent automatic differentiation. You compute the forward pass, PyTorch calculates the gradients on its own.
  • Rich ecosystem: torchvision, torchaudio, torchtext, Lightning

Key Components

  • torch.Tensor: fundamental data structure (like numpy, but on GPU). Documentation
  • torch.nn: neural network modules (Linear, Conv2d, Transformer, etc.). Documentation
  • torch.optim: optimizers (Adam, SGD, AdamW). Documentation
  • torch.distributed: distributed training across multi-GPU and multi-node setups
  • torch.compile: JIT compilation to speed up code (PyTorch 2.0+). A game-changer.

PyTorch 2.0+

Version 2.0 was a quiet revolution:

  • torch.compile: compiles PyTorch code into optimized kernels. Often 30-50% free speedup with a single line of code.
  • Scaled Dot Product Attention: native implementation of Flash Attention. No need for external libraries anymore.
  • Improved distributed training support (FSDP)

Official documentation: pytorch.org/docs GitHub: pytorch/pytorch (85K+ stars) Tutorials: pytorch.org/tutorials

PyTorch in production

Historically criticized for its production performance, PyTorch has improved considerably with torch.compile, TorchServe, and ONNX export. The majority of AI production systems now use PyTorch. The argument "TensorFlow for prod, PyTorch for research" is dead and buried.

JAX

Overview

Developed by Google, JAX is a high-performance numerical computing framework. If PyTorch is "Python with superpowers," JAX is "numpy on steroids, compiled to the moon."

The approach is radically different from PyTorch: everything is functional and composable. You write pure functions, and JAX transforms them into ultra-optimized code.

  • Functional transformations: jit (compilation), grad (differentiation), vmap (vectorization), pmap (parallelization)
  • XLA backend: compilation to optimized kernels for GPU and TPU (Tensor Processing Unit)
  • Numpy-compatible: API similar to NumPy for easy adoption
  • Immutability: pure functional approach (no state mutation)

Key Transformations

JAX's four "superpowers":

  • jax.jit: Just-In-Time compilation. Transforms your Python function into optimized machine code.
  • jax.grad: automatic differentiation (forward and reverse). More flexible than PyTorch autograd.
  • jax.vmap: automatic vectorization. Write your function for a single example, vmap makes it batched automatically.
  • jax.pmap: parallelization across multiple devices (multi-GPU/TPU). Nearly free horizontal scaling.

JAX Ecosystem

  • Flax: neural network library for JAX (Google). The equivalent of torch.nn.
  • Optax: optimizers for JAX. The equivalent of torch.optim.
  • Equinox: PyTorch-like interface for JAX. The best of both worlds.

JAX vs PyTorch

AspectPyTorchJAX
ParadigmImperative (eager)Functional (compiled)
DebuggingEasy (print, breakpoint)More complex (functional code)
PerformanceVery good (torch.compile)Excellent (XLA)
Multi-GPU/TPUFSDP, DDPpmap, native sharding
EcosystemMassiveSmaller but growing
AdoptionResearch + industryGoogle, HPC research
Learning curveGentleSteep (functional paradigm)

Documentation: jax.readthedocs.io GitHub: google/jax (30K+ stars)

Who uses JAX?

Google DeepMind uses JAX for its largest models (Gemini, AlphaFold). JAX is preferred when computational performance is critical, especially for distributed training on TPU. Outside of Google, adoption remains more limited than PyTorch, but projects like Mistral use it too.

Hugging Face Transformers

The Universal Library

Transformers by Hugging Face is the standard access point for pre-trained models. It's like an App Store for AI models: you search, download, and use.

  • 200,000+ models accessible via a unified API on the Hugging Face Hub
  • Support for PyTorch, TensorFlow, and JAX
  • One-line pipeline for common tasks
  • Integration with the Model Hub

Typical Usage

The standard workflow is simple and fast:

  1. Search for a model on the Hub
  2. Load it with AutoModel.from_pretrained()
  3. Use it for inference or fine-tuning

Documentation: huggingface.co/docs/transformers GitHub: huggingface/transformers (135K+ stars)

Complementary Libraries

The Hugging Face ecosystem goes well beyond Transformers:

  • PEFT (Parameter-Efficient Fine-Tuning): efficient fine-tuning (LoRA, QLoRA). Essential for fine-tuning without 8 GPUs.
  • TRL: Transformer Reinforcement Learning (RLHF, DPO). For alignment.
  • Accelerate: simplified distributed training. Multi-GPU by changing 3 lines of code.
  • Datasets: data loading and processing. Faster and more efficient than pandas for large datasets.
  • Evaluate: standardized evaluation metrics.
  • Diffusers: diffusion models (Stable Diffusion, Flux, SDXL). The standard framework for image generation.
Abstraction vs Understanding

Transformers makes model usage very easy (a few lines of code), which can mask the underlying complexity. It's great for prototyping, but for research and advanced development, it's crucial to understand what's happening under the hood. High-level abstractions aren't enough when you need to debug a training run that's diverging.

Other Essential Tools

Modern deep learning is a whole ecosystem. Here are the most important complementary tools:

  • vLLM: optimized inference server for LLMs. PagedAttention for efficient memory usage. The standard for serving LLMs.
  • TensorRT-LLM: inference optimization by NVIDIA. Maximum performance on NVIDIA GPUs.
  • DeepSpeed: distributed training library (Microsoft). ZeRO for training models larger than GPU memory.
  • Megatron-LM: large-scale LLM training framework (NVIDIA). Used to train the largest models.
  • Weights & Biases (wandb): experiment tracking and monitoring. Like a dashboard for your training runs.
  • MLflow: ML lifecycle management. Open-source, self-hosted.