


'''simple docstring'''






from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
		import tensorflow as tf

		from transformers.models.xglm.modeling_tf_xglm import (
		    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFXGLMForCausalLM,
		    TFXGLMModel,
		)






@require_tf
class        __snake_case	:







				"""simple docstring"""



				lowercase       			= XGLMConfig
				lowercase       			= {}
				lowercase       			= "gelu"

				def __init__(       self   :  List[str]   ,						lowerCamelCase   :  List[str]   ,						lowerCamelCase   :  Optional[Any]=14   ,						lowerCamelCase   :  Optional[Any]=7   ,						lowerCamelCase   :  Union[str, Any]=True   ,						lowerCamelCase   :  Union[str, Any]=True   ,						lowerCamelCase   :  Any=True   ,						lowerCamelCase   :  Optional[Any]=99   ,						lowerCamelCase   :  Union[str, Any]=32   ,						lowerCamelCase   :  str=2   ,						lowerCamelCase   :  List[Any]=4   ,						lowerCamelCase   :  Any=37   ,						lowerCamelCase   :  List[Any]="gelu"   ,						lowerCamelCase   :  int=0.1   ,						lowerCamelCase   :  Tuple=0.1   ,						lowerCamelCase   :  Dict=5_12   ,						lowerCamelCase   :  Any=0.02   ,						)       ->  Any:
						lowerCAmelCase_      :				Any      =       parent
						lowerCAmelCase_      :				Optional[Any]      =       batch_size
						lowerCAmelCase_      :				str      =       seq_length
						lowerCAmelCase_      :				Any      =       is_training
						lowerCAmelCase_      :				Optional[int]      =       use_input_mask
						lowerCAmelCase_      :				Tuple      =       use_labels
						lowerCAmelCase_      :				int      =       vocab_size
						lowerCAmelCase_      :				List[str]      =       d_model
						lowerCAmelCase_      :				List[Any]      =       num_hidden_layers
						lowerCAmelCase_      :				Any      =       num_attention_heads
						lowerCAmelCase_      :				Tuple      =       ffn_dim
						lowerCAmelCase_      :				Dict      =       activation_function
						lowerCAmelCase_      :				int      =       activation_dropout
						lowerCAmelCase_      :				List[str]      =       attention_dropout
						lowerCAmelCase_      :				Optional[int]      =       max_position_embeddings
						lowerCAmelCase_      :				Tuple      =       initializer_range
						lowerCAmelCase_      :				str      =       None
						lowerCAmelCase_      :				int      =       0
						lowerCAmelCase_      :				Dict      =       2
						lowerCAmelCase_      :				List[Any]      =       1

				def __lowercase       (       self   :  Dict		)       ->  Optional[int]:
						return XGLMConfig.from_pretrained("""facebook/xglm-564M"""		)

				def __lowercase       (       self   :  Any		)       ->  List[str]:
						lowerCAmelCase_      :				Dict      =       tf.clip_by_value(
						    ids_tensor([self.batch_size, self.seq_length]   ,						self.vocab_size		)   ,						clip_value_min=0   ,						clip_value_max=3		)

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

						lowerCAmelCase_      :				Tuple      =       self.get_config()

						lowerCAmelCase_      :				Union[str, Any]      =       floats_tensor([self.num_hidden_layers, self.num_attention_heads]   ,						2		)

						return (
						    config,
						    input_ids,
						    input_mask,
						    head_mask,
						)

				def __lowercase       (       self   :  Tuple		)       ->  Union[str, Any]:
						return XGLMConfig(
						    vocab_size=self.vocab_size   ,						d_model=self.hidden_size   ,						num_layers=self.num_hidden_layers   ,						attention_heads=self.num_attention_heads   ,						ffn_dim=self.ffn_dim   ,						activation_function=self.activation_function   ,						activation_dropout=self.activation_dropout   ,						attention_dropout=self.attention_dropout   ,						max_position_embeddings=self.max_position_embeddings   ,						initializer_range=self.initializer_range   ,						use_cache=snake_case__   ,						bos_token_id=self.bos_token_id   ,						eos_token_id=self.eos_token_id   ,						pad_token_id=self.pad_token_id   ,						return_dict=snake_case__   ,						)





				def __lowercase       (       self   :  List[str]		)       ->  Optional[int]:
						lowerCAmelCase_      :				Optional[int]      =       self.prepare_config_and_inputs()

						(
						    lowerCAmelCase_ 
						)      :				str      =       config_and_inputs

						lowerCAmelCase_      :				int      =       {
						    "input_ids": input_ids,
						    "head_mask": head_mask,
						}

						return config, inputs_dict







