Uruchamiać zadania za pomocą torchrun
Tworzyć i koordynować procesy robocze
Uruchamiać zadania za pomocą torchrun to bezpłatna lekcja Deep Learning Academy na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Deep Learning Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Who Starts the Processes
DDP needs one process per GPU, but who spawns them? You do not launch them by hand. torchrun is the tool that does it for you.
Meet torchrun
torchrun is PyTorch's launcher. You give it your script and how many processes to start, and it handles the coordination.
torchrun --nproc_per_node=4 train.pyProcesses per Node
The nproc_per_node flag sets how many processes to spawn on this machine. Usually it equals the number of GPUs you have.
torchrun --nproc_per_node=8 train.pyEnvironment Variables Appear
torchrun injects key values as environment variables: RANK, LOCAL_RANK, and WORLD_SIZE. Your script reads them to know who it is.
import os
local_rank = int(os.environ["LOCAL_RANK"])No Manual Address Wiring
Older launchers made you pass ranks by hand. With torchrun, the rendezvous is automatic, so your script stays clean and portable.
Init Reads the Env
Because torchrun sets the env vars, your init_process_group call needs no arguments beyond the backend. It picks everything up automatically.
import torch.distributed as dist
dist.init_process_group(backend="nccl")Scale to Many Machines
To go multi-node, add nnodes and a node rank. Each machine runs the same command with its own node id.
torchrun --nnodes=2 --node_rank=0 --nproc_per_node=4 train.pyPoint to the Master
Across nodes, all processes must find a meeting point. The rdzv_endpoint gives the host and port they rendezvous on.
torchrun --rdzv_endpoint=host0:29500 train.pySurvive a Worker Crash
torchrun supports elastic training: set a min and max node count, and the job can recover if a worker drops out. 💪
torchrun --nnodes=1:4 --max_restarts=3 train.pyLog From One Rank
Every process prints, so logs get noisy. Gate prints behind a rank check so only rank 0 reports progress.
if int(os.environ["RANK"]) == 0:
print("epoch done")The Whole Recipe
The pattern is simple: write a normal DDP script, then launch it with torchrun. The launcher and DDP handle the rest together.
Quick Check
Recall what torchrun hands to your script.
Recap
You learned to launch jobs with torchrun: set processes per node, read the injected env vars, and scale to many nodes with elastic recovery.
Często zadawane pytania
Czy lekcja „Uruchamiać zadania za pomocą torchrun” jest bezpłatna?
Tak — pełny tekst „Uruchamiać zadania za pomocą torchrun” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Deep Learning Academy, przejdź na CoddyKit PRO. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Uruchamiać zadania za pomocą torchrun”?
Tworzyć i koordynować procesy robocze Ćwiczysz Deep Learning Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Deep Learning Academy?
Nie wymagamy żadnego doświadczenia. Deep Learning Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Uruchamiać zadania za pomocą torchrun”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Deep Learning Academy?
Tak. Każda lekcja Deep Learning Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Równoległość danych a równoległość modelu
- Podstawy DistributedDataParallel
- Synchronizowany Batch Norm i shardowany stan
- Uruchamiać zadania za pomocą torchrun