0Pricing
Deep Learning Academy · レッスン

torchrunでジョブを起動する

ワーカープロセスを生成して連携させます

「torchrunでジョブを起動する」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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.py

Processes 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.py

Environment 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.py

Point 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.py

Survive 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.py

Log 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.

よくある質問

「torchrunでジョブを起動する」レッスンは無料ですか?

はい。「torchrunでジョブを起動する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。

「torchrunでジョブを起動する」で何を学びますか?

ワーカープロセスを生成して連携させます ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Deep Learning Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「torchrunでジョブを起動する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDeep Learning Academyレッスンでコードを書いて実行できますか?

はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. データ並列とモデル並列
  2. DistributedDataParallelの基礎
  3. 同期Batch Normとシャード化state
  4. torchrunでジョブを起動する
← Deep Learning Academyに戻る