Installation and Ecosystem
The ecosystem is the three packages: the torch (the core, the tensor, the autograd, the nn), the torchvision (the dataset, the transform, the pretrain model), and the torchaudio (the audio) - the pip installs the CPU, the index-url the CUDA, and the conda the GPU's alternative. The device variable (the cuda/mps/cpu) is the model's target, the is_available() the check, and the dtype (the float32, the float16, the bfloat16) is the precision the AMP uses. This chapter covers the install (the pip, the conda, the index-url), the device (the cuda, the mps, the cpu), the dtype (the float32, the float16, the AMP), and the torch.hub the model's load (the next chapter's).
The Install: The pip and the conda
# the pip: the CPU (the default, the no GPU):
pip install torch torchvision
pip install torch torchvision --index-url \
https://download.pytorch.org/whl/cpu
# the CUDA (the NVIDIA's GPU, the driver's 12.1):
# the wheel's the 2.5GB, the driver's the check
# the nvidia-smi first
pip install torch torchvision --index-url \
https://download.pytorch.org/whl/cu121
# the conda: the GPU's alternative (the no pip
# conflict, the channel's the pytorch + the nvidia)
conda install pytorch torchvision pytorch-cuda=12.1 \
-c pytorch -c nvidia
# the CHECK (the build, the GPU, the count):
python -c "import torch; print(
torch.__version__,
torch.cuda.is_available(),
torch.cuda.device_count() if torch.cuda.is_available() else 0)"
The install is the one pip (or the conda): the pip install torch torchvision the CPU build (the no GPU, the 1GB the download), and the --index-url the CUDA (the cu121 the 12.1 the NVIDIA's driver, the 2.5GB the wheel) - the conda is the GPU's alternative (the pytorch-cuda=12.1 the -c pytorch -c nvidia, the driver the no pip conflict). The check is the one-liner: the torch.__version__ the build, the torch.cuda.is_available() the GPU's presence, the torch.cuda.device_count() the card's count - the True the .cuda() the model's move, the False the .cpu() the fallback. The MPS (the Apple silicon) is the torch.backends.mps.is_available() (the 13.0+ the macOS), and the device variable (the next section) is the pattern the same code runs on the both - the install the environment, the device the target, and the rule is the one: the pip the CPU, the index-url the CUDA, the conda the GPU's alt, the device the model's choice.
The device and the dtype
# the DEVICE (the the model's the target, the the
# the same code the the both):
import torch
device = ("cuda" if torch.cuda.is_available()
else "mps" if torch.backends.mps.is_available()
else "cpu")
print(device) # the cuda / the mps / the cpu
# the the MOVE (the the tensor's, the the model's):
x = torch.randn(4, 784).to(device) # the data
model = torch.nn.Linear(784, 10).to(device) # the
# weights
# the
# the
# the DTYPE (the the precision, the
# the):
x32 = torch.randn(4, dtype=torch.float32) # the
# default
x16 = torch.randn(4, dtype=torch.float16) # the
# AMP's
xbf = torch.randn(4, dtype=torch.bfloat16) # the
# A100's
# the
# the
The device is the model's target: the cuda (the NVIDIA), the mps (the Apple), the cpu (the fallback) - the is_available() the check, and the .to(device) the move (the data plus the weights, the model.to(device) the parameter's). The rule is the one: the device variable at the top, the .to(device) the data and the model, and the model.parameters() the optimizer the same device (the no cross-device, the cpu weight the cuda input the error). The dtype is the precision: the float32 (the default, the 4 byte), the float16 (the AMP's, the 2 byte the GPU's fast), the bfloat16 (the A100's, the range's wide) - the AMP (the next chapter's optimization) is the float16 the compute plus the float32 the master (the speedup the 2x, the no underflow). The ecosystem is the three packages (the torch, the torchvision, the torchaudio), the install the pip/conda, the device the target, the dtype the precision - the next chapter's tensor is the array the device holds and the autograd tracks.
The Working Rules
- The
pipthe CPU, theindex-urlthe CUDA, thecondathe alt. Thepip install torch torchvisionthe CPU (the1GB), thecu121theNVIDIA(the2.5GB), theconda install pytorch-cuda=12.1thedriver's no-conflict - thenvidia-smithe driver's check first. - The
deviceis the target, the.to(device)the move. Thecuda/mps/cputheis_available()the select, thex.to(device)the data, themodel.to(device)the weights - the same code the both, the no hardcode. - The
dtypeis the precision, theAMPthefloat16. Thefloat32the default, thefloat16the2byte (theGPU's fast), thebfloat16theA100(the range) - theAMPthefloat16the compute plus thefloat32the master (the no underflow). - The check is the one-liner, the
torch.hubthe load. Thetorch.__version__plus thecuda.is_available()plus thedevice_count()the environment, and thetorch.hub.load(the next chapter) the pretrain model the no-train.
Summary
The ecosystem is the three packages: the torch (the core, the tensor, the autograd, the nn), the torchvision (the dataset, the transform, the pretrain), and the torchaudio (the audio). The install is the pip install torch torchvision (the CPU), the --index-url .../cu121 the CUDA (the NVIDIA), the conda install pytorch-cuda=12.1 the driver's no-conflict, and the check is the torch.__version__ plus the cuda.is_available() plus the device_count(). The device variable is the model's target (the cuda/mps/cpu the is_available() the select, the .to(device) the data and the weights) so the same code runs on the both, and the dtype is the precision (the float32 the default, the float16 the AMP's, the bfloat16 the A100's) - the AMP the float16 compute plus the float32 master. The next chapter's tensor is the array the device holds: the torch.tensor the create, the shape the dimension, the requires_grad the autograd's flag, and the operation the gradient's path.