
'''simple docstring'''


import gc
import random
import unittest

import torch

from diffusers import (
    IFImgaImgPipeline,
    IFImgaImgSuperResolutionPipeline,
    IFInpaintingPipeline,
    IFInpaintingSuperResolutionPipeline,
    IFPipeline,
    IFSuperResolutionPipeline,
)
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference
from . import IFPipelineTesterMixin


@skip_mps
class 						lowercase__	(  snake_case_,					snake_case_,					unittest.TestCase   ):






   '''simple docstring'''







   _snake_case		   =							IFPipeline
   _snake_case		   =							TEXT_TO_IMAGE_PARAMS - {'''width''', '''height''', '''latents'''}
   _snake_case		   =							TEXT_TO_IMAGE_BATCH_PARAMS
   _snake_case		   =							PipelineTesterMixin.required_optional_params - {'''latents'''}







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      return self._get_dummy_components()







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__=0   ):

      '''simple docstring'''







      if str(lowerCamelCase__   ).startswith('''mps'''   ):
         UpperCamelCase       =  torch.manual_seed(lowerCamelCase__   )
      else:
         UpperCamelCase       =  torch.Generator(device=lowerCamelCase__   ).manual_seed(lowerCamelCase__   )

      UpperCamelCase       =  {
          '''prompt''': '''A painting of a squirrel eating a burger''',
          '''generator''': generator,
          '''num_inference_steps''': 2,
          '''output_type''': '''numpy''',
      }

      return inputs







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      self._test_save_load_optional_components()







   @unittest.skipIf(torch_device != '''cuda''' ,   reason='''float16 requires CUDA'''   )
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      super().test_save_load_floataa(expected_max_diff=1e-1   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      self._test_attention_slicing_forward_pass(expected_max_diff=1e-2   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      self._test_save_load_local()







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      self._test_inference_batch_single_identical(
          expected_max_diff=1e-2 ,   )







   @unittest.skipIf(
       torch_device != '''cuda''' or not is_xformers_available() ,   reason='''XFormers attention is only available with CUDA and `xformers` installed''' ,   )
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3   )




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






   '''simple docstring'''







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0''' ,   variant='''fp16''' ,   torch_dtype=torch.floataa   )

      UpperCamelCase       =  IFSuperResolutionPipeline.from_pretrained(
          '''DeepFloyd/IF-II-L-v1.0''' ,   variant='''fp16''' ,   torch_dtype=torch.floataa ,   text_encoder=lowerCamelCase__ ,   tokenizer=lowerCamelCase__   )

      # pre compute text embeddings and remove T5 to save memory

      pipe_a.text_encoder.to('''cuda'''   )

      UpperCamelCase     , UpperCamelCase       =  pipe_a.encode_prompt('''anime turtle''' ,   device='''cuda'''   )

      del pipe_a.tokenizer
      del pipe_a.text_encoder
      gc.collect()

      UpperCamelCase       =  None
      UpperCamelCase       =  None

      pipe_a.enable_model_cpu_offload()
      pipe_a.enable_model_cpu_offload()

      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )
      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )

      self._test_if(lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   )

      pipe_a.remove_all_hooks()
      pipe_a.remove_all_hooks()

      # img2img

      UpperCamelCase       =  IFImgaImgPipeline(**pipe_a.components   )
      UpperCamelCase       =  IFImgaImgSuperResolutionPipeline(**pipe_a.components   )

      pipe_a.enable_model_cpu_offload()
      pipe_a.enable_model_cpu_offload()

      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )
      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )

      self._test_if_imgaimg(lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   )

      pipe_a.remove_all_hooks()
      pipe_a.remove_all_hooks()

      # inpainting

      UpperCamelCase       =  IFInpaintingPipeline(**pipe_a.components   )
      UpperCamelCase       =  IFInpaintingSuperResolutionPipeline(**pipe_a.components   )

      pipe_a.enable_model_cpu_offload()
      pipe_a.enable_model_cpu_offload()

      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )
      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()   )

      self._test_if_inpainting(lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      _start_torch_memory_measurement()

      UpperCamelCase       =  torch.Generator(device='''cpu'''   ).manual_seed(0   )
      UpperCamelCase       =  pipe_a(
          prompt_embeds=lowerCamelCase__ ,   negative_prompt_embeds=lowerCamelCase__ ,   num_inference_steps=2 ,   generator=lowerCamelCase__ ,   output_type='''np''' ,   )

      UpperCamelCase       =  output.images[0]

      assert image.shape == (6_4, 6_4, 3)

      UpperCamelCase       =  torch.cuda.max_memory_allocated()
      assert mem_bytes < 1_3 * 1_0**9

      UpperCamelCase       =  load_numpy(
          '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy'''   )
      assert_mean_pixel_difference(lowerCamelCase__ ,   lowerCamelCase__   )

      # pipeline 2

      _start_torch_memory_measurement()

      UpperCamelCase       =  torch.Generator(device='''cpu'''   ).manual_seed(0   )

      UpperCamelCase       =  floats_tensor((1, 3, 6_4, 6_4) ,   rng=random.Random(0   )   ).to(lowerCamelCase__   )

      UpperCamelCase       =  pipe_a(
          prompt_embeds=lowerCamelCase__ ,   negative_prompt_embeds=lowerCamelCase__ ,   image=lowerCamelCase__ ,   generator=lowerCamelCase__ ,   num_inference_steps=2 ,   output_type='''np''' ,   )

      UpperCamelCase       =  output.images[0]

      assert image.shape == (2_5_6, 2_5_6, 3)

      UpperCamelCase       =  torch.cuda.max_memory_allocated()
      assert mem_bytes < 4 * 1_0**9

      UpperCamelCase       =  load_numpy(
          '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy'''   )
      assert_mean_pixel_difference(lowerCamelCase__ ,   lowerCamelCase__   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      _start_torch_memory_measurement()

      UpperCamelCase       =  floats_tensor((1, 3, 6_4, 6_4) ,   rng=random.Random(0   )   ).to(lowerCamelCase__   )

      UpperCamelCase       =  torch.Generator(device='''cpu'''   ).manual_seed(0   )

      UpperCamelCase       =  pipe_a(
          prompt_embeds=lowerCamelCase__ ,   negative_prompt_embeds=lowerCamelCase__ ,   image=lowerCamelCase__ ,   num_inference_steps=2 ,   generator=lowerCamelCase__ ,   output_type='''np''' ,   )

      UpperCamelCase       =  output.images[0]

      assert image.shape == (6_4, 6_4, 3)

      UpperCamelCase       =  torch.cuda.max_memory_allocated()
      assert mem_bytes < 1_0 * 1_0**9

      UpperCamelCase       =  load_numpy(
          '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy'''   )
      assert_mean_pixel_difference(lowerCamelCase__ ,   lowerCamelCase__   )

      # pipeline 2

      _start_torch_memory_measurement()

      UpperCamelCase       =  torch.Generator(device='''cpu'''   ).manual_seed(0   )

      UpperCamelCase       =  floats_tensor((1, 3, 2_5_6, 2_5_6) ,   rng=random.Random(0   )   ).to(lowerCamelCase__   )
      UpperCamelCase       =  floats_tensor((1, 3, 6_4, 6_4) ,   rng=random.Random(0   )   ).to(lowerCamelCase__   )

      UpperCamelCase       =  pipe_a(
          prompt_embeds=lowerCamelCase__ ,   negative_prompt_embeds=lowerCamelCase__ ,   image=lowerCamelCase__ ,   original_image=lowerCamelCase__ ,   generator=lowerCamelCase__ ,   num_inference_steps=2 ,   output_type='''np''' ,   )

      UpperCamelCase       =  output.images[0]

      assert image.shape == (2_5_6, 2_5_6, 3)

      UpperCamelCase       =  torch.cuda.max_memory_allocated()
      assert mem_bytes < 4 * 1_0**9

      UpperCamelCase       =  load_numpy(
          '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy'''   )
      assert_mean_pixel_difference(lowerCamelCase__ ,   lowerCamelCase__   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      _start_torch_memory_measurement()

      UpperCamelCase       =  floats_tensor((1, 3, 6_4, 6_4) ,   rng=random.Random(0   )   ).to(lowerCamelCase__   )
      UpperCamelCase       =  floats_tensor((1, 3, 6_4, 6_4) ,   rng=random.Random(1   )   ).to(lowerCamelCase__   )

      UpperCamelCase       =  torch.Generator(device='''cpu'''   ).manual_seed(0   )
      UpperCamelCase       =  pipe_a(
          prompt_embeds=lowerCamelCase__ ,   negative_prompt_embeds=lowerCamelCase__ ,   image=lowerCamelCase__ ,   mask_image=lowerCamelCase__ ,   num_inference_steps=2 ,   generator=lowerCamelCase__ ,   output_type='''np''' ,   )

      UpperCamelCase       =  output.images[0]

      assert image.shape == (6_4, 6_4, 3)

      UpperCamelCase       =  torch.cuda.max_memory_allocated()
      assert mem_bytes < 1_0 * 1_0**9

      UpperCamelCase       =  load_numpy(
          '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy'''   )
      assert_mean_pixel_difference(lowerCamelCase__ ,   lowerCamelCase__   )

      # pipeline 2

      _start_torch_memory_measurement()

      UpperCamelCase       =  torch.Generator(device='''cpu'''   ).manual_seed(0   )

      UpperCamelCase       =  floats_tensor((1, 3, 6_4, 6_4) ,   rng=random.Random(0   )   ).to(lowerCamelCase__   )
      UpperCamelCase       =  floats_tensor((1, 3, 2_5_6, 2_5_6) ,   rng=random.Random(0   )   ).to(lowerCamelCase__   )
      UpperCamelCase       =  floats_tensor((1, 3, 2_5_6, 2_5_6) ,   rng=random.Random(1   )   ).to(lowerCamelCase__   )

      UpperCamelCase       =  pipe_a(
          prompt_embeds=lowerCamelCase__ ,   negative_prompt_embeds=lowerCamelCase__ ,   image=lowerCamelCase__ ,   mask_image=lowerCamelCase__ ,   original_image=lowerCamelCase__ ,   generator=lowerCamelCase__ ,   num_inference_steps=2 ,   output_type='''np''' ,   )

      UpperCamelCase       =  output.images[0]

      assert image.shape == (2_5_6, 2_5_6, 3)

      UpperCamelCase       =  torch.cuda.max_memory_allocated()
      assert mem_bytes < 4 * 1_0**9

      UpperCamelCase       =  load_numpy(
          '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy'''   )
      assert_mean_pixel_difference(lowerCamelCase__ ,   lowerCamelCase__   )




def  __snake_case     (    ):
   torch.cuda.empty_cache()
   torch.cuda.reset_max_memory_allocated()
   torch.cuda.reset_peak_memory_stats()







'''simple docstring'''


import math
from typing import Optional

import numpy as np

from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case_ :  Dict     =					logging.get_logger(__name__)

snake_case_ :  Union[str, Any]     =					{
    'facebook/encodec_24khz': 'https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json',
    'facebook/encodec_48khz': 'https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json',
}


class 						lowercase__	(  snake_case_   ):






   '''simple docstring'''







   _snake_case		   =							'''encodec'''







   def __init__(			self ,   lowerCamelCase__=[1.5, 3.0, 6.0, 12.0, 24.0] ,   lowerCamelCase__=2_4_0_0_0 ,   lowerCamelCase__=1 ,   lowerCamelCase__=False ,   lowerCamelCase__=None ,   lowerCamelCase__=None ,   lowerCamelCase__=1_2_8 ,   lowerCamelCase__=3_2 ,   lowerCamelCase__=1 ,   lowerCamelCase__=[8, 5, 4, 2] ,   lowerCamelCase__="weight_norm" ,   lowerCamelCase__=7 ,   lowerCamelCase__=7 ,   lowerCamelCase__=3 ,   lowerCamelCase__=2 ,   lowerCamelCase__=True ,   lowerCamelCase__="reflect" ,   lowerCamelCase__=2 ,   lowerCamelCase__=2 ,   lowerCamelCase__=1.0 ,   lowerCamelCase__=1_0_2_4 ,   lowerCamelCase__=None ,   lowerCamelCase__=True ,   **lowerCamelCase__ ,   ):

      '''simple docstring'''







      UpperCamelCase       =  target_bandwidths
      UpperCamelCase       =  sampling_rate
      UpperCamelCase       =  audio_channels
      UpperCamelCase       =  normalize
      UpperCamelCase       =  chunk_length_s
      UpperCamelCase       =  overlap
      UpperCamelCase       =  hidden_size
      UpperCamelCase       =  num_filters
      UpperCamelCase       =  num_residual_layers
      UpperCamelCase       =  upsampling_ratios
      UpperCamelCase       =  norm_type
      UpperCamelCase       =  kernel_size
      UpperCamelCase       =  last_kernel_size
      UpperCamelCase       =  residual_kernel_size
      UpperCamelCase       =  dilation_growth_rate
      UpperCamelCase       =  use_causal_conv
      UpperCamelCase       =  pad_mode
      UpperCamelCase       =  compress
      UpperCamelCase       =  num_lstm_layers
      UpperCamelCase       =  trim_right_ratio
      UpperCamelCase       =  codebook_size
      UpperCamelCase       =  codebook_dim if codebook_dim is not None else hidden_size
      UpperCamelCase       =  use_conv_shortcut

      if self.norm_type not in ["weight_norm", "time_group_norm"]:
         raise ValueError(
             f'self.norm_type must be one of `"weight_norm"`, `"time_group_norm"`), got {self.norm_type}'   )

      super().__init__(**lowerCamelCase__   )







   @property
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      if self.chunk_length_s is None:
         return None
      else:
         return int(self.chunk_length_s * self.sampling_rate   )







   @property
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      if self.chunk_length_s is None or self.overlap is None:
         return None
      else:
         return max(1 ,   int((1.0 - self.overlap) * self.chunk_length   )   )







   @property
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  np.prod(self.upsampling_ratios   )
      return math.ceil(self.sampling_rate / hop_length   )







   @property
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      return int(1_0_0_0 * self.target_bandwidths[-1] // (self.frame_rate * 1_0)   )





