

import logging

import numpy as np
import pytest
from scipy.linalg import eigh

logging.basicConfig(level=logging.INFO, format='%(message)s')
def   a   (    A__			:     np.ndarray  )					->   np.ndarray:





      """simple docstring"""



      return input_array.reshape((input_array.size, 1)  )
def   a   (    A__			:     np.ndarray   ,     A__			:     np.ndarray   ,     A__			:     int  )					->   np.ndarray:





      """simple docstring"""



      _lowercase			 =np.nan
      for i in range(A__  ):
            _lowercase			 =features[:, labels == i]
            _lowercase			 =data.mean(1  )
            # Centralize the data of class i
            _lowercase			 =data - column_reshape(A__  )
            if i > 0:
                  # If covariance_sum is not None
                  covariance_sum += np.dot(A__   ,     centered_data.T  )
            else:
                  # If covariance_sum is np.nan (i.e. first loop)
                  _lowercase			 =np.dot(A__   ,     centered_data.T  )

      return covariance_sum / features.shape[1]
def   a   (    A__			:     np.ndarray   ,     A__			:     np.ndarray   ,     A__			:     int  )					->   np.ndarray:





      """simple docstring"""



      _lowercase			 =features.mean(1  )
      _lowercase			 =np.nan
      for i in range(A__  ):
            _lowercase			 =features[:, labels == i]
            _lowercase			 =data.shape[1]
            _lowercase			 =data.mean(1  )
            if i > 0:
                  # If covariance_sum is not None
                  covariance_sum += device_data * np.dot(
                      column_reshape(A__  ) - column_reshape(A__  )   ,     (column_reshape(A__  ) - column_reshape(A__  )).T   ,     )
            else:
                  # If covariance_sum is np.nan (i.e. first loop)
                  _lowercase			 =device_data * np.dot(
                      column_reshape(A__  ) - column_reshape(A__  )   ,     (column_reshape(A__  ) - column_reshape(A__  )).T   ,     )

      return covariance_sum / features.shape[1]
def   a   (    A__			:     np.ndarray   ,     A__			:     int  )					->   np.ndarray:





      """simple docstring"""



      if features.any():
            _lowercase			 =features.mean(1  )
            # Center the dataset
            _lowercase			 =features - np.reshape(A__   ,     (data_mean.size, 1)  )
            _lowercase			 =np.dot(A__   ,     centered_data.T  ) / features.shape[1]
            _lowercase , _lowercase			 =np.linalg.eigh(A__  )
            # Take all the columns in the reverse order (-1), and then takes only the first
            _lowercase			 =eigenvectors[:, ::-1][:, 0:dimensions]
            # Project the database on the new space
            _lowercase			 =np.dot(filtered_eigenvectors.T   ,     A__  )
            logging.info('Principal Component Analysis computed'  )

            return projected_data
      else:
            logging.basicConfig(level=logging.ERROR   ,     format='%(message)s'   ,     force=A__  )
            logging.error('Dataset empty'  )
            raise AssertionError
def   a   (    A__			:     np.ndarray   ,     A__			:     np.ndarray   ,     A__			:     int   ,     A__			:     int  )					->   np.ndarray:





      """simple docstring"""



      assert classes > dimensions

      # Check if features have been already loaded
      if features.any:
            _lowercase , _lowercase			 =eigh(
                covariance_between_classes(A__   ,     A__   ,     A__  )   ,     covariance_within_classes(A__   ,     A__   ,     A__  )   ,     )
            _lowercase			 =eigenvectors[:, ::-1][:, :dimensions]
            _lowercase , _lowercase , _lowercase			 =np.linalg.svd(A__  )
            _lowercase			 =svd_matrix[:, 0:dimensions]
            _lowercase			 =np.dot(filtered_svd_matrix.T   ,     A__  )
            logging.info('Linear Discriminant Analysis computed'  )

            return projected_data
      else:
            logging.basicConfig(level=logging.ERROR   ,     format='%(message)s'   ,     force=A__  )
            logging.error('Dataset empty'  )
            raise AssertionError
