MCPcopy Create free account
hub / github.com/pytorch/examples / ParameterServer

Class ParameterServer

distributed/rpc/parameter_server/rpc_parameter_server.py:84–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

82
83# --------- Parameter Server --------------------
84class ParameterServer(nn.Module):
85 def __init__(self, num_gpus=0):
86 super().__init__()
87 model = Net(num_gpus=num_gpus)
88 self.model = model
89 if torch.accelerator.is_available() and num_gpus > 0:
90 acc = torch.accelerator.current_accelerator()
91 self.input_device = torch.device(f'{acc}:0')
92 else:
93 self.input_device = torch.device("cpu")
94
95 def forward(self, inp):
96 inp = inp.to(self.input_device)
97 out = self.model(inp)
98 # This output is forwarded over RPC, which as of 1.5.0 only accepts CPU tensors.
99 # Tensors must be moved in and out of GPU memory due to this.
100 out = out.to("cpu")
101 return out
102
103 # Use dist autograd to retrieve gradients accumulated for this model.
104 # Primarily used for verification.
105 def get_dist_gradients(self, cid):
106 grads = dist_autograd.get_gradients(cid)
107 # This output is forwarded over RPC, which as of 1.5.0 only accepts CPU tensors.
108 # Tensors must be moved in and out of GPU memory due to this.
109 cpu_grads = {}
110 for k, v in grads.items():
111 k_cpu, v_cpu = k.to("cpu"), v.to("cpu")
112 cpu_grads[k_cpu] = v_cpu
113 return cpu_grads
114
115 # Wrap local parameters in a RRef. Needed for building the
116 # DistributedOptimizer which optimizes parameters remotely.
117 def get_param_rrefs(self):
118 param_rrefs = [rpc.RRef(param) for param in self.model.parameters()]
119 return param_rrefs
120
121param_server = None
122global_lock = Lock()

Callers 1

get_parameter_serverFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected