liveBook · Manning

CUDA

As explained in Hugging Face in Action

CUDA is NVIDIA's parallel computing platform and programming model for executing general-purpose computations on GPUs.

CUDA is NVIDIA’s parallel computing platform and programming interface for running general-purpose computations on NVIDIA GPUs. In PyTorch workflows, it accelerates tensor operations, model training, and inference by moving computation from CPU to GPU memory. section 2.2.1 section 3.2.4

Run a Transformers pipeline on CUDA

from transformers import pipeline classifier = pipeline( task="text-classification", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english", device="cuda" ) classifier("The service was excellent and I would gladly return.") The device parameter selects CUDA instead of the default CPU execution. The pipeline returns a classification label and confidence score. section 3.2.4

CUDA support is useful when you need to:

  • Accelerate training and inference for deep learning models. section 2.2.1
  • Use PyTorch to transfer tensor operations between CPU and GPU memory. section 2.2.1
  • Distribute a model across multiple GPUs when it cannot fit on one device. section 2.2.1
  • Select CUDA automatically when loading a pretrained model alongside other hardware backends such as Apple MPS. section 6.1.4
$ pip install torch torchvision torchaudio 
--index-url https://download.pytorch.org/whl/cu121 -U

Install CUDA-compatible PyTorch packages from the cu121 wheel index before testing GPU availability. section 2.2.1

Listing 2.1 Using the torch package to find details on your GPU
import torch
use_cuda = torch.cuda.is_available()

if use_cuda:
    print('__CUDNN VERSION:', torch.backends.cudnn.version())
    print('__Number CUDA Devices:', torch.cuda.device_count())
    print('__CUDA Device Name:', torch.cuda.get_device_name(0))
    print('__CUDA Device Total Memory [GB]:',
          torch.cuda.get_device_properties(0).total_memory/1e9)

Inspect CUDA availability, cuDNN version, device count, GPU name, and total GPU memory with torch. section 2.2.1

How Hugging Face in Action covers it

How deep it goes
Defines CUDA and demonstrates installation, availability checks, device selection, and multi-GPU model parallelism without explaining CUDA internals.
Examples
Demonstrates CUDA installation, torch diagnostics, GPUtil inspection, sentiment classification, and pretrained sequence-classification loading.
What it assumes
Assumes Python, PyTorch, and Transformers usage while teaching CUDA availability checks and device assignment directly.
What you can do after
Apply it via PyTorch and Transformers to detect GPUs, select CUDA execution, inspect hardware, and accelerate model workloads.
How it connects
Connects CUDA with NVIDIA GPUs, PyTorch, Transformers pipelines, cuDNN, GPU memory, model parallelism, and Apple MPS.

Also explained in

Compare how 4 books explain CUDA →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hugging Face in Action. The section links point to the parts of the book it draws on.