Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[agents docs] update models.md with class attributes and attention mask#13665

Merged
yiyixuxu merged 3 commits into
mainfrom
update-ai-models-docs
May 1, 2026
Merged

[agents docs] update models.md with class attributes and attention mask#13665
yiyixuxu merged 3 commits into
mainfrom
update-ai-models-docs

Conversation

@yiyixuxu

@yiyixuxu yiyixuxu commented May 1, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

…sk guidance

- Add "Model class attributes" section documenting _no_split_modules,
  _repeated_blocks, _skip_layerwise_casting_patterns, _keep_in_fp32_modules,
  _cp_plan, and _supports_gradient_checkpointing with their corresponding
  user-facing APIs and how they work
- Improve attention mask guidance: recommend passing None when no real
  padding exists, document backend compatibility
- Move _no_split_modules from gotchas to its own section with first-principles
  explanation of why it's needed (accelerate device hooks)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@github-actions github-actions Bot added the size/M PR with diff < 200 LOC label May 1, 2026
@github-actions github-actions Bot added size/M PR with diff < 200 LOC and removed size/M PR with diff < 200 LOC labels May 1, 2026
@github-actions github-actions Bot added size/M PR with diff < 200 LOC and removed size/M PR with diff < 200 LOC labels May 1, 2026
@yiyixuxu yiyixuxu requested a review from sayakpaul May 1, 2026 02:45

@sayakpaul sayakpaul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the dead code path addition. It's quite nice!

Comment thread .ai/models.md
- **Padding mask → bool `(B, L)` or `(B, 1, 1, L)`.** Stays compatible with the `*_varlen` kernels via `_normalize_attn_mask` (`attention_dispatch.py:639`), which reduces bool masks to `cu_seqlens`. Dense additive-float masks *cannot* be reduced this way and so lose the varlen path. This is the Qwen pattern (`transformer_qwenimage.py:951`).
- **Structural mask (causal, sliding-window, band-diagonal) → dense `(1, 1, L, L)` is unavoidable.** Row-varying patterns can't be expressed as `(B, L)`. Expect SDPA/Flex-only for these layers; consider Flex's `sliding_window_mask_mod` or FA3's native `window_size=` kwarg if backend flexibility matters. Consult `src/models/transformers/transformer_kandinsky.py` as a reference.
- **Padding mask → bool `(B, L)` or `(B, 1, 1, L)`.** Only pass when the batch actually contains different-length sequences (i.e. there is real padding). If all sequences are the same length, set the mask to `None` — many backends (flash, sage, aiter) raise `ValueError` on any non-None mask, and even SDPA-based backends pay unnecessary overhead processing a no-op mask. See `pipeline_qwenimage.py` `encode_prompt` for the pattern: `if mask.all(): mask = None`. When a mask is needed, use bool format — it stays compatible with the `*_varlen` kernels via `_normalize_attn_mask` (`attention_dispatch.py:639`), which reduces bool masks to `cu_seqlens`. Dense additive-float masks *cannot* be reduced this way and so lose the varlen path.
- **Other mask types (structural, BlockMask, etc.)** — if the model requires a different mask pattern, figure out how to support as many backends as possible (e.g. use `window_size` kwarg for sliding window on flash, `BlockMask` for Flex) and document which backends are supported for that model.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we might want to keep the kandinsky reference in case it ends up discovering that Flex would be the way to go.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

let's see if it can figure it out by itself next time when it reviews a model that only works with flex
I think it's pretty straightforward, but we can always add a reference if it struggles

Comment thread .ai/models.md
- **`[]`** — split anywhere you like.
- **`["MyBlock"]`** — keep all `MyBlock` instances intact on one device.

**Why it's needed.** When `accelerate` splits a model across devices, it installs hooks on leaf modules that move inputs to the module's device before `forward` runs. Any inline operation (`+`, `*`, `torch.cat`) that combines tensors from different submodules has no hook — if those submodules landed on different devices, it crashes with "tensors on different devices". The fix is either: (a) list the parent module in `_no_split_modules` so all its children stay co-located, or (b) pack the operation into its own `nn.Module`. Inline ops on outputs from the **same** submodule call are fine since they're already on the same device.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A good rule of thumb is any block where the forward has a residual connection.

@yiyixuxu yiyixuxu merged commit c8eba43 into main May 1, 2026
8 checks passed
@yiyixuxu yiyixuxu deleted the update-ai-models-docs branch May 1, 2026 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M PR with diff < 200 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants