forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_lib_test.py
More file actions
384 lines (323 loc) · 14 KB
/
Copy pathserver_lib_test.py
File metadata and controls
384 lines (323 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for tf.GrpcServer."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
import numpy as np
import tensorflow as tf
class GrpcServerTest(tf.test.TestCase):
def testRunStep(self):
server = tf.train.Server.create_local_server()
with tf.Session(server.target) as sess:
c = tf.constant([[2, 1]])
d = tf.constant([[1], [2]])
e = tf.matmul(c, d)
self.assertAllEqual([[4]], sess.run(e))
# TODO(mrry): Add `server.stop()` and `server.join()` when these work.
def testMultipleSessions(self):
server = tf.train.Server.create_local_server()
c = tf.constant([[2, 1]])
d = tf.constant([[1], [2]])
e = tf.matmul(c, d)
sess_1 = tf.Session(server.target)
sess_2 = tf.Session(server.target)
self.assertAllEqual([[4]], sess_1.run(e))
self.assertAllEqual([[4]], sess_2.run(e))
sess_1.close()
sess_2.close()
# TODO(mrry): Add `server.stop()` and `server.join()` when these work.
# Verifies behavior of multiple variables with multiple sessions connecting to
# the same server.
def testSameVariablesNoClear(self):
server = tf.train.Server.create_local_server()
with tf.Session(server.target) as sess_1:
v0 = tf.Variable([[2, 1]], name="v0")
v1 = tf.Variable([[1], [2]], name="v1")
v2 = tf.matmul(v0, v1)
sess_1.run([v0.initializer, v1.initializer])
self.assertAllEqual([[4]], sess_1.run(v2))
with tf.Session(server.target) as sess_2:
new_v0 = tf.get_default_graph().get_tensor_by_name("v0:0")
new_v1 = tf.get_default_graph().get_tensor_by_name("v1:0")
new_v2 = tf.matmul(new_v0, new_v1)
self.assertAllEqual([[4]], sess_2.run(new_v2))
# Verifies behavior of tf.Session.reset().
def testSameVariablesClear(self):
server = tf.train.Server.create_local_server()
# Creates a graph with 2 variables.
v0 = tf.Variable([[2, 1]], name="v0")
v1 = tf.Variable([[1], [2]], name="v1")
v2 = tf.matmul(v0, v1)
# Verifies that both sessions connecting to the same target return
# the same results.
sess_1 = tf.Session(server.target)
sess_2 = tf.Session(server.target)
sess_1.run(tf.initialize_all_variables())
self.assertAllEqual([[4]], sess_1.run(v2))
self.assertAllEqual([[4]], sess_2.run(v2))
# Resets target. sessions abort. Use sess_2 to verify.
tf.Session.reset(server.target)
with self.assertRaises(tf.errors.AbortedError):
self.assertAllEqual([[4]], sess_2.run(v2))
# Connects to the same target. Device memory for the variables would have
# been released, so they will be unitialized.
sess_2 = tf.Session(server.target)
with self.assertRaises(tf.errors.FailedPreconditionError):
sess_2.run(v2)
# Reinitialzes the variables.
sess_2.run(tf.initialize_all_variables())
self.assertAllEqual([[4]], sess_2.run(v2))
sess_2.close()
# Verifies behavior of tf.Session.reset() with multiple containers using
# default container names as defined by the target name.
def testSameVariablesClearContainer(self):
# Starts two servers with different names so they map to different
# resource "containers".
server0 = tf.train.Server({"local0": ["localhost:0"]}, protocol="grpc",
start=True)
server1 = tf.train.Server({"local1": ["localhost:0"]}, protocol="grpc",
start=True)
# Creates a graph with 2 variables.
v0 = tf.Variable(1.0, name="v0")
v1 = tf.Variable(2.0, name="v0")
# Initializes the variables. Verifies that the values are correct.
sess_0 = tf.Session(server0.target)
sess_1 = tf.Session(server1.target)
sess_0.run(v0.initializer)
sess_1.run(v1.initializer)
self.assertAllEqual(1.0, sess_0.run(v0))
self.assertAllEqual(2.0, sess_1.run(v1))
# Resets container "local0". Verifies that v0 is no longer initialized.
tf.Session.reset(server0.target, ["local0"])
sess = tf.Session(server0.target)
with self.assertRaises(tf.errors.FailedPreconditionError):
sess.run(v0)
# Reinitializes v0 for the following test.
sess.run(v0.initializer)
# Verifies that v1 is still valid.
self.assertAllEqual(2.0, sess_1.run(v1))
# Resets container "local1". Verifies that v1 is no longer initialized.
tf.Session.reset(server1.target, ["local1"])
sess = tf.Session(server1.target)
with self.assertRaises(tf.errors.FailedPreconditionError):
sess.run(v1)
# Verifies that v0 is still valid.
sess = tf.Session(server0.target)
self.assertAllEqual(1.0, sess.run(v0))
# Verifies behavior of tf.Session.reset() with multiple containers using
# tf.container.
def testMultipleContainers(self):
with tf.container("test0"):
v0 = tf.Variable(1.0, name="v0")
with tf.container("test1"):
v1 = tf.Variable(2.0, name="v0")
server = tf.train.Server.create_local_server()
sess = tf.Session(server.target)
sess.run(tf.initialize_all_variables())
self.assertAllEqual(1.0, sess.run(v0))
self.assertAllEqual(2.0, sess.run(v1))
# Resets container. Session aborts.
tf.Session.reset(server.target, ["test0"])
with self.assertRaises(tf.errors.AbortedError):
sess.run(v1)
# Connects to the same target. Device memory for the v0 would have
# been released, so it will be unitialized. But v1 should still
# be valid.
sess = tf.Session(server.target)
with self.assertRaises(tf.errors.FailedPreconditionError):
sess.run(v0)
self.assertAllEqual(2.0, sess.run(v1))
# Verifies various reset failures.
def testResetFails(self):
# Creates variable with container name.
with tf.container("test0"):
v0 = tf.Variable(1.0, name="v0")
# Creates variable with default container.
v1 = tf.Variable(2.0, name="v1")
# Verifies resetting the non-existent target returns error.
with self.assertRaises(tf.errors.NotFoundError):
tf.Session.reset("nonexistent", ["test0"])
# Verifies resetting with config.
# Verifies that resetting target with no server times out.
with self.assertRaises(tf.errors.DeadlineExceededError):
tf.Session.reset("grpc://localhost:0", ["test0"],
config=tf.ConfigProto(operation_timeout_in_ms=5))
# Verifies no containers are reset with non-existent container.
server = tf.train.Server.create_local_server()
sess = tf.Session(server.target)
sess.run(tf.initialize_all_variables())
self.assertAllEqual(1.0, sess.run(v0))
self.assertAllEqual(2.0, sess.run(v1))
# No container is reset, but the server is reset.
tf.Session.reset(server.target, ["test1"])
# Verifies that both variables are still valid.
sess = tf.Session(server.target)
self.assertAllEqual(1.0, sess.run(v0))
self.assertAllEqual(2.0, sess.run(v1))
def testLargeConstant(self):
server = tf.train.Server.create_local_server()
with tf.Session(server.target) as sess:
const_val = np.empty([10000, 3000], dtype=np.float32)
const_val.fill(0.5)
c = tf.constant(const_val)
shape_t = tf.shape(c)
self.assertAllEqual([10000, 3000], sess.run(shape_t))
def testLargeFetch(self):
server = tf.train.Server.create_local_server()
with tf.Session(server.target) as sess:
c = tf.fill([10000, 3000], 0.5)
expected_val = np.empty([10000, 3000], dtype=np.float32)
expected_val.fill(0.5)
self.assertAllEqual(expected_val, sess.run(c))
def testLargeFeed(self):
server = tf.train.Server.create_local_server()
with tf.Session(server.target) as sess:
feed_val = np.empty([10000, 3000], dtype=np.float32)
feed_val.fill(0.5)
p = tf.placeholder(tf.float32, shape=[10000, 3000])
min_t = tf.reduce_min(p)
max_t = tf.reduce_max(p)
min_val, max_val = sess.run([min_t, max_t], feed_dict={p: feed_val})
self.assertEqual(0.5, min_val)
self.assertEqual(0.5, max_val)
def testCloseCancelsBlockingOperation(self):
server = tf.train.Server.create_local_server()
sess = tf.Session(server.target)
q = tf.FIFOQueue(10, [tf.float32])
enqueue_op = q.enqueue(37.0)
dequeue_t = q.dequeue()
sess.run(enqueue_op)
sess.run(dequeue_t)
def blocking_dequeue():
with self.assertRaises(tf.errors.CancelledError):
sess.run(dequeue_t)
blocking_thread = self.checkedThread(blocking_dequeue)
blocking_thread.start()
time.sleep(0.5)
sess.close()
blocking_thread.join()
def testSetConfiguration(self):
config = tf.ConfigProto(
gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.1))
# Configure a server using the default local server options.
server = tf.train.Server.create_local_server(config=config, start=False)
self.assertEqual(
0.1,
server.server_def.default_session_config
.gpu_options.per_process_gpu_memory_fraction)
# Configure a server using an explicit ServerDefd with an
# overridden config.
cluster_def = tf.train.ClusterSpec(
{"localhost": ["localhost:0"]}).as_cluster_def()
server_def = tf.train.ServerDef(
cluster=cluster_def, job_name="localhost", task_index=0,
protocol="grpc")
server = tf.train.Server(server_def, config=config, start=False)
self.assertEqual(
0.1,
server.server_def.default_session_config
.gpu_options.per_process_gpu_memory_fraction)
def testInvalidHostname(self):
with self.assertRaisesRegexp(tf.errors.InvalidArgumentError, "port"):
_ = tf.train.Server({"local": ["localhost"]},
job_name="local",
task_index=0)
def testInteractiveSession(self):
server = tf.train.Server.create_local_server()
# TODO(b/29900832): Remove this assertion when the bug is fixed.
a = tf.constant(1.0)
with self.assertRaisesRegexp(tf.errors.UnimplementedError, "pruned"):
sess = tf.InteractiveSession(target=server.target)
sess.run(a)
# TODO(b/29900832): The following code fails (without the unimplemented
# check in `tensorflow::MasterSession`):
# a = tf.constant(1.0)
# b = tf.constant(2.0)
# self.assertEqual(1.0, sess.run(a))
# self.assertEqual(2.0, sess.run(b))
class ServerDefTest(tf.test.TestCase):
def testLocalServer(self):
cluster_def = tf.train.ClusterSpec(
{"local": ["localhost:2222"]}).as_cluster_def()
server_def = tf.train.ServerDef(
cluster=cluster_def, job_name="local", task_index=0, protocol="grpc")
self.assertProtoEquals("""
cluster {
job { name: 'local' tasks { key: 0 value: 'localhost:2222' } }
}
job_name: 'local' task_index: 0 protocol: 'grpc'
""", server_def)
# Verifies round trip from Proto->Spec->Proto is correct.
cluster_spec = tf.train.ClusterSpec(cluster_def)
self.assertProtoEquals(cluster_def, cluster_spec.as_cluster_def())
def testTwoProcesses(self):
cluster_def = tf.train.ClusterSpec(
{"local": ["localhost:2222", "localhost:2223"]}).as_cluster_def()
server_def = tf.train.ServerDef(
cluster=cluster_def, job_name="local", task_index=1, protocol="grpc")
self.assertProtoEquals("""
cluster {
job { name: 'local' tasks { key: 0 value: 'localhost:2222' }
tasks { key: 1 value: 'localhost:2223' } }
}
job_name: 'local' task_index: 1 protocol: 'grpc'
""", server_def)
# Verifies round trip from Proto->Spec->Proto is correct.
cluster_spec = tf.train.ClusterSpec(cluster_def)
self.assertProtoEquals(cluster_def, cluster_spec.as_cluster_def())
def testTwoJobs(self):
cluster_def = tf.train.ClusterSpec(
{"ps": ["ps0:2222", "ps1:2222"],
"worker": ["worker0:2222", "worker1:2222", "worker2:2222"]}
).as_cluster_def()
server_def = tf.train.ServerDef(
cluster=cluster_def, job_name="worker", task_index=2, protocol="grpc")
self.assertProtoEquals("""
cluster {
job { name: 'ps' tasks { key: 0 value: 'ps0:2222' }
tasks { key: 1 value: 'ps1:2222' } }
job { name: 'worker' tasks { key: 0 value: 'worker0:2222' }
tasks { key: 1 value: 'worker1:2222' }
tasks { key: 2 value: 'worker2:2222' } }
}
job_name: 'worker' task_index: 2 protocol: 'grpc'
""", server_def)
# Verifies round trip from Proto->Spec->Proto is correct.
cluster_spec = tf.train.ClusterSpec(cluster_def)
self.assertProtoEquals(cluster_def, cluster_spec.as_cluster_def())
def testClusterSpec(self):
cluster_spec = tf.train.ClusterSpec(
{"ps": ["ps0:2222", "ps1:2222"],
"worker": ["worker0:2222", "worker1:2222", "worker2:2222"]})
expected_proto = """
job { name: 'ps' tasks { key: 0 value: 'ps0:2222' }
tasks { key: 1 value: 'ps1:2222' } }
job { name: 'worker' tasks { key: 0 value: 'worker0:2222' }
tasks { key: 1 value: 'worker1:2222' }
tasks { key: 2 value: 'worker2:2222' } }
"""
self.assertProtoEquals(expected_proto, cluster_spec.as_cluster_def())
self.assertProtoEquals(
expected_proto, tf.train.ClusterSpec(cluster_spec).as_cluster_def())
self.assertProtoEquals(
expected_proto,
tf.train.ClusterSpec(cluster_spec.as_cluster_def()).as_cluster_def())
self.assertProtoEquals(
expected_proto,
tf.train.ClusterSpec(cluster_spec.as_dict()).as_cluster_def())
if __name__ == "__main__":
tf.test.main()