Skip to main content
TopMiniSite

Back to all posts

How to Make Pytorch Run on the GPU By Default?

Published on
8 min read
How to Make Pytorch Run on the GPU By Default? image

Best PyTorch-Compatible GPUs to Buy in December 2026

1 ASUS Dual Radeon RX 9060 XT 16GB GDDR6 Gaming Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1a, 2.5-slot design, Axial-tech Fans, 0dB Technology

ASUS Dual Radeon RX 9060 XT 16GB GDDR6 Gaming Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1a, 2.5-slot design, Axial-tech Fans, 0dB Technology

  • ENHANCED COOLING WITH AXIAL-TECH FANS FOR SUPERIOR PERFORMANCE.

  • 2.5-SLOT DESIGN ENSURES COMPATIBILITY AND EFFICIENT AIRFLOW.

  • DUAL BIOS FOR SILENT LIGHT GAMING OR PEAK PERFORMANCE.

BUY & SAVE
$524.69 $789.99
Save 34%
ASUS Dual Radeon RX 9060 XT 16GB GDDR6 Gaming Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1a, 2.5-slot design, Axial-tech Fans, 0dB Technology
2 msi Gaming GeForce RTX 3060 Ventus 2X 12G OC V1 Graphics Card - 15 Gbps GDRR6 Boost Clock: 1807 MHz 192-Bit HDMI/DP PCIe 4 Torx Twin Fan Ampere

msi Gaming GeForce RTX 3060 Ventus 2X 12G OC V1 Graphics Card - 15 Gbps GDRR6 Boost Clock: 1807 MHz 192-Bit HDMI/DP PCIe 4 Torx Twin Fan Ampere

  • UNLEASH GAMING POWER WITH NVIDIA GEFORCE RTX 3060 CHIPSET!
  • EXPERIENCE STUNNING VISUALS WITH 12GB GDDR6 VIDEO MEMORY.
  • HIGH-RES SUPPORT: ENJOY 8K GAMING WITH 7680 X 4320 RESOLUTION!
BUY & SAVE
$413.99 $491.99
Save 16%
msi Gaming GeForce RTX 3060 Ventus 2X 12G OC V1 Graphics Card - 15 Gbps GDRR6 Boost Clock: 1807 MHz 192-Bit HDMI/DP PCIe 4 Torx Twin Fan Ampere
3 ASUS Dual GeForce RTX 5060 Ti 16GB GDDR7 White OC Edition Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1b, 2.5-slot design, Axial-tech Fans, 0dB Technology

ASUS Dual GeForce RTX 5060 Ti 16GB GDDR7 White OC Edition Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1b, 2.5-slot design, Axial-tech Fans, 0dB Technology

  • BOOST PERFORMANCE: 2632 MHZ WITH NVIDIA BLACKWELL ARCHITECTURE!

  • SILENT GAMING MODE: ENJOY 0DB TECHNOLOGY IN LIGHT GAMEPLAY!

  • COMPACT DESIGN: 2.5-SLOT FOR OPTIMIZED COOLING IN SMALL SPACES!

BUY & SAVE
$849.99
ASUS Dual GeForce RTX 5060 Ti 16GB GDDR7 White OC Edition Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1b, 2.5-slot design, Axial-tech Fans, 0dB Technology
4 ASUS Dual GeForce RTX 5060 Ti 16GB GDDR7 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1b, 2.5-slot, Axial-tech Fans, 0dB Technology, Dual BIOS

ASUS Dual GeForce RTX 5060 Ti 16GB GDDR7 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1b, 2.5-slot, Axial-tech Fans, 0dB Technology, Dual BIOS

  • EXPERIENCE ULTIMATE GAMING WITH 767 AI TOPS & DLSS 4 TECHNOLOGY.

  • STAY COOL AND QUIET WITH ADVANCED AXIAL-TECH FAN DESIGN.

  • MAXIMIZE PERFORMANCE WITH DUAL BIOS FOR QUIET OR PERFORMANCE MODES.

BUY & SAVE
$824.00
ASUS Dual GeForce RTX 5060 Ti 16GB GDDR7 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI 2.1b, DisplayPort 2.1b, 2.5-slot, Axial-tech Fans, 0dB Technology, Dual BIOS
5 ASUS Prime Radeon RX 9070 XT 16GB GDDR6 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI/DP 2.1, 2.5-Slot, Axial-tech Fans, Dual-Ball Fan Bearings, Dual BIOS, GPU Guard