def   a   (    )					->   None:





      """simple docstring"""



      _lowercase			 =np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]  )
      _lowercase			 =np.array([0, 0, 0, 1, 1]  )
      _lowercase			 =2
      _lowercase			 =2

      # Assert that the function raises an AssertionError if dimensions > classes
      with pytest.raises(A__  ) as error_info:
            _lowercase			 =linear_discriminant_analysis(
                A__   ,     A__   ,     A__   ,     A__  )
            if isinstance(A__   ,     np.ndarray  ):
                  raise AssertionError(
                      'Did not raise AssertionError for dimensions > classes'  )
            assert error_info.type is AssertionError
def   a   (    )					->   None:





      """simple docstring"""



      _lowercase			 =np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]  )
      _lowercase			 =2
      _lowercase			 =np.array([[6.92820323, 8.66025404, 10.39230485], [3.0, 3.0, 3.0]]  )

      with pytest.raises(A__  ) as error_info:
            _lowercase			 =principal_component_analysis(A__   ,     A__  )
            if not np.allclose(A__   ,     A__  ):
                  raise AssertionError
            assert error_info.type is AssertionError


if __name__ == "__main__":
  import doctest

  doctest.testmod()





from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
  from transformers import (
      TFBertModel,
      TFCLIPVisionModel,
      TFDeiTModel,
      TFRobertaModel,
      TFVisionTextDualEncoderModel,
      TFViTModel,
      VisionTextDualEncoderConfig,
  )

if is_vision_available():
  from PIL import Image

  from transformers import VisionTextDualEncoderProcessor
def   a   (    A__			:     List[Any]  )					->   Union[str, Any]:





      """simple docstring"""



      if isinstance(A__   ,     collections.abc.Iterable  ):
            return x
      return (x, x)





@require_tf
class 				__lowerCAmelCase  :




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				List[Any]:
        '''simple docstring'''






        pass




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        pass




  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        pass




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase			 =VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase							,     lowerCAmelCase      )

        _lowercase			 =TFVisionTextDualEncoderModel(lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )

        self.assertEqual(output['text_embeds'].shape							,     (input_ids.shape[0], config.projection_dim)      )
        self.assertEqual(output['image_embeds'].shape							,     (pixel_values.shape[0], config.projection_dim)      )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				int:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )

        self.assertEqual(output['text_embeds'].shape							,     (input_ids.shape[0], model.config.projection_dim)      )
        self.assertEqual(output['image_embeds'].shape							,     (pixel_values.shape[0], model.config.projection_dim)      )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 ={'vision_model': vision_model, 'text_model': text_model}
        _lowercase			 =TFVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )

        self.assertEqual(output['text_embeds'].shape							,     (input_ids.shape[0], model.config.projection_dim)      )
        self.assertEqual(output['image_embeds'].shape							,     (pixel_values.shape[0], model.config.projection_dim)      )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				int:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )
        _lowercase			 =output[0].numpy()

        with tempfile.TemporaryDirectory() as tmpdirname:
              model.save_pretrained(lowerCAmelCase      )
              _lowercase			 =TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase      )

              _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )
              _lowercase			 =after_output[0].numpy()
              _lowercase			 =np.amax(np.abs(out_a - out_a      )      )
              self.assertLessEqual(lowerCAmelCase							,     1e-5      )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				Optional[int]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(
            input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase							,     output_attentions=lowerCAmelCase      )

        _lowercase			 =output.vision_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     vision_config.num_hidden_layers      )

        # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
        _lowercase			 =to_atuple(vision_model.config.image_size      )
        _lowercase			 =to_atuple(vision_model.config.patch_size      )
        _lowercase			 =(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
        _lowercase			 =num_patches + 1
        self.assertEqual(vision_attentions[0].shape[-3:]							,     (vision_config.num_attention_heads, seq_len, seq_len)      )

        _lowercase			 =output.text_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     text_config.num_hidden_layers      )

        self.assertEqual(
            text_attentions[0].shape[-3:]							,     (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])							,     )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase			 =np.abs((a - b)      ).max()
        self.assertLessEqual(lowerCAmelCase							,     lowerCAmelCase							,     F'''Difference between torch and flax is {diff} (>= {tol}).'''      )




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_vision_text_dual_encoder_model(**lowerCAmelCase      )




  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_model_from_pretrained_configs(**lowerCAmelCase      )




  def 						A__  (       self      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase      )




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_save_load(**lowerCAmelCase      )




  def 						A__  (       self      )						->				Tuple:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_vision_text_output_attention(**lowerCAmelCase      )




  @slow
  def 						A__  (       self      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_pretrained_model_and_inputs()

        _lowercase			 =model_a(**lowerCAmelCase      )
        _lowercase			 =outputs[0].numpy()

        with tempfile.TemporaryDirectory() as tmp_dirname:
              model_a.save_pretrained(lowerCAmelCase      )
              _lowercase			 =TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase      )

              _lowercase			 =model_a(**lowerCAmelCase      )
              _lowercase			 =after_outputs[0].numpy()
              _lowercase			 =np.amax(np.abs(out_a - out_a      )      )
              self.assertLessEqual(lowerCAmelCase							,     1e-5      )





