


from __future__ import annotations

import unittest

from transformers import AlbertConfig, 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
  from transformers.models.albert.modeling_tf_albert import (
      TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFAlbertForMaskedLM,
      TFAlbertForMultipleChoice,
      TFAlbertForPreTraining,
      TFAlbertForQuestionAnswering,
      TFAlbertForSequenceClassification,
      TFAlbertForTokenClassification,
      TFAlbertModel,
  )

class   lowerCamelCase						:

       def __init__(      self						,    a__						,    a__=13						,    a__=7						,    a__=True						,    a__=True						,    a__=True						,    a__=True						,    a__=99						,    a__=16						,    a__=32						,    a__=2						,    a__=4						,    a__=37						,    a__="gelu"						,    a__=0.1						,    a__=0.1						,    a__=5_12						,    a__=16						,    a__=2						,    a__=0.02						,    a__=3						,    a__=4						,    a__=None						,    ):






        '''simple docstring'''






        snake_case :			Union[str, Any]							=   parent
        snake_case :			str							=   13
        snake_case :			Any							=   7
        snake_case :			int							=   True
        snake_case :			Union[str, Any]							=   True
        snake_case :			Any							=   True
        snake_case :			Optional[Any]							=   True
        snake_case :			Optional[int]							=   99
        snake_case :			Optional[int]							=   16
        snake_case :			Dict							=   32
        snake_case :			Optional[Any]							=   2
        snake_case :			Optional[int]							=   4
        snake_case :			Union[str, Any]							=   37
        snake_case :			Tuple							=   '''gelu'''
        snake_case :			Any							=   0.1
        snake_case :			Optional[Any]							=   0.1
        snake_case :			List[Any]							=   5_12
        snake_case :			Union[str, Any]							=   16
        snake_case :			List[str]							=   2
        snake_case :			List[Any]							=   0.02
        snake_case :			List[Any]							=   3
        snake_case :			int							=   4
        snake_case :			List[Any]							=   None

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Union[str, Any]							=   ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size       )

        snake_case :			Tuple							=   None
        if self.use_input_mask:
         snake_case :			Union[str, Any]							=   random_attention_mask([self.batch_size, self.seq_length]       )

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

        snake_case :			str							=   None
        snake_case :			str							=   None
        snake_case :			Tuple							=   None
        if self.use_labels:
         snake_case :			List[str]							=   ids_tensor([self.batch_size]						,    self.type_sequence_label_size       )
         snake_case :			Optional[Any]							=   ids_tensor([self.batch_size, self.seq_length]						,    self.num_labels       )
         snake_case :			List[Any]							=   ids_tensor([self.batch_size]						,    self.num_choices       )

        snake_case :			Optional[int]							=   AlbertConfig(
            vocab_size=self.vocab_size						,    hidden_size=self.hidden_size						,    embedding_size=self.embedding_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						,    )

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

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        snake_case :			List[str]							=   TFAlbertModel(config=a__       )
        # inputs = {'input_ids': input_ids,
        #           'attention_mask': input_mask,
        #           'token_type_ids': token_type_ids}
        # sequence_output, pooled_output = model(**inputs)
        snake_case :			Union[str, Any]							=   {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        snake_case :			Dict							=   model(a__       )

        snake_case :			List[str]							=   [input_ids, input_mask]
        snake_case :			Dict							=   model(a__       )

        snake_case :			Tuple							=   model(a__       )

        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 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        snake_case :			int							=   self.num_labels
        snake_case :			Union[str, Any]							=   TFAlbertForPreTraining(config=a__       )
        snake_case :			Union[str, Any]							=   {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        snake_case :			Dict							=   model(a__       )
        self.parent.assertEqual(result.prediction_logits.shape						,    (self.batch_size, self.seq_length, self.vocab_size)       )
        self.parent.assertEqual(result.sop_logits.shape						,    (self.batch_size, self.num_labels)       )

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   TFAlbertForMaskedLM(config=a__       )
        snake_case :			str							=   {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        snake_case :			Optional[int]							=   model(a__       )
        self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.seq_length, self.vocab_size)       )

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        snake_case :			Optional[Any]							=   self.num_labels
        snake_case :			Optional[Any]							=   TFAlbertForSequenceClassification(config=a__       )
        snake_case :			Union[str, Any]							=   {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        snake_case :			Dict							=   model(a__       )
        self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_labels)       )

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        snake_case :			List[str]							=   TFAlbertForQuestionAnswering(config=a__       )
        snake_case :			Optional[Any]							=   {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        snake_case :			Union[str, Any]							=   model(a__       )
        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 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        snake_case :			Dict							=   self.num_choices
        snake_case :			List[Any]							=   TFAlbertForMultipleChoice(config=a__       )
        snake_case :			List[str]							=   tf.tile(tf.expand_dims(a__						,    1       )						,    (1, self.num_choices, 1)       )
        snake_case :			Optional[int]							=   tf.tile(tf.expand_dims(a__						,    1       )						,    (1, self.num_choices, 1)       )
        snake_case :			Union[str, Any]							=   tf.tile(tf.expand_dims(a__						,    1       )						,    (1, self.num_choices, 1)       )
        snake_case :			str							=   {
            '''input_ids''': multiple_choice_inputs_ids,
            '''attention_mask''': multiple_choice_input_mask,
            '''token_type_ids''': multiple_choice_token_type_ids,
        }
        snake_case :			Tuple							=   model(a__       )
        self.parent.assertListEqual(list(result['''logits'''].shape       )						,    [self.batch_size, self.num_choices]       )

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        snake_case :			int							=   self.num_labels
        snake_case :			str							=   TFAlbertForTokenClassification(config=a__       )
        snake_case :			List[Any]							=   {
            '''input_ids''': input_ids,
            '''attention_mask''': input_mask,
            '''token_type_ids''': token_type_ids,
        }
        snake_case :			List[str]							=   model(a__       )
        self.parent.assertListEqual(list(result['''logits'''].shape       )						,    [self.batch_size, self.seq_length, self.num_labels]       )



       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   self.prepare_config_and_inputs()
        (
            (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       
        ) :			Tuple							=   config_and_inputs
        snake_case :			Tuple							=   {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
        return config, inputs_dict







@require_tf
class   lowerCamelCase						(	SCREAMING_SNAKE_CASE,      SCREAMING_SNAKE_CASE,      unittest.TestCase  ):
       _UpperCamelCase     		=   (
           (
               TFAlbertModel,
               TFAlbertForPreTraining,
               TFAlbertForMaskedLM,
               TFAlbertForSequenceClassification,
               TFAlbertForQuestionAnswering,
               TFAlbertForTokenClassification,
               TFAlbertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       _UpperCamelCase     		=   (
           {
               """feature-extraction""": TFAlbertModel,
               """fill-mask""": TFAlbertForMaskedLM,
               """question-answering""": TFAlbertForQuestionAnswering,
               """text-classification""": TFAlbertForSequenceClassification,
               """token-classification""": TFAlbertForTokenClassification,
               """zero-shot""": TFAlbertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       _UpperCamelCase     		=   False
       _UpperCamelCase     		=   False

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__=False       ):






        '''simple docstring'''






        snake_case :			Tuple							=   super()._prepare_for_class(a__						,    a__						,    return_labels=a__       )

        if return_labels:
         if model_class in get_values(a__       ):
          snake_case :			Union[str, Any]							=   tf.zeros(self.model_tester.batch_size						,    dtype=tf.intaa       )

        return inputs_dict

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			int							=   TFAlbertModelTester(self       )
        snake_case :			Any							=   ConfigTester(self						,    config_class=a__						,    hidden_size=37       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        self.config_tester.run_common_tests()

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Any							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_model(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Any							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_pretraining(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Union[str, Any]							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_masked_lm(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Union[str, Any]							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_multiple_choice(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			str							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_sequence_classification(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_question_answering(*a__       )



       @slow
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case :			int							=   TFAlbertModel.from_pretrained(a__       )
         self.assertIsNotNone(a__       )







@require_tf
class   lowerCamelCase						(	unittest.TestCase  ):

       @slow
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			str							=   TFAlbertForPreTraining.from_pretrained('''albert-base-v2'''       )
        snake_case :			Tuple							=   tf.constant([[0, 1, 2, 3, 4, 5]]       )
        snake_case :			List[Any]							=   model(a__       )[0]

        snake_case :			int							=   [1, 6, 3_00_00]
        self.assertEqual(output.shape						,    a__       )

        snake_case :			Any							=   tf.constant(
            [
                [
                    [4.59_5668, 0.7446_2754, -1.81_8147],
                    [4.595_4347, 0.745_4184, -1.818_8258],
                    [4.595_4905, 0.744_8235, -1.818_2316],
                ]
            ]       )
        tf.debugging.assert_near(output[:, :3, :3]						,    a__						,    atol=1e-4       )










import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import TvltImageProcessor






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase=10			,  __lowerCamelCase=10			,  __lowerCamelCase=False			,  __lowerCamelCase=False							)		->Tuple:


 '''simple docstring'''
 snake_case :			Dict							=   []
 for i in range(image_processor_tester.num_frames							):
  video.append(np.random.randint(255			,  size=(image_processor_tester.num_channels, width, height)			,  dtype=np.uinta							)							)

 if not numpify and not torchify:
  # PIL expects the channel dimension as last dimension
  snake_case :			Optional[int]							=   [Image.fromarray(np.moveaxis(__lowerCamelCase			,  0			,  -1							)							) for frame in video]

 if torchify:
  snake_case :			int							=   [torch.from_numpy(__lowerCamelCase							) for frame in video]

 return video






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase=False			,  __lowerCamelCase=False			,  __lowerCamelCase=False							)		->Optional[Any]:


 '''simple docstring'''
 assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

 snake_case :			Any							=   []
 for i in range(image_processor_tester.batch_size							):
  if equal_resolution:
   snake_case :			Optional[int]							=   image_processor_tester.max_resolution
  else:
   snake_case	,       snake_case :			str							=   np.random.choice(
       np.arange(image_processor_tester.min_resolution			,  image_processor_tester.max_resolution							)			,  2							)
   snake_case :			Any							=   prepare_video(
       image_processor_tester=__lowerCamelCase			,  width=__lowerCamelCase			,  height=__lowerCamelCase			,  numpify=__lowerCamelCase			,  torchify=__lowerCamelCase			,  )
  video_inputs.append(__lowerCamelCase							)

 return video_inputs

class   lowerCamelCase						(	unittest.TestCase  ):

       def __init__(      self						,    a__						,    a__=7						,    a__=3						,    a__=4						,    a__=18						,    a__=30						,    a__=4_00						,    a__=True						,    a__=None						,    a__=True						,    a__=[0.5, 0.5, 0.5]						,    a__=[0.5, 0.5, 0.5]						,    a__=True						,    a__=None						,    ):






        '''simple docstring'''






        snake_case :			Tuple							=   size if size is not None else {'''shortest_edge''': 18}
        snake_case :			List[str]							=   crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

        snake_case :			Optional[Any]							=   parent
        snake_case :			List[Any]							=   batch_size
        snake_case :			Optional[int]							=   num_channels
        snake_case :			Union[str, Any]							=   num_frames
        snake_case :			List[Any]							=   image_size
        snake_case :			Tuple							=   min_resolution
        snake_case :			List[Any]							=   max_resolution
        snake_case :			Tuple							=   do_resize
        snake_case :			List[Any]							=   size
        snake_case :			List[Any]							=   do_normalize
        snake_case :			List[Any]							=   image_mean
        snake_case :			List[str]							=   image_std
        snake_case :			Tuple							=   do_center_crop
        snake_case :			str							=   crop_size



       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
            "do_center_crop": self.do_center_crop,
            "crop_size": self.crop_size,
        }







@require_torch
@require_vision
class   lowerCamelCase						(	SCREAMING_SNAKE_CASE,      unittest.TestCase  ):
       _UpperCamelCase     		=   TvltImageProcessor if is_vision_available() else None

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   TvltImageProcessorTester(self       )

       @property
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        return self.image_processor_tester.prepare_image_processor_dict()

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   self.image_processing_class(**self.image_processor_dict       )
        self.assertTrue(hasattr(a__						,    '''image_mean'''       )       )
        self.assertTrue(hasattr(a__						,    '''image_std'''       )       )
        self.assertTrue(hasattr(a__						,    '''do_normalize'''       )       )
        self.assertTrue(hasattr(a__						,    '''do_resize'''       )       )
        self.assertTrue(hasattr(a__						,    '''do_center_crop'''       )       )
        self.assertTrue(hasattr(a__						,    '''size'''       )       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   self.image_processing_class(**self.image_processor_dict       )
        # create random PIL videos
        snake_case :			List[str]							=   prepare_video_inputs(self.image_processor_tester						,    equal_resolution=a__       )
        for video in video_inputs:
         self.assertIsInstance(a__						,    a__       )
         self.assertIsInstance(video[0]						,    Image.Image       )

        # Test not batched input
        snake_case :			int							=   image_processor(video_inputs[0]						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )

        # Test batched
        snake_case :			Optional[Any]							=   image_processor(a__						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   self.image_processing_class(**self.image_processor_dict       )
        # create random numpy tensors
        snake_case :			Union[str, Any]							=   prepare_video_inputs(self.image_processor_tester						,    equal_resolution=a__						,    numpify=a__       )
        for video in video_inputs:
         self.assertIsInstance(a__						,    a__       )
         self.assertIsInstance(video[0]						,    np.ndarray       )

        # Test not batched input
        snake_case :			List[str]							=   image_processor(video_inputs[0]						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )

        # Test batched
        snake_case :			Tuple							=   image_processor(a__						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )



       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			str							=   self.image_processing_class(**self.image_processor_dict       )
        # create random PyTorch tensors
        snake_case :			str							=   prepare_video_inputs(self.image_processor_tester						,    equal_resolution=a__						,    torchify=a__       )
        for video in video_inputs:
         self.assertIsInstance(a__						,    a__       )
         self.assertIsInstance(video[0]						,    torch.Tensor       )

        # Test not batched input
        snake_case :			Tuple							=   image_processor(video_inputs[0]						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )

        # Test batched
        snake_case :			Optional[int]							=   image_processor(a__						,    return_tensors='''pt'''       ).pixel_values
        self.assertEqual(
            encoded_videos.shape						,    (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )						,    )






