# 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
from dataclasses import dataclass

import torch
import torch.nn.functional as F
from huggingface_hub.dataclasses import strict
from torch import nn

from ... import initialization as init
from ...configuration_utils import PreTrainedConfig, SubConfigSpec
from ...modeling_outputs import BaseModelOutput
from ...modeling_utils import PreTrainedModel
from ...processing_utils import Unpack
from ...utils import TransformersKwargs, auto_docstring, logging
from ..auto import AutoConfig
from ..pp_doclayout_v3.configuration_pp_doclayout_v3 import PPDocLayoutV3Config
from ..pp_doclayout_v3.modeling_pp_doclayout_v3 import (
    PPDocLayoutV3ConvEncoder,
    PPDocLayoutV3Decoder,
    PPDocLayoutV3DecoderOutput,
    PPDocLayoutV3ForObjectDetection,
    PPDocLayoutV3ForObjectDetectionOutput,
    PPDocLayoutV3MLPPredictionHead,
    PPDocLayoutV3Model,
    PPDocLayoutV3ModelOutput,
    PPDocLayoutV3MultiscaleDeformableAttention,
    PPDocLayoutV3PreTrainedModel,
)
from ..rt_detr.modeling_rt_detr import (
    RTDetrHybridEncoder,
    inverse_sigmoid,
)


logger = logging.get_logger(__name__)


