code: 
  
  
  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 tensorflow as tf
  
     from transformers import (
         TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
         TFXLMForMultipleChoice,
         TFXLMForQuestionAnsweringSimple,
         TFXLMForSequenceClassification,
         TFXLMForTokenClassification,
         TFXLMModel,
         TFXLMWithLMHeadModel,
         XLMConfig,
     )
  
  class 						UpperCAmelCase__   :
  
  
  
       '''simple docstring'''
  
  
  
  
       def __init__( self       :       Optional[int]	,      UpperCamelCase       :       str	,      ):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Union[str, Any]           =			parent
              _lowercase					:				Tuple           =			13
              _lowercase					:				List[Any]           =			7
              _lowercase					:				Union[str, Any]           =			True
              _lowercase					:				List[Any]           =			True
              _lowercase					:				Any           =			True
              _lowercase					:				List[str]           =			True
              _lowercase					:				Dict           =			True
              _lowercase					:				List[Any]           =			False
              _lowercase					:				Union[str, Any]           =			False
              _lowercase					:				Optional[int]           =			False
              _lowercase					:				Optional[int]           =			2
              _lowercase					:				Any           =			99
              _lowercase					:				Optional[int]           =			0
              _lowercase					:				str           =			32
              _lowercase					:				Dict           =			2
              _lowercase					:				Optional[int]           =			4
              _lowercase					:				Any           =			0.1
              _lowercase					:				List[str]           =			0.1
              _lowercase					:				Tuple           =			5_12
              _lowercase					:				Optional[Any]           =			16
              _lowercase					:				Union[str, Any]           =			2
              _lowercase					:				Dict           =			0.02
              _lowercase					:				Any           =			3
              _lowercase					:				List[Any]           =			4
              _lowercase					:				str           =			'''last'''
              _lowercase					:				Optional[Any]           =			True
              _lowercase					:				List[str]           =			None
              _lowercase					:				int           =			0
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Tuple		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Optional[Any]           =			ids_tensor([self.batch_size, self.seq_length]	,      self.vocab_size		)
              _lowercase					:				Optional[int]           =			random_attention_mask([self.batch_size, self.seq_length]	,      dtype=tf.floataa		)
  
              _lowercase					:				Tuple           =			None
              if self.use_input_lengths:
                     _lowercase					:				Tuple           =			(
                         ids_tensor([self.batch_size]	,      vocab_size=2		) + self.seq_length - 2
                     )  # small variation of seq_length
  
              _lowercase					:				List[str]           =			None
              if self.use_token_type_ids:
                     _lowercase					:				Any           =			ids_tensor([self.batch_size, self.seq_length]	,      self.n_langs		)
  
              _lowercase					:				Tuple           =			None
              _lowercase					:				Union[str, Any]           =			None
              _lowercase					:				str           =			None
              if self.use_labels:
                     _lowercase					:				List[Any]           =			ids_tensor([self.batch_size]	,      self.type_sequence_label_size		)
                     _lowercase					:				Optional[Any]           =			ids_tensor([self.batch_size, self.seq_length]	,      self.num_labels		)
                     _lowercase					:				List[Any]           =			ids_tensor([self.batch_size]	,      2	,      dtype=tf.floataa		)
                     _lowercase					:				int           =			ids_tensor([self.batch_size]	,      self.num_choices		)
  
              _lowercase					:				int           =			XLMConfig(
                  vocab_size=self.vocab_size	,      n_special=self.n_special	,      emb_dim=self.hidden_size	,      n_layers=self.num_hidden_layers	,      n_heads=self.num_attention_heads	,      dropout=self.hidden_dropout_prob	,      attention_dropout=self.attention_probs_dropout_prob	,      gelu_activation=self.gelu_activation	,      sinusoidal_embeddings=self.sinusoidal_embeddings	,      asm=self.asm	,      causal=self.causal	,      n_langs=self.n_langs	,      max_position_embeddings=self.max_position_embeddings	,      initializer_range=self.initializer_range	,      summary_type=self.summary_type	,      use_proj=self.use_proj	,      bos_token_id=self.bos_token_id	,      )
  
              return (
                  config,
                  input_ids,
                  token_type_ids,
                  input_lengths,
                  sequence_labels,
                  token_labels,
                  is_impossible_labels,
                  choice_labels,
                  input_mask,
              )
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Dict	,      UpperCamelCase       :       Union[str, Any]	,      UpperCamelCase       :       Tuple	,      UpperCamelCase       :       Dict	,      UpperCamelCase       :       Optional[Any]	,      UpperCamelCase       :       Any	,      UpperCamelCase       :       Optional[int]	,      UpperCamelCase       :       List[Any]	,      UpperCamelCase       :       Optional[Any]	,      UpperCamelCase       :       Optional[Any]	,      ):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Any           =			TFXLMModel(config=UpperCamelCase		)
              _lowercase					:				List[Any]           =			{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
              _lowercase					:				List[str]           =			model(UpperCamelCase		)
  
              _lowercase					:				Union[str, Any]           =			[input_ids, input_mask]
              _lowercase					:				str           =			model(UpperCamelCase		)
              self.parent.assertEqual(result.last_hidden_state.shape	,      (self.batch_size, self.seq_length, self.hidden_size)		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Any	,      UpperCamelCase       :       Optional[Any]	,      UpperCamelCase       :       str	,      UpperCamelCase       :       Optional[int]	,      UpperCamelCase       :       Dict	,      UpperCamelCase       :       List[str]	,      UpperCamelCase       :       Tuple	,      UpperCamelCase       :       Tuple	,      UpperCamelCase       :       List[Any]	,      UpperCamelCase       :       Union[str, Any]	,      ):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				int           =			TFXLMWithLMHeadModel(UpperCamelCase		)
  
              _lowercase					:				Any           =			{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
              _lowercase					:				Tuple           =			model(UpperCamelCase		)
  
              _lowercase					:				Optional[int]           =			outputs
  
              self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.seq_length, self.vocab_size)		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Tuple	,      UpperCamelCase       :       Tuple	,      UpperCamelCase       :       Optional[int]	,      UpperCamelCase       :       Union[str, Any]	,      UpperCamelCase       :       int	,      UpperCamelCase       :       Union[str, Any]	,      UpperCamelCase       :       Optional[Any]	,      UpperCamelCase       :       Any	,      UpperCamelCase       :       Any	,      UpperCamelCase       :       Any	,      ):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Optional[Any]           =			TFXLMForQuestionAnsweringSimple(UpperCamelCase		)
  
              _lowercase					:				Optional[Any]           =			{'''input_ids''': input_ids, '''lengths''': input_lengths}
  
              _lowercase					:				Optional[int]           =			model(UpperCamelCase		)
  
              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   lowerCAmelCase_   ( self       :       Optional[Any]	,      UpperCamelCase       :       Optional[int]	,      UpperCamelCase       :       Optional[Any]	,      UpperCamelCase       :       Dict	,      UpperCamelCase       :       str	,      UpperCamelCase       :       Any	,      UpperCamelCase       :       Tuple	,      UpperCamelCase       :       Optional[int]	,      UpperCamelCase       :       Any	,      UpperCamelCase       :       Optional[int]	,      ):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Dict           =			TFXLMForSequenceClassification(UpperCamelCase		)
  
              _lowercase					:				Tuple           =			{'''input_ids''': input_ids, '''lengths''': input_lengths}
  
              _lowercase					:				str           =			model(UpperCamelCase		)
  
              self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.type_sequence_label_size)		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[str]	,      UpperCamelCase       :       List[str]	,      UpperCamelCase       :       Optional[int]	,      UpperCamelCase       :       int	,      UpperCamelCase       :       str	,      UpperCamelCase       :       Union[str, Any]	,      UpperCamelCase       :       Union[str, Any]	,      UpperCamelCase       :       Any	,      UpperCamelCase       :       List[str]	,      UpperCamelCase       :       List[str]	,      ):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Tuple           =			self.num_labels
              _lowercase					:				Optional[Any]           =			TFXLMForTokenClassification(config=UpperCamelCase		)
              _lowercase					:				Optional[Any]           =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
              _lowercase					:				Any           =			model(UpperCamelCase		)
              self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.seq_length, self.num_labels)		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[int]	,      UpperCamelCase       :       List[Any]	,      UpperCamelCase       :       List[Any]	,      UpperCamelCase       :       Optional[Any]	,      UpperCamelCase       :       str	,      UpperCamelCase       :       Optional[int]	,      UpperCamelCase       :       int	,      UpperCamelCase       :       Tuple	,      UpperCamelCase       :       Optional[Any]	,      UpperCamelCase       :       List[str]	,      ):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				List[Any]           =			self.num_choices
              _lowercase					:				Union[str, Any]           =			TFXLMForMultipleChoice(config=UpperCamelCase		)
              _lowercase					:				Union[str, Any]           =			tf.tile(tf.expand_dims(UpperCamelCase	,      1		)	,      (1, self.num_choices, 1)		)
              _lowercase					:				Tuple           =			tf.tile(tf.expand_dims(UpperCamelCase	,      1		)	,      (1, self.num_choices, 1)		)
              _lowercase					:				List[Any]           =			tf.tile(tf.expand_dims(UpperCamelCase	,      1		)	,      (1, self.num_choices, 1)		)
              _lowercase					:				Tuple           =			{
                  '''input_ids''': multiple_choice_inputs_ids,
                  '''attention_mask''': multiple_choice_input_mask,
                  '''token_type_ids''': multiple_choice_token_type_ids,
              }
              _lowercase					:				Dict           =			model(UpperCamelCase		)
              self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.num_choices)		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[int]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Union[str, Any]           =			self.prepare_config_and_inputs()
              (
                  (
                  _lowercase
              )       ,	(
                  _lowercase
              )       ,	(
                  _lowercase
              )       ,	(
                  _lowercase
              )       ,	(
                  _lowercase
              )       ,	(
                  _lowercase
              )       ,	(
                  _lowercase
              )       ,	(
                  _lowercase
              )       ,	(
                  _lowercase
              )       ,	
              )					:				Dict           =			config_and_inputs
              _lowercase					:				int           =			{
                  '''input_ids''': input_ids,
                  '''token_type_ids''': token_type_ids,
                  '''langs''': token_type_ids,
                  '''lengths''': input_lengths,
              }
              return config, inputs_dict
  
  
  
  
  @require_tf
  class 						UpperCAmelCase__   (      A_      ,						A_      ,						unittest.TestCase		):
  
  
  
       '''simple docstring'''
       UpperCAmelCase_         =  (
           (
               TFXLMModel,
               TFXLMWithLMHeadModel,
               TFXLMForSequenceClassification,
               TFXLMForQuestionAnsweringSimple,
               TFXLMForTokenClassification,
               TFXLMForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       UpperCAmelCase_         =  (
           (TFXLMWithLMHeadModel,) if is_tf_available() else ()
       )  # TODO (PVP): Check other models whether language generation is also applicable
       UpperCAmelCase_         =  (
           {
               '''feature-extraction''': TFXLMModel,
               '''fill-mask''': TFXLMWithLMHeadModel,
               '''question-answering''': TFXLMForQuestionAnsweringSimple,
               '''text-classification''': TFXLMForSequenceClassification,
               '''text-generation''': TFXLMWithLMHeadModel,
               '''token-classification''': TFXLMForTokenClassification,
               '''zero-shot''': TFXLMForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       UpperCAmelCase_         =  False
       UpperCAmelCase_         =  False
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[Any]	,      UpperCamelCase       :       List[Any]	,      UpperCamelCase       :       Tuple	,      UpperCamelCase       :       Optional[Any]	,      UpperCamelCase       :       List[Any]	,      UpperCamelCase       :       Optional[int]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              if (
                  pipeline_test_casse_name == "QAPipelineTests"
                  and tokenizer_name is not None
                  and not tokenizer_name.endswith('''Fast'''		)
              ):
                     # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
                     # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
                     # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
                     return True
  
              return False
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[str]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				List[Any]           =			TFXLMModelTester(self		)
              _lowercase					:				Dict           =			ConfigTester(self	,      config_class=UpperCamelCase	,      emb_dim=37		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       int		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              self.config_tester.run_common_tests()
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Dict		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				str           =			self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_xlm_model(*UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Dict		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Tuple           =			self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_xlm_lm_head(*UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[int]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Tuple           =			self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_xlm_qa(*UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       int		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Any           =			self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_xlm_sequence_classif(*UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[str]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				List[str]           =			self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_xlm_for_token_classification(*UpperCamelCase		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Tuple		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				str           =			self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_xlm_for_multiple_choice(*UpperCamelCase		)
  
  
  
  
       @slow
       def   lowerCAmelCase_   ( self       :       str		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     _lowercase					:				Optional[Any]           =			TFXLMModel.from_pretrained(UpperCamelCase		)
                     self.assertIsNotNone(UpperCamelCase		)
  
  
  
  
  @require_tf
  class 						UpperCAmelCase__   (      unittest.TestCase		):
  
  
  
       '''simple docstring'''
  
  
  
  
       @slow
       def   lowerCAmelCase_   ( self       :       List[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Dict           =			TFXLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048'''		)
              _lowercase					:				List[str]           =			tf.convert_to_tensor([[14, 4_47]]	,      dtype=tf.intaa		)  # the president
              _lowercase					:				Optional[int]           =			[
                  14,
                  4_47,
                  14,
                  4_47,
                  14,
                  4_47,
                  14,
                  4_47,
                  14,
                  4_47,
                  14,
                  4_47,
                  14,
                  4_47,
                  14,
                  4_47,
                  14,
                  4_47,
                  14,
                  4_47,
              ]  # the president the president the president the president the president the president the president the president the president the president
              # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
              _lowercase					:				Union[str, Any]           =			model.generate(UpperCamelCase	,      do_sample=UpperCamelCase		)
              self.assertListEqual(output_ids[0].numpy().tolist()	,      UpperCamelCase		)
style_context: 
  
  
  # limitations under the License.
  
  
  from typing import Optional, Tuple, Union
  
  import torch
  
  from diffusers.pipeline_utils import DiffusionPipeline, ImagePipelineOutput
  
  class 						UpperCAmelCase__   (      A_		):
  
  
  
       '''simple docstring'''
  
  
  
  
       def __init__( self       :       List[str]	,      UpperCamelCase       :       Union[str, Any]	,      UpperCamelCase       :       int		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              super().__init__()
              self.register_modules(unet=UpperCamelCase	,      scheduler=UpperCamelCase		)
  
  
  
  
       @torch.no_grad()
       def __call__( self       :       Any	,      UpperCamelCase       :       int = 1	,      UpperCamelCase       :       Optional[torch.Generator] = None	,      UpperCamelCase       :       int = 50	,      UpperCamelCase       :       Optional[str] = "pil"	,      UpperCamelCase       :       bool = True	,      **UpperCamelCase       :       int	,      ):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Optional[int]           =			torch.randn(
                  (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size)	,      generator=UpperCamelCase	,      )
              _lowercase					:				Dict           =			image.to(self.device		)
  
              # set step values
              self.scheduler.set_timesteps(UpperCamelCase		)
  
              for t in self.progress_bar(self.scheduler.timesteps		):
                     # 1. predict noise model_output
                     _lowercase					:				Dict           =			self.unet(UpperCamelCase	,      UpperCamelCase		).sample
  
                     # 2. predict previous mean of image x_t-1 and add variance depending on eta
                     # eta corresponds to η in paper and should be between [0, 1]
                     # do x_t -> x_t-1
                     _lowercase					:				Tuple           =			self.scheduler.step(UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase		).prev_sample
  
              _lowercase					:				Optional[int]           =			(image / 2 + 0.5).clamp(0	,      1		)
              _lowercase					:				int           =			image.cpu().permute(0	,      2	,      3	,      1		).numpy()
              if output_type == "pil":
                     _lowercase					:				List[Any]           =			self.numpy_to_pil(UpperCamelCase		)
  
              if not return_dict:
                     return (image,), "This is a local test"
  
              return ImagePipelineOutput(images=UpperCamelCase		), "This is a local test"