


"""simple docstring"""







import argparse
import os

import evaluate
import torch
from datasets import load_dataset
from torch.optim import AdamW
from torch.utils.data import DataLoader
from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed

from accelerate import Accelerator, DistributedType
from accelerate.local_sgd import LocalSGD


########################################################################
# This is a fully working simple example to use Accelerate
# with LocalSGD, which is a method to synchronize model
# parameters every K batches. It is different, but complementary
# to gradient accumulation.
#
# This example trains a Bert base model on GLUE MRPC
# in any of the following settings (with the same script):
#   - single CPU or single GPU
#   - multi GPUS (using PyTorch distributed mode)
#   - (multi) TPUs
#   - fp16 (mixed-precision) or fp32 (normal precision)
#
# To run it in each of these various modes, follow the instructions
# in the readme for examples:
# https://github.com/huggingface/accelerate/tree/main/examples
#
########################################################################


__lowercase    =				16
__lowercase    =				32


def 					lowercase      (    A_  ,       A_ = 16	)->				Any:
  '''simple docstring'''







  a						:		Tuple											=      AutoTokenizer.from_pretrained("bert-base-cased"	)
  a						:		str											=      load_dataset("glue"  ,       "mrpc"	)

  def tokenize_function(A_	):
    # max_length=None => use the model max length (it's actually the default)
    a						:		List[str]											=      tokenizer(examples["sentence1"]  ,       examples["sentence2"]  ,       truncation=A_  ,       max_length=A_	)
    return outputs

  # Apply the method we just defined to all the examples in all the splits of the dataset
  # starting with the main process first:
  with accelerator.main_process_first():
    a						:		List[Any]											=      datasets.map(
        A_  ,       batched=A_  ,       remove_columns=["idx", "sentence1", "sentence2"]  ,       )

  # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the
  # transformers library
  a						:		int											=      tokenized_datasets.rename_column("label"  ,       "labels"	)

  def collate_fn(A_	):
    # On TPU it's best to pad everything to the same length or training will be very slow.
    a						:		List[str]											=      128 if accelerator.distributed_type == DistributedType.TPU else None
    # When using mixed precision we want round multiples of 8/16
    if accelerator.mixed_precision == "fp8":
      a						:		List[Any]											=      16
    elif accelerator.mixed_precision != "no":
      a						:		Any											=      8
    else:
      a						:		Union[str, Any]											=      None

    return tokenizer.pad(
        A_  ,       padding="longest"  ,       max_length=A_  ,       pad_to_multiple_of=A_  ,       return_tensors="pt"  ,       )

  # Instantiate dataloaders.
  a						:		Optional[Any]											=      DataLoader(
      tokenized_datasets["train"]  ,       shuffle=A_  ,       collate_fn=A_  ,       batch_size=A_	)
  a						:		Any											=      DataLoader(
      tokenized_datasets["validation"]  ,       shuffle=A_  ,       collate_fn=A_  ,       batch_size=A_	)

  return train_dataloader, eval_dataloader


# For testing only
if os.environ.get("""TESTING_MOCKED_DATALOADERS""", None) == "1":
 from accelerate.test_utils.training import mocked_dataloaders

 __lowercase    =				mocked_dataloaders  # noqa: F811


