Phase 23 of 25 · Topic 23.1

PyTorch Tensor Fundamentals & CUDA Device GPU Acceleration

1Concept

A PyTorch `Tensor` is an N-dimensional array capable of running on GPUs via CUDA/MPS. Moving tensors to GPU memory (`tensor.to('cuda')`) enables massive matrix hardware acceleration.

2Architecture Diagram

Host RAM (CPU Tensor) ---> .to('cuda') ---> GPU VRAM (CUDA Matrix Cores Accelerated!)

3Code Example

Python 3.12
pytorch_tensor_demo = '''
import torch

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

# Allocate tensor on target device
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device)
y = torch.tensor([[5.0, 6.0], [7.0, 8.0]], device=device)

# High-speed GPU Matrix Multiplication
z = torch.matmul(x, y)
print(f"Matrix Output:\n{z}")
'''
print("=== PyTorch CUDA Tensor Allocation ===")
print(pytorch_tensor_demo.strip())

4Expected Output

=== PyTorch CUDA Tensor Allocation ===
import torch

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

# Allocate tensor on target device
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device)
y = torch.tensor([[5.0, 6.0], [7.0, 8.0]], device=device)

# High-speed GPU Matrix Multiplication
z = torch.matmul(x, y)
print(f"Matrix Output:\n{z}")

5Key Takeaways

  • Always check `torch.cuda.is_available()` or Apple Silicon `torch.backends.mps.is_available()`.
  • Tensors on different devices (e.g. CPU vs GPU) cannot be combined in operations.
  • Use `torch.from_numpy()` to create tensors sharing memory with NumPy arrays.