Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow OOM after freeze graph

Tags:

tensorflow

I'm running a seq2seq model with tf, the inference program runs well when loading parameters from checkpoint file using tf.train.Saver. But after exporting the graph with freeze_graph.py (using tf.framework.graph_util.convert_variables_to_constants()), and import with tf.import_graph_def in the inference program, it got OOM problem.

Here is a part of error log:

W tensorflow/core/common_runtime/bfc_allocator.cc:274] ****************************************************************************************************
W tensorflow/core/common_runtime/bfc_allocator.cc:275] Ran out of memory trying to allocate 4.0KiB.  See logs for memory state.
W tensorflow/core/framework/op_kernel.cc:983] Internal: Dst tensor is not initialized.
E tensorflow/core/common_runtime/executor.cc:594] Executor failed to create kernel. Internal: Dst tensor is not initialized.
     [[Node: embedding_attention_seq2seq/embedding_attention_decoder/attention_decoder/AttnV_0 = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [1024] values: -0.016628871 -0.2054652 -0.045054652...>, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
Traceback (most recent call last):
  File "inference.py", line 88, in console_main
    result = list(inference(source_sentence))
  File "inference.py", line 54, in inference
    for sequence in result:
  File "/data/experiment/decoder.py", line 115, in search_best_sequence
    State.batch_predict(self.session, self.model, self.context, beam)
  File "/data/experiment/decoder.py", line 82, in batch_predict
    state_list[0].depth)
  File "/data/experiment/seq2seq_model.py", line 452, in batch_feed_decoder
    log_softmax, attns, state = session.run(output_fetch, input_feed)
  File "/home/.conda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run
    run_metadata_ptr)
  File "/home/.conda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 966, in _run
    feed_dict_string, options, run_metadata)
  File "/home/.conda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1016, in _do_run
    target_list, options, run_metadata)
  File "/home/.conda/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1036, in _do_call
    raise type(e)(node_def, op, message)
InternalError: Dst tensor is not initialized.
     [[Node: embedding_attention_seq2seq/embedding_attention_decoder/attention_decoder/AttnV_0 = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [1024] values: -0.016628871 -0.2054652 -0.045054652...>, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

Caused by op u'embedding_attention_seq2seq/embedding_attention_decoder/attention_decoder/AttnV_0', defined at:
  File "inference.py", line 169, in <module>
    tf.app.run()
  File "/home/.conda/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 44, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "inference.py", line 165, in main
    console_main(session)
  File "inference.py", line 66, in console_main
    model = create_model(session, False)
  File "/data/experiment/model.py", line 145, in create_model
    tensor_name_pickle=tensor_name_pickle)
  File "/data/experiment/seq2seq_model.py", line 106, in __init__
    tf.import_graph_def(graph_def, name="")
  File "/home/.conda/lib/python2.7/site-packages/tensorflow/python/framework/importer.py", line 287, in import_graph_def
    op_def=op_def)
  File "/home/.conda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2395, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/.conda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1264, in __init__
    self._traceback = _extract_stack()

InternalError (see above for traceback): Dst tensor is not initialized.
     [[Node: embedding_attention_seq2seq/embedding_attention_decoder/attention_decoder/AttnV_0 = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [1024] values: -0.016628871 -0.2054652 -0.045054652...>, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

I thought it might cause by the memory issue of tf.Constant. Does someone have experience with this problem?

like image 234
wodesuck Avatar asked Jun 25 '26 07:06

wodesuck


1 Answers

I had the same issue but when trying to load and run the inference from a C++ application using the C API. After a lot of twiddling and testing it appeared the culprit was the frozen graph and freeze_graph.py itself. It's probably a bug of some kind. There are actually multiple issue reports on github's TF repo, but they were just closed due to lack of activity, e.g. here and here. I guess apparent bugs of model freezing aren't of any priority.

In my case the model .pb file was around 500mb and it took around 10Gb of RAM while running a session. Not only did it occupy an insane amount of RAM, it was actually orders of magnitudes slower that way.

When I switched to loading just a SavedModel directory everything went to normal. I'm not sure how to achieve that in python, but for C code I replaced a TF_GraphImportGraphDef() call with TF_LoadSessionFromSavedModel().

I used TF v1.14.0. The library is built with Bazel by me, not the stock version. I could provide some details here and there if anybody was interested. Just not sure where to start, I had many trials and errors.

like image 128
Roman Kruglov Avatar answered Jun 26 '26 20:06

Roman Kruglov