message: Modify dynamic_rnn to use max_seq_length for its while_loop.
  This is especially important with XLA compilation, which will usually have
  a maximum time dimension on the input that is (sometimes much larger than)
  max(sequence_length).  In these cases, a lot of compute can be saved.
diff: @@ -35,7 +35,6 @@ from tensorflow.python.framework import tensor_shape
  from tensorflow.python.framework import tensor_util
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import control_flow_ops
  -from tensorflow.python.ops import control_flow_util
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import rnn_cell_impl
  from tensorflow.python.ops import tensor_array_ops
  @@ -726,6 +725,8 @@ def _dynamic_rnn_loop(cell,
  if sequence_length is not None:
  min_sequence_length = math_ops.reduce_min(sequence_length)
  max_sequence_length = math_ops.reduce_max(sequence_length)
  + else:
  + max_sequence_length = time_steps
  time = array_ops.constant(0, dtype=dtypes.int32, name="time")
  @@ -810,28 +811,18 @@ def _dynamic_rnn_loop(cell,
  return (time + 1, output_ta_t, new_state)
  - # TODO(pbar) `loop_bound` can be reduced to `max_sequence_length` once
  - # TensorArray shape inference is working. When sequence lengths are highly
  - # variable, this will reduce the performance overheads of padding to a fixed
  - # maximum length.
  - loop_bound = time_steps
  -
  - # This is a workaround since we cannot currently use maximum_iterations if
  - # time_steps is defined inside control flow, see the comment in
  - # control_flow_ops.py.
  - if (context.in_eager_mode() or
  - not (control_flow_util.IsInWhileLoop(time_steps.op) or
  - control_flow_util.IsInCond(time_steps.op))):
  - maximum_iterations = time_steps
  + if in_graph_mode:
  + loop_bound = max_sequence_length
  else:
  - maximum_iterations = None
  + # Using max_sequence_length isn't currently supported in the Eager branch.
  + loop_bound = time_steps
  _, output_final_ta, final_state = control_flow_ops.while_loop(
  cond=lambda time, *_: time < loop_bound,
  body=_time_step,
  loop_vars=(time, output_ta, state),
  parallel_iterations=parallel_iterations,
  - maximum_iterations=maximum_iterations,
  + maximum_iterations=time_steps,
  swap_memory=swap_memory)
  # Unpack final output if not using output tuples.
  