




'''simple docstring'''



import logging

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

from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.albert.modeling_albert import (
    ALBERT_INPUTS_DOCSTRING,
    ALBERT_START_DOCSTRING,
    AlbertModel,
    AlbertPreTrainedModel,
    AlbertTransformer,
)


lowerCamelCase_	 =		logging.getLogger(__name__)







class 	lowercase_					(				A     ):


   """simple docstring"""

   def 							lowerCAmelCase_    (					self      :			List[Any]						,   __lowerCamelCase      :			Optional[Any]						,   __lowerCamelCase      :			int						,   __lowerCamelCase      :			Optional[int]=None						,   __lowerCamelCase      :			int=None					):

        """simple docstring"""
        if current_layer == 0:
             _SCREAMING_SNAKE_CASE =   self.embedding_hidden_mapping_in(__lowerCamelCase					)
        else:
             _SCREAMING_SNAKE_CASE =   hidden_states[0]

        _SCREAMING_SNAKE_CASE =   int(self.config.num_hidden_layers / self.config.num_hidden_groups					)

        # Index of the hidden group
        _SCREAMING_SNAKE_CASE =   int(current_layer / (self.config.num_hidden_layers / self.config.num_hidden_groups)					)

        _SCREAMING_SNAKE_CASE =   self.albert_layer_groups[group_idx](
            __lowerCamelCase						,   __lowerCamelCase						,   head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group]						,   )
        _SCREAMING_SNAKE_CASE =   layer_group_output[0]

        return (hidden_states,)







@add_start_docstrings(
    '''The bare ALBERT Model transformer with PABEE outputting raw hidden-states without any specific head on top.'''  ,						A  ,						)
class 	lowercase_					(				A     ):


   """simple docstring"""

   def __init__(					self      :			List[str]						,   __lowerCamelCase      :			List[Any]					):

        """simple docstring"""
        super().__init__(__lowerCamelCase					)

        _SCREAMING_SNAKE_CASE =   AlbertTransformerWithPabee(__lowerCamelCase					)

        self.init_weights()
        _SCREAMING_SNAKE_CASE =   0
        _SCREAMING_SNAKE_CASE =   0
        _SCREAMING_SNAKE_CASE =   0

        _SCREAMING_SNAKE_CASE =   0



   def 							lowerCAmelCase_    (					self      :			List[Any]						,   __lowerCamelCase      :			int					):

        """simple docstring"""
        _SCREAMING_SNAKE_CASE =   threshold



   def 							lowerCAmelCase_    (					self      :			Union[str, Any]						,   __lowerCamelCase      :			Dict					):

        """simple docstring"""
        _SCREAMING_SNAKE_CASE =   patience



   def 							lowerCAmelCase_    (					self      :			List[Any]					):

        """simple docstring"""
        _SCREAMING_SNAKE_CASE =   0
        _SCREAMING_SNAKE_CASE =   0



   def 							lowerCAmelCase_    (					self      :			List[str]					):

        """simple docstring"""
        _SCREAMING_SNAKE_CASE =   self.inference_layers_num / self.inference_instances_num
        _SCREAMING_SNAKE_CASE =   (
            F"""*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up ="""
            F""" {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***"""
        )
        print(__lowerCamelCase					)



   @add_start_docstrings_to_model_forward(__lowerCamelCase					)
   def 							lowerCAmelCase_    (					self      :			Dict						,   __lowerCamelCase      :			Optional[Any]=None						,   __lowerCamelCase      :			Optional[int]=None						,   __lowerCamelCase      :			Any=None						,   __lowerCamelCase      :			Any=None						,   __lowerCamelCase      :			Tuple=None						,   __lowerCamelCase      :			Any=None						,   __lowerCamelCase      :			List[Any]=None						,   __lowerCamelCase      :			List[Any]=None						,   __lowerCamelCase      :			Optional[int]=False						,   ):

        """simple docstring"""

        if input_ids is not None and inputs_embeds is not None:
             raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time"					)
        elif input_ids is not None:
             _SCREAMING_SNAKE_CASE =   input_ids.size()
        elif inputs_embeds is not None:
             _SCREAMING_SNAKE_CASE =   inputs_embeds.size()[:-1]
        else:
             raise ValueError("You have to specify either input_ids or inputs_embeds"					)

        _SCREAMING_SNAKE_CASE =   input_ids.device if input_ids is not None else inputs_embeds.device

        if attention_mask is None:
             _SCREAMING_SNAKE_CASE =   torch.ones(__lowerCamelCase						,   device=__lowerCamelCase					)
        if token_type_ids is None:
             _SCREAMING_SNAKE_CASE =   torch.zeros(__lowerCamelCase						,   dtype=torch.long						,   device=__lowerCamelCase					)

        _SCREAMING_SNAKE_CASE =   attention_mask.unsqueeze(1					).unsqueeze(2					)
        _SCREAMING_SNAKE_CASE =   extended_attention_mask.to(dtype=self.dtype					)  # fp16 compatibility
        _SCREAMING_SNAKE_CASE =   (1.0 - extended_attention_mask) * -1_0_0_0_0.0
        _SCREAMING_SNAKE_CASE =   self.get_head_mask(__lowerCamelCase						,   self.config.num_hidden_layers					)

        _SCREAMING_SNAKE_CASE =   self.embeddings(
            __lowerCamelCase						,   position_ids=__lowerCamelCase						,   token_type_ids=__lowerCamelCase						,   inputs_embeds=__lowerCamelCase					)
        _SCREAMING_SNAKE_CASE =   embedding_output

        if self.training:
             _SCREAMING_SNAKE_CASE =   []
             for i in range(self.config.num_hidden_layers					):
                  _SCREAMING_SNAKE_CASE =   self.encoder.adaptive_forward(
                      __lowerCamelCase						,   current_layer=__lowerCamelCase						,   attention_mask=__lowerCamelCase						,   head_mask=__lowerCamelCase						,   )

                  _SCREAMING_SNAKE_CASE =   self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]					)					)
                  _SCREAMING_SNAKE_CASE =   output_layers[i](output_dropout(__lowerCamelCase					)					)
                  res.append(__lowerCamelCase					)
        elif self.patience == 0:  # Use all layers for inference
             _SCREAMING_SNAKE_CASE =   self.encoder(__lowerCamelCase						,   __lowerCamelCase						,   head_mask=__lowerCamelCase					)
             _SCREAMING_SNAKE_CASE =   self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]					)					)
             _SCREAMING_SNAKE_CASE =   [output_layers[self.config.num_hidden_layers - 1](__lowerCamelCase					)]
        else:
             _SCREAMING_SNAKE_CASE =   0
             _SCREAMING_SNAKE_CASE =   None
             _SCREAMING_SNAKE_CASE =   0
             for i in range(self.config.num_hidden_layers					):
                  calculated_layer_num += 1
                  _SCREAMING_SNAKE_CASE =   self.encoder.adaptive_forward(
                      __lowerCamelCase						,   current_layer=__lowerCamelCase						,   attention_mask=__lowerCamelCase						,   head_mask=__lowerCamelCase						,   )

                  _SCREAMING_SNAKE_CASE =   self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]					)					)
                  _SCREAMING_SNAKE_CASE =   output_layers[i](__lowerCamelCase					)
                  if regression:
                       _SCREAMING_SNAKE_CASE =   logits.detach()
                       if patient_result is not None:
                            _SCREAMING_SNAKE_CASE =   patient_result.detach()
                       if (patient_result is not None) and torch.abs(patient_result - labels					) < self.regression_threshold:
                            patient_counter += 1
                       else:
                            _SCREAMING_SNAKE_CASE =   0
                  else:
                       _SCREAMING_SNAKE_CASE =   logits.detach().argmax(dim=1					)
                       if patient_result is not None:
                            _SCREAMING_SNAKE_CASE =   patient_result.detach().argmax(dim=1					)
                       if (patient_result is not None) and torch.all(labels.eq(__lowerCamelCase					)					):
                            patient_counter += 1
                       else:
                            _SCREAMING_SNAKE_CASE =   0

                  _SCREAMING_SNAKE_CASE =   logits
                  if patient_counter == self.patience:
                       break
             _SCREAMING_SNAKE_CASE =   [patient_result]
             self.inference_layers_num += calculated_layer_num
             self.inference_instances_num += 1

        return res







@add_start_docstrings(
    '''Albert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of
    the pooled output) e.g. for GLUE tasks. '''  ,						A  ,						)
