{"record":{"id":"5f60d43a1b892623","repo":"tensorflow/models","slug":"key-shape-2-must-be-divisible-by-tgt-block-size","errorCode":null,"errorMessage":"key_shape[-2] must be divisible by tgt_block_size.","messagePattern":"key_shape\\[-2\\] must be divisible by tgt_block_size\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/nlp/modeling/layers/block_sparse_attention.py","lineNumber":269,"sourceCode":"      )\n      # pytype: disable=attribute-error\n      return super()._compute_attention(\n          query,\n          key,\n          value,\n          attention_mask=attention_mask,\n          training=training,\n      )\n      # pytype: enable=attribute-error\n    # src_num_blocks and tgt_num_blocks are the number of blocks in the source\n    # and target. Care should be taken to ensure that the number of blocks in\n    # the source and target are the same.\n    if self._query_shape[-2] % self._src_block_size != 0:\n      raise ValueError(\n          \"query_shape[-2] must be divisible by src_block_size.\"\n      )\n    if self._key_shape[-2] % self._tgt_block_size != 0:\n      raise ValueError(\n          \"key_shape[-2] must be divisible by tgt_block_size.\"\n      )\n    src_num_blocks = self._query_shape[-2] // self._src_block_size\n    tgt_num_blocks = self._key_shape[-2] // self._tgt_block_size\n\n    if src_num_blocks != tgt_num_blocks and tgt_num_blocks != 1:\n      raise ValueError(\n          \"src_num_blocks must be equal to tgt_num_blocks.\"\n      )\n    # Convert the query/key/value into blocks to perform block diagonal\n    # attention.\n    query_blocks = tf.reshape(query, [\n        -1,\n        self._num_heads,\n        src_num_blocks,\n        self._src_block_size,\n        self._key_dim,\n    ])","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/nlp/modeling/layers/block_sparse_attention.py#L251-L287","documentation":"Error \"key_shape[-2] must be divisible by tgt_block_size.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/nlp/modeling/layers/block_sparse_attention.py:269 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"}