@auto_docstring(checkpoint="PaddlePaddle/PP-DocLayoutV4_safetensors")
@strict
class PPDocLayoutV4Config(PPDocLayoutV3Config):
    r"""
    initializer_bias_prior_prob (`float`, *optional*):
        The prior probability used by the bias initializer to initialize biases for `enc_score_head` and `class_embed`.
        If `None`, it defaults to `1 / (num_labels + 1)`.
    freeze_backbone_batch_norms (`bool`, *optional*, defaults to `True`):
        Whether to freeze the batch normalization layers in the backbone.
    encoder_in_channels (`list`, *optional*, defaults to `[512, 1024, 2048]`):
        Multi level features input for encoder.
    feat_strides (`list[int]`, *optional*, defaults to `[8, 16, 32]`):
        Strides used in each feature map.
    encode_proj_layers (`list[int]`, *optional*, defaults to `[2]`):
        Indexes of the projected layers to be used in the encoder.
    positional_encoding_temperature (`int`, *optional*, defaults to 10000):
        The temperature parameter used to create the positional encodings.
    encoder_activation_function (`str`, *optional*, defaults to `"gelu"`):
        The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
        `"relu"`, `"silu"` and `"gelu_new"` are supported.
    eval_size (`tuple[int, int]`, *optional*):
        Height and width used to computes the effective height and width of the position embeddings after taking
        into account the stride. Must be left as `None` to reproduce the reference implementation, which computes the
        position embeddings dynamically.
    normalize_before (`bool`, *optional*, defaults to `False`):
        Determine whether to apply layer normalization in the transformer encoder layer before self-attention and
        feed-forward modules.
    hidden_expansion (`float`, *optional*, defaults to 1.0):
        Expansion ratio to enlarge the dimension size of RepVGGBlock and CSPRepLayer.
    num_queries (`int`, *optional*, defaults to 300):
        Number of object queries.
    decoder_in_channels (`list`, *optional*, defaults to `[256, 256, 256]`):
        Multi level features dimension for decoder
    decoder_ffn_dim (`int`, *optional*, defaults to 1024):
        Dimension of the "intermediate" (often named feed-forward) layer in decoder.
    num_feature_levels (`int`, *optional*, defaults to 3):
        The number of input feature levels.
    decoder_n_points (`int`, *optional*, defaults to 4):
        The number of sampled keys in each feature level for each attention head in the decoder.
    decoder_activation_function (`str`, *optional*, defaults to `"relu"`):
        The non-linear activation function (function or string) in the decoder. If string, `"gelu"`,
        `"relu"`, `"silu"` and `"gelu_new"` are supported.
    num_denoising (`int`, *optional*, defaults to 100):
        The total number of denoising tasks or queries to be used for contrastive denoising.
    anchor_image_size (`tuple[int, int]`, *optional*):
        Height and width of the input image used during evaluation to generate the bounding box anchors. If None, automatic generate anchor is applied.
    disable_custom_kernels (`bool`, *optional*, defaults to `True`):
        Whether to disable custom kernels.
    global_pointer_head_size (`int`, *optional*, defaults to 64):
        The size of the global pointer head.
    gp_dropout_value (`float`, *optional*, defaults to 0.1):
        The dropout probability in the global pointer head.
    hidden_size (`int`, *optional*, defaults to 256):
        Dimension of the decoder layers, excluding the hybrid encoder. Also readable as `d_model`, the name used by
        the RT-DETR lineage this model descends from.
    num_coords (`int`, *optional*, defaults to 10):
        Size of the box parameterization predicted by the bbox heads. PP-DocLayoutV4 regresses a four point
        quadrilateral encoded as `[center_x, center_y, dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4]` in sigmoid space,
        where the corner offsets are shifted by `+0.5`. Only `10` is supported.
    s2r_steps (`int`, *optional*, defaults to 3):
        Number of propagation steps used to approximate the transitive closure of the successor matrix.
    s2r_damping (`float`, *optional*, defaults to 0.5):
        Damping factor applied to every additional propagation step of the transitive closure.
    s2r_closure_weight_init (`float`, *optional*, defaults to 0.0):
        Initial value of the learnable gate that weights the closure term. Defaults to `0.0` so that an untrained
        fusion module is numerically identical to using the relative order logits alone.

    Examples:

    ```python
    >>> from transformers import PPDocLayoutV4Config, PPDocLayoutV4ForObjectDetection

    >>> # Initializing a PP-DocLayoutV4 configuration
    >>> configuration = PPDocLayoutV4Config()

    >>> # Initializing a model (with random weights) from the configuration
    >>> model = PPDocLayoutV4ForObjectDetection(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```"""

    model_type = "pp_doclayout_v4"
    sub_configs_defaults = {
        "backbone_config": SubConfigSpec(
            config_class=AutoConfig,
            model_type="hgnet_v2",
            init_kwargs={
                # PP-DocLayoutV4 has no mask branch, so the stride 4 feature is never consumed and the
                # backbone only has to emit the last three stages.
                "return_idx": [1, 2, 3],
                "freeze_stem_only": True,
                "freeze_at": 0,
                "freeze_norm": True,
                "lr_mult_list": [0, 0.05, 0.05, 0.05, 0.05],
                "out_features": ["stage2", "stage3", "stage4"],
            },
        ),
    }

    # PP-DocLayoutV3 declares `d_model` and aliases `hidden_size` onto it; PP-DocLayoutV4 does the reverse so
    # that the canonical name is the one the rest of the library expects.
    attribute_map = {
        "d_model": "hidden_size",
        "num_attention_heads": "encoder_attention_heads",
    }

    hidden_size: int = 256
    eval_size: list[int] | tuple[int, int] | None = None
    anchor_image_size: list[int] | tuple[int, int] | None = None
    num_coords: int = 10
    s2r_steps: int = 3
    s2r_damping: float = 0.5
    s2r_closure_weight_init: float = 0.0

    # PP-DocLayoutV4 has no mask branch, so none of the PP-DocLayoutV3 mask prototype knobs apply.
    mask_feature_channels = AttributeError()
    mask_enhanced = AttributeError()
    num_prototypes = AttributeError()
    x4_feat_dim = AttributeError()
    label_noise_ratio = AttributeError()
    box_noise_scale = AttributeError()
    # This removes the inherited `d_model` field rather than declaring one: PP-DocLayoutV4 declares the canonical
    # `hidden_size` above and keeps `d_model` only as an `attribute_map` alias.
    d_model = AttributeError()  # trf-ignore: TRF023
    layer_types = AttributeError()
    learn_initial_query = AttributeError()

    def __post_init__(self, **kwargs):
        PreTrainedConfig.__post_init__(self, **kwargs)

        # Resolved here rather than in `_init_weights` so that the effective prior is visible on the config.
        # `num_labels` is only materialized by `PreTrainedConfig.__post_init__`, hence the ordering.
        if self.initializer_bias_prior_prob is None:
            self.initializer_bias_prior_prob = 1 / (self.num_labels + 1)

    def validate_architecture(self):
        """Part of `@strict`-powered validation. Validates the architecture of the config."""
        if self.num_coords != 10:
            raise ValueError(f"PP-DocLayoutV4 only supports `num_coords=10`, got {self.num_coords}.")


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 PPDocLayoutV4MultiscaleDeformableAttention(PPDocLayoutV3MultiscaleDeformableAttention):
    pass


