#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/pp_doclayout_v4/modular_pp_doclayout_v4.py.
#               Do NOT edit this file manually as any edits will be overwritten by the generation of
#             the file from the modular. If any change should be done, please apply the change to the
#                          modular_pp_doclayout_v4.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 The PaddlePaddle Team and The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import math
import warnings
from collections.abc import Callable
from dataclasses import dataclass

import torch
import torch.nn.functional as F
from torch import Tensor, nn

from ... import initialization as init
from ...activations import ACT2CLS, ACT2FN
from ...backbone_utils import load_backbone
from ...integrations import use_kernel_forward_from_hub
from ...modeling_outputs import BaseModelOutput
from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel
from ...processing_utils import Unpack
from ...pytorch_utils import compile_compatible_method_lru_cache
from ...utils import ModelOutput, TransformersKwargs, auto_docstring, torch_compilable_check, torch_int
from ...utils.generic import can_return_tuple, merge_with_config_defaults
from ...utils.output_capturing import capture_outputs
from .configuration_pp_doclayout_v4 import PPDocLayoutV4Config


@use_kernel_forward_from_hub("MultiScaleDeformableAttention")
class MultiScaleDeformableAttention(nn.Module):
    def forward(
        self,
        value: Tensor,
        value_spatial_shapes: Tensor,
        value_spatial_shapes_list: list[tuple],
        level_start_index: Tensor,
        sampling_locations: Tensor,
        attention_weights: Tensor,
        im2col_step: int,
    ):
        batch_size, _, num_heads, hidden_dim = value.shape
        _, num_queries, num_heads, num_levels, num_points, _ = sampling_locations.shape
        value_list = value.split([height * width for height, width in value_spatial_shapes_list], dim=1)
        sampling_grids = 2 * sampling_locations - 1
        sampling_value_list = []
        for level_id, (height, width) in enumerate(value_spatial_shapes_list):
            # batch_size, height*width, num_heads, hidden_dim
            # -> batch_size, height*width, num_heads*hidden_dim
            # -> batch_size, num_heads*hidden_dim, height*width
            # -> batch_size*num_heads, hidden_dim, height, width
            value_l_ = (
                value_list[level_id]
                .flatten(2)
                .transpose(1, 2)
                .reshape(batch_size * num_heads, hidden_dim, height, width)
            )
            # batch_size, num_queries, num_heads, num_points, 2
            # -> batch_size, num_heads, num_queries, num_points, 2
            # -> batch_size*num_heads, num_queries, num_points, 2
            sampling_grid_l_ = sampling_grids[:, :, :, level_id].transpose(1, 2).flatten(0, 1)
            # batch_size*num_heads, hidden_dim, num_queries, num_points
            sampling_value_l_ = nn.functional.grid_sample(
                value_l_,
                sampling_grid_l_,
                mode="bilinear",
                padding_mode="zeros",
                align_corners=False,
            )
            sampling_value_list.append(sampling_value_l_)
        # (batch_size, num_queries, num_heads, num_levels, num_points)
        # -> (batch_size, num_heads, num_queries, num_levels, num_points)
        # -> (batch_size, num_heads, 1, num_queries, num_levels*num_points)
        attention_weights = attention_weights.transpose(1, 2).reshape(
            batch_size * num_heads, 1, num_queries, num_levels * num_points
        )
        output = (
            (torch.stack(sampling_value_list, dim=-2).flatten(-2) * attention_weights)
            .sum(-1)
            .view(batch_size, num_heads * hidden_dim, num_queries)
        )
        return output.transpose(1, 2).contiguous()


