"""simple docstring"""





import tempfile
import unittest

import transformers
from transformers import XGLMConfig, XGLMTokenizer, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, require_sentencepiece, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
       import jax
       import jax.numpy as jnp
       import numpy as np

       from transformers.modeling_flax_pytorch_utils import (
           convert_pytorch_state_dict_to_flax,
           load_flax_weights_in_pytorch_model,
       )
       from transformers.models.xglm.modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel


if is_torch_available():
       import torch


@require_flax
class 					UpperCAmelCase_     :


      def __init__(       self		,      a		,      a=1_4		,      a=7		,      a=True		,      a=True		,      a=True		,      a=9_9		,      a=3_2		,      a=5		,      a=4		,      a=3_7		,      a="gelu"		,      a=0.1		,      a=0.1		,      a=5_1_2		,      a=0.02		,      a=None		,      )	-> List[str]:
             lowercase__      :		Dict          =     parent
             lowercase__      :		List[str]          =     batch_size
             lowercase__      :		Optional[Any]          =     seq_length
             lowercase__      :		Any          =     is_training
             lowercase__      :		Any          =     use_input_mask
             lowercase__      :		str          =     use_labels
             lowercase__      :		Dict          =     vocab_size
             lowercase__      :		str          =     d_model
             lowercase__      :		Dict          =     num_hidden_layers
             lowercase__      :		Optional[int]          =     num_attention_heads
             lowercase__      :		Optional[Any]          =     ffn_dim
             lowercase__      :		str          =     activation_function
             lowercase__      :		Optional[int]          =     activation_dropout
             lowercase__      :		Optional[Any]          =     attention_dropout
             lowercase__      :		int          =     max_position_embeddings
             lowercase__      :		str          =     initializer_range
             lowercase__      :		int          =     None
             lowercase__      :		Union[str, Any]          =     0
             lowercase__      :		Tuple          =     2
             lowercase__      :		str          =     1


      def 		_UpperCAmelCase	(       self							)	-> str:
             lowercase__      :		int          =     np.clip(ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size							)		,      3		,      self.vocab_size							)

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

             lowercase__      :		List[Any]          =     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=a		,      bos_token_id=self.bos_token_id		,      eos_token_id=self.eos_token_id		,      pad_token_id=self.pad_token_id		,      )

             return (config, input_ids, input_mask)


      def 		_UpperCAmelCase	(       self							)	-> Tuple:
             lowercase__      :		Union[str, Any]          =     self.prepare_config_and_inputs()
             lowercase__   ,				lowercase__   ,				lowercase__      :		Any          =     config_and_inputs
             lowercase__      :		Tuple          =     {'input_ids': input_ids, 'attention_mask': attention_mask}
             return config, inputs_dict


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__   ,				lowercase__   ,				lowercase__      :		List[str]          =     self.prepare_config_and_inputs()

             lowercase__      :		Union[str, Any]          =     floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
             lowercase__      :		int          =     ids_tensor([self.batch_size, self.seq_length]		,      vocab_size=2							)

             return (
                 config,
                 input_ids,
                 attention_mask,
                 encoder_hidden_states,
                 encoder_attention_mask,
             )


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a							)	-> List[Any]:
             lowercase__      :		Tuple          =     2_0
             lowercase__      :		List[str]          =     model_class_name(a							)

             lowercase__      :		List[str]          =     model.init_cache(input_ids.shape[0]		,      a							)
             lowercase__      :		int          =     jnp.ones((input_ids.shape[0], max_decoder_length)		,      dtype='i4'							)

             lowercase__      :		Any          =     jnp.broadcast_to(
                 jnp.arange(input_ids.shape[-1] - 1							)[None, :]		,      (input_ids.shape[0], input_ids.shape[-1] - 1)							)
             lowercase__      :		List[Any]          =     model(
                 input_ids[:, :-1]		,      attention_mask=a		,      past_key_values=a		,      position_ids=a		,      )

             lowercase__      :		int          =     jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]		,      dtype='i4'							)
             lowercase__      :		Tuple          =     model(
                 input_ids[:, -1:]		,      attention_mask=a		,      past_key_values=outputs_cache.past_key_values		,      position_ids=a		,      )

             lowercase__      :		Optional[int]          =     model(a							)

             lowercase__      :		Dict          =     np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])							)							)
             self.parent.assertTrue(diff < 1e-3		,      msg=f"""Max diff is {diff}"""							)


      def 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a							)	-> Any:
             lowercase__      :		int          =     2_0
             lowercase__      :		Dict          =     model_class_name(a							)

             lowercase__      :		Optional[int]          =     jnp.concatenate(
                 [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1])							)]		,      axis=-1		,      )

             lowercase__      :		Dict          =     model.init_cache(input_ids.shape[0]		,      a							)
             lowercase__      :		List[Any]          =     jnp.broadcast_to(
                 jnp.arange(input_ids.shape[-1] - 1							)[None, :]		,      (input_ids.shape[0], input_ids.shape[-1] - 1)							)

             lowercase__      :		Optional[int]          =     model(
                 input_ids[:, :-1]		,      attention_mask=a		,      past_key_values=a		,      position_ids=a		,      )
             lowercase__      :		Optional[int]          =     jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]		,      dtype='i4'							)
             lowercase__      :		Union[str, Any]          =     model(
                 input_ids[:, -1:]		,      past_key_values=outputs_cache.past_key_values		,      attention_mask=a		,      position_ids=a		,      )

             lowercase__      :		Optional[int]          =     model(a		,      attention_mask=a							)
             lowercase__      :		List[Any]          =     np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])							)							)
             self.parent.assertTrue(diff < 1e-3		,      msg=f"""Max diff is {diff}"""							)





