File tree Expand file tree Collapse file tree 2 files changed +44
-4
lines changed Expand file tree Collapse file tree 2 files changed +44
-4
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,22 @@ def average_gradients(tower_grads):
104
104
return average_grads
105
105
106
106
107
+ # By default, all variables will be placed on '/gpu:0'
108
+ # So we need a custom device function, to assign all variables to '/cpu:0'
109
+ # Note: If GPUs are peered, '/gpu:0' can be a faster option
110
+ PS_OPS = ['Variable' , 'VariableV2' , 'AutoReloadVariable' ]
111
+
112
+ def assign_to_device (device , ps_device = '/cpu:0' ):
113
+ def _assign (op ):
114
+ node_def = op if isinstance (op , tf .NodeDef ) else op .node_def
115
+ if node_def .op in PS_OPS :
116
+ return "/" + ps_device
117
+ else :
118
+ return device
119
+
120
+ return _assign
121
+
122
+
107
123
# Place all ops on CPU by default
108
124
with tf .device ('/cpu:0' ):
109
125
tower_grads = []
@@ -115,7 +131,7 @@ def average_gradients(tower_grads):
115
131
116
132
# Loop over all GPUs and construct their own computation graph
117
133
for i in range (num_gpus ):
118
- with tf .device ('/gpu:%d' % i ):
134
+ with tf .device (assign_to_device ( '/gpu:{}' . format ( i ), ps_device = '/cpu:0' ) ):
119
135
120
136
# Split data between GPUs
121
137
_x = X [i * batch_size : (i + 1 ) * batch_size ]
Original file line number Diff line number Diff line change 167
167
{
168
168
"cell_type" : " code" ,
169
169
"execution_count" : 4 ,
170
+ "metadata" : {
171
+ "collapsed" : true
172
+ },
173
+ "outputs" : [],
174
+ "source" : [
175
+ " # By default, all variables will be placed on '/gpu:0'\n " ,
176
+ " # So we need a custom device function, to assign all variables to '/cpu:0'\n " ,
177
+ " # Note: If GPUs are peered, '/gpu:0' can be a faster option\n " ,
178
+ " PS_OPS = ['Variable', 'VariableV2', 'AutoReloadVariable']\n " ,
179
+ " \n " ,
180
+ " def assign_to_device(device, ps_device='/cpu:0'):\n " ,
181
+ " def _assign(op):\n " ,
182
+ " node_def = op if isinstance(op, tf.NodeDef) else op.node_def\n " ,
183
+ " if node_def.op in PS_OPS:\n " ,
184
+ " return \" /\" + ps_device\n " ,
185
+ " else:\n " ,
186
+ " return device\n " ,
187
+ " \n " ,
188
+ " return _assign"
189
+ ]
190
+ },
191
+ {
192
+ "cell_type" : " code" ,
193
+ "execution_count" : 5 ,
170
194
"metadata" : {
171
195
"collapsed" : false ,
172
196
"scrolled" : false
214
238
" \n " ,
215
239
" # Loop over all GPUs and construct their own computation graph\n " ,
216
240
" for i in range(num_gpus):\n " ,
217
- " with tf.device('/gpu:%d' % i ):\n " ,
241
+ " with tf.device(assign_to_device( '/gpu:{}'.format(i), ps_device='/cpu:0') ):\n " ,
218
242
" \n " ,
219
243
" # Split data between GPUs\n " ,
220
244
" _x = X[i * batch_size: (i+1) * batch_size]\n " ,
289
313
"language_info" : {
290
314
"codemirror_mode" : {
291
315
"name" : " ipython" ,
292
- "version" : 2.0
316
+ "version" : 2
293
317
},
294
318
"file_extension" : " .py" ,
295
319
"mimetype" : " text/x-python" ,
301
325
},
302
326
"nbformat" : 4 ,
303
327
"nbformat_minor" : 0
304
- }
328
+ }
You can’t perform that action at this time.
0 commit comments