code: def _sequential_index(samples):
      """
      For a tensor ``samples`` whose time dimension is -2 and state dimension
      is -1, compute Markov paths by sequential indexing.
  
      For example, for ``samples`` with 3 states and time duration 5::
  
          tensor([[0, 1, 1],
                  [1, 0, 2],
                  [2, 1, 0],
                  [0, 2, 1],
                  [1, 1, 0]])
  
      computed paths are::
  
          tensor([[0, 1, 1],
                  [1, 0, 0],
                  [1, 2, 2],
                  [2, 1, 1],
                  [0, 1, 1]])
  
          # path for a 0th state
          #
          # 0 1 1
          # |
          # 1 0 2
          #  \
          # 2 1 0
          #   |
          # 0 2 1
          #    \
          # 1 1 0
          #
          # paths for 1st and 2nd states
          #
          # 0 1 1
          #   |/
          # 1 0 2
          #  /
          # 2 1 0
          #  \
          #    \
          # 0 2 1
          #    /
          # 1 1 0
      """
      # new Markov time dimension at -2
      samples = samples.unsqueeze(-2)
      batch_shape = samples.shape[:-3]
      state_dim = samples.size(-1)
      duration = samples.size(-3)
      while samples.size(-3) > 1:
          time = samples.size(-3)
          even_time = time // 2 * 2
          even_part = samples[..., :even_time, :, :]
          x_y = even_part.reshape(
              batch_shape + (even_time // 2, 2, -1, state_dim)
          )
          x, y = x_y.unbind(-3)
          contracted = _markov_index(x, y)
          if time > even_time:
              padded = F.pad(
                  input=samples[..., -1:, :, :],
                  pad=(0, 0, 0, contracted.size(-2) // 2),
              )
              contracted = torch.cat((contracted, padded), dim=-3)
          samples = contracted
      return samples.squeeze(-3)[..., :duration, :]
  
imports: import torch
  import torch.nn.functional
  from pyro.ops.gamma_gaussian import GammaGaussian, gamma_and_mvn_to_gamma_gaussian, gamma_gaussian_tensordot, matrix_and_mvn_to_gamma_gaussian
  from pyro.ops.gaussian import Gaussian, gaussian_tensordot, matrix_and_mvn_to_gaussian, mvn_to_gaussian, sequential_gaussian_filter_sample, sequential_gaussian_tensordot
  from pyro.ops.indexing import Vindex
  from pyro.ops.special import safe_log
  from pyro.ops.tensor_utils import cholesky_solve, safe_cholesky
  from .None import constraints
  from .torch import Categorical, Gamma, Independent, MultivariateNormal
  from .torch_distribution import TorchDistribution
  from .util import broadcast_shape, torch_jit_script_if_tracing
label_desc: Use image node_id 5 for calling a global function with example usage: _sequential_index(samples) and returns: samples