class PPDocLayoutV4ConvEncoder(PPDocLayoutV3ConvEncoder):
    pass


class PPDocLayoutV4MLPPredictionHead(PPDocLayoutV3MLPPredictionHead):
    pass


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(PPDocLayoutV3PreTrainedModel):
    @torch.no_grad()
    def _init_weights(self, module):
        """Initialize the weights"""
        PreTrainedModel._init_weights(self, 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 PPDocLayoutV4HybridEncoder(RTDetrHybridEncoder):
    """
    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.
    """


@auto_docstring(
    custom_intro="""
    Output type of [`PPDocLayoutV4Decoder`].
    """
)
@dataclass
class PPDocLayoutV4DecoderOutput(PPDocLayoutV3DecoderOutput):
    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.
    """

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

    # PP-DocLayoutV4 does not produce these RT-DETR / PP-DocLayoutV3 decoder outputs: only the last layer is scored
    # (no intermediate logits), there is no corner prediction, and there is no mask branch.
    intermediate_logits = AttributeError()
    intermediate_predicted_corners = AttributeError()
    initial_reference_points = AttributeError()
    decoder_out_order_logits = AttributeError()
    decoder_out_masks = AttributeError()


class PPDocLayoutV4Decoder(PPDocLayoutV3Decoder):
    """
    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.
    """

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

        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.successor_order_head = PPDocLayoutV4SuccessorOrderHead(config)
        self.relative_order_head = PPDocLayoutV4RelativeOrderHead(config)

    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(PPDocLayoutV3ModelOutput):
    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.
    """

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

    # PP-DocLayoutV4 does not produce these RT-DETR / PP-DocLayoutV3 outputs: only the last layer is scored (no
    # intermediate logits), there is no corner prediction, and there is no mask branch.
    intermediate_logits = AttributeError()
    intermediate_predicted_corners = AttributeError()
    initial_reference_points = AttributeError()
    out_order_logits = AttributeError()
    out_masks = AttributeError()


@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(PPDocLayoutV3Model):
    # 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):
        PPDocLayoutV4PreTrainedModel.__init__(self, 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 _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 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(PPDocLayoutV3ForObjectDetectionOutput):
    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`.
    """

    relative_order_logits: torch.Tensor | None = None
    successor_order_logits: 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

    # PP-DocLayoutV4 does not produce these RT-DETR / PP-DocLayoutV3 outputs: only the last layer is scored (no
    # intermediate logits), there is no corner prediction, and there is no mask branch.
    order_logits = AttributeError()
    out_masks = AttributeError()
    intermediate_logits = AttributeError()
    intermediate_predicted_corners = AttributeError()
    initial_reference_points = AttributeError()


@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(PPDocLayoutV3ForObjectDetection, PPDocLayoutV4PreTrainedModel):
    _tied_weights_keys = {}
    _keys_to_ignore_on_load_missing = ["num_batches_tracked"]

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

        self.model = PPDocLayoutV4Model(config)
        # `PPDocLayoutV4Model` already set this up, so drop V3's unconditional override.
        del self.model.denoising_class_embed

    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__ = [
    "PPDocLayoutV4Config",
    "PPDocLayoutV4ForObjectDetection",
    "PPDocLayoutV4Model",
    "PPDocLayoutV4PreTrainedModel",
]
