src/transformers/pipelines/fill_mask.py/FillMaskPipeline/postprocess
class FillMaskPipeline:    def postprocess(self, model_outputs, top_k=5, target_ids=None):
        # Cap top_k if there are targets
        if target_ids is not None and target_ids.shape[0] < top_k:
            top_k = target_ids.shape[0]
        input_ids = model_outputs["input_ids"]
        outputs = model_outputs["logits"]
        result = []

        if self.framework == "tf":
            masked_index = tf.where(input_ids == self.tokenizer.mask_token_id).numpy()

            # Fill mask pipeline supports only one ${mask_token} per sample

            logits = outputs[0, masked_index.item(), :]
            probs = tf.nn.softmax(logits)
            if target_ids is not None:
                probs = tf.gather_nd(probs, tf.reshape(target_ids, (-1, 1)))

            topk = tf.math.top_k(probs, k=top_k)
            values, predictions = topk.values.numpy(), topk.indices.numpy()
        else:
            masked_index = torch.nonzero(input_ids == self.tokenizer.mask_token_id, as_tuple=False)
            # Fill mask pipeline supports only one ${mask_token} per sample

            logits = outputs[0, masked_index.item(), :]
            probs = logits.softmax(dim=0)
            if target_ids is not None:
                probs = probs[..., target_ids]

            values, predictions = probs.topk(top_k)

        for v, p in zip(values.tolist(), predictions.tolist()):
            tokens = input_ids.numpy()
            if target_ids is not None:
                p = target_ids[p].tolist()
            tokens[masked_index] = p
            # Filter padding out:
            tokens = tokens[np.where(tokens != self.tokenizer.pad_token_id)]
            result.append(
                {
                    "sequence": self.tokenizer.decode(tokens, skip_special_tokens=True),
                    "score": v,
                    "token": p,
                    "token_str": self.tokenizer.decode(p),
                }
            )
        return result