



'''simple docstring'''






import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_flax_cross_test,
    require_flax,
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import is_flax_available, is_torch_available, is_vision_available

from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_flax_bert import FlaxBertModelTester
from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester
from ..vit.test_modeling_flax_vit import FlaxViTModelTester


if is_flax_available():
     from transformers import (
         FlaxBertModel,
         FlaxCLIPVisionModel,
         FlaxVisionTextDualEncoderModel,
         FlaxViTModel,
         VisionTextDualEncoderConfig,
         VisionTextDualEncoderProcessor,
     )
     from transformers.modeling_flax_pytorch_utils import (
         convert_pytorch_state_dict_to_flax,
         load_flax_weights_in_pytorch_model,
     )


if is_torch_available():
     import torch

     from transformers import VisionTextDualEncoderModel

if is_vision_available():
     from PIL import Image
def    lowerCamelCase__ (		_A       ):
  if isinstance(_A				,    collections.abc.Iterable       ):
    return x
  return (x, x)

@require_flax
class 							a__:







    def     lowercase_ (   self				:						int		,    __snake_case				:						Optional[Any]		,    __snake_case				:						List[str]			):
      pass







    def     lowercase_ (   self				:						Optional[Any]			):
      pass







    def     lowercase_ (   self				:						Optional[int]			):
      pass







    def     lowercase_ (   self				:						Dict		,    __snake_case				:						np.ndarray		,    __snake_case				:						np.ndarray		,    __snake_case				:						float			):
      a		:      Optional[int]       =  np.abs((a - b)			).max()
      self.assertLessEqual(__snake_case		,    __snake_case		,    F"""Difference between torch and flax is {diff} (>= {tol})."""			)







    def     lowercase_ (   self				:						Any		,    __snake_case				:						Optional[Any]		,    __snake_case				:						Dict		,    __snake_case				:						Union[str, Any]		,    __snake_case				:						Optional[Any]		,    __snake_case				:						int=None		,    **__snake_case				:						Optional[Any]			):
      a		:      Optional[Any]       =  VisionTextDualEncoderConfig.from_vision_text_configs(__snake_case		,    __snake_case			)

      a		:      Dict       =  FlaxVisionTextDualEncoderModel(__snake_case			)

      a		:      List[str]       =  model(input_ids=__snake_case		,    pixel_values=__snake_case		,    attention_mask=__snake_case			)

      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     lowercase_ (   self				:						Dict		,    __snake_case				:						int		,    __snake_case				:						str		,    __snake_case				:						Any		,    __snake_case				:						Optional[int]		,    __snake_case				:						List[str]=None		,    **__snake_case				:						int			):
      a     ,			a		:      Union[str, Any]       =  self.get_vision_text_model(__snake_case		,    __snake_case			)
      a		:      Union[str, Any]       =  {'vision_model': vision_model, 'text_model': text_model}
      a		:      Union[str, Any]       =  FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__snake_case			)

      a		:      Tuple       =  model(input_ids=__snake_case		,    pixel_values=__snake_case		,    attention_mask=__snake_case			)

      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     lowercase_ (   self				:						Optional[Any]		,    __snake_case				:						int		,    __snake_case				:						Tuple		,    __snake_case				:						List[Any]		,    __snake_case				:						Any		,    __snake_case				:						List[Any]=None		,    **__snake_case				:						Optional[int]			):
      a     ,			a		:      Union[str, Any]       =  self.get_vision_text_model(__snake_case		,    __snake_case			)
      a		:      Optional[int]       =  {'vision_model': vision_model, 'text_model': text_model}
      a		:      Optional[Any]       =  FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__snake_case			)

      a		:      Tuple       =  model(input_ids=__snake_case		,    pixel_values=__snake_case		,    attention_mask=__snake_case			)
      a		:      Optional[Any]       =  output[0]

      with tempfile.TemporaryDirectory() as tmpdirname:
        model.save_pretrained(__snake_case			)
        a		:      Optional[int]       =  FlaxVisionTextDualEncoderModel.from_pretrained(__snake_case			)

        a		:      List[Any]       =  model(input_ids=__snake_case		,    pixel_values=__snake_case		,    attention_mask=__snake_case			)
        a		:      str       =  after_output[0]
        a		:      Union[str, Any]       =  np.amax(np.abs(out_a - out_a			)			)
        self.assertLessEqual(__snake_case		,    1e-3			)







    def     lowercase_ (   self				:						str		,    __snake_case				:						Dict		,    __snake_case				:						int		,    __snake_case				:						List[Any]		,    __snake_case				:						List[Any]		,    __snake_case				:						int=None		,    **__snake_case				:						Optional[int]			):
      a     ,			a		:      Tuple       =  self.get_vision_text_model(__snake_case		,    __snake_case			)
      a		:      Dict       =  {'vision_model': vision_model, 'text_model': text_model}
      a		:      Optional[Any]       =  FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**__snake_case			)

      a		:      Optional[int]       =  model(
          input_ids=__snake_case		,    pixel_values=__snake_case		,    attention_mask=__snake_case		,    output_attentions=__snake_case			)

      a		:      Dict       =  output.vision_model_output.attentions
      self.assertEqual(len(__snake_case			)		,    vision_config.num_hidden_layers			)

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

      a		:      Optional[Any]       =  output.text_model_output.attentions
      self.assertEqual(len(__snake_case			)		,    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     lowercase_ (   self				:						Optional[Any]		,    __snake_case				:						List[str]		,    __snake_case				:						Any		,    __snake_case				:						Dict			):
      pt_model.to(__snake_case			)
      pt_model.eval()

      # prepare inputs
      a		:      List[str]       =  inputs_dict
      a		:      Optional[Any]       =  {k: torch.tensor(v.tolist()			) for k, v in flax_inputs.items()}

      with torch.no_grad():
        a		:      Optional[int]       =  pt_model(**__snake_case			).to_tuple()

      a		:      Any       =  fx_model(**__snake_case			).to_tuple()
      self.assertEqual(len(__snake_case			)		,    len(__snake_case			)		,    'Output lengths differ between Flax and PyTorch'			)
      for fx_output, pt_output in zip(fx_outputs[:4]		,    pt_outputs[:4]			):
        self.assert_almost_equals(__snake_case		,    pt_output.numpy()		,    4e-2			)

      # PT -> Flax
      with tempfile.TemporaryDirectory() as tmpdirname:
        pt_model.save_pretrained(__snake_case			)
        a		:      Union[str, Any]       =  FlaxVisionTextDualEncoderModel.from_pretrained(__snake_case		,    from_pt=__snake_case			)

      a		:      Dict       =  fx_model_loaded(**__snake_case			).to_tuple()
      self.assertEqual(len(__snake_case			)		,    len(__snake_case			)		,    'Output lengths differ between Flax and PyTorch'			)
      for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4]		,    pt_outputs[:4]			):
        self.assert_almost_equals(__snake_case		,    pt_output.numpy()		,    4e-2			)

      # Flax -> PT
      with tempfile.TemporaryDirectory() as tmpdirname:
        fx_model.save_pretrained(__snake_case			)
        a		:      List[Any]       =  VisionTextDualEncoderModel.from_pretrained(__snake_case		,    from_flax=__snake_case			)

      pt_model_loaded.to(__snake_case			)
      pt_model_loaded.eval()

      with torch.no_grad():
        a		:      Optional[int]       =  pt_model_loaded(**__snake_case			).to_tuple()

      self.assertEqual(len(__snake_case			)		,    len(__snake_case			)		,    'Output lengths differ between Flax and PyTorch'			)
      for fx_output, pt_output_loaded in zip(fx_outputs[:4]		,    pt_outputs_loaded[:4]			):
        self.assert_almost_equals(__snake_case		,    pt_output_loaded.numpy()		,    4e-2			)







    def     lowercase_ (   self				:						int		,    __snake_case				:						str		,    __snake_case				:						Any		,    __snake_case				:						Optional[int]			):
      a		:      Optional[int]       =  VisionTextDualEncoderConfig.from_vision_text_configs(__snake_case		,    __snake_case			)

      a		:      int       =  VisionTextDualEncoderModel(__snake_case			)
      a		:      Tuple       =  FlaxVisionTextDualEncoderModel(__snake_case			)

      a		:      str       =  convert_pytorch_state_dict_to_flax(pt_model.state_dict()		,    __snake_case			)
      a		:      Optional[Any]       =  fx_state

      self.check_pt_flax_equivalence(__snake_case		,    __snake_case		,    __snake_case			)







    def     lowercase_ (   self				:						Optional[int]		,    __snake_case				:						Any		,    __snake_case				:						Dict		,    __snake_case				:						str			):
      a		:      Any       =  VisionTextDualEncoderConfig.from_vision_text_configs(__snake_case		,    __snake_case			)

      a		:      Optional[Any]       =  VisionTextDualEncoderModel(__snake_case			)
      a		:      int       =  FlaxVisionTextDualEncoderModel(__snake_case			)

      a		:      Tuple       =  load_flax_weights_in_pytorch_model(__snake_case		,    fx_model.params			)

      self.check_pt_flax_equivalence(__snake_case		,    __snake_case		,    __snake_case			)







    def     lowercase_ (   self				:						List[Any]			):
      a		:      Dict       =  self.prepare_config_and_inputs()
      self.check_model_from_pretrained_configs(**__snake_case			)







    def     lowercase_ (   self				:						Optional[int]			):
      a		:      Union[str, Any]       =  self.prepare_config_and_inputs()
      self.check_vision_text_dual_encoder_from_pretrained(**__snake_case			)







    def     lowercase_ (   self				:						List[str]			):
      a		:      List[str]       =  self.prepare_config_and_inputs()
      self.check_save_load(**__snake_case			)







    def     lowercase_ (   self				:						List[Any]			):
      a		:      List[str]       =  self.prepare_config_and_inputs()
      self.check_vision_text_output_attention(**__snake_case			)







    @is_pt_flax_cross_test
    def     lowercase_ (   self				:						int			):
      a		:      Tuple       =  self.prepare_config_and_inputs()
      a		:      str       =  config_inputs_dict.pop('vision_config'			)
      a		:      Union[str, Any]       =  config_inputs_dict.pop('text_config'			)

      a		:      str       =  config_inputs_dict

      self.check_equivalence_pt_to_flax(__snake_case		,    __snake_case		,    __snake_case			)
      self.check_equivalence_flax_to_pt(__snake_case		,    __snake_case		,    __snake_case			)







    @slow
    def     lowercase_ (   self				:						List[Any]			):
      a     ,			a		:      List[str]       =  self.get_pretrained_model_and_inputs()

      a		:      Union[str, Any]       =  model_a(**__snake_case			)
      a		:      Dict       =  outputs[0]

      with tempfile.TemporaryDirectory() as tmp_dirname:
        model_a.save_pretrained(__snake_case			)
        a		:      Optional[Any]       =  FlaxVisionTextDualEncoderModel.from_pretrained(__snake_case			)

        a		:      Dict       =  model_a(**__snake_case			)
        a		:      List[str]       =  after_outputs[0]
        a		:      List[str]       =  np.amax(np.abs(out_a - out_a			)			)
        self.assertLessEqual(__snake_case		,    1e-5			)