class PPDocLayoutV4MultiscaleDeformableAttention(nn.Module):
    """
    Multiscale deformable attention as proposed in Deformable DETR.
    """

    def __init__(self, config: PPDocLayoutV4Config, num_heads: int, n_points: int):
        super().__init__()

        self.attn = MultiScaleDeformableAttention()

        if config.d_model % num_heads != 0:
            raise ValueError(
                f"embed_dim (d_model) must be divisible by num_heads, but got {config.d_model} and {num_heads}"
            )
        dim_per_head = config.d_model // num_heads
        # check if dim_per_head is power of 2
        if not ((dim_per_head & (dim_per_head - 1) == 0) and dim_per_head != 0):
            warnings.warn(
                "You'd better set embed_dim (d_model) in PPDocLayoutV4MultiscaleDeformableAttention to make the"
                " dimension of each attention head a power of 2 which is more efficient in the authors' CUDA"
                " implementation."
            )

        self.im2col_step = 64

        self.d_model = config.d_model
        self.n_levels = config.num_feature_levels
        self.n_heads = num_heads
        self.n_points = n_points

        self.sampling_offsets = nn.Linear(config.d_model, num_heads * self.n_levels * n_points * 2)
        self.attention_weights = nn.Linear(config.d_model, num_heads * self.n_levels * n_points)
        self.value_proj = nn.Linear(config.d_model, config.d_model)
        self.output_proj = nn.Linear(config.d_model, config.d_model)

        self.disable_custom_kernels = config.disable_custom_kernels

    def forward(
        self,
        hidden_states: torch.Tensor,
        attention_mask: torch.Tensor | None = None,
        encoder_hidden_states=None,
        encoder_attention_mask=None,
        position_embeddings: torch.Tensor | None = None,
        reference_points=None,
        spatial_shapes=None,
        spatial_shapes_list=None,
        level_start_index=None,
        **kwargs: Unpack[TransformersKwargs],
    ) -> tuple[torch.Tensor, torch.Tensor]:
        # add position embeddings to the hidden states before projecting to queries and keys
        if position_embeddings is not None:
            hidden_states = hidden_states + position_embeddings

        batch_size, num_queries, _ = hidden_states.shape
        batch_size, sequence_length, _ = encoder_hidden_states.shape
        total_elements = sum(height * width for height, width in spatial_shapes_list)
        torch_compilable_check(
            total_elements == sequence_length,
            "Make sure to align the spatial shapes with the sequence length of the encoder hidden states",
        )

        value = self.value_proj(encoder_hidden_states)
        if attention_mask is not None:
            # we invert the attention_mask
            value = value.masked_fill(~attention_mask[..., None], float(0))
        value = value.view(batch_size, sequence_length, self.n_heads, self.d_model // self.n_heads)
        sampling_offsets = self.sampling_offsets(hidden_states).view(
            batch_size, num_queries, self.n_heads, self.n_levels, self.n_points, 2
        )
        attention_weights = self.attention_weights(hidden_states).view(
            batch_size, num_queries, self.n_heads, self.n_levels * self.n_points
        )
        attention_weights = F.softmax(attention_weights, -1).view(
            batch_size, num_queries, self.n_heads, self.n_levels, self.n_points
        )
        # batch_size, num_queries, n_heads, n_levels, n_points, 2
        num_coordinates = reference_points.shape[-1]
        if num_coordinates == 2:
            offset_normalizer = torch.stack([spatial_shapes[..., 1], spatial_shapes[..., 0]], -1)
            sampling_locations = (
                reference_points[:, :, None, :, None, :]
                + sampling_offsets / offset_normalizer[None, None, None, :, None, :]
            )
        elif num_coordinates == 4:
            sampling_locations = (
                reference_points[:, :, None, :, None, :2]
                + sampling_offsets / self.n_points * reference_points[:, :, None, :, None, 2:] * 0.5
            )
        else:
            raise ValueError(f"Last dim of reference_points must be 2 or 4, but got {reference_points.shape[-1]}")

        output = self.attn(
            value,
            spatial_shapes,
            spatial_shapes_list,
            level_start_index,
            sampling_locations,
            attention_weights,
            self.im2col_step,
        )

        output = self.output_proj(output)

        return output, attention_weights


class PPDocLayoutV4FrozenBatchNorm2d(nn.Module):
    """
    BatchNorm2d where the batch statistics and the affine parameters are fixed.

    Copy-paste from torchvision.misc.ops with added eps before rsqrt, without which any other models than
    torchvision.models.resnet[18,34,50,101] produce nans.
    """

    def __init__(self, n):
        super().__init__()
        self.weight = nn.Buffer(torch.ones(n))
        self.bias = nn.Buffer(torch.zeros(n))
        self.running_mean = nn.Buffer(torch.zeros(n))
        self.running_var = nn.Buffer(torch.ones(n))

    def _load_from_state_dict(
        self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs
    ):
        num_batches_tracked_key = prefix + "num_batches_tracked"
        if num_batches_tracked_key in state_dict:
            del state_dict[num_batches_tracked_key]

        super()._load_from_state_dict(
            state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs
        )

    def forward(self, x):
        # move reshapes to the beginning
        # to make it user-friendly
        weight = self.weight.reshape(1, -1, 1, 1)
        bias = self.bias.reshape(1, -1, 1, 1)
        running_var = self.running_var.reshape(1, -1, 1, 1)
        running_mean = self.running_mean.reshape(1, -1, 1, 1)
        epsilon = 1e-5
        scale = weight * (running_var + epsilon).rsqrt()
        bias = bias - running_mean * scale
        return x * scale + bias


def replace_batch_norm(model):
    r"""
    Recursively replace all `torch.nn.BatchNorm2d` with `PPDocLayoutV4FrozenBatchNorm2d`.

    Args:
        model (torch.nn.Module):
            input model
    """
    for name, module in model.named_children():
        if isinstance(module, nn.BatchNorm2d):
            new_module = PPDocLayoutV4FrozenBatchNorm2d(module.num_features)

            if module.weight.device != torch.device("meta"):
                new_module.weight.copy_(module.weight)
                new_module.bias.copy_(module.bias)
                new_module.running_mean.copy_(module.running_mean)
                new_module.running_var.copy_(module.running_var)

            model._modules[name] = new_module

        if len(list(module.children())) > 0:
            replace_batch_norm(module)


class PPDocLayoutV4ConvEncoder(nn.Module):
    """
    Convolutional backbone using the modeling_pp_doclayout_v4_resnet.py.

    nn.BatchNorm2d layers are replaced by PPDocLayoutV4FrozenBatchNorm2d as defined above.
    https://github.com/lyuwenyu/RT-DETR/blob/main/PPDocLayoutV4_pytorch/src/nn/backbone/presnet.py#L142
    """

    def __init__(self, config):
        super().__init__()

        backbone = load_backbone(config)

        if config.freeze_backbone_batch_norms:
            # replace batch norm by frozen batch norm
            with torch.no_grad():
                replace_batch_norm(backbone)
        self.model = backbone
        self.intermediate_channel_sizes = self.model.channels

    def forward(self, pixel_values: torch.Tensor, pixel_mask: torch.Tensor):
        # send pixel_values through the model to get list of feature maps
        features = self.model(pixel_values).feature_maps

        out = []
        for feature_map in features:
            # downsample pixel_mask to match shape of corresponding feature_map
            mask = nn.functional.interpolate(pixel_mask[None].float(), size=feature_map.shape[-2:]).to(torch.bool)[0]
            out.append((feature_map, mask))
        return out


class PPDocLayoutV4MLPPredictionHead(nn.Module):
    """
    Very simple multi-layer perceptron (MLP, also called FFN), used to predict the normalized center coordinates,
    height and width of a bounding box w.r.t. an image.

    """

    def __init__(self, input_dim, hidden_dim, output_dim, num_layers):
        super().__init__()
        self.num_layers = num_layers
        h = [hidden_dim] * (num_layers - 1)
        self.layers = nn.ModuleList(nn.Linear(n, k) for n, k in zip([input_dim] + h, h + [output_dim]))

    def forward(self, x):
        for i, layer in enumerate(self.layers):
            x = nn.functional.relu(layer(x)) if i < self.num_layers - 1 else layer(x)
        return x


class PPDocLayoutV4GlobalPointer(nn.Module):
    """
    Pairwise scorer shared by both reading order heads.

    With `antisymmetric=True` (the relative order head) the scores satisfy `logits[i, j] == -logits[j, i]`, so a
    positive score means "read `i` before `j`". With `antisymmetric=False` (the successor head) only self loops are
    suppressed, and a positive score means "`j` directly follows `i`".
    """

    def __init__(self, config: PPDocLayoutV4Config, antisymmetric: bool):
        super().__init__()
        self.head_dim = config.global_pointer_head_size
        self.scaling = self.head_dim**-0.5
        self.antisymmetric = antisymmetric
        self.q_proj = nn.Linear(config.d_model, self.head_dim)
        self.k_proj = nn.Linear(config.d_model, self.head_dim)
        self.dropout = nn.Dropout(config.gp_dropout_value)
        self.eye = nn.Buffer(torch.eye(config.num_queries), persistent=False)

    def forward(self, inputs: torch.Tensor) -> torch.Tensor:
        queries = self.dropout(self.q_proj(inputs))
        keys = self.dropout(self.k_proj(inputs))

        logits = (queries @ keys.transpose(-2, -1)) * self.scaling
        if self.antisymmetric:
            return logits - logits.transpose(-2, -1)
        # Soft `-inf` masking the diagonal so that a query is never its own successor. Kept below the fp16 maximum
        # of 65504 to stay finite in half precision, where `-inf` would leak `NaN` into `exp`-based sigmoids.
        return logits - self.eye * 1e4


class PPDocLayoutV4S2RFusion(nn.Module):
    """
    Gated fusion of the successor matrix's transitive closure into the relative order logits:
    `closure_weight * antisymmetrize(closure(successor)) + relative_weight * relative` (S2R = "Successor to
    Relation", from PaddlePaddle).

    `s2r_closure_weight_init=0.0` starts the module out identical to the relative logits alone. `relative_weight`
    is a plain float that is never learned, so checkpoints only carry `closure_weight` (named `a` upstream).
    """

    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__()
        self.steps = config.s2r_steps
        self.damping = config.s2r_damping
        self.closure_weight = nn.Parameter(torch.full((1,), config.s2r_closure_weight_init))
        self.relative_weight = 1.0
        self.one_minus_eye = nn.Buffer(1.0 - torch.eye(config.num_queries), persistent=False)

    def forward(self, relative_logits: torch.Tensor, successor_logits: torch.Tensor) -> torch.Tensor:
        # Soft directed adjacency, where adjacency[i, j] approximates P(i directly precedes j).
        adjacency = successor_logits.sigmoid() * self.one_minus_eye.to(successor_logits.dtype)
        # Clamping the row sums from below at 1 damps dense rows without amplifying weak edges or terminal nodes.
        adjacency = adjacency / adjacency.sum(-1, keepdim=True).clamp(min=1.0)

        # Soft transitive closure: sum over k of damping^(k - 1) * adjacency^k.
        closure = adjacency
        power = adjacency
        for _ in range(self.steps - 1):
            power = self.damping * torch.bmm(adjacency, power)
            closure = closure + power

        return self.closure_weight * (closure - closure.transpose(-2, -1)) + self.relative_weight * relative_logits


class PPDocLayoutV4SuccessorOrderHead(nn.Module):
    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__()
        self.proj = nn.ModuleList([nn.Linear(config.d_model, config.d_model) for _ in range(config.decoder_layers)])
        self.global_pointer = PPDocLayoutV4GlobalPointer(config, antisymmetric=False)

    def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
        return self.global_pointer(self.proj[-1](hidden_states))


class PPDocLayoutV4RelativeOrderHead(nn.Module):
    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__()
        self.proj = nn.ModuleList([nn.Linear(config.d_model, config.d_model) for _ in range(config.decoder_layers)])
        self.global_pointer = PPDocLayoutV4GlobalPointer(config, antisymmetric=True)
        self.s2r_fusion = PPDocLayoutV4S2RFusion(config)

    def forward(self, hidden_states: torch.Tensor, successor_logits: torch.Tensor) -> torch.Tensor:
        relative_logits = self.global_pointer(self.proj[-1](hidden_states))
        return self.s2r_fusion(relative_logits, successor_logits)


@auto_docstring
class PPDocLayoutV4PreTrainedModel(PreTrainedModel):
    config: PPDocLayoutV4Config
    base_model_prefix = "model"
    main_input_name = "pixel_values"
    input_modalities = ("image",)
    _no_split_modules = [r"PPDocLayoutV4HybridEncoder", r"PPDocLayoutV4DecoderLayer"]
    _supports_sdpa = True
    _supports_flash_attn = True
    _supports_attention_backend = True
    _supports_flex_attn = True

    @torch.no_grad()
    def _init_weights(self, module):
        """Initialize the weights"""
        super()._init_weights(module)
        if isinstance(module, PPDocLayoutV4MultiscaleDeformableAttention):
            init.constant_(module.sampling_offsets.weight, 0.0)
            default_dtype = torch.get_default_dtype()
            thetas = torch.arange(module.n_heads, dtype=torch.int64).to(default_dtype) * (
                2.0 * math.pi / module.n_heads
            )
            grid_init = torch.stack([thetas.cos(), thetas.sin()], -1)
            grid_init = (
                (grid_init / grid_init.abs().max(-1, keepdim=True)[0])
                .view(module.n_heads, 1, 1, 2)
                .repeat(1, module.n_levels, module.n_points, 1)
            )
            for i in range(module.n_points):
                grid_init[:, :, i, :] *= i + 1

            init.copy_(module.sampling_offsets.bias, grid_init.view(-1))
            init.constant_(module.attention_weights.weight, 0.0)
            init.constant_(module.attention_weights.bias, 0.0)
            init.xavier_uniform_(module.value_proj.weight)
            init.constant_(module.value_proj.bias, 0.0)
            init.xavier_uniform_(module.output_proj.weight)
            init.constant_(module.output_proj.bias, 0.0)

        elif isinstance(module, (PPDocLayoutV4Model, PPDocLayoutV4Decoder)):
            class_heads = [module.enc_score_head] if isinstance(module, PPDocLayoutV4Model) else module.class_embed
            prior_prob = self.config.initializer_bias_prior_prob
            for class_head in class_heads:
                init.xavier_uniform_(class_head.weight)
                init.constant_(class_head.bias, float(-math.log((1 - prior_prob) / prior_prob)))

        elif isinstance(module, PPDocLayoutV4S2RFusion):
            init.constant_(module.closure_weight, self.config.s2r_closure_weight_init)
            init.copy_(module.one_minus_eye, 1.0 - torch.eye(module.one_minus_eye.shape[0]))

        elif isinstance(module, PPDocLayoutV4GlobalPointer):
            init.copy_(module.eye, torch.eye(module.eye.shape[-1]))

        elif isinstance(module, nn.BatchNorm2d):
            init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
            if module.bias is not None:
                init.zeros_(module.bias)
            if getattr(module, "running_mean", None) is not None:
                init.zeros_(module.running_mean)
                init.ones_(module.running_var)
                init.zeros_(module.num_batches_tracked)


class PPDocLayoutV4MLP(nn.Module):
    def __init__(
        self, config: PPDocLayoutV4Config, hidden_size: int, intermediate_size: int, activation_function: str
    ):
        super().__init__()
        self.fc1 = nn.Linear(hidden_size, intermediate_size)
        self.fc2 = nn.Linear(intermediate_size, hidden_size)
        self.activation_fn = ACT2FN[activation_function]
        self.activation_dropout = config.activation_dropout
        self.dropout = config.dropout

    def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
        hidden_states = self.activation_fn(self.fc1(hidden_states))
        hidden_states = nn.functional.dropout(hidden_states, p=self.activation_dropout, training=self.training)
        hidden_states = self.fc2(hidden_states)
        hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
        return hidden_states


def eager_attention_forward(
    module: nn.Module,
    query: torch.Tensor,
    key: torch.Tensor,
    value: torch.Tensor,
    attention_mask: torch.Tensor | None,
    scaling: float | None = None,
    dropout: float = 0.0,
    **kwargs: Unpack[TransformersKwargs],
):
    if scaling is None:
        scaling = query.size(-1) ** -0.5

    # Take the dot product between "query" and "key" to get the raw attention scores.
    attn_weights = torch.matmul(query, key.transpose(2, 3)) * scaling

    if attention_mask is not None:
        attn_weights = attn_weights + attention_mask

    attn_weights = nn.functional.softmax(attn_weights, dim=-1)
    attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training)

    attn_output = torch.matmul(attn_weights, value)
    attn_output = attn_output.transpose(1, 2).contiguous()

    return attn_output, attn_weights