@require_tf
class 				__lowerCAmelCase  (      SCREAMING_SNAKE_CASE    ,    unittest.TestCase							):




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =TFVisionTextDualEncoderModel.from_vision_text_pretrained(
            'hf-internal-testing/tiny-random-vit'							,     'hf-internal-testing/tiny-random-bert'      )
        _lowercase			 =13
        _lowercase			 =floats_tensor(
            [
                batch_size,
                model.vision_model.config.num_channels,
                model.vision_model.config.image_size,
                model.vision_model.config.image_size,
            ]      )
        _lowercase			 =ids_tensor([batch_size, 4]							,     model.text_model.config.vocab_size      )
        _lowercase			 =random_attention_mask([batch_size, 4]      )
        _lowercase			 ={'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

        return model, inputs




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =TFViTModel(lowerCAmelCase							,     name='vision_model'      )
        _lowercase			 =TFBertModel(lowerCAmelCase							,     name='text_model'      )
        return vision_model, text_model




  def 						A__  (       self      )						->				Any:
        '''simple docstring'''






        _lowercase			 =TFViTModelTester(self      )
        _lowercase			 =TFBertModelTester(self      )
        _lowercase			 =vit_model_tester.prepare_config_and_inputs()
        _lowercase			 =bert_model_tester.prepare_config_and_inputs()

        _lowercase , _lowercase , _lowercase			 =vision_config_and_inputs

        (
            (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , 
        )			 =text_config_and_inputs

        return {
            "text_config": text_config,
            "vision_config": vision_config,
            "pixel_values": pixel_values,
            "attention_mask": input_mask,
            "input_ids": input_ids,
            "text_token_type_ids": token_type_ids,
            "text_sequence_labels": sequence_labels,
            "text_token_labels": token_labels,
            "text_choice_labels": choice_labels,
        }





@require_tf
class 				__lowerCAmelCase  (      SCREAMING_SNAKE_CASE    ,    unittest.TestCase							):




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =TFVisionTextDualEncoderModel.from_vision_text_pretrained(
            'Rocketknight1/tiny-random-deit-tf'							,     'hf-internal-testing/tiny-random-roberta'      )
        _lowercase			 =13
        _lowercase			 =floats_tensor(
            [
                batch_size,
                model.vision_model.config.num_channels,
                model.vision_model.config.image_size,
                model.vision_model.config.image_size,
            ]      )
        _lowercase			 =ids_tensor([batch_size, 4]							,     model.text_model.config.vocab_size      )
        _lowercase			 =random_attention_mask([batch_size, 4]      )
        _lowercase			 ={'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

        return model, inputs




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				List[str]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(
            input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase							,     output_attentions=lowerCAmelCase      )

        _lowercase			 =output.vision_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     vision_config.num_hidden_layers      )

        # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
        _lowercase			 =to_atuple(vision_model.config.image_size      )
        _lowercase			 =to_atuple(vision_model.config.patch_size      )
        _lowercase			 =(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
        _lowercase			 =num_patches + 2
        self.assertEqual(vision_attentions[0].shape[-3:]							,     (vision_config.num_attention_heads, seq_len, seq_len)      )

        _lowercase			 =output.text_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     text_config.num_hidden_layers      )

        self.assertEqual(
            text_attentions[0].shape[-3:]							,     (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])							,     )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Tuple:
        '''simple docstring'''






        _lowercase			 =TFDeiTModel(lowerCAmelCase							,     name='vision_model'      )
        _lowercase			 =TFRobertaModel(lowerCAmelCase							,     name='text_model'      )
        return vision_model, text_model




  def 						A__  (       self      )						->				Any:
        '''simple docstring'''






        _lowercase			 =TFDeiTModelTester(self      )
        _lowercase			 =TFRobertaModelTester(self      )
        _lowercase			 =vit_model_tester.prepare_config_and_inputs()
        _lowercase			 =bert_model_tester.prepare_config_and_inputs()

        _lowercase , _lowercase , _lowercase			 =vision_config_and_inputs

        (
            (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , 
        )			 =text_config_and_inputs

        return {
            "text_config": text_config,
            "vision_config": vision_config,
            "pixel_values": pixel_values,
            "attention_mask": input_mask,
            "input_ids": input_ids,
            "text_token_type_ids": token_type_ids,
            "text_sequence_labels": sequence_labels,
            "text_token_labels": token_labels,
            "text_choice_labels": choice_labels,
        }





@require_tf
class 				__lowerCAmelCase  (      SCREAMING_SNAKE_CASE    ,    unittest.TestCase							):




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =TFVisionTextDualEncoderModel.from_vision_text_pretrained(
            'Rocketknight1/tiny-random-clip-tf'							,     'hf-internal-testing/tiny-random-bert'      )
        _lowercase			 =13
        _lowercase			 =floats_tensor(
            [
                batch_size,
                model.vision_model.config.num_channels,
                model.vision_model.config.image_size,
                model.vision_model.config.image_size,
            ]      )
        _lowercase			 =ids_tensor([batch_size, 4]							,     model.text_model.config.vocab_size      )
        _lowercase			 =random_attention_mask([batch_size, 4]      )
        _lowercase			 ={'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

        return model, inputs




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Tuple:
        '''simple docstring'''






        _lowercase			 =TFCLIPVisionModel(lowerCAmelCase							,     name='vision_model'      )
        _lowercase			 =TFBertModel(lowerCAmelCase							,     name='text_model'      )
        return vision_model, text_model




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =TFCLIPVisionModelTester(self      )
        _lowercase			 =TFBertModelTester(self      )
        _lowercase			 =clip_model_tester.prepare_config_and_inputs()
        _lowercase			 =bert_model_tester.prepare_config_and_inputs()

        _lowercase , _lowercase			 =vision_config_and_inputs

        (
            (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , 
        )			 =text_config_and_inputs

        return {
            "text_config": text_config,
            "vision_config": vision_config,
            "pixel_values": pixel_values,
            "attention_mask": input_mask,
            "input_ids": input_ids,
            "text_token_type_ids": token_type_ids,
            "text_sequence_labels": sequence_labels,
            "text_token_labels": token_labels,
            "text_choice_labels": choice_labels,
        }





@require_vision
@require_tf
class 				__lowerCAmelCase  (      unittest.TestCase							):




  @slow
  def 						A__  (       self      )						->				Optional[int]:
        '''simple docstring'''






        _lowercase			 =TFVisionTextDualEncoderModel.from_pretrained(
            'clip-italian/clip-italian'							,     logit_scale_init_value=1.0							,     from_pt=lowerCAmelCase      )
        _lowercase			 =VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'      )

        _lowercase			 =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
        _lowercase			 =processor(
            text=['una foto di un gatto', 'una foto di un cane']							,     images=lowerCAmelCase							,     padding=lowerCAmelCase							,     return_tensors='np'      )

        _lowercase			 =model(**lowerCAmelCase      )

        # verify the logits
        self.assertEqual(outputs.logits_per_image.shape							,     (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])      )
        self.assertEqual(
            outputs.logits_per_text.shape							,     (inputs.input_ids.shape[0], inputs.pixel_values.shape[0])							,     )

        _lowercase			 =np.array([[1.2284727, 0.3104122]]      )

        self.assertTrue(np.allclose(outputs.logits_per_image.numpy()							,     lowerCAmelCase							,     atol=1e-3      )      )