ASUS Prime Radeon RX 9070 XT 16GB GDDR6 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI/DP 2.1, 2.5-Slot, Axial-tech Fans, Dual-Ball Fan Bearings, Dual BIOS, GPU Guard

  • ENHANCED COOLING WITH AXIAL-TECH FANS FOR SUPERIOR PERFORMANCE.
  • 0DB TECH ENABLES SILENT GAMING WITHOUT COMPROMISING POWER.
  • DUAL BIOS SWITCH FOR EASY TRANSITION BETWEEN QUIET AND PERFORMANCE.
BUY & SAVE
$1,079.99
ASUS Prime Radeon RX 9070 XT 16GB GDDR6 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI/DP 2.1, 2.5-Slot, Axial-tech Fans, Dual-Ball Fan Bearings, Dual BIOS, GPU Guard
6 ASUS Prime Radeon RX 9070 EVO 16GB GDDR6 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI/DP 2.1, 2.5-Slot, Axial-tech Fans, Dual-Ball Fan Bearings, Dual BIOS, GPU Guard

ASUS Prime Radeon RX 9070 EVO 16GB GDDR6 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI/DP 2.1, 2.5-Slot, Axial-tech Fans, Dual-Ball Fan Bearings, Dual BIOS, GPU Guard

  • OVERCLOCK WITH EASE: BOOST CLOCKS UP TO 2610 MHZ FOR MAXIMUM PERFORMANCE.

  • ENHANCED COOLING: AXIAL-TECH FANS IMPROVE AIRFLOW WITH INNOVATIVE DESIGN.

  • WHISPER-QUIET GAMING: 0DB TECHNOLOGY ENSURES SILENT OPERATION DURING LIGHT USE.

BUY & SAVE
$975.99
ASUS Prime Radeon RX 9070 EVO 16GB GDDR6 OC Edition Gaming Graphics Card | PCIe 5.0, HDMI/DP 2.1, 2.5-Slot, Axial-tech Fans, Dual-Ball Fan Bearings, Dual BIOS, GPU Guard
7 GIGABYTE GeForce RTX 3050 WINDFORCE OC V2 6G Graphics Card, 2X WINDFORCE Fans, 6GB GDDR6 96-bit GDDR6, GV-N3050WF2OCV2-6GD Graphics Card

GIGABYTE GeForce RTX 3050 WINDFORCE OC V2 6G Graphics Card, 2X WINDFORCE Fans, 6GB GDDR6 96-bit GDDR6, GV-N3050WF2OCV2-6GD Graphics Card

  • BOOST PERFORMANCE WITH NVIDIA AMPERE ARCHITECTURE FOR GAMING EXCELLENCE.
  • EXPERIENCE STUNNING VISUALS WITH 2ND GEN RT CORES FOR REAL-TIME RAY TRACING.
  • ELEVATE AI TASKS USING 3RD GEN TENSOR CORES FOR ENHANCED EFFICIENCY.
BUY & SAVE
$249.99
GIGABYTE GeForce RTX 3050 WINDFORCE OC V2 6G Graphics Card, 2X WINDFORCE Fans, 6GB GDDR6 96-bit GDDR6, GV-N3050WF2OCV2-6GD Graphics Card
8 GIGABYTE Radeon RX 9070 XT Gaming OC 16G Graphics Card, PCIe 5.0, 16GB GDDR6, GV-R9070XTGAMING OC-16GD Video Card

GIGABYTE Radeon RX 9070 XT Gaming OC 16G Graphics Card, PCIe 5.0, 16GB GDDR6, GV-R9070XTGAMING OC-16GD Video Card

  • EXCEPTIONAL PERFORMANCE WITH RADEON RX 9070 XT POWER.
  • ADVANCED WINDFORCE COOLING FOR MAXIMUM EFFICIENCY.
  • STUNNING RGB LIGHTING FOR A PERSONALIZED GAMING AESTHETIC.
BUY & SAVE
$1,034.99
GIGABYTE Radeon RX 9070 XT Gaming OC 16G Graphics Card, PCIe 5.0, 16GB GDDR6, GV-R9070XTGAMING OC-16GD Video Card
+
ONE MORE?

By default, PyTorch runs on the CPU. However, you can make PyTorch run on the GPU by default by following these steps:

  1. Check for GPU availability: Before running the code, ensure that you have a GPU available on your machine. PyTorch uses CUDA, so you need to have an NVIDIA GPU with CUDA support. You can check if a GPU is available using the torch.cuda.is_available() method, which returns True if a GPU is available, or False if not.
  2. Set the device: If a GPU is available, set the device to the GPU using torch.cuda.set_device() method. This will ensure that all tensors and operations are performed on the GPU. If a GPU is not available, PyTorch will automatically use the CPU.

Here is an example code snippet to make PyTorch run on the GPU by default:

import torch

Check for GPU availability

if torch.cuda.is_available(): # Set device to GPU torch.cuda.set_device(0) device = torch.device("cuda") print("Using GPU:", torch.cuda.get_device_name(0)) else: # Set device to CPU device = torch.device("cpu") print("Using CPU.")

Use the device for tensor operations

a = torch.tensor([1, 2, 3]).to(device) b = torch.tensor([4, 5, 6]).to(device) c = a + b

print("Result:", c)

In this code, the if statement checks for GPU availability. If a GPU is available, it sets the device to the GPU (in this case, GPU 0). If a GPU is not available, it sets the device to the CPU. The tensors a and b are then created on the selected device, and the addition operation c = a + b is performed on the selected device.

Remember that running code on a GPU may require additional setup, such as installing CUDA and appropriate GPU drivers.

What is the default GPU device in PyTorch?

The default GPU device in PyTorch is the one specified by the environment variable CUDA_VISIBLE_DEVICES, or if not set, it is the device with index 0.

What is the default device in PyTorch?

The default device in PyTorch is the CPU.

What are the best practices for GPU memory management in PyTorch?

Here are some best practices for GPU memory management in PyTorch:

  1. Use torch.cuda.empty_cache(): PyTorch keeps a cache of GPU memory, and sometimes memory is not immediately released after tensors go out of scope. Calling torch.cuda.empty_cache() can release unused memory from the cache.
  2. Use the proper data types: PyTorch has different data types like torch.float32, torch.float16, etc. Use the smallest data type that still maintains the required precision to reduce memory usage.
  3. Limit the number of parallel workers: If you are using DataLoader for loading data in parallel, reduce the number of parallel workers to limit memory consumption. Higher numbers of workers can lead to higher memory usage.
  4. Use smaller batch sizes: Larger batch sizes consume more memory. If memory usage is a concern, consider using smaller batch sizes during training.
  5. Use gradient accumulation instead of larger batch sizes: If you need larger effective batch sizes for better convergence, consider using gradient accumulation. This involves accumulating gradients over multiple small batches before performing weight updates.
  6. Use the pin_memory flag: When using DataLoader, set the pin_memory flag to True. This can improve data transfer from CPU to GPU by enabling pinned memory, which avoids memory copies.
  7. Use to(device) for moving tensors: Instead of manually moving tensors using .cuda() or .cpu(), use the to(device) method to move tensors between CPU and GPU. This provides flexibility in case you want to use different devices in the future.
  8. Use torch.no_grad() when evaluating: When evaluating a model, wrap the evaluation code with torch.no_grad() to disable gradient tracking. This avoids unnecessary memory usage for gradient computations.
  9. Use gradient checkpointing: If your model has a large memory footprint due to intermediate activations, consider using gradient checkpointing. This technique trades off some computation time for reduced memory usage by recomputing some intermediate activations during backpropagation.
  10. Reduce unnecessary memory allocations: Minimize unnecessary tensor allocations by reusing existing tensors whenever possible. Creating new tensors unnecessarily can lead to memory fragmentation.

Remember that the best practices for GPU memory management may depend on the specific task, model, and available hardware resources.

To debug errors related to PyTorch GPU usage, you can follow these steps:

  1. Check if your system supports GPU: Confirm that your system has a compatible GPU and the necessary drivers are installed. You can check this using the nvidia-smi command in the terminal.
  2. Verify PyTorch installation: Ensure that PyTorch is installed with GPU support. You can check this by running the following code:

import torch print(torch.cuda.is_available())