def 					lowercase      (    A_  ,       A_	)->				str:
  '''simple docstring'''







  if os.environ.get("TESTING_MOCKED_DATALOADERS"  ,       A_	) == "1":
    a						:		Union[str, Any]											=      2
  # New Code #
  a						:		Union[str, Any]											=      int(args.gradient_accumulation_steps	)
  a						:		List[str]											=      int(args.local_sgd_steps	)
  # Initialize accelerator
  a						:		List[Any]											=      Accelerator(
      cpu=args.cpu  ,       mixed_precision=args.mixed_precision  ,       gradient_accumulation_steps=A_	)
  if accelerator.distributed_type not in [DistributedType.NO, DistributedType.MULTI_CPU, DistributedType.MULTI_GPU]:
    raise NotImplementedError("LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)"	)
  # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs
  a						:		str											=      config["lr"]
  a						:		List[str]											=      int(config["num_epochs"]	)
  a						:		Dict											=      int(config["seed"]	)
  a						:		Optional[Any]											=      int(config["batch_size"]	)

  a						:		str											=      evaluate.load("glue"  ,       "mrpc"	)

  set_seed(A_	)
  a							,		a						:		Optional[Any]											=      get_dataloaders(A_  ,       A_	)
  # Instantiate the model (we build the model here so that the seed also control new weights initialization)
  a						:		Union[str, Any]											=      AutoModelForSequenceClassification.from_pretrained("bert-base-cased"  ,       return_dict=A_	)

  # We could avoid this line since the accelerator is set with `device_placement=True` (default value).
  # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer
  # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that).
  a						:		Any											=      model.to(accelerator.device	)

  # Instantiate optimizer
  a						:		List[Any]											=      AdamW(params=model.parameters()  ,       lr=A_	)

  # Instantiate scheduler
  a						:		List[Any]											=      get_linear_schedule_with_warmup(
      optimizer=A_  ,       num_warmup_steps=100  ,       num_training_steps=(len(A_	) * num_epochs)  ,       )

  # Prepare everything
  # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the
  # prepare method.
  a							,		a							,		a							,		a							,		a						:		Any											=      accelerator.prepare(
      A_  ,       A_  ,       A_  ,       A_  ,       A_	)

  # Now we train the model
  for epoch in range(A_	):
    model.train()
    with LocalSGD(
        accelerator=A_  ,       model=A_  ,       local_sgd_steps=A_  ,       enabled=local_sgd_steps is not None	) as local_sgd:
      for step, batch in enumerate(A_	):
        # We could avoid this line since we set the accelerator with `device_placement=True`.
        batch.to(accelerator.device	)
        # New code #
        # We use the new `accumulate` context manager to perform gradient accumulation
        # We also currently do not support TPUs nor advise it as bugs were found on the XLA side when running our tests.
        with accelerator.accumulate(A_	):
          a						:		Union[str, Any]											=      model(**A_	)
          a						:		List[Any]											=      output.loss
          accelerator.backward(A_	)
          optimizer.step()
          lr_scheduler.step()
          optimizer.zero_grad()
          # LocalSGD-specific line
          local_sgd.step()

    model.eval()
    for step, batch in enumerate(A_	):
      # We could avoid this line since we set the accelerator with `device_placement=True`.
      batch.to(accelerator.device	)
      with torch.no_grad():
        a						:		List[Any]											=      model(**A_	)
      a						:		Optional[int]											=      outputs.logits.argmax(dim=-1	)
      a							,		a						:		List[Any]											=      accelerator.gather_for_metrics((predictions, batch["labels"])	)
      metric.add_batch(
          predictions=A_  ,       references=A_  ,       )

    a						:		int											=      metric.compute()
    # Use accelerator.print to print only on the main process.
    accelerator.print(F'''epoch {epoch}:'''  ,       A_	)


def 					lowercase      (    )->				int:
  '''simple docstring'''







  a						:		Optional[Any]											=      argparse.ArgumentParser(description="Simple example of training script."	)
  parser.add_argument(
      "--mixed_precision"  ,       type=A_  ,       default=A_  ,       choices=["no", "fp16", "bf16", "fp8"]  ,       help="Whether to use mixed precision. Choose"
      "between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10."
      "and an Nvidia Ampere GPU."  ,       )
  # New Code #
  parser.add_argument(
      "--gradient_accumulation_steps"  ,       type=A_  ,       default=1  ,       help="The number of minibatches to be ran before gradients are accumulated."  ,       )
  parser.add_argument(
      "--local_sgd_steps"  ,       type=A_  ,       default=8  ,       help="Number of local SGD steps or None to disable local SGD"	)
  parser.add_argument("--cpu"  ,       action="store_true"  ,       help="If passed, will train on the CPU."	)
  a						:		Union[str, Any]											=      parser.parse_args()
  a						:		List[str]											=      {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16}
  training_function(A_  ,       A_	)