class 	lowercase_					(				A     ):


   """simple docstring"""

   def __init__(					self      :			Dict						,   __lowerCamelCase      :			str					):

        """simple docstring"""
        super().__init__(__lowerCamelCase					)
        _SCREAMING_SNAKE_CASE =   config.num_labels

        _SCREAMING_SNAKE_CASE =   AlbertModelWithPabee(__lowerCamelCase					)
        _SCREAMING_SNAKE_CASE =   nn.Dropout(config.classifier_dropout_prob					)
        _SCREAMING_SNAKE_CASE =   nn.ModuleList(
            [nn.Linear(config.hidden_size						,   self.config.num_labels					) for _ in range(config.num_hidden_layers					)]					)

        self.init_weights()



   @add_start_docstrings_to_model_forward(__lowerCamelCase					)
   def 							lowerCAmelCase_    (					self      :			str						,   __lowerCamelCase      :			int=None						,   __lowerCamelCase      :			Union[str, Any]=None						,   __lowerCamelCase      :			Optional[Any]=None						,   __lowerCamelCase      :			Dict=None						,   __lowerCamelCase      :			Optional[Any]=None						,   __lowerCamelCase      :			int=None						,   __lowerCamelCase      :			Any=None						,   ):

        """simple docstring"""
        _SCREAMING_SNAKE_CASE =   self.albert(
            input_ids=__lowerCamelCase						,   attention_mask=__lowerCamelCase						,   token_type_ids=__lowerCamelCase						,   position_ids=__lowerCamelCase						,   head_mask=__lowerCamelCase						,   inputs_embeds=__lowerCamelCase						,   output_dropout=self.dropout						,   output_layers=self.classifiers						,   regression=self.num_labels == 1						,   )

        _SCREAMING_SNAKE_CASE =   (logits[-1],)

        if labels is not None:
             _SCREAMING_SNAKE_CASE =   None
             _SCREAMING_SNAKE_CASE =   0
             for ix, logits_item in enumerate(__lowerCamelCase					):
                  if self.num_labels == 1:
                       #  We are doing regression
                       _SCREAMING_SNAKE_CASE =   MSELoss()
                       _SCREAMING_SNAKE_CASE =   loss_fct(logits_item.view(-1					)						,   labels.view(-1					)					)
                  else:
                       _SCREAMING_SNAKE_CASE =   CrossEntropyLoss()
                       _SCREAMING_SNAKE_CASE =   loss_fct(logits_item.view(-1						,   self.num_labels					)						,   labels.view(-1					)					)
                  if total_loss is None:
                       _SCREAMING_SNAKE_CASE =   loss
                  else:
                       total_loss += loss * (ix + 1)
                  total_weights += ix + 1
             _SCREAMING_SNAKE_CASE =   (total_loss / total_weights,) + outputs

        return outputs







'''simple docstring'''



import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:







 class 	lowercase_					:


    """simple docstring"""

    @staticmethod
    def 							lowerCAmelCase_    (					*__lowerCamelCase      :			Union[str, Any]						,   **__lowerCamelCase      :			str					):

       """simple docstring"""
       pass







