> This page is for version Nightly (default).
> For other versions, use one of these documentation indexes:
> - Nightly (default): https://docs.nvidia.com/nemo/automodel/nightly/llms.txt
> - Latest: https://docs.nvidia.com/nemo/automodel/latest/llms.txt
> - 0.7.0 · 26.10: https://docs.nvidia.com/nemo/automodel/v0.7/llms.txt
> - 0.5.0 · 26.06: https://docs.nvidia.com/nemo/automodel/v0.5/llms.txt
> - 0.4.0 · 26.04: https://docs.nvidia.com/nemo/automodel/v0.4/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/automodel/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/automodel/_mcp/server.

# nemo_automodel.components.models.gemma4_moe.attention

Composition of document-causal masks with Gemma4's native attention policy.

## Module Contents

### Functions

| Name                                                                                                                  | Description                                                                  |
| --------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| [`prepare_gemma4_document_mask`](#nemo_automodel-components-models-gemma4_moe-attention-prepare_gemma4_document_mask) | Compose native full/sliding attention with an explicit document-causal mask. |

### API

```python
nemo_automodel.components.models.gemma4_moe.attention.prepare_gemma4_document_mask(
    config: transformers.configuration_utils.PreTrainedConfig,
    attention_mask: torch.Tensor,
    mm_token_type_ids: torch.Tensor | None,
    document_ids: torch.Tensor,
    dtype: torch.dtype
) -> dict[str, torch.Tensor]
```

Compose native full/sliding attention with an explicit document-causal mask.

Call only for the document-causal contract marked by `_packed_seq_ids`;
arbitrary custom 4D masks are already complete and must not be interpreted here.
HF otherwise treats a 4D input as complete, bypassing its vision/window policy.

**Parameters:**

**`config`** `PreTrainedConfig`

Gemma4 text configuration using eager or SDPA attention.

---

**`attention_mask`** `torch.Tensor`

Boolean-allowed or additive-zero/minimum mask \[batch, 1,
sequence, sequence]. This carries the caller's selected document
isolation and excludes padding/fake media on both axes.

---

**`mm_token_type_ids`** `torch.Tensor | None`

Processor modality IDs \[batch, sequence], or None for text.

---

**`document_ids`** `torch.Tensor`

Original document IDs \[batch, sequence]. These keep image
groups separate even when the selected mask allows cross-document attention.

---

**`dtype`** `torch.dtype`

Model floating point dtype for the additive native masks.

---

**Returns:** `dict[str, torch.Tensor]`

Full and sliding masks \[batch, 1, sequence, sequence]: boolean for SDPA