MCPcopy Create free account
hub / github.com/pytorch/tutorials / run_worker

Function run_worker

advanced_source/rpc_ddp_tutorial/main.py:112–178  ·  view source on GitHub ↗

r""" A wrapper function that initializes RPC, calls the function, and shuts down RPC.

(rank, world_size)

Source from the content-addressed store, hash-verified

110
111# BEGIN run_worker
112def run_worker(rank, world_size):
113 r"""
114 A wrapper function that initializes RPC, calls the function, and shuts down
115 RPC.
116 """
117
118 # We need to use different port numbers in TCP init_method for init_rpc and
119 # init_process_group to avoid port conflicts.
120 rpc_backend_options = TensorPipeRpcBackendOptions()
121 rpc_backend_options.init_method = "tcp://localhost:29501"
122
123 # Rank 2 is master, 3 is ps and 0 and 1 are trainers.
124 if rank == 2:
125 rpc.init_rpc(
126 "master",
127 rank=rank,
128 world_size=world_size,
129 rpc_backend_options=rpc_backend_options,
130 )
131
132 remote_emb_module = RemoteModule(
133 "ps",
134 torch.nn.EmbeddingBag,
135 args=(NUM_EMBEDDINGS, EMBEDDING_DIM),
136 kwargs={"mode": "sum"},
137 )
138
139 # Run the training loop on trainers.
140 futs = []
141 for trainer_rank in [0, 1]:
142 trainer_name = "trainer{}".format(trainer_rank)
143 fut = rpc.rpc_async(
144 trainer_name, _run_trainer, args=(remote_emb_module, trainer_rank)
145 )
146 futs.append(fut)
147
148 # Wait for all training to finish.
149 for fut in futs:
150 fut.wait()
151 elif rank <= 1:
152 # Initialize process group for Distributed DataParallel on trainers.
153 dist.init_process_group(
154 backend="gloo", rank=rank, world_size=2, init_method="tcp://localhost:29500"
155 )
156
157 # Initialize RPC.
158 trainer_name = "trainer{}".format(rank)
159 rpc.init_rpc(
160 trainer_name,
161 rank=rank,
162 world_size=world_size,
163 rpc_backend_options=rpc_backend_options,
164 )
165
166 # Trainer just waits for RPCs from master.
167 else:
168 rpc.init_rpc(
169 "ps",

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected