Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cuDNN tests #8189

Merged
merged 1 commit into from
Oct 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions tests/keras/layers/cudnn_recurrent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,46 +122,45 @@ def test_cudnn_rnn_canonical_to_params_gru():


@keras_test
@pytest.mark.parametrize('rnn_type', ['lstm', 'gru'], ids=['LSTM', 'GRU'])
@pytest.mark.skipif((keras.backend.backend() != 'tensorflow'),
reason='Requires TensorFlow backend')
@pytest.mark.skipif(not keras.backend.tensorflow_backend._get_available_gpus(),
reason='Requires GPU')
def test_cudnn_rnn_timing():
def test_cudnn_rnn_timing(rnn_type):
input_size = 1000
timesteps = 60
units = 256
num_samples = 10000

times = []
for rnn_type in ['lstm', 'gru']:
for use_cudnn in [True, False]:
start_time = time.time()
inputs = keras.layers.Input(shape=(None, input_size))
if use_cudnn:
if rnn_type == 'lstm':
layer = keras.layers.CuDNNLSTM(units)
else:
layer = keras.layers.CuDNNGRU(units)
for use_cudnn in [True, False]:
start_time = time.time()
inputs = keras.layers.Input(shape=(None, input_size))
if use_cudnn:
if rnn_type == 'lstm':
layer = keras.layers.CuDNNLSTM(units)
else:
if rnn_type == 'lstm':
layer = keras.layers.LSTM(units)
else:
layer = keras.layers.GRU(units)
outputs = layer(inputs)
layer = keras.layers.CuDNNGRU(units)
else:
if rnn_type == 'lstm':
layer = keras.layers.LSTM(units)
else:
layer = keras.layers.GRU(units)
outputs = layer(inputs)

model = keras.models.Model(inputs, outputs)
model.compile('sgd', 'mse')
model = keras.models.Model(inputs, outputs)
model.compile('sgd', 'mse')

x = np.random.random((num_samples, timesteps, input_size))
y = np.random.random((num_samples, units))
model.fit(x, y, epochs=4, batch_size=32)
x = np.random.random((num_samples, timesteps, input_size))
y = np.random.random((num_samples, units))
model.fit(x, y, epochs=4, batch_size=32)

times.append(time.time() - start_time)
times.append(time.time() - start_time)

speedup = times[1] / times[0]
print(rnn_type, 'speedup', speedup)
assert speedup > 3
keras.backend.clear_session()
speedup = times[1] / times[0]
print(rnn_type, 'speedup', speedup)
assert speedup > 3


@keras_test
Expand Down