Skip to content

Commit 21883bd

Browse files
author
hhsecond
committed
test cases for crash test
1 parent 9ab3cec commit 21883bd

File tree

1 file changed

+125
-52
lines changed

1 file changed

+125
-52
lines changed

test/tests_tensorflow.py

Lines changed: 125 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import redis
2+
from functools import wraps
23

34
from includes import *
45

@@ -7,11 +8,19 @@
78
'''
89

910

10-
def test_run_mobilenet(env):
11-
if not TEST_TF:
12-
env.debugPrint("skipping {} since TEST_TF=0".format(sys._getframe().f_code.co_name), force=True)
13-
return
11+
def skip_if_no_TF(f):
12+
@wraps(f)
13+
def wrapper(env, *args, **kwargs):
14+
if not TEST_TF:
15+
env.debugPrint("skipping {} since TEST_TF=0".format(
16+
sys._getframe().f_code.co_name), force=True)
17+
return
18+
return f(env, *args, **kwargs)
19+
return wrapper
20+
1421

22+
@skip_if_no_TF
23+
def test_run_mobilenet(env):
1524
con = env.getConnection()
1625

1726
input_var = 'input'
@@ -24,26 +33,31 @@ def test_run_mobilenet(env):
2433

2534
ensureSlaveSynced(con, env)
2635

27-
mobilenet_model_serialized = con.execute_command('AI.MODELGET', 'mobilenet')
36+
mobilenet_model_serialized = con.execute_command(
37+
'AI.MODELGET', 'mobilenet')
2838

2939
ensureSlaveSynced(con, env)
3040
if env.useSlaves:
3141
con2 = env.getSlaveConnection()
32-
slave_mobilenet_model_serialized = con2.execute_command('AI.MODELGET', 'mobilenet')
33-
env.assertEqual(len(mobilenet_model_serialized), len(slave_mobilenet_model_serialized))
42+
slave_mobilenet_model_serialized = con2.execute_command(
43+
'AI.MODELGET', 'mobilenet')
44+
env.assertEqual(len(mobilenet_model_serialized),
45+
len(slave_mobilenet_model_serialized))
3446

3547
con.execute_command('AI.TENSORSET', 'input',
3648
'FLOAT', 1, img.shape[1], img.shape[0], img.shape[2],
3749
'BLOB', img.tobytes())
3850

3951
ensureSlaveSynced(con, env)
4052
input_tensor_meta = con.execute_command('AI.TENSORGET', 'input', 'META')
41-
env.assertEqual([b'FLOAT', [1, img.shape[1], img.shape[0], img.shape[2]]], input_tensor_meta)
53+
env.assertEqual(
54+
[b'FLOAT', [1, img.shape[1], img.shape[0], img.shape[2]]], input_tensor_meta)
4255

4356
ensureSlaveSynced(con, env)
4457
if env.useSlaves:
4558
con2 = env.getSlaveConnection()
46-
slave_tensor_meta = con2.execute_command('AI.TENSORGET', 'input', 'META')
59+
slave_tensor_meta = con2.execute_command(
60+
'AI.TENSORGET', 'input', 'META')
4761
env.assertEqual(input_tensor_meta, slave_tensor_meta)
4862

4963
con.execute_command('AI.MODELRUN', 'mobilenet',
@@ -63,19 +77,18 @@ def test_run_mobilenet(env):
6377

6478
if env.useSlaves:
6579
con2 = env.getSlaveConnection()
66-
slave_dtype, slave_shape, slave_data = con2.execute_command('AI.TENSORGET', 'output', 'BLOB')
80+
slave_dtype, slave_shape, slave_data = con2.execute_command(
81+
'AI.TENSORGET', 'output', 'BLOB')
6782
env.assertEqual(dtype, slave_dtype)
6883
env.assertEqual(shape, slave_shape)
6984
env.assertEqual(data, slave_data)
7085

7186

87+
@skip_if_no_TF
7288
def test_run_mobilenet_multiproc(env):
73-
if not TEST_TF:
74-
env.debugPrint("skipping {} since TEST_TF=0".format(sys._getframe().f_code.co_name), force=True)
75-
return
76-
7789
if VALGRIND:
78-
env.debugPrint("skipping {} since VALGRIND=1".format(sys._getframe().f_code.co_name), force=True)
90+
env.debugPrint("skipping {} since VALGRIND=1".format(
91+
sys._getframe().f_code.co_name), force=True)
7992
return
8093

8194
con = env.getConnection()
@@ -106,17 +119,15 @@ def test_run_mobilenet_multiproc(env):
106119

107120
if env.useSlaves:
108121
con2 = env.getSlaveConnection()
109-
slave_dtype, slave_shape, slave_data = con2.execute_command('AI.TENSORGET', 'output', 'BLOB')
122+
slave_dtype, slave_shape, slave_data = con2.execute_command(
123+
'AI.TENSORGET', 'output', 'BLOB')
110124
env.assertEqual(dtype, slave_dtype)
111125
env.assertEqual(shape, slave_shape)
112126
env.assertEqual(data, slave_data)
113127

114128

129+
@skip_if_no_TF
115130
def test_del_tf_model(env):
116-
if not TEST_TF:
117-
env.debugPrint("skipping {} since TEST_TF=0".format(sys._getframe().f_code.co_name), force=True)
118-
return
119-
120131
con = env.getConnection()
121132

122133
test_data_path = os.path.join(os.path.dirname(__file__), 'test_data')
@@ -154,14 +165,12 @@ def test_del_tf_model(env):
154165
except Exception as e:
155166
exception = e
156167
env.assertEqual(type(exception), redis.exceptions.ResponseError)
157-
env.assertEqual("WRONGTYPE Operation against a key holding the wrong kind of value", exception.__str__())
168+
env.assertEqual(
169+
"WRONGTYPE Operation against a key holding the wrong kind of value", exception.__str__())
158170

159171

172+
@skip_if_no_TF
160173
def test_run_tf_model(env):
161-
if not TEST_TF:
162-
env.debugPrint("skipping {} since TEST_TF=0".format(sys._getframe().f_code.co_name), force=True)
163-
return
164-
165174
con = env.getConnection()
166175

167176
test_data_path = os.path.join(os.path.dirname(__file__), 'test_data')
@@ -182,8 +191,10 @@ def test_run_tf_model(env):
182191
# env.assertEqual(ret[0], b'TF')
183192
# env.assertEqual(ret[1], b'CPU')
184193

185-
con.execute_command('AI.TENSORSET', 'a', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
186-
con.execute_command('AI.TENSORSET', 'b', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
194+
con.execute_command('AI.TENSORSET', 'a', 'FLOAT',
195+
2, 2, 'VALUES', 2, 3, 2, 3)
196+
con.execute_command('AI.TENSORSET', 'b', 'FLOAT',
197+
2, 2, 'VALUES', 2, 3, 2, 3)
187198

188199
ensureSlaveSynced(con, env)
189200

@@ -217,11 +228,8 @@ def test_run_tf_model(env):
217228
env.assertFalse(con2.execute_command('EXISTS', 'm'))
218229

219230

231+
@skip_if_no_TF
220232
def test_run_tf_model_errors(env):
221-
if not TEST_TF:
222-
env.debugPrint("skipping {} since TEST_TF=0".format(sys._getframe().f_code.co_name), force=True)
223-
return
224-
225233
con = env.getConnection()
226234

227235
test_data_path = os.path.join(os.path.dirname(__file__), 'test_data')
@@ -245,7 +253,8 @@ def test_run_tf_model_errors(env):
245253
except Exception as e:
246254
exception = e
247255
env.assertEqual(type(exception), redis.exceptions.ResponseError)
248-
env.assertEqual("wrong number of arguments for 'AI.MODELGET' command", exception.__str__())
256+
env.assertEqual(
257+
"wrong number of arguments for 'AI.MODELGET' command", exception.__str__())
249258

250259
# ERR WRONGTYPE
251260
con.execute_command('SET', 'NOT_MODEL', 'BAR')
@@ -254,7 +263,8 @@ def test_run_tf_model_errors(env):
254263
except Exception as e:
255264
exception = e
256265
env.assertEqual(type(exception), redis.exceptions.ResponseError)
257-
env.assertEqual("WRONGTYPE Operation against a key holding the wrong kind of value", exception.__str__())
266+
env.assertEqual(
267+
"WRONGTYPE Operation against a key holding the wrong kind of value", exception.__str__())
258268
# cleanup
259269
con.execute_command('DEL', 'NOT_MODEL')
260270

@@ -365,6 +375,7 @@ def test_run_tf_model_errors(env):
365375
env.assertEqual(type(exception), redis.exceptions.ResponseError)
366376

367377

378+
@skip_if_no_TF
368379
def test_run_tf_model_autobatch(env):
369380
if not TEST_PT:
370381
return
@@ -382,17 +393,22 @@ def test_run_tf_model_autobatch(env):
382393
'INPUTS', 'a', 'b', 'OUTPUTS', 'mul', model_pb)
383394
env.assertEqual(ret, b'OK')
384395

385-
con.execute_command('AI.TENSORSET', 'a', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
386-
con.execute_command('AI.TENSORSET', 'b', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
396+
con.execute_command('AI.TENSORSET', 'a', 'FLOAT',
397+
2, 2, 'VALUES', 2, 3, 2, 3)
398+
con.execute_command('AI.TENSORSET', 'b', 'FLOAT',
399+
2, 2, 'VALUES', 2, 3, 2, 3)
387400

388-
con.execute_command('AI.TENSORSET', 'd', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
389-
con.execute_command('AI.TENSORSET', 'e', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
401+
con.execute_command('AI.TENSORSET', 'd', 'FLOAT',
402+
2, 2, 'VALUES', 2, 3, 2, 3)
403+
con.execute_command('AI.TENSORSET', 'e', 'FLOAT',
404+
2, 2, 'VALUES', 2, 3, 2, 3)
390405

391406
ensureSlaveSynced(con, env)
392407

393408
def run():
394409
con = env.getConnection()
395-
con.execute_command('AI.MODELRUN', 'm', 'INPUTS', 'd', 'e', 'OUTPUTS', 'f')
410+
con.execute_command('AI.MODELRUN', 'm', 'INPUTS',
411+
'd', 'e', 'OUTPUTS', 'f')
396412
ensureSlaveSynced(con, env)
397413

398414
t = threading.Thread(target=run)
@@ -411,11 +427,8 @@ def run():
411427
env.assertEqual(values, [b'4', b'9', b'4', b'9'])
412428

413429

430+
@skip_if_no_TF
414431
def test_tensorflow_modelinfo(env):
415-
if not TEST_TF:
416-
env.debugPrint("skipping {} since TEST_TF=0".format(sys._getframe().f_code.co_name), force=True)
417-
return
418-
419432
con = env.getConnection()
420433

421434
test_data_path = os.path.join(os.path.dirname(__file__), 'test_data')
@@ -428,17 +441,20 @@ def test_tensorflow_modelinfo(env):
428441
'INPUTS', 'a', 'b', 'OUTPUTS', 'mul', model_pb)
429442
env.assertEqual(ret, b'OK')
430443

431-
ret = con.execute_command('AI.TENSORSET', 'a', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
444+
ret = con.execute_command(
445+
'AI.TENSORSET', 'a', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
432446
env.assertEqual(ret, b'OK')
433447

434-
ret = con.execute_command('AI.TENSORSET', 'b', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
448+
ret = con.execute_command(
449+
'AI.TENSORSET', 'b', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
435450
env.assertEqual(ret, b'OK')
436451

437452
ensureSlaveSynced(con, env)
438453

439454
previous_duration = 0
440455
for call in range(1, 10):
441-
ret = con.execute_command('AI.MODELRUN', 'm', 'INPUTS', 'a', 'b', 'OUTPUTS', 'c')
456+
ret = con.execute_command(
457+
'AI.MODELRUN', 'm', 'INPUTS', 'a', 'b', 'OUTPUTS', 'c')
442458
env.assertEqual(ret, b'OK')
443459
ensureSlaveSynced(con, env)
444460

@@ -466,11 +482,8 @@ def test_tensorflow_modelinfo(env):
466482
env.assertEqual(info_dict_0['ERRORS'], 0)
467483

468484

485+
@skip_if_no_TF
469486
def test_tensorflow_modelrun_disconnect(env):
470-
if not TEST_TF:
471-
env.debugPrint("skipping {} since TEST_TF=0".format(sys._getframe().f_code.co_name), force=True)
472-
return
473-
474487
red = env.getConnection()
475488

476489
test_data_path = os.path.join(os.path.dirname(__file__), 'test_data')
@@ -483,13 +496,73 @@ def test_tensorflow_modelrun_disconnect(env):
483496
'INPUTS', 'a', 'b', 'OUTPUTS', 'mul', model_pb)
484497
env.assertEqual(ret, b'OK')
485498

486-
ret = red.execute_command('AI.TENSORSET', 'a', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
499+
ret = red.execute_command(
500+
'AI.TENSORSET', 'a', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
487501
env.assertEqual(ret, b'OK')
488502

489-
ret = red.execute_command('AI.TENSORSET', 'b', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
503+
ret = red.execute_command(
504+
'AI.TENSORSET', 'b', 'FLOAT', 2, 2, 'VALUES', 2, 3, 2, 3)
490505
env.assertEqual(ret, b'OK')
491506

492507
ensureSlaveSynced(red, env)
493508

494-
ret = send_and_disconnect(('AI.MODELRUN', 'm', 'INPUTS', 'a', 'b', 'OUTPUTS', 'c'), red)
509+
ret = send_and_disconnect(
510+
('AI.MODELRUN', 'm', 'INPUTS', 'a', 'b', 'OUTPUTS', 'c'), red)
495511
env.assertEqual(ret, None)
512+
513+
514+
@skip_if_no_TF
515+
def test_with_batch_and_minbatch(env):
516+
con = env.getConnection()
517+
batch_size = 2
518+
minbatch_size = 2
519+
model_name = 'model'
520+
another_model_name = 'another_model'
521+
inputvar = 'input'
522+
outputvar = 'MobilenetV2/Predictions/Reshape_1'
523+
524+
model_pb, labels, img = load_mobilenet_test_data()
525+
526+
con.execute_command('AI.MODELSET', model_name, 'TF', DEVICE,
527+
'BATCHSIZE', batch_size, 'MINBATCHSIZE', minbatch_size,
528+
'INPUTS', inputvar,
529+
'OUTPUTS', outputvar,
530+
model_pb)
531+
con.execute_command('AI.TENSORSET', 'input',
532+
'FLOAT', 1, img.shape[1], img.shape[0], img.shape[2],
533+
'BLOB', img.tobytes())
534+
535+
def run(name=model_name, output_name='output'):
536+
con.execute_command('AI.MODELRUN', name,
537+
'INPUTS', 'input', 'OUTPUTS', output_name)
538+
539+
# Running thrice since minbatchsize = 2
540+
t1 = threading.Thread(target=run)
541+
t1.start()
542+
t2 = threading.Thread(target=run)
543+
t2.start()
544+
t3 = threading.Thread(target=run)
545+
t3.start()
546+
t2.join()
547+
# This is where the problem. If we set the model again (Note that the new
548+
# MODELSET can set the model on a new key or on the same key), then the
549+
# subsequent requests fails.
550+
con.execute_command('AI.MODELSET', another_model_name, 'TF', DEVICE,
551+
'BATCHSIZE', batch_size, 'MINBATCHSIZE', minbatch_size,
552+
'INPUTS', inputvar,
553+
'OUTPUTS', outputvar,
554+
model_pb)
555+
556+
t1 = threading.Thread(target=run, args=(another_model_name, 'final1'))
557+
t1.start()
558+
t2 = threading.Thread(target=run, args=(another_model_name, 'final2'))
559+
t2.start()
560+
time.sleep(3)
561+
dtype, shape, data = con.execute_command('AI.TENSORGET', 'final1', 'BLOB')
562+
dtype_map = {b'FLOAT': np.float32}
563+
tensor = np.frombuffer(data, dtype=dtype_map[dtype]).reshape(shape)
564+
label_id = np.argmax(tensor) - 1
565+
566+
_, label = labels[str(label_id)]
567+
568+
env.assertEqual(label, 'giant_panda')

0 commit comments

Comments
 (0)