



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  __lowerCAmelCase     (	unittest.TestCase   ):




 def        lowerCamelCase      (						self				):



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

       __lowerCamelCase													=    [t[-1] for t in os.walk(os.path.join(__UpperCAmelCase	,				os.listdir(__UpperCAmelCase				)[0]	,				'''snapshots'''				)				)]
       __lowerCamelCase													=    [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  __lowerCAmelCase     (	unittest.TestCase   ):




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase							,__lowerCamelCase													=    FlaxStableDiffusionPipeline.from_pretrained(
        '''hf-internal-testing/tiny-stable-diffusion-pipe'''	,				safety_checker=__UpperCAmelCase				)

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

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

    __lowerCamelCase													=    jax.device_count()
    __lowerCamelCase													=    num_samples * [prompt]
    __lowerCamelCase													=    pipeline.prepare_inputs(__UpperCAmelCase				)

    # shard inputs and rng
    __lowerCamelCase													=    replicate(__UpperCAmelCase				)
    __lowerCamelCase													=    jax.random.split(__UpperCAmelCase	,				__UpperCAmelCase				)
    __lowerCamelCase													=    shard(__UpperCAmelCase				)

    __lowerCamelCase													=    pipeline(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				jit=__UpperCAmelCase				).images

    assert images.shape == (num_samples, 1, 64, 64, 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(__UpperCAmelCase	,				dtype=np.floataa				).sum() - 49_947.875				) < 5E-1

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




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase							,__lowerCamelCase													=    FlaxStableDiffusionPipeline.from_pretrained(
        '''CompVis/stable-diffusion-v1-4'''	,				revision='''flax'''	,				safety_checker=__UpperCAmelCase				)

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

    __lowerCamelCase													=    jax.random.PRNGKey(0				)
    __lowerCamelCase													=    50

    __lowerCamelCase													=    jax.device_count()
    __lowerCamelCase													=    num_samples * [prompt]
    __lowerCamelCase													=    pipeline.prepare_inputs(__UpperCAmelCase				)

    # shard inputs and rng
    __lowerCamelCase													=    replicate(__UpperCAmelCase				)
    __lowerCamelCase													=    jax.random.split(__UpperCAmelCase	,				__UpperCAmelCase				)
    __lowerCamelCase													=    shard(__UpperCAmelCase				)

    __lowerCamelCase													=    pipeline(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				jit=__UpperCAmelCase				).images

    assert images.shape == (num_samples, 1, 512, 512, 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(__UpperCAmelCase	,				dtype=np.floataa				).sum() - 2_383_808.2)				) < 5E-1




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase							,__lowerCamelCase													=    FlaxStableDiffusionPipeline.from_pretrained(
        '''CompVis/stable-diffusion-v1-4'''	,				revision='''bf16'''	,				dtype=jnp.bfloataa	,				safety_checker=__UpperCAmelCase				)

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

    __lowerCamelCase													=    jax.random.PRNGKey(0				)
    __lowerCamelCase													=    50

    __lowerCamelCase													=    jax.device_count()
    __lowerCamelCase													=    num_samples * [prompt]
    __lowerCamelCase													=    pipeline.prepare_inputs(__UpperCAmelCase				)

    # shard inputs and rng
    __lowerCamelCase													=    replicate(__UpperCAmelCase				)
    __lowerCamelCase													=    jax.random.split(__UpperCAmelCase	,				__UpperCAmelCase				)
    __lowerCamelCase													=    shard(__UpperCAmelCase				)

    __lowerCamelCase													=    pipeline(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				jit=__UpperCAmelCase				).images

    assert images.shape == (num_samples, 1, 512, 512, 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(__UpperCAmelCase	,				dtype=np.floataa				).sum() - 2_373_516.75)				) < 5E-1




 def        lowerCamelCase      (						self				):



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

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

    __lowerCamelCase													=    jax.random.PRNGKey(0				)
    __lowerCamelCase													=    50

    __lowerCamelCase													=    jax.device_count()
    __lowerCamelCase													=    num_samples * [prompt]
    __lowerCamelCase													=    pipeline.prepare_inputs(__UpperCAmelCase				)

    # shard inputs and rng
    __lowerCamelCase													=    replicate(__UpperCAmelCase				)
    __lowerCamelCase													=    jax.random.split(__UpperCAmelCase	,				__UpperCAmelCase				)
    __lowerCamelCase													=    shard(__UpperCAmelCase				)

    __lowerCamelCase													=    pipeline(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				jit=__UpperCAmelCase				).images

    assert images.shape == (num_samples, 1, 512, 512, 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(__UpperCAmelCase	,				dtype=np.floataa				).sum() - 2_373_516.75)				) < 5E-1




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    FlaxDDIMScheduler(
        beta_start=0.00_085	,				beta_end=0.012	,				beta_schedule='''scaled_linear'''	,				set_alpha_to_one=__UpperCAmelCase	,				steps_offset=1	,				)

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

    __lowerCamelCase													=    scheduler_state

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

    __lowerCamelCase													=    jax.random.PRNGKey(0				)
    __lowerCamelCase													=    50

    __lowerCamelCase													=    jax.device_count()
    __lowerCamelCase													=    num_samples * [prompt]
    __lowerCamelCase													=    pipeline.prepare_inputs(__UpperCAmelCase				)

    # shard inputs and rng
    __lowerCamelCase													=    replicate(__UpperCAmelCase				)
    __lowerCamelCase													=    jax.random.split(__UpperCAmelCase	,				__UpperCAmelCase				)
    __lowerCamelCase													=    shard(__UpperCAmelCase				)

    __lowerCamelCase													=    pipeline(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				jit=__UpperCAmelCase				).images

    assert images.shape == (num_samples, 1, 512, 512, 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(__UpperCAmelCase	,				dtype=np.floataa				).sum() - 2_347_693.5)				) < 5E-1




 def        lowerCamelCase      (						self				):



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

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

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

    __lowerCamelCase													=    replicate(__UpperCAmelCase				)
    __lowerCamelCase													=    pipeline.prepare_inputs(__UpperCAmelCase				)
    __lowerCamelCase													=    shard(__UpperCAmelCase				)
    __lowerCamelCase													=    pipeline(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				jit=__UpperCAmelCase				).images
    assert images.shape == (num_samples, 1, 512, 512, 3)
    __lowerCamelCase													=    images[2, 0, 256, 10:17, 1]

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

    __lowerCamelCase													=    replicate(__UpperCAmelCase				)
    __lowerCamelCase													=    pipeline.prepare_inputs(__UpperCAmelCase				)
    __lowerCamelCase													=    shard(__UpperCAmelCase				)
    __lowerCamelCase													=    pipeline(__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase	,				jit=__UpperCAmelCase				).images
    assert images_eff.shape == (num_samples, 1, 512, 512, 3)
    __lowerCamelCase													=    images[2, 0, 256, 10:17, 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











import math
import os
from copy import deepcopy

import datasets
import evaluate
import torch
import transformers
from datasets import load_dataset
from torch.utils.data import DataLoader
from transformers import AutoModelForSequenceClassification, AutoTokenizer

from accelerate import Accelerator
from accelerate.test_utils import RegressionDataset, RegressionModel
from accelerate.utils import is_tpu_available, set_seed


a_			=					"""true"""





def 		a__			(		_UpperCamelCase			: Union[str, Any] ,_UpperCamelCase			: List[str]=82 ,_UpperCamelCase			: Optional[Any]=16							):
   set_seed(42							)
   __lowerCamelCase													=    RegressionModel()
   __lowerCamelCase													=    deepcopy(_UpperCamelCase							)
   __lowerCamelCase													=    RegressionDataset(length=_UpperCamelCase							)
   __lowerCamelCase													=    DataLoader(_UpperCamelCase ,batch_size=_UpperCamelCase							)
   model.to(accelerator.device							)
   __lowerCamelCase							,__lowerCamelCase													=    accelerator.prepare(_UpperCamelCase ,_UpperCamelCase							)
   return model, ddp_model, dataloader





def 		a__			(		_UpperCamelCase			: Accelerator ,_UpperCamelCase			: str=False							):
   __lowerCamelCase													=    AutoTokenizer.from_pretrained('''hf-internal-testing/mrpc-bert-base-cased'''							)
   __lowerCamelCase													=    load_dataset('''glue''' ,'''mrpc''' ,split='''validation'''							)

   def tokenize_function(_UpperCamelCase			: int							):
      __lowerCamelCase													=    tokenizer(examples['''sentence1'''] ,examples['''sentence2'''] ,truncation=_UpperCamelCase ,max_length=_UpperCamelCase							)
      return outputs

   with accelerator.main_process_first():
      __lowerCamelCase													=    dataset.map(
          _UpperCamelCase ,batched=_UpperCamelCase ,remove_columns=['''idx''', '''sentence1''', '''sentence2'''] ,)

   __lowerCamelCase													=    tokenized_datasets.rename_column('''label''' ,'''labels'''							)

   def collate_fn(_UpperCamelCase			: Any							):
      if use_longest:
         return tokenizer.pad(_UpperCamelCase ,padding='''longest''' ,return_tensors='''pt'''							)
      return tokenizer.pad(_UpperCamelCase ,padding='''max_length''' ,max_length=1_28 ,return_tensors='''pt'''							)

   return DataLoader(_UpperCamelCase ,shuffle=_UpperCamelCase ,collate_fn=_UpperCamelCase ,batch_size=16							)





def 		a__			(		_UpperCamelCase			: Dict ,_UpperCamelCase			: List[str]							):
   __lowerCamelCase													=    Accelerator(dispatch_batches=_UpperCamelCase ,split_batches=_UpperCamelCase							)
   __lowerCamelCase													=    get_dataloader(_UpperCamelCase ,not dispatch_batches							)
   __lowerCamelCase													=    AutoModelForSequenceClassification.from_pretrained(
       '''hf-internal-testing/mrpc-bert-base-cased''' ,return_dict=_UpperCamelCase							)
   __lowerCamelCase							,__lowerCamelCase													=    accelerator.prepare(_UpperCamelCase ,_UpperCamelCase							)
   return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator





def 		a__			(		_UpperCamelCase			: Dict ,_UpperCamelCase			: Optional[Any] ,_UpperCamelCase			: Union[str, Any]							):
   __lowerCamelCase													=    []
   for batch in dataloader:
      __lowerCamelCase							,__lowerCamelCase													=    batch.values()
      with torch.no_grad():
         __lowerCamelCase													=    model(_UpperCamelCase							)
         __lowerCamelCase							,__lowerCamelCase													=    accelerator.gather_for_metrics((logit, target)							)
         logits_and_targets.append((logit, target)							)
   __lowerCamelCase							,__lowerCamelCase													=    [], []
   for logit, targ in logits_and_targets:
      logits.append(_UpperCamelCase							)
      targs.append(_UpperCamelCase							)
   __lowerCamelCase							,__lowerCamelCase													=    torch.cat(_UpperCamelCase							), torch.cat(_UpperCamelCase							)
   return logits, targs





def 		a__			(		_UpperCamelCase			: Accelerator ,_UpperCamelCase			: List[Any]=82 ,_UpperCamelCase			: str=False ,_UpperCamelCase			: List[str]=False ,_UpperCamelCase			: Optional[int]=16							):
   __lowerCamelCase							,__lowerCamelCase							,__lowerCamelCase													=    get_basic_setup(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase							)
   __lowerCamelCase							,__lowerCamelCase													=    generate_predictions(_UpperCamelCase ,_UpperCamelCase ,_UpperCamelCase							)
   assert (
       len(_UpperCamelCase							) == num_samples
   ), F"""Unexpected number of inputs:\n    Expected: {num_samples}\n    Actual: {len(_UpperCamelCase							)}"""





def 		a__			(		_UpperCamelCase			: bool = False ,_UpperCamelCase			: bool = False							):
   __lowerCamelCase													=    evaluate.load('''glue''' ,'''mrpc'''							)
   __lowerCamelCase							,__lowerCamelCase													=    get_mrpc_setup(_UpperCamelCase ,_UpperCamelCase							)
   # First do baseline
   __lowerCamelCase							,__lowerCamelCase							,__lowerCamelCase													=    setup['''no''']
   model.to(_UpperCamelCase							)
   model.eval()
   for batch in dataloader:
      batch.to(_UpperCamelCase							)
      with torch.inference_mode():
         __lowerCamelCase													=    model(**_UpperCamelCase							)
      __lowerCamelCase													=    outputs.logits.argmax(dim=-1							)
      metric.add_batch(predictions=_UpperCamelCase ,references=batch['''labels''']							)
   __lowerCamelCase													=    metric.compute()

   # Then do distributed
   __lowerCamelCase							,__lowerCamelCase							,__lowerCamelCase													=    setup['''ddp''']
   model.eval()
   for batch in dataloader:
      with torch.inference_mode():
         __lowerCamelCase													=    model(**_UpperCamelCase							)
      __lowerCamelCase													=    outputs.logits.argmax(dim=-1							)
      __lowerCamelCase													=    batch['''labels''']
      __lowerCamelCase							,__lowerCamelCase													=    accelerator.gather_for_metrics((preds, references)							)
      metric.add_batch(predictions=_UpperCamelCase ,references=_UpperCamelCase							)
   __lowerCamelCase													=    metric.compute()

   for key in "accuracy f1".split():
      assert math.isclose(
          baseline[key] ,distributed[key]							), F"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n"""





def 		a__			(		):
   __lowerCamelCase													=    Accelerator(split_batches=_UpperCamelCase ,dispatch_batches=_UpperCamelCase							)
   if accelerator.is_local_main_process:
      datasets.utils.logging.set_verbosity_warning()
      transformers.utils.logging.set_verbosity_warning()
   else:
      datasets.utils.logging.set_verbosity_error()
      transformers.utils.logging.set_verbosity_error()
   # These are a bit slower so they should only be ran on the GPU or TPU
   if torch.cuda.is_available() or is_tpu_available():
      if accelerator.is_local_main_process:
         print('''**Testing gather_for_metrics**'''							)
      for split_batches in [True, False]:
         for dispatch_batches in [True, False]:
            if accelerator.is_local_main_process:
               print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`"""							)
            test_mrpc(_UpperCamelCase ,_UpperCamelCase							)
            accelerator.state._reset_state()
   if accelerator.is_local_main_process:
      print('''**Test torch metrics**'''							)
   for split_batches in [True, False]:
      for dispatch_batches in [True, False]:
         __lowerCamelCase													=    Accelerator(split_batches=_UpperCamelCase ,dispatch_batches=_UpperCamelCase							)
         if accelerator.is_local_main_process:
            print(F"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99"""							)
         test_torch_metrics(_UpperCamelCase ,99							)
         accelerator.state._reset_state()
   if accelerator.is_local_main_process:
      print('''**Test last batch is not dropped when perfectly divisible**'''							)
   __lowerCamelCase													=    Accelerator()
   test_torch_metrics(_UpperCamelCase ,5_12							)
   accelerator.state._reset_state()





def 		a__			(		_UpperCamelCase			: Optional[int]							):
   # For xla_spawn (TPUs)
   main()


if __name__ == "__main__":
  main()