if __name__ == "__main__":
 main()










"""simple docstring"""







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 					lowercase      (    A_  ,       A_  ,       A_=None	)->				int:
  '''simple docstring'''







  a						:		Union[str, Any]											=      {}

  if isinstance(A_  ,       A_	):
    a						:		Tuple											=      model.mobilenet_va
  else:
    a						:		Optional[int]											=      model

  a						:		Any											=      "MobilenetV1/Conv2d_0/"
  a						:		List[Any]											=      backbone.conv_stem.convolution.weight
  a						:		Tuple											=      backbone.conv_stem.normalization.bias
  a						:		int											=      backbone.conv_stem.normalization.weight
  a						:		Optional[int]											=      backbone.conv_stem.normalization.running_mean
  a						:		Dict											=      backbone.conv_stem.normalization.running_var

  for i in range(13	):
    a						:		Dict											=      i + 1
    a						:		str											=      i * 2

    a						:		str											=      backbone.layer[pt_index]
    a						:		Tuple											=      F'''MobilenetV1/Conv2d_{tf_index}_depthwise/'''
    a						:		Union[str, Any]											=      pointer.convolution.weight
    a						:		Optional[int]											=      pointer.normalization.bias
    a						:		Any											=      pointer.normalization.weight
    a						:		Optional[int]											=      pointer.normalization.running_mean
    a						:		Dict											=      pointer.normalization.running_var

    a						:		Dict											=      backbone.layer[pt_index + 1]
    a						:		List[str]											=      F'''MobilenetV1/Conv2d_{tf_index}_pointwise/'''
    a						:		Dict											=      pointer.convolution.weight
    a						:		Any											=      pointer.normalization.bias
    a						:		List[Any]											=      pointer.normalization.weight
    a						:		Tuple											=      pointer.normalization.running_mean
    a						:		List[str]											=      pointer.normalization.running_var

  if isinstance(A_  ,       A_	):
    a						:		Dict											=      "MobilenetV1/Logits/Conv2d_1c_1x1/"
    a						:		List[Any]											=      model.classifier.weight
    a						:		Optional[int]											=      model.classifier.bias

  return tf_to_pt_map


def 					lowercase      (    A_  ,       A_  ,       A_	)->				Optional[int]:
  '''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
  a						:		List[str]											=      tf.train.list_variables(A_	)
  a						:		Dict											=      {}
  for name, shape in init_vars:
    logger.info(F'''Loading TF weight {name} with shape {shape}'''	)
    a						:		List[str]											=      tf.train.load_variable(A_  ,       A_	)
    a						:		int											=      array

  # Build TF to PyTorch weights loading map
  a						:		int											=      _build_tf_to_pytorch_map(A_  ,       A_  ,       A_	)

  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

    a						:		Tuple											=      tf_weights[name]

    if "depthwise_weights" in name:
      logger.info("Transposing depthwise"	)
      a						:		Optional[Any]											=      np.transpose(A_  ,       (2, 3, 0, 1)	)
    elif "weights" in name:
      logger.info("Transposing"	)
      if len(pointer.shape	) == 2:  # copying into linear layer
        a						:		List[str]											=      array.squeeze().transpose()
      else:
        a						:		List[str]											=      np.transpose(A_  ,       (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}'''	)
    a						:		Any											=      torch.from_numpy(A_	)

    tf_weights.pop(A_  ,       A_	)
    tf_weights.pop(name + "/RMSProp"  ,       A_	)
    tf_weights.pop(name + "/RMSProp_1"  ,       A_	)
    tf_weights.pop(name + "/ExponentialMovingAverage"  ,       A_	)

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