class PPDocLayoutV4SelfAttention(nn.Module):
    """
    Multi-headed self-attention from 'Attention Is All You Need' paper.

    In PP_DOCLAYOUT_V4, position embeddings are added to both queries and keys (but not values) in self-attention.
    """

    def __init__(
        self,
        config: PPDocLayoutV4Config,
        hidden_size: int,
        num_attention_heads: int,
        dropout: float = 0.0,
        bias: bool = True,
    ):
        super().__init__()
        self.config = config
        self.head_dim = hidden_size // num_attention_heads
        self.scaling = self.head_dim**-0.5
        self.attention_dropout = dropout
        self.is_causal = False

        self.k_proj = nn.Linear(hidden_size, hidden_size, bias=bias)
        self.v_proj = nn.Linear(hidden_size, hidden_size, bias=bias)
        self.q_proj = nn.Linear(hidden_size, hidden_size, bias=bias)
        self.o_proj = nn.Linear(hidden_size, hidden_size, bias=bias)

    def forward(
        self,
        hidden_states: torch.Tensor,
        attention_mask: torch.Tensor | None = None,
        position_embeddings: torch.Tensor | None = None,
        **kwargs: Unpack[TransformersKwargs],
    ) -> tuple[torch.Tensor, torch.Tensor]:
        """
        Position embeddings are added to both queries and keys (but not values).
        """
        input_shape = hidden_states.shape[:-1]
        hidden_shape = (*input_shape, -1, self.head_dim)

        query_key_input = hidden_states + position_embeddings if position_embeddings is not None else hidden_states

        query_states = self.q_proj(query_key_input).view(hidden_shape).transpose(1, 2)
        key_states = self.k_proj(query_key_input).view(hidden_shape).transpose(1, 2)
        value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)

        attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface(
            self.config._attn_implementation, eager_attention_forward
        )

        attn_output, attn_weights = attention_interface(
            self,
            query_states,
            key_states,
            value_states,
            attention_mask,
            dropout=0.0 if not self.training else self.attention_dropout,
            scaling=self.scaling,
            **kwargs,
        )

        attn_output = attn_output.reshape(*input_shape, -1).contiguous()
        attn_output = self.o_proj(attn_output)
        return attn_output, attn_weights


class PPDocLayoutV4ConvNormLayer(nn.Module):
    def __init__(self, config, in_channels, out_channels, kernel_size, stride, padding=None, activation=None):
        super().__init__()
        self.conv = nn.Conv2d(
            in_channels,
            out_channels,
            kernel_size,
            stride,
            padding=(kernel_size - 1) // 2 if padding is None else padding,
            bias=False,
        )
        self.norm = nn.BatchNorm2d(out_channels, config.batch_norm_eps)
        self.activation = nn.Identity() if activation is None else ACT2CLS[activation]()

    def forward(self, hidden_state):
        hidden_state = self.conv(hidden_state)
        hidden_state = self.norm(hidden_state)
        hidden_state = self.activation(hidden_state)
        return hidden_state


class PPDocLayoutV4EncoderLayer(nn.Module):
    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__()
        self.normalize_before = config.normalize_before
        self.hidden_size = config.encoder_hidden_dim

        # self-attention
        self.self_attn = PPDocLayoutV4SelfAttention(
            config=config,
            hidden_size=self.hidden_size,
            num_attention_heads=config.num_attention_heads,
            dropout=config.dropout,
        )
        self.self_attn_layer_norm = nn.LayerNorm(self.hidden_size, eps=config.layer_norm_eps)
        self.dropout = config.dropout
        self.mlp = PPDocLayoutV4MLP(
            config, self.hidden_size, config.encoder_ffn_dim, config.encoder_activation_function
        )
        self.final_layer_norm = nn.LayerNorm(self.hidden_size, eps=config.layer_norm_eps)

    def forward(
        self,
        hidden_states: torch.Tensor,
        attention_mask: torch.Tensor,
        spatial_position_embeddings: torch.Tensor | None = None,
        **kwargs: Unpack[TransformersKwargs],
    ) -> torch.Tensor:
        """
        Args:
            hidden_states (`torch.Tensor`): input to the layer of shape `(batch, seq_len, hidden_size)`
            attention_mask (`torch.Tensor`): attention mask of size
                `(batch, 1, target_len, source_len)` where padding elements are indicated by very large negative
                values.
            spatial_position_embeddings (`torch.Tensor`, *optional*):
                Spatial position embeddings (2D positional encodings of image locations), to be added to both
                the queries and keys in self-attention (but not to values).
        """
        residual = hidden_states
        if self.normalize_before:
            hidden_states = self.self_attn_layer_norm(hidden_states)

        hidden_states, _ = self.self_attn(
            hidden_states=hidden_states,
            attention_mask=attention_mask,
            position_embeddings=spatial_position_embeddings,
            **kwargs,
        )

        hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
        hidden_states = residual + hidden_states
        if not self.normalize_before:
            hidden_states = self.self_attn_layer_norm(hidden_states)

        if self.normalize_before:
            hidden_states = self.final_layer_norm(hidden_states)
        residual = hidden_states

        hidden_states = self.mlp(hidden_states)

        hidden_states = residual + hidden_states
        if not self.normalize_before:
            hidden_states = self.final_layer_norm(hidden_states)

        if self.training:
            if not torch.isfinite(hidden_states).all():
                clamp_value = torch.finfo(hidden_states.dtype).max - 1000
                hidden_states = torch.clamp(hidden_states, min=-clamp_value, max=clamp_value)

        return hidden_states


class PPDocLayoutV4RepVggBlock(nn.Module):
    """
    RepVGG architecture block introduced by the work "RepVGG: Making VGG-style ConvNets Great Again".
    """

    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__()

        activation = config.activation_function
        hidden_channels = int(config.encoder_hidden_dim * config.hidden_expansion)
        self.conv1 = PPDocLayoutV4ConvNormLayer(config, hidden_channels, hidden_channels, 3, 1, padding=1)
        self.conv2 = PPDocLayoutV4ConvNormLayer(config, hidden_channels, hidden_channels, 1, 1, padding=0)
        self.activation = nn.Identity() if activation is None else ACT2CLS[activation]()

    def forward(self, x):
        y = self.conv1(x) + self.conv2(x)
        return self.activation(y)


class PPDocLayoutV4CSPRepLayer(nn.Module):
    """
    Cross Stage Partial (CSP) network layer with RepVGG blocks.
    """

    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__()

        in_channels = config.encoder_hidden_dim * 2
        out_channels = config.encoder_hidden_dim
        num_blocks = 3
        activation = config.activation_function

        hidden_channels = int(out_channels * config.hidden_expansion)
        self.conv1 = PPDocLayoutV4ConvNormLayer(config, in_channels, hidden_channels, 1, 1, activation=activation)
        self.conv2 = PPDocLayoutV4ConvNormLayer(config, in_channels, hidden_channels, 1, 1, activation=activation)
        self.bottlenecks = nn.Sequential(*[PPDocLayoutV4RepVggBlock(config) for _ in range(num_blocks)])
        if hidden_channels != out_channels:
            self.conv3 = PPDocLayoutV4ConvNormLayer(config, hidden_channels, out_channels, 1, 1, activation=activation)
        else:
            self.conv3 = nn.Identity()

    def forward(self, hidden_state):
        hidden_state_1 = self.conv1(hidden_state)
        hidden_state_1 = self.bottlenecks(hidden_state_1)
        hidden_state_2 = self.conv2(hidden_state)
        return self.conv3(hidden_state_1 + hidden_state_2)


