# Copyright 2024 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.
"""DepthAnything model configuration"""

from huggingface_hub.dataclasses import strict

from ...backbone_utils import consolidate_backbone_kwargs_to_config
from ...configuration_utils import PreTrainedConfig
from ...utils import auto_docstring
from ..auto.configuration_auto import AutoConfig


@auto_docstring(checkpoint="LiheYoung/depth-anything-small-hf")
@strict
class DepthAnythingConfig(PreTrainedConfig):
    r"""
    reassemble_hidden_size (`int`, *optional*, defaults to 384):
        The number of input channels of the reassemble layers.
    reassemble_factors (`list[int]`, *optional*, defaults to `[4, 2, 1, 0.5]`):
        The up/downsampling factors of the reassemble layers.
    neck_hidden_sizes (`list[str]`, *optional*, defaults to `[48, 96, 192, 384]`):
        The hidden sizes to project to for the feature maps of the backbone.
    fusion_hidden_size (`int`, *optional*, defaults to 64):
        The number of channels before fusion.
    head_in_index (`int`, *optional*, defaults to -1):
        The index of the features to use in the depth estimation head.
    head_hidden_size (`int`, *optional*, defaults to 32):
        The number of output channels in the second convolution of the depth estimation head.
    depth_estimation_type (`str`, *optional*, defaults to `"relative"`):
        The type of depth estimation to use. Can be one of `["relative", "metric"]`.
    max_depth (`float`, *optional*):
        The maximum depth to use for the "metric" depth estimation head. 20 should be used for indoor models
        and 80 for outdoor models. For "relative" depth estimation, this value is ignored.

    Example:

    ```python
    >>> from transformers import DepthAnythingConfig, DepthAnythingForDepthEstimation

    >>> # Initializing a DepthAnything small style configuration
    >>> configuration = DepthAnythingConfig()

    >>> # Initializing a model from the DepthAnything small style configuration
    >>> model = DepthAnythingForDepthEstimation(configuration)

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

    model_type = "depth_anything"
    sub_configs = {"backbone_config": AutoConfig}

    backbone_config: dict | PreTrainedConfig | None = None
    patch_size: int | list[int] | tuple[int, int] = 14
    initializer_range: float = 0.02
    reassemble_hidden_size: int = 384
    reassemble_factors: list[int | float] | tuple[int | float, ...] = (4, 2, 1, 0.5)
    neck_hidden_sizes: list[int] | tuple[int, ...] = (48, 96, 192, 384)
    fusion_hidden_size: int = 64
    head_in_index: int = -1
    head_hidden_size: int = 32
    depth_estimation_type: str = "relative"
    max_depth: int | None = None

    def __post_init__(self, **kwargs):
        self.backbone_config, kwargs = consolidate_backbone_kwargs_to_config(
            backbone_config=self.backbone_config,
            default_config_type="dinov2",
            default_config_kwargs={
                "image_size": 518,
                "hidden_size": 384,
                "num_attention_heads": 6,
                "out_indices": [9, 10, 11, 12],
                "reshape_hidden_states": False,
            },
            **kwargs,
        )

        self.max_depth = self.max_depth if self.max_depth else 1
        super().__post_init__(**kwargs)

    def validate_architecture(self):
        """Part of `@strict`-powered validation. Validates the architecture of the config."""
        if self.depth_estimation_type not in ["relative", "metric"]:
            raise ValueError("depth_estimation_type must be one of ['relative', 'metric']")


__all__ = ["DepthAnythingConfig"]
