codingutf8 2023 mbzuai 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 swiftformer 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 swiftformerpretrainedconfigarchivemap mbzuaiswiftformerxs https huggingface combzuaiswiftformerxsresolvemainconfig json class swiftformerconfigpretrainedconfig r this is the configuration class to store the configuration of a swiftformermodel it is used to instantiate an swiftformer 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 swiftformer mbzuaiswiftformerxshttps huggingface combzuaiswiftformerxs architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args numchannels int optional defaults to 3 the number of input channels depths listint optional defaults to 3 3 6 4 depth of each stage embeddims listint optional defaults to 48 56 112 220 the embedding dimension at each stage mlpratio int optional defaults to 4 ratio of size of the hidden dimensionality of an mlp to the dimensionality of its input downsamples listbool optional defaults to true true true true whether or not to downsample inputs between two stages hiddenact str optional defaults to gelu the nonlinear activation function string gelu relu selu and gelunew are supported downpatchsize int optional defaults to 3 the size of patches in downsampling layers downstride int optional defaults to 2 the stride of convolution kernels in downsampling layers downpad int optional defaults to 1 padding in downsampling layers droppathrate float optional defaults to 0 0 rate at which to increase dropout probability in droppath uselayerscale bool optional defaults to true whether to scale outputs from token mixers layerscaleinitvalue float optional defaults to 1e05 factor by which outputs from token mixers are scaled batchnormeps float optional defaults to 1e05 the epsilon used by the batch normalization layers example python from transformers import swiftformerconfig swiftformermodel initializing a swiftformer swiftformerbasepatch16224 style configuration configuration swiftformerconfig initializing a model with random weights from the swiftformerbasepatch16224 style configuration model swiftformermodelconfiguration accessing the model configuration configuration model config modeltype swiftformer def init self numchannels3 depths3 3 6 4 embeddims48 56 112 220 mlpratio4 downsamplestrue true true true hiddenactgelu downpatchsize3 downstride2 downpad1 droppathrate0 0 uselayerscaletrue layerscaleinitvalue1e5 batchnormeps1e5 kwargs super initkwargs self numchannels numchannels self depths depths self embeddims embeddims self mlpratio mlpratio self downsamples downsamples self hiddenact hiddenact self downpatchsize downpatchsize self downstride downstride self downpad downpad self droppathrate droppathrate self uselayerscale uselayerscale self layerscaleinitvalue layerscaleinitvalue self batchnormeps batchnormeps class swiftformeronnxconfigonnxconfig 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 2023 mbzuai 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 swiftformer model configuration this is the configuration class to store the configuration of a swiftformermodel it is used to instantiate an swiftformer 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 swiftformer mbzuai swiftformer xs https huggingface co mbzuai swiftformer xs architecture configuration objects inherit from pretrainedconfig and can be used to control the model outputs read the documentation from pretrainedconfig for more information args num_channels int optional defaults to 3 the number of input channels depths list int optional defaults to 3 3 6 4 depth of each stage embed_dims list int optional defaults to 48 56 112 220 the embedding dimension at each stage mlp_ratio int optional defaults to 4 ratio of size of the hidden dimensionality of an mlp to the dimensionality of its input downsamples list bool optional defaults to true true true true whether or not to downsample inputs between two stages hidden_act str optional defaults to gelu the non linear activation function string gelu relu selu and gelu_new are supported down_patch_size int optional defaults to 3 the size of patches in downsampling layers down_stride int optional defaults to 2 the stride of convolution kernels in downsampling layers down_pad int optional defaults to 1 padding in downsampling layers drop_path_rate float optional defaults to 0 0 rate at which to increase dropout probability in droppath use_layer_scale bool optional defaults to true whether to scale outputs from token mixers layer_scale_init_value float optional defaults to 1e 05 factor by which outputs from token mixers are scaled batch_norm_eps float optional defaults to 1e 05 the epsilon used by the batch normalization layers example python from transformers import swiftformerconfig swiftformermodel initializing a swiftformer swiftformer base patch16 224 style configuration configuration swiftformerconfig initializing a model with random weights from the swiftformer base patch16 224 style configuration model swiftformermodel configuration accessing the model configuration configuration model config
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__)

SWIFTFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP = {
    "MBZUAI/swiftformer-xs": "https://huggingface.co/MBZUAI/swiftformer-xs/resolve/main/config.json",
}


class SwiftFormerConfig(PretrainedConfig):
    r

    model_type = "swiftformer"

    def __init__(
        self,
        num_channels=3,
        depths=[3, 3, 6, 4],
        embed_dims=[48, 56, 112, 220],
        mlp_ratio=4,
        downsamples=[True, True, True, True],
        hidden_act="gelu",
        down_patch_size=3,
        down_stride=2,
        down_pad=1,
        drop_path_rate=0.0,
        use_layer_scale=True,
        layer_scale_init_value=1e-5,
        batch_norm_eps=1e-5,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.num_channels = num_channels
        self.depths = depths
        self.embed_dims = embed_dims
        self.mlp_ratio = mlp_ratio
        self.downsamples = downsamples
        self.hidden_act = hidden_act
        self.down_patch_size = down_patch_size
        self.down_stride = down_stride
        self.down_pad = down_pad
        self.drop_path_rate = drop_path_rate
        self.use_layer_scale = use_layer_scale
        self.layer_scale_init_value = layer_scale_init_value
        self.batch_norm_eps = batch_norm_eps


class SwiftFormerOnnxConfig(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