11Parameter Server
22================
33
4- This document walks through how to implement a simple parameter server example
5- using actors. To run the application, first install some dependencies.
4+ This document walks through how to implement simple synchronous and asynchronous
5+ parameter servers using actors. To run the application, first install some
6+ dependencies.
67
78.. code-block :: bash
89
@@ -12,17 +13,24 @@ You can view the `code for this example`_.
1213
1314.. _`code for this example` : https://github.com/ray-project/ray/tree/master/examples/parameter_server
1415
15- The example can be run as follows.
16+ The examples can be run as follows.
1617
1718.. code-block :: bash
1819
19- python ray/examples/parameter_server/parameter_server.py --num-workers=4
20+ # Run the asynchronous parameter server.
21+ python ray/examples/parameter_server/async_parameter_server.py --num-workers=4
22+
23+ # Run the synchronous parameter server.
24+ python ray/examples/parameter_server/sync_parameter_server.py --num-workers=4
2025
2126 Note that this examples uses distributed actor handles, which are still
2227considered experimental.
2328
24- The parameter server itself is implemented as an actor, which exposes the
25- methods ``push `` and ``pull ``.
29+ Asynchronous Parameter Server
30+ -----------------------------
31+
32+ The asynchronous parameter server itself is implemented as an actor, which
33+ exposes the methods ``push `` and ``pull ``.
2634
2735.. code-block :: python
2836
@@ -62,3 +70,58 @@ Then we can create a parameter server and initiate training as follows.
6270
6371 ps = ParameterServer.remote(keys, initial_values)
6472 worker_tasks = [worker_task.remote(ps) for _ in range (4 )]
73+
74+ Synchronous Parameter Server
75+ ----------------------------
76+
77+ The parameter server is implemented as an actor, which exposes the
78+ methods ``apply_gradients `` and ``get_weights ``. A constant linear scaling
79+ rule is applied by scaling the learning rate by the number of workers.
80+
81+ .. code-block :: python
82+
83+ @ray.remote
84+ class ParameterServer (object ):
85+ def __init__ (self , learning_rate ):
86+ self .net = model.SimpleCNN(learning_rate = learning_rate)
87+
88+ def apply_gradients (self , * gradients ):
89+ self .net.apply_gradients(np.mean(gradients, axis = 0 ))
90+ return self .net.variables.get_flat()
91+
92+ def get_weights (self ):
93+ return self .net.variables.get_flat()
94+
95+
96+ Workers are actors which expose the method ``compute_gradients ``.
97+
98+ .. code-block :: python
99+
100+ @ray.remote
101+ class Worker (object ):
102+ def __init__ (self , worker_index , batch_size = 50 ):
103+ self .worker_index = worker_index
104+ self .batch_size = batch_size
105+ self .mnist = input_data.read_data_sets(" MNIST_data" , one_hot = True ,
106+ seed = worker_index)
107+ self .net = model.SimpleCNN()
108+
109+ def compute_gradients (self , weights ):
110+ self .net.variables.set_flat(weights)
111+ xs, ys = self .mnist.train.next_batch(self .batch_size)
112+ return self .net.compute_gradients(xs, ys)
113+
114+ Training alternates between computing the gradients given the current weights
115+ from the parameter server and updating the parameter server's weights with the
116+ resulting gradients.
117+
118+ .. code-block :: python
119+
120+ while True :
121+ gradients = [worker.compute_gradients.remote(current_weights)
122+ for worker in workers]
123+ current_weights = ps.apply_gradients.remote(* gradients)
124+
125+ Both of these examples implement the parameter server using a single actor,
126+ however they can be easily extended to **shard the parameters across multiple
127+ actors **.
0 commit comments