@require_tf
class        __snake_case	(       A_     ,A_     ,unittest.TestCase):







				"""simple docstring"""



				lowercase       			= (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
				lowercase       			= (TFXGLMForCausalLM,) if is_tf_available() else ()
				lowercase       			= (
				    {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
				)
				lowercase       			= False
				lowercase       			= False
				lowercase       			= False

				def __lowercase       (       self   :  Union[str, Any]		)       ->  Dict:
						lowerCAmelCase_      :				int      =       TFXGLMModelTester(self		)
						lowerCAmelCase_      :				List[str]      =       ConfigTester(self   ,						config_class=snake_case__   ,						n_embd=37		)

				def __lowercase       (       self   :  str		)       ->  Any:
						self.config_tester.run_common_tests()

				@slow
				def __lowercase       (       self   :  Union[str, Any]		)       ->  Tuple:


						for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								lowerCAmelCase_      :				List[str]      =       TFXGLMModel.from_pretrained(snake_case__		)
								self.assertIsNotNone(snake_case__		)





				@unittest.skip(reason="""Currently, model embeddings are going to undergo a major refactor."""		)
				def __lowercase       (       self   :  Tuple		)       ->  Union[str, Any]:
						super().test_resize_token_embeddings()







@require_tf
class        __snake_case	(       unittest.TestCase):







				"""simple docstring"""



				@slow
				def __lowercase       (       self   :  Tuple   ,						lowerCamelCase   :  str=True		)       ->  int:
						lowerCAmelCase_      :				Any      =       TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""		)
						lowerCAmelCase_      :				List[str]      =       tf.convert_to_tensor([[2, 2_68, 98_65]]   ,						dtype=tf.intaa		)  # The dog
						# </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
						# fmt: off
						lowerCAmelCase_      :				str      =       [2, 2_68, 98_65, 67, 11, 19_88, 5_72_52, 98_65, 5, 9_84, 67, 19_88, 21_38_38, 16_58, 53, 7_04_46, 33, 66_57, 2_78, 15_81]
						# fmt: on
						lowerCAmelCase_      :				str      =       model.generate(snake_case__   ,						do_sample=snake_case__   ,						num_beams=1		)
						if verify_outputs:
								self.assertListEqual(output_ids[0].numpy().tolist()   ,						snake_case__		)

				@slow
				def __lowercase       (       self   :  Union[str, Any]		)       ->  Dict:
						lowerCAmelCase_      :				str      =       XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""		)
						lowerCAmelCase_      :				int      =       TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""		)

						tf.random.set_seed(0		)
						lowerCAmelCase_      :				int      =       tokenizer("""Today is a nice day and"""   ,						return_tensors="""tf"""		)
						lowerCAmelCase_      :				int      =       tokenized.input_ids
						# forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
						with tf.device(""":/CPU:0"""		):
								lowerCAmelCase_      :				Union[str, Any]      =       model.generate(snake_case__   ,						do_sample=snake_case__   ,						seed=[7, 0]		)
						lowerCAmelCase_      :				Union[str, Any]      =       tokenizer.decode(output_ids[0]   ,						skip_special_tokens=snake_case__		)

						lowerCAmelCase_      :				str      =       (
						    "Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due"
						)
						self.assertEqual(snake_case__   ,						snake_case__		)





				@slow
				def __lowercase       (       self   :  Union[str, Any]		)       ->  List[str]:
						lowerCAmelCase_      :				Optional[int]      =       TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""		)
						lowerCAmelCase_      :				str      =       XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""		)

						lowerCAmelCase_      :				Any      =       "left"

						# use different length sentences to test batching
						lowerCAmelCase_      :				Union[str, Any]      =       [
						    "This is an extremelly long sentence that only exists to test the ability of the model to cope with "
						    "left-padding, such as in batched generation. The output for the sequence below should be the same "
						    "regardless of whether left padding is applied or not. When",
						    "Hello, my dog is a little",
						]

						lowerCAmelCase_      :				Tuple      =       tokenizer(snake_case__   ,						return_tensors="""tf"""   ,						padding=snake_case__		)
						lowerCAmelCase_      :				Any      =       inputs["input_ids"]

						lowerCAmelCase_      :				str      =       model.generate(input_ids=snake_case__   ,						attention_mask=inputs["""attention_mask"""]   ,						max_new_tokens=12		)

						lowerCAmelCase_      :				str      =       tokenizer(sentences[0]   ,						return_tensors="""tf"""		).input_ids
						lowerCAmelCase_      :				str      =       model.generate(input_ids=snake_case__   ,						max_new_tokens=12		)

						lowerCAmelCase_      :				Dict      =       tokenizer(sentences[1]   ,						return_tensors="""tf"""		).input_ids
						lowerCAmelCase_      :				List[Any]      =       model.generate(input_ids=snake_case__   ,						max_new_tokens=12		)

						lowerCAmelCase_      :				List[str]      =       tokenizer.batch_decode(snake_case__   ,						skip_special_tokens=snake_case__		)
						lowerCAmelCase_      :				Any      =       tokenizer.decode(output_non_padded[0]   ,						skip_special_tokens=snake_case__		)
						lowerCAmelCase_      :				Optional[int]      =       tokenizer.decode(output_padded[0]   ,						skip_special_tokens=snake_case__		)

						lowerCAmelCase_      :				Union[str, Any]      =       [
						    "This is an extremelly long sentence that only exists to test the ability of the model to cope with "
						    "left-padding, such as in batched generation. The output for the sequence below should be the same "
						    "regardless of whether left padding is applied or not. When left padding is applied, the sequence will be "
						    "a single",
						    "Hello, my dog is a little bit of a shy one, but he is very friendly",
						]
						self.assertListEqual(snake_case__   ,						snake_case__		)
						self.assertListEqual(snake_case__   ,						[non_padded_sentence, padded_sentence]		)







