> 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.deepseek_v41.processing

DeepSeek-V4.1 image sizing, row-major token spans, and standard chat processor.

## Module Contents

### Classes

| Name                                                                                                       | Description                                                                |
| ---------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| [`DeepseekV41ImageInput`](#nemo_automodel-components-models-deepseek_v41-processing-DeepseekV41ImageInput) | One image's patches and placement in a padded text batch.                  |
| [`DeepseekV41Processor`](#nemo_automodel-components-models-deepseek_v41-processing-DeepseekV41Processor)   | Processor for V4.1 text and local image SFT, using the released chat mode. |
| [`_ImageGrid`](#nemo_automodel-components-models-deepseek_v41-processing-_ImageGrid)                       | -                                                                          |
| [`_ImageRecord`](#nemo_automodel-components-models-deepseek_v41-processing-_ImageRecord)                   | -                                                                          |

### Functions

| Name                                                                                                           | Description                                                                   |
| -------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
| [`_load_image`](#nemo_automodel-components-models-deepseek_v41-processing-_load_image)                         | Load the local/PIL/byte image forms emitted by the VLM datasets.              |
| [`_plan_image_grid`](#nemo_automodel-components-models-deepseek_v41-processing-_plan_image_grid)               | Apply the released aspect-preserving resize and exact row-major token budget. |
| [`_preprocess_image`](#nemo_automodel-components-models-deepseek_v41-processing-_preprocess_image)             | Load, resize, normalize, and flatten one image into RGB patches.              |
| [`image_inputs_from_batch`](#nemo_automodel-components-models-deepseek_v41-processing-image_inputs_from_batch) | Validate and partition processor tensors into ordered image inputs.           |

### Data

[`IMAGE_PLACEHOLDER`](#nemo_automodel-components-models-deepseek_v41-processing-IMAGE_PLACEHOLDER)

[`TEXT`](#nemo_automodel-components-models-deepseek_v41-processing-TEXT)

[`_ASSISTANT`](#nemo_automodel-components-models-deepseek_v41-processing-_ASSISTANT)

[`_BOS`](#nemo_automodel-components-models-deepseek_v41-processing-_BOS)

[`_EOS`](#nemo_automodel-components-models-deepseek_v41-processing-_EOS)

[`_LABEL_CHAT_TEMPLATE`](#nemo_automodel-components-models-deepseek_v41-processing-_LABEL_CHAT_TEMPLATE)

[`_SYSTEM`](#nemo_automodel-components-models-deepseek_v41-processing-_SYSTEM)

[`_USER`](#nemo_automodel-components-models-deepseek_v41-processing-_USER)

### API

```python
class nemo_automodel.components.models.deepseek_v41.processing.DeepseekV41ImageInput(
    batch_index: int,
    start: int,
    patches: torch.Tensor,
    n_vit_h: int,
    n_vit_w: int,
    types: torch.Tensor
)
```

Dataclass

One image's patches and placement in a padded text batch.

**`batch_index`** `int`

---

**`n_vit_h`** `int`

---

**`n_vit_w`** `int`

---

**`patches`** `Tensor`

---

**`start`** `int`

---

**`types`** `Tensor`

---

```python
class nemo_automodel.components.models.deepseek_v41.processing.DeepseekV41Processor(
    tokenizer: transformers.PreTrainedTokenizerFast,
    config: nemo_automodel.components.models.deepseek_v41.config.DeepseekV41Config
)
```

**Bases:** `ProcessorMixin`

Processor for V4.1 text and local image SFT, using the released chat mode.

Tool schemas/calls, reasoning traces, and internal task formatting must be
encoded with DeepSeek's full encoder before calling this processor on text.
`apply_chat_template` rejects those fields instead of silently losing them.

**Parameters:**

**`tokenizer`** `PreTrainedTokenizerFast`

Fast tokenizer containing the configured image placeholder.

---

**`config`** `DeepseekV41Config`

Typed checkpoint configuration with image and vision settings.

---

**`attributes`** `= ['tokenizer']`

---

**`tokenizer_class`** `= 'AutoTokenizer'`

---

```python
nemo_automodel.components.models.deepseek_v41.processing.DeepseekV41Processor.__call__(
    text: str | list[str],
    images: Image.Image | list[Image.Image] | list[list[Image.Image]] | None = None,
    return_tensors: str | None = None,
    padding: bool | str = False,
    truncation: bool = False,
    max_length: int | None = None,
    kwargs: typing.Any = {}
) -> transformers.feature_extraction_utils.BatchFeature
```

Expand image placeholders and concatenate patches without pseudo token IDs.

**Parameters:**

**`text`** `str | list[str]`

Rendered prompt or prompts containing one image placeholder per image.

---

**`images`** `Image.Image | list[Image.Image] | list[list[Image.Image]] | None` — default: None

PIL images grouped per prompt, or one prompt's flat image list.

---

**`return_tensors`** `str | None` — default: None

`"pt"` for PyTorch token tensors or None for token lists.

---

**`padding`** `bool | str` — default: False

False, True/`"longest"`, or `"max_length"`; padding is on the right.

---

**`truncation`** `bool` — default: False

Whether max\_length removes trailing text and complete trailing images.

---

**`max_length`** `int | None` — default: None

Token limit after image expansion. Cutting through an image raises.

---

**`**kwargs`** `Any` — default: \{}

Hugging Face tokenizer keyword arguments.

---

**Returns:** `BatchFeature`

BatchFeature with input\_ids/attention\_mask/vision\_token\_types of shape

```python
nemo_automodel.components.models.deepseek_v41.processing.DeepseekV41Processor._render_chat(
    messages: collections.abc.Sequence[dict[str, typing.Any]],
    add_generation_prompt: bool
) -> tuple[str, list[Image.Image]]
```

Render the standard-chat subset of DeepSeek's released encoding.py.

```python
nemo_automodel.components.models.deepseek_v41.processing.DeepseekV41Processor.apply_chat_template(
    conversation: collections.abc.Sequence[dict[str, typing.Any]] | collections.abc.Sequence[collections.abc.Sequence[dict[str, typing.Any]]],
    tokenize: bool = False,
    return_dict: bool = False,
    return_tensors: str | None = None,
    add_generation_prompt: bool = True,
    processor_kwargs: dict[str, typing.Any] | None = None,
    kwargs: typing.Any = {}
) -> str | list[str] | list[list[int]] | torch.Tensor | transformers.feature_extraction_utils.BatchFeature
```

Render standard chat-mode system/user/assistant messages and local images.

**Parameters:**

**`conversation`** `Sequence[dict[str, Any]] | Sequence[Sequence[dict[str, Any]]]`

Hugging Face chat messages or batches thereof, using
string content or ordered text/image content blocks.

---

**`tokenize`** `bool` — default: False

Whether to expand images and tokenize the rendered prompts.

---

**`return_dict`** `bool` — default: False

Whether tokenization returns the complete processor batch.

---

**`return_tensors`** `str | None` — default: None

None or `"pt"` for the tokenization result.

---

**`add_generation_prompt`** `bool` — default: True

Append the assistant header after a final user
or mid-conversation system message, matching official chat mode.

---

**`processor_kwargs`** `dict[str, Any] | None` — default: None

Arguments forwarded to the image/token processor.

---

**`**kwargs`** `Any` — default: \{}

Hugging Face chat-template options. Non-chat modes and tools
raise explicitly; pre-render them with the full official encoder.

---

**Returns:** `str | list[str] | list[list[int]] | torch.Tensor | BatchFeature`

Prompt string(s), token IDs \[batch, sequence], or BatchFeature with

```python
nemo_automodel.components.models.deepseek_v41.processing.DeepseekV41Processor.from_pretrained(
    pretrained_model_name_or_path: str | pathlib.Path,
    kwargs: typing.Any = {}
) -> nemo_automodel.components.models.deepseek_v41.processing.DeepseekV41Processor
```

classmethod

Load the checkpoint's fast tokenizer and nested vision configuration.

```python
nemo_automodel.components.models.deepseek_v41.processing.DeepseekV41Processor.save_pretrained(
    save_directory: str | pathlib.Path,
    kwargs: typing.Any = {}
) -> list[str]
```

Persist the model's image settings alongside the tokenizer and processor.

**Parameters:**

**`save_directory`** `str | Path`

Output directory for the Hugging Face artifacts.

---

**`**kwargs`** `Any` — default: \{}

Hugging Face ProcessorMixin save options.

---

**Returns:** `list[str]`

Processor artifact paths returned by ProcessorMixin.save\_pretrained.

```python
class nemo_automodel.components.models.deepseek_v41.processing._ImageGrid(
    height: int,
    width: int,
    vit_height: int,
    vit_width: int,
    llm_height: int,
    llm_width: int
)
```

Dataclass

**`height`** `int`

---

**`llm_height`** `int`

---

**`llm_width`** `int`

---

**`vit_height`** `int`

---

**`vit_width`** `int`

---

**`width`** `int`

---

```python
class nemo_automodel.components.models.deepseek_v41.processing._ImageRecord
```

**Bases:** `typing.TypedDict`

**`bytes`** `bytes`

---

**`image`** `Image`

---

**`path`** `str`

---

```python
nemo_automodel.components.models.deepseek_v41.processing._load_image(
    value: Image.Image | str | pathlib.Path | bytes | nemo_automodel.components.models.deepseek_v41.processing._ImageRecord
) -> Image.Image
```

Load the local/PIL/byte image forms emitted by the VLM datasets.

```python
nemo_automodel.components.models.deepseek_v41.processing._plan_image_grid(
    width: int,
    height: int,
    config: nemo_automodel.components.models.deepseek_v41.config.DeepseekV41VisionConfig
) -> nemo_automodel.components.models.deepseek_v41.processing._ImageGrid
```

Apply the released aspect-preserving resize and exact row-major token budget.

```python
nemo_automodel.components.models.deepseek_v41.processing._preprocess_image(
    value: Image.Image | str | pathlib.Path | bytes | nemo_automodel.components.models.deepseek_v41.processing._ImageRecord,
    config: nemo_automodel.components.models.deepseek_v41.config.DeepseekV41VisionConfig
) -> tuple[torch.Tensor, nemo_automodel.components.models.deepseek_v41.processing._ImageGrid]
```

Load, resize, normalize, and flatten one image into RGB patches.

**Returns:** `torch.Tensor`

BF16 patches of shape \[vit\_height \* vit\_width, 3, patch\_size, patch\_size]

```python
nemo_automodel.components.models.deepseek_v41.processing.image_inputs_from_batch(
    pixel_values: torch.Tensor,
    image_grid_hws: torch.Tensor,
    vision_token_types: torch.Tensor,
    downsample_ratio: int
) -> tuple[nemo_automodel.components.models.deepseek_v41.processing.DeepseekV41ImageInput, ...]
```

Validate and partition processor tensors into ordered image inputs.

**Parameters:**

**`pixel_values`** `torch.Tensor`

Tensor of shape \[all\_patches, 3, patch\_size, patch\_size],
concatenated in batch-row then image-span order.

---

**`image_grid_hws`** `torch.Tensor`

Integer tensor of shape \[images, 2] storing ViT height
and width for each image in the same order.

---

**`vision_token_types`** `torch.Tensor`

Integer tensor of shape \[batch, sequence], with
TEXT=-1 outside complete image spans and types 0 through 3 inside.

---

**`downsample_ratio`** `int`

Configured spatial ratio mapping the ViT grid to
the language model's image rows and columns.

---

**Returns:** `DeepseekV41ImageInput`

Ordered image records. Each patches/types tensor is a read-only view

```python
nemo_automodel.components.models.deepseek_v41.processing.IMAGE_PLACEHOLDER = '<｜deepseek_image｜>'
```

```python
nemo_automodel.components.models.deepseek_v41.processing.TEXT = -1
```

```python
nemo_automodel.components.models.deepseek_v41.processing._ASSISTANT = '<｜Assistant｜></think>'
```

```python
nemo_automodel.components.models.deepseek_v41.processing._BOS = '<｜begin▁of▁sentence｜>'
```

```python
nemo_automodel.components.models.deepseek_v41.processing._EOS = '<｜end▁of▁sentence｜>'
```

```python
nemo_automodel.components.models.deepseek_v41.processing._LABEL_CHAT_TEMPLATE = "{%- if messages %}{{- bos_token }}{%- endif -%}\n{%- for message in messages -%...
```

```python
nemo_automodel.components.models.deepseek_v41.processing._SYSTEM = '<｜System｜>'
```

```python
nemo_automodel.components.models.deepseek_v41.processing._USER = '<｜User｜>'
```