{"record":{"id":"c7eb0c4b7f65e47b","repo":"tensorflow/models","slug":"maskedlm-cannot-be-directly-serialized-because-it-c7eb0c","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/mobile_bert_layers.py","lineNumber":545,"sourceCode":"        lm_data = tf.matmul(lm_data, self.embedding_table, transpose_b=True)\n      else:\n        lm_data = tf.matmul(\n            lm_data,\n            tf.concat([self.embedding_table, self.extra_output_weights],\n                      axis=1),\n            transpose_b=True)\n\n    logits = tf.nn.bias_add(lm_data, self.bias)\n    masked_positions_length = masked_positions.shape.as_list()[1] or tf.shape(\n        masked_positions)[1]\n    logits = tf.reshape(logits,\n                        [-1, masked_positions_length, self._vocab_size])\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.\n\n    Args:\n      sequence_tensor: Sequence output of `BertModel` layer of shape\n        `(batch_size, seq_length, num_hidden)` where `num_hidden` is number of\n        hidden units of `BertModel` layer.\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\n        `(batch_size * num_predictions, num_hidden)`.\n    \"\"\"","sourceCodeStart":527,"sourceCodeEnd":563,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/nlp/modeling/layers/mobile_bert_layers.py#L527-L563","documentation":"Error \"MaskedLM cannot be directly serialized because it has variable sharing logic.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/nlp/modeling/layers/mobile_bert_layers.py:545 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"}