docstr: Generate input batches.
docstr_tok: ["Generate","input","batches","."]
code: def _batchify(
      self, data: List[List[str]], max_seq_len: int, batch_size: int
  ):
      """
      Generate input batches.
      """
      padding = False if batch_size == 1 else True
      pad_func = Pad(
          pad_val=self.tokenizer.pad_token_id,
          pad_right=False,
          dtype=np.int64,
      )
  
      def pad_mask(batch_attention_mask):
          batch_size = len(batch_attention_mask)
          max_len = max(map(len, batch_attention_mask))
          attention_mask = (
              np.ones((batch_size, max_len, max_len), dtype="float32")
              * -1e9
          )
          for i, mask_data in enumerate(attention_mask):
              seq_len = len(batch_attention_mask[i])
              mask_data[-seq_len:, -seq_len:] = np.array(
                  batch_attention_mask[i], dtype="float32"
              )
          # In order to ensure the correct broadcasting mechanism, expand one
          # dimension to the second dimension (n_head of Transformer).
          attention_mask = np.expand_dims(attention_mask, axis=1)
          return attention_mask
  
      def _parse_batch(batch_examples):
          if padding:
              input_ids = pad_func(
                  [example["input_ids"] for example in batch_examples]
              )
              token_type_ids = pad_func(
                  [
                      example["token_type_ids"]
                      for example in batch_examples
                  ]
              )
              position_ids = pad_func(
                  [
                      example["position_ids"]
                      for example in batch_examples
                  ]
              )
              attention_mask = pad_mask(
                  [
                      example["attention_mask"]
                      for example in batch_examples
                  ]
              )
          else:
              input_ids = np.asarray(
                  [example["input_ids"] for example in batch_examples],
                  dtype=np.int64,
              )
              token_type_ids = np.asarray(
                  [
                      example["token_type_ids"]
                      for example in batch_examples
                  ],
                  dtype=np.int64,
              )
              position_ids = np.asarray(
                  [
                      example["position_ids"]
                      for example in batch_examples
                  ],
                  dtype=np.int64,
              )
              attention_mask = np.asarray(
                  [
                      example["attention_mask"]
                      for example in batch_examples
                  ]
              )
              attention_mask = np.expand_dims(attention_mask, 0)
  
          return input_ids, token_type_ids, position_ids, attention_mask
  
      examples = []
      for texts in data:
          examples.append(
              self._convert_text_to_input(texts, max_seq_len)
          )
  
      # Seperates data into some batches.
      one_batch = []
      for example in examples:
          one_batch.append(example)
          if len(one_batch) == batch_size:
              yield _parse_batch(one_batch)
              one_batch = []
      if one_batch:
          yield _parse_batch(one_batch)
  
file_path: PaddleHub/modules/text/text_generation/plato-mini/module.py
imports: import contextlib
  from collections import deque
  from typing import List, Union
  import numpy
  import paddle
  import paddle.nn
  from paddlehub.module.module import moduleinfo, serving
  from paddlenlp.data import Pad
  from paddlenlp.transformers import UnifiedTransformerLMHeadModel, UnifiedTransformerTokenizer
  from plato_mini.utils import select_response
label_desc: Use image node_id 3 for calling the UnifiedTransformer obj's underlying member method code with example usage: obj._batchify(data, max_seq_len, batch_size) and returns: attention_mask, input_ids,  token_type_ids,  position_ids,  attention_mask