




import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionModelEditingPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()

@skip_mps
class      UpperCAmelCase						(			__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					unittest.TestCase   ):
   __SCREAMING_SNAKE_CASE		:							int							=					StableDiffusionModelEditingPipeline
   __SCREAMING_SNAKE_CASE		:							List[Any]							=					TEXT_TO_IMAGE_PARAMS
   __SCREAMING_SNAKE_CASE		:							Tuple							=					TEXT_TO_IMAGE_BATCH_PARAMS
   __SCREAMING_SNAKE_CASE		:							Dict							=					TEXT_TO_IMAGE_IMAGE_PARAMS
   __SCREAMING_SNAKE_CASE		:							Tuple							=					TEXT_TO_IMAGE_IMAGE_PARAMS






   def  _a						(	self   :       Dict     )->			List[str]:
     torch.manual_seed(0     )
     _lowercase: List[Any]						     =    UNetaDConditionModel(
         block_out_channels=(32, 64)						,					layers_per_block=2						,					sample_size=32						,					in_channels=4						,					out_channels=4						,					down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')						,					up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')						,					cross_attention_dim=32						,					)
     _lowercase: Union[str, Any]						     =    DDIMScheduler()
     torch.manual_seed(0     )
     _lowercase: List[Any]						     =    AutoencoderKL(
         block_out_channels=[32, 64]						,					in_channels=3						,					out_channels=3						,					down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']						,					up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']						,					latent_channels=4						,					)
     torch.manual_seed(0     )
     _lowercase: Dict						     =    CLIPTextConfig(
         bos_token_id=0						,					eos_token_id=2						,					hidden_size=32						,					intermediate_size=37						,					layer_norm_eps=1E-05						,					num_attention_heads=4						,					num_hidden_layers=5						,					pad_token_id=1						,					vocab_size=10_00						,					)
     _lowercase: Tuple						     =    CLIPTextModel(UpperCamelCase__     )
     _lowercase: List[str]						     =    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'     )

     _lowercase: List[str]						     =    {
         '''unet''': unet,
         '''scheduler''': scheduler,
         '''vae''': vae,
         '''text_encoder''': text_encoder,
         '''tokenizer''': tokenizer,
         '''safety_checker''': None,
         '''feature_extractor''': None,
     }
     return components






   def  _a						(	self   :       Union[str, Any]						,					UpperCamelCase_   :       Optional[int]						,					UpperCamelCase_   :       Union[str, Any]=0     )->			str:
     _lowercase: List[str]						     =    torch.manual_seed(UpperCamelCase__     )
     _lowercase: str						     =    {
         '''prompt''': '''A field of roses''',
         '''generator''': generator,
         # Setting height and width to None to prevent OOMs on CPU.
         '''height''': None,
         '''width''': None,
         '''num_inference_steps''': 2,
         '''guidance_scale''': 6.0,
         '''output_type''': '''numpy''',
     }
     return inputs






   def  _a						(	self   :       Dict     )->			Union[str, Any]:
     _lowercase: str						     =    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
     _lowercase: Optional[int]						     =    self.get_dummy_components()
     _lowercase: Dict						     =    StableDiffusionModelEditingPipeline(**UpperCamelCase__     )
     _lowercase: Optional[Any]						     =    sd_pipe.to(UpperCamelCase__     )
     sd_pipe.set_progress_bar_config(disable=UpperCamelCase__     )

     _lowercase: Dict						     =    self.get_dummy_inputs(UpperCamelCase__     )
     _lowercase: Dict						     =    sd_pipe(**UpperCamelCase__     ).images
     _lowercase: Optional[Any]						     =    image[0, -3:, -3:, -1]
     assert image.shape == (1, 64, 64, 3)

     _lowercase: Dict						     =    np.array([0.4_755, 0.5_132, 0.4_976, 0.3_904, 0.3_554, 0.4_765, 0.5_139, 0.5_158, 0.4_889]     )

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






   def  _a						(	self   :       Any     )->			List[str]:
     _lowercase: str						     =    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
     _lowercase: Optional[Any]						     =    self.get_dummy_components()
     _lowercase: Optional[int]						     =    StableDiffusionModelEditingPipeline(**UpperCamelCase__     )
     _lowercase: str						     =    sd_pipe.to(UpperCamelCase__     )
     sd_pipe.set_progress_bar_config(disable=UpperCamelCase__     )

     _lowercase: List[Any]						     =    self.get_dummy_inputs(UpperCamelCase__     )
     _lowercase: int						     =    '''french fries'''
     _lowercase: Optional[Any]						     =    sd_pipe(**UpperCamelCase__						,					negative_prompt=UpperCamelCase__     )
     _lowercase: List[str]						     =    output.images
     _lowercase: Any						     =    image[0, -3:, -3:, -1]

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

     _lowercase: Union[str, Any]						     =    np.array([0.4_992, 0.5_101, 0.5_004, 0.3_949, 0.3_604, 0.4_735, 0.5_216, 0.5_204, 0.4_913]     )

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






   def  _a						(	self   :       Dict     )->			Optional[int]:
     _lowercase: Dict						     =    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
     _lowercase: Union[str, Any]						     =    self.get_dummy_components()
     _lowercase: Any						     =    EulerAncestralDiscreteScheduler(
         beta_start=0.00_085						,					beta_end=0.012						,					beta_schedule='scaled_linear'     )
     _lowercase: Tuple						     =    StableDiffusionModelEditingPipeline(**UpperCamelCase__     )
     _lowercase: Optional[int]						     =    sd_pipe.to(UpperCamelCase__     )
     sd_pipe.set_progress_bar_config(disable=UpperCamelCase__     )

     _lowercase: Any						     =    self.get_dummy_inputs(UpperCamelCase__     )
     _lowercase: Optional[int]						     =    sd_pipe(**UpperCamelCase__     ).images
     _lowercase: Any						     =    image[0, -3:, -3:, -1]

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

     _lowercase: List[Any]						     =    np.array([0.4_747, 0.5_372, 0.4_779, 0.4_982, 0.5_543, 0.4_816, 0.5_238, 0.4_904, 0.5_027]     )

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






   def  _a						(	self   :       str     )->			Optional[Any]:
     _lowercase: Optional[Any]						     =    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
     _lowercase: Optional[Any]						     =    self.get_dummy_components()
     _lowercase: int						     =    PNDMScheduler()
     _lowercase: Optional[Any]						     =    StableDiffusionModelEditingPipeline(**UpperCamelCase__     )
     _lowercase: Dict						     =    sd_pipe.to(UpperCamelCase__     )
     sd_pipe.set_progress_bar_config(disable=UpperCamelCase__     )

     _lowercase: Tuple						     =    self.get_dummy_inputs(UpperCamelCase__     )
     # the pipeline does not expect pndm so test if it raises error.
     with self.assertRaises(UpperCamelCase__     ):
       _lowercase: Dict						     =    sd_pipe(**UpperCamelCase__     ).images






   def  _a						(	self   :       List[Any]     )->			Optional[Any]:
     super().test_inference_batch_single_identical(expected_max_diff=5E-3     )






   def  _a						(	self   :       Union[str, Any]     )->			Dict:
     super().test_attention_slicing_forward_pass(expected_max_diff=5E-3     )

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






   def  _a						(	self   :       List[Any]     )->			Optional[Any]:
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()






   def  _a						(	self   :       List[str]						,					UpperCamelCase_   :       Dict=0     )->			Tuple:
     _lowercase: Tuple						     =    torch.manual_seed(UpperCamelCase__     )
     _lowercase: Any						     =    {
         '''prompt''': '''A field of roses''',
         '''generator''': generator,
         '''num_inference_steps''': 3,
         '''guidance_scale''': 7.5,
         '''output_type''': '''numpy''',
     }
     return inputs






   def  _a						(	self   :       Tuple     )->			int:
     _lowercase: List[str]						     =    '''CompVis/stable-diffusion-v1-4'''
     _lowercase: Union[str, Any]						     =    StableDiffusionModelEditingPipeline.from_pretrained(UpperCamelCase__						,					safety_checker=UpperCamelCase__     )
     pipe.to(UpperCamelCase__     )
     pipe.set_progress_bar_config(disable=UpperCamelCase__     )
     pipe.enable_attention_slicing()

     _lowercase: Optional[int]						     =    self.get_inputs()
     _lowercase: Tuple						     =    pipe(**UpperCamelCase__     ).images
     _lowercase: Optional[int]						     =    image[0, -3:, -3:, -1].flatten()

     assert image.shape == (1, 5_12, 5_12, 3)

     _lowercase: List[str]						     =    np.array(
         [0.6_749_496, 0.6_386_453, 0.51_443_267, 0.66_094_905, 0.61_921_215, 0.5_491_332, 0.5_744_417, 0.58_075_106, 0.5_174_658]     )

     assert np.abs(expected_slice - image_slice     ).max() < 1E-2

     # make sure image changes after editing
     pipe.edit_model('A pack of roses'						,					'A pack of blue roses'     )

     _lowercase: List[Any]						     =    pipe(**UpperCamelCase__     ).images
     _lowercase: List[Any]						     =    image[0, -3:, -3:, -1].flatten()

     assert image.shape == (1, 5_12, 5_12, 3)

     assert np.abs(expected_slice - image_slice     ).max() > 1E-1






   def  _a						(	self   :       Optional[Any]     )->			Any:
     torch.cuda.empty_cache()
     torch.cuda.reset_max_memory_allocated()
     torch.cuda.reset_peak_memory_stats()

     _lowercase: Optional[Any]						     =    '''CompVis/stable-diffusion-v1-4'''
     _lowercase: Dict						     =    DDIMScheduler.from_pretrained(UpperCamelCase__						,					subfolder='scheduler'     )
     _lowercase: List[str]						     =    StableDiffusionModelEditingPipeline.from_pretrained(
         UpperCamelCase__						,					scheduler=UpperCamelCase__						,					safety_checker=UpperCamelCase__     )
     _lowercase: int						     =    pipe.to(UpperCamelCase__     )
     pipe.set_progress_bar_config(disable=UpperCamelCase__     )
     pipe.enable_attention_slicing(1     )
     pipe.enable_sequential_cpu_offload()

     _lowercase: Optional[Any]						     =    self.get_inputs()
     _lowercase: Optional[int]						     =    pipe(**UpperCamelCase__     )

     _lowercase: Optional[Any]						     =    torch.cuda.max_memory_allocated()
     # make sure that less than 4.4 GB is allocated
     assert mem_bytes < 4.4 * 10**9