def build_2d_sinusoidal_position_embedding(
    height: int,
    width: int,
    embed_dim: int = 256,
    temperature: float = 10000.0,
    cls_token: bool = False,
    device: torch.device | None = None,
    dtype: torch.dtype = torch.float32,
) -> torch.Tensor:
    """2D sinusoidal position embeddings for an image patch grid.

    Each (h, w) position gets an ``embed_dim``-dimensional vector laid out as
    ``[sin_h | cos_h | sin_w | cos_w]``, with row-major (H-outer) patch ordering.

    Args:
        height: Grid height in patches.
        width: Grid width in patches.
        embed_dim: Total embedding dimension; must be divisible by 4.
        temperature: Base for the frequency decay.
        cls_token: If `True`, prepend a zero row for a CLS token.
        device: Target device; defaults to CPU.
        dtype: Output dtype; frequency arithmetic uses float64 internally.

    Returns:
        Tensor of shape ``(height * width [+1], embed_dim)``.
    """
    if embed_dim % 4 != 0:
        raise ValueError(f"`embed_dim` must be divisible by 4, got {embed_dim}")

    pos_dim = embed_dim // 4
    # mps doesn't support float64
    compute_dtype = torch.float64 if device is None or device.type != "mps" else torch.float32
    omega = torch.arange(pos_dim, dtype=compute_dtype, device=device) / pos_dim
    omega = 1.0 / temperature**omega  # (D/4,)

    grid_h = torch.arange(height, dtype=compute_dtype, device=device)
    grid_w = torch.arange(width, dtype=compute_dtype, device=device)
    grid_h, grid_w = torch.meshgrid(grid_h, grid_w, indexing="ij")  # (H, W) each

    emb_h = grid_h.flatten().outer(omega)  # (H*W, D/4)
    emb_w = grid_w.flatten().outer(omega)  # (H*W, D/4)

    pos_embed = torch.cat([emb_h.sin(), emb_h.cos(), emb_w.sin(), emb_w.cos()], dim=1)

    if cls_token:
        pos_embed = torch.cat([torch.zeros(1, embed_dim, dtype=compute_dtype, device=device), pos_embed], dim=0)

    return pos_embed.to(dtype)


class PPDocLayoutV4SinePositionEmbedding(nn.Module):
    """
    2D sinusoidal position embedding used in RT-DETR hybrid encoder.
    """

    def __init__(self, embed_dim: int = 256, temperature: int = 10000):
        super().__init__()
        self.embed_dim = embed_dim
        self.temperature = temperature

    @staticmethod
    @compile_compatible_method_lru_cache(maxsize=32)
    def _cached_build_2d_sinusoidal_position_embedding(*args, **kwargs) -> torch.Tensor:
        return build_2d_sinusoidal_position_embedding(*args, **kwargs)

    def forward(
        self,
        width: int,
        height: int,
        device: torch.device | str,
        dtype: torch.dtype,
    ) -> torch.Tensor:
        """
        Generate 2D sinusoidal position embeddings.

        Returns:
            Position embeddings of shape (1, height*width, embed_dim)
        """
        return self._cached_build_2d_sinusoidal_position_embedding(
            height=torch_int(height),
            width=torch_int(width),
            embed_dim=self.embed_dim,
            temperature=self.temperature,
            device=device,
            dtype=dtype,
        ).unsqueeze(0)


class PPDocLayoutV4AIFILayer(nn.Module):
    """
    AIFI (Attention-based Intra-scale Feature Interaction) layer used in RT-DETR hybrid encoder.
    """

    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__()
        self.config = config
        self.encoder_hidden_dim = config.encoder_hidden_dim
        self.eval_size = config.eval_size

        self.position_embedding = PPDocLayoutV4SinePositionEmbedding(
            embed_dim=self.encoder_hidden_dim,
            temperature=config.positional_encoding_temperature,
        )
        self.layers = nn.ModuleList([PPDocLayoutV4EncoderLayer(config) for _ in range(config.encoder_layers)])

    def forward(
        self,
        hidden_states: torch.Tensor,
        **kwargs: Unpack[TransformersKwargs],
    ) -> torch.Tensor:
        """
        Args:
            hidden_states (`torch.Tensor` of shape `(batch_size, channels, height, width)`):
                Feature map to process.
        """
        batch_size = hidden_states.shape[0]
        height, width = hidden_states.shape[2:]

        hidden_states = hidden_states.flatten(2).transpose(1, 2)

        if self.training or self.eval_size is None:
            pos_embed = self.position_embedding(
                width=width,
                height=height,
                device=hidden_states.device,
                dtype=hidden_states.dtype,
            )
        else:
            pos_embed = None

        for layer in self.layers:
            hidden_states = layer(
                hidden_states,
                attention_mask=None,
                spatial_position_embeddings=pos_embed,
                **kwargs,
            )

        hidden_states = (
            hidden_states.transpose(1, 2).reshape(batch_size, self.encoder_hidden_dim, height, width).contiguous()
        )

        return hidden_states


class PPDocLayoutV4HybridEncoder(PPDocLayoutV4PreTrainedModel):
    """
    PP-DocLayoutV4 uses the plain RT-DETR hybrid encoder: AIFI, a top-down FPN and a bottom-up PAN. Unlike
    [`PPDocLayoutV3HybridEncoder`] there is no mask feature head, so the stride 4 backbone feature is unused.
    """

    _can_record_outputs = {
        "hidden_states": PPDocLayoutV4AIFILayer,
        "attentions": PPDocLayoutV4SelfAttention,
    }

    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__(config)
        self.config = config
        self.in_channels = config.encoder_in_channels
        self.feat_strides = config.feat_strides
        self.encoder_hidden_dim = config.encoder_hidden_dim
        self.encode_proj_layers = config.encode_proj_layers
        self.positional_encoding_temperature = config.positional_encoding_temperature
        self.eval_size = config.eval_size
        self.out_channels = [self.encoder_hidden_dim for _ in self.in_channels]
        self.out_strides = self.feat_strides
        self.num_fpn_stages = len(self.in_channels) - 1
        self.num_pan_stages = len(self.in_channels) - 1

        # AIFI (Attention-based Intra-scale Feature Interaction) layers
        self.aifi = nn.ModuleList([PPDocLayoutV4AIFILayer(config) for _ in range(len(self.encode_proj_layers))])

        # top-down FPN
        self.lateral_convs = nn.ModuleList()
        self.fpn_blocks = nn.ModuleList()
        for _ in range(self.num_fpn_stages):
            lateral_conv = PPDocLayoutV4ConvNormLayer(
                config,
                in_channels=self.encoder_hidden_dim,
                out_channels=self.encoder_hidden_dim,
                kernel_size=1,
                stride=1,
                activation=config.activation_function,
            )
            fpn_block = PPDocLayoutV4CSPRepLayer(config)
            self.lateral_convs.append(lateral_conv)
            self.fpn_blocks.append(fpn_block)

        # bottom-up PAN
        self.downsample_convs = nn.ModuleList()
        self.pan_blocks = nn.ModuleList()
        for _ in range(self.num_pan_stages):
            downsample_conv = PPDocLayoutV4ConvNormLayer(
                config,
                in_channels=self.encoder_hidden_dim,
                out_channels=self.encoder_hidden_dim,
                kernel_size=3,
                stride=2,
                activation=config.activation_function,
            )
            pan_block = PPDocLayoutV4CSPRepLayer(config)
            self.downsample_convs.append(downsample_conv)
            self.pan_blocks.append(pan_block)

        self.post_init()

    @merge_with_config_defaults
    @capture_outputs(tie_last_hidden_states=False)
    def forward(
        self,
        inputs_embeds=None,
        **kwargs: Unpack[TransformersKwargs],
    ) -> BaseModelOutput:
        r"""
        Args:
            inputs_embeds (`torch.Tensor` of shape `(batch_size, sequence_length, hidden_size)`):
                Flattened feature map (output of the backbone + projection layer) that is passed to the encoder.
        """
        feature_maps = inputs_embeds

        # AIFI: Apply transformer encoder to specified feature levels
        if self.config.encoder_layers > 0:
            for i, enc_ind in enumerate(self.encode_proj_layers):
                feature_maps[enc_ind] = self.aifi[i](feature_maps[enc_ind], **kwargs)

        # top-down FPN
        fpn_feature_maps = [feature_maps[-1]]
        for idx, (lateral_conv, fpn_block) in enumerate(zip(self.lateral_convs, self.fpn_blocks)):
            backbone_feature_map = feature_maps[self.num_fpn_stages - idx - 1]
            top_fpn_feature_map = fpn_feature_maps[-1]
            # apply lateral block
            top_fpn_feature_map = lateral_conv(top_fpn_feature_map)
            fpn_feature_maps[-1] = top_fpn_feature_map
            # apply fpn block
            top_fpn_feature_map = F.interpolate(top_fpn_feature_map, scale_factor=2.0, mode="nearest")
            fused_feature_map = torch.concat([top_fpn_feature_map, backbone_feature_map], dim=1)
            new_fpn_feature_map = fpn_block(fused_feature_map)
            fpn_feature_maps.append(new_fpn_feature_map)

        fpn_feature_maps.reverse()

        # bottom-up PAN
        pan_feature_maps = [fpn_feature_maps[0]]
        for idx, (downsample_conv, pan_block) in enumerate(zip(self.downsample_convs, self.pan_blocks)):
            top_pan_feature_map = pan_feature_maps[-1]
            fpn_feature_map = fpn_feature_maps[idx + 1]
            downsampled_feature_map = downsample_conv(top_pan_feature_map)
            fused_feature_map = torch.concat([downsampled_feature_map, fpn_feature_map], dim=1)
            new_pan_feature_map = pan_block(fused_feature_map)
            pan_feature_maps.append(new_pan_feature_map)

        return BaseModelOutput(last_hidden_state=pan_feature_maps)


