



'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import (
     TF_MODEL_FOR_PRETRAINING_MAPPING,
     TFMobileBertForMaskedLM,
     TFMobileBertForMultipleChoice,
     TFMobileBertForNextSentencePrediction,
     TFMobileBertForPreTraining,
     TFMobileBertForQuestionAnswering,
     TFMobileBertForSequenceClassification,
     TFMobileBertForTokenClassification,
     TFMobileBertModel,
 )


@require_tf
class 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:Optional[int]     =       (
           (
               TFMobileBertModel,
               TFMobileBertForMaskedLM,
               TFMobileBertForNextSentencePrediction,
               TFMobileBertForPreTraining,
               TFMobileBertForQuestionAnswering,
               TFMobileBertForSequenceClassification,
               TFMobileBertForTokenClassification,
               TFMobileBertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       lowerCAmelCase__	:Optional[int]     =       (
           {
               "feature-extraction": TFMobileBertModel,
               "fill-mask": TFMobileBertForMaskedLM,
               "question-answering": TFMobileBertForQuestionAnswering,
               "text-classification": TFMobileBertForSequenceClassification,
               "token-classification": TFMobileBertForTokenClassification,
               "zero-shot": TFMobileBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       lowerCAmelCase__	:Any     =       False
       lowerCAmelCase__	:str     =       False



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_=False   )	->				List[str]:
           lowercase__									=    super()._prepare_for_class(UpperCAmelCase_  ,UpperCAmelCase_  ,return_labels=UpperCAmelCase_   )

           if return_labels:
               if model_class in get_values(UpperCAmelCase_   ):
                   lowercase__									=    tf.zeros(self.model_tester.batch_size  ,dtype=tf.intaa   )

           return inputs_dict


       class 					snake_case			(UpperCamelCase ):



              def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=13  ,UpperCAmelCase_=7  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=99  ,UpperCAmelCase_=32  ,UpperCAmelCase_=32  ,UpperCAmelCase_=2  ,UpperCAmelCase_=4  ,UpperCAmelCase_=37  ,UpperCAmelCase_="gelu"  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=512  ,UpperCAmelCase_=16  ,UpperCAmelCase_=2  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=3  ,UpperCAmelCase_=4  ,UpperCAmelCase_=None  ,)	->				str:
                  lowercase__									=    parent
                  lowercase__									=    batch_size
                  lowercase__									=    seq_length
                  lowercase__									=    is_training
                  lowercase__									=    use_input_mask
                  lowercase__									=    use_token_type_ids
                  lowercase__									=    use_labels
                  lowercase__									=    vocab_size
                  lowercase__									=    hidden_size
                  lowercase__									=    num_hidden_layers
                  lowercase__									=    num_attention_heads
                  lowercase__									=    intermediate_size
                  lowercase__									=    hidden_act
                  lowercase__									=    hidden_dropout_prob
                  lowercase__									=    attention_probs_dropout_prob
                  lowercase__									=    max_position_embeddings
                  lowercase__									=    type_vocab_size
                  lowercase__									=    type_sequence_label_size
                  lowercase__									=    initializer_range
                  lowercase__									=    num_labels
                  lowercase__									=    num_choices
                  lowercase__									=    scope
                  lowercase__									=    embedding_size



              def 				_a   (					self   )	->				Any:
                  lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size   )

                  lowercase__									=    None
                  if self.use_input_mask:
                      lowercase__									=    random_attention_mask([self.batch_size, self.seq_length]   )

                  lowercase__									=    None
                  if self.use_token_type_ids:
                      lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.type_vocab_size   )

                  lowercase__									=    None
                  lowercase__									=    None
                  lowercase__									=    None
                  if self.use_labels:
                      lowercase__									=    ids_tensor([self.batch_size]  ,self.type_sequence_label_size   )
                      lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.num_labels   )
                      lowercase__									=    ids_tensor([self.batch_size]  ,self.num_choices   )

                  lowercase__									=    MobileBertConfig(
                      vocab_size=self.vocab_size  ,hidden_size=self.hidden_size  ,num_hidden_layers=self.num_hidden_layers  ,num_attention_heads=self.num_attention_heads  ,intermediate_size=self.intermediate_size  ,hidden_act=self.hidden_act  ,hidden_dropout_prob=self.hidden_dropout_prob  ,attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,max_position_embeddings=self.max_position_embeddings  ,type_vocab_size=self.type_vocab_size  ,initializer_range=self.initializer_range  ,embedding_size=self.embedding_size  ,)

                  return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



              def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Tuple:
                  lowercase__									=    TFMobileBertModel(config=UpperCAmelCase_   )
                  lowercase__									=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                  lowercase__									=    model(UpperCAmelCase_   )

                  lowercase__									=    [input_ids, input_mask]
                  lowercase__									=    model(UpperCAmelCase_   )

                  lowercase__									=    model(UpperCAmelCase_   )

                  self.parent.assertEqual(
                      result.last_hidden_state.shape  ,(self.batch_size, self.seq_length, self.hidden_size)   )
                  self.parent.assertEqual(result.pooler_output.shape  ,(self.batch_size, self.hidden_size)   )



              def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				List[Any]:
                  lowercase__									=    TFMobileBertForMaskedLM(config=UpperCAmelCase_   )
                  lowercase__									=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                  lowercase__									=    model(UpperCAmelCase_   )
                  self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.vocab_size)   )



              def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				str:
                  lowercase__									=    TFMobileBertForNextSentencePrediction(config=UpperCAmelCase_   )
                  lowercase__									=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                  lowercase__									=    model(UpperCAmelCase_   )
                  self.parent.assertEqual(result.logits.shape  ,(self.batch_size, 2)   )



              def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Union[str, Any]:
                  lowercase__									=    TFMobileBertForPreTraining(config=UpperCAmelCase_   )
                  lowercase__									=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                  lowercase__									=    model(UpperCAmelCase_   )
                  self.parent.assertEqual(
                      result.prediction_logits.shape  ,(self.batch_size, self.seq_length, self.vocab_size)   )
                  self.parent.assertEqual(result.seq_relationship_logits.shape  ,(self.batch_size, 2)   )



              def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Union[str, Any]:
                  lowercase__									=    self.num_labels
                  lowercase__									=    TFMobileBertForSequenceClassification(config=UpperCAmelCase_   )
                  lowercase__									=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                  lowercase__									=    model(UpperCAmelCase_   )
                  self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)   )



              def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				int:
                  lowercase__									=    self.num_choices
                  lowercase__									=    TFMobileBertForMultipleChoice(config=UpperCAmelCase_   )
                  lowercase__									=    tf.tile(tf.expand_dims(UpperCAmelCase_  ,1   )  ,(1, self.num_choices, 1)   )
                  lowercase__									=    tf.tile(tf.expand_dims(UpperCAmelCase_  ,1   )  ,(1, self.num_choices, 1)   )
                  lowercase__									=    tf.tile(tf.expand_dims(UpperCAmelCase_  ,1   )  ,(1, self.num_choices, 1)   )
                  lowercase__									=    {
                      "input_ids": multiple_choice_inputs_ids,
                      "attention_mask": multiple_choice_input_mask,
                      "token_type_ids": multiple_choice_token_type_ids,
                  }
                  lowercase__									=    model(UpperCAmelCase_   )
                  self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_choices)   )



              def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Union[str, Any]:
                  lowercase__									=    self.num_labels
                  lowercase__									=    TFMobileBertForTokenClassification(config=UpperCAmelCase_   )
                  lowercase__									=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                  lowercase__									=    model(UpperCAmelCase_   )
                  self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.num_labels)   )



              def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Optional[int]:
                  lowercase__									=    TFMobileBertForQuestionAnswering(config=UpperCAmelCase_   )
                  lowercase__									=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
                  lowercase__									=    model(UpperCAmelCase_   )
                  self.parent.assertEqual(result.start_logits.shape  ,(self.batch_size, self.seq_length)   )
                  self.parent.assertEqual(result.end_logits.shape  ,(self.batch_size, self.seq_length)   )







              def 				_a   (					self   )	->				List[str]:
                  lowercase__									=    self.prepare_config_and_inputs()
                  (
                      (
                      lowercase__
                  )							,							(
                      lowercase__
                  )							,							(
                      lowercase__
                  )							,							(
                      lowercase__
                  )							,							(
                      lowercase__
                  )							,							(
                      lowercase__
                  )							,							(
                      lowercase__
                  )							,							
                  )									=    config_and_inputs
                  lowercase__									=    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
                  return config, inputs_dict





       def 				_a   (					self   )	->				int:
           lowercase__									=    TFMobileBertModelTest.TFMobileBertModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,hidden_size=37   )



       def 				_a   (					self   )	->				str:
           self.config_tester.run_common_tests()



       def 				_a   (					self   )	->				int:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[str]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[str]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[str]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Tuple:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCAmelCase_   )







       @slow
       def 				_a   (					self   )	->				str:
           # for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           for model_name in ["google/mobilebert-uncased"]:
               lowercase__									=    TFMobileBertModel.from_pretrained(UpperCAmelCase_   )
               self.assertIsNotNone(UpperCAmelCase_   )





