Skip to content

fix(vllm): respect left-padded attention masks - #1804

Merged
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
emerardd:fix/vllm-left-padding-mask
Sep 24, 2026
Merged

jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
emerardd:fix/vllm-left-padding-mask

Conversation

@emerardd

Copy link
Copy Markdown
Contributor

Description

VLLMDriver.forward() previously treated attention_mask.sum() as a prefix length. A left-padded input such as IDs [0, 0, 7, 8] with mask [0, 0, 1, 1] therefore sent [0, 0] to vLLM instead of [7, 8]. The eager batched path had the same behavior, changing the prompt and any resulting logits or captures without an error.

Both paths now retain the contiguous span of real tokens, allowing left, right, and two-sided padding. The driver rejects length mismatches, non-binary masks, all-masked prompts, and interior gaps rather than silently changing the prompt's meaning. Mask validation happens before the optional vLLM import on the batched path.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Validation

  • New offline regression tests reproduce the old wrong-token dispatch for single and batched left-padded prompts, then pass with the fix. They also cover right/two-sided padding and unsupported mask layouts.
  • tests/unit/model_bridge/test_vllm_driver.py: 86 passed, 18 skipped. The skipped existing tests require a local vLLM installation.
  • pycln --check --all, isort --check-only, and black --check pass for both changed files.
  • mypy .: no issues in 383 source files. git diff --check passes.
  • A real Linux/CUDA vLLM run was not available locally; these tests verify the token IDs passed to the vLLM request boundary using offline mocks.

Checklist

  • I have added tests that prove my fix is effective
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

No documentation or unrelated tests were changed.

@jlarson4 jlarson4 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.

Thanks @emerardd! The validation is thorough and the prompt-API stub is a nice touch. A couple comments on what the driver returns after the trim on both paths.

)
prompts_ids = [ids[: int(row.sum())] for ids, row in zip(prompts_ids, mask)]
prompts_ids = [
self._trim_masked_prompt(ids, row) for ids, row in zip(prompts_ids, mask)

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.

The assembly below still lays every row out from position 0, so a left-padded batch comes back right-padded: logits[:, -1] is -inf for the shorter rows and the bridge's loss path goes NaN against the caller's mask. Can the returned logits and captures go back where the mask had them?

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.

Fixed in 33276b7. After vLLM processes each trimmed prompt, the driver restores reconstructed or fallback logits and captures to the caller's original mask coordinates. Pad logits remain -inf and pad captures zero. Added batched regressions for left/right/two-sided padding and a finite masked-loss check; the vLLM driver unit file passes (93 passed, 18 skipped). Real vLLM/GPU validation is still outstanding.

mask = torch.as_tensor(attention_mask)
if mask.ndim == 2 and mask.shape[0] == 1:
mask = mask[0]
ids_list = self._trim_masked_prompt(ids_list, mask)

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.

A left-padded trim shifts pos and the returned captures by the leading padding, so a pos in the caller's coordinates is rejected or lands on a different token. Is there a way to keep the caller's coordinates on this path as well?

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.

Fixed in 33276b7. The single-prompt path now translates caller pos coordinates to the trimmed prompt before dispatch, restores captured tensors and logits to the caller's positions, and rejects positions that point into padding. Added tests for valid and padded positions plus restored single-prompt outputs. The vLLM driver unit file passes (93 passed, 18 skipped).

@jlarson4

Copy link
Copy Markdown
Collaborator

Looks good! Thanks @emerardd

@jlarson4
jlarson4 merged commit 27576af into TransformerLensOrg:dev Sep 24, 2026
26 checks passed
@emerardd
emerardd deleted the fix/vllm-left-padding-mask branch October 17, 2026 12:55
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.

2 participants