


import argparse
import collections

import numpy as np
import torch
from flax import traverse_util
from tax import checkpoints

from transformers import MTaConfig, UMTaEncoderModel, UMTaForConditionalGeneration
from transformers.utils import logging


logging.set_verbosity_info()







def 							__magic_name__    (     lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_):
	'''simple docstring'''


	return params[F"""{prefix}/{prefix}/relpos_bias/rel_embedding"""][:, i, :]







def 							__magic_name__    (     lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_="attention"):
	'''simple docstring'''


	lowerCamelCase_					:				Optional[int]              =  np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/key/kernel"""][:, i, :, :])
	lowerCamelCase_					:				int              =  k_tmp.reshape(k_tmp.shape[0]     ,	k_tmp.shape[1] * k_tmp.shape[2])
	lowerCamelCase_					:				Tuple              =  np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/out/kernel"""][:, i, :, :])
	lowerCamelCase_					:				List[Any]              =  o_tmp.reshape(o_tmp.shape[0] * o_tmp.shape[1]     ,	o_tmp.shape[2])
	lowerCamelCase_					:				int              =  np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/query/kernel"""][:, i, :, :])
	lowerCamelCase_					:				List[Any]              =  q_tmp.reshape(q_tmp.shape[0]     ,	q_tmp.shape[1] * q_tmp.shape[2])
	lowerCamelCase_					:				Optional[Any]              =  np.ascontiguousarray(params[F"""{prefix}/{prefix}/{layer_name}/value/kernel"""][:, i, :, :])
	lowerCamelCase_					:				int              =  v_tmp.reshape(v_tmp.shape[0]     ,	v_tmp.shape[1] * v_tmp.shape[2])
	return k, o, q, v







def 							__magic_name__    (     lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_=False):
	'''simple docstring'''


	if split_mlp_wi:
		lowerCamelCase_					:				str              =  params[F"""{prefix}/{prefix}/mlp/wi_0/kernel"""][:, i, :]
		lowerCamelCase_					:				Union[str, Any]              =  params[F"""{prefix}/{prefix}/mlp/wi_1/kernel"""][:, i, :]
		lowerCamelCase_					:				List[Any]              =  (wi_a, wi_a)
	else:
		lowerCamelCase_					:				Dict              =  params[F"""{prefix}/{prefix}/mlp/wi/kernel"""][:, i, :]

	lowerCamelCase_					:				Union[str, Any]              =  params[F"""{prefix}/{prefix}/mlp/wo/kernel"""][:, i, :]
	return wi, wo







def 							__magic_name__    (     lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_):
	'''simple docstring'''


	return params[F"""{prefix}/{prefix}/{layer_name}/scale"""][:, i]







def 							__magic_name__    (     lowerCAmelCase_     ,	*, lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_ = False):
	'''simple docstring'''


	lowerCamelCase_					:				Any              =  traverse_util.flatten_dict(variables["target"])
	lowerCamelCase_					:				List[str]              =  {"/".join(_UpperCamelCase): v for k, v in old.items()}

	# v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi
	lowerCamelCase_					:				Union[str, Any]              =  "encoder/encoder/mlp/wi_0/kernel" in old
	print("Split MLP:"     ,	_UpperCamelCase)

	lowerCamelCase_					:				Optional[int]              =  collections.OrderedDict()

	# Shared embeddings.
	lowerCamelCase_					:				Union[str, Any]              =  old["token_embedder/embedding"]

	# Encoder.
	for i in range(_UpperCamelCase):
		# Block i, layer 0 (Self Attention).
		lowerCamelCase_					:				Optional[int]              =  tax_layer_norm_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"encoder"     ,	"pre_attention_layer_norm")
		lowerCamelCase_       ,lowerCamelCase_       ,lowerCamelCase_       ,lowerCamelCase_					:				Optional[Any]              =  tax_attention_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"encoder"     ,	"attention")
		lowerCamelCase_					:				Dict              =  layer_norm
		lowerCamelCase_					:				Union[str, Any]              =  k.T
		lowerCamelCase_					:				Union[str, Any]              =  o.T
		lowerCamelCase_					:				List[str]              =  q.T
		lowerCamelCase_					:				int              =  v.T

		# Block i, layer 1 (MLP).
		lowerCamelCase_					:				List[str]              =  tax_layer_norm_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"encoder"     ,	"pre_mlp_layer_norm")
		lowerCamelCase_       ,lowerCamelCase_					:				Tuple              =  tax_mlp_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"encoder"     ,	_UpperCamelCase)
		lowerCamelCase_					:				Any              =  layer_norm
		if split_mlp_wi:
			lowerCamelCase_					:				List[Any]              =  wi[0].T
			lowerCamelCase_					:				Optional[Any]              =  wi[1].T
		else:
			lowerCamelCase_					:				Tuple              =  wi.T
		lowerCamelCase_					:				List[str]              =  wo.T
		if scalable_attention:
			# convert the rel_embedding of each layer
			lowerCamelCase_					:				Optional[int]              =  tax_relpos_bias_lookup(
			    _UpperCamelCase     ,	_UpperCamelCase     ,	"encoder").T

	lowerCamelCase_					:				List[Any]              =  old["encoder/encoder_norm/scale"]

	if not scalable_attention:
		lowerCamelCase_					:				Optional[Any]              =  tax_relpos_bias_lookup(
		    _UpperCamelCase     ,	0     ,	"encoder").T
		lowerCamelCase_					:				Optional[Any]              =  tax_relpos_bias_lookup(
		    _UpperCamelCase     ,	0     ,	"decoder").T

	if not is_encoder_only:
		# Decoder.
		for i in range(_UpperCamelCase):
			# Block i, layer 0 (Self Attention).
			lowerCamelCase_					:				Optional[Any]              =  tax_layer_norm_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"decoder"     ,	"pre_self_attention_layer_norm")
			lowerCamelCase_       ,lowerCamelCase_       ,lowerCamelCase_       ,lowerCamelCase_					:				Any              =  tax_attention_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"decoder"     ,	"self_attention")
			lowerCamelCase_					:				Union[str, Any]              =  layer_norm
			lowerCamelCase_					:				Dict              =  k.T
			lowerCamelCase_					:				Dict              =  o.T
			lowerCamelCase_					:				List[str]              =  q.T
			lowerCamelCase_					:				Union[str, Any]              =  v.T

			# Block i, layer 1 (Cross Attention).
			lowerCamelCase_					:				Tuple              =  tax_layer_norm_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"decoder"     ,	"pre_cross_attention_layer_norm")
			lowerCamelCase_       ,lowerCamelCase_       ,lowerCamelCase_       ,lowerCamelCase_					:				Union[str, Any]              =  tax_attention_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"decoder"     ,	"encoder_decoder_attention")
			lowerCamelCase_					:				int              =  layer_norm
			lowerCamelCase_					:				List[str]              =  k.T
			lowerCamelCase_					:				Union[str, Any]              =  o.T
			lowerCamelCase_					:				Optional[int]              =  q.T
			lowerCamelCase_					:				List[Any]              =  v.T

			# Block i, layer 2 (MLP).
			lowerCamelCase_					:				Tuple              =  tax_layer_norm_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"decoder"     ,	"pre_mlp_layer_norm")
			lowerCamelCase_       ,lowerCamelCase_					:				Optional[int]              =  tax_mlp_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"decoder"     ,	_UpperCamelCase)
			lowerCamelCase_					:				int              =  layer_norm
			if split_mlp_wi:
				lowerCamelCase_					:				Optional[Any]              =  wi[0].T
				lowerCamelCase_					:				int              =  wi[1].T
			else:
				lowerCamelCase_					:				List[str]              =  wi.T
			lowerCamelCase_					:				Union[str, Any]              =  wo.T

			if scalable_attention:
				# convert the rel_embedding of each layer
				lowerCamelCase_					:				Optional[int]              =  tax_relpos_bias_lookup(_UpperCamelCase     ,	_UpperCamelCase     ,	"decoder").T

		lowerCamelCase_					:				Optional[Any]              =  old["decoder/decoder_norm/scale"]

		# LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead)
		if "decoder/logits_dense/kernel" in old:
			lowerCamelCase_					:				Dict              =  old["decoder/logits_dense/kernel"].T

	return new







def 							__magic_name__    (     lowerCAmelCase_     ,	lowerCAmelCase_):
	'''simple docstring'''


	lowerCamelCase_					:				List[str]              =  collections.OrderedDict([(k, torch.from_numpy(v.copy())) for (k, v) in converted_params.items()])

	# Add what is missing.
	if "encoder.embed_tokens.weight" not in state_dict:
		lowerCamelCase_					:				Union[str, Any]              =  state_dict["shared.weight"]

	if not is_encoder_only:
		if "decoder.embed_tokens.weight" not in state_dict:
			lowerCamelCase_					:				str              =  state_dict["shared.weight"]

		if "lm_head.weight" not in state_dict:  # For old 1.0 models.
			print("Using shared word embeddings as lm_head.")
			lowerCamelCase_					:				Any              =  state_dict["shared.weight"]

	return state_dict







def 							__magic_name__    (     lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_):
	'''simple docstring'''


	lowerCamelCase_					:				List[Any]              =  checkpoints.load_tax_checkpoint(_UpperCamelCase)
	lowerCamelCase_					:				Dict              =  convert_tax_to_pytorch(
	    _UpperCamelCase     ,	num_layers=config.num_layers     ,	is_encoder_only=_UpperCamelCase     ,	scalable_attention=_UpperCamelCase)
	lowerCamelCase_					:				Dict              =  make_state_dict(_UpperCamelCase     ,	_UpperCamelCase)
	model.load_state_dict(_UpperCamelCase     ,	strict=_UpperCamelCase)







def 							__magic_name__    (     lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_     ,	lowerCAmelCase_ = False     ,	lowerCAmelCase_ = False     ,	):
	'''simple docstring'''


	lowerCamelCase_					:				int              =  MTaConfig.from_json_file(_UpperCamelCase)
	print(F"""Building PyTorch model from configuration: {config}""")
	# Non-v1.1 checkpoints could also use T5Model, but this works for all.
	# The v1.0 checkpoints will simply have an LM head that is the word embeddings.
	if is_encoder_only:
		lowerCamelCase_					:				Tuple              =  UMTaEncoderModel(_UpperCamelCase)
	else:
		lowerCamelCase_					:				Union[str, Any]              =  UMTaForConditionalGeneration(_UpperCamelCase)

	# Load weights from tf checkpoint
	load_tax_weights_in_ta(_UpperCamelCase     ,	_UpperCamelCase     ,	_UpperCamelCase     ,	_UpperCamelCase     ,	_UpperCamelCase)

	# Save pytorch-model
	print(F"""Save PyTorch model to {pytorch_dump_path}""")
	model.save_pretrained(_UpperCamelCase)

	# Verify that we can load the checkpoint.
	model.from_pretrained(_UpperCamelCase)
	print("Done")


if __name__ == "__main__":
							__magic_name__  	=     argparse.ArgumentParser(description='''Converts a native T5X checkpoint into a PyTorch checkpoint.''')
							# Required parameters
							parser.add_argument(
							    '''--t5x_checkpoint_path''', default=None, type=str, required=True, help='''Path to the T5X checkpoint.'''
							)
							parser.add_argument(
							    '''--config_file''',
							    default=None,
							    type=str,
							    required=True,
							    help='''The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.''',
							)
							parser.add_argument(
							    '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
							)
							parser.add_argument(
							    '''--is_encoder_only''', action='''store_true''', help='''Check if the model is encoder-decoder model''', default=False
							)
							parser.add_argument(
							    '''--scalable_attention''',
							    action='''store_true''',
							    help='''Whether the model uses scaled attention (umt5 model)''',
							    default=False,
							)
							__magic_name__  	=     parser.parse_args()
							convert_tax_checkpoint_to_pytorch(
							    args.tax_checkpoint_path,
							    args.config_file,
							    args.pytorch_dump_path,
							    args.is_encoder_only,
							    args.scalable_attention,
							)




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