@require_tf
class 					snake_case			(unittest.TestCase ):



       @slow
       def 				_a   (					self   )	->				Any:
           lowercase__									=    TFMobileBertForPreTraining.from_pretrained("google/mobilebert-uncased"   )
           lowercase__									=    tf.constant([[0, 1, 2, 3, 4, 5]]   )
           lowercase__									=    model(UpperCAmelCase_   )[0]

           lowercase__									=    [1, 6, 30_522]
           self.assertEqual(output.shape  ,UpperCAmelCase_   )

           lowercase__									=    tf.constant(
               [
                   [
                       [-4.5_91_95_47, -9.24_82_95, -9.64_52_56],
                       [-6.7_30_61_75, -6.44_02_84, -6.6_05_28_37],
                       [-7.2_74_35_06, -6.7_84_79_15, -6.02_46_73],
                   ]
               ]   )
           tf.debugging.assert_near(output[:, :3, :3]  ,UpperCAmelCase_  ,atol=1E-4   )









'''simple docstring'''





import json
import os
from collections import Counter

import torch
import torchvision
import torchvision.transforms as transforms
from PIL import Image
from torch import nn
from torch.utils.data import Dataset


SCREAMING_SNAKE_CASE__          =  {1: (1, 1), 2: (2, 1), 3: (3, 1), 4: (2, 2), 5: (5, 1), 6: (3, 2), 7: (7, 1), 8: (4, 2), 9: (3, 3)}


