> This page is for version 0.5.0 · 26.06.
> 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.

# nemo_automodel.components.models.ergo.context_parallel

Ergo document-preserving batch preparation over the shared CP sharder.

## Module Contents

### Functions

| Name                                                                                       | Description                                                               |
| ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------- |
| [`shard_batch`](#nemo_automodel-components-models-ergo-context_parallel-shard_batch)       | Keep IDs global for embedding, and shard auxiliaries in contiguous order. |
| [`shard_te_batch`](#nemo_automodel-components-models-ergo-context_parallel-shard_te_batch) | Keep packed stage-zero IDs global while TE owns the token partition.      |

### API

```python
nemo_automodel.components.models.ergo.context_parallel.shard_batch(
    cp_mesh: torch.distributed.device_mesh.DeviceMesh,
    tp_mesh: torch.distributed.device_mesh.DeviceMesh | None,
    batch: dict[str, torch.Tensor | str],
    loss_mask: torch.Tensor | None = None,
    padding_token_id: int = 0
) -> tuple[collections.abc.Callable[[], contextlib.AbstractContextManager], dict[str, torch.Tensor | str], nemo_automodel.components.distributed.context_parallel.sharder.ShardLayout]
```

Keep IDs global for embedding, and shard auxiliaries in contiguous order.

**Parameters:**

**`cp_mesh`** `DeviceMesh`

Stage-local context mesh. Peers receive the same full batch.

---

**`tp_mesh`** `DeviceMesh | None`

Unused TP mesh passed to the shared sharder.

---

**`batch`** `dict[str, torch.Tensor | str]`

Global IDs, labels, positions, masks \[batch, sequence]. The
attention mask contains document IDs (zero means padding); positions
must be supplied for packed documents. Entries are replaced in place,
but input tensor storage is never modified. Native THD is unsupported.

---

**`loss_mask`** `torch.Tensor | None` — default: None

Optional global supervision weights \[batch, sequence].

---

**`padding_token_id`** `int` — default: 0

Sentinel passed to shared padding machinery.

---

**Returns:** `Callable[[], contextlib.AbstractContextManager]`

Null context factory, prepared batch, and contiguous shard layout.

```python
nemo_automodel.components.models.ergo.context_parallel.shard_te_batch(
    cp_mesh: torch.distributed.device_mesh.DeviceMesh | None,
    tp_mesh: torch.distributed.device_mesh.DeviceMesh | None,
    batch: dict[str, torch.Tensor | str],
    num_chunks: int,
    loss_mask: torch.Tensor | None = None,
    padding_token_id: int = 0
) -> tuple[collections.abc.Callable[[], contextlib.AbstractContextManager], dict[str, torch.Tensor | str | int | bool], nemo_automodel.components.distributed.context_parallel.sharder.ShardLayout | None]
```

Keep packed stage-zero IDs global while TE owns the token partition.

**Parameters:**

**`cp_mesh`** `DeviceMesh | None`

This pipeline stage's context mesh; None means CP1.

---

**`tp_mesh`** `DeviceMesh | None`

Unused TP mesh from the shared sharder contract.

---

**`batch`** `dict[str, torch.Tensor | str]`

IDs, labels and positions \[batch, sequence], real/padded document
lengths \[batch, documents], and optional loss weights of the ID shape.
Tensor storage is retained unchanged; the returned mapping is new.

---

**`num_chunks`** `int`

Number of PP microbatches, evenly dividing the batch dimension.

---

**`loss_mask`** `torch.Tensor | None` — default: None

Optional supervision weights \[batch, sequence].

---

**`padding_token_id`** `int` — default: 0

Filler passed to the shared TE preparation.

---

**Returns:** `Callable[[], contextlib.AbstractContextManager]`

Null context, prepared mapping and single-chunk layout (None for multiple