@auto_docstring(
    custom_intro="""
    Output type of [`PPDocLayoutV4Decoder`].
    """
)
@dataclass
class PPDocLayoutV4DecoderOutput(ModelOutput):
    r"""
    last_hidden_state (`torch.Tensor` of shape `(batch_size, num_queries, hidden_size)`):
        Sequence of hidden-states at the output of the last layer of the decoder.
    intermediate_hidden_states (`torch.Tensor` of shape `(batch_size, config.decoder_layers, num_queries, hidden_size)`):
        Stacked intermediate hidden states (output of each layer of the decoder).
    intermediate_reference_points (`torch.Tensor` of shape `(batch_size, config.decoder_layers, num_queries, config.num_coords)`):
        Stacked intermediate reference points (refined quads of each layer of the decoder).
    logits (`torch.Tensor` of shape `(batch_size, num_queries, config.num_labels)`):
        Classification logits of the last decoder layer. Unlike RT-DETR, PP-DocLayoutV4 only evaluates its
        classification and reading order heads on the last layer.
    relative_order_logits (`torch.Tensor` of shape `(batch_size, config.num_queries, config.num_queries)`):
        Pairwise relative reading order logits of the last decoder layer, after the optional S2R fusion.
    successor_order_logits (`torch.Tensor` of shape `(batch_size, config.num_queries, config.num_queries)`):
        Pairwise direct successor (ROOR) logits of the last decoder layer.
    """

    last_hidden_state: torch.Tensor | None = None
    intermediate_hidden_states: torch.Tensor | None = None
    intermediate_reference_points: torch.Tensor | None = None
    hidden_states: tuple[torch.Tensor] | None = None
    attentions: tuple[torch.Tensor] | None = None
    cross_attentions: tuple[torch.Tensor] | None = None

    logits: torch.Tensor | None = None
    relative_order_logits: torch.Tensor | None = None
    successor_order_logits: torch.Tensor | None = None


class PPDocLayoutV4DecoderLayer(nn.Module):
    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__()
        self.hidden_size = config.d_model

        # self-attention
        self.self_attn = PPDocLayoutV4SelfAttention(
            config=config,
            hidden_size=self.hidden_size,
            num_attention_heads=config.decoder_attention_heads,
            dropout=config.attention_dropout,
        )
        self.dropout = config.dropout

        self.self_attn_layer_norm = nn.LayerNorm(self.hidden_size, eps=config.layer_norm_eps)
        # cross-attention
        self.encoder_attn = PPDocLayoutV4MultiscaleDeformableAttention(
            config,
            num_heads=config.decoder_attention_heads,
            n_points=config.decoder_n_points,
        )
        self.encoder_attn_layer_norm = nn.LayerNorm(self.hidden_size, eps=config.layer_norm_eps)
        # feedforward neural networks
        self.mlp = PPDocLayoutV4MLP(
            config, self.hidden_size, config.decoder_ffn_dim, config.decoder_activation_function
        )
        self.final_layer_norm = nn.LayerNorm(self.hidden_size, eps=config.layer_norm_eps)

    def forward(
        self,
        hidden_states: torch.Tensor,
        object_queries_position_embeddings: torch.Tensor | None = None,
        reference_points=None,
        spatial_shapes=None,
        spatial_shapes_list=None,
        level_start_index=None,
        encoder_hidden_states: torch.Tensor | None = None,
        encoder_attention_mask: torch.Tensor | None = None,
        **kwargs: Unpack[TransformersKwargs],
    ) -> torch.Tensor:
        """
        Args:
            hidden_states (`torch.Tensor`):
                Input to the layer of shape `(batch, seq_len, hidden_size)`.
            object_queries_position_embeddings (`torch.Tensor`, *optional*):
                Position embeddings for the object query slots. These are added to both queries and keys
                in the self-attention layer (not values).
            reference_points (`torch.Tensor`, *optional*):
                Reference points.
            spatial_shapes (`torch.LongTensor`, *optional*):
                Spatial shapes.
            level_start_index (`torch.LongTensor`, *optional*):
                Level start index.
            encoder_hidden_states (`torch.Tensor`):
                cross attention input to the layer of shape `(batch, seq_len, hidden_size)`
            encoder_attention_mask (`torch.Tensor`): encoder attention mask of size
                `(batch, 1, target_len, source_len)` where padding elements are indicated by very large negative
                values.
        """
        residual = hidden_states

        # Self Attention
        hidden_states, _ = self.self_attn(
            hidden_states=hidden_states,
            attention_mask=encoder_attention_mask,
            position_embeddings=object_queries_position_embeddings,
            **kwargs,
        )

        hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
        hidden_states = residual + hidden_states
        hidden_states = self.self_attn_layer_norm(hidden_states)

        residual = hidden_states

        # Cross-Attention
        hidden_states, _ = self.encoder_attn(
            hidden_states=hidden_states,
            encoder_hidden_states=encoder_hidden_states,
            position_embeddings=object_queries_position_embeddings,
            reference_points=reference_points,
            spatial_shapes=spatial_shapes,
            spatial_shapes_list=spatial_shapes_list,
            level_start_index=level_start_index,
            **kwargs,
        )

        hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
        hidden_states = residual + hidden_states

        hidden_states = self.encoder_attn_layer_norm(hidden_states)

        # Fully Connected
        residual = hidden_states
        hidden_states = self.mlp(hidden_states)
        hidden_states = residual + hidden_states
        hidden_states = self.final_layer_norm(hidden_states)

        return hidden_states


def inverse_sigmoid(x, eps=1e-5):
    x = x.clamp(min=0, max=1)
    x1 = x.clamp(min=eps)
    x2 = (1 - x).clamp(min=eps)
    return torch.log(x1 / x2)


def quad_to_rect(quad: torch.Tensor) -> torch.Tensor:
    """
    Converts the 10 dimensional quad parameterization to `(center_x, center_y, width, height)`.

    `quad` is `[..., 10]` in sigmoid space with the corner offsets shifted by `+0.5`. The returned center is the
    *predicted* `center_x, center_y` rather than the centroid of the corners, and `width, height` are the full extent
    of the offsets. This rect is only used to place the deformable attention sampling grid.
    """
    center = quad[..., :2]
    offsets_x = quad[..., 2::2] - 0.5
    offsets_y = quad[..., 3::2] - 0.5
    width = offsets_x.amax(-1, keepdim=True) - offsets_x.amin(-1, keepdim=True)
    height = offsets_y.amax(-1, keepdim=True) - offsets_y.amin(-1, keepdim=True)
    return torch.cat([center, width, height], dim=-1)


