tensorflow/models · error · NotImplementedError

MaskedLM cannot be directly serialized because it has variab

Error message

MaskedLM cannot be directly serialized because it has variable sharing logic.

What it means

Error "MaskedLM cannot be directly serialized because it has variable sharing logic." thrown in tensorflow/models.

Source

Thrown at official/nlp/modeling/layers/mobile_bert_layers.py:545

        lm_data = tf.matmul(lm_data, self.embedding_table, transpose_b=True)
      else:
        lm_data = tf.matmul(
            lm_data,
            tf.concat([self.embedding_table, self.extra_output_weights],
                      axis=1),
            transpose_b=True)

    logits = tf.nn.bias_add(lm_data, self.bias)
    masked_positions_length = masked_positions.shape.as_list()[1] or tf.shape(
        masked_positions)[1]
    logits = tf.reshape(logits,
                        [-1, masked_positions_length, self._vocab_size])
    if self._output_type == 'logits':
      return logits
    return tf.nn.log_softmax(logits)

  def get_config(self):
    raise NotImplementedError('MaskedLM cannot be directly serialized because '
                              'it has variable sharing logic.')

  def _gather_indexes(self, sequence_tensor, positions):
    """Gathers the vectors at the specific positions.

    Args:
      sequence_tensor: Sequence output of `BertModel` layer of shape
        `(batch_size, seq_length, num_hidden)` where `num_hidden` is number of
        hidden units of `BertModel` layer.
      positions: Positions ids of tokens in sequence to mask for pretraining
        of with dimension `(batch_size, num_predictions)` where
        `num_predictions` is maximum number of tokens to mask out and predict
        per each sequence.

    Returns:
      Masked out sequence tensor of shape
        `(batch_size * num_predictions, num_hidden)`.
    """

View on GitHub (pinned to e006f5f0d5)

When it happens

Trigger: Thrown at official/nlp/modeling/layers/mobile_bert_layers.py:545 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/c7eb0c4b7f65e47b. Report an issue: GitHub.