'''simple docstring'''




from __future__ import annotations

import unittest

from transformers import AutoTokenizer, GPTJConfig, is_tf_available
from transformers.testing_utils import require_tf, slow, tooslow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin
from ...utils.test_modeling_tf_core import TFCoreModelTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers.models.gptj.modeling_tf_gptj import (
     TFGPTJForCausalLM,
     TFGPTJForQuestionAnswering,
     TFGPTJForSequenceClassification,
     TFGPTJModel,
     shape_list,
 )


class    UpperCAmelCase__	:






   """simple docstring"""
   def __init__(self							,					_a					)     ->  Optional[int]:
        lowercase_       :							List[Any]  					=       parent
        lowercase_       :							List[str]  					=       13
        lowercase_       :							str  					=       7
        lowercase_       :							int  					=       True
        lowercase_       :							Any  					=       True
        lowercase_       :							int  					=       True
        lowercase_       :							str  					=       True
        lowercase_       :							Tuple  					=       True
        lowercase_       :							str  					=       99
        lowercase_       :							Tuple  					=       32
        lowercase_       :							int  					=       4
        lowercase_       :							Optional[int]  					=       2
        lowercase_       :							Optional[Any]  					=       4
        lowercase_       :							Any  					=       37
        lowercase_       :							List[str]  					=       'gelu'
        lowercase_       :							str  					=       0.1
        lowercase_       :							str  					=       0.1
        lowercase_       :							Any  					=       512
        lowercase_       :							Union[str, Any]  					=       16
        lowercase_       :							Any  					=       2
        lowercase_       :							Optional[Any]  					=       0.02
        lowercase_       :							List[Any]  					=       3
        lowercase_       :							Optional[int]  					=       4
        lowercase_       :							List[Any]  					=       None
        lowercase_       :							List[str]  					=       self.vocab_size - 1
        lowercase_       :							Optional[Any]  					=       self.vocab_size - 1
        lowercase_       :							Any  					=       self.vocab_size - 1
   def 		_lowerCamelCase			(self					)     ->  Optional[int]:
        lowercase_       :							Any  					=       ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size					)

        lowercase_       :							Tuple  					=       None
        if self.use_input_mask:
             lowercase_       :							int  					=       random_attention_mask([self.batch_size, self.seq_length]					)

        lowercase_       :							List[str]  					=       None
        if self.use_token_type_ids:
             lowercase_       :							Any  					=       ids_tensor([self.batch_size, self.seq_length]							,					self.type_vocab_size					)

        lowercase_       :							Union[str, Any]  					=       None
        if self.use_mc_token_ids:
             lowercase_       :							Union[str, Any]  					=       ids_tensor([self.batch_size, self.num_choices]							,					self.seq_length					)

        lowercase_       :							Tuple  					=       None
        lowercase_       :							Optional[Any]  					=       None
        lowercase_       :							Optional[int]  					=       None
        if self.use_labels:
             lowercase_       :							Optional[Any]  					=       ids_tensor([self.batch_size]							,					self.type_sequence_label_size					)
             lowercase_       :							Optional[int]  					=       ids_tensor([self.batch_size, self.seq_length]							,					self.num_labels					)
             lowercase_       :							List[Any]  					=       ids_tensor([self.batch_size]							,					self.num_choices					)

        lowercase_       :							Tuple  					=       GPTJConfig(
            vocab_size=self.vocab_size							,					n_embd=self.hidden_size							,					n_layer=self.num_hidden_layers							,					n_head=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							,					n_positions=self.max_position_embeddings							,					type_vocab_size=self.type_vocab_size							,					initializer_range=self.initializer_range							,					bos_token_id=self.bos_token_id							,					eos_token_id=self.eos_token_id							,					pad_token_id=self.pad_token_id							,					rotary_dim=self.rotary_dim							,					return_dict=__UpperCamelCase							,					)

        lowercase_       :							int  					=       ids_tensor([self.num_hidden_layers, self.num_attention_heads]							,					2					)

        return (
            config,
            input_ids,
            input_mask,
            head_mask,
            token_type_ids,
            mc_token_ids,
            sequence_labels,
            token_labels,
            choice_labels,
        )
   def 		_lowerCamelCase			(self							,					_a							,					_a							,					_a							,					_a							,					_a							,					*_a					)     ->  Optional[Any]:
        lowercase_       :							Optional[Any]  					=       TFGPTJModel(config=__UpperCamelCase					)
        lowercase_       :							Tuple  					=       {
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }
        lowercase_       :							List[str]  					=       model(__UpperCamelCase					)

        lowercase_       :							Optional[int]  					=       [input_ids, None, input_mask]  # None is the input for 'past'
        lowercase_       :							Any  					=       model(__UpperCamelCase					)

        lowercase_       :							Optional[int]  					=       model(__UpperCamelCase					)

        self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size)					)
   def 		_lowerCamelCase			(self							,					_a							,					_a							,					_a							,					_a							,					_a							,					*_a					)     ->  List[str]:
        lowercase_       :							Optional[int]  					=       TFGPTJModel(config=__UpperCamelCase					)

        # first forward pass
        lowercase_       :							List[Any]  					=       model(__UpperCamelCase							,					token_type_ids=__UpperCamelCase							,					use_cache=__UpperCamelCase					)
        lowercase_       :							Optional[Any]  					=       model(__UpperCamelCase							,					token_type_ids=__UpperCamelCase					)
        lowercase_       :							Optional[Any]  					=       model(__UpperCamelCase							,					token_type_ids=__UpperCamelCase							,					use_cache=__UpperCamelCase					)

        self.parent.assertTrue(len(__UpperCamelCase					) == len(__UpperCamelCase					)					)
        self.parent.assertTrue(len(__UpperCamelCase					) == len(__UpperCamelCase					) + 1					)

        lowercase_					,lowercase_       :							Union[str, Any]  					=       outputs.to_tuple()

        # create hypothetical next token and extent to next_input_ids
        lowercase_       :							Optional[int]  					=       ids_tensor((self.batch_size, 1)							,					config.vocab_size					)
        lowercase_       :							str  					=       ids_tensor([self.batch_size, 1]							,					self.type_vocab_size					)

        # append to next input_ids and token_type_ids
        lowercase_       :							List[str]  					=       tf.concat([input_ids, next_tokens]							,					axis=-1					)
        lowercase_       :							Dict  					=       tf.concat([token_type_ids, next_token_types]							,					axis=-1					)

        lowercase_       :							Optional[int]  					=       model(__UpperCamelCase							,					token_type_ids=__UpperCamelCase					)['last_hidden_state']
        lowercase_       :							Any  					=       model(__UpperCamelCase							,					token_type_ids=__UpperCamelCase							,					past_key_values=__UpperCamelCase					)[
            'last_hidden_state'
        ]

        # select random slice
        lowercase_       :							str  					=       int(ids_tensor((1,)							,					shape_list(__UpperCamelCase					)[-1]					)					)
        lowercase_       :							List[Any]  					=       output_from_no_past[:, -1, random_slice_idx]
        lowercase_       :							Optional[Any]  					=       output_from_past[:, 0, random_slice_idx]

        # test that outputs are equal for slice
        tf.debugging.assert_near(__UpperCamelCase							,					__UpperCamelCase							,					rtol=1e-6					)
   def 		_lowerCamelCase			(self							,					_a							,					_a							,					_a							,					_a							,					_a							,					*_a					)     ->  Tuple:
        lowercase_       :							Tuple  					=       TFGPTJModel(config=__UpperCamelCase					)

        # create attention mask
        lowercase_       :							Union[str, Any]  					=       self.seq_length // 2
        lowercase_       :							Any  					=       tf.ones((self.batch_size, half_seq_length)							,					dtype=tf.intaa					)
        lowercase_       :							Any  					=       tf.zeros((self.batch_size, self.seq_length - half_seq_length)							,					dtype=tf.intaa					)
        lowercase_       :							Optional[int]  					=       tf.concat([attn_mask_begin, attn_mask_end]							,					axis=1					)

        # first forward pass
        lowercase_					,lowercase_       :							Tuple  					=       model(__UpperCamelCase							,					attention_mask=__UpperCamelCase					).to_tuple()

        # create hypothetical next token and extent to next_input_ids
        lowercase_       :							int  					=       ids_tensor((self.batch_size, 1)							,					config.vocab_size					)

        # change a random masked slice from input_ids
        lowercase_       :							Dict  					=       ids_tensor((1,)							,					__UpperCamelCase					).numpy() + 1
        lowercase_       :							Union[str, Any]  					=       ids_tensor((self.batch_size, self.seq_length)							,					config.vocab_size					)
        lowercase_       :							List[str]  					=       tf.range(self.seq_length					) == (self.seq_length - random_seq_idx_to_change)
        lowercase_       :							List[str]  					=       tf.transpose(
            tf.broadcast_to(tf.expand_dims(__UpperCamelCase							,					-1					)							,					(self.seq_length, self.batch_size)					)					)
        lowercase_       :							str  					=       tf.where(__UpperCamelCase							,					__UpperCamelCase							,					__UpperCamelCase					)

        # append to next input_ids and attn_mask
        lowercase_       :							Tuple  					=       tf.concat([input_ids, next_tokens]							,					axis=-1					)
        lowercase_       :							Union[str, Any]  					=       tf.concat([attn_mask, tf.ones((shape_list(__UpperCamelCase					)[0], 1)							,					dtype=tf.intaa					)]							,					axis=1					)

        # get two different outputs
        lowercase_       :							Dict  					=       model(__UpperCamelCase							,					attention_mask=__UpperCamelCase					)['last_hidden_state']
        lowercase_       :							List[str]  					=       model(__UpperCamelCase							,					past_key_values=__UpperCamelCase							,					attention_mask=__UpperCamelCase					)[
            'last_hidden_state'
        ]

        # select random slice
        lowercase_       :							str  					=       int(ids_tensor((1,)							,					shape_list(__UpperCamelCase					)[-1]					)					)
        lowercase_       :							List[str]  					=       output_from_no_past[:, -1, random_slice_idx]
        lowercase_       :							int  					=       output_from_past[:, 0, random_slice_idx]

        # test that outputs are equal for slice
        tf.debugging.assert_near(__UpperCamelCase							,					__UpperCamelCase							,					rtol=1e-12					)
   def 		_lowerCamelCase			(self							,					_a							,					_a							,					_a							,					_a							,					_a							,					*_a					)     ->  Any:
        lowercase_       :							Dict  					=       TFGPTJModel(config=__UpperCamelCase					)

        lowercase_       :							Optional[int]  					=       input_ids[:1, :]
        lowercase_       :							str  					=       input_mask[:1, :]
        lowercase_       :							Tuple  					=       token_type_ids[:1, :]
        lowercase_       :							int  					=       1

        # first forward pass
        lowercase_       :							int  					=       model(__UpperCamelCase							,					attention_mask=__UpperCamelCase							,					token_type_ids=__UpperCamelCase							,					use_cache=__UpperCamelCase					)

        lowercase_					,lowercase_       :							str  					=       outputs.to_tuple()

        # create hypothetical next token and extent to next_input_ids
        lowercase_       :							List[Any]  					=       ids_tensor((self.batch_size, 3)							,					config.vocab_size					)
        lowercase_       :							int  					=       ids_tensor((self.batch_size, 3)							,					2					)
        lowercase_       :							int  					=       ids_tensor((self.batch_size, 3)							,					self.type_vocab_size					)

        # append to next input_ids and token_type_ids
        lowercase_       :							Tuple  					=       tf.concat([input_ids, next_tokens]							,					axis=-1					)
        lowercase_       :							List[str]  					=       tf.concat([input_mask, next_attn_mask]							,					axis=-1					)
        lowercase_       :							List[str]  					=       tf.concat([token_type_ids, next_token_types]							,					axis=-1					)

        lowercase_       :							List[Any]  					=       model(
            __UpperCamelCase							,					token_type_ids=__UpperCamelCase							,					attention_mask=__UpperCamelCase					)['last_hidden_state']
        lowercase_       :							Dict  					=       model(
            __UpperCamelCase							,					token_type_ids=__UpperCamelCase							,					attention_mask=__UpperCamelCase							,					past_key_values=__UpperCamelCase							,					)['last_hidden_state']
        self.parent.assertTrue(output_from_past.shape[1] == next_tokens.shape[1]					)

        # select random slice
        lowercase_       :							Dict  					=       int(ids_tensor((1,)							,					shape_list(__UpperCamelCase					)[-1]					)					)
        lowercase_       :							str  					=       output_from_no_past[:, -3:, random_slice_idx]
        lowercase_       :							str  					=       output_from_past[:, :, random_slice_idx]

        # test that outputs are equal for slice
        tf.debugging.assert_near(__UpperCamelCase							,					__UpperCamelCase							,					rtol=1e-3					)
   def 		_lowerCamelCase			(self							,					_a							,					_a							,					_a							,					_a							,					_a							,					*_a					)     ->  Optional[int]:
        lowercase_       :							int  					=       TFGPTJForCausalLM(config=__UpperCamelCase					)
        lowercase_       :							Union[str, Any]  					=       {
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }
        lowercase_       :							Optional[Any]  					=       model(__UpperCamelCase					)
        self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.seq_length, self.vocab_size)					)





   def 		_lowerCamelCase			(self					)     ->  Dict:
        lowercase_       :							Optional[int]  					=       self.prepare_config_and_inputs()

        (
            (
            lowercase_      
        )					,(
            lowercase_      
        )					,(
            lowercase_      
        )					,(
            lowercase_      
        )					,(
            lowercase_      
        )					,(
            lowercase_      
        )					,(
            lowercase_      
        )					,(
            lowercase_      
        )					,(
            lowercase_      
        )					,      
        )       :							Any  					=       config_and_inputs

        lowercase_       :							Tuple  					=       {
            'input_ids': input_ids,
            'token_type_ids': token_type_ids,
            'attention_mask': input_mask,
        }
        return config, inputs_dict