import os
import tempfile
from pathlib import Path
from unittest import TestCase

import pyarrow as pa
import pytest

from datasets.arrow_dataset import Dataset
from datasets.arrow_reader import ArrowReader, BaseReader, FileInstructions, ReadInstruction, make_file_instructions
from datasets.info import DatasetInfo
from datasets.splits import NamedSplit, Split, SplitDict, SplitInfo

from .utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases

class      UpperCAmelCase						(			A   ):






   def  _a						(	self   :       List[str]						,					UpperCamelCase_   :       str						,					UpperCamelCase_   :       Optional[Any]=False     )->			List[str]:
     _lowercase     ,					_lowercase     ,					_lowercase: Dict						     =    (
         filename_skip_take['filename'],
         filename_skip_take['skip'] if 'skip' in filename_skip_take else None,
         filename_skip_take['take'] if 'take' in filename_skip_take else None,
     )
     open(os.path.join(UpperCamelCase_     )						,					'wb'     ).close()
     _lowercase: int						     =    pa.Table.from_pydict({'filename': [Path(UpperCamelCase_     ).name] * 1_00}     )
     if take == -1:
       _lowercase: int						     =    len(UpperCamelCase_     ) - skip
     if skip is not None and take is not None:
       _lowercase: Any						     =    pa_table.slice(UpperCamelCase_						,					UpperCamelCase_     )
     return pa_table