@require_sentencepiece
@require_flax
class 					UpperCAmelCase_     (   _a			,							_a			,							unittest.TestCase):
      lowerCamelCase__		:   int			  =  (FlaxXGLMModel, FlaxXGLMForCausalLM) if is_flax_available() else ()
      lowerCamelCase__		:   List[str]			  =  (FlaxXGLMForCausalLM,) if is_flax_available() else ()


      def 		_UpperCAmelCase	(       self							)	-> List[str]:
             lowercase__      :		str          =     FlaxXGLMModelTester(self							)


      def 		_UpperCAmelCase	(       self							)	-> Optional[Any]:
             for model_class_name in self.all_model_classes:
                    lowercase__   ,				lowercase__   ,				lowercase__      :		Optional[int]          =     self.model_tester.prepare_config_and_inputs()
                    self.model_tester.check_use_cache_forward(a		,      a		,      a		,      a							)


      def 		_UpperCAmelCase	(       self							)	-> int:
             for model_class_name in self.all_model_classes:
                    lowercase__   ,				lowercase__   ,				lowercase__      :		Union[str, Any]          =     self.model_tester.prepare_config_and_inputs()
                    self.model_tester.check_use_cache_forward_with_attn_mask(
                        a		,      a		,      a		,      a							)


      @slow
      def 		_UpperCAmelCase	(       self							)	-> Optional[Any]:
             lowercase__      :		Any          =     XGLMTokenizer.from_pretrained('XGLM'		,      padding_side='left'							)
             lowercase__      :		Dict          =     tokenizer(['Hello this is a long string', 'Hey']		,      return_tensors='np'		,      padding=a		,      truncation=a							)

             lowercase__      :		Tuple          =     FlaxXGLMForCausalLM.from_pretrained('facebook/xglm-564M'							)
             lowercase__      :		Dict          =     1
             lowercase__      :		Optional[Any]          =     False

             lowercase__      :		Optional[Any]          =     jax.jit(model.generate							)

             lowercase__      :		Union[str, Any]          =     jit_generate(inputs['input_ids']		,      attention_mask=inputs['attention_mask']							).sequences

             lowercase__      :		int          =     tokenizer.batch_decode(a		,      skip_special_tokens=a							)

             lowercase__      :		List[Any]          =     [
                 'Hello this is a long string of questions, but I\'m not sure if I\'m',
                 'Hey, I\'m a newbie to the forum and I\'',
             ]

             self.assertListEqual(a		,      a							)


      @is_pt_flax_cross_test
      def 		_UpperCAmelCase	(       self							)	-> int:
             lowercase__   ,				lowercase__      :		str          =     self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    with self.subTest(model_class.__name__							):
                           # prepare inputs
                           lowercase__      :		int          =     self._prepare_for_class(a		,      a							)
                           lowercase__      :		List[Any]          =     {k: torch.tensor(v.tolist()							) for k, v in prepared_inputs_dict.items()}

                           # load corresponding PyTorch class
                           lowercase__      :		Optional[Any]          =     model_class.__name__[4:]  # Skip the "Flax" at the beginning
                           lowercase__      :		Dict          =     getattr(a		,      a							)

                           lowercase__   ,				lowercase__      :		Optional[int]          =     pt_inputs['input_ids'].shape
                           lowercase__      :		Optional[Any]          =     np.random.randint(0		,      seq_length - 1		,      size=(batch_size,)							)
                           for batch_idx, start_index in enumerate(a							):
                                  lowercase__      :		int          =     0
                                  lowercase__      :		List[str]          =     1
                                  lowercase__      :		Dict          =     0
                                  lowercase__      :		List[Any]          =     1
                           lowercase__      :		Dict          =     pt_model_class(a							).eval()
                           # Flax models don't use the `use_cache` option and cache is not returned as a default.
                           # So we disable `use_cache` here for PyTorch model.
                           lowercase__      :		Optional[int]          =     False
                           lowercase__      :		Tuple          =     model_class(a		,      dtype=jnp.floataa							)
                           lowercase__      :		int          =     convert_pytorch_state_dict_to_flax(pt_model.state_dict()		,      a							)
                           lowercase__      :		Dict          =     fx_state

                           with torch.no_grad():
                                  lowercase__      :		List[str]          =     pt_model(**a							).to_tuple()

                           lowercase__      :		Tuple          =     fx_model(**a							).to_tuple()
                           self.assertEqual(len(a							)		,      len(a							)		,      'Output lengths differ between Flax and PyTorch'							)
                           for fx_output, pt_output in zip(a		,      a							):
                                  self.assert_almost_equals(fx_output[:, -1]		,      pt_output[:, -1].numpy()		,      4e-2							)

                           with tempfile.TemporaryDirectory() as tmpdirname:
                                  pt_model.save_pretrained(a							)
                                  lowercase__      :		Any          =     model_class.from_pretrained(a		,      from_pt=a							)

                           lowercase__      :		Tuple          =     fx_model_loaded(**a							).to_tuple()
                           self.assertEqual(
                               len(a							)		,      len(a							)		,      'Output lengths differ between Flax and PyTorch'							)
                           for fx_output_loaded, pt_output in zip(a		,      a							):
                                  self.assert_almost_equals(fx_output_loaded[:, -1]		,      pt_output[:, -1].numpy()		,      4e-2							)


      @is_pt_flax_cross_test
      def 		_UpperCAmelCase	(       self							)	-> Any:
             lowercase__   ,				lowercase__      :		Union[str, Any]          =     self.model_tester.prepare_config_and_inputs_for_common()
             for model_class in self.all_model_classes:
                    with self.subTest(model_class.__name__							):
                           # prepare inputs
                           lowercase__      :		Dict          =     self._prepare_for_class(a		,      a							)
                           lowercase__      :		Optional[int]          =     {k: torch.tensor(v.tolist()							) for k, v in prepared_inputs_dict.items()}

                           # load corresponding PyTorch class
                           lowercase__      :		Union[str, Any]          =     model_class.__name__[4:]  # Skip the "Flax" at the beginning
                           lowercase__      :		str          =     getattr(a		,      a							)

                           lowercase__      :		Optional[Any]          =     pt_model_class(a							).eval()
                           lowercase__      :		int          =     False
                           lowercase__      :		int          =     model_class(a		,      dtype=jnp.floataa							)

                           lowercase__      :		Optional[int]          =     load_flax_weights_in_pytorch_model(a		,      fx_model.params							)
                           lowercase__   ,				lowercase__      :		Optional[int]          =     pt_inputs['input_ids'].shape
                           lowercase__      :		List[Any]          =     np.random.randint(0		,      seq_length - 1		,      size=(batch_size,)							)
                           for batch_idx, start_index in enumerate(a							):
                                  lowercase__      :		Optional[Any]          =     0
                                  lowercase__      :		Dict          =     1
                                  lowercase__      :		Any          =     0
                                  lowercase__      :		Dict          =     1

                           # make sure weights are tied in PyTorch
                           pt_model.tie_weights()

                           with torch.no_grad():
                                  lowercase__      :		Dict          =     pt_model(**a							).to_tuple()

                           lowercase__      :		Union[str, Any]          =     fx_model(**a							).to_tuple()
                           self.assertEqual(len(a							)		,      len(a							)		,      'Output lengths differ between Flax and PyTorch'							)
                           for fx_output, pt_output in zip(a		,      a							):
                                  self.assert_almost_equals(fx_output[:, -1]		,      pt_output[:, -1].numpy()		,      4e-2							)

                           with tempfile.TemporaryDirectory() as tmpdirname:
                                  fx_model.save_pretrained(a							)
                                  lowercase__      :		Dict          =     pt_model_class.from_pretrained(a		,      from_flax=a							)

                           with torch.no_grad():
                                  lowercase__      :		Optional[int]          =     pt_model_loaded(**a							).to_tuple()

                           self.assertEqual(
                               len(a							)		,      len(a							)		,      'Output lengths differ between Flax and PyTorch'							)
                           for fx_output, pt_output in zip(a		,      a							):
                                  self.assert_almost_equals(fx_output[:, -1]		,      pt_output[:, -1].numpy()		,      4e-2							)


      @slow
      def 		_UpperCAmelCase	(       self							)	-> Dict:
             for model_class_name in self.all_model_classes:
                    lowercase__      :		Union[str, Any]          =     model_class_name.from_pretrained('facebook/xglm-564M'							)
                    lowercase__      :		Optional[int]          =     model(np.ones((1, 1)							)							)
                    self.assertIsNotNone(a							)


