






import unittest

from transformers import GPTNeoXJapaneseConfig, is_torch_available
from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel

class        lowerCAmelCase_				:







				"""simple docstring"""




				def __init__( self     :Optional[Any]  ,     lowerCamelCase__     :Optional[int]  ,     lowerCamelCase__     :Tuple=13  ,     lowerCamelCase__     :Tuple=7  ,     lowerCamelCase__     :Optional[Any]=True  ,     lowerCamelCase__     :Union[str, Any]=True  ,     lowerCamelCase__     :Optional[int]=True  ,     lowerCamelCase__     :List[Any]=True  ,     lowerCamelCase__     :List[str]=99  ,     lowerCamelCase__     :int=32  ,     lowerCamelCase__     :List[Any]=5  ,     lowerCamelCase__     :Tuple=4  ,     lowerCamelCase__     :List[Any]=4  ,     lowerCamelCase__     :str="gelu"  ,     lowerCamelCase__     :Optional[Any]=0.0  ,     lowerCamelCase__     :Optional[int]=0.1  ,     lowerCamelCase__     :str=True  ,     lowerCamelCase__     :Dict=5_12  ,     lowerCamelCase__     :Optional[Any]=16  ,     lowerCamelCase__     :Optional[Any]=2  ,     lowerCamelCase__     :Union[str, Any]=0.02  ,     lowerCamelCase__     :Union[str, Any]=3  ,     lowerCamelCase__     :int=4  ,     lowerCamelCase__     :str=None  ,     ):
					UpperCamelCase__ :Optional[Any]						=      parent
					UpperCamelCase__ :Dict						=      batch_size
					UpperCamelCase__ :Tuple						=      seq_length
					UpperCamelCase__ :Dict						=      is_training
					UpperCamelCase__ :List[str]						=      use_input_mask
					UpperCamelCase__ :Optional[Any]						=      use_token_type_ids
					UpperCamelCase__ :Tuple						=      use_labels
					UpperCamelCase__ :int						=      vocab_size
					UpperCamelCase__ :Tuple						=      hidden_size
					UpperCamelCase__ :Optional[Any]						=      num_hidden_layers
					UpperCamelCase__ :int						=      num_attention_heads
					UpperCamelCase__ :Optional[int]						=      intermediate_multiple_size
					UpperCamelCase__ :Optional[Any]						=      hidden_act
					UpperCamelCase__ :Optional[int]						=      hidden_dropout
					UpperCamelCase__ :List[Any]						=      attention_dropout
					UpperCamelCase__ :List[str]						=      weight_tying
					UpperCamelCase__ :List[str]						=      max_position_embeddings
					UpperCamelCase__ :Dict						=      type_vocab_size
					UpperCamelCase__ :List[Any]						=      type_sequence_label_size
					UpperCamelCase__ :List[str]						=      initializer_range
					UpperCamelCase__ :int						=      num_labels
					UpperCamelCase__ :Dict						=      num_choices
					UpperCamelCase__ :Any						=      scope







				def 				__a   ( self     :Any     ):
					UpperCamelCase__ :Tuple						=      ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size     )

					UpperCamelCase__ :str						=      None
					if self.use_input_mask:
						UpperCamelCase__ :Optional[int]						=      random_attention_mask([self.batch_size, self.seq_length]     )

					UpperCamelCase__ :Union[str, Any]						=      None
					if self.use_labels:
						UpperCamelCase__ :Tuple						=      ids_tensor([self.batch_size, self.seq_length]  ,     self.num_labels     )

					UpperCamelCase__ :Optional[Any]						=      self.get_config()

					return config, input_ids, input_mask, token_labels







				def 				__a   ( self     :Union[str, Any]     ):
					return GPTNeoXJapaneseConfig(
					    vocab_size=self.vocab_size  ,     hidden_size=self.hidden_size  ,     num_hidden_layers=self.num_hidden_layers  ,     num_attention_heads=self.num_attention_heads  ,     intermediate_multiple_size=self.intermediate_multiple_size  ,     hidden_act=self.hidden_act  ,     hidden_dropout=self.hidden_dropout  ,     attention_dropout=self.attention_dropout  ,     weight_tying=self.weight_tying  ,     max_position_embeddings=self.max_position_embeddings  ,     type_vocab_size=self.type_vocab_size  ,     is_decoder=lowerCamelCase__  ,     initializer_range=self.initializer_range  ,     )







				def 				__a   ( self     :Union[str, Any]     ):
					UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__ :Optional[Any]						=      self.prepare_config_and_inputs()

					UpperCamelCase__ :Optional[int]						=      True

					return config, input_ids, input_mask, token_labels







				def 				__a   ( self     :List[str]  ,     lowerCamelCase__     :List[Any]  ,     lowerCamelCase__     :Any  ,     lowerCamelCase__     :Any     ):
					UpperCamelCase__ :Union[str, Any]						=      GPTNeoXJapaneseModel(config=lowerCamelCase__     )
					model.to(lowerCamelCase__     )
					model.eval()
					UpperCamelCase__ :Tuple						=      model(lowerCamelCase__  ,     attention_mask=lowerCamelCase__     )
					UpperCamelCase__ :Optional[Any]						=      model(lowerCamelCase__     )
					self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)     )







				def 				__a   ( self     :Dict  ,     lowerCamelCase__     :Dict  ,     lowerCamelCase__     :Optional[Any]  ,     lowerCamelCase__     :List[Any]     ):
					UpperCamelCase__ :List[str]						=      True
					UpperCamelCase__ :int						=      GPTNeoXJapaneseModel(lowerCamelCase__     )
					model.to(lowerCamelCase__     )
					model.eval()
					UpperCamelCase__ :Union[str, Any]						=      model(lowerCamelCase__  ,     attention_mask=lowerCamelCase__     )
					self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)     )







				def 				__a   ( self     :List[Any]  ,     lowerCamelCase__     :Tuple  ,     lowerCamelCase__     :Optional[Any]  ,     lowerCamelCase__     :Any  ,     lowerCamelCase__     :Optional[Any]     ):
					UpperCamelCase__ :Any						=      GPTNeoXJapaneseForCausalLM(config=lowerCamelCase__     )
					model.to(lowerCamelCase__     )
					model.eval()
					UpperCamelCase__ :Tuple						=      model(lowerCamelCase__  ,     attention_mask=lowerCamelCase__  ,     labels=lowerCamelCase__     )
					self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)     )







				def 				__a   ( self     :Any  ,     lowerCamelCase__     :Optional[int]  ,     lowerCamelCase__     :Optional[Any]  ,     lowerCamelCase__     :List[str]     ):
					UpperCamelCase__ :Union[str, Any]						=      True
					UpperCamelCase__ :List[str]						=      GPTNeoXJapaneseForCausalLM(config=lowerCamelCase__     )
					model.to(lowerCamelCase__     )
					model.eval()

					# first forward pass
					UpperCamelCase__ :Optional[Any]						=      model(lowerCamelCase__  ,     attention_mask=lowerCamelCase__  ,     use_cache=lowerCamelCase__     )
					UpperCamelCase__ :List[Any]						=      outputs.past_key_values

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

					# append to next input_ids and
					UpperCamelCase__ :Union[str, Any]						=      torch.cat([input_ids, next_tokens]  ,     dim=-1     )
					UpperCamelCase__ :Optional[int]						=      torch.cat([input_mask, next_mask]  ,     dim=-1     )

					UpperCamelCase__ :Union[str, Any]						=      model(lowerCamelCase__  ,     attention_mask=lowerCamelCase__  ,     output_hidden_states=lowerCamelCase__     )
					UpperCamelCase__ :Optional[int]						=      output_from_no_past["""hidden_states"""][0]
					UpperCamelCase__ :Union[str, Any]						=      model(
					    lowerCamelCase__  ,     attention_mask=lowerCamelCase__  ,     past_key_values=lowerCamelCase__  ,     output_hidden_states=lowerCamelCase__  ,     )["""hidden_states"""][0]

					# select random slice
					UpperCamelCase__ :int						=      ids_tensor((1,)  ,     output_from_past.shape[-1]     ).item()
					UpperCamelCase__ :str						=      output_from_no_past[:, -3:, random_slice_idx].detach()
					UpperCamelCase__ :Any						=      output_from_past[:, :, random_slice_idx].detach()

					self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]     )

					# test that outputs are equal for slice
					self.parent.assertTrue(torch.allclose(lowerCamelCase__  ,     lowerCamelCase__  ,     atol=1e-3     )     )







				def 				__a   ( self     :Tuple     ):
					UpperCamelCase__ :int						=      self.prepare_config_and_inputs()
					UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__ :List[Any]						=      config_and_inputs
					UpperCamelCase__ :Any						=      {"""input_ids""": input_ids, """attention_mask""": input_mask}
					return config, inputs_dict