class 					snake_case			(nn.Module ):



       def __init__(					self  ,UpperCAmelCase_   )	->				Dict:
           super().__init__()
           lowercase__									=    torchvision.models.resnetaaa(pretrained=UpperCAmelCase_   )
           lowercase__									=    list(model.children()   )[:-2]
           lowercase__									=    nn.Sequential(*UpperCAmelCase_   )
           lowercase__									=    nn.AdaptiveAvgPoolad(POOLING_BREAKDOWN[args.num_image_embeds]   )







       def 				_a   (					self  ,UpperCAmelCase_   )	->				str:
           # Bx3x224x224 -> Bx2048x7x7 -> Bx2048xN -> BxNx2048
           lowercase__									=    self.pool(self.model(UpperCAmelCase_   )   )
           lowercase__									=    torch.flatten(UpperCAmelCase_  ,start_dim=2   )
           lowercase__									=    out.transpose(1  ,2   ).contiguous()
           return out  # BxNx2048





class 					snake_case			(UpperCamelCase ):



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Dict:
           lowercase__									=    [json.loads(UpperCAmelCase_   ) for l in open(UpperCAmelCase_   )]
           lowercase__									=    os.path.dirname(UpperCAmelCase_   )
           lowercase__									=    tokenizer
           lowercase__									=    labels
           lowercase__									=    len(UpperCAmelCase_   )
           lowercase__									=    max_seq_length

           lowercase__									=    transforms



       def __len__(					self   )	->				Optional[Any]:
           return len(self.data   )



       def __getitem__(					self  ,UpperCAmelCase_   )	->				Optional[int]:
           lowercase__									=    torch.LongTensor(self.tokenizer.encode(self.data[index]["text"]  ,add_special_tokens=UpperCAmelCase_   )   )
           lowercase__							,							lowercase__							,							lowercase__									=    sentence[0], sentence[1:-1], sentence[-1]
           lowercase__									=    sentence[: self.max_seq_length]

           lowercase__									=    torch.zeros(self.n_classes   )
           lowercase__									=    1

           lowercase__									=    Image.open(os.path.join(self.data_dir  ,self.data[index]["img"]   )   ).convert("RGB"   )
           lowercase__									=    self.transforms(UpperCAmelCase_   )

           return {
               "image_start_token": start_token,
               "image_end_token": end_token,
               "sentence": sentence,
               "image": image,
               "label": label,
           }







       def 				_a   (					self   )	->				str:
           lowercase__									=    Counter()
           for row in self.data:
               label_freqs.update(row["label"]   )
           return label_freqs





def 							lowerCamelCase	(					_snake_case	:    Optional[int]							):





    '''simple docstring'''


    lowercase__									=    [len(row["sentence"]							) for row in batch]
    lowercase__							,							lowercase__									=    len(_snake_case							), max(_snake_case							)

    lowercase__									=    torch.zeros(_snake_case  ,_snake_case  ,dtype=torch.long							)
    lowercase__									=    torch.zeros(_snake_case  ,_snake_case  ,dtype=torch.long							)

    for i_batch, (input_row, length) in enumerate(zip(_snake_case  ,_snake_case							)							):
        lowercase__									=    input_row["sentence"]
        lowercase__									=    1

    lowercase__									=    torch.stack([row["image"] for row in batch]							)
    lowercase__									=    torch.stack([row["label"] for row in batch]							)
    lowercase__									=    torch.stack([row["image_start_token"] for row in batch]							)
    lowercase__									=    torch.stack([row["image_end_token"] for row in batch]							)

    return text_tensor, mask_tensor, img_tensor, img_start_token, img_end_token, tgt_tensor




def 							lowerCamelCase	(					):





    '''simple docstring'''


    return [
        "Crime",
        "Drama",
        "Thriller",
        "Action",
        "Comedy",
        "Romance",
        "Documentary",
        "Short",
        "Mystery",
        "History",
        "Family",
        "Adventure",
        "Fantasy",
        "Sci-Fi",
        "Western",
        "Horror",
        "Sport",
        "War",
        "Music",
        "Musical",
        "Animation",
        "Biography",
        "Film-Noir",
    ]




def 							lowerCamelCase	(					):





    '''simple docstring'''


    return transforms.Compose(
        [
            transforms.Resize(256							),
            transforms.CenterCrop(224							),
            transforms.ToTensor(),
            transforms.Normalize(
                mean=[0.46_777_044, 0.44_531_429, 0.40_661_017]  ,std=[0.12_221_994, 0.12_145_835, 0.14_380_469]  ,),
        ]							)