class      UpperCAmelCase						(			A   ):






   def  _a						(	self   :       Any     )->			Dict:
     _lowercase: str						     =    'my_name'
     _lowercase: Optional[int]						     =    SplitInfo(name='train'						,					num_examples=1_00     )
     _lowercase: Dict						     =    SplitInfo(name='test'						,					num_examples=1_00     )
     _lowercase: Any						     =    [train_info, test_info]
     _lowercase: Any						     =    SplitDict()
     split_dict.add(UpperCamelCase_     )
     split_dict.add(UpperCamelCase_     )
     _lowercase: Tuple						     =    DatasetInfo(splits=UpperCamelCase_     )

     with tempfile.TemporaryDirectory() as tmp_dir:
       _lowercase: List[str]						     =    ReaderTest(UpperCamelCase_						,					UpperCamelCase_     )

       _lowercase: Dict						     =    'test[:33%]'
       _lowercase: Optional[Any]						     =    Dataset(**reader.read(UpperCamelCase_						,					UpperCamelCase_						,					UpperCamelCase_     )     )
       self.assertEqual(dset['filename'][0]						,					f'{name}-test'     )
       self.assertEqual(dset.num_rows						,					33     )
       self.assertEqual(dset.num_columns						,					1     )

       _lowercase: Tuple						     =    ['train', 'test[:33%]']
       _lowercase: Union[str, Any]						     =    [Split.TRAIN, ReadInstruction.from_spec('test[:33%]'     )]
       for instructions in [instructionsa, instructionsa]:
         _lowercase: int						     =    [reader.read(UpperCamelCase_						,					UpperCamelCase_						,					UpperCamelCase_     ) for instr in instructions]
         _lowercase     ,					_lowercase: Dict						     =    (Dataset(**UpperCamelCase_     ) for dataset_kwargs in datasets_kwargs)
         self.assertEqual(train_dset['filename'][0]						,					f'{name}-train'     )
         self.assertEqual(train_dset.num_rows						,					1_00     )
         self.assertEqual(train_dset.num_columns						,					1     )
         self.assertIsInstance(train_dset.split						,					UpperCamelCase_     )
         self.assertEqual(str(train_dset.split     )						,					'train'     )
         self.assertEqual(test_dset['filename'][0]						,					f'{name}-test'     )
         self.assertEqual(test_dset.num_rows						,					33     )
         self.assertEqual(test_dset.num_columns						,					1     )
         self.assertIsInstance(test_dset.split						,					UpperCamelCase_     )
         self.assertEqual(str(test_dset.split     )						,					'test[:33%]'     )
         del train_dset, test_dset






   def  _a						(	self   :       Any     )->			List[str]:
     _lowercase: int						     =    'my_name'
     _lowercase: List[str]						     =    SplitInfo(name='train'						,					num_examples=10_00						,					shard_lengths=[1_00] * 10     )
     _lowercase: Optional[Any]						     =    [train_info]
     _lowercase: Optional[int]						     =    SplitDict()
     split_dict.add(UpperCamelCase_     )
     _lowercase: Optional[Any]						     =    DatasetInfo(splits=UpperCamelCase_     )

     with tempfile.TemporaryDirectory() as tmp_dir:
       _lowercase: Optional[int]						     =    ReaderTest(UpperCamelCase_						,					UpperCamelCase_     )

       _lowercase: Any						     =    'train[:33%]'
       _lowercase: List[Any]						     =    Dataset(**reader.read(UpperCamelCase_						,					UpperCamelCase_						,					UpperCamelCase_     )     )
       self.assertEqual(dset['filename'][0]						,					f'{name}-train-00000-of-00010'     )
       self.assertEqual(dset['filename'][-1]						,					f'{name}-train-00003-of-00010'     )
       self.assertEqual(dset.num_rows						,					3_30     )
       self.assertEqual(dset.num_columns						,					1     )






   def  _a						(	self   :       List[Any]     )->			Dict:
     _lowercase: Any						     =    SplitInfo(name='train'						,					num_examples=1_00     )
     _lowercase: List[Any]						     =    SplitInfo(name='test'						,					num_examples=1_00     )
     _lowercase: List[Any]						     =    SplitDict()
     split_dict.add(UpperCamelCase_     )
     split_dict.add(UpperCamelCase_     )
     _lowercase: Union[str, Any]						     =    DatasetInfo(splits=UpperCamelCase_     )

     with tempfile.TemporaryDirectory() as tmp_dir:
       _lowercase: Dict						     =    ReaderTest(UpperCamelCase_						,					UpperCamelCase_     )

       _lowercase: List[Any]						     =    [
           {'filename': os.path.join(UpperCamelCase_						,					'train'     )},
           {'filename': os.path.join(UpperCamelCase_						,					'test'     ), 'skip': 10, 'take': 10},
       ]
       _lowercase: Tuple						     =    Dataset(**reader.read_files(UpperCamelCase_						,					original_instructions='train+test[10:20]'     )     )
       self.assertEqual(dset.num_rows						,					1_10     )
       self.assertEqual(dset.num_columns						,					1     )
       del dset