@require_torch
class        lowerCAmelCase_				(      lowercase						,      lowercase						,      unittest.TestCase	):







				"""simple docstring"""




				_snake_case				:   Dict        =			(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
				_snake_case				:   int        =			(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
				_snake_case				:   str        =			(
				    {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM}
				    if is_torch_available()
				    else {}
				)
				_snake_case				:   Union[str, Any]        =			False
				_snake_case				:   Dict        =			False
				_snake_case				:   List[str]        =			False
				_snake_case				:   Optional[int]        =			False







				def 				__a   ( self     :List[Any]     ):
					UpperCamelCase__ :Tuple						=      GPTNeoXJapaneseModelTester(self     )
					UpperCamelCase__ :Optional[Any]						=      ConfigTester(self  ,     config_class=lowerCamelCase__  ,     hidden_size=37     )







				def 				__a   ( self     :Dict     ):
					self.config_tester.run_common_tests()







				def 				__a   ( self     :Any     ):
					UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__ :Optional[Any]						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(lowerCamelCase__  ,     lowerCamelCase__  ,     lowerCamelCase__     )







				def 				__a   ( self     :Any     ):
					UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__ :Union[str, Any]						=      self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_model_as_decoder(lowerCamelCase__  ,     lowerCamelCase__  ,     lowerCamelCase__     )







				def 				__a   ( self     :Union[str, Any]     ):
					# This regression test was failing with PyTorch < 1.3
					UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__ :List[str]						=      self.model_tester.prepare_config_and_inputs_for_decoder()

					UpperCamelCase__ :Dict						=      None

					self.model_tester.create_and_check_model_as_decoder(lowerCamelCase__  ,     lowerCamelCase__  ,     lowerCamelCase__     )







				def 				__a   ( self     :List[str]     ):
					UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__				,		UpperCamelCase__ :int						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCamelCase__  ,     lowerCamelCase__  ,     lowerCamelCase__     )







				def 				__a   ( self     :Union[str, Any]     ):
					UpperCamelCase__ :int						=      self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_causal_lm(*lowerCamelCase__     )







				@slow
				def 				__a   ( self     :int     ):
					UpperCamelCase__ :int						=      """abeja/gpt-neox-japanese-2.7b"""

					UpperCamelCase__ :List[Any]						=      ["""データサイエンティストとは、""", """100年後に必要とされる会社は、""", """フルリモートの環境で働くために必要なことは、""", """国境の長いトンネルを抜けると""", """美味しい日本食といえば、"""]

					UpperCamelCase__ :Union[str, Any]						=      [
					    """データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。""",
					    """100年後に必要とされる会社は、「人」が中心の会社です。""",
					    """フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。""",
					    """国境の長いトンネルを抜けると、そこは雪国だった。""",
					    """美味しい日本食といえば、やっぱりお寿司ですよね。""",
					]

					UpperCamelCase__ :Any						=      GPTNeoXJapaneseTokenizer.from_pretrained(lowerCamelCase__     )
					UpperCamelCase__ :List[Any]						=      GPTNeoXJapaneseForCausalLM.from_pretrained(lowerCamelCase__     )

					UpperCamelCase__ :Optional[Any]						=      []
					for prompt in prompts:
						UpperCamelCase__ :str						=      tokenizer(lowerCamelCase__  ,     return_tensors="""pt"""     ).input_ids
						UpperCamelCase__ :Union[str, Any]						=      model.generate(lowerCamelCase__  ,     max_length=50     )
						UpperCamelCase__ :Dict						=      tokenizer.batch_decode(lowerCamelCase__  ,     skip_special_tokens=lowerCamelCase__     )
						predicted_outputs += generated_string
					self.assertListEqual(lowerCamelCase__  ,     lowerCamelCase__     )


import os
import tempfile
import unittest

import numpy as np

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax, slow


if is_flax_available():
      import jax
      import jax.numpy as jnp
      from flax.jax_utils import replicate
      from flax.training.common_utils import shard

      from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline




@require_flax
class   UpperCamelCase			(  unittest.TestCase ):

    def UpperCamelCase      (      self			:   Tuple     ):

     """simple docstring"""






     with tempfile.TemporaryDirectory() as tmpdirname:
      # pipeline has Flax weights
      SCREAMING_SNAKE_CASE		    =      FlaxDiffusionPipeline.from_pretrained(
          'hf-internal-testing/tiny-stable-diffusion-pipe'				,     safety_checker=snake_case__				,     cache_dir=snake_case__     )

      SCREAMING_SNAKE_CASE		    =      [t[-1] for t in os.walk(os.path.join(snake_case__				,     os.listdir(snake_case__     )[0]				,     'snapshots'     )     )]
      SCREAMING_SNAKE_CASE		    =      [item for sublist in all_root_files for item in sublist]

      # None of the downloaded files should be a PyTorch file even if we have some here:
      # https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin
      assert not any(f.endswith('.bin'     ) for f in files     )




@slow
@require_flax
class   UpperCamelCase			(  unittest.TestCase ):

    def UpperCamelCase      (      self			:   Optional[Any]     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE		    =      FlaxStableDiffusionPipeline.from_pretrained(
         'hf-internal-testing/tiny-stable-diffusion-pipe'				,     safety_checker=snake_case__     )

     SCREAMING_SNAKE_CASE		    =      (
         'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'
         ' field, close up, split lighting, cinematic'
     )

     SCREAMING_SNAKE_CASE		    =      jax.random.PRNGKey(0     )
     SCREAMING_SNAKE_CASE		    =      4

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )

     # shard inputs and rng
     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      jax.random.split(snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images

     assert images.shape == (num_samples, 1, 6_4, 6_4, 3)
     if jax.device_count() == 8:
      assert np.abs(np.abs(images[0, 0, :2, :2, -2:]				,     dtype=np.floataa     ).sum() - 4.1_514_745     ) < 1E-3
      assert np.abs(np.abs(snake_case__				,     dtype=np.floataa     ).sum() - 49_947.875     ) < 5E-1

     SCREAMING_SNAKE_CASE		    =      pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]     )     )     )
     assert len(snake_case__     ) == num_samples

    def UpperCamelCase      (      self			:   Union[str, Any]     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE		    =      FlaxStableDiffusionPipeline.from_pretrained(
         'CompVis/stable-diffusion-v1-4'				,     revision='flax'				,     safety_checker=snake_case__     )

     SCREAMING_SNAKE_CASE		    =      (
         'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'
         ' field, close up, split lighting, cinematic'
     )

     SCREAMING_SNAKE_CASE		    =      jax.random.PRNGKey(0     )
     SCREAMING_SNAKE_CASE		    =      5_0

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )

     # shard inputs and rng
     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      jax.random.split(snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images

     assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     if jax.device_count() == 8:
      assert np.abs((np.abs(images[0, 0, :2, :2, -2:]				,     dtype=np.floataa     ).sum() - 0.05_652_401)     ) < 1E-3
      assert np.abs((np.abs(snake_case__				,     dtype=np.floataa     ).sum() - 2_383_808.2)     ) < 5E-1

    def UpperCamelCase      (      self			:   Optional[Any]     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE		    =      FlaxStableDiffusionPipeline.from_pretrained(
         'CompVis/stable-diffusion-v1-4'				,     revision='bf16'				,     dtype=jnp.bfloataa				,     safety_checker=snake_case__     )

     SCREAMING_SNAKE_CASE		    =      (
         'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'
         ' field, close up, split lighting, cinematic'
     )

     SCREAMING_SNAKE_CASE		    =      jax.random.PRNGKey(0     )
     SCREAMING_SNAKE_CASE		    =      5_0

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )

     # shard inputs and rng
     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      jax.random.split(snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images

     assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     if jax.device_count() == 8:
      assert np.abs((np.abs(images[0, 0, :2, :2, -2:]				,     dtype=np.floataa     ).sum() - 0.04_003_906)     ) < 1E-3
      assert np.abs((np.abs(snake_case__				,     dtype=np.floataa     ).sum() - 2_373_516.75)     ) < 5E-1

    def UpperCamelCase      (      self			:   List[Any]     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE		    =      FlaxStableDiffusionPipeline.from_pretrained(
         'CompVis/stable-diffusion-v1-4'				,     revision='bf16'				,     dtype=jnp.bfloataa     )

     SCREAMING_SNAKE_CASE		    =      (
         'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'
         ' field, close up, split lighting, cinematic'
     )

     SCREAMING_SNAKE_CASE		    =      jax.random.PRNGKey(0     )
     SCREAMING_SNAKE_CASE		    =      5_0

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )

     # shard inputs and rng
     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      jax.random.split(snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images

     assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     if jax.device_count() == 8:
      assert np.abs((np.abs(images[0, 0, :2, :2, -2:]				,     dtype=np.floataa     ).sum() - 0.04_003_906)     ) < 1E-3
      assert np.abs((np.abs(snake_case__				,     dtype=np.floataa     ).sum() - 2_373_516.75)     ) < 5E-1

    def UpperCamelCase      (      self			:   Dict     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      FlaxDDIMScheduler(
         beta_start=0.00_085				,     beta_end=0.012				,     beta_schedule='scaled_linear'				,     set_alpha_to_one=snake_case__				,     steps_offset=1				,     )

     SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE		    =      FlaxStableDiffusionPipeline.from_pretrained(
         'CompVis/stable-diffusion-v1-4'				,     revision='bf16'				,     dtype=jnp.bfloataa				,     scheduler=snake_case__				,     safety_checker=snake_case__				,     )
     SCREAMING_SNAKE_CASE		    =      scheduler.create_state()

     SCREAMING_SNAKE_CASE		    =      scheduler_state

     SCREAMING_SNAKE_CASE		    =      (
         'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'
         ' field, close up, split lighting, cinematic'
     )

     SCREAMING_SNAKE_CASE		    =      jax.random.PRNGKey(0     )
     SCREAMING_SNAKE_CASE		    =      5_0

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )

     # shard inputs and rng
     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      jax.random.split(snake_case__				,     snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )

     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images

     assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     if jax.device_count() == 8:
      assert np.abs((np.abs(images[0, 0, :2, :2, -2:]				,     dtype=np.floataa     ).sum() - 0.045_043_945)     ) < 1E-3
      assert np.abs((np.abs(snake_case__				,     dtype=np.floataa     ).sum() - 2_347_693.5)     ) < 5E-1




    def UpperCamelCase      (      self			:   Tuple     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      (
         'A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of'
         ' field, close up, split lighting, cinematic'
     )

     SCREAMING_SNAKE_CASE		    =      jax.device_count()
     SCREAMING_SNAKE_CASE		    =      num_samples * [prompt]
     SCREAMING_SNAKE_CASE		    =      jax.random.split(jax.random.PRNGKey(0     )				,     snake_case__     )

     SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE		    =      FlaxStableDiffusionPipeline.from_pretrained(
         'CompVis/stable-diffusion-v1-4'				,     revision='bf16'				,     dtype=jnp.bfloataa				,     safety_checker=snake_case__				,     )

     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images
     assert images.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     SCREAMING_SNAKE_CASE		    =      images[2, 0, 2_5_6, 1_0:1_7, 1]

     # With memory efficient attention
     SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE		    =      FlaxStableDiffusionPipeline.from_pretrained(
         'CompVis/stable-diffusion-v1-4'				,     revision='bf16'				,     dtype=jnp.bfloataa				,     safety_checker=snake_case__				,     use_memory_efficient_attention=snake_case__				,     )

     SCREAMING_SNAKE_CASE		    =      replicate(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      pipeline.prepare_inputs(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      shard(snake_case__     )
     SCREAMING_SNAKE_CASE		    =      pipeline(snake_case__				,     snake_case__				,     snake_case__				,     jit=snake_case__     ).images
     assert images_eff.shape == (num_samples, 1, 5_1_2, 5_1_2, 3)
     SCREAMING_SNAKE_CASE		    =      images[2, 0, 2_5_6, 1_0:1_7, 1]

     # I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum`
     # over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now.
     assert abs(slice_eff - slice     ).max() < 1E-2

