使用TPU时使用CrossShardOptimizer时出错

时间:2018-10-23 08:07:14

标签: tensorflow google-cloud-tpu

在TPU上运行代码时,我尝试使用CrossShardOptimizer。我在这里定义优化器

optimizer = tf.contrib.tpu.CrossShardOptimizer(tf.train.AdamOptimizer(1.0)).minimize(loss) 

这是我用来运行会话的代码

with tf.Session(tpu_address, graph=graph) as session:

    for step in range(1, num_steps):

        batch_data, batch_labels = generate_batch(
          batch_size, num_inputs)
        feed_dict = {train_dataset : batch_data, train_labels : batch_labels}
        _, l = session.run([optimizer, loss], feed_dict=feed_dict) 

这是我得到的错误

Initialized
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1333     try:
-> 1334       return fn(*args)
   1335     except errors.OpError as e:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1318       return self._call_tf_sessionrun(
-> 1319           options, feed_dict, fetch_list, target_list, run_metadata)
   1320 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1406         self._session, options, feed_dict, fetch_list, target_list,
-> 1407         run_metadata)
   1408 

InvalidArgumentError: Input 1 to CrossReplicaSum operator must be a compile-time constant.

XLA compilation requires that operator arguments that represent shapes or dimensions be evaluated to concrete values at compile time. This error means that a shape or dimension argument could not be evaluated at compile time, usually because the value of the argument depends on a parameter to the computation, on a variable, or on a stateful operation such as a random number generator.
     [[{{node CrossReplicaSum_1}} = CrossReplicaSum[T=DT_FLOAT, _device="/job:tpu_worker/replica:0/task:0/device:TPU:0"](CrossReplicaSum_1/input, CrossReplicaSum_1/group_assignment)]]
     [[{{node CrossReplicaSum_1}} = CrossReplicaSum[T=DT_FLOAT, _device="/job:tpu_worker/replica:0/task:0/device:TPU:0"](CrossReplicaSum_1/input, CrossReplicaSum_1/group_assignment)]]
     [[{{node CrossReplicaSum_G12}} = _Recv[client_terminated=false, recv_device="/job:tpu_worker/replica:0/task:0/device:CPU:0", send_device="/job:tpu_worker/replica:0/task:0/device:TPU:0", send_device_incarnation=2958871627099111662, tensor_name="edge_181_CrossReplicaSum", tensor_type=DT_FLOAT, _device="/job:tpu_worker/replica:0/task:0/device:CPU:0"]()]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-21-8c0ef10a68cf> in <module>()
     24         #print(batch_data)
     25         feed_dict = {train_dataset : batch_data, train_labels : batch_labels}
---> 26         _, l = session.run([optimizer, loss], feed_dict=feed_dict)
     27 
     28         if l == l:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    927     try:
    928       result = self._run(None, fetches, feed_dict, options_ptr,
--> 929                          run_metadata_ptr)
    930       if run_metadata:
    931         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1150     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1151       results = self._do_run(handle, final_targets, final_fetches,
-> 1152                              feed_dict_tensor, options, run_metadata)
   1153     else:
   1154       results = []

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1326     if handle is None:
   1327       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1328                            run_metadata)
   1329     else:
   1330       return self._do_call(_prun_fn, handle, feeds, fetches)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1346           pass
   1347       message = error_interpolation.interpolate(message, self._graph)
-> 1348       raise type(e)(node_def, op, message)
   1349 
   1350   def _extend_graph(self):

InvalidArgumentError: Input 1 to CrossReplicaSum operator must be a compile-time constant.