@pytest.mark.parametrize('in_memory'					,     [False, True]							)
def 		_lowerCamelCase				(					UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase							)			->		Union[str, Any]:





  """simple docstring"""





  _lowercase: Optional[Any]						     =    arrow_file
  with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
    _lowercase: List[Any]						     =    ArrowReader.read_table(UpperCamelCase					,     in_memory=UpperCamelCase							)
  assert table.shape == dataset.data.shape
  assert set(table.column_names							) == set(dataset.data.column_names							)
  assert dict(table.to_pydict()							) == dict(dataset.data.to_pydict()							)  # to_pydict returns OrderedDict


@pytest.mark.parametrize('in_memory'					,     [False, True]							)
def 		_lowerCamelCase				(					UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase							)			->		List[Any]:





  """simple docstring"""





  _lowercase: Any						     =    arrow_file
  _lowercase: Union[str, Any]						     =    ArrowReader(''					,     UpperCamelCase							)
  with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
    _lowercase: Tuple						     =    reader.read_files([{'filename': filename}]					,     in_memory=UpperCamelCase							)
  assert dataset_kwargs.keys() == {"arrow_table", "info", "split"}
  _lowercase: Any						     =    dataset_kwargs['arrow_table']
  assert table.shape == dataset.data.shape
  assert set(table.column_names							) == set(dataset.data.column_names							)
  assert dict(table.to_pydict()							) == dict(dataset.data.to_pydict()							)  # to_pydict returns OrderedDict


