tensor2tensor icon indicating copy to clipboard operation
tensor2tensor copied to clipboard

How to export saved mode with GPU support ?

Open huache opened this issue 8 years ago • 6 comments

Hi, everyone, I trained a transformer model, and exported it with --export_saved_model. I got the same error of #457 . So I follow the suggestion from that issue, add --locally_shard_to_cpu --worker_gpu 0 parameters, and exported the model successfully.

However, I found the exported model seemed to use CPU for calculation only.

I looked into the source code of tensor2tensor, I found this in utils/devices.py:

def _gpu_order(num_gpus):
  if FLAGS.gpu_order:
    ret = [int(s) for s in FLAGS.gpu_order.split(" ")]
    if len(ret) == num_gpus:
      return ret
  return list(range(num_gpus))

......
    datashard_devices = ["gpu:%d" % d for d in _gpu_order(FLAGS.worker_gpu)]
    if FLAGS.locally_shard_to_cpu or FLAGS.worker_gpu < 1:
      datashard_devices += ["cpu:0"]
......

As I understand it, if add --locally_shard_to_cpu --worker_gpu 0 parameters, the datashard_devices will be ["cpu:0"], which means to use CPU only.

But, if the two parameters was not added, the model can not be exported.

I have tried use --locally_shard_to_cpu --worker_gpu 1, I got another error:

Traceback (most recent call last):
  File "/home/huache/anaconda3/envs/t2t/bin/t2t-trainer", line 108, in <module>
    tf.app.run()
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "/home/huache/anaconda3/envs/t2t/bin/t2t-trainer", line 104, in main
    run_experiment()
  File "/home/huache/anaconda3/envs/t2t/bin/t2t-trainer", line 94, in run_experiment
    schedule=FLAGS.schedule)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensor2tensor/utils/trainer_utils.py", line 385, in run
    hparams=hparams)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/learn_runner.py", line 218, in run
    return _execute_schedule(experiment, schedule)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/learn_runner.py", line 46, in _execute_schedule
    return task()
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/experiment.py", line 641, in train_and_evaluate
    export_results = self._maybe_export(eval_result)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/experiment.py", line 744, in _maybe_export
    eval_result=eval_result))
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/export_strategy.py", line 87, in export
    return self.export_fn(estimator, export_path, **kwargs)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/utils/saved_model_export_utils.py", line 442, in export_fn
    checkpoint_path=checkpoint_path)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 511, in export_savedmodel
    config=self.config)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 694, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensor2tensor/utils/model_builder.py", line 334, in wrapping_model_fn
    return model_fn(model, features, mode, hparams, **kwargs)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensor2tensor/utils/model_builder.py", line 163, in model_fn
    max_idx=len(hparams.problems) - 1)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensor2tensor/utils/input_fn_builder.py", line 182, in cond_on_index
    return fn(cur_idx)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensor2tensor/utils/model_builder.py", line 119, in nth_model
    decode_length=decode_hp.extra_length)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensor2tensor/utils/t2t_model.py", line 236, in infer
    features, decode_length, beam_size, top_beams, alpha)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensor2tensor/models/transformer.py", line 201, in _beam_decode
    beam_size, top_beams, alpha)
  File "/home/huache/anaconda3/envs/t2t/lib/python3.6/site-packages/tensor2tensor/models/transformer.py", line 230, in _fast_decode
    raise NotImplementedError("Fast decoding only supports a single shard.")
NotImplementedError: Fast decoding only supports a single shard.

The model exported with --locally_shard_to_cpu --worker_gpu 0 is terribly slow, I really need it can use GPU . Does anybody could tell me how to solve this ? Thanks in advance.

huache avatar Jan 06 '18 10:01 huache

Hi huache,

For this issue, you may refer to https://github.com/tensorflow/tensorflow/commit/bf05a2eef97863fc78778bcde5987f93af8a7598 for the detailed explanation and solution. After modifying tf_session.Session() in estimator.py to give it an argument config=self._session_config, this issue can be resolved.

aldcool avatar Jan 08 '18 04:01 aldcool

Exporting will be reworked a bit in an upcoming release. The devices will be empty on most ops and so if the model server is built with GPU support, then I'm hoping it will automatically put ops on GPU. If not, we'll find another way.

rsepassi avatar Jan 08 '18 18:01 rsepassi

@aldcool @rsepassi Thank you very much for your replies.

I modified the source file of tensorflow as aldcool suggested, the model was export successfully, however, it could not load by tensorflow serving, this error was reported:

2018-01-09 12:55:19.862364: E tensorflow_serving/util/retrier.cc:38] Loading servable: {name: default version: 1515473596} failed: Invalid argument: Cannot assign a device for operation 'save_1/ShardedFilename_1': Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available.
Colocation Debug Info:
Colocation group had the following types and devices: 
Identity: CPU 
ShardedFilename: CPU 
	 [[Node: save_1/ShardedFilename_1 = ShardedFilename[_output_shapes=[[]], _device="/device:GPU:0"](save_1/StringJoin, save_1/ShardedFilename_1/shard, save_1/num_shards)]]

I also modified the source code of tensorflow in tensorflow serving project, and then recompile the tensorflow serving. The error still exists.

As rsepassi said, may be I need to upgrade tenosrflow to v1.5, I will try it later.

huache avatar Jan 09 '18 05:01 huache

See the new serving README.

rsepassi avatar Jan 18 '18 18:01 rsepassi

@rsepassi

Hi, I tried t2t-exporter with tensorflow-1.5.0-rc1, however, I got this error from tensorflow serving:

[libprotobuf ERROR external/protobuf_archive/src/google/protobuf/text_format.cc:288] Error parsing text-format tensorflow.SavedModel: 249:15: Unknown enumeration value of "DT_UINT32" for field "type".
2018-01-19 13:26:01.497149: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:284] Loading SavedModel: fail. Took 522 microseconds.
2018-01-19 13:26:01.497160: E tensorflow_serving/util/retrier.cc:38] Loading servable: {name: default version: 1516339226} failed: Data loss: Can't parse /data/tensor2tensor/en_cn/en_cn_2000W_new/output_base_single_gpu/export/Servo/1516339226/saved_model.pbtxt as text proto

Does that mean I need to compile a new version of tensorflow serving ?
The tensorflow serving repo only has r1.4 branch now.

huache avatar Jan 19 '18 05:01 huache

Everything works fine with Tensorflow r1.5, Thank you guys !

huache avatar Mar 06 '18 02:03 huache