keras-team/keras · error · ValueError

Sharding along sequence dimension not allowed in TPU kernel…

Error message

Sharding along sequence dimension not allowed in TPU kernel attention

What it means

Raised inside the TPU Splash/attention kernel wrapper used by dot_product_attention on TPU. The kernel requires the model weights (and the sequence dimension) to be replicated, so if the mesh partitions the sequence axis the wrapper rejects the configuration with this guard before launching the TPU attention kernel.

Solutions

  1. Change the sharding spec so the sequence dimension is not partitioned (replicate it across the mesh)
  2. Use a non-TPU backend, or disable the TPU attention kernel path (e.g. flash_attention=False) to fall back to a kernel that tolerates sequence sharding
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at keras/src/backend/jax/nn.py:1612 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/ad218a42ad6bd8c8. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/jax/nn.py:1612

        decoder_segment_ids: Optional. Segment IDs for the decoder, used for
            sharding or masking.
        custom_mask: Optional[jax.Array]. A custom attention mask to apply. If
            None, a causal mask is used.
        attn_logits_soft_cap: Optional[float]. If provided, applies a soft cap
            to the attention logits.
        head_shards: int, default=1. Number of shards for the attention heads.
        q_seq_shards: int, default=1. Number of shards for the query sequence
            dimension.
    Returns:
        jax.Array: The result of applying the Splash multi-head attention
            kernel to the inputs.
    Raises:
        AssertionError: If sharding along the sequence dimension is attempted
            with decoder_segment_ids.
    """
    if decoder_segment_ids is not None:
        if query.shape[2] != decoder_segment_ids.q.shape[1]:
            raise ValueError(
                "Sharding along sequence dimension not allowed"
                " in TPU kernel attention"
            )

    if custom_mask is not None:
        mask = splash_attention_mask.NumpyMask(array=custom_mask)
    else:
        mask = splash_attention_mask.CausalMask(
            shape=(query.shape[2], query.shape[2])
        )

    # Create multi-head mask
    multi_head_mask = splash_attention_mask.MultiHeadMask(
        masks=(mask,) * query.shape[1]
    )
    splash_kernel = splash_attention_kernel.make_splash_mha(
        mask=multi_head_mask,
        head_shards=head_shards,

View on GitHub (pinned to 7a34a03db6)