class_docstr: Generates target sentence/ids using a base model.
  
  It generates target sentence/ids for a model (a pretrained transformer model or a function).
class_docstr_tok: ["Generates","target","sentence\/ids","using","a","base","model",".","It","generates","target","sentence\/ids","for","a","model","(","a","pretrained","transformer","model","or","a","function",")","."]
docstr: This function performs text generation for tensorflow and pytorch models.
  
  Parameters
  ----------
  X: dict
      Dictionary of padded source sentence ids and attention mask as tensors.
  
  Returns
  -------
  numpy.ndarray
      Returns target sentence ids.
docstr_tok: ["This","function","performs","text","generation","for","tensorflow","and","pytorch","models",".","Parameters","--","--","--","--","--","X",":","dict","Dictionary","of","padded","source","sentence","ids","and","attention","mask","as","tensors",".","Returns","--","--","--","-","numpy.ndarray","Returns","target","sentence","ids","."]
code: def model_generate(self, X):
      """This function performs text generation for tensorflow and pytorch models.
  
      Parameters
      ----------
      X: dict
          Dictionary of padded source sentence ids and attention mask as tensors.
  
      Returns
      -------
      numpy.ndarray
          Returns target sentence ids.
      """
      if (
          hasattr(self.inner_model.config, "is_encoder_decoder")
          and not self.inner_model.config.is_encoder_decoder
      ) and (
          hasattr(self.inner_model.config, "is_decoder")
          and not self.inner_model.config.is_decoder
      ):
          pass
          # TODOmaybe: Is this okay? I am just assuming we want is_decoder when neither are set
          # self.inner_model.config.is_decoder = True
          # raise ValueError(
          #     "Please assign either of is_encoder_decoder or is_decoder to True in model config for extracting target sentence ids"
          # )
      # check if user assigned any text generation specific kwargs
      text_generation_params = {}
      if (
          self.inner_model.config.__dict__.get("task_specific_params")
          is not None
          and self.inner_model.config.task_specific_params.get(
              "text-generation"
          )
          is not None
      ):
          text_generation_params = (
              self.inner_model.config.task_specific_params[
                  "text-generation"
              ]
          )
          if not isinstance(text_generation_params, dict):
              raise ValueError(
                  "Please assign text generation params as a dictionary under task_specific_params with key 'text-generation' "
              )
          # remove keys that are overridden by params on the model itself
          # (this is to mimic how precedence works for transformers pipelines)
          for k in list(text_generation_params.keys()):
              if hasattr(self.inner_model.config, k):
                  del text_generation_params[k]
      if self.model_type == "pt":
          # create torch tensors and move to device
          # TODOmaybe: SML: why move the model from where it was? the could mess with the user env (i.e. it breaks pipelines)
          # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') if self.device is None else self.device
          # self.inner_model = self.inner_model.to(device)
          # self.inner_model.eval()
          import torch
  
          with torch.no_grad():
              if self.inner_model.config.is_encoder_decoder:
                  inputs = self.get_inputs(X)
              else:
                  inputs = self.get_inputs(X, padding_side="left")
              if self.device is not None:
                  inputs = inputs.to(self.device)
              outputs = (
                  self.inner_model.generate(
                      **inputs, **text_generation_params
                  )
                  .detach()
                  .cpu()
                  .numpy()
              )
      elif self.model_type == "tf":
          if self.inner_model.config.is_encoder_decoder:
              inputs = self.get_inputs(X)
          else:
              inputs = self.get_inputs(X, padding_side="left")
          if self.device is None:
              outputs = self.inner_model.generate(
                  inputs, **text_generation_params
              ).numpy()
          else:
              try:
                  import tensorflow as tf
  
                  with tf.device(self.device):
                      outputs = self.inner_model.generate(
                          inputs, **text_generation_params
                      ).numpy()
              except RuntimeError as err:
                  print(err)
      if getattr(self.inner_model.config, "is_decoder", True):
          # slice the output ids after the input ids
          outputs = outputs[:, inputs["input_ids"].shape[1] :]
      # parse output ids to find special tokens in prefix and suffix
      parsed_tokenizer_dict = (
          self.parse_prefix_suffix_for_model_generate_output(
              outputs[0, :].tolist()
          )
      )
      keep_prefix, keep_suffix = (
          parsed_tokenizer_dict["keep_prefix"],
          parsed_tokenizer_dict["keep_suffix"],
      )
      # extract target sentence ids by slicing off prefix and suffix
      if keep_suffix > 0:
          target_X = outputs[:, keep_prefix:-keep_suffix]
      else:
          target_X = outputs[:, keep_prefix:]
      return target_X
  
file_path: shap/shap/models/_text_generation.py
imports: import numpy
  from .._serializable import Deserializer, Serializer
  from ..utils import safe_isinstance
  from ._model import Model
label_desc: Use image node_id 4 for calling the TextGeneration obj's underlying member method code with example usage: obj.model_generate(X) and returns: target_X