XLA compilation requires that operator arguments that represent shapes or dimensions be evaluated to concrete values at compile time. This error means that a shape or dimension argument could not be evaluated at compile time, usually because the value of the argument depends on a parameter to the computation, on a variable, or on a stateful operation such as a random number generator.
     [[node CrossReplicaSum_1 (defined at /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/tpu/ops/gen_tpu_ops.py:322)  = CrossReplicaSum[T=DT_FLOAT, _device="/job:tpu_worker/replica:0/task:0/device:TPU:0"](CrossReplicaSum_1/input, CrossReplicaSum_1/group_assignment)]]
     [[node CrossReplicaSum_1 (defined at /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/tpu/ops/gen_tpu_ops.py:322)  = CrossReplicaSum[T=DT_FLOAT, _device="/job:tpu_worker/replica:0/task:0/device:TPU:0"](CrossReplicaSum_1/input, CrossReplicaSum_1/group_assignment)]]
     [[{{node CrossReplicaSum_G12}} = _Recv[client_terminated=false, recv_device="/job:tpu_worker/replica:0/task:0/device:CPU:0", send_device="/job:tpu_worker/replica:0/task:0/device:TPU:0", send_device_incarnation=2958871627099111662, tensor_name="edge_181_CrossReplicaSum", tensor_type=DT_FLOAT, _device="/job:tpu_worker/replica:0/task:0/device:CPU:0"]()]]

Caused by op 'CrossReplicaSum_1', defined at:
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/usr/local/lib/python3.6/dist-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/usr/local/lib/python3.6/dist-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
    if self.run_code(code, result):
  File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-20-ecaf96c21b22>", line 42, in <module>
    optimizer = tf.contrib.tpu.CrossShardOptimizer(tf.train.AdamOptimizer(1.0)).minimize(loss)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/optimizer.py", line 410, in minimize
    name=name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/tpu/python/tpu/tpu_optimizer.py", line 170, in apply_gradients
    grad, self._group_assignment), var))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/tpu/python/ops/tpu_ops.py", line 113, in cross_replica_sum
    return gen_tpu_ops.cross_replica_sum(x, group_assignment, name=name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/contrib/tpu/ops/gen_tpu_ops.py", line 322, in cross_replica_sum
    name=name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 3274, in create_op
    op_def=op_def)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 1770, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): Input 1 to CrossReplicaSum operator must be a compile-time constant.

XLA compilation requires that operator arguments that represent shapes or dimensions be evaluated to concrete values at compile time. This error means that a shape or dimension argument could not be evaluated at compile time, usually because the value of the argument depends on a parameter to the computation, on a variable, or on a stateful operation such as a random number generator.
     [[node CrossReplicaSum_1 (defined at /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/tpu/ops/gen_tpu_ops.py:322)  = CrossReplicaSum[T=DT_FLOAT, _device="/job:tpu_worker/replica:0/task:0/device:TPU:0"](CrossReplicaSum_1/input, CrossReplicaSum_1/group_assignment)]]
     [[node CrossReplicaSum_1 (defined at /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/tpu/ops/gen_tpu_ops.py:322)  = CrossReplicaSum[T=DT_FLOAT, _device="/job:tpu_worker/replica:0/task:0/device:TPU:0"](CrossReplicaSum_1/input, CrossReplicaSum_1/group_assignment)]]
     [[{{node CrossReplicaSum_G12}} = _Recv[client_terminated=false, recv_device="/job:tpu_worker/replica:0/task:0/device:CPU:0", send_device="/job:tpu_worker/replica:0/task:0/device:TPU:0", send_device_incarnation=2958871627099111662, tensor_name="edge_181_CrossReplicaSum", tensor_type=DT_FLOAT, _device="/job:tpu_worker/replica:0/task:0/device:CPU:0"]()]]

这里是我Colab笔记本中完整代码的链接。一切都已设置好,包括自动下载我的数据。

https://drive.google.com/open?id=1dgYblB6zg9PmIulaaVo9Lvhd5viuPtIp

在其余部分在TPU上进行评估时,它似乎可能试图在CPU上进行评估,但是我将整个图形的目标设置为TPU地址,所以我不确定出什么问题。

0 个答案:

没有答案