"""simple docstring"""





import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class 					UpperCAmelCase_     (   _a			,							unittest.TestCase):
      lowerCamelCase__		:   Tuple			  =  KandinskyPriorPipeline
      lowerCamelCase__		:   Tuple			  =  ["prompt"]
      lowerCamelCase__		:   List[str]			  =  ["prompt", "negative_prompt"]
      lowerCamelCase__		:   List[Any]			  =  [
          "num_images_per_prompt",
          "generator",
          "num_inference_steps",
          "latents",
          "negative_prompt",
          "guidance_scale",
          "output_type",
          "return_dict",
      ]
      lowerCamelCase__		:   Optional[Any]			  =  False


      @property
      def 		_UpperCAmelCase	(       self							)	-> Union[str, Any]:
             return 3_2


      @property
      def 		_UpperCAmelCase	(       self							)	-> str:
             return 3_2


      @property
      def 		_UpperCAmelCase	(       self							)	-> Optional[Any]:
             return self.time_input_dim


      @property
      def 		_UpperCAmelCase	(       self							)	-> Any:
             return self.time_input_dim * 4


      @property
      def 		_UpperCAmelCase	(       self							)	-> Optional[int]:
             return 1_0_0


      @property
      def 		_UpperCAmelCase	(       self							)	-> List[Any]:
             lowercase__      :		Tuple          =     CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'							)
             return tokenizer


      @property
      def 		_UpperCAmelCase	(       self							)	-> Any:
             torch.manual_seed(0							)
             lowercase__      :		Optional[int]          =     CLIPTextConfig(
                 bos_token_id=0		,      eos_token_id=2		,      hidden_size=self.text_embedder_hidden_size		,      projection_dim=self.text_embedder_hidden_size		,      intermediate_size=3_7		,      layer_norm_eps=1e-05		,      num_attention_heads=4		,      num_hidden_layers=5		,      pad_token_id=1		,      vocab_size=1_0_0_0		,      )
             return CLIPTextModelWithProjection(a							)


      @property
      def 		_UpperCAmelCase	(       self							)	-> Union[str, Any]:
             torch.manual_seed(0							)

             lowercase__      :		int          =     {
                 'num_attention_heads': 2,
                 'attention_head_dim': 1_2,
                 'embedding_dim': self.text_embedder_hidden_size,
                 'num_layers': 1,
             }

             lowercase__      :		Tuple          =     PriorTransformer(**a							)
             # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
             lowercase__      :		int          =     nn.Parameter(torch.ones(model.clip_std.shape							)							)
             return model


      @property
      def 		_UpperCAmelCase	(       self							)	-> Optional[int]:
             torch.manual_seed(0							)
             lowercase__      :		Optional[int]          =     CLIPVisionConfig(
                 hidden_size=self.text_embedder_hidden_size		,      image_size=2_2_4		,      projection_dim=self.text_embedder_hidden_size		,      intermediate_size=3_7		,      num_attention_heads=4		,      num_channels=3		,      num_hidden_layers=5		,      patch_size=1_4		,      )

             lowercase__      :		Optional[Any]          =     CLIPVisionModelWithProjection(a							)
             return model


      @property
      def 		_UpperCAmelCase	(       self							)	-> Tuple:
             lowercase__      :		Dict          =     CLIPImageProcessor(
                 crop_size=2_2_4		,      do_center_crop=a		,      do_normalize=a		,      do_resize=a		,      image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073]		,      image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711]		,      resample=3		,      size=2_2_4		,      )

             return image_processor


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		List[str]          =     self.dummy_prior
             lowercase__      :		Tuple          =     self.dummy_image_encoder
             lowercase__      :		Optional[Any]          =     self.dummy_text_encoder
             lowercase__      :		List[str]          =     self.dummy_tokenizer
             lowercase__      :		Optional[int]          =     self.dummy_image_processor

             lowercase__      :		List[Any]          =     UnCLIPScheduler(
                 variance_type='fixed_small_log'		,      prediction_type='sample'		,      num_train_timesteps=1_0_0_0		,      clip_sample=a		,      clip_sample_range=10.0		,      )

             lowercase__      :		List[str]          =     {
                 'prior': prior,
                 'image_encoder': image_encoder,
                 'text_encoder': text_encoder,
                 'tokenizer': tokenizer,
                 'scheduler': scheduler,
                 'image_processor': image_processor,
             }

             return components


      def 		_UpperCAmelCase	(       self		,      a		,      a=0							)	-> Union[str, Any]:
             if str(a							).startswith('mps'							):
                    lowercase__      :		Optional[Any]          =     torch.manual_seed(a							)
             else:
                    lowercase__      :		Any          =     torch.Generator(device=a							).manual_seed(a							)
             lowercase__      :		str          =     {
                 'prompt': 'horse',
                 'generator': generator,
                 'guidance_scale': 4.0,
                 'num_inference_steps': 2,
                 'output_type': 'np',
             }
             return inputs


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		Optional[int]          =     'cpu'

             lowercase__      :		Optional[Any]          =     self.get_dummy_components()

             lowercase__      :		Optional[Any]          =     self.pipeline_class(**a							)
             lowercase__      :		Optional[int]          =     pipe.to(a							)

             pipe.set_progress_bar_config(disable=a							)

             lowercase__      :		Optional[int]          =     pipe(**self.get_dummy_inputs(a							)							)
             lowercase__      :		Optional[Any]          =     output.image_embeds

             lowercase__      :		Dict          =     pipe(
                 **self.get_dummy_inputs(a							)		,      return_dict=a		,      )[0]

             lowercase__      :		List[str]          =     image[0, -1_0:]
             lowercase__      :		Optional[Any]          =     image_from_tuple[0, -1_0:]

             assert image.shape == (1, 3_2)

             lowercase__      :		Union[str, Any]          =     np.array(
                 [-0.0_532, 1.7_120, 0.3_656, -1.0_852, -0.8_946, -1.1_756, 0.4_348, 0.2_482, 0.5_146, -0.1_156]							)

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


      @skip_mps
      def 		_UpperCAmelCase	(       self							)	-> List[Any]:
             lowercase__      :		Tuple          =     torch_device == 'cpu'
             lowercase__      :		Union[str, Any]          =     True
             lowercase__      :		int          =     False

             self._test_inference_batch_single_identical(
                 test_max_difference=a		,      relax_max_difference=a		,      test_mean_pixel_difference=a		,      )


      @skip_mps
      def 		_UpperCAmelCase	(       self							)	-> int:
             lowercase__      :		Tuple          =     torch_device == 'cpu'
             lowercase__      :		Any          =     False

             self._test_attention_slicing_forward_pass(
                 test_max_difference=a		,      test_mean_pixel_difference=a		,      )

