codingutf8 2022 meta platforms inc and the huggingface inc team 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 orglicenseslicense2 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 levit model configuration from collections import ordereddict from typing import mapping from packaging import version from configurationutils import pretrainedconfig from onnx import onnxconfig from utils import logging logger logging getloggername levitpretrainedconfigarchivemap facebooklevit128s https huggingface cofacebooklevit128sresolvemainconfig json see all levit models at https huggingface comodels filterlevit class levitconfigpretrainedconfig r this is the configuration class to store the configuration of a levitmodel it is used to instantiate a levit model according to the specified arguments defining the model architecture instantiating a configuration with the defaults will yield a similar configuration to that of the levit facebooklevit128shttps huggingface cofacebooklevit128s architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args imagesize int optional defaults to 224 the size of the input image numchannels int optional defaults to 3 number of channels in the input image kernelsize int optional defaults to 3 the kernel size for the initial convolution layers of patch embedding stride int optional defaults to 2 the stride size for the initial convolution layers of patch embedding padding int optional defaults to 1 the padding size for the initial convolution layers of patch embedding patchsize int optional defaults to 16 the patch size for embeddings hiddensizes listint optional defaults to 128 256 384 dimension of each of the encoder blocks numattentionheads listint optional defaults to 4 8 12 number of attention heads for each attention layer in each block of the transformer encoder depths listint optional defaults to 4 4 4 the number of layers in each encoder block keydim listint optional defaults to 16 16 16 the size of key in each of the encoder blocks droppathrate int optional defaults to 0 the dropout probability for stochastic depths used in the blocks of the transformer encoder mlpratios listint optional defaults to 2 2 2 ratio of the size of the hidden layer compared to the size of the input layer of the mix ffns in the encoder blocks attentionratios listint optional defaults to 2 2 2 ratio of the size of the output dimension compared to input dimension of attention layers initializerrange float optional defaults to 0 02 the standard deviation of the truncatednormalinitializer for initializing all weight matrices example python from transformers import levitconfig levitmodel initializing a levit levit128s style configuration configuration levitconfig initializing a model with random weights from the levit128s style configuration model levitmodelconfiguration accessing the model configuration configuration model config modeltype levit def init self imagesize224 numchannels3 kernelsize3 stride2 padding1 patchsize16 hiddensizes128 256 384 numattentionheads4 8 12 depths4 4 4 keydim16 16 16 droppathrate0 mlpratio2 2 2 attentionratio2 2 2 initializerrange0 02 kwargs super initkwargs self imagesize imagesize self numchannels numchannels self kernelsize kernelsize self stride stride self padding padding self hiddensizes hiddensizes self numattentionheads numattentionheads self depths depths self keydim keydim self droppathrate droppathrate self patchsize patchsize self attentionratio attentionratio self mlpratio mlpratio self initializerrange initializerrange self downops subsample keydim0 hiddensizes0 keydim0 4 2 2 subsample keydim0 hiddensizes1 keydim0 4 2 2 copied from transformers models vit configurationvit vitonnxconfig class levitonnxconfigonnxconfig torchonnxminimumversion version parse1 11 property def inputsself mappingstr mappingint str return ordereddict pixelvalues 0 batch 1 numchannels 2 height 3 width property def atolforvalidationself float return 1e4 coding utf 8 2022 meta platforms inc and the huggingface inc team 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 levit model configuration see all levit models at https huggingface co models filter levit this is the configuration class to store the configuration of a levitmodel it is used to instantiate a levit model according to the specified arguments defining the model architecture instantiating a configuration with the defaults will yield a similar configuration to that of the levit facebook levit 128s https huggingface co facebook levit 128s architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args image_size int optional defaults to 224 the size of the input image num_channels int optional defaults to 3 number of channels in the input image kernel_size int optional defaults to 3 the kernel size for the initial convolution layers of patch embedding stride int optional defaults to 2 the stride size for the initial convolution layers of patch embedding padding int optional defaults to 1 the padding size for the initial convolution layers of patch embedding patch_size int optional defaults to 16 the patch size for embeddings hidden_sizes list int optional defaults to 128 256 384 dimension of each of the encoder blocks num_attention_heads list int optional defaults to 4 8 12 number of attention heads for each attention layer in each block of the transformer encoder depths list int optional defaults to 4 4 4 the number of layers in each encoder block key_dim list int optional defaults to 16 16 16 the size of key in each of the encoder blocks drop_path_rate int optional defaults to 0 the dropout probability for stochastic depths used in the blocks of the transformer encoder mlp_ratios list int optional defaults to 2 2 2 ratio of the size of the hidden layer compared to the size of the input layer of the mix ffns in the encoder blocks attention_ratios list int optional defaults to 2 2 2 ratio of the size of the output dimension compared to input dimension of attention layers initializer_range float optional defaults to 0 02 the standard deviation of the truncated_normal_initializer for initializing all weight matrices example python from transformers import levitconfig levitmodel initializing a levit levit 128s style configuration configuration levitconfig initializing a model with random weights from the levit 128s style configuration model levitmodel configuration accessing the model configuration configuration model config copied from transformers models vit configuration_vit vitonnxconfig
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


logger = logging.get_logger(__name__)

LEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "facebook/levit-128S": "https://huggingface.co/facebook/levit-128S/resolve/main/config.json",
    
}


class LevitConfig(PretrainedConfig):
    r

    model_type = "levit"

    def __init__(
        self,
        image_size=224,
        num_channels=3,
        kernel_size=3,
        stride=2,
        padding=1,
        patch_size=16,
        hidden_sizes=[128, 256, 384],
        num_attention_heads=[4, 8, 12],
        depths=[4, 4, 4],
        key_dim=[16, 16, 16],
        drop_path_rate=0,
        mlp_ratio=[2, 2, 2],
        attention_ratio=[2, 2, 2],
        initializer_range=0.02,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.image_size = image_size
        self.num_channels = num_channels
        self.kernel_size = kernel_size
        self.stride = stride
        self.padding = padding
        self.hidden_sizes = hidden_sizes
        self.num_attention_heads = num_attention_heads
        self.depths = depths
        self.key_dim = key_dim
        self.drop_path_rate = drop_path_rate
        self.patch_size = patch_size
        self.attention_ratio = attention_ratio
        self.mlp_ratio = mlp_ratio
        self.initializer_range = initializer_range
        self.down_ops = [
            ["Subsample", key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
            ["Subsample", key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
        ]



class LevitOnnxConfig(OnnxConfig):
    torch_onnx_minimum_version = version.parse("1.11")

    @property
    def inputs(self) -> Mapping[str, Mapping[int, str]]:
        return OrderedDict(
            [
                ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
            ]
        )

    @property
    def atol_for_validation(self) -> float:
        return 1e-4