code: def _no_beam_search_generate(
      decoder: PromptBartDecoder,
      state,
      tokens=None,
      max_length=20,
      max_len_a=0.0,
      bos_token_id=None,
      eos_token_id=None,
      repetition_penalty=1.0,
      length_penalty=1.0,
      pad_token_id=0,
      restricter=None,
  ):
      device = get_model_device(decoder)
      if tokens is None:
          if bos_token_id is None:
              raise RuntimeError(
                  "You have to specify either `tokens` or `bos_token_id`."
              )
          batch_size = state.num_samples()
          if batch_size is None:
              raise RuntimeError(
                  "Cannot infer the number of samples from `state`."
              )
          tokens = torch.full(
              [batch_size, 1], fill_value=bos_token_id, dtype=torch.long
          ).to(device)
      batch_size = tokens.size(0)
      if state.num_samples:
          assert (
              state.num_samples() == batch_size
          ), "The number of samples in `tokens` and `state` should match."
  
      if eos_token_id is None:
          _eos_token_id = -1
      else:
          _eos_token_id = eos_token_id
      scores = decoder.decode(
          tokens=tokens, state=state
      )  # update state
      if restricter is not None:
          _, next_tokens = restricter(
              state, tokens, scores, num_beams=1
          )
      else:
          next_tokens = scores.argmax(dim=-1, keepdim=True)
      token_ids = torch.cat([tokens, next_tokens], dim=1)
      cur_len = token_ids.size(1)
      dones = (
          token_ids.new_zeros(batch_size)
          .eq(1)
          .__or__(next_tokens.squeeze(1).eq(eos_token_id))
      )
      # tokens = tokens[:, -1:]
  
      if max_len_a != 0:
          # (bsz x num_beams, )
          if state.encoder_mask is not None:
              max_lengths = (
                  state.encoder_mask.sum(dim=1).float() * max_len_a
              ).long() + max_length
          else:
              max_lengths = tokens.new_full(
                  (tokens.size(0),),
                  fill_value=max_length,
                  dtype=torch.long,
              )
          real_max_length = max_lengths.max().item()
      else:
          real_max_length = max_length
          if state.encoder_mask is not None:
              max_lengths = (
                  state.encoder_mask.new_ones(
                      state.encoder_mask.size(0)
                  ).long()
                  * max_length
              )
          else:
              max_lengths = tokens.new_full(
                  (tokens.size(0),),
                  fill_value=max_length,
                  dtype=torch.long,
              )
  
      while cur_len < real_max_length:
          scores = decoder.decode(
              tokens=token_ids, state=state
          )  # batch_size x vocab_size
  
          if repetition_penalty != 1.0:
              token_scores = scores.gather(dim=1, index=token_ids)
              lt_zero_mask = token_scores.lt(0).float()
              ge_zero_mask = lt_zero_mask.eq(0).float()
              token_scores = (
                  lt_zero_mask * repetition_penalty * token_scores
                  + ge_zero_mask / repetition_penalty * token_scores
              )
              scores.scatter_(dim=1, index=token_ids, src=token_scores)
  
          if eos_token_id is not None and length_penalty != 1.0:
              token_scores = (
                  scores / cur_len**length_penalty
              )  # batch_size x vocab_size
              eos_mask = scores.new_ones(scores.size(1))
              eos_mask[eos_token_id] = 0
              eos_mask = eos_mask.unsqueeze(0).eq(1)
              scores = scores.masked_scatter(eos_mask, token_scores)
  
          if restricter is not None:
              _, next_tokens = restricter(state, token_ids, scores, 1)
          else:
              next_tokens = scores.argmax(dim=-1, keepdim=True)
          next_tokens = next_tokens.squeeze(-1)
  
          if _eos_token_id != -1:
              next_tokens = next_tokens.masked_fill(
                  max_lengths.eq(cur_len + 1), _eos_token_id
              )
          next_tokens = next_tokens.masked_fill(dones, pad_token_id)
          tokens = next_tokens.unsqueeze(1)
  
          token_ids = torch.cat(
              [token_ids, tokens], dim=-1
          )  # batch_size x max_len
  
          end_mask = next_tokens.eq(_eos_token_id)
          dones = dones.__or__(end_mask)
          cur_len += 1
  
          if dones.min() == 1:
              break
      return token_ids
  
imports: import torch
  from torch import nn
  from torch.nn import functional
  from transformers.models.bart.configuration_bart import BartConfig
  from .modeling_bart import BartModel
  from ..utils.util import avg_token_embeddings, seq_to_mask, get_model_device
  from functools import partial
  from typing import Union
label_desc: Use image node_id 2 for calling a global function with example usage: _no_beam_search_generate(decoder, state, tokens, max_length, max_len_a, bos_token_id, eos_token_id, repetition_penalty, length_penalty, pad_token_id, restricter) and returns: token_ids