def 		_lowerCamelCase				(					)			->		int:





  """simple docstring"""





  assert ReadInstruction('train'					,     to=10					,     unit='abs'							).to_spec() == "train[:10]"
  assert ReadInstruction('train'					,     from_=-80					,     to=10					,     unit='%'							).to_spec() == "train[-80%:10%]"

  _lowercase: List[str]						     =    'train+test'
  assert ReadInstruction.from_spec(UpperCamelCase							).to_spec() == spec_train_test

  _lowercase: Union[str, Any]						     =    'train[2:10]'
  assert ReadInstruction.from_spec(UpperCamelCase							).to_spec() == spec_train_abs

  _lowercase: Any						     =    'train[15%:-20%]'
  assert ReadInstruction.from_spec(UpperCamelCase							).to_spec() == spec_train_pct

  _lowercase: List[Any]						     =    'train[:10%](closest)'
  assert ReadInstruction.from_spec(UpperCamelCase							).to_spec() == "train[:10%]"

  _lowercase: int						     =    'train[:10%](pct1_dropremainder)'
  assert ReadInstruction.from_spec(UpperCamelCase							).to_spec() == spec_train_pct_rounding

  _lowercase: int						     =    'train[:10%](pct1_dropremainder)+test[-10%:](pct1_dropremainder)'
  assert ReadInstruction.from_spec(UpperCamelCase							).to_spec() == spec_train_test_pct_rounding


def 		_lowerCamelCase				(					)			->		Union[str, Any]:





  """simple docstring"""





  _lowercase: int						     =    'dummy'
  _lowercase: Any						     =    [SplitInfo(name='train'					,     num_examples=100							)]
  _lowercase: Any						     =    'train[:33%]'
  _lowercase: Optional[int]						     =    'arrow'
  _lowercase: Optional[int]						     =    'prefix'

  _lowercase: Optional[int]						     =    make_file_instructions(UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase							)
  assert isinstance(UpperCamelCase					,     UpperCamelCase							)
  assert file_instructions.num_examples == 33
  assert file_instructions.file_instructions == [
      {"filename": os.path.join(UpperCamelCase					,     F'{name}-train.arrow'							), "skip": 0, "take": 33}
  ]

  _lowercase: int						     =    [SplitInfo(name='train'					,     num_examples=100					,     shard_lengths=[10] * 10							)]
  _lowercase: Dict						     =    make_file_instructions(UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase							)
  assert isinstance(UpperCamelCase					,     UpperCamelCase							)
  assert file_instructions.num_examples == 33
  assert file_instructions.file_instructions == [
      {"filename": os.path.join(UpperCamelCase					,     F'{name}-train-00000-of-00010.arrow'							), "skip": 0, "take": -1},
      {"filename": os.path.join(UpperCamelCase					,     F'{name}-train-00001-of-00010.arrow'							), "skip": 0, "take": -1},
      {"filename": os.path.join(UpperCamelCase					,     F'{name}-train-00002-of-00010.arrow'							), "skip": 0, "take": -1},
      {"filename": os.path.join(UpperCamelCase					,     F'{name}-train-00003-of-00010.arrow'							), "skip": 0, "take": 3},
  ]





@pytest.mark.parametrize('name, expected_exception'					,     [(None, TypeError), ('', ValueError)]							)
def 		_lowerCamelCase				(					UpperCamelCase					,     UpperCamelCase							)			->		Tuple:





  """simple docstring"""





  _lowercase: List[str]						     =    [SplitInfo(name='train'					,     num_examples=100							)]
  _lowercase: List[Any]						     =    'train'
  _lowercase: List[str]						     =    'arrow'
  _lowercase: Optional[Any]						     =    'prefix_path'
  with pytest.raises(UpperCamelCase							):
    _lowercase: List[str]						     =    make_file_instructions(UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase							)
