Repository navigation
Description
I am testing how to use CudnnLSTM, there is not a lot of documentation on this. In my own experiment, I found when use AdamOptimizer with CudnnLSTM, it always raises the following Exception.
I also found another repository using CudnnLSTM, and uploaded it here: https://github.com/boche/LM-PTB-CUDNNLSTM. It also raises the same exception.
File "ptb_word_lm.py", line 465, in main
m = PTBModel(is_training=True, config=config, debug=FLAGS.debug)
File "ptb_word_lm.py", line 254, in init
self._train_op = optimizer.apply_gradients(zip(allgrads, allvars))
File "/data/ASR1/ramons/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 409, in apply_gradients
self._create_slots(var_list)
File "/data/ASR1/ramons/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/training/adam.py", line 119, in _create_slots
self._zeros_slot(v, "m", self._name)
File "/data/ASR1/ramons/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/training/optimizer.py", line 609, in _zeros_slot
named_slots[var] = slot_creator.create_zeros_slot(var, op_name)
File "/data/ASR1/ramons/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/training/slot_creator.py", line 121, in create_zeros_slot
val = array_ops.zeros(primary.get_shape().as_list(), dtype=dtype)
File "/data/ASR1/ramons/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 782, in as_list
raise ValueError("as_list() is not defined on an unknown TensorShape.")
ValueError: as_list() is not defined on an unknown TensorShape.
For now, using GradientDescentOptimizer is okay, but it seems all other fancy optimizers all have similar problems. I have looked up this problem on stackoverflow, and found a related thread: http://stackoverflow.com/questions/40698821/tensorflow-adamoptimizer-throws-error-when-variable-has-validate-shape-false .
It seems the problem is that parameter buffer used in CudnnLSTM doesn't have fixed shape (validate_shape=False, defined in line 145 of https://github.com/boche/LM-PTB-CUDNNLSTM/blob/master/ptb_word_lm.py ) , which is required by AdamOptimizer.
Cudnn seems to be faster (less time per epoch), but if we can't use better learning algorithms with it (meaning more epochs), then the total running time may not be improved so much.
Activity
robotnc commentedon Jan 5, 2017
I also met the problem. Now I am using a method like this: Compute the params size first , and build the cudnn_lstm model with fixed shape. It can work, but looks ugly. Hope to get a better solution.
zhangyaobit commentedon Jan 12, 2017
@boche, yeah, you are right that CudnnLSTM doesn't have a shape known at static time, which is required by AdamOptimizer. As CuDNN manages the internal storage structure for weight and bias parameters, this shape is hardware-and-CuDNN dependent and only available at runtime. I think the solution by @robotnc is a right, natural thing to do, given the this requirement of a known shape by AdamOptimizer, and the fact that it could only be provided at runtime by CuDNN.
ebrevdo commentedon Apr 25, 2017
Looks like a serious issue; @zheng-xq @zhangyaobit - can we add static shape info to the parameters created by cudnn-rnn? Perhaps by exposing the cudnn library helper functions via swig? Is there a document describing how the shape varies as a fn of hardware and version?
Alternatively can we modify the Adam optimizer to not require static shapes?
ebrevdo commentedon Apr 25, 2017
Following up: looking at the slot_creator code, looks like we no longer require static shapes. Can you check to see if the error still exists in the TF nightlies?
alquraishi commentedon Apr 25, 2017
I just tried it with the latest nightly build and the problem is still there.
Note that what's needed is not really the shape, but the size of the parameter buffer, since what's being passed to CudnnLSTM is just a flat 1D vector anyway. And the size should not vary from HW iteration to another (otherwise there cannot be a 'canonical' version and the whole premise of
RNNParamsSaveablewould be moot). Given that, it seems like a very simple solution would be to just calculate the size and return that formodel.params_size(). For an LSTM it's literally just:for one layer. Multiple layers would need to modify the input_size for layers > 1 to be the size of the previous layer.
alquraishi commentedon May 11, 2017
Any updates on this?
albertz commentedon Jul 5, 2017
A related issue is #5972, to be able to optimize variables with dynamic/unknown shape.
dchaws commentedon Aug 24, 2017
@robotnc Could you elaborate more on your workaround? Thanks!
albertz commentedon Aug 25, 2017
@dchaws: When you create the variable, use the static size
params_sizelike @alquraishi describes. That's all.2 remaining items
protoget commentedon Nov 3, 2017
AdamOptimizer should work with the new Cudnn layer API in cudnn_rnn.py
We have a CL to switch tf.contrib.cudnn_rnn.CudnnLSTM to point to classes in cudnn_rnn.py instead of cudnn_rnn_ops.py, it's running a series of tests, should be submitted soon.
The following should work with the new layer API.
stefbraun commentedon Nov 3, 2017
@protoget what does "CL" stand for?
ebrevdo commentedon Nov 3, 2017
amitoengg commentedon Nov 3, 2017
TypeError: call() takes at least 5 arguments (2 given)
Also , I am having difficulty figuring out the param sizes for bidirectional Cudnns. So If my expected output for a single layer bi-directional cudnnLSTM is 2*dim and input is of size [batch_size,seq_len,dim] how should i set up the h,c matrices . Is the following method correct in terms of usage. (Basically i cannot figure out the link between num_units,input_size in CudnnLSTM and in h and c matrix.
protoget commentedon Nov 4, 2017
You might be using the old cudnn_rnn "ops" API instead of the "layers" API.
The new nightly build (tmr) should now point
tf.contrib.cudnn_rnn.CuDNNLSTMto the layers API.With the layer API, you don't need to figure out the size yourself, the layer creates and owns the variables.
amitoengg commentedon Nov 5, 2017
excellent !! Thanks it works now.
yao-zhao commentedon Nov 30, 2017
will this change be included in tf-1.5? any examples for multi gpu usage of the new CudnnLSTM?
protoget commentedon Nov 30, 2017
yes it'd be included in 1.5.
You can check
cudnn_rnn_test.py.ptb_word_lm.pyis updated but not released to the public yet.amitoengg commentedon Nov 30, 2017
Thanks again. Could you please advise on how to use CudnnLSTM trained models on cpu's at test time ? i,e how to convert Bidirectional CudnnLSTM to say Bidirectional BasicLSTM that can be used on cpus.
alquraishi commentedon Jan 15, 2018
I'm no longer seeing problems with Adam, but both RMSprop and Adagrad don't appear to work with cudnnLSTM. This is the error I'm getting with RMSprop:
Shape of a new variable (geomnet/geomnet/layer0/fw/cudnn_lstm/opaque_kernel/RMSProp/) must be fully defined, but instead was <unknown>.And Adagrad:
ValueError: If initializer is a constant, do not specify shape.I tried steepest, momentum, adam, and adadelta and they all work.
protoget commentedon Jan 23, 2018
Re @alquraishi I have a fix pending review and hopefully submit in 2 dasy.
xingjinglu commentedon Jan 24, 2018
@protoget It does need to give state_h and state_c variable for CudnnLSTM in TF 1.5?
Address Adagrad/RMSProp incompatibility with CudnnRNN