Skip to content

Falcon: pass position_ids to create_causal_mask so packed sequences stay isolated - #50894

Merged
vasqu merged 2 commits into
huggingface:mainfrom
sohailshk:fix/falcon-packed-position-ids
Nov 18, 2026
Merged

vasqu merged 2 commits into
huggingface:mainfrom
sohailshk:fix/falcon-packed-position-ids

Conversation

@sohailshk

@sohailshk sohailshk commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

Padding-free training (DataCollatorWithFlattening, no attention_mask) relies on create_causal_mask recovering the document boundaries from position_ids. FalconModel.forward calls it without position_ids, so with eager and sdpa each document attends to the documents packed before it, and the packed logits (and loss) differ from the per-sequence ones. Trainer runs with use_cache=False by default, so a Trainer + DataCollatorWithFlattening run on Falcon takes this path.

The fix passes position_ids (already computed a few lines above) the same way LlamaModel does. The mask only changes for packed inputs (position_ids given, no attention_mask, no cache).

Reproducer (published checkpoint, CPU), fails on main, passes with the fix:

import torch
from transformers import AutoModelForCausalLM, DataCollatorWithFlattening

model = AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-FalconForCausalLM").eval()
seqs = [[5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16]]
batch = DataCollatorWithFlattening(return_position_ids=True)([{"input_ids": s} for s in seqs])

with torch.no_grad():
    packed = model(input_ids=batch["input_ids"], position_ids=batch["position_ids"], use_cache=False).logits[0]
    separate = torch.cat([model(torch.tensor([s]), use_cache=False).logits[0] for s in seqs])
print("max |packed - separate|:", (packed - separate).abs().max().item())

main: 0.1989. With this PR: 1.2e-07.

Test: FalconModelTest.test_packed_sequences_do_not_attend_across_documents fails on main and passes here. The common test_{eager,sdpa}_padding_matches_padding_free_with_position_ids tests skip Falcon (_supports_attention_backend is False), which is why CI didn't catch this.

OPT and XGLM also call create_causal_mask without position_ids and leak the same way. I kept this PR to Falcon. The use_cache=True case (an empty cache disables the detection for every model) is #50419 / #50190 and is independent of this change.

Who can review?

@vasqu @ArthurZucker

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it.

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit but lgtm overall

Comment on lines +85 to +86
seq_a = torch.randint(0, config.vocab_size, (1, 5), device=torch_device)
seq_b = torch.randint(0, config.vocab_size, (1, 7), device=torch_device)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm can we not reuse the already prepared input ids? but nit

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. Done in 8fc031c:

  • The test no longer draws its own random ids. It takes the first row of input_ids from prepare_config_and_inputs_for_common(), cuts it in half, and packs the two halves back into one row with per-document position_ids.
  • The test keeps its meaning: with the position_ids=position_ids line removed from FalconModel.forward it fails (1 failed), with the fix it passes.
  • tests/models/falcon/test_modeling_falcon.py: 163 passed, 172 skipped. ruff check and ruff format --check pass on the file.

@vasqu

vasqu commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

run-slow: falcon

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@github-actions

Copy link
Copy Markdown
Contributor

CI (Nvidia + AMD)

Nvidia · ✅ No new failures 🎉👏 · ⚙️ Workflow · 📊 Dashboard

Failures (1 pre-existing)

    Pre-existing failures (still failing) — 1 total:

        Same error (1):

        - falcon:
            tests/models/falcon/test_modeling_falcon.py::FalconLanguageGenerationTest::test_falcon_alibi_sdpa_matches_eager

AMD · ✅ No new failures 🎉👏 · ⚙️ Workflow · 📊 Dashboard

Failures (2 pre-existing)

    Pre-existing failures (still failing) — 2 total:

        Same error (2):

        - falcon:
            tests/models/falcon/test_modeling_falcon.py::FalconModelTest::test_flash_attn_2_fp32_ln
            tests/models/falcon/test_modeling_falcon.py::FalconLanguageGenerationTest::test_falcon_alibi_sdpa_matches_eager

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: falcon

@vasqu
vasqu enabled auto-merge November 18, 2026 16:01
@vasqu
vasqu added this pull request to the merge queue Nov 18, 2026
Merged via the queue into huggingface:main with commit f560fe1 Nov 18, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants