import math
from collections import defaultdict
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE=0.999  ,      SCREAMING_SNAKE_CASE="cosine"  ,      ):
    '''simple docstring'''







    if alpha_transform_type == "cosine":

        def alpha_bar_fn(SCREAMING_SNAKE_CASE     ):
            return math.cos((t + 0.008) / 1.008 * math.pi / 2     ) ** 2

    elif alpha_transform_type == "exp":

        def alpha_bar_fn(SCREAMING_SNAKE_CASE     ):
            return math.exp(t * -12.0     )

    else:
        raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}"""     )

    __UpperCamelCase					:Tuple        =		[]
    for i in range(SCREAMING_SNAKE_CASE     ):
        __UpperCamelCase					:Optional[Any]        =		i / num_diffusion_timesteps
        __UpperCamelCase					:Optional[int]        =		(i + 1) / num_diffusion_timesteps
        betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE     ) / alpha_bar_fn(SCREAMING_SNAKE_CASE     )  ,      SCREAMING_SNAKE_CASE     )     )
    return torch.tensor(SCREAMING_SNAKE_CASE  ,      dtype=torch.floataa     )


class      lowerCamelCase_					(       UpperCAmelCase_						,     UpperCAmelCase_			):






    '''simple docstring'''






    a__     :						Optional[Any]      =				[e.name for e in KarrasDiffusionSchedulers]
    a__     :						str      =				2







    @register_to_config
    def __init__(     self			,			__lowercase = 1_000			,			__lowercase = 0.0_00_85			,			__lowercase = 0.0_12			,			__lowercase = "linear"			,			__lowercase = None			,			__lowercase = "epsilon"			,			__lowercase = False			,			__lowercase = False			,			__lowercase = 1.0			,			__lowercase = "linspace"			,			__lowercase = 0			,			)	->     str:
        if trained_betas is not None:
            __UpperCamelCase					:str        =		torch.tensor(__lowercase			,			dtype=torch.floataa)
        elif beta_schedule == "linear":
            __UpperCamelCase					:Any        =		torch.linspace(__lowercase			,			__lowercase			,			__lowercase			,			dtype=torch.floataa)
        elif beta_schedule == "scaled_linear":
            # this schedule is very specific to the latent diffusion model.
            __UpperCamelCase					:Optional[int]        =		(
                torch.linspace(beta_start**0.5			,			beta_end**0.5			,			__lowercase			,			dtype=torch.floataa) ** 2
            )
        elif beta_schedule == "squaredcos_cap_v2":
            # Glide cosine schedule
            __UpperCamelCase					:List[str]        =		betas_for_alpha_bar(__lowercase			,			alpha_transform_type='''cosine''')
        elif beta_schedule == "exp":
            __UpperCamelCase					:str        =		betas_for_alpha_bar(__lowercase			,			alpha_transform_type='''exp''')
        else:
            raise NotImplementedError(f"""{beta_schedule} does is not implemented for {self.__class__}""")

        __UpperCamelCase					:List[Any]        =		1.0 - self.betas
        __UpperCamelCase					:Any        =		torch.cumprod(self.alphas			,			dim=0)

        #  set all values
        self.set_timesteps(__lowercase			,			__lowercase			,			__lowercase)
        __UpperCamelCase					:Dict        =		use_karras_sigmas







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase=None)	->     Optional[int]:
        if schedule_timesteps is None:
            __UpperCamelCase					:Any        =		self.timesteps

        __UpperCamelCase					:Optional[int]        =		(schedule_timesteps == timestep).nonzero()

        # The sigma index that is taken for the **very** first `step`
        # is always the second index (or the last index if there is only 1)
        # This way we can ensure we don't accidentally skip a sigma in
        # case we start in the middle of the denoising schedule (e.g. for image-to-image)
        if len(self._index_counter) == 0:
            __UpperCamelCase					:Optional[int]        =		1 if len(__lowercase) > 1 else 0
        else:
            __UpperCamelCase					:Union[str, Any]        =		timestep.cpu().item() if torch.is_tensor(__lowercase) else timestep
            __UpperCamelCase					:Tuple        =		self._index_counter[timestep_int]

        return indices[pos].item()







    @property
    def       UpperCamelCase__       (     self)	->     str:
        # standard deviation of the initial noise distribution
        if self.config.timestep_spacing in ["linspace", "trailing"]:
            return self.sigmas.max()

        return (self.sigmas.max() ** 2 + 1) ** 0.5







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			)	->     torch.FloatTensor:
        __UpperCamelCase					:Optional[int]        =		self.index_for_timestep(__lowercase)

        __UpperCamelCase					:Optional[Any]        =		self.sigmas[step_index]
        __UpperCamelCase					:Dict        =		sample / ((sigma**2 + 1) ** 0.5)
        return sample







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase = None			,			__lowercase = None			,			)	->     Union[str, Any]:
        __UpperCamelCase					:List[str]        =		num_inference_steps

        __UpperCamelCase					:Union[str, Any]        =		num_train_timesteps or self.config.num_train_timesteps

        # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
        if self.config.timestep_spacing == "linspace":
            __UpperCamelCase					:Tuple        =		np.linspace(0			,			num_train_timesteps - 1			,			__lowercase			,			dtype=__lowercase)[::-1].copy()
        elif self.config.timestep_spacing == "leading":
            __UpperCamelCase					:Optional[int]        =		num_train_timesteps // self.num_inference_steps
            # creates integer timesteps by multiplying by ratio
            # casting to int to avoid issues when num_inference_step is power of 3
            __UpperCamelCase					:Optional[int]        =		(np.arange(0			,			__lowercase) * step_ratio).round()[::-1].copy().astype(__lowercase)
            timesteps += self.config.steps_offset
        elif self.config.timestep_spacing == "trailing":
            __UpperCamelCase					:Tuple        =		num_train_timesteps / self.num_inference_steps
            # creates integer timesteps by multiplying by ratio
            # casting to int to avoid issues when num_inference_step is power of 3
            __UpperCamelCase					:Optional[int]        =		(np.arange(__lowercase			,			0			,			-step_ratio)).round().copy().astype(__lowercase)
            timesteps -= 1
        else:
            raise ValueError(
                f"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'.""")

        __UpperCamelCase					:Any        =		np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5)
        __UpperCamelCase					:Union[str, Any]        =		np.log(__lowercase)
        __UpperCamelCase					:List[Any]        =		np.interp(__lowercase			,			np.arange(0			,			len(__lowercase))			,			__lowercase)

        if self.config.use_karras_sigmas:
            __UpperCamelCase					:Optional[int]        =		self._convert_to_karras(in_sigmas=__lowercase			,			num_inference_steps=self.num_inference_steps)
            __UpperCamelCase					:Any        =		np.array([self._sigma_to_t(__lowercase			,			__lowercase) for sigma in sigmas])

        __UpperCamelCase					:List[Any]        =		np.concatenate([sigmas, [0.0]]).astype(np.floataa)
        __UpperCamelCase					:List[str]        =		torch.from_numpy(__lowercase).to(device=__lowercase)
        __UpperCamelCase					:List[str]        =		torch.cat([sigmas[:1], sigmas[1:-1].repeat_interleave(2), sigmas[-1:]])

        __UpperCamelCase					:Optional[Any]        =		torch.from_numpy(__lowercase)
        __UpperCamelCase					:Dict        =		torch.cat([timesteps[:1], timesteps[1:].repeat_interleave(2)])

        if str(__lowercase).startswith('''mps'''):
            # mps does not support float64
            __UpperCamelCase					:Dict        =		timesteps.to(__lowercase			,			dtype=torch.floataa)
        else:
            __UpperCamelCase					:Any        =		timesteps.to(device=__lowercase)

        # empty dt and derivative
        __UpperCamelCase					:List[Any]        =		None
        __UpperCamelCase					:List[str]        =		None

        # for exp beta schedules, such as the one for `pipeline_shap_e.py`
        # we need an index counter
        __UpperCamelCase					:List[Any]        =		defaultdict(__lowercase)







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase)	->     Union[str, Any]:
        # get log sigma
        __UpperCamelCase					:Optional[Any]        =		np.log(__lowercase)

        # get distribution
        __UpperCamelCase					:Optional[int]        =		log_sigma - log_sigmas[:, np.newaxis]

        # get sigmas range
        __UpperCamelCase					:List[Any]        =		np.cumsum((dists >= 0)			,			axis=0).argmax(axis=0).clip(max=log_sigmas.shape[0] - 2)
        __UpperCamelCase					:Optional[Any]        =		low_idx + 1

        __UpperCamelCase					:Union[str, Any]        =		log_sigmas[low_idx]
        __UpperCamelCase					:Optional[int]        =		log_sigmas[high_idx]

        # interpolate sigmas
        __UpperCamelCase					:Optional[Any]        =		(low - log_sigma) / (low - high)
        __UpperCamelCase					:Any        =		np.clip(__lowercase			,			0			,			1)

        # transform interpolation to time range
        __UpperCamelCase					:Tuple        =		(1 - w) * low_idx + w * high_idx
        __UpperCamelCase					:Optional[Any]        =		t.reshape(sigma.shape)
        return t







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase)	->     torch.FloatTensor:
        __UpperCamelCase					:float        =		in_sigmas[-1].item()
        __UpperCamelCase					:float        =		in_sigmas[0].item()

        __UpperCamelCase					:int        =		7.0  # 7.0 is the value used in the paper
        __UpperCamelCase					:str        =		np.linspace(0			,			1			,			__lowercase)
        __UpperCamelCase					:List[Any]        =		sigma_min ** (1 / rho)
        __UpperCamelCase					:List[str]        =		sigma_max ** (1 / rho)
        __UpperCamelCase					:List[str]        =		(max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
        return sigmas







    @property
    def       UpperCamelCase__       (     self)	->     List[str]:
        return self.dt is None







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase = True			,			)	->     Union[SchedulerOutput, Tuple]:
        __UpperCamelCase					:Any        =		self.index_for_timestep(__lowercase)

        # advance index counter by 1
        __UpperCamelCase					:List[Any]        =		timestep.cpu().item() if torch.is_tensor(__lowercase) else timestep
        self._index_counter[timestep_int] += 1

        if self.state_in_first_order:
            __UpperCamelCase					:Tuple        =		self.sigmas[step_index]
            __UpperCamelCase					:List[str]        =		self.sigmas[step_index + 1]
        else:
            # 2nd order / Heun's method
            __UpperCamelCase					:int        =		self.sigmas[step_index - 1]
            __UpperCamelCase					:Tuple        =		self.sigmas[step_index]

        # currently only gamma=0 is supported. This usually works best anyways.
        # We can support gamma in the future but then need to scale the timestep before
        # passing it to the model which requires a change in API
        __UpperCamelCase					:List[str]        =		0
        __UpperCamelCase					:Any        =		sigma * (gamma + 1)  # Note: sigma_hat == sigma for now

        # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
        if self.config.prediction_type == "epsilon":
            __UpperCamelCase					:List[str]        =		sigma_hat if self.state_in_first_order else sigma_next
            __UpperCamelCase					:str        =		sample - sigma_input * model_output
        elif self.config.prediction_type == "v_prediction":
            __UpperCamelCase					:Optional[Any]        =		sigma_hat if self.state_in_first_order else sigma_next
            __UpperCamelCase					:Any        =		model_output * (-sigma_input / (sigma_input**2 + 1) ** 0.5) + (
                sample / (sigma_input**2 + 1)
            )
        elif self.config.prediction_type == "sample":
            __UpperCamelCase					:Optional[int]        =		model_output
        else:
            raise ValueError(
                f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`""")

        if self.config.clip_sample:
            __UpperCamelCase					:int        =		pred_original_sample.clamp(
                -self.config.clip_sample_range			,			self.config.clip_sample_range)

        if self.state_in_first_order:
            # 2. Convert to an ODE derivative for 1st order
            __UpperCamelCase					:Dict        =		(sample - pred_original_sample) / sigma_hat
            # 3. delta timestep
            __UpperCamelCase					:Optional[int]        =		sigma_next - sigma_hat

            # store for 2nd order step
            __UpperCamelCase					:Dict        =		derivative
            __UpperCamelCase					:Tuple        =		dt
            __UpperCamelCase					:List[Any]        =		sample
        else:
            # 2. 2nd order / Heun's method
            __UpperCamelCase					:int        =		(sample - pred_original_sample) / sigma_next
            __UpperCamelCase					:Dict        =		(self.prev_derivative + derivative) / 2

            # 3. take prev timestep & sample
            __UpperCamelCase					:str        =		self.dt
            __UpperCamelCase					:List[Any]        =		self.sample

            # free dt and derivative
            # Note, this puts the scheduler in "first order mode"
            __UpperCamelCase					:Any        =		None
            __UpperCamelCase					:Dict        =		None
            __UpperCamelCase					:Tuple        =		None

        __UpperCamelCase					:Optional[Any]        =		sample + derivative * dt

        if not return_dict:
            return (prev_sample,)

        return SchedulerOutput(prev_sample=__lowercase)







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			__lowercase			,			)	->     torch.FloatTensor:
        # Make sure sigmas and timesteps have the same device and dtype as original_samples
        __UpperCamelCase					:Any        =		self.sigmas.to(device=original_samples.device			,			dtype=original_samples.dtype)
        if original_samples.device.type == "mps" and torch.is_floating_point(__lowercase):
            # mps does not support float64
            __UpperCamelCase					:Tuple        =		self.timesteps.to(original_samples.device			,			dtype=torch.floataa)
            __UpperCamelCase					:Optional[int]        =		timesteps.to(original_samples.device			,			dtype=torch.floataa)
        else:
            __UpperCamelCase					:Dict        =		self.timesteps.to(original_samples.device)
            __UpperCamelCase					:Union[str, Any]        =		timesteps.to(original_samples.device)

        __UpperCamelCase					:int        =		[self.index_for_timestep(__lowercase			,			__lowercase) for t in timesteps]

        __UpperCamelCase					:Optional[int]        =		sigmas[step_indices].flatten()
        while len(sigma.shape) < len(original_samples.shape):
            __UpperCamelCase					:Tuple        =		sigma.unsqueeze(-1)

        __UpperCamelCase					:Tuple        =		original_samples + noise * sigma
        return noisy_samples







    def __len__(     self)	->     List[str]:
        return self.config.num_train_timesteps






from typing import Optional, Union

import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_mobilenet_va import MobileNetVaConfig


__lowercase										= logging.get_logger(__name__)


# General docstring
__lowercase										= '''MobileNetV1Config'''

# Base docstring
__lowercase										= '''google/mobilenet_v1_1.0_224'''
__lowercase										= [1, 1024, 7, 7]

# Image classification docstring
__lowercase										= '''google/mobilenet_v1_1.0_224'''
__lowercase										= '''tabby, tabby cat'''


__lowercase										= [
    '''google/mobilenet_v1_1.0_224''',
    '''google/mobilenet_v1_0.75_192''',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE=None     ):
    '''simple docstring'''







    __UpperCamelCase					:Tuple        =		{}

    if isinstance(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
        __UpperCamelCase					:Dict        =		model.mobilenet_va
    else:
        __UpperCamelCase					:str        =		model

    __UpperCamelCase					:int        =		'''MobilenetV1/Conv2d_0/'''
    __UpperCamelCase					:str        =		backbone.conv_stem.convolution.weight
    __UpperCamelCase					:int        =		backbone.conv_stem.normalization.bias
    __UpperCamelCase					:Union[str, Any]        =		backbone.conv_stem.normalization.weight
    __UpperCamelCase					:Optional[int]        =		backbone.conv_stem.normalization.running_mean
    __UpperCamelCase					:Optional[int]        =		backbone.conv_stem.normalization.running_var

    for i in range(13     ):
        __UpperCamelCase					:Optional[Any]        =		i + 1
        __UpperCamelCase					:Optional[int]        =		i * 2

        __UpperCamelCase					:List[Any]        =		backbone.layer[pt_index]
        __UpperCamelCase					:Tuple        =		f"""MobilenetV1/Conv2d_{tf_index}_depthwise/"""
        __UpperCamelCase					:Any        =		pointer.convolution.weight
        __UpperCamelCase					:Dict        =		pointer.normalization.bias
        __UpperCamelCase					:List[str]        =		pointer.normalization.weight
        __UpperCamelCase					:Any        =		pointer.normalization.running_mean
        __UpperCamelCase					:List[str]        =		pointer.normalization.running_var

        __UpperCamelCase					:Union[str, Any]        =		backbone.layer[pt_index + 1]
        __UpperCamelCase					:List[str]        =		f"""MobilenetV1/Conv2d_{tf_index}_pointwise/"""
        __UpperCamelCase					:Optional[Any]        =		pointer.convolution.weight
        __UpperCamelCase					:Dict        =		pointer.normalization.bias
        __UpperCamelCase					:int        =		pointer.normalization.weight
        __UpperCamelCase					:Optional[int]        =		pointer.normalization.running_mean
        __UpperCamelCase					:Optional[int]        =		pointer.normalization.running_var

    if isinstance(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
        __UpperCamelCase					:Any        =		'''MobilenetV1/Logits/Conv2d_1c_1x1/'''
        __UpperCamelCase					:Union[str, Any]        =		model.classifier.weight
        __UpperCamelCase					:int        =		model.classifier.bias

    return tf_to_pt_map



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
    '''simple docstring'''







    try:
        import numpy as np
        import tensorflow as tf
    except ImportError:
        logger.error(
            '''Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see '''
            '''https://www.tensorflow.org/install/ for installation instructions.'''     )
        raise

    # Load weights from TF model
    __UpperCamelCase					:Any        =		tf.train.list_variables(SCREAMING_SNAKE_CASE     )
    __UpperCamelCase					:List[str]        =		{}
    for name, shape in init_vars:
        logger.info(f"""Loading TF weight {name} with shape {shape}"""     )
        __UpperCamelCase					:str        =		tf.train.load_variable(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     )
        __UpperCamelCase					:List[str]        =		array

    # Build TF to PyTorch weights loading map
    __UpperCamelCase					:Optional[Any]        =		_build_tf_to_pytorch_map(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     )

    for name, pointer in tf_to_pt_map.items():
        logger.info(f"""Importing {name}"""     )
        if name not in tf_weights:
            logger.info(f"""{name} not in tf pre-trained weights, skipping"""     )
            continue

        __UpperCamelCase					:Optional[Any]        =		tf_weights[name]

        if "depthwise_weights" in name:
            logger.info('''Transposing depthwise'''     )
            __UpperCamelCase					:Optional[int]        =		np.transpose(SCREAMING_SNAKE_CASE  ,      (2, 3, 0, 1)     )
        elif "weights" in name:
            logger.info('''Transposing'''     )
            if len(pointer.shape     ) == 2:  # copying into linear layer
                __UpperCamelCase					:Tuple        =		array.squeeze().transpose()
            else:
                __UpperCamelCase					:Union[str, Any]        =		np.transpose(SCREAMING_SNAKE_CASE  ,      (3, 2, 0, 1)     )

        if pointer.shape != array.shape:
            raise ValueError(f"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched"""     )

        logger.info(f"""Initialize PyTorch weight {name} {array.shape}"""     )
        __UpperCamelCase					:Optional[int]        =		torch.from_numpy(SCREAMING_SNAKE_CASE     )

        tf_weights.pop(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     )
        tf_weights.pop(name + '''/RMSProp'''  ,      SCREAMING_SNAKE_CASE     )
        tf_weights.pop(name + '''/RMSProp_1'''  ,      SCREAMING_SNAKE_CASE     )
        tf_weights.pop(name + '''/ExponentialMovingAverage'''  ,      SCREAMING_SNAKE_CASE     )

    logger.info(f"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys()     )}"""     )
    return model



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
    '''simple docstring'''







    __UpperCamelCase  ,	__UpperCamelCase					:str        =		features.shape[-2:]
    __UpperCamelCase  ,	__UpperCamelCase					:Union[str, Any]        =		conv_layer.stride
    __UpperCamelCase  ,	__UpperCamelCase					:Union[str, Any]        =		conv_layer.kernel_size

    if in_height % stride_height == 0:
        __UpperCamelCase					:Optional[int]        =		max(kernel_height - stride_height  ,      0     )
    else:
        __UpperCamelCase					:List[Any]        =		max(kernel_height - (in_height % stride_height)  ,      0     )

    if in_width % stride_width == 0:
        __UpperCamelCase					:List[str]        =		max(kernel_width - stride_width  ,      0     )
    else:
        __UpperCamelCase					:Tuple        =		max(kernel_width - (in_width % stride_width)  ,      0     )

    __UpperCamelCase					:Any        =		pad_along_width // 2
    __UpperCamelCase					:Tuple        =		pad_along_width - pad_left
    __UpperCamelCase					:Union[str, Any]        =		pad_along_height // 2
    __UpperCamelCase					:str        =		pad_along_height - pad_top

    __UpperCamelCase					:Optional[Any]        =		(pad_left, pad_right, pad_top, pad_bottom)
    return nn.functional.pad(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      '''constant'''  ,      0.0     )


class      lowerCamelCase_					(       nn.Module			):






    '''simple docstring'''






    def __init__(     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase = 1			,			__lowercase = 1			,			__lowercase = False			,			__lowercase = True			,			__lowercase = True			,			)	->     None:
        super().__init__()
        __UpperCamelCase					:str        =		config

        if in_channels % groups != 0:
            raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups.""")
        if out_channels % groups != 0:
            raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups.""")

        __UpperCamelCase					:Any        =		0 if config.tf_padding else int((kernel_size - 1) / 2)

        __UpperCamelCase					:List[Any]        =		nn.Convad(
            in_channels=__lowercase			,			out_channels=__lowercase			,			kernel_size=__lowercase			,			stride=__lowercase			,			padding=__lowercase			,			groups=__lowercase			,			bias=__lowercase			,			padding_mode='''zeros'''			,			)

        if use_normalization:
            __UpperCamelCase					:str        =		nn.BatchNormad(
                num_features=__lowercase			,			eps=config.layer_norm_eps			,			momentum=0.99_97			,			affine=__lowercase			,			track_running_stats=__lowercase			,			)
        else:
            __UpperCamelCase					:Tuple        =		None

        if use_activation:
            if isinstance(__lowercase			,			__lowercase):
                __UpperCamelCase					:Union[str, Any]        =		ACTaFN[use_activation]
            elif isinstance(config.hidden_act			,			__lowercase):
                __UpperCamelCase					:Dict        =		ACTaFN[config.hidden_act]
            else:
                __UpperCamelCase					:List[Any]        =		config.hidden_act
        else:
            __UpperCamelCase					:Optional[Any]        =		None







    def       UpperCamelCase__       (     self			,			__lowercase)	->     torch.Tensor:
        if self.config.tf_padding:
            __UpperCamelCase					:Any        =		apply_tf_padding(__lowercase			,			self.convolution)
        __UpperCamelCase					:str        =		self.convolution(__lowercase)
        if self.normalization is not None:
            __UpperCamelCase					:Any        =		self.normalization(__lowercase)
        if self.activation is not None:
            __UpperCamelCase					:List[str]        =		self.activation(__lowercase)
        return features


class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    a__     :						List[str]      =				MobileNetVaConfig
    a__     :						Dict      =				load_tf_weights_in_mobilenet_va
    a__     :						Tuple      =				"""mobilenet_v1"""
    a__     :						Optional[Any]      =				"""pixel_values"""
    a__     :						int      =				False







    def       UpperCamelCase__       (     self			,			__lowercase)	->     None:
        if isinstance(__lowercase			,			(nn.Linear, nn.Convad)):
            module.weight.data.normal_(mean=0.0			,			std=self.config.initializer_range)
            if module.bias is not None:
                module.bias.data.zero_()
        elif isinstance(__lowercase			,			nn.BatchNormad):
            module.bias.data.zero_()
            module.weight.data.fill_(1.0)
__lowercase										= r'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

__lowercase										= r'''
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`MobileNetV1ImageProcessor.__call__`] for details.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
'''


@add_start_docstrings(
    """The bare MobileNetV1 model outputting raw hidden-states without any specific head on top."""						,     UpperCAmelCase_						,     )
class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    def __init__(     self			,			__lowercase			,			__lowercase = True)	->     Optional[Any]:
        super().__init__(__lowercase)
        __UpperCamelCase					:List[str]        =		config

        __UpperCamelCase					:Any        =		32
        __UpperCamelCase					:List[str]        =		max(int(depth * config.depth_multiplier)			,			config.min_depth)

        __UpperCamelCase					:Union[str, Any]        =		MobileNetVaConvLayer(
            __lowercase			,			in_channels=config.num_channels			,			out_channels=__lowercase			,			kernel_size=3			,			stride=2			,			)

        __UpperCamelCase					:str        =		[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

        __UpperCamelCase					:Any        =		nn.ModuleList()
        for i in range(13):
            __UpperCamelCase					:str        =		out_channels

            if strides[i] == 2 or i == 0:
                depth *= 2
                __UpperCamelCase					:Tuple        =		max(int(depth * config.depth_multiplier)			,			config.min_depth)

            self.layer.append(
                MobileNetVaConvLayer(
                    __lowercase			,			in_channels=__lowercase			,			out_channels=__lowercase			,			kernel_size=3			,			stride=strides[i]			,			groups=__lowercase			,			))

            self.layer.append(
                MobileNetVaConvLayer(
                    __lowercase			,			in_channels=__lowercase			,			out_channels=__lowercase			,			kernel_size=1			,			))

        __UpperCamelCase					:str        =		nn.AdaptiveAvgPoolad((1, 1)) if add_pooling_layer else None

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







    def       UpperCamelCase__       (     self			,			__lowercase)	->     Union[str, Any]:
        raise NotImplementedError







    @add_start_docstrings_to_model_forward(__lowercase)
    @add_code_sample_docstrings(
        checkpoint=_CHECKPOINT_FOR_DOC			,			output_type=__lowercase			,			config_class=_CONFIG_FOR_DOC			,			modality='''vision'''			,			expected_output=_EXPECTED_OUTPUT_SHAPE			,			)
    def       UpperCamelCase__       (     self			,			__lowercase = None			,			__lowercase = None			,			__lowercase = None			,			)	->     Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
        __UpperCamelCase					:Union[str, Any]        =		(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        __UpperCamelCase					:str        =		return_dict if return_dict is not None else self.config.use_return_dict

        if pixel_values is None:
            raise ValueError('''You have to specify pixel_values''')

        __UpperCamelCase					:int        =		self.conv_stem(__lowercase)

        __UpperCamelCase					:List[str]        =		() if output_hidden_states else None

        for i, layer_module in enumerate(self.layer):
            __UpperCamelCase					:Optional[Any]        =		layer_module(__lowercase)

            if output_hidden_states:
                __UpperCamelCase					:int        =		all_hidden_states + (hidden_states,)

        __UpperCamelCase					:Any        =		hidden_states

        if self.pooler is not None:
            __UpperCamelCase					:str        =		torch.flatten(self.pooler(__lowercase)			,			start_dim=1)
        else:
            __UpperCamelCase					:Tuple        =		None

        if not return_dict:
            return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None)

        return BaseModelOutputWithPoolingAndNoAttention(
            last_hidden_state=__lowercase			,			pooler_output=__lowercase			,			hidden_states=__lowercase			,			)


@add_start_docstrings(
    """
    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """						,     UpperCAmelCase_						,     )
class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    def __init__(     self			,			__lowercase)	->     None:
        super().__init__(__lowercase)

        __UpperCamelCase					:int        =		config.num_labels
        __UpperCamelCase					:Optional[int]        =		MobileNetVaModel(__lowercase)

        __UpperCamelCase					:Optional[Any]        =		self.mobilenet_va.layer[-1].convolution.out_channels

        # Classifier head
        __UpperCamelCase					:str        =		nn.Dropout(config.classifier_dropout_prob			,			inplace=__lowercase)
        __UpperCamelCase					:Dict        =		nn.Linear(__lowercase			,			config.num_labels) if config.num_labels > 0 else nn.Identity()

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







    @add_start_docstrings_to_model_forward(__lowercase)
    @add_code_sample_docstrings(
        checkpoint=_IMAGE_CLASS_CHECKPOINT			,			output_type=__lowercase			,			config_class=_CONFIG_FOR_DOC			,			expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,			)
    def       UpperCamelCase__       (     self			,			__lowercase = None			,			__lowercase = None			,			__lowercase = None			,			__lowercase = None			,			)	->     Union[tuple, ImageClassifierOutputWithNoAttention]:
        __UpperCamelCase					:List[Any]        =		return_dict if return_dict is not None else self.config.use_return_dict

        __UpperCamelCase					:Tuple        =		self.mobilenet_va(__lowercase			,			output_hidden_states=__lowercase			,			return_dict=__lowercase)

        __UpperCamelCase					:List[str]        =		outputs.pooler_output if return_dict else outputs[1]

        __UpperCamelCase					:Union[str, Any]        =		self.classifier(self.dropout(__lowercase))

        __UpperCamelCase					:int        =		None
        if labels is not None:
            if self.config.problem_type is None:
                if self.num_labels == 1:
                    __UpperCamelCase					:Tuple        =		'''regression'''
                elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                    __UpperCamelCase					:Union[str, Any]        =		'''single_label_classification'''
                else:
                    __UpperCamelCase					:Optional[Any]        =		'''multi_label_classification'''

            if self.config.problem_type == "regression":
                __UpperCamelCase					:Any        =		MSELoss()
                if self.num_labels == 1:
                    __UpperCamelCase					:List[str]        =		loss_fct(logits.squeeze()			,			labels.squeeze())
                else:
                    __UpperCamelCase					:Dict        =		loss_fct(__lowercase			,			__lowercase)
            elif self.config.problem_type == "single_label_classification":
                __UpperCamelCase					:Optional[int]        =		CrossEntropyLoss()
                __UpperCamelCase					:str        =		loss_fct(logits.view(-1			,			self.num_labels)			,			labels.view(-1))
            elif self.config.problem_type == "multi_label_classification":
                __UpperCamelCase					:Dict        =		BCEWithLogitsLoss()
                __UpperCamelCase					:List[str]        =		loss_fct(__lowercase			,			__lowercase)

        if not return_dict:
            __UpperCamelCase					:Tuple        =		(logits,) + outputs[2:]
            return ((loss,) + output) if loss is not None else output

        return ImageClassifierOutputWithNoAttention(
            loss=__lowercase			,			logits=__lowercase			,			hidden_states=outputs.hidden_states			,			)





