code: def get_step_function(
      transition_matrix: torch.Tensor, with_timestep: bool = False
  ) -> Union[StepFunctionTypeNoTimestep, StepFunctionTypeWithTimestep]:
      def _step_function(
          last_predictions: torch.Tensor, state: Dict[str, torch.Tensor]
      ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
          log_probs_list = []
          for last_token in last_predictions:
              log_probs = torch.log(
                  transition_matrix[last_token.item()]
              )
              log_probs_list.append(log_probs)
  
          return torch.stack(log_probs_list), state
  
      if not with_timestep:
          return _step_function
  
      def _step_function_with_timestep(
          last_predictions: torch.Tensor,
          state: Dict[str, torch.Tensor],
          timestep: int,
      ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
          return _step_function(last_predictions, state)
  
      return _step_function_with_timestep
  
imports: from typing import Dict, Tuple, Union
  import numpy
  import pytest
  import torch
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.nn.beam_search import MultinomialSampler, BeamSearch, TopKSampler, TopPSampler, GumbelSampler, LengthNormalizedSequenceLogProbabilityScorer, RepeatedNGramBlockingConstraint, StepFunctionTypeWithTimestep, StepFunctionTypeNoTimestep
  from allennlp.common.params import Params
  from allennlp.nn.util import min_value_of_dtype
label_desc: Use image node_id 1 for calling a global function with example usage: get_step_function(transition_matrix, with_timestep) and returns: _step_function_with_timestep, torch,  state, _step_function, _step_function