docstr: Perturb the instances in a single forward pass (parallel).
  
  Parameters
  ----------
  num_samples
      Number of samples to be generated
  raw
      Array of mask templates. Has `mask_templates` rows.
  data
      Binary array having 0 where the tokens are masked. Has `mask_templates` rows.
  top_n:
      Use the top n words when sampling.
  batch_size_lm:
      Batch size used for language model.
  temperature
      Sample weight hyper-parameter.
  use_proba
      Bool whether to sample according to the predicted words distribution
  **kwargs
      Other arguments. Not used.
  
  Returns
  -------
  sampled_tokens
      Array containing the ids of the sampled tokens. Has `num_samples` rows.
  sampled_data
      Binary array having 0 where the tokens were masked. Has `num_samples` rows.
docstr_tok: ["Perturb","the","instances","in","a","single","forward","pass","(","parallel",")",".","Parameters","--","--","--","--","--","num_samples","Number","of","samples","to","be","generated","raw","Array","of","mask","templates",".","Has","`","mask_templates","`","rows",".","data","Binary","array","having","0","where","the","tokens","are","masked",".","Has","`","mask_templates","`","rows",".","top_n",":","Use","the","top","n","words","when","sampling",".","batch_size_lm",":","Batch","size","used","for","language","model",".","temperature","Sample","weight","hyper-parameter",".","use_proba","Bool","whether","to","sample","according","to","the","predicted","words","distribution","*","*","kwargs","Other","arguments",".","Not","used",".","Returns","--","--","--","-","sampled_tokens","Array","containing","the","ids","of","the","sampled","tokens",".","Has","`","num_samples","`","rows",".","sampled_data","Binary","array","having","0","where","the","tokens","were","masked",".","Has","`","num_samples","`","rows","."]
code: def _perturb_instances_parallel(
      self,
      num_samples: int,
      raw: np.ndarray,
      data: np.ndarray,
      top_n: int = 100,
      batch_size_lm: int = 32,
      temperature: float = 1.0,
      use_proba: bool = False,
      **kwargs,
  ) -> Tuple[np.ndarray, np.ndarray]:
      """
      Perturb the instances in a single forward pass (parallel).
  
      Parameters
      ----------
      num_samples
          Number of samples to be generated
      raw
          Array of mask templates. Has `mask_templates` rows.
      data
          Binary array having 0 where the tokens are masked. Has `mask_templates` rows.
      top_n:
          Use the top n words when sampling.
      batch_size_lm:
          Batch size used for language model.
      temperature
          Sample weight hyper-parameter.
      use_proba
          Bool whether to sample according to the predicted words distribution
      **kwargs
          Other arguments. Not used.
  
      Returns
      -------
      sampled_tokens
          Array containing the ids of the sampled tokens. Has `num_samples` rows.
      sampled_data
          Binary array having 0 where the tokens were masked. Has `num_samples` rows.
      """
      # tokenize instances
      tokens_plus = self.model.tokenizer.batch_encode_plus(
          list(raw), padding=True, return_tensors="tf"
      )
  
      # number of samples to generate per mask template
      remainder = num_samples % len(raw)
      mult_factor = num_samples // len(raw)
  
      # fill in masks with language model
      # (mask_template x max_length_sentence x num_tokens)
      logits = self.model.predict_batch_lm(
          x=tokens_plus,
          vocab_size=self.model.tokenizer.vocab_size,
          batch_size=batch_size_lm,
      )
  
      # select rows and cols where the input the tokens are masked
      tokens = tokens_plus[
          "input_ids"
      ]  # (mask_template x max_length_sentence)
      mask_pos = tf.where(tokens == self.model.mask_id)
      mask_row, mask_col = mask_pos[:, 0], mask_pos[:, 1]
  
      # buffer containing sampled tokens
      sampled_tokens = np.zeros(
          (num_samples, tokens.shape[1]), dtype=np.int32
      )
      sampled_data = np.zeros((num_samples, data.shape[1]))
  
      for i in range(logits.shape[0]):
          # select indices corresponding to the current row `i`
          idx = tf.reshape(tf.where(mask_row == i), shape=-1)
  
          # select columns corresponding to the current row `i`
          cols = tf.gather(mask_col, idx)
  
          # select the logits of the masked input
          logits_mask = logits[i, cols, :]
  
          # mask out tokens according to the subword_mask
          logits_mask[:, self.subwords_mask] = -np.inf
  
          # select top n tokens from each distribution
          top_k = tf.math.top_k(logits_mask, top_n)
          top_k_logits, top_k_tokens = top_k.values, top_k.indices
          top_k_logits = (
              (top_k_logits / temperature)
              if use_proba
              else (top_k_logits * 0)
          )
  
          # sample `num_samples` instance for the current mask template
          for j in range(mult_factor + int(i < remainder)):
              # Compute the buffer index
              idx = i * mult_factor + j + min(i, remainder)
  
              # Sample indices
              ids_k = tf.reshape(
                  tf.random.categorical(top_k_logits, 1), shape=-1
              )
  
              # Set the unmasked tokens and for the masked one and replace them with the samples drawn
              sampled_tokens[idx] = tokens[i]
              sampled_tokens[idx, cols] = tf.gather(
                  top_k_tokens, ids_k, batch_dims=1
              )
  
          # Add the original binary mask which marks the beginning of a masked
          # word, as is needed for the anchor algorithm (backend stuff)
          idx, offset = i * mult_factor, min(i, remainder)
          sampled_data[
              idx
              + offset : idx
              + mult_factor
              + offset
              + (i < remainder)
          ] = data[i]
  
      # Check that there are not masked tokens left
      assert np.all(sampled_tokens != self.model.mask_id)
      assert np.all(np.any(sampled_tokens != 0, axis=1))
      return sampled_tokens, sampled_data
  
imports: import string
  from functools import partial
  from typing import Dict, List, Optional, Tuple
  import numpy
  import tensorflow
  from alibi.utils.lang_model import LanguageModel
  from alibi.explainers.anchors.text_samplers import AnchorTextSampler
label_desc: Use image node_id 11 for calling the LanguageModelSampler obj's underlying member method code with example usage: obj._perturb_instances_parallel(num_samples, raw, data, top_n, batch_size_lm, temperature, use_proba) and returns: sampled_tokens,  sampled_data