If it returns True, PyTorch is installed with GPU support.

  1. Verify GPU device availability: You can check the available GPU devices using the following code:

import torch print(torch.cuda.device_count())

This will print the number of available GPUs.

  1. Place tensors on the GPU: When working with PyTorch, make sure to move tensors and models to the GPU explicitly. You can do this using the to() method. For example:

import torch

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') x = torch.tensor([1, 2, 3]).to(device)

This will ensure that the tensor is placed on the GPU if available, or on the CPU if not.

  1. Check for GPU memory issues: In some cases, you may encounter CUDA out-of-memory errors. This can happen if the GPU memory is not sufficient for the operation. You can monitor GPU memory usage using the nvidia-smi command, or in the PyTorch code using:

import torch

print(torch.cuda.memory_allocated()) print(torch.cuda.memory_cached())

These commands will print the currently allocated and cached GPU memory.

  1. Enable CUDA error tracking: PyTorch provides an option to enable CUDA error tracking, which can help in identifying specific errors. You can enable it by setting the CUDA_LAUNCH_BLOCKING environment variable before running your script:

export CUDA_LAUNCH_BLOCKING=1 python your_script.py

This will raise an error whenever there is an issue with the GPU operations, providing more specific information about the error.

  1. Check error messages and stack traces: When encountering errors, carefully read the error messages and stack traces. These often provide valuable information about the source of the error, such as specific functions or lines in your code that caused the problem.

By following these steps, you should be able to identify and debug errors related to PyTorch GPU usage effectively.

What is the difference between GPU and CPU memory in PyTorch?

In PyTorch, GPU (Graphics Processing Unit) and CPU (Central Processing Unit) memory perform different roles in the training and inference processes:

  1. GPU Memory: GPU memory refers to the memory resources available on the graphics card. It is used for storing the intermediate computations and data during the training process. GPUs excel at performing massively parallel computations, making them efficient at handling matrix multiplications and other operations involved in deep learning. PyTorch allows you to move tensors from CPU memory to GPU memory using functions like .to('cuda').
  2. CPU Memory: CPU memory, also known as system memory or RAM (Random Access Memory), refers to the memory resources available to the central processing unit. It is used for general-purpose computing tasks and for storing data that doesn't fit in the GPU memory. PyTorch tensors residing in CPU memory can be transferred to GPU memory for computations by calling .to('cuda') or .cuda().

In summary, GPU memory is specifically allocated on the GPU and is used for efficient parallel computations, while CPU memory is the system's general-purpose memory used to store data and perform various computations.

How to profile PyTorch GPU usage?

To profile PyTorch GPU usage, you can follow these steps:

  1. PyTorch provides a tool called torch.cuda.profiler that allows you to profile GPU usage. First, import this module along with other required modules:

import torch from torch.cuda import profiler

  1. Start and stop the profiler around the code snippet you want to profile. You can wrap your code snippet using the torch.cuda.profiler.profile() context manager:

with profiler.profile(record_shapes=True, profile_memory=True, use_cuda=True) as prof: # code snippet to profile ...

  • record_shapes=True records shape information for each operation (Optional).
  • profile_memory=True profiles GPU memory usage (Optional).
  • use_cuda=True uses CUDA to collect events (Required, as we are profiling GPU usage).
  1. Make sure to place your PyTorch tensors and models on the GPU for profiling:

device = torch.device("cuda") model.to(device) inputs = inputs.to(device)

  1. After running your code, you can print the stats to analyze the GPU usage:

print(prof.key_averages().table(sort_by="cuda_time_total"))

  • key_averages() will return the profiling results.
  • table() sorts the results by "cuda_time_total" for detailed information.
  1. Run the script and observe the profiling output that shows the execution time of each operation on the GPU.

Here is a complete example:

import torch from torch.cuda import profiler

device = torch.device("cuda")

Put your PyTorch tensors and models on the GPU

model = Model() model.to(device) inputs = torch.randn(10, 3, 224, 224).to(device)

Start profiling

with profiler.profile(record_shapes=True, profile_memory=True, use_cuda=True) as prof: # Code snippet to profile output = model(inputs)

Print the profiling stats

print(prof.key_averages().table(sort_by="cuda_time_total"))

By analyzing the profiling results, you can identify bottlenecks and optimize your PyTorch code for better GPU utilization.