


'''simple docstring'''






from __future__ import annotations

import unittest

from transformers import is_tf_available
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 numpy
 import tensorflow as tf

 from transformers import (
     TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
     TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
     TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
     BertConfig,
     DPRConfig,
     TFDPRContextEncoder,
     TFDPRQuestionEncoder,
     TFDPRReader,
 )






class    __magic_name__				:






       def __init__(     self    ,				snake_case    ,				snake_case=1_3    ,				snake_case=7    ,				snake_case=True    ,				snake_case=True    ,				snake_case=True    ,				snake_case=True    ,				snake_case=9_9    ,				snake_case=3_2    ,				snake_case=2    ,				snake_case=4    ,				snake_case=3_7    ,				snake_case="gelu"    ,				snake_case=0.1    ,				snake_case=0.1    ,				snake_case=5_1_2    ,				snake_case=1_6    ,				snake_case=2    ,				snake_case=0.02    ,				snake_case=3    ,				snake_case=4    ,				snake_case=None    ,				snake_case=0    ,				)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =parent
          _UpperCAmelCase  :  Optional[int]				       =batch_size
          _UpperCAmelCase  :  List[str]				       =seq_length
          _UpperCAmelCase  :  Union[str, Any]				       =is_training
          _UpperCAmelCase  :  List[Any]				       =use_input_mask
          _UpperCAmelCase  :  Union[str, Any]				       =use_token_type_ids
          _UpperCAmelCase  :  List[str]				       =use_labels
          _UpperCAmelCase  :  Any				       =vocab_size
          _UpperCAmelCase  :  List[str]				       =hidden_size
          _UpperCAmelCase  :  Tuple				       =num_hidden_layers
          _UpperCAmelCase  :  Tuple				       =num_attention_heads
          _UpperCAmelCase  :  Optional[Any]				       =intermediate_size
          _UpperCAmelCase  :  Tuple				       =hidden_act
          _UpperCAmelCase  :  Union[str, Any]				       =hidden_dropout_prob
          _UpperCAmelCase  :  Dict				       =attention_probs_dropout_prob
          _UpperCAmelCase  :  List[str]				       =max_position_embeddings
          _UpperCAmelCase  :  Union[str, Any]				       =type_vocab_size
          _UpperCAmelCase  :  Optional[Any]				       =type_sequence_label_size
          _UpperCAmelCase  :  Optional[int]				       =initializer_range
          _UpperCAmelCase  :  List[Any]				       =num_labels
          _UpperCAmelCase  :  Tuple				       =num_choices
          _UpperCAmelCase  :  List[str]				       =scope
          _UpperCAmelCase  :  List[Any]				       =projection_dim






       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =ids_tensor([self.batch_size, self.seq_length]    ,				self.vocab_size)

          _UpperCAmelCase  :  Optional[Any]				       =None
          if self.use_input_mask:
             # follow test_modeling_tf_ctrl.py
             _UpperCAmelCase  :  List[Any]				       =random_attention_mask([self.batch_size, self.seq_length])

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

          _UpperCAmelCase  :  Any				       =None
          _UpperCAmelCase  :  Union[str, Any]				       =None
          _UpperCAmelCase  :  Union[str, Any]				       =None
          if self.use_labels:
             _UpperCAmelCase  :  int				       =ids_tensor([self.batch_size]    ,				self.type_sequence_label_size)
             _UpperCAmelCase  :  Tuple				       =ids_tensor([self.batch_size, self.seq_length]    ,				self.num_labels)
             _UpperCAmelCase  :  Tuple				       =ids_tensor([self.batch_size]    ,				self.num_choices)

          _UpperCAmelCase  :  int				       =BertConfig(
              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    ,				is_decoder=snake_case    ,				initializer_range=self.initializer_range    ,				)
          _UpperCAmelCase  :  Dict				       =DPRConfig(projection_dim=self.projection_dim    ,				**config.to_dict())

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






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =TFDPRContextEncoder(config=snake_case)
          _UpperCAmelCase  :  str				       =model(snake_case    ,				attention_mask=snake_case    ,				token_type_ids=snake_case)
          _UpperCAmelCase  :  Dict				       =model(snake_case    ,				token_type_ids=snake_case)
          _UpperCAmelCase  :  Union[str, Any]				       =model(snake_case)
          self.parent.assertEqual(result.pooler_output.shape    ,				(self.batch_size, self.projection_dim or self.hidden_size))






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =TFDPRQuestionEncoder(config=snake_case)
          _UpperCAmelCase  :  Union[str, Any]				       =model(snake_case    ,				attention_mask=snake_case    ,				token_type_ids=snake_case)
          _UpperCAmelCase  :  int				       =model(snake_case    ,				token_type_ids=snake_case)
          _UpperCAmelCase  :  List[str]				       =model(snake_case)
          self.parent.assertEqual(result.pooler_output.shape    ,				(self.batch_size, self.projection_dim or self.hidden_size))






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case    ,				snake_case)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =TFDPRReader(config=snake_case)
          _UpperCAmelCase  :  str				       =model(snake_case    ,				attention_mask=snake_case)

          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))
          self.parent.assertEqual(result.relevance_logits.shape    ,				(self.batch_size,))






       def      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[Any]				       =self.prepare_config_and_inputs()
          (
              (
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			
          )  :  Tuple				       =config_and_inputs
          _UpperCAmelCase  :  List[Any]				       ={'input_ids': input_ids}
          return config, inputs_dict






@require_tf
class    __magic_name__				(						lowerCAmelCase   ,lowerCAmelCase   ,unittest.TestCase							):
       UpperCAmelCase		 =(
           (
               TFDPRContextEncoder,
               TFDPRQuestionEncoder,
               TFDPRReader,
           )
           if is_tf_available()
           else ()
       )
       UpperCAmelCase		 ={"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

       UpperCAmelCase		 =False
       UpperCAmelCase		 =False
       UpperCAmelCase		 =False
       UpperCAmelCase		 =False
       UpperCAmelCase		 =False






       def      lowerCAmelCase						(     self)		->    int:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =TFDPRModelTester(self)
          _UpperCAmelCase  :  str				       =ConfigTester(self    ,				config_class=snake_case    ,				hidden_size=3_7)






       def      lowerCAmelCase						(     self)		->    List[Any]:




          '''simple docstring'''
          self.config_tester.run_common_tests()






       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_dpr_context_encoder(*snake_case)






       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_dpr_question_encoder(*snake_case)






       def      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_dpr_reader(*snake_case)






       @slow
       def      lowerCAmelCase						(     self)		->    int:




          '''simple docstring'''
          for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase  :  Dict				       =TFDPRContextEncoder.from_pretrained(snake_case)
             self.assertIsNotNone(snake_case)

          for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase  :  int				       =TFDPRContextEncoder.from_pretrained(snake_case)
             self.assertIsNotNone(snake_case)

          for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase  :  Dict				       =TFDPRQuestionEncoder.from_pretrained(snake_case)
             self.assertIsNotNone(snake_case)

          for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase  :  str				       =TFDPRReader.from_pretrained(snake_case)
             self.assertIsNotNone(snake_case)






@require_tf
class    __magic_name__				(						unittest.TestCase							):






       @slow
       def      lowerCAmelCase						(     self)		->    Optional[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base')

          _UpperCAmelCase  :  Dict				       =tf.constant(
              [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]])  # [CLS] hello, is my dog cute? [SEP]
          _UpperCAmelCase  :  Dict				       =model(snake_case)[0]  # embedding shape = (1, 768)
          # compare the actual values for a slice.
          _UpperCAmelCase  :  List[Any]				       =tf.constant(
              [
                  [
                      0.03_23_62_53,
                      0.12_75_33_35,
                      0.16_81_85_09,
                      0.00_27_97_86,
                      0.3_89_69_33,
                      0.24_26_49_45,
                      0.2_17_89_71,
                      -0.02_33_52_27,
                      -0.08_48_19_59,
                      -0.14_32_41_17,
                  ]
              ])
          self.assertTrue(numpy.allclose(output[:, :1_0].numpy()    ,				expected_slice.numpy()    ,				atol=1E-4))





'''simple docstring'''






import unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible






class    __magic_name__				(						unittest.TestCase							):






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =[
              'safety_checker/pytorch_model.bin',
              'safety_checker/model.safetensors',
              'vae/diffusion_pytorch_model.bin',
              'vae/diffusion_pytorch_model.safetensors',
              'text_encoder/pytorch_model.bin',
              'text_encoder/model.safetensors',
              'unet/diffusion_pytorch_model.bin',
              'unet/diffusion_pytorch_model.safetensors',
          ]
          self.assertTrue(is_safetensors_compatible(snake_case))






       def      lowerCAmelCase						(     self)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =[
              'unet/diffusion_pytorch_model.bin',
              'unet/diffusion_pytorch_model.safetensors',
          ]
          self.assertTrue(is_safetensors_compatible(snake_case))






       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =[
              'safety_checker/pytorch_model.bin',
              'safety_checker/model.safetensors',
              'vae/diffusion_pytorch_model.bin',
              'vae/diffusion_pytorch_model.safetensors',
              'text_encoder/pytorch_model.bin',
              'text_encoder/model.safetensors',
              'unet/diffusion_pytorch_model.bin',
              # Removed: 'unet/diffusion_pytorch_model.safetensors',
          ]
          self.assertFalse(is_safetensors_compatible(snake_case))






       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  int				       =[
              'text_encoder/pytorch_model.bin',
              'text_encoder/model.safetensors',
          ]
          self.assertTrue(is_safetensors_compatible(snake_case))






       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          _UpperCAmelCase  :  Any				       =[
              'safety_checker/pytorch_model.bin',
              'safety_checker/model.safetensors',
              'vae/diffusion_pytorch_model.bin',
              'vae/diffusion_pytorch_model.safetensors',
              'text_encoder/pytorch_model.bin',
              # Removed: 'text_encoder/model.safetensors',
              'unet/diffusion_pytorch_model.bin',
              'unet/diffusion_pytorch_model.safetensors',
          ]
          self.assertFalse(is_safetensors_compatible(snake_case))






       def      lowerCAmelCase						(     self)		->    List[Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =[
              'safety_checker/pytorch_model.fp16.bin',
              'safety_checker/model.fp16.safetensors',
              'vae/diffusion_pytorch_model.fp16.bin',
              'vae/diffusion_pytorch_model.fp16.safetensors',
              'text_encoder/pytorch_model.fp16.bin',
              'text_encoder/model.fp16.safetensors',
              'unet/diffusion_pytorch_model.fp16.bin',
              'unet/diffusion_pytorch_model.fp16.safetensors',
          ]
          _UpperCAmelCase  :  str				       ='fp16'
          self.assertTrue(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =[
              'unet/diffusion_pytorch_model.fp16.bin',
              'unet/diffusion_pytorch_model.fp16.safetensors',
          ]
          _UpperCAmelCase  :  int				       ='fp16'
          self.assertTrue(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    Optional[int]:




          '''simple docstring'''
          # pass variant but use the non-variant filenames
          _UpperCAmelCase  :  Optional[int]				       =[
              'unet/diffusion_pytorch_model.bin',
              'unet/diffusion_pytorch_model.safetensors',
          ]
          _UpperCAmelCase  :  Optional[Any]				       ='fp16'
          self.assertTrue(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    int:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =[
              'safety_checker/pytorch_model.fp16.bin',
              'safety_checker/model.fp16.safetensors',
              'vae/diffusion_pytorch_model.fp16.bin',
              'vae/diffusion_pytorch_model.fp16.safetensors',
              'text_encoder/pytorch_model.fp16.bin',
              'text_encoder/model.fp16.safetensors',
              'unet/diffusion_pytorch_model.fp16.bin',
              # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
          ]
          _UpperCAmelCase  :  List[str]				       ='fp16'
          self.assertFalse(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  Dict				       =[
              'text_encoder/pytorch_model.fp16.bin',
              'text_encoder/model.fp16.safetensors',
          ]
          _UpperCAmelCase  :  Optional[Any]				       ='fp16'
          self.assertTrue(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    Any:




          '''simple docstring'''
          # pass variant but use the non-variant filenames
          _UpperCAmelCase  :  Optional[int]				       =[
              'text_encoder/pytorch_model.bin',
              'text_encoder/model.safetensors',
          ]
          _UpperCAmelCase  :  List[Any]				       ='fp16'
          self.assertTrue(is_safetensors_compatible(snake_case    ,				variant=snake_case))






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =[
              'safety_checker/pytorch_model.fp16.bin',
              'safety_checker/model.fp16.safetensors',
              'vae/diffusion_pytorch_model.fp16.bin',
              'vae/diffusion_pytorch_model.fp16.safetensors',
              'text_encoder/pytorch_model.fp16.bin',
              # 'text_encoder/model.fp16.safetensors',
              'unet/diffusion_pytorch_model.fp16.bin',
              'unet/diffusion_pytorch_model.fp16.safetensors',
          ]
          _UpperCAmelCase  :  Tuple				       ='fp16'
          self.assertFalse(is_safetensors_compatible(snake_case    ,				variant=snake_case))