def 					lowercase      (    A_  ,       A_	)->				torch.Tensor:
  '''simple docstring'''







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

  if in_height % stride_height == 0:
    a						:		Union[str, Any]											=      max(kernel_height - stride_height  ,       0	)
  else:
    a						:		Tuple											=      max(kernel_height - (in_height % stride_height)  ,       0	)

  if in_width % stride_width == 0:
    a						:		Optional[int]											=      max(kernel_width - stride_width  ,       0	)
  else:
    a						:		int											=      max(kernel_width - (in_width % stride_width)  ,       0	)

  a						:		List[Any]											=      pad_along_width // 2
  a						:		List[str]											=      pad_along_width - pad_left
  a						:		str											=      pad_along_height // 2
  a						:		Any											=      pad_along_height - pad_top

  a						:		Tuple											=      (pad_left, pad_right, pad_top, pad_bottom)
  return nn.functional.pad(A_  ,       A_  ,       "constant"  ,       0.0	)






class 						_A  (  nn.Module  ):







    """simple docstring"""




    def __init__( self    :   Dict    ,			__UpperCAmelCase    :   MobileNetVaConfig    ,			__UpperCAmelCase    :   int    ,			__UpperCAmelCase    :   int    ,			__UpperCAmelCase    :   int    ,			__UpperCAmelCase    :   Optional[int] = 1    ,			__UpperCAmelCase    :   Optional[int] = 1    ,			__UpperCAmelCase    :   bool = False    ,			__UpperCAmelCase    :   Optional[bool] = True    ,			__UpperCAmelCase    :   Optional[bool or str] = True    ,			):
      super().__init__()
      a						:		Optional[Any]											=      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.''')

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

      a						:		List[str]											=      nn.Convad(
          in_channels=__UpperCAmelCase    ,			out_channels=__UpperCAmelCase    ,			kernel_size=__UpperCAmelCase    ,			stride=__UpperCAmelCase    ,			padding=__UpperCAmelCase    ,			groups=__UpperCAmelCase    ,			bias=__UpperCAmelCase    ,			padding_mode="zeros"    ,			)

      if use_normalization:
        a						:		str											=      nn.BatchNormad(
            num_features=__UpperCAmelCase    ,			eps=config.layer_norm_eps    ,			momentum=0.9_997    ,			affine=__UpperCAmelCase    ,			track_running_stats=__UpperCAmelCase    ,			)
      else:
        a						:		List[str]											=      None

      if use_activation:
        if isinstance(__UpperCAmelCase    ,			__UpperCAmelCase):
          a						:		Optional[int]											=      ACTaFN[use_activation]
        elif isinstance(config.hidden_act    ,			__UpperCAmelCase):
          a						:		Optional[Any]											=      ACTaFN[config.hidden_act]
        else:
          a						:		str											=      config.hidden_act
      else:
        a						:		Dict											=      None






    def      __snake_case							( self    :   Tuple    ,			__UpperCAmelCase    :   torch.Tensor):
      if self.config.tf_padding:
        a						:		Tuple											=      apply_tf_padding(__UpperCAmelCase    ,			self.convolution)
      a						:		Dict											=      self.convolution(__UpperCAmelCase)
      if self.normalization is not None:
        a						:		List[str]											=      self.normalization(__UpperCAmelCase)
      if self.activation is not None:
        a						:		Optional[Any]											=      self.activation(__UpperCAmelCase)
      return features






class 						_A  (  _a  ):







    """simple docstring"""




    UpperCAmelCase	:							Union[str, Any]     =	MobileNetVaConfig
    UpperCAmelCase	:							str     =	load_tf_weights_in_mobilenet_va
    UpperCAmelCase	:							List[str]     =	"""mobilenet_v1"""
    UpperCAmelCase	:							Dict     =	"""pixel_values"""
    UpperCAmelCase	:							str     =	False






    def      __snake_case							( self    :   str    ,			__UpperCAmelCase    :   Union[nn.Linear, nn.Convad]):
      if isinstance(__UpperCAmelCase    ,			(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(__UpperCAmelCase    ,			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."""   ,_a   ,)
class 						_A  (  _a  ):







    """simple docstring"""




    def __init__( self    :   Optional[int]    ,			__UpperCAmelCase    :   MobileNetVaConfig    ,			__UpperCAmelCase    :   bool = True):
      super().__init__(__UpperCAmelCase)
      a						:		List[str]											=      config

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

      a						:		List[str]											=      MobileNetVaConvLayer(
          __UpperCAmelCase    ,			in_channels=config.num_channels    ,			out_channels=__UpperCAmelCase    ,			kernel_size=3    ,			stride=2    ,			)

      a						:		int											=      [1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

      a						:		List[Any]											=      nn.ModuleList()
      for i in range(13):
        a						:		Optional[Any]											=      out_channels

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

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

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

      a						:		Optional[Any]											=      nn.AdaptiveAvgPoolad((1, 1)) if add_pooling_layer else None

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






    def      __snake_case							( self    :   Optional[Any]    ,			__UpperCAmelCase    :   Any):
      raise NotImplementedError






    @add_start_docstrings_to_model_forward(__UpperCAmelCase)
    @add_code_sample_docstrings(
        checkpoint=_CHECKPOINT_FOR_DOC    ,			output_type=__UpperCAmelCase    ,			config_class=_CONFIG_FOR_DOC    ,			modality="vision"    ,			expected_output=_EXPECTED_OUTPUT_SHAPE    ,			)
    def      __snake_case							( self    :   List[Any]    ,			__UpperCAmelCase    :   Optional[torch.Tensor] = None    ,			__UpperCAmelCase    :   Optional[bool] = None    ,			__UpperCAmelCase    :   Optional[bool] = None    ,			):
      a						:		str											=      (
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      a						:		Optional[Any]											=      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")

      a						:		Optional[int]											=      self.conv_stem(__UpperCAmelCase)

      a						:		Tuple											=      () if output_hidden_states else None

      for i, layer_module in enumerate(self.layer):
        a						:		Union[str, Any]											=      layer_module(__UpperCAmelCase)

        if output_hidden_states:
          a						:		str											=      all_hidden_states + (hidden_states,)

      a						:		Dict											=      hidden_states

      if self.pooler is not None:
        a						:		Optional[int]											=      torch.flatten(self.pooler(__UpperCAmelCase)    ,			start_dim=1)
      else:
        a						:		List[str]											=      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=__UpperCAmelCase    ,			pooler_output=__UpperCAmelCase    ,			hidden_states=__UpperCAmelCase    ,			)






@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.
    """   ,_a   ,)
class 						_A  (  _a  ):







    """simple docstring"""




    def __init__( self    :   int    ,			__UpperCAmelCase    :   MobileNetVaConfig):
      super().__init__(__UpperCAmelCase)

      a						:		Dict											=      config.num_labels
      a						:		Union[str, Any]											=      MobileNetVaModel(__UpperCAmelCase)

      a						:		List[Any]											=      self.mobilenet_va.layer[-1].convolution.out_channels

      # Classifier head
      a						:		Union[str, Any]											=      nn.Dropout(config.classifier_dropout_prob    ,			inplace=__UpperCAmelCase)
      a						:		Optional[int]											=      nn.Linear(__UpperCAmelCase    ,			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(__UpperCAmelCase)
    @add_code_sample_docstrings(
        checkpoint=_IMAGE_CLASS_CHECKPOINT    ,			output_type=__UpperCAmelCase    ,			config_class=_CONFIG_FOR_DOC    ,			expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT    ,			)
    def      __snake_case							( self    :   Dict    ,			__UpperCAmelCase    :   Optional[torch.Tensor] = None    ,			__UpperCAmelCase    :   Optional[bool] = None    ,			__UpperCAmelCase    :   Optional[torch.Tensor] = None    ,			__UpperCAmelCase    :   Optional[bool] = None    ,			):
      a						:		Union[str, Any]											=      return_dict if return_dict is not None else self.config.use_return_dict

      a						:		Dict											=      self.mobilenet_va(__UpperCAmelCase    ,			output_hidden_states=__UpperCAmelCase    ,			return_dict=__UpperCAmelCase)

      a						:		List[Any]											=      outputs.pooler_output if return_dict else outputs[1]

      a						:		List[Any]											=      self.classifier(self.dropout(__UpperCAmelCase))

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

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

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

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