class PPDocLayoutV4Decoder(PPDocLayoutV4PreTrainedModel):
    """
    Main differences to `PPDocLayoutV3Decoder`:
        1. The bbox and class heads are untied from the encoder heads. There is one bbox head per decoder layer, while
           classification and reading order are scored once, after the last layer.
        2. Reference points are `config.num_coords` dimensional quads. Deformable attention samples on the enclosing
           rect of the quad, while the query position embedding consumes the full quad.
        3. There is no mask branch.
    """

    _can_record_outputs = {
        "hidden_states": PPDocLayoutV4DecoderLayer,
        "attentions": PPDocLayoutV4SelfAttention,
        "cross_attentions": PPDocLayoutV4MultiscaleDeformableAttention,
    }

    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__(config)

        self.dropout = config.dropout
        self.layers = nn.ModuleList([PPDocLayoutV4DecoderLayer(config) for _ in range(config.decoder_layers)])

        self.query_pos_head = PPDocLayoutV4MLPPredictionHead(
            config.num_coords, 2 * config.d_model, config.d_model, num_layers=2
        )
        self.bbox_embed = nn.ModuleList(
            [
                PPDocLayoutV4MLPPredictionHead(config.d_model, config.d_model, config.num_coords, num_layers=3)
                for _ in range(config.decoder_layers)
            ]
        )
        # Keep every layer's classification and reading order projection weights for checkpoint compatibility.
        # Earlier layers are only used for auxiliary training losses in PaddleDetection and are unused here:
        # training is not supported, and inference scores only the last layer.
        self.class_embed = nn.ModuleList(
            [nn.Linear(config.d_model, config.num_labels) for _ in range(config.decoder_layers)]
        )

        self.num_queries = config.num_queries
        self.successor_order_head = PPDocLayoutV4SuccessorOrderHead(config)
        self.relative_order_head = PPDocLayoutV4RelativeOrderHead(config)

        # Initialize weights and apply final processing
        self.post_init()

    @auto_docstring
    @merge_with_config_defaults
    @capture_outputs
    def forward(
        self,
        inputs_embeds=None,
        encoder_hidden_states=None,
        encoder_attention_mask=None,
        reference_points=None,
        spatial_shapes=None,
        spatial_shapes_list=None,
        level_start_index=None,
        **kwargs: Unpack[TransformersKwargs],
    ):
        r"""
        inputs_embeds (`torch.Tensor` of shape `(batch_size, num_queries, hidden_size)`):
            The query embeddings that are passed into the decoder.
        encoder_hidden_states (`torch.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention
            of the decoder.
        encoder_attention_mask (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing cross-attention on padding pixel_values of the encoder. Mask values selected
            in `[0, 1]`:
            - 1 for pixels that are real (i.e. **not masked**),
            - 0 for pixels that are padding (i.e. **masked**).
        reference_points (`torch.Tensor` of shape `(batch_size, num_queries, config.num_coords)`):
            Quad reference points in inverse sigmoid space.
        spatial_shapes (`torch.Tensor` of shape `(num_feature_levels, 2)`):
            Spatial shapes of the feature maps.
        spatial_shapes_list (`list[tuple[int, int]]`, *optional*):
            Spatial shapes of the feature maps as a list, kept alongside `spatial_shapes` so that the deformable
            attention can index them without a device synchronization.
        level_start_index (`torch.LongTensor` of shape `(num_feature_levels)`, *optional*):
            Indexes for the start of each feature level. In range `[0, sequence_length]`.
        """
        if inputs_embeds is not None:
            hidden_states = inputs_embeds

        # Only the hidden states and the refined quads are accumulated here: PP-DocLayoutV3 also collects a class,
        # an order and a mask output per layer, none of which PP-DocLayoutV4 produces.
        intermediate = ()
        intermediate_reference_points = ()

        reference_points = F.sigmoid(reference_points)

        for idx, decoder_layer in enumerate(self.layers):
            # Deformable attention samples on the enclosing rect of the quad, while the position embedding below sees
            # the full quad. PP-DocLayoutV3 feeds the same 4 dimensional box to both.
            reference_points_input = quad_to_rect(reference_points).unsqueeze(2)

            hidden_states = decoder_layer(
                hidden_states,
                object_queries_position_embeddings=self.query_pos_head(reference_points),
                encoder_hidden_states=encoder_hidden_states,
                reference_points=reference_points_input,
                spatial_shapes=spatial_shapes,
                spatial_shapes_list=spatial_shapes_list,
                level_start_index=level_start_index,
                encoder_attention_mask=encoder_attention_mask,
                **kwargs,
            )

            # Each layer refines the quads with its own untied bbox head.
            reference_points = F.sigmoid(self.bbox_embed[idx](hidden_states) + inverse_sigmoid(reference_points))

            intermediate += (hidden_states,)
            intermediate_reference_points += (reference_points,)

        # PP-DocLayoutV3 scores every layer from inside the loop, on hidden states passed through a `norm` first.
        # PP-DocLayoutV4 has no such norm and scores the last layer only.
        logits = self.class_embed[-1](hidden_states)
        valid_query = hidden_states[:, -self.num_queries :] if self.num_queries is not None else hidden_states
        # The direct successor branch and its fusion into the relative order logits are new in PP-DocLayoutV4.
        successor_order_logits = self.successor_order_head(valid_query)
        relative_order_logits = self.relative_order_head(valid_query, successor_order_logits)

        return PPDocLayoutV4DecoderOutput(
            last_hidden_state=hidden_states,
            intermediate_hidden_states=torch.stack(intermediate, dim=1),
            intermediate_reference_points=torch.stack(intermediate_reference_points, dim=1),
            logits=logits,
            relative_order_logits=relative_order_logits,
            successor_order_logits=successor_order_logits,
        )


@auto_docstring(
    custom_intro="""
    Output type of [`PPDocLayoutV4Model`].
    """
)
@dataclass
class PPDocLayoutV4ModelOutput(ModelOutput):
    r"""
    last_hidden_state (`torch.Tensor` of shape `(batch_size, num_queries, hidden_size)`):
        Sequence of hidden-states at the output of the last layer of the decoder of the model.
    intermediate_hidden_states (`torch.Tensor` of shape `(batch_size, config.decoder_layers, num_queries, hidden_size)`):
        Stacked intermediate hidden states (output of each layer of the decoder).
    intermediate_reference_points (`torch.Tensor` of shape `(batch_size, config.decoder_layers, num_queries, config.num_coords)`):
        Stacked intermediate reference points (refined quads of each layer of the decoder).
    init_reference_points (`torch.Tensor` of shape `(batch_size, num_queries, config.num_coords)`):
        Initial quad reference points sent through the Transformer decoder.
    enc_topk_logits (`torch.Tensor` of shape `(batch_size, num_queries, config.num_labels)`):
        Class logits of the encoder proposals that were selected as decoder queries.
    enc_topk_bboxes (`torch.Tensor` of shape `(batch_size, num_queries, config.num_coords)`):
        Quads of the encoder proposals that were selected as decoder queries.
    enc_outputs_class (`torch.Tensor` of shape `(batch_size, sequence_length, config.num_labels)`):
        Class logits of every encoder proposal.
    enc_outputs_coord_logits (`torch.Tensor` of shape `(batch_size, sequence_length, config.num_coords)`):
        Quad logits of every encoder proposal.
    denoising_meta_values (`dict`):
        Extra dictionary for the denoising related values.
    logits (`torch.Tensor` of shape `(batch_size, num_queries, config.num_labels)`):
        Classification logits of the last decoder layer.
    relative_order_logits (`torch.Tensor` of shape `(batch_size, config.num_queries, config.num_queries)`):
        Pairwise relative reading order logits, after the optional S2R fusion.
    successor_order_logits (`torch.Tensor` of shape `(batch_size, config.num_queries, config.num_queries)`):
        Pairwise direct successor (ROOR) logits.
    """

    last_hidden_state: torch.Tensor | None = None
    intermediate_hidden_states: torch.Tensor | None = None
    intermediate_reference_points: torch.Tensor | None = None
    decoder_hidden_states: tuple[torch.Tensor] | None = None
    decoder_attentions: tuple[torch.Tensor] | None = None
    cross_attentions: tuple[torch.Tensor] | None = None
    encoder_last_hidden_state: torch.Tensor | None = None
    encoder_hidden_states: tuple[torch.Tensor] | None = None
    encoder_attentions: tuple[torch.Tensor] | None = None
    init_reference_points: torch.Tensor | None = None
    enc_topk_logits: torch.Tensor | None = None
    enc_topk_bboxes: torch.Tensor | None = None
    enc_outputs_class: torch.Tensor | None = None
    enc_outputs_coord_logits: torch.Tensor | None = None
    denoising_meta_values: dict | None = None

    logits: torch.Tensor | None = None
    relative_order_logits: torch.Tensor | None = None
    successor_order_logits: torch.Tensor | None = None


