{"record":{"id":"1066a7594e70fc14","repo":"tensorflow/models","slug":"hidden-size-must-be-divisible-by-the-number-o","errorCode":null,"errorMessage":"Hidden size ({}) must be divisible by the number of heads ({}).","messagePattern":"Hidden size \\((.+?)\\) must be divisible by the number of heads \\((.+?)\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/legacy/transformer/attention_layer.py","lineNumber":35,"sourceCode":"\nimport tensorflow as tf, tf_keras\n\nfrom official.modeling import tf_utils\n\n\nclass Attention(tf_keras.layers.Layer):\n  \"\"\"Multi-headed attention layer.\"\"\"\n\n  def __init__(self, hidden_size, num_heads, attention_dropout):\n    \"\"\"Initialize Attention.\n\n    Args:\n      hidden_size: int, output dim of hidden layer.\n      num_heads: int, number of heads to repeat the same attention structure.\n      attention_dropout: float, dropout rate inside attention for training.\n    \"\"\"\n    if hidden_size % num_heads:\n      raise ValueError(\n          \"Hidden size ({}) must be divisible by the number of heads ({}).\"\n          .format(hidden_size, num_heads))\n\n    super(Attention, self).__init__()\n    self.hidden_size = hidden_size\n    self.num_heads = num_heads\n    self.attention_dropout = attention_dropout\n\n  def build(self, input_shape):\n    \"\"\"Builds the layer.\"\"\"\n    # Layers for linearly projecting the queries, keys, and values.\n    size_per_head = self.hidden_size // self.num_heads\n\n    def _glorot_initializer(fan_in, fan_out):\n      limit = math.sqrt(6.0 / (fan_in + fan_out))\n      return tf_keras.initializers.RandomUniform(minval=-limit, maxval=limit)\n\n    attention_initializer = _glorot_initializer(input_shape.as_list()[-1],","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/legacy/transformer/attention_layer.py#L17-L53","documentation":"Error \"Hidden size ({}) must be divisible by the number of heads ({}).\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/legacy/transformer/attention_layer.py:35 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"}