gpt4 book ai didi

python - 尝试对具有屏蔽输入的LSTM Seq2Seq进行推理时,CUDNN_STATUS_BAD_PARAM

转载 作者:行者123 更新时间:2023-12-03 09:42:14 24 4
gpt4 key购买 nike

我在tensorflow 2.0上使用keras图层来构建一个简单的基于LSTM的基于LSTM的Seq2Seq模型来生成文本

versions I'm using: Python 3.6.9, Tensorflow 2.0.0, CUDA 10.0, CUDNN 7.6.1, Nvidia driver version 410.78.



我知道 criteria needed by TF to delegate to CUDNNLstm when a GPU is present(我 确实有GPU ,并且我的模型/数据符合所有这些条件)。

培训进行得很顺利(带有警告消息,请参阅本文结尾),我可以验证是否正在使用CUDNNLstm。

但是,当我尝试在推断时间调用 encoder_model.predict(input_sequence) 时,我收到以下错误消息:
UnknownError:  [_Derived_]  CUDNN_STATUS_BAD_PARAM
in tensorflow/stream_executor/cuda/cuda_dnn.cc(1424): 'cudnnSetRNNDataDescriptor( data_desc.get(), data_type, layout, max_seq_length, batch_size, data_size, seq_lengths_array, (void*)&padding_fill)'
[[{{node cond/then/_0/CudnnRNNV3}}]]
[[lstm/StatefulPartitionedCall]] [Op:__inference_keras_scratch_graph_91878]

Function call stack:
keras_scratch_graph -> keras_scratch_graph -> keras_scratch_graph

这是训练代码:(source_sequencestarget_sequences都是右填充序列,而嵌入矩阵是预训练的Glove嵌入)
# Define an input sequence and process it.
encoder_inputs = tf.keras.layers.Input(shape=(24,))
encoder_embedding_layer = tf.keras.layers.Embedding(
VOCABULARY_SIZE_1,
EMBEDDING_DIMS,
embeddings_initializer=initializers.Constant(encoder_embedding_matrix),
mask_zero=True)
encoder_embedding = encoder_embedding_layer(encoder_inputs)

_, state_h, state_c = tf.keras.layers.LSTM(
EMBEDDING_DIMS,
implementation=1,
return_state=True)(encoder_embedding)

encoder_states = [state_h, state_c]

decoder_inputs = tf.keras.layers.Input(shape=(24,))
decoder_embedding_layer = tf.keras.layers.Embedding(
VOCABULARY_SIZE_2,
EMBEDDING_DIMS,
embeddings_initializer=initializers.Constant(decoder_embedding_matrix),
mask_zero=True)
decoder_embedding = decoder_embedding_layer(decoder_inputs)

decoder_lstm = tf.keras.layers.LSTM(
EMBEDDING_DIMS,
return_sequences=True,
return_state=True,
implementation=1)

decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=encoder_states)

decoder_dense = tf.keras.layers.Dense(VOCABULARY_SIZE_TITLE, activation='softmax')

output = decoder_dense(decoder_outputs)

model = tf.keras.models.Model([encoder_inputs, decoder_inputs], output)

model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')
model.summary()

model.fit([source_sequences, target_sequences], decoder_target_data,
batch_size=32,
epochs=10,
validation_split=0.0,
verbose=2)

enter image description here

这些是推断模型:
encoder_model = tf.keras.models.Model(encoder_inputs, encoder_states)

decoder_state_input_h = tf.keras.layers.Input(shape=(input_dimension ,))
decoder_state_input_c = tf.keras.layers.Input(shape=(input_dimension ,))

decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]

decoder_outputs, state_h, state_c = decoder_lstm_layer(
decoder_embedding_layer , initial_state=decoder_states_inputs)

decoder_states = [state_h, state_c]

decoder_outputs = output_layer(decoder_outputs)
decoder_model = tf.keras.models.Model(
[decoder_inputs] + decoder_states_inputs,
[decoder_outputs] + decoder_states)

当我在predict()上调用encoder_model时,得到CUDNN_STATUS_BAD_PARAM

推断代码(触发错误的地方)
# build the initial state with a right-padded input sequence
#### CUDNN_STATUS_BAD_PARAM is TRIGGERED ON THIS LINE!!! ######## <<<<<<<<<
state = encoder_model.predict(masked_input_sequence)

empty_target_sequence = np.zeros((1,1))
# this signals the Start of sequence
empty_target_sequence[0,0] = titles_word_index[sos_token]

decoder_outputs, h, c = decoder_model.predict([empty_target_sequence] + state)

我尝试过的事情
  • 显式创建掩码(encoder_embedding_layer.compute_mask()),并在每次调用LSTM层时将其作为参数添加,例如:
    encoder_embedding = encoder_embedding_layer(encoder_inputs)

    encoder_mask = encoder_embedding_layer.compute_mask(encoder_inputs)

    _, state_h, state_c = tf.keras.layers.LSTM(
    EMBEDDING_DIMS,
    return_state=True)(encoder_embedding,mask=encoder_mask)
  • 不使用初始化程序进行嵌入层,以查看问题是否存在


  • P.S .: 强制在CPU上进行培训会使错误消失,但我需要在GPU上进行培训,否则需要一段时间才能完成。

    附言:这似乎是我遇到的相同错误:Masking LSTM: OP_REQUIRES failed at cudnn_rnn_ops.cc:1498 : Unknown: CUDNN_STATUS_BAD_PARAM

    P.S .: ,当我在supports_maskingmodelencoder_model上调用decoder_model方法时,由于某种原因,它们都返回False

    P.S .: 如我所说,培训已完成,没有(明显)错误,但是如果我在命令行上查看Jupyter输出日志,则可以在培训
    期间看到以下 警告消息:
    2019-11-16 19:48:20.144265: W 
    tensorflow/core/grappler/optimizers/implementation_selector.cc:310] Skipping optimization due to error while loading function libraries:
    Invalid argument: Functions '__inference___backward_cudnn_lstm_with_fallback_47598_49057' and
    '__inference___backward_cudnn_lstm_with_fallback_47598_49057_specialized_for_StatefulPartitionedCall_1_at___inference_distributed_function_52868'
    both implement 'lstm_d41d5ccb-14be-4a74-b5e8-cc4f63c5bb02' but their signatures do not match.

    最佳答案

    您应该使用cudnn7.4引用此web

    关于python - 尝试对具有屏蔽输入的LSTM Seq2Seq进行推理时,CUDNN_STATUS_BAD_PARAM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58895694/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com