code: def varlength_roll(input, shift, axis=1, dtype=None):
      """Rolls the elements of *each row* of a tensor along an axis for
      variable steps.
  
      This is a `tf.while_loop` wrapper of :tf_main:`tf.roll <roll>`. Note the
      different definition of :attr:`shift` and :attr:`axis` here compared
      to :tf_main:`tf.roll <roll>`.
  
      Args:
          input: A tensor of shape `[batch_size, other_dims]` where
              `other_dims` can be multiple dimensions.
          shift: A 1D int tensor of shape `[batch_size]` containing
              the steps for which each row in the batch are rolled.
              Positive shifts will roll towards larger indices, while
              negative shifts will roll towards smaller indices.
          axis: A scalar int tensor > 0. The dimension that the roll
              should occur.
          dtype: Type of :attr:`input`. If `None`, inferred from
              :attr:`input` automatically.
  
      Returns:
          A Tensor of the same shape/dtype as :attr:`input`.
  
      Example:
          .. code-block:: python
  
              x = tf.constant([[0, 0, 1, 0],
                               [0, 1, 1, 1]])
              shift = [-2, -1]
  
              out = varlength_roll(x, shift)
              # out = [[1, 0, 0, 0]
              #        [1, 1, 1, 0]]
  
  
          .. code-block:: python
  
              x = tf.constant([[1, 2, 3, 4],
                               [5, 6, 7, 8]])
              shift = [1, -1]
  
              out = varlength_roll(x, shift)
              # out = [[4, 1, 2, 3]
              #        [6, 7, 8, 5]]
      """
      x = tf.convert_to_tensor(input)
      # x = input
      shift = tf.convert_to_tensor(shift)
  
      batch_size = tf.shape(x)[0]
  
      initial_index = tf.constant(0, dtype=tf.int32)
      initial_outputs_ta = tf.TensorArray(
          dtype=dtype or x.dtype, size=0, dynamic_size=True
      )
  
      def _cond(index, _):
          return tf.less(index, batch_size)
  
      def _body(index, outputs_ta):
          x_i_rolled = tf.roll(
              input=x[index], shift=shift[index], axis=axis - 1
          )
          return [index + 1, outputs_ta.write(index, x_i_rolled)]
  
      res = tf.while_loop(
          _cond, _body, [initial_index, initial_outputs_ta]
      )
  
      return res[1].stack()
  
file_path: texar/texar/tf/utils/shapes.py
imports: import numpy
  import tensorflow
  from tensorflow.python.util import nest
  from tensorflow.python.ops import rnn
  from tensorflow.python.framework import ops
label_desc: Use image node_id 13 for calling a global function with example usage: varlength_roll(input, shift, axis, dtype) and returns: res, tf, list