@require_tf
class    UpperCAmelCase__	(							lowerCamelCase__       ,  lowerCamelCase__       ,  lowerCamelCase__       ,  unittest.TestCase      ):






   """simple docstring"""
   A				:							Tuple			     =     (
       (TFGPTJForCausalLM, TFGPTJForSequenceClassification, TFGPTJForQuestionAnswering, TFGPTJModel)
       if is_tf_available()
       else ()
   )

   A				:							str			     =     (TFGPTJForCausalLM,) if is_tf_available() else ()
   A				:							str			     =     (
       {
           '''feature-extraction''': TFGPTJModel,
           '''question-answering''': TFGPTJForQuestionAnswering,
           '''text-classification''': TFGPTJForSequenceClassification,
           '''text-generation''': TFGPTJForCausalLM,
           '''zero-shot''': TFGPTJForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   A				:							Optional[int]			     =     False
   A				:							Union[str, Any]			     =     False
   A				:							int			     =     False
   A				:							Dict			     =     False
   def 		_lowerCamelCase			(self							,					_a							,					_a							,					_a							,					_a							,					_a					)     ->  Any:
        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					)     ->  Dict:
        lowercase_       :							Optional[Any]  					=       TFGPTJModelTester(self					)
        lowercase_       :							List[Any]  					=       ConfigTester(self							,					config_class=__UpperCamelCase							,					n_embd=37					)
   def 		_lowerCamelCase			(self					)     ->  Any:
        self.config_tester.run_common_tests()
   def 		_lowerCamelCase			(self					)     ->  List[Any]:
        lowercase_       :							Optional[Any]  					=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_gptj_model(*__UpperCamelCase					)
   def 		_lowerCamelCase			(self					)     ->  List[str]:
        lowercase_       :							List[str]  					=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_gptj_model_past(*__UpperCamelCase					)
   def 		_lowerCamelCase			(self					)     ->  int:
        lowercase_       :							Optional[int]  					=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_gptj_model_attention_mask_past(*__UpperCamelCase					)
   def 		_lowerCamelCase			(self					)     ->  Optional[Any]:
        lowercase_       :							Tuple  					=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_gptj_model_past_large_inputs(*__UpperCamelCase					)
   def 		_lowerCamelCase			(self					)     ->  str:
        lowercase_       :							Dict  					=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_gptj_lm_head_model(*__UpperCamelCase					)
   @slow
   @unittest.skipIf(
       not is_tf_available() or len(tf.config.list_physical_devices('GPU'					)					) > 0							,					'skip testing on GPU for now to avoid GPU OOM.'							,					)
   def 		_lowerCamelCase			(self					)     ->  Union[str, Any]:
        lowercase_       :							Dict  					=       TFGPTJModel.from_pretrained('EleutherAI/gpt-j-6B'							,					from_pt=__UpperCamelCase					)
        self.assertIsNotNone(__UpperCamelCase					)





   @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.'					)
   def 		_lowerCamelCase			(self					)     ->  Optional[int]:
        super().test_resize_token_embeddings()



@require_tf
@tooslow
# Marked as @tooslow due to GPU OOM -- but still useful to run locally. Requires ~39GB of RAM.
class    UpperCAmelCase__	(							unittest.TestCase      ):






   """simple docstring"""
   def 		_lowerCamelCase			(self					)     ->  Optional[Any]:
        lowercase_       :							Optional[Any]  					=       TFGPTJForCausalLM.from_pretrained('EleutherAI/gpt-j-6B'							,					from_pt=__UpperCamelCase					)
        lowercase_       :							Dict  					=       tf.convert_to_tensor([[464, 3_290]]							,					dtype=tf.intaa					)  # The dog
        # fmt: off
        # The dog is a man's best friend. It is a loyal companion, and it is a friend
        lowercase_       :							Any  					=       [464, 3_290, 318, 257, 582, 338, 1_266, 1_545, 13, 632, 318, 257, 9_112, 15_185, 11, 290, 340, 318, 257, 1_545]
        # fmt: on
        lowercase_       :							Dict  					=       model.generate(__UpperCamelCase							,					do_sample=__UpperCamelCase					)
        self.assertListEqual(output_ids[0].numpy().tolist()							,					__UpperCamelCase					)
   def 		_lowerCamelCase			(self					)     ->  Any:
        lowercase_       :							Dict  					=       AutoTokenizer.from_pretrained('EleutherAI/gpt-j-6B'							,					revision='float16'					)
        lowercase_       :							int  					=       TFGPTJForCausalLM.from_pretrained('EleutherAI/gpt-j-6B'							,					revision='float16'							,					from_pt=__UpperCamelCase					)

        lowercase_       :							Dict  					=       tokenizer('Today is a nice day and'							,					return_tensors='tf'					)
        # forces the generation to happen on CPU, to avoid GPU-related quirks
        with tf.device(':/CPU:0'					):
             lowercase_       :							Optional[Any]  					=       model.generate(**__UpperCamelCase							,					do_sample=__UpperCamelCase							,					seed=[42, 0]					)
        lowercase_       :							Any  					=       tokenizer.decode(output_ids[0]							,					skip_special_tokens=__UpperCamelCase					)

        lowercase_       :							int  					=       'Today is a nice day and I’m going to go for a walk. I’'
        self.assertEqual(__UpperCamelCase							,					__UpperCamelCase					)
   def 		_lowerCamelCase			(self					)     ->  str:
        lowercase_       :							List[Any]  					=       TFGPTJForCausalLM.from_pretrained('EleutherAI/gpt-j-6B'							,					revision='float16'							,					from_pt=__UpperCamelCase					)
        lowercase_       :							int  					=       AutoTokenizer.from_pretrained('EleutherAI/gpt-j-6B'							,					revision='float16'					)

        lowercase_       :							Optional[Any]  					=       'left'

        # Define PAD Token = EOS Token = 50256
        lowercase_       :							Optional[Any]  					=       tokenizer.eos_token
        lowercase_       :							List[str]  					=       model.config.eos_token_id

        # use different length sentences to test batching
        lowercase_       :							str  					=       [
            'Hello, my dog is a little',
            'Today, I',
        ]
        lowercase_       :							List[str]  					=       [
            'Hello, my dog is a little over a year old and has been diagnosed with hip dysplasia',
            'Today, I’m going to be talking about a topic that’',
        ]
        return model, tokenizer, sentences, expected_output_sentences
   def 		_lowerCamelCase			(self					)     ->  List[Any]:
        # Confirms that we get the expected results with left-padded beam search
        lowercase_					,lowercase_					,lowercase_					,lowercase_       :							List[str]  					=       self._get_beam_search_test_objects()

        lowercase_       :							Any  					=       tokenizer(__UpperCamelCase							,					return_tensors='tf'							,					padding=__UpperCamelCase					)
        lowercase_       :							Optional[int]  					=       model.generate(**__UpperCamelCase							,					do_sample=__UpperCamelCase							,					num_beams=2					)
        lowercase_       :							Any  					=       tokenizer.batch_decode(__UpperCamelCase							,					skip_special_tokens=__UpperCamelCase					)
        self.assertListEqual(__UpperCamelCase							,					__UpperCamelCase					)
   def 		_lowerCamelCase			(self					)     ->  List[str]:
        # Confirms that left-padding is working properly
        lowercase_					,lowercase_					,lowercase_					,lowercase_       :							Union[str, Any]  					=       self._get_beam_search_test_objects()

        lowercase_       :							int  					=       tokenizer(__UpperCamelCase							,					return_tensors='tf'							,					padding=__UpperCamelCase					)
        lowercase_       :							List[str]  					=       tokenizer(sentences[0]							,					return_tensors='tf'					)
        lowercase_       :							Dict  					=       model.generate(**__UpperCamelCase							,					do_sample=__UpperCamelCase							,					num_beams=2					)
        lowercase_       :							Dict  					=       (
            shape_list(inputs_non_padded['input_ids']					)[-1]
            - tf.reduce_sum(tf.cast(inputs['attention_mask'][-1]							,					tf.intaa					)					).numpy()
        )
        lowercase_       :							List[str]  					=       tokenizer(sentences[1]							,					return_tensors='tf'					)
        lowercase_       :							Union[str, Any]  					=       model.generate(
            **__UpperCamelCase							,					do_sample=__UpperCamelCase							,					num_beams=2							,					max_length=model.config.max_length - num_paddings					)
        lowercase_       :							Tuple  					=       tokenizer.decode(output_non_padded[0]							,					skip_special_tokens=__UpperCamelCase					)
        lowercase_       :							Union[str, Any]  					=       tokenizer.decode(output_padded[0]							,					skip_special_tokens=__UpperCamelCase					)
        self.assertListEqual(__UpperCamelCase							,					[non_padded_sentence, padded_sentence]					)





   def 		_lowerCamelCase			(self					)     ->  Tuple:
        # Confirms that XLA is working properly
        lowercase_					,lowercase_					,lowercase_					,lowercase_       :							Union[str, Any]  					=       self._get_beam_search_test_objects()

        lowercase_       :							List[Any]  					=       tokenizer(__UpperCamelCase							,					return_tensors='tf'							,					padding=__UpperCamelCase					)
        lowercase_       :							Dict  					=       tf.function(model.generate							,					jit_compile=__UpperCamelCase					)
        lowercase_       :							Tuple  					=       xla_generate(**__UpperCamelCase							,					do_sample=__UpperCamelCase							,					num_beams=2					)
        lowercase_       :							int  					=       tokenizer.batch_decode(__UpperCamelCase							,					skip_special_tokens=__UpperCamelCase					)
        self.assertListEqual(__UpperCamelCase							,					__UpperCamelCase					)








'''simple docstring'''

import random
import unittest

import torch

from diffusers import IFImgaImgPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin





@skip_mps
class     __SCREAMING_SNAKE_CASE       (   lowerCamelCase__			,      lowerCamelCase__			,      unittest.TestCase			):
     UpperCAmelCase			       =					IFImgaImgPipeline
     UpperCAmelCase			       =					TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''width''', '''height'''}
     UpperCAmelCase			       =					TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     UpperCAmelCase			       =					PipelineTesterMixin.required_optional_params - {'''latents'''}






     def 			a_       (      self		)							->     Optional[Any]:
          return self._get_dummy_components()






     def 			a_       (      self  ,				__UpperCamelCase  ,				__UpperCamelCase=0		)							->     Any:
          if str(__UpperCamelCase		).startswith("mps"		):
               _a            =		torch.manual_seed(__UpperCamelCase		)
          else:
               _a            =		torch.Generator(device=__UpperCamelCase		).manual_seed(__UpperCamelCase		)

          _a            =		floats_tensor((1, 3, 32, 32)  ,				rng=random.Random(__UpperCamelCase		)		).to(__UpperCamelCase		)

          _a            =		{
              "prompt": "A painting of a squirrel eating a burger",
              "image": image,
              "generator": generator,
              "num_inference_steps": 2,
              "output_type": "numpy",
          }

          return inputs






     def 			a_       (      self		)							->     Any:
          self._test_save_load_optional_components()






     @unittest.skipIf(
         torch_device != "cuda" or not is_xformers_available()  ,				reason="XFormers attention is only available with CUDA and `xformers` installed"  ,				)
     def 			a_       (      self		)							->     Dict:
          self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3		)






     @unittest.skipIf(torch_device != "cuda"  ,				reason="float16 requires CUDA"		)
     def 			a_       (      self		)							->     List[Any]:
          # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
          super().test_save_load_floataa(expected_max_diff=1e-1		)






     @unittest.skipIf(torch_device != "cuda"  ,				reason="float16 requires CUDA"		)
     def 			a_       (      self		)							->     int:
          super().test_floataa_inference(expected_max_diff=1e-1		)






     def 			a_       (      self		)							->     Dict:
          self._test_attention_slicing_forward_pass(expected_max_diff=1e-2		)






     def 			a_       (      self		)							->     Optional[Any]:
          self._test_save_load_local()






     def 			a_       (      self		)							->     Union[str, Any]:
          self._test_inference_batch_single_identical(
              expected_max_diff=1e-2  ,				)

