{"record":{"id":"f12dd0b9652c752d","repo":"xai-org/x-algorithm","slug":"fixed-total-len-fixed-total-len-is-smaller-than","errorCode":null,"errorMessage":"fixed_total_len={fixed_total_len} is smaller than packed length {packed_len}","messagePattern":"fixed_total_len=(.+?) is smaller than packed length (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phoenix/xrex/pallas/ranker_attention_varlen.py","lineNumber":164,"sourceCode":"\n    flat_segment_ids = segment_ids.reshape(total_len)\n    valid = flat_segment_ids != PADDING_SEGMENT_ID\n\n    flat_q = q.reshape(total_len, num_q_heads, head_dim)\n    flat_k = k.reshape(total_len, num_kv_heads, head_dim)\n    flat_v = v.reshape(total_len, num_kv_heads, head_dim)\n    flat_temp = temp.reshape(total_len)\n\n    q_packed = flat_q[valid]\n    k_packed = flat_k[valid]\n    v_packed = flat_v[valid]\n    temp_packed = flat_temp[valid]\n    segment_ids_packed = flat_segment_ids[valid]\n\n    packed_len = int(q_packed.shape[0])\n    if fixed_total_len is not None:\n        if fixed_total_len < packed_len:\n            raise ValueError(\n                f\"fixed_total_len={fixed_total_len} is smaller than packed length {packed_len}\"\n            )\n        pad_len = fixed_total_len - packed_len\n        if pad_len > 0:\n            q_packed = jnp.pad(q_packed, ((0, pad_len), (0, 0), (0, 0)))\n            k_packed = jnp.pad(k_packed, ((0, pad_len), (0, 0), (0, 0)))\n            v_packed = jnp.pad(v_packed, ((0, pad_len), (0, 0), (0, 0)))\n            temp_packed = jnp.pad(temp_packed, ((0, pad_len),), constant_values=0)\n            segment_ids_packed = jnp.pad(\n                segment_ids_packed, ((0, pad_len),), constant_values=PADDING_SEGMENT_ID\n            )\n\n    seqlens = jnp.sum(segment_ids != PADDING_SEGMENT_ID, axis=1).astype(jnp.int32)\n    cu_seqlens = jnp.concatenate([jnp.array([0], dtype=seqlens.dtype), jnp.cumsum(seqlens)])\n    max_seqlen = int(jnp.max(seqlens))\n\n    return (\n        q_packed,","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/phoenix/xrex/pallas/ranker_attention_varlen.py#L146-L182","documentation":"pack_inputs flattens variable-length sequences, drops padding, and optionally re-pads to fixed_total_len. If the actual packed (valid token) length exceeds fixed_total_len, there is not enough room and the function raises this error. It typically means the caller's static bucket size is smaller than the real total sequence length in the batch.","triggerScenarios":"Calling the varlen attention wrapper with fixed_total_len set to a value smaller than sum of valid sequence lengths in the batch (packed_len), e.g. bucket 8192 while the batch packs 10000 tokens.","commonSituations":"Static-shape bucketing for TPU compilation where batch content grew beyond the bucket; mixed datasets with occasional longer batches after shuffling; incorrect computation of fixed_total_len from batch metadata.","solutions":["Increase fixed_total_len to >= the true packed length (e.g. next bucket size).","Compute fixed_total_len from the batch: int(valid.sum()) or sum of lengths before calling, with headroom.","Filter/clip batches whose total length exceeds the bucket during data loading.","Check whether padding tokens are being counted as valid due to a wrong mask, inflating packed_len."],"exampleFix":"# before\nout = attn(q, k, v, lens, fixed_total_len=8192)  # batch packs 9000 tokens\n# after\nfixed = int(sum(lens))  # or next bucket >= packed len\nout = attn(q, k, v, lens, fixed_total_len=16384)","handlingStrategy":"validation","validationCode":"packed = int(sum(lens))  # or int(valid_mask.sum())\nassert fixed_total_len is None or fixed_total_len >= packed","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive fixed_total_len from the batch's true valid token count plus headroom.","Bucket/filter batches at data-loading time so packed length never exceeds the compiled bucket.","Verify padding masks are correct so padded tokens are not counted as valid."],"tags":["pallas","jax","tpu","varlen","bucketing","static-shapes"],"backgroundTag":"buffer-size-too-small","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}