
'''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 					a_					:




       def __init__(       self				,				snake_case_					):
         _lowerCAmelCase    :					Dict							=   parent
         _lowerCAmelCase    :					Dict							=   1_3
         _lowerCAmelCase    :					Union[str, Any]							=   7
         _lowerCAmelCase    :					Tuple							=   True
         _lowerCAmelCase    :					str							=   True
         _lowerCAmelCase    :					str							=   True
         _lowerCAmelCase    :					str							=   True
         _lowerCAmelCase    :					Union[str, Any]							=   True
         _lowerCAmelCase    :					Optional[int]							=   9_9
         _lowerCAmelCase    :					Optional[int]							=   3_2
         _lowerCAmelCase    :					List[Any]							=   4
         _lowerCAmelCase    :					List[Any]							=   2
         _lowerCAmelCase    :					Optional[Any]							=   4
         _lowerCAmelCase    :					Optional[Any]							=   3_7
         _lowerCAmelCase    :					int							=   """gelu"""
         _lowerCAmelCase    :					Optional[Any]							=   0.1
         _lowerCAmelCase    :					Tuple							=   0.1
         _lowerCAmelCase    :					Tuple							=   5_1_2
         _lowerCAmelCase    :					str							=   1_6
         _lowerCAmelCase    :					List[str]							=   2
         _lowerCAmelCase    :					Dict							=   0.02
         _lowerCAmelCase    :					Optional[int]							=   3
         _lowerCAmelCase    :					List[str]							=   4
         _lowerCAmelCase    :					Tuple							=   None
         _lowerCAmelCase    :					Dict							=   self.vocab_size - 1
         _lowerCAmelCase    :					int							=   self.vocab_size - 1
         _lowerCAmelCase    :					Union[str, Any]							=   self.vocab_size - 1




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Dict							=   ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size					)

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

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

         _lowerCAmelCase    :					Any							=   None
         if self.use_mc_token_ids:
           _lowerCAmelCase    :					int							=   ids_tensor([self.batch_size, self.num_choices]				,				self.seq_length					)

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

         _lowerCAmelCase    :					str							=   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=snake_case_				,				)

         _lowerCAmelCase    :					Dict							=   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 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				*snake_case_					):
         _lowerCAmelCase    :					int							=   TFGPTJModel(config=snake_case_					)
         _lowerCAmelCase    :					Any							=   {
             """input_ids""": input_ids,
             """attention_mask""": input_mask,
             """token_type_ids""": token_type_ids,
         }
         _lowerCAmelCase    :					int							=   model(snake_case_					)

         _lowerCAmelCase    :					Tuple							=   [input_ids, None, input_mask]  # None is the input for 'past'
         _lowerCAmelCase    :					Tuple							=   model(snake_case_					)

         _lowerCAmelCase    :					List[Any]							=   model(snake_case_					)

         self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				*snake_case_					):
         _lowerCAmelCase    :					Any							=   TFGPTJModel(config=snake_case_					)

         # first forward pass
         _lowerCAmelCase    :					int							=   model(snake_case_				,				token_type_ids=snake_case_				,				use_cache=snake_case_					)
         _lowerCAmelCase    :					Optional[int]							=   model(snake_case_				,				token_type_ids=snake_case_					)
         _lowerCAmelCase    :					List[str]							=   model(snake_case_				,				token_type_ids=snake_case_				,				use_cache=snake_case_					)

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

         _lowerCAmelCase			,   _lowerCAmelCase    :					Optional[int]							=   outputs.to_tuple()

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

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

         _lowerCAmelCase    :					int							=   model(snake_case_				,				token_type_ids=snake_case_					)["""last_hidden_state"""]
         _lowerCAmelCase    :					List[Any]							=   model(snake_case_				,				token_type_ids=snake_case_				,				past_key_values=snake_case_					)[
             """last_hidden_state"""
         ]

         # select random slice
         _lowerCAmelCase    :					Any							=   int(ids_tensor((1,)				,				shape_list(snake_case_					)[-1]					)					)
         _lowerCAmelCase    :					Optional[Any]							=   output_from_no_past[:, -1, random_slice_idx]
         _lowerCAmelCase    :					Optional[int]							=   output_from_past[:, 0, random_slice_idx]

         # test that outputs are equal for slice
         tf.debugging.assert_near(snake_case_				,				snake_case_				,				rtol=1E-6					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				*snake_case_					):
         _lowerCAmelCase    :					List[str]							=   TFGPTJModel(config=snake_case_					)

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

         # first forward pass
         _lowerCAmelCase			,   _lowerCAmelCase    :					Tuple							=   model(snake_case_				,				attention_mask=snake_case_					).to_tuple()

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

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

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

         # get two different outputs
         _lowerCAmelCase    :					Dict							=   model(snake_case_				,				attention_mask=snake_case_					)["""last_hidden_state"""]
         _lowerCAmelCase    :					List[Any]							=   model(snake_case_				,				past_key_values=snake_case_				,				attention_mask=snake_case_					)[
             """last_hidden_state"""
         ]

         # select random slice
         _lowerCAmelCase    :					Union[str, Any]							=   int(ids_tensor((1,)				,				shape_list(snake_case_					)[-1]					)					)
         _lowerCAmelCase    :					List[str]							=   output_from_no_past[:, -1, random_slice_idx]
         _lowerCAmelCase    :					str							=   output_from_past[:, 0, random_slice_idx]

         # test that outputs are equal for slice
         tf.debugging.assert_near(snake_case_				,				snake_case_				,				rtol=1E-12					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				*snake_case_					):
         _lowerCAmelCase    :					Optional[int]							=   TFGPTJModel(config=snake_case_					)

         _lowerCAmelCase    :					List[str]							=   input_ids[:1, :]
         _lowerCAmelCase    :					Optional[Any]							=   input_mask[:1, :]
         _lowerCAmelCase    :					Optional[int]							=   token_type_ids[:1, :]
         _lowerCAmelCase    :					Optional[int]							=   1

         # first forward pass
         _lowerCAmelCase    :					int							=   model(snake_case_				,				attention_mask=snake_case_				,				token_type_ids=snake_case_				,				use_cache=snake_case_					)

         _lowerCAmelCase			,   _lowerCAmelCase    :					List[str]							=   outputs.to_tuple()

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

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

         _lowerCAmelCase    :					Optional[int]							=   model(
             snake_case_				,				token_type_ids=snake_case_				,				attention_mask=snake_case_					)["""last_hidden_state"""]
         _lowerCAmelCase    :					Optional[Any]							=   model(
             snake_case_				,				token_type_ids=snake_case_				,				attention_mask=snake_case_				,				past_key_values=snake_case_				,				)["""last_hidden_state"""]
         self.parent.assertTrue(output_from_past.shape[1] == next_tokens.shape[1]					)

         # select random slice
         _lowerCAmelCase    :					Optional[Any]							=   int(ids_tensor((1,)				,				shape_list(snake_case_					)[-1]					)					)
         _lowerCAmelCase    :					str							=   output_from_no_past[:, -3:, random_slice_idx]
         _lowerCAmelCase    :					Optional[int]							=   output_from_past[:, :, random_slice_idx]

         # test that outputs are equal for slice
         tf.debugging.assert_near(snake_case_				,				snake_case_				,				rtol=1E-3					)




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				*snake_case_					):
         _lowerCAmelCase    :					List[str]							=   TFGPTJForCausalLM(config=snake_case_					)
         _lowerCAmelCase    :					Any							=   {
             """input_ids""": input_ids,
             """attention_mask""": input_mask,
             """token_type_ids""": token_type_ids,
         }
         _lowerCAmelCase    :					Dict							=   model(snake_case_					)
         self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.vocab_size)					)







       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Union[str, Any]							=   self.prepare_config_and_inputs()

         (
             (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   (
             _lowerCAmelCase
         )			,   
         )    :					Union[str, Any]							=   config_and_inputs

         _lowerCAmelCase    :					int							=   {
             """input_ids""": input_ids,
             """token_type_ids""": token_type_ids,
             """attention_mask""": input_mask,
         }
         return config, inputs_dict



@require_tf
class 					a_					(_a							,     _a							,     _a							,     unittest.TestCase      ):
       __lowerCAmelCase      :			List[Any]      = (
           (TFGPTJForCausalLM, TFGPTJForSequenceClassification, TFGPTJForQuestionAnswering, TFGPTJModel)
           if is_tf_available()
           else ()
       )

       __lowerCAmelCase      :			Optional[Any]      = (TFGPTJForCausalLM,) if is_tf_available() else ()
       __lowerCAmelCase      :			List[str]      = (
           {
               """feature-extraction""": TFGPTJModel,
               """question-answering""": TFGPTJForQuestionAnswering,
               """text-classification""": TFGPTJForSequenceClassification,
               """text-generation""": TFGPTJForCausalLM,
               """zero-shot""": TFGPTJForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       __lowerCAmelCase      :			Tuple      = False
       __lowerCAmelCase      :			List[str]      = False
       __lowerCAmelCase      :			Dict      = False
       __lowerCAmelCase      :			Union[str, Any]      = False




       def 							__UpperCamelCase    (       self				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_				,				snake_case_					):
         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 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Tuple							=   TFGPTJModelTester(self					)
         _lowerCAmelCase    :					Dict							=   ConfigTester(self				,				config_class=snake_case_				,				n_embd=3_7					)




       def 							__UpperCamelCase    (       self					):
         self.config_tester.run_common_tests()




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Optional[Any]							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_gptj_model(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Union[str, Any]							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_gptj_model_past(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Dict							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_gptj_model_attention_mask_past(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Dict							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_gptj_model_past_large_inputs(*snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					str							=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_gptj_lm_head_model(*snake_case_					)




       @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 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					str							=   TFGPTJModel.from_pretrained("""EleutherAI/gpt-j-6B"""				,				from_pt=snake_case_					)
         self.assertIsNotNone(snake_case_					)







       @unittest.skip(reason="""Currently, model embeddings are going to undergo a major refactor."""					)
       def 							__UpperCamelCase    (       self					):
         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 					a_					(unittest.TestCase      ):




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					List[Any]							=   TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B"""				,				from_pt=snake_case_					)
         _lowerCAmelCase    :					str							=   tf.convert_to_tensor([[4_6_4, 3_2_9_0]]				,				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
         _lowerCAmelCase    :					int							=   [4_6_4, 3_2_9_0, 3_1_8, 2_5_7, 5_8_2, 3_3_8, 1_2_6_6, 1_5_4_5, 1_3, 6_3_2, 3_1_8, 2_5_7, 9_1_1_2, 1_5_1_8_5, 1_1, 2_9_0, 3_4_0, 3_1_8, 2_5_7, 1_5_4_5]
         # fmt: on
         _lowerCAmelCase    :					str							=   model.generate(snake_case_				,				do_sample=snake_case_					)
         self.assertListEqual(output_ids[0].numpy().tolist()				,				snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					str							=   AutoTokenizer.from_pretrained("""EleutherAI/gpt-j-6B"""				,				revision="""float16"""					)
         _lowerCAmelCase    :					Union[str, Any]							=   TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B"""				,				revision="""float16"""				,				from_pt=snake_case_					)

         _lowerCAmelCase    :					Any							=   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"""					):
           _lowerCAmelCase    :					Tuple							=   model.generate(**snake_case_				,				do_sample=snake_case_				,				seed=[4_2, 0]					)
         _lowerCAmelCase    :					Tuple							=   tokenizer.decode(output_ids[0]				,				skip_special_tokens=snake_case_					)

         _lowerCAmelCase    :					List[Any]							=   """Today is a nice day and I’m going to go for a walk. I’"""
         self.assertEqual(snake_case_				,				snake_case_					)




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Optional[Any]							=   TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B"""				,				revision="""float16"""				,				from_pt=snake_case_					)
         _lowerCAmelCase    :					Optional[int]							=   AutoTokenizer.from_pretrained("""EleutherAI/gpt-j-6B"""				,				revision="""float16"""					)

         _lowerCAmelCase    :					Optional[int]							=   """left"""

         # Define PAD Token = EOS Token = 50256
         _lowerCAmelCase    :					Dict							=   tokenizer.eos_token
         _lowerCAmelCase    :					List[Any]							=   model.config.eos_token_id

         # use different length sentences to test batching
         _lowerCAmelCase    :					str							=   [
             """Hello, my dog is a little""",
             """Today, I""",
         ]
         _lowerCAmelCase    :					Tuple							=   [
             """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 							__UpperCamelCase    (       self					):
         # Confirms that we get the expected results with left-padded beam search
         _lowerCAmelCase			,   _lowerCAmelCase			,   _lowerCAmelCase			,   _lowerCAmelCase    :					Dict							=   self._get_beam_search_test_objects()

         _lowerCAmelCase    :					Dict							=   tokenizer(snake_case_				,				return_tensors="""tf"""				,				padding=snake_case_					)
         _lowerCAmelCase    :					Any							=   model.generate(**snake_case_				,				do_sample=snake_case_				,				num_beams=2					)
         _lowerCAmelCase    :					Optional[Any]							=   tokenizer.batch_decode(snake_case_				,				skip_special_tokens=snake_case_					)
         self.assertListEqual(snake_case_				,				snake_case_					)




       def 							__UpperCamelCase    (       self					):
         # Confirms that left-padding is working properly
         _lowerCAmelCase			,   _lowerCAmelCase			,   _lowerCAmelCase			,   _lowerCAmelCase    :					Tuple							=   self._get_beam_search_test_objects()

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







       def 							__UpperCamelCase    (       self					):
         # Confirms that XLA is working properly
         _lowerCAmelCase			,   _lowerCAmelCase			,   _lowerCAmelCase			,   _lowerCAmelCase    :					Dict							=   self._get_beam_search_test_objects()

         _lowerCAmelCase    :					Optional[int]							=   tokenizer(snake_case_				,				return_tensors="""tf"""				,				padding=snake_case_					)
         _lowerCAmelCase    :					Any							=   tf.function(model.generate				,				jit_compile=snake_case_					)
         _lowerCAmelCase    :					Any							=   xla_generate(**snake_case_				,				do_sample=snake_case_				,				num_beams=2					)
         _lowerCAmelCase    :					int							=   tokenizer.batch_decode(snake_case_				,				skip_special_tokens=snake_case_					)
         self.assertListEqual(snake_case_				,				snake_case_					)




'''simple docstring'''

import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPImageProcessor, CLIPVisionConfig

from diffusers import AutoencoderKL, PaintByExamplePipeline, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.paint_by_example import PaintByExampleImageEncoder
from diffusers.utils import floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import IMAGE_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, IMAGE_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class 					a_					(_a							,     unittest.TestCase      ):
       __lowerCAmelCase      :			Union[str, Any]      = PaintByExamplePipeline
       __lowerCAmelCase      :			int      = IMAGE_GUIDED_IMAGE_INPAINTING_PARAMS
       __lowerCAmelCase      :			str      = IMAGE_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
       __lowerCAmelCase      :			Any      = frozenset([]      )  # TO_DO: update the image_prams once refactored VaeImageProcessor.preprocess




       def 							__UpperCamelCase    (       self					):
         torch.manual_seed(0					)
         _lowerCAmelCase    :					Dict							=   UNetaDConditionModel(
             block_out_channels=(3_2, 6_4)				,				layers_per_block=2				,				sample_size=3_2				,				in_channels=9				,				out_channels=4				,				down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")				,				up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")				,				cross_attention_dim=3_2				,				)
         _lowerCAmelCase    :					List[Any]							=   PNDMScheduler(skip_prk_steps=snake_case_					)
         torch.manual_seed(0					)
         _lowerCAmelCase    :					List[Any]							=   AutoencoderKL(
             block_out_channels=[3_2, 6_4]				,				in_channels=3				,				out_channels=3				,				down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]				,				up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]				,				latent_channels=4				,				)
         torch.manual_seed(0					)
         _lowerCAmelCase    :					List[str]							=   CLIPVisionConfig(
             hidden_size=3_2				,				projection_dim=3_2				,				intermediate_size=3_7				,				layer_norm_eps=1E-05				,				num_attention_heads=4				,				num_hidden_layers=5				,				image_size=3_2				,				patch_size=4				,				)
         _lowerCAmelCase    :					Optional[int]							=   PaintByExampleImageEncoder(snake_case_				,				proj_size=3_2					)
         _lowerCAmelCase    :					Optional[int]							=   CLIPImageProcessor(crop_size=3_2				,				size=3_2					)

         _lowerCAmelCase    :					Union[str, Any]							=   {
             """unet""": unet,
             """scheduler""": scheduler,
             """vae""": vae,
             """image_encoder""": image_encoder,
             """safety_checker""": None,
             """feature_extractor""": feature_extractor,
         }
         return components




       def 							__UpperCamelCase    (       self				,				snake_case_					):
         _lowerCAmelCase    :					Optional[int]							=   np.array(image.convert("""RGB"""					)					)
         _lowerCAmelCase    :					Any							=   image[None].transpose(0				,				3				,				1				,				2					)
         _lowerCAmelCase    :					List[str]							=   torch.from_numpy(snake_case_					).to(dtype=torch.floataa					) / 127.5 - 1.0
         return image




       def 							__UpperCamelCase    (       self				,				snake_case_="cpu"				,				snake_case_=0					):
         # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched
         _lowerCAmelCase    :					Tuple							=   floats_tensor((1, 3, 3_2, 3_2)				,				rng=random.Random(snake_case_					)					).to(snake_case_					)
         _lowerCAmelCase    :					List[Any]							=   image.cpu().permute(0				,				2				,				3				,				1					)[0]
         _lowerCAmelCase    :					List[Any]							=   Image.fromarray(np.uinta(snake_case_					)					).convert("""RGB"""					).resize((6_4, 6_4)					)
         _lowerCAmelCase    :					List[str]							=   Image.fromarray(np.uinta(image + 4					)					).convert("""RGB"""					).resize((6_4, 6_4)					)
         _lowerCAmelCase    :					Any							=   Image.fromarray(np.uinta(snake_case_					)					).convert("""RGB"""					).resize((3_2, 3_2)					)

         if str(snake_case_					).startswith("""mps"""					):
           _lowerCAmelCase    :					str							=   torch.manual_seed(snake_case_					)
         else:
           _lowerCAmelCase    :					Tuple							=   torch.Generator(device=snake_case_					).manual_seed(snake_case_					)
         _lowerCAmelCase    :					str							=   {
             """example_image""": example_image,
             """image""": init_image,
             """mask_image""": mask_image,
             """generator""": generator,
             """num_inference_steps""": 2,
             """guidance_scale""": 6.0,
             """output_type""": """numpy""",
         }
         return inputs




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Dict							=   self.get_dummy_components()

         # make sure here that pndm scheduler skips prk
         _lowerCAmelCase    :					Optional[Any]							=   PaintByExamplePipeline(**snake_case_					)
         _lowerCAmelCase    :					List[Any]							=   pipe.to("""cpu"""					)
         pipe.set_progress_bar_config(disable=snake_case_					)

         _lowerCAmelCase    :					int							=   self.get_dummy_inputs()
         _lowerCAmelCase    :					List[str]							=   pipe(**snake_case_					)
         _lowerCAmelCase    :					Any							=   output.images

         _lowerCAmelCase    :					Optional[int]							=   image[0, -3:, -3:, -1]

         assert image.shape == (1, 6_4, 6_4, 3)
         _lowerCAmelCase    :					Optional[Any]							=   np.array([0.4686, 0.5687, 0.4007, 0.5218, 0.5741, 0.4482, 0.4940, 0.4629, 0.4503]					)

         assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2




       def 							__UpperCamelCase    (       self					):
         _lowerCAmelCase    :					Optional[int]							=   """cpu"""
         _lowerCAmelCase    :					int							=   self.get_dummy_inputs()
         inputs.pop("""mask_image"""					)
         _lowerCAmelCase    :					Optional[int]							=   self.convert_to_pt(inputs.pop("""image"""					)					)
         _lowerCAmelCase    :					int							=   image.clamp(0				,				1					) / 2

         # make sure here that pndm scheduler skips prk
         _lowerCAmelCase    :					List[Any]							=   PaintByExamplePipeline(**self.get_dummy_components()					)
         _lowerCAmelCase    :					Optional[int]							=   pipe.to(snake_case_					)
         pipe.set_progress_bar_config(disable=snake_case_					)

         _lowerCAmelCase    :					str							=   pipe(image=snake_case_				,				mask_image=mask_image[:, 0]				,				**snake_case_					)
         _lowerCAmelCase    :					str							=   output.images

         _lowerCAmelCase    :					List[str]							=   image.cpu().permute(0				,				2				,				3				,				1					)[0]
         _lowerCAmelCase    :					List[str]							=   mask_image.cpu().permute(0				,				2				,				3				,				1					)[0]

         _lowerCAmelCase    :					Any							=   Image.fromarray(np.uinta(snake_case_					)					).convert("""RGB"""					)
         _lowerCAmelCase    :					Any							=   Image.fromarray(np.uinta(snake_case_					)					).convert("""RGB"""					)

         _lowerCAmelCase    :					str							=   pipe(**self.get_dummy_inputs()					)
         _lowerCAmelCase    :					int							=   output.images

         assert out_a.shape == (1, 6_4, 6_4, 3)
         assert np.abs(out_a.flatten() - out_a.flatten()					).max() < 5E-2







       def 							__UpperCamelCase    (       self					):
         super().test_inference_batch_single_identical(expected_max_diff=3E-3					)



@slow
@require_torch_gpu
class 					a_					(unittest.TestCase      ):




       def 							__UpperCamelCase    (       self					):
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()







       def 							__UpperCamelCase    (       self					):
         # make sure here that pndm scheduler skips prk
         _lowerCAmelCase    :					Union[str, Any]							=   load_image(
             """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
             """/paint_by_example/dog_in_bucket.png"""					)
         _lowerCAmelCase    :					List[Any]							=   load_image(
             """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
             """/paint_by_example/mask.png"""					)
         _lowerCAmelCase    :					List[Any]							=   load_image(
             """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
             """/paint_by_example/panda.jpg"""					)

         _lowerCAmelCase    :					str							=   PaintByExamplePipeline.from_pretrained("""Fantasy-Studio/Paint-by-Example"""					)
         _lowerCAmelCase    :					int							=   pipe.to(snake_case_					)
         pipe.set_progress_bar_config(disable=snake_case_					)

         _lowerCAmelCase    :					str							=   torch.manual_seed(3_2_1					)
         _lowerCAmelCase    :					Optional[int]							=   pipe(
             image=snake_case_				,				mask_image=snake_case_				,				example_image=snake_case_				,				generator=snake_case_				,				guidance_scale=5.0				,				num_inference_steps=5_0				,				output_type="""np"""				,				)

         _lowerCAmelCase    :					List[str]							=   output.images

         _lowerCAmelCase    :					List[str]							=   image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_1_2, 5_1_2, 3)
         _lowerCAmelCase    :					Any							=   np.array([0.4834, 0.4811, 0.4874, 0.5122, 0.5081, 0.5144, 0.5291, 0.5290, 0.5374]					)

         assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2


