{"record":{"id":"2700abb51a0701d7","repo":"tensorflow/models","slug":"maskedlm-cannot-be-directly-serialized-because-it","errorCode":null,"errorMessage":"MaskedLM cannot be directly serialized because it has variable sharing logic.","messagePattern":"MaskedLM cannot be directly serialized because it has variable sharing logic\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"official/nlp/modeling/layers/masked_lm.py","lineNumber":99,"sourceCode":"    lm_data = self.layer_norm(lm_data)\n    lm_data = tf.matmul(lm_data, self.embedding_table, transpose_b=True)\n    logits = tf.nn.bias_add(lm_data, self.bias)\n    masked_positions_length = (\n        masked_positions.shape.as_list()[1] or tf.shape(masked_positions)[1]\n    )\n    batch_size = (\n        masked_positions.shape.as_list()[0] or tf.shape(masked_positions)[0]\n    )\n    logits = tf.reshape(\n        logits,\n        [batch_size, masked_positions_length, self._vocab_size],\n    )\n    if self._output_type == 'logits':\n      return logits\n    return tf.nn.log_softmax(logits)\n\n  def get_config(self):\n    raise NotImplementedError('MaskedLM cannot be directly serialized because '\n                              'it has variable sharing logic.')\n\n  def _gather_indexes(self, sequence_tensor, positions):\n    \"\"\"Gathers the vectors at the specific positions, for performance.\n\n    Args:\n        sequence_tensor: Sequence output of shape\n          (`batch_size`, `seq_length`, num_hidden) where num_hidden is number of\n          hidden units.\n        positions: Positions ids of tokens in sequence to mask for pretraining\n          of with dimension (batch_size, num_predictions) where\n          `num_predictions` is maximum number of tokens to mask out and predict\n          per each sequence.\n\n    Returns:\n        Masked out sequence tensor of shape (batch_size * num_predictions,\n        num_hidden).\n    \"\"\"","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/nlp/modeling/layers/masked_lm.py#L81-L117","documentation":"Error \"MaskedLM cannot be directly serialized because it has variable sharing logic.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/nlp/modeling/layers/masked_lm.py:99 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}