




'''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_			:     Tuple							    =     logging.getLogger(__name__)







class     SCREAMING_SNAKE_CASE						(	snake_case_							):






      def        lowercase_	(   self      :     int	,	lowercase__      :     str	,	lowercase__      :     str	,	lowercase__      :     Any=None	,	lowercase__      :     str=None					):


           '''simple docstring'''

           if current_layer == 0:
                a_    :       List[Any]    	=     self.embedding_hidden_mapping_in(lowercase__					)
           else:
                a_    :       List[str]    	=     hidden_states[0]

           a_    :       Tuple    	=     int(self.config.num_hidden_layers / self.config.num_hidden_groups					)

           # Index of the hidden group
           a_    :       Optional[Any]    	=     int(current_layer / (self.config.num_hidden_layers / self.config.num_hidden_groups)					)

           a_    :       int    	=     self.albert_layer_groups[group_idx](
               lowercase__	,	lowercase__	,	head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group]	,	)
           a_    :       int    	=     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.'''       ,			snake_case_       ,			)
class     SCREAMING_SNAKE_CASE						(	snake_case_							):






      def __init__(   self      :     int	,	lowercase__      :     Optional[int]					):


           '''simple docstring'''

           super().__init__(lowercase__					)

           a_    :       str    	=     AlbertTransformerWithPabee(lowercase__					)

           self.init_weights()
           a_    :       Any    	=     0
           a_    :       Optional[Any]    	=     0
           a_    :       Optional[int]    	=     0

           a_    :       int    	=     0






      def        lowercase_	(   self      :     Union[str, Any]	,	lowercase__      :     List[str]					):


           '''simple docstring'''

           a_    :       List[Any]    	=     threshold






      def        lowercase_	(   self      :     str	,	lowercase__      :     Any					):


           '''simple docstring'''

           a_    :       str    	=     patience






      def        lowercase_	(   self      :     Optional[Any]					):


           '''simple docstring'''

           a_    :       int    	=     0
           a_    :       Tuple    	=     0






      def        lowercase_	(   self      :     Dict					):


           '''simple docstring'''

           a_    :       Optional[Any]    	=     self.inference_layers_num / self.inference_instances_num
           a_    :       int    	=     (
               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(lowercase__					)






      @add_start_docstrings_to_model_forward(lowercase__					)
      def        lowercase_	(   self      :     List[str]	,	lowercase__      :     Dict=None	,	lowercase__      :     Optional[Any]=None	,	lowercase__      :     Any=None	,	lowercase__      :     int=None	,	lowercase__      :     Union[str, Any]=None	,	lowercase__      :     Tuple=None	,	lowercase__      :     int=None	,	lowercase__      :     Dict=None	,	lowercase__      :     Optional[Any]=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:
                a_    :       Tuple    	=     input_ids.size()
           elif inputs_embeds is not None:
                a_    :       List[str]    	=     inputs_embeds.size()[:-1]
           else:
                raise ValueError("""You have to specify either input_ids or inputs_embeds"""					)

           a_    :       Dict    	=     input_ids.device if input_ids is not None else inputs_embeds.device

           if attention_mask is None:
                a_    :       List[Any]    	=     torch.ones(lowercase__	,	device=lowercase__					)
           if token_type_ids is None:
                a_    :       str    	=     torch.zeros(lowercase__	,	dtype=torch.long	,	device=lowercase__					)

           a_    :       int    	=     attention_mask.unsqueeze(1					).unsqueeze(2					)
           a_    :       List[str]    	=     extended_attention_mask.to(dtype=self.dtype					)  # fp16 compatibility
           a_    :       Dict    	=     (1.0 - extended_attention_mask) * -10000.0
           a_    :       List[str]    	=     self.get_head_mask(lowercase__	,	self.config.num_hidden_layers					)

           a_    :       int    	=     self.embeddings(
               lowercase__	,	position_ids=lowercase__	,	token_type_ids=lowercase__	,	inputs_embeds=lowercase__					)
           a_    :       int    	=     embedding_output

           if self.training:
                a_    :       List[Any]    	=     []
                for i in range(self.config.num_hidden_layers					):
                     a_    :       Any    	=     self.encoder.adaptive_forward(
                         lowercase__	,	current_layer=lowercase__	,	attention_mask=lowercase__	,	head_mask=lowercase__	,	)

                     a_    :       Dict    	=     self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]					)					)
                     a_    :       Union[str, Any]    	=     output_layers[i](output_dropout(lowercase__					)					)
                     res.append(lowercase__					)
           elif self.patience == 0:  # Use all layers for inference
                a_    :       int    	=     self.encoder(lowercase__	,	lowercase__	,	head_mask=lowercase__					)
                a_    :       str    	=     self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]					)					)
                a_    :       Optional[int]    	=     [output_layers[self.config.num_hidden_layers - 1](lowercase__					)]
           else:
                a_    :       Dict    	=     0
                a_    :       List[str]    	=     None
                a_    :       Dict    	=     0
                for i in range(self.config.num_hidden_layers					):
                     calculated_layer_num += 1
                     a_    :       str    	=     self.encoder.adaptive_forward(
                         lowercase__	,	current_layer=lowercase__	,	attention_mask=lowercase__	,	head_mask=lowercase__	,	)

                     a_    :       int    	=     self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]					)					)
                     a_    :       Optional[int]    	=     output_layers[i](lowercase__					)
                     if regression:
                          a_    :       Optional[Any]    	=     logits.detach()
                          if patient_result is not None:
                               a_    :       Dict    	=     patient_result.detach()
                          if (patient_result is not None) and torch.abs(patient_result - labels					) < self.regression_threshold:
                               patient_counter += 1
                          else:
                               a_    :       List[Any]    	=     0
                     else:
                          a_    :       List[str]    	=     logits.detach().argmax(dim=1					)
                          if patient_result is not None:
                               a_    :       Optional[Any]    	=     patient_result.detach().argmax(dim=1					)
                          if (patient_result is not None) and torch.all(labels.eq(lowercase__					)					):
                               patient_counter += 1
                          else:
                               a_    :       Union[str, Any]    	=     0

                     a_    :       Union[str, Any]    	=     logits
                     if patient_counter == self.patience:
                          break
                a_    :       Optional[Any]    	=     [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. '''       ,			snake_case_       ,			)
class     SCREAMING_SNAKE_CASE						(	snake_case_							):






      def __init__(   self      :     str	,	lowercase__      :     int					):


           '''simple docstring'''

           super().__init__(lowercase__					)
           a_    :       Any    	=     config.num_labels

           a_    :       Optional[int]    	=     AlbertModelWithPabee(lowercase__					)
           a_    :       Union[str, Any]    	=     nn.Dropout(config.classifier_dropout_prob					)
           a_    :       Optional[int]    	=     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(lowercase__					)
      def        lowercase_	(   self      :     Any	,	lowercase__      :     Dict=None	,	lowercase__      :     Any=None	,	lowercase__      :     Tuple=None	,	lowercase__      :     Union[str, Any]=None	,	lowercase__      :     Union[str, Any]=None	,	lowercase__      :     str=None	,	lowercase__      :     int=None	,	):


           '''simple docstring'''

           a_    :       Union[str, Any]    	=     self.albert(
               input_ids=lowercase__	,	attention_mask=lowercase__	,	token_type_ids=lowercase__	,	position_ids=lowercase__	,	head_mask=lowercase__	,	inputs_embeds=lowercase__	,	output_dropout=self.dropout	,	output_layers=self.classifiers	,	regression=self.num_labels == 1	,	)

           a_    :       Dict    	=     (logits[-1],)

           if labels is not None:
                a_    :       Tuple    	=     None
                a_    :       int    	=     0
                for ix, logits_item in enumerate(lowercase__					):
                     if self.num_labels == 1:
                          #  We are doing regression
                          a_    :       Dict    	=     MSELoss()
                          a_    :       Union[str, Any]    	=     loss_fct(logits_item.view(-1					)	,	labels.view(-1					)					)
                     else:
                          a_    :       str    	=     CrossEntropyLoss()
                          a_    :       Union[str, Any]    	=     loss_fct(logits_item.view(-1	,	self.num_labels					)	,	labels.view(-1					)					)
                     if total_loss is None:
                          a_    :       Any    	=     loss
                     else:
                          total_loss += loss * (ix + 1)
                     total_weights += ix + 1
                a_    :       List[Any]    	=     (total_loss / total_weights,) + outputs

           return outputs






'''simple docstring'''


import inspect
import unittest

from transformers import EfficientNetConfig
from transformers.testing_utils import is_pipeline_test, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import EfficientNetForImageClassification, EfficientNetModel
 from transformers.models.efficientnet.modeling_efficientnet import EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor







class     SCREAMING_SNAKE_CASE						:






      def __init__(   self      :     Dict	,	lowercase__      :     Any	,	lowercase__      :     Tuple=13	,	lowercase__      :     Any=32	,	lowercase__      :     List[str]=3	,	lowercase__      :     Any=[3, 3, 5]	,	lowercase__      :     int=[32, 16, 24]	,	lowercase__      :     str=[16, 24, 40]	,	lowercase__      :     Optional[Any]=[1, 1, 2]	,	lowercase__      :     int=[1, 1, 2]	,	lowercase__      :     Tuple=[1, 6, 6]	,	lowercase__      :     Optional[Any]=True	,	lowercase__      :     Union[str, Any]=True	,	lowercase__      :     List[str]=37	,	lowercase__      :     Optional[int]="gelu"	,	lowercase__      :     str=10	,	):


           '''simple docstring'''

           a_    :       Dict    	=     parent
           a_    :       Any    	=     batch_size
           a_    :       str    	=     image_size
           a_    :       Tuple    	=     num_channels
           a_    :       Dict    	=     kernel_sizes
           a_    :       List[Any]    	=     in_channels
           a_    :       int    	=     out_channels
           a_    :       Union[str, Any]    	=     strides
           a_    :       List[Any]    	=     num_block_repeats
           a_    :       List[Any]    	=     expand_ratios
           a_    :       Dict    	=     is_training
           a_    :       Optional[int]    	=     hidden_act
           a_    :       List[Any]    	=     num_labels
           a_    :       List[str]    	=     use_labels






      def        lowercase_	(   self      :     Optional[int]					):


           '''simple docstring'''

           a_    :       List[Any]    	=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

           a_    :       int    	=     None
           if self.use_labels:
                a_    :       int    	=     ids_tensor([self.batch_size]	,	self.num_labels					)

           a_    :       List[Any]    	=     self.get_config()
           return config, pixel_values, labels






      def        lowercase_	(   self      :     Dict					):


           '''simple docstring'''

           return EfficientNetConfig(
               num_channels=self.num_channels	,	kernel_sizes=self.kernel_sizes	,	in_channels=self.in_channels	,	out_channels=self.out_channels	,	strides=self.strides	,	num_block_repeats=self.num_block_repeats	,	expand_ratios=self.expand_ratios	,	hidden_act=self.hidden_act	,	num_labels=self.num_labels	,	)






      def        lowercase_	(   self      :     int	,	lowercase__      :     Any	,	lowercase__      :     Any	,	lowercase__      :     Optional[int]					):


           '''simple docstring'''

           a_    :       Tuple    	=     EfficientNetModel(config=lowercase__					)
           model.to(lowercase__					)
           model.eval()
           a_    :       Optional[int]    	=     model(lowercase__					)
           # expected last hidden states: B, C, H // 4, W // 4
           self.parent.assertEqual(
               result.last_hidden_state.shape	,	(self.batch_size, config.hidden_dim, self.image_size // 4, self.image_size // 4)	,	)






      def        lowercase_	(   self      :     Optional[Any]	,	lowercase__      :     Union[str, Any]	,	lowercase__      :     Any	,	lowercase__      :     str					):


           '''simple docstring'''

           a_    :       Dict    	=     EfficientNetForImageClassification(lowercase__					)
           model.to(lowercase__					)
           model.eval()
           a_    :       str    	=     model(lowercase__	,	labels=lowercase__					)
           self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.num_labels)					)






      def        lowercase_	(   self      :     int					):


           '''simple docstring'''

           a_    :       Union[str, Any]    	=     self.prepare_config_and_inputs()
           a_	,    a_	,    a_    :       Optional[Any]    	=     config_and_inputs
           a_    :       str    	=     {"""pixel_values""": pixel_values}
           return config, inputs_dict







@require_torch
class     SCREAMING_SNAKE_CASE						(	snake_case_       ,			snake_case_       ,			unittest.TestCase							):
      __magic_name__     :					Any             =				(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
      __magic_name__     :					List[Any]             =				(
          {'''feature-extraction''': EfficientNetModel, '''image-classification''': EfficientNetForImageClassification}
          if is_torch_available()
          else {}
      )

      __magic_name__     :					int             =				False
      __magic_name__     :					Dict             =				False
      __magic_name__     :					Dict             =				False
      __magic_name__     :					Union[str, Any]             =				False
      __magic_name__     :					Optional[int]             =				False






      def        lowercase_	(   self      :     List[Any]					):


           '''simple docstring'''

           a_    :       Optional[int]    	=     EfficientNetModelTester(self					)
           a_    :       Union[str, Any]    	=     ConfigTester(
               self	,	config_class=lowercase__	,	has_text_modality=lowercase__	,	hidden_size=37					)






      def        lowercase_	(   self      :     List[str]					):


           '''simple docstring'''

           self.create_and_test_config_common_properties()
           self.config_tester.create_and_test_config_to_json_string()
           self.config_tester.create_and_test_config_to_json_file()
           self.config_tester.create_and_test_config_from_and_save_pretrained()
           self.config_tester.create_and_test_config_with_num_labels()
           self.config_tester.check_config_can_be_init_without_params()
           self.config_tester.check_config_arguments_init()






      def        lowercase_	(   self      :     Tuple					):


           '''simple docstring'''

           return






      @unittest.skip(reason="""EfficientNet does not use inputs_embeds"""					)
      def        lowercase_	(   self      :     Union[str, Any]					):


           '''simple docstring'''

           pass






      @unittest.skip(reason="""EfficientNet does not support input and output embeddings"""					)
      def        lowercase_	(   self      :     Optional[Any]					):


           '''simple docstring'''

           pass






      @unittest.skip(reason="""EfficientNet does not use feedforward chunking"""					)
      def        lowercase_	(   self      :     str					):


           '''simple docstring'''

           pass






      def        lowercase_	(   self      :     Union[str, Any]					):


           '''simple docstring'''

           a_	,    a_    :       Dict    	=     self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                a_    :       Tuple    	=     model_class(lowercase__					)
                a_    :       int    	=     inspect.signature(model.forward					)
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                a_    :       Any    	=     [*signature.parameters.keys()]

                a_    :       Dict    	=     ["""pixel_values"""]
                self.assertListEqual(arg_names[:1]	,	lowercase__					)






      def        lowercase_	(   self      :     Optional[int]					):


           '''simple docstring'''

           a_    :       Tuple    	=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase__					)






      def        lowercase_	(   self      :     Tuple					):


           '''simple docstring'''

           def check_hidden_states_output(lowercase__      :     Optional[int]	,	lowercase__      :     List[Any]	,	lowercase__      :     Tuple					):
                a_    :       str    	=     model_class(lowercase__					)
                model.to(lowercase__					)
                model.eval()

                with torch.no_grad():
                     a_    :       Tuple    	=     model(**self._prepare_for_class(lowercase__	,	lowercase__					)					)

                a_    :       Tuple    	=     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
                a_    :       Tuple    	=     sum(config.num_block_repeats					) * 4
                self.assertEqual(len(lowercase__					)	,	lowercase__					)

                # EfficientNet's feature maps are of shape (batch_size, num_channels, height, width)
                self.assertListEqual(
                    list(hidden_states[0].shape[-2:]					)	,	[self.model_tester.image_size // 2, self.model_tester.image_size // 2]	,	)

           a_	,    a_    :       Any    	=     self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                a_    :       Any    	=     True
                check_hidden_states_output(lowercase__	,	lowercase__	,	lowercase__					)

                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                a_    :       Any    	=     True

                check_hidden_states_output(lowercase__	,	lowercase__	,	lowercase__					)






      def        lowercase_	(   self      :     Union[str, Any]					):


           '''simple docstring'''

           a_    :       Optional[int]    	=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*lowercase__					)






      @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""					)
      def        lowercase_	(   self      :     List[Any]					):


           '''simple docstring'''

           pass






      @slow
      def        lowercase_	(   self      :     Dict					):


           '''simple docstring'''

           for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                a_    :       List[str]    	=     EfficientNetModel.from_pretrained(lowercase__					)
                self.assertIsNotNone(lowercase__					)






      @is_pipeline_test
      @require_vision
      @slow
      def        lowercase_	(   self      :     Tuple					):


           '''simple docstring'''

           super().test_pipeline_image_classification()



def    _SCREAMING_SNAKE_CASE    (			):




     """simple docstring"""


     a_    :       List[str]    	=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""							)
     return image







@require_torch
@require_vision
class     SCREAMING_SNAKE_CASE						(	unittest.TestCase							):






      @cached_property
      def        lowercase_	(   self      :     Dict					):


           '''simple docstring'''

           return AutoImageProcessor.from_pretrained("""google/efficientnet-b7"""					) if is_vision_available() else None






      @slow
      def        lowercase_	(   self      :     Any					):


           '''simple docstring'''

           a_    :       Optional[Any]    	=     EfficientNetForImageClassification.from_pretrained("""google/efficientnet-b7"""					).to(lowercase__					)

           a_    :       Any    	=     self.default_image_processor
           a_    :       List[str]    	=     prepare_img()
           a_    :       str    	=     image_processor(images=lowercase__	,	return_tensors="""pt"""					).to(lowercase__					)

           # forward pass
           with torch.no_grad():
                a_    :       Tuple    	=     model(**lowercase__					)

           # verify the logits
           a_    :       Dict    	=     torch.Size((1, 1000)					)
           self.assertEqual(outputs.logits.shape	,	lowercase__					)

           a_    :       List[Any]    	=     torch.tensor([0.0001, 0.0002, 0.0002]					).to(lowercase__					)
           self.assertTrue(torch.allclose(outputs.logits[0, :3]	,	lowercase__	,	atol=1e-4					)					)