@require_flax
class 							a__(      lowerCamelCase__	,				unittest.TestCase			):







    def     lowercase_ (   self				:						Dict			):
      a		:      str       =  FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
          'hf-internal-testing/tiny-random-vit'		,    'hf-internal-testing/tiny-bert'		,    vision_from_pt=__snake_case		,    text_from_pt=__snake_case		,    )
      a		:      int       =  13
      a		:      Union[str, Any]       =  floats_tensor(
          [
              batch_size,
              model.config.vision_config.num_channels,
              model.config.vision_config.image_size,
              model.config.vision_config.image_size,
          ]			)
      a		:      List[Any]       =  ids_tensor([batch_size, 4]		,    model.config.text_config.vocab_size			)
      a		:      Dict       =  random_attention_mask([batch_size, 4]			)
      a		:      Tuple       =  {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

      return model, inputs







    def     lowercase_ (   self				:						int		,    __snake_case				:						Optional[int]		,    __snake_case				:						List[str]			):
      a		:      Union[str, Any]       =  FlaxViTModel(__snake_case			)
      a		:      Any       =  FlaxBertModel(__snake_case			)
      return vision_model, text_model







    def     lowercase_ (   self				:						Union[str, Any]			):
      a		:      str       =  FlaxViTModelTester(self			)
      a		:      Optional[Any]       =  FlaxBertModelTester(self			)
      a		:      List[Any]       =  vit_model_tester.prepare_config_and_inputs()
      a		:      List[Any]       =  bert_model_tester.prepare_config_and_inputs()

      a     ,			a		:      List[Any]       =  vision_config_and_inputs

      a     ,			a     ,			a     ,			a		:      Optional[int]       =  text_config_and_inputs

      # make sure that cross attention layers are added
      return {
          "text_config": text_config,
          "vision_config": vision_config,
          "pixel_values": pixel_values,
          "attention_mask": attention_mask,
          "input_ids": input_ids,
          "token_type_ids": token_type_ids,
      }

@require_torch
class 							a__(      lowerCamelCase__	,				unittest.TestCase			):







    def     lowercase_ (   self				:						Dict			):
      a		:      Union[str, Any]       =  FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
          'hf-internal-testing/tiny-random-clip'		,    'hf-internal-testing/tiny-bert'		,    vision_from_pt=__snake_case		,    text_from_pt=__snake_case		,    )
      a		:      List[str]       =  13
      a		:      str       =  floats_tensor(
          [
              batch_size,
              model.config.vision_config.num_channels,
              model.config.vision_config.image_size,
              model.config.vision_config.image_size,
          ]			)
      a		:      List[Any]       =  ids_tensor([batch_size, 4]		,    model.config.text_config.vocab_size			)
      a		:      Any       =  random_attention_mask([batch_size, 4]			)
      a		:      int       =  {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

      return model, inputs







    def     lowercase_ (   self				:						Dict		,    __snake_case				:						Optional[Any]		,    __snake_case				:						Optional[Any]			):
      a		:      Optional[Any]       =  FlaxCLIPVisionModel(__snake_case			)
      a		:      Any       =  FlaxBertModel(__snake_case			)
      return vision_model, text_model







    def     lowercase_ (   self				:						Tuple			):
      a		:      List[str]       =  FlaxCLIPVisionModelTester(self			)
      a		:      Optional[int]       =  FlaxBertModelTester(self			)
      a		:      Tuple       =  clip_model_tester.prepare_config_and_inputs()
      a		:      Any       =  bert_model_tester.prepare_config_and_inputs()

      a     ,			a		:      Any       =  vision_config_and_inputs

      a     ,			a     ,			a     ,			a		:      int       =  text_config_and_inputs

      # make sure that cross attention layers are added
      return {
          "text_config": text_config,
          "vision_config": vision_config,
          "pixel_values": pixel_values,
          "attention_mask": attention_mask,
          "input_ids": input_ids,
          "token_type_ids": token_type_ids,
      }

@require_flax
@require_vision
class 							a__(      unittest.TestCase			):







    @slow
    def     lowercase_ (   self				:						Dict			):
      a		:      int       =  FlaxVisionTextDualEncoderModel.from_pretrained('clip-italian/clip-italian'		,    logit_scale_init_value=1.0			)
      a		:      List[Any]       =  VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'			)

      a		:      Optional[Any]       =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
      a		:      List[str]       =  processor(
          text=['una foto di un gatto', 'una foto di un cane']		,    images=__snake_case		,    padding=__snake_case		,    return_tensors='np'			)

      a		:      int       =  model(**__snake_case			)

      # 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])		,    )

      a		:      Optional[Any]       =  np.array([[1.2284727, 0.3104122]]			)

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




'''simple docstring'''






from math import factorial, pi
def    lowerCamelCase__ (		_A				,    _A = 30       ):

  if not isinstance(_A				,    (int, float)       ):
    raise ValueError('maclaurin_sin() requires either an int or float for theta'       )

  if not isinstance(_A				,    _A       ) or accuracy <= 0:
    raise ValueError('maclaurin_sin() requires a positive int for accuracy'       )

  a		:      List[Any]       =  float(_A       )
  a		:      Optional[int]       =  theta // (2 * pi)
  theta -= 2 * div * pi
  return sum(
      (-1) ** r * theta ** (2 * r + 1) / factorial(2 * r + 1       ) for r in range(_A       )       )
def    lowerCamelCase__ (		_A				,    _A = 30       ):

  if not isinstance(_A				,    (int, float)       ):
    raise ValueError('maclaurin_cos() requires either an int or float for theta'       )

  if not isinstance(_A				,    _A       ) or accuracy <= 0:
    raise ValueError('maclaurin_cos() requires a positive int for accuracy'       )

  a		:      List[str]       =  float(_A       )
  a		:      List[str]       =  theta // (2 * pi)
  theta -= 2 * div * pi
  return sum((-1) ** r * theta ** (2 * r) / factorial(2 * r       ) for r in range(_A       )       )


if __name__ == "__main__":
     import doctest

     doctest.testmod()

     print(maclaurin_sin(1_0))
     print(maclaurin_sin(-1_0))
     print(maclaurin_sin(1_0, 1_5))
     print(maclaurin_sin(-1_0, 1_5))

     print(maclaurin_cos(5))
     print(maclaurin_cos(-5))
     print(maclaurin_cos(1_0, 1_5))
     print(maclaurin_cos(-1_0, 1_5))