{"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":"Divisibility guard in the transformer Attention layer constructor: hidden_size % num_heads != 0, so the embedding cannot be split into equal per-head depth vectors. The model hyperparameters (hidden_size and num_heads) are inconsistent.","triggerScenarios":"Thrown at official/legacy/transformer/attention_layer.py:35 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Choose num_heads that divides hidden_size (e.g. hidden 512 with 8 heads)","Adjust hidden_size in model_params to a multiple of num_heads"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}