@auto_docstring(
    custom_intro="""
    PP-DocLayoutV4 Model (consisting of a backbone and encoder-decoder) outputting raw hidden states without any head on top.
    """
)
class PPDocLayoutV4Model(PPDocLayoutV4PreTrainedModel):
    # The bbox and class heads are untied in PP-DocLayoutV4, so nothing is shared with the encoder heads.
    _tied_weights_keys = {}

    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__(config)

        # Create backbone
        self.backbone = PPDocLayoutV4ConvEncoder(config)
        intermediate_channel_sizes = self.backbone.intermediate_channel_sizes

        # Preserve the RT-DETR projection names for checkpoint compatibility.
        num_backbone_outs = len(intermediate_channel_sizes)

        # The backbone emits exactly the three levels the encoder consumes, so unlike PP-DocLayoutV3 there is no
        # leading projection to drop here.
        encoder_input_proj_list = []
        for i in range(num_backbone_outs):
            in_channels = intermediate_channel_sizes[i]
            encoder_input_proj_list.append(
                nn.Sequential(  # trf-ignore: TRF036
                    nn.Conv2d(in_channels, config.encoder_hidden_dim, kernel_size=1, bias=False),
                    nn.BatchNorm2d(config.encoder_hidden_dim),
                )
            )
        self.encoder_input_proj = nn.ModuleList(encoder_input_proj_list)

        # Create encoder
        self.encoder = PPDocLayoutV4HybridEncoder(config)

        # encoder head
        self.enc_output = nn.Sequential(  # trf-ignore: TRF036
            nn.Linear(config.d_model, config.d_model),
            nn.LayerNorm(config.d_model, eps=config.layer_norm_eps),
        )
        self.enc_score_head = nn.Linear(config.d_model, config.num_labels)

        self.enc_bbox_head = PPDocLayoutV4MLPPredictionHead(
            config.d_model, config.d_model, config.num_coords, num_layers=3
        )

        # init encoder output anchors and valid_mask
        if config.anchor_image_size:
            self.anchors, self.valid_mask = self.generate_anchors(dtype=self.dtype)

        # Create decoder input projection layers
        num_backbone_outs = len(config.decoder_in_channels)
        decoder_input_proj_list = []
        for i in range(num_backbone_outs):
            in_channels = config.decoder_in_channels[i]
            decoder_input_proj_list.append(
                nn.Sequential(  # trf-ignore: TRF036
                    nn.Conv2d(in_channels, config.d_model, kernel_size=1, bias=False),
                    nn.BatchNorm2d(config.d_model, config.batch_norm_eps),
                )
            )
        for _ in range(config.num_feature_levels - num_backbone_outs):
            decoder_input_proj_list.append(
                nn.Sequential(  # trf-ignore: TRF036
                    nn.Conv2d(in_channels, config.d_model, kernel_size=3, stride=2, padding=1, bias=False),
                    nn.BatchNorm2d(config.d_model, config.batch_norm_eps),
                )
            )
            in_channels = config.d_model
        self.decoder_input_proj = nn.ModuleList(decoder_input_proj_list)

        self.decoder = PPDocLayoutV4Decoder(config)

        # No extra "no object" row, unlike the `num_labels + 1` embedding of PP-DocLayoutV3.
        self.denoising_class_embed = (
            nn.Embedding(config.num_labels, config.d_model) if config.num_denoising > 0 else None
        )

        self.post_init()

    def freeze_backbone(self):
        for param in self.backbone.parameters():
            param.requires_grad_(False)

    def unfreeze_backbone(self):
        for param in self.backbone.parameters():
            param.requires_grad_(True)

    @staticmethod
    @compile_compatible_method_lru_cache(maxsize=32)
    def _cached_generate_anchors(
        spatial_shapes: tuple[tuple[int, int], ...],
        grid_size: float,
        device: torch.device | str = "cpu",
        dtype: torch.dtype = torch.float32,
    ) -> tuple[torch.Tensor, torch.Tensor]:
        """
        Builds the 10 dimensional quad anchors in inverse sigmoid space.

        The layout is `[center_x, center_y, x1 + 0.5, y1 + 0.5, ..., x4 + 0.5, y4 + 0.5]` with the corners in
        top-left, top-right, bottom-right, bottom-left order. Only the centers vary spatially, the four offsets are a
        per-level constant, so anchor validity is decided by the centers alone.
        """
        anchors = []
        for level, (height, width) in enumerate(spatial_shapes):
            grid_y, grid_x = torch.meshgrid(
                torch.arange(end=height, device=device).to(dtype),
                torch.arange(end=width, device=device).to(dtype),
                indexing="ij",
            )
            grid_xy = torch.stack([grid_x, grid_y], -1)
            grid_xy = grid_xy.unsqueeze(0) + 0.5
            grid_xy[..., 0] /= width
            grid_xy[..., 1] /= height

            # Key change from RT-DETR: instead of 4-dim (cx, cy, w, h) anchors, each cell gets a 10-dim
            # quad anchor — the center plus the offsets of the four corners (TL, TR, BR, BL) of a square
            # with side grid_size * 2**level centered at 0.5.
            half = grid_size * (2.0**level) / 2.0
            corners = torch.tensor(
                [0.5 - half, 0.5 - half, 0.5 + half, 0.5 - half, 0.5 + half, 0.5 + half, 0.5 - half, 0.5 + half],
                device=device,
                dtype=dtype,
            ).expand(1, height * width, 8)
            anchors.append(torch.concat([grid_xy.reshape(1, height * width, 2), corners], -1))

        # define the valid range for anchor coordinates
        eps = 1e-2
        anchors = torch.concat(anchors, 1)
        valid_mask = ((anchors > eps) * (anchors < 1 - eps)).all(-1, keepdim=True)
        anchors = torch.log(anchors / (1 - anchors))
        anchors = torch.where(valid_mask, anchors, torch.full((), torch.finfo(dtype).max, dtype=dtype, device=device))

        return anchors, valid_mask

    def generate_anchors(self, spatial_shapes=None, grid_size=0.05, device="cpu", dtype=torch.float32):
        if spatial_shapes is None:
            spatial_shapes = (
                (int(self.config.anchor_image_size[0] / s), int(self.config.anchor_image_size[1] / s))
                for s in self.config.feat_strides
            )
        return self._cached_generate_anchors(spatial_shapes, grid_size, device, dtype)

    @auto_docstring
    @can_return_tuple
    def forward(
        self,
        pixel_values: torch.Tensor,
        pixel_mask: torch.LongTensor | None = None,
        encoder_outputs: torch.Tensor | None = None,
        labels: list[dict] | None = None,
        **kwargs: Unpack[TransformersKwargs],
    ) -> tuple[torch.Tensor] | PPDocLayoutV4ModelOutput:
        r"""
        labels (`list[Dict]` of len `(batch_size,)`, *optional*):
            Not supported: PP-DocLayoutV4 is inference only in Transformers.
        """
        if labels is not None:
            raise ValueError("PPDocLayoutV4Model does not support training")

        batch_size, num_channels, height, width = pixel_values.shape
        device = pixel_values.device

        if pixel_mask is None:
            pixel_mask = torch.ones(((batch_size, height, width)), device=device)

        features = self.backbone(pixel_values, pixel_mask)
        proj_feats = [self.encoder_input_proj[level](source) for level, (source, mask) in enumerate(features)]

        if encoder_outputs is None:
            encoder_outputs = self.encoder(proj_feats, **kwargs)
        # If the user passed a tuple for encoder_outputs, we wrap it in a BaseModelOutput
        elif not isinstance(encoder_outputs, BaseModelOutput):
            encoder_outputs = BaseModelOutput(
                last_hidden_state=encoder_outputs[0],
                hidden_states=encoder_outputs[1] if len(encoder_outputs) > 1 else None,
                attentions=encoder_outputs[2] if len(encoder_outputs) > 2 else None,
            )

        sources = [
            self.decoder_input_proj[level](source) for level, source in enumerate(encoder_outputs.last_hidden_state)
        ]

        # Prepare decoder inputs (by flattening)
        source_flatten = []
        spatial_shapes_list = []
        spatial_shapes = torch.empty((len(sources), 2), device=device, dtype=torch.long)
        for level, source in enumerate(sources):
            height, width = source.shape[-2:]
            spatial_shapes[level, 0] = height
            spatial_shapes[level, 1] = width
            spatial_shapes_list.append((height, width))
            source_flatten.append(source.flatten(2).transpose(1, 2))
        source_flatten = torch.cat(source_flatten, 1)
        level_start_index = torch.cat((spatial_shapes.new_zeros((1,)), spatial_shapes.prod(1).cumsum(0)[:-1]))

        dtype = source_flatten.dtype
        if self.training or self.config.anchor_image_size is None:
            # Pass spatial_shapes as tuple to make it hashable and make sure lru_cache is working
            anchors, valid_mask = self.generate_anchors(tuple(spatial_shapes_list), device=device, dtype=dtype)
        else:
            anchors, valid_mask = self.anchors.to(device, dtype), self.valid_mask.to(device, dtype)

        # use the valid_mask to selectively retain values in the feature map where the mask is `True`
        memory = valid_mask.to(dtype) * source_flatten
        output_memory = self.enc_output(memory)

        enc_outputs_class = self.enc_score_head(output_memory)
        enc_outputs_coord_logits = self.enc_bbox_head(output_memory) + anchors

        # Class-agnostic top-k over positions
        _, topk_ind = torch.topk(enc_outputs_class.max(-1).values, self.config.num_queries, dim=1)
        reference_points_unact = enc_outputs_coord_logits.gather(
            dim=1, index=topk_ind.unsqueeze(-1).repeat(1, 1, enc_outputs_coord_logits.shape[-1])
        )
        enc_topk_bboxes = F.sigmoid(reference_points_unact)
        enc_topk_logits = enc_outputs_class.gather(
            dim=1, index=topk_ind.unsqueeze(-1).repeat(1, 1, enc_outputs_class.shape[-1])
        )

        # extract region features
        target = output_memory.gather(dim=1, index=topk_ind.unsqueeze(-1).repeat(1, 1, output_memory.shape[-1]))
        target = target.detach()

        init_reference_points = reference_points_unact.detach()

        decoder_outputs = self.decoder(
            inputs_embeds=target,
            encoder_hidden_states=source_flatten,
            reference_points=init_reference_points,
            spatial_shapes=spatial_shapes,
            spatial_shapes_list=spatial_shapes_list,
            level_start_index=level_start_index,
            **kwargs,
        )

        return PPDocLayoutV4ModelOutput(
            last_hidden_state=decoder_outputs.last_hidden_state,
            intermediate_hidden_states=decoder_outputs.intermediate_hidden_states,
            intermediate_reference_points=decoder_outputs.intermediate_reference_points,
            logits=decoder_outputs.logits,
            relative_order_logits=decoder_outputs.relative_order_logits,
            successor_order_logits=decoder_outputs.successor_order_logits,
            decoder_hidden_states=decoder_outputs.hidden_states,
            decoder_attentions=decoder_outputs.attentions,
            cross_attentions=decoder_outputs.cross_attentions,
            encoder_last_hidden_state=encoder_outputs.last_hidden_state,
            encoder_hidden_states=encoder_outputs.hidden_states,
            encoder_attentions=encoder_outputs.attentions,
            init_reference_points=init_reference_points,
            enc_topk_logits=enc_topk_logits,
            enc_topk_bboxes=enc_topk_bboxes,
            enc_outputs_class=enc_outputs_class,
            enc_outputs_coord_logits=enc_outputs_coord_logits,
        )


@auto_docstring(
    custom_intro="""
    Output type of [`PPDocLayoutV4ForObjectDetection`].
    """
)
@dataclass
class PPDocLayoutV4ForObjectDetectionOutput(ModelOutput):
    r"""
    logits (`torch.Tensor` of shape `(batch_size, num_queries, config.num_labels)`):
        Classification logits (without no-object) for all queries.
    pred_boxes (`torch.Tensor` of shape `(batch_size, num_queries, config.num_coords)`):
        Normalized quads for all queries, encoded as `[center_x, center_y, dx1, dy1, ..., dx4, dy4]` where the corner
        offsets are shifted by `+0.5`. Use
        [`~PPDocLayoutV4ImageProcessor.post_process_object_detection`] to retrieve the unnormalized corners and their
        enclosing boxes.
    last_hidden_state (`torch.Tensor` of shape `(batch_size, num_queries, hidden_size)`):
        Sequence of hidden-states at the output of the last layer of the decoder of the model.
    intermediate_hidden_states (`torch.Tensor` of shape `(batch_size, config.decoder_layers, num_queries, hidden_size)`):
        Stacked intermediate hidden states (output of each layer of the decoder).
    intermediate_reference_points (`torch.Tensor` of shape `(batch_size, config.decoder_layers, num_queries, config.num_coords)`):
        Stacked intermediate reference points (refined quads of each layer of the decoder).
    init_reference_points (`torch.Tensor` of shape `(batch_size, num_queries, config.num_coords)`):
        Initial quad reference points sent through the Transformer decoder.
    enc_topk_logits (`torch.Tensor` of shape `(batch_size, num_queries, config.num_labels)`):
        Class logits of the encoder proposals that were selected as decoder queries.
    enc_topk_bboxes (`torch.Tensor` of shape `(batch_size, num_queries, config.num_coords)`):
        Quads of the encoder proposals that were selected as decoder queries.
    enc_outputs_class (`torch.Tensor` of shape `(batch_size, sequence_length, config.num_labels)`):
        Class logits of every encoder proposal.
    enc_outputs_coord_logits (`torch.Tensor` of shape `(batch_size, sequence_length, config.num_coords)`):
        Quad logits of every encoder proposal.
    denoising_meta_values (`dict`):
        Extra dictionary for the denoising related values.
    relative_order_logits (`torch.Tensor` of shape `(batch_size, config.num_queries, config.num_queries)`):
        Pairwise relative reading order logits, after the optional S2R fusion. A positive `relative_order_logits[i, j]`
        means query `i` is read before query `j`.
    successor_order_logits (`torch.Tensor` of shape `(batch_size, config.num_queries, config.num_queries)`):
        Pairwise direct successor (ROOR) logits. A positive `successor_order_logits[i, j]` means query `j` directly
        follows query `i`.
    """

    logits: torch.Tensor | None = None
    pred_boxes: torch.Tensor | None = None
    last_hidden_state: torch.Tensor | None = None
    intermediate_hidden_states: torch.Tensor | None = None
    intermediate_reference_points: torch.Tensor | None = None
    decoder_hidden_states: tuple[torch.Tensor] | None = None
    decoder_attentions: tuple[torch.Tensor] | None = None
    cross_attentions: tuple[torch.Tensor] | None = None
    encoder_last_hidden_state: torch.Tensor | None = None
    encoder_hidden_states: tuple[torch.Tensor] | None = None
    encoder_attentions: tuple[torch.Tensor] | None = None
    # PP-DocLayoutV3 annotates this as a tuple, but PP-DocLayoutV4 emits a single tensor.
    init_reference_points: torch.Tensor | None = None
    enc_topk_logits: torch.Tensor | None = None
    enc_topk_bboxes: torch.Tensor | None = None
    enc_outputs_class: torch.Tensor | None = None
    enc_outputs_coord_logits: torch.Tensor | None = None
    denoising_meta_values: dict | None = None

    relative_order_logits: torch.Tensor | None = None
    successor_order_logits: torch.Tensor | None = None


@auto_docstring(
    custom_intro="""
    PP-DocLayoutV4 Model (consisting of a backbone and encoder-decoder) outputting quadrilaterals, class logits and
    reading order logits, for tasks such as document layout analysis.
    """
)
class PPDocLayoutV4ForObjectDetection(PPDocLayoutV4PreTrainedModel):
    # When using clones, all layers > 0 will be clones, but layer 0 *is* required
    # We can't initialize the model on meta device as some weights are modified during the initialization
    _no_split_modules = None
    _keys_to_ignore_on_load_missing = ["num_batches_tracked"]
    _tied_weights_keys = {}

    def __init__(self, config: PPDocLayoutV4Config):
        super().__init__(config)

        self.model = PPDocLayoutV4Model(config)
        self.num_queries = config.num_queries
        # if two-stage, the last class_embed and bbox_embed is for region proposal generation
        self.post_init()

    def _set_aux_loss(self, outputs_class, outputs_coord):
        return [{"logits": a, "pred_boxes": b} for a, b in zip(outputs_class, outputs_coord)]

    @auto_docstring
    @can_return_tuple
    def forward(
        self,
        pixel_values: torch.Tensor,
        pixel_mask: torch.LongTensor | None = None,
        encoder_outputs: torch.Tensor | None = None,
        labels: list[dict] | None = None,
        **kwargs: Unpack[TransformersKwargs],
    ) -> tuple[torch.Tensor] | PPDocLayoutV4ForObjectDetectionOutput:
        r"""
        labels (`list[Dict]` of len `(batch_size,)`, *optional*):
            Not supported: PP-DocLayoutV4 is inference only in Transformers.

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, AutoModelForObjectDetection
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO

        >>> url = "https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/layout_demo.jpg"
        >>> with httpx.stream("GET", url) as response:
        ...     image = Image.open(BytesIO(response.read()))

        >>> model_path = "PaddlePaddle/PP-DocLayoutV4_safetensors"
        >>> image_processor = AutoImageProcessor.from_pretrained(model_path)
        >>> model = AutoModelForObjectDetection.from_pretrained(model_path)

        >>> inputs = image_processor(images=[image], return_tensors="pt")
        >>> outputs = model(**inputs)

        >>> # results are already sorted by reading order
        >>> results = image_processor.post_process_object_detection(outputs, target_sizes=[image.size[::-1]])
        >>> for result in results:
        ...     for idx, (score, label_id, box) in enumerate(zip(result["scores"], result["labels"], result["boxes"])):
        ...         box = [round(i, 2) for i in box.tolist()]
        ...         print(f"Order {idx + 1}: {model.config.id2label[label_id.item()]}: {score.item():.2f} {box}")
        Order 1: text: 0.99 [336.07, 182.04, 894.17, 652.62]
        Order 2: paragraph_title: 0.98 [336.45, 681.88, 868.78, 796.91]
        Order 3: text: 0.99 [334.01, 840.84, 889.11, 1452.29]
        Order 4: text: 0.99 [920.65, 183.62, 1476.75, 462.75]
        Order 5: text: 0.99 [919.55, 482.55, 1479.85, 763.41]
        Order 6: text: 0.98 [919.34, 844.48, 1481.28, 1219.44]
        Order 7: text: 0.98 [920.84, 1238.61, 1467.66, 1374.54]
        Order 8: text: 0.90 [334.24, 1612.85, 1478.79, 1730.92]
        Order 9: text: 0.95 [334.25, 1755.98, 1467.34, 1845.6]
        Order 10: text: 0.65 [337.14, 1909.47, 659.78, 1938.2]
        Order 11: footnote: 0.78 [338.73, 2114.87, 1448.16, 2172.06]
        Order 12: number: 0.98 [106.08, 2257.42, 134.76, 2281.3]
        Order 13: footer: 0.93 [339.27, 2255.7, 984.16, 2282.81]
        ```"""
        if labels is not None:
            raise ValueError("PPDocLayoutV4ForObjectDetection does not support training")

        outputs = self.model(
            pixel_values,
            pixel_mask=pixel_mask,
            encoder_outputs=encoder_outputs,
            **kwargs,
        )

        return PPDocLayoutV4ForObjectDetectionOutput(
            logits=outputs.logits,
            pred_boxes=outputs.intermediate_reference_points[:, -1],
            relative_order_logits=outputs.relative_order_logits,
            successor_order_logits=outputs.successor_order_logits,
            last_hidden_state=outputs.last_hidden_state,
            intermediate_hidden_states=outputs.intermediate_hidden_states,
            intermediate_reference_points=outputs.intermediate_reference_points,
            decoder_hidden_states=outputs.decoder_hidden_states,
            decoder_attentions=outputs.decoder_attentions,
            cross_attentions=outputs.cross_attentions,
            encoder_last_hidden_state=outputs.encoder_last_hidden_state,
            encoder_hidden_states=outputs.encoder_hidden_states,
            encoder_attentions=outputs.encoder_attentions,
            init_reference_points=outputs.init_reference_points,
            enc_topk_logits=outputs.enc_topk_logits,
            enc_topk_bboxes=outputs.enc_topk_bboxes,
            enc_outputs_class=outputs.enc_outputs_class,
            enc_outputs_coord_logits=outputs.enc_outputs_coord_logits,
            denoising_meta_values=outputs.denoising_meta_values,
        )


__all__ = ["PPDocLayoutV4ForObjectDetection", "PPDocLayoutV4Model", "PPDocLayoutV4PreTrainedModel"]