@is_pipeline_test
@require_torch_or_tf
@require_vision
class 	lowercase_					(				unittest.TestCase     ):


   """simple docstring"""

   lowerCamelCase_       						=		MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
   lowerCamelCase_       						=		TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING



   def 							lowerCAmelCase_    (					self      :			Any						,   __lowerCamelCase      :			Tuple						,   __lowerCamelCase      :			Dict						,   __lowerCamelCase      :			Optional[Any]					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   ImageClassificationPipeline(model=__lowerCamelCase						,   image_processor=__lowerCamelCase						,   top_k=2					)
      _SCREAMING_SNAKE_CASE =   [
          Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					),
          "http://images.cocodataset.org/val2017/000000039769.jpg",
      ]
      return image_classifier, examples



   def 							lowerCAmelCase_    (					self      :			Any						,   __lowerCamelCase      :			Optional[Any]						,   __lowerCamelCase      :			Tuple					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   image_classifier("./tests/fixtures/tests_samples/COCO/000000039769.png"					)

      self.assertEqual(
          __lowerCamelCase						,   [
              {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
          ]						,   )

      import datasets

      _SCREAMING_SNAKE_CASE =   datasets.load_dataset("hf-internal-testing/fixtures_image_utils"						,   "image"						,   split="test"					)

      # Accepts URL + PIL.Image + lists
      _SCREAMING_SNAKE_CASE =   image_classifier(
          [
              Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					),
              "http://images.cocodataset.org/val2017/000000039769.jpg",
              # RGBA
              dataset[0]["file"],
              # LA
              dataset[1]["file"],
              # L
              dataset[2]["file"],
          ]					)
      self.assertEqual(
          __lowerCamelCase						,   [
              [
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              ],
              [
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              ],
              [
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              ],
              [
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              ],
              [
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
                  {"score": ANY(__lowerCamelCase					), "label": ANY(__lowerCamelCase					)},
              ],
          ]						,   )



   @require_torch
   def 							lowerCAmelCase_    (					self      :			Dict					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   "hf-internal-testing/tiny-random-vit"
      _SCREAMING_SNAKE_CASE =   pipeline("image-classification"						,   model=__lowerCamelCase					)

      _SCREAMING_SNAKE_CASE =   image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"					)
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}]						,   )

      _SCREAMING_SNAKE_CASE =   image_classifier(
          [
              "http://images.cocodataset.org/val2017/000000039769.jpg",
              "http://images.cocodataset.org/val2017/000000039769.jpg",
          ]						,   top_k=2						,   )
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
          ]						,   )



   @require_tf
   def 							lowerCAmelCase_    (					self      :			Optional[Any]					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   "hf-internal-testing/tiny-random-vit"
      _SCREAMING_SNAKE_CASE =   pipeline("image-classification"						,   model=__lowerCamelCase						,   framework="tf"					)

      _SCREAMING_SNAKE_CASE =   image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"					)
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}]						,   )

      _SCREAMING_SNAKE_CASE =   image_classifier(
          [
              "http://images.cocodataset.org/val2017/000000039769.jpg",
              "http://images.cocodataset.org/val2017/000000039769.jpg",
          ]						,   top_k=2						,   )
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
          ]						,   )



   def 							lowerCAmelCase_    (					self      :			Any					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   PreTrainedTokenizer()

      # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
      _SCREAMING_SNAKE_CASE =   pipeline(
          "image-classification"						,   model="hf-internal-testing/tiny-random-vit"						,   tokenizer=__lowerCamelCase					)

      self.assertIs(image_classifier.tokenizer						,   __lowerCamelCase					)



   @slow
   @require_torch
   def 							lowerCAmelCase_    (					self      :			List[str]					):

      """simple docstring"""
      # Perceiver is not tested by `run_pipeline_test` properly.
      # That is because the type of feature_extractor and model preprocessor need to be kept
      # in sync, which is not the case in the current design
      _SCREAMING_SNAKE_CASE =   pipeline("image-classification"						,   model="deepmind/vision-perceiver-conv"					)
      _SCREAMING_SNAKE_CASE =   image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"					)
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [
              {"score": 0.4_3_8_5, "label": "tabby, tabby cat"},
              {"score": 0.3_2_1, "label": "tiger cat"},
              {"score": 0.0_5_0_2, "label": "Egyptian cat"},
              {"score": 0.0_1_3_7, "label": "crib, cot"},
              {"score": 0.0_0_7, "label": "radiator"},
          ]						,   )

      _SCREAMING_SNAKE_CASE =   pipeline("image-classification"						,   model="deepmind/vision-perceiver-fourier"					)
      _SCREAMING_SNAKE_CASE =   image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"					)
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [
              {"score": 0.5_6_5_8, "label": "tabby, tabby cat"},
              {"score": 0.1_3_0_9, "label": "tiger cat"},
              {"score": 0.0_7_2_2, "label": "Egyptian cat"},
              {"score": 0.0_7_0_7, "label": "remote control, remote"},
              {"score": 0.0_0_8_2, "label": "computer keyboard, keypad"},
          ]						,   )

      _SCREAMING_SNAKE_CASE =   pipeline("image-classification"						,   model="deepmind/vision-perceiver-learned"					)
      _SCREAMING_SNAKE_CASE =   image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"					)
      self.assertEqual(
          nested_simplify(__lowerCamelCase						,   decimals=4					)						,   [
              {"score": 0.3_0_2_2, "label": "tabby, tabby cat"},
              {"score": 0.2_3_6_2, "label": "Egyptian cat"},
              {"score": 0.1_8_5_6, "label": "tiger cat"},
              {"score": 0.0_3_2_4, "label": "remote control, remote"},
              {"score": 0.0_0_9_6, "label": "quilt, comforter, comfort, puff"},
          ]						,   )