'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


__A :  List[str]    							=				{
    "configuration_owlvit": [
        "OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "OwlViTConfig",
        "OwlViTOnnxConfig",
        "OwlViTTextConfig",
        "OwlViTVisionConfig",
    ],
    "processing_owlvit": ["OwlViTProcessor"],
}


try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __A :  Optional[int]    							=				["OwlViTFeatureExtractor"]
  __A :  str    							=				["OwlViTImageProcessor"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __A :  Tuple    							=				[
      "OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "OwlViTModel",
      "OwlViTPreTrainedModel",
      "OwlViTTextModel",
      "OwlViTVisionModel",
      "OwlViTForObjectDetection",
  ]

if TYPE_CHECKING:
  from .configuration_owlvit import (
      OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
      OwlViTConfig,
      OwlViTOnnxConfig,
      OwlViTTextConfig,
      OwlViTVisionConfig,
  )
  from .processing_owlvit import OwlViTProcessor

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .feature_extraction_owlvit import OwlViTFeatureExtractor
    from .image_processing_owlvit import OwlViTImageProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_owlvit import (
        OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
        OwlViTForObjectDetection,
        OwlViTModel,
        OwlViTPreTrainedModel,
        OwlViTTextModel,
        OwlViTVisionModel,
    )

else:
  import sys

  __A :  List[Any]    							=				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



