MCPcopy Create free account
hub / github.com/OpenMOSS/MOSS / _reorder_cache

Method _reorder_cache

models/modeling_moss.py:722–733  ·  view source on GitHub ↗

This function is used to re-order the `past_key_values` cache if [`~PretrainedModel.beam_search`] or [`~PretrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct beam_idx at every generation step.

(
        past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor
    )

Source from the content-addressed store, hash-verified

720
721 @staticmethod
722 def _reorder_cache(
723 past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor
724 ) -> Tuple[Tuple[torch.Tensor]]:
725 """
726 This function is used to re-order the `past_key_values` cache if [`~PretrainedModel.beam_search`] or
727 [`~PretrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct
728 beam_idx at every generation step.
729 """
730 return tuple(
731 tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past)
732 for layer_past in past_key_values
733 )
734
735 def quantize(self, wbits, groupsize):
736 from .quantization import quantize_with_gptq

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected