docstr: Reorders cache for generation. BART-inspired but we need to take care of the extra dimension for docs
code: def _reorder_cache(past, beam_idx):
      """Reorders cache for generation. BART-inspired but we need to take care of the extra dimension for docs"""
  
      def _reorder_stacked(hidden_states, new_order):
          n_docs = hidden_states.shape[0] // new_order.shape[0]
          hidden_states = hidden_states.view(
              -1, n_docs, *hidden_states.shape[1:]
          )
          hidden_states = hidden_states.index_select(0, new_order)
          result = hidden_states.view(-1, *hidden_states.shape[2:])
          return result
  
      reordered_past = ()
      for layer_past in past:
          # get the correct batch idx from decoder layer's batch dim for cross and self-attn
          reordered_past += (
              tuple(
                  _reorder_stacked(past_state, beam_idx)
                  for past_state in layer_past
              ),
          )
  
      return reordered_past
  
imports: from dataclasses import dataclass
  from typing import Callable, List, Optional, Tuple, Union
  import torch
  from torch import nn
  from ...configuration_utils import PretrainedConfig
  from ...generation_beam_search import BeamSearchScorer
  from ...generation_logits_process import LogitsProcessorList
  from ...generation_stopping_criteria import StoppingCriteriaList
  from ...modeling_outputs import ModelOutput
  from ...modeling_utils import PreTrainedModel
  from ...utils import add_start_docstrings_to_model_forward, logging, replace_return_docstrings
  from .configuration_rag import RagConfig
  from .retrieval_rag import RagRetriever
label_desc: Use image node_id 8 for calling the RagTokenForGeneration obj's underlying member method code with example usage: obj._reorder_cache(past, beam_idx) and returns: reordered_past, result