






import gc
import random
import tempfile
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.semantic_stable_diffusion import SemanticStableDiffusionPipeline as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()


class 			__SCREAMING_SNAKE_CASE    (      unittest.TestCase):






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()






						@property
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  1
												lowerCAmelCase__			  =  3
												lowerCAmelCase__			  =  (32, 32)

												lowerCAmelCase__			  =  floats_tensor((batch_size, num_channels) + sizes   ,  rng=random.Random(0      )      ).to(_UpperCamelCase      )
												return image






						@property
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												torch.manual_seed(0      )
												lowerCAmelCase__			  =  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   ,  )
												return model






						@property
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												torch.manual_seed(0      )
												lowerCAmelCase__			  =  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   ,  )
												return model






						@property
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												torch.manual_seed(0      )
												lowerCAmelCase__			  =  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   ,  )
												return CLIPTextModel(_UpperCamelCase      )






						@property
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												def extract(*_UpperCamelCase   ,  **_UpperCamelCase      ):


																		class 			__SCREAMING_SNAKE_CASE    :






																								def __init__(		self      ):






																														"""simple docstring"""
																														lowerCAmelCase__			  =  torch.ones([0]      )







																								def 					UpperCamelCase__					(		self   ,  _UpperCamelCase      ):






																														"""simple docstring"""
																														self.pixel_values.to(_UpperCamelCase      )
																														return self




																		return Out()

												return extract






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  'cpu'  # ensure determinism for the device-dependent torch.Generator
												lowerCAmelCase__			  =  self.dummy_cond_unet
												lowerCAmelCase__			  =  DDIMScheduler(
												    beta_start=0.0_00_85   ,  beta_end=0.0_12   ,  beta_schedule='scaled_linear'   ,  clip_sample=_UpperCamelCase   ,  set_alpha_to_one=_UpperCamelCase   ,  )

												lowerCAmelCase__			  =  self.dummy_vae
												lowerCAmelCase__			  =  self.dummy_text_encoder
												lowerCAmelCase__			  =  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'      )

												# make sure here that pndm scheduler skips prk
												lowerCAmelCase__			  =  StableDiffusionPipeline(
												    unet=_UpperCamelCase   ,  scheduler=_UpperCamelCase   ,  vae=_UpperCamelCase   ,  text_encoder=_UpperCamelCase   ,  tokenizer=_UpperCamelCase   ,  safety_checker=_UpperCamelCase   ,  feature_extractor=self.dummy_extractor   ,  )
												lowerCAmelCase__			  =  sd_pipe.to(_UpperCamelCase      )
												sd_pipe.set_progress_bar_config(disable=_UpperCamelCase      )

												lowerCAmelCase__			  =  'A painting of a squirrel eating a burger'

												lowerCAmelCase__			  =  torch.Generator(device=_UpperCamelCase      ).manual_seed(0      )
												lowerCAmelCase__			  =  sd_pipe([prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=6.0   ,  num_inference_steps=2   ,  output_type='np'      )
												lowerCAmelCase__			  =  output.images

												lowerCAmelCase__			  =  torch.Generator(device=_UpperCamelCase      ).manual_seed(0      )
												lowerCAmelCase__			  =  sd_pipe(
												    [prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=6.0   ,  num_inference_steps=2   ,  output_type='np'   ,  return_dict=_UpperCamelCase   ,  )[0]

												lowerCAmelCase__			  =  image[0, -3:, -3:, -1]
												lowerCAmelCase__			  =  image_from_tuple[0, -3:, -3:, -1]

												assert image.shape == (1, 64, 64, 3)
												lowerCAmelCase__			  =  np.array([0.57_53, 0.61_14, 0.50_01, 0.50_34, 0.54_70, 0.47_29, 0.49_71, 0.48_67, 0.48_67]      )

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






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  'cpu'  # ensure determinism for the device-dependent torch.Generator
												lowerCAmelCase__			  =  self.dummy_cond_unet
												lowerCAmelCase__			  =  PNDMScheduler(skip_prk_steps=_UpperCamelCase      )
												lowerCAmelCase__			  =  self.dummy_vae
												lowerCAmelCase__			  =  self.dummy_text_encoder
												lowerCAmelCase__			  =  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'      )

												# make sure here that pndm scheduler skips prk
												lowerCAmelCase__			  =  StableDiffusionPipeline(
												    unet=_UpperCamelCase   ,  scheduler=_UpperCamelCase   ,  vae=_UpperCamelCase   ,  text_encoder=_UpperCamelCase   ,  tokenizer=_UpperCamelCase   ,  safety_checker=_UpperCamelCase   ,  feature_extractor=self.dummy_extractor   ,  )
												lowerCAmelCase__			  =  sd_pipe.to(_UpperCamelCase      )
												sd_pipe.set_progress_bar_config(disable=_UpperCamelCase      )

												lowerCAmelCase__			  =  'A painting of a squirrel eating a burger'
												lowerCAmelCase__			  =  torch.Generator(device=_UpperCamelCase      ).manual_seed(0      )
												lowerCAmelCase__			  =  sd_pipe([prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=6.0   ,  num_inference_steps=2   ,  output_type='np'      )

												lowerCAmelCase__			  =  output.images

												lowerCAmelCase__			  =  torch.Generator(device=_UpperCamelCase      ).manual_seed(0      )
												lowerCAmelCase__			  =  sd_pipe(
												    [prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=6.0   ,  num_inference_steps=2   ,  output_type='np'   ,  return_dict=_UpperCamelCase   ,  )[0]

												lowerCAmelCase__			  =  image[0, -3:, -3:, -1]
												lowerCAmelCase__			  =  image_from_tuple[0, -3:, -3:, -1]

												assert image.shape == (1, 64, 64, 3)
												lowerCAmelCase__			  =  np.array([0.51_22, 0.57_12, 0.48_25, 0.50_53, 0.56_46, 0.47_69, 0.51_79, 0.48_94, 0.49_94]      )

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






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  StableDiffusionPipeline.from_pretrained(
												    'hf-internal-testing/tiny-stable-diffusion-lms-pipe'   ,  safety_checker=_UpperCamelCase      )
												assert isinstance(_UpperCamelCase   ,  _UpperCamelCase      )
												assert isinstance(pipe.scheduler   ,  _UpperCamelCase      )
												assert pipe.safety_checker is None

												lowerCAmelCase__			  =  pipe('example prompt'   ,  num_inference_steps=2      ).images[0]
												assert image is not None

												# check that there's no error when saving a pipeline with one of the models being None
												with tempfile.TemporaryDirectory() as tmpdirname:
																		pipe.save_pretrained(_UpperCamelCase      )
																		lowerCAmelCase__			  =  StableDiffusionPipeline.from_pretrained(_UpperCamelCase      )

												# sanity check that the pipeline still works
												assert pipe.safety_checker is None
												lowerCAmelCase__			  =  pipe('example prompt'   ,  num_inference_steps=2      ).images[0]
												assert image is not None







						@unittest.skipIf(torch_device != 'cuda'   ,  'This test requires a GPU'      )
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  self.dummy_cond_unet
												lowerCAmelCase__			  =  PNDMScheduler(skip_prk_steps=_UpperCamelCase      )
												lowerCAmelCase__			  =  self.dummy_vae
												lowerCAmelCase__			  =  self.dummy_text_encoder
												lowerCAmelCase__			  =  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'      )

												# put models in fp16
												lowerCAmelCase__			  =  unet.half()
												lowerCAmelCase__			  =  vae.half()
												lowerCAmelCase__			  =  bert.half()

												# make sure here that pndm scheduler skips prk
												lowerCAmelCase__			  =  StableDiffusionPipeline(
												    unet=_UpperCamelCase   ,  scheduler=_UpperCamelCase   ,  vae=_UpperCamelCase   ,  text_encoder=_UpperCamelCase   ,  tokenizer=_UpperCamelCase   ,  safety_checker=_UpperCamelCase   ,  feature_extractor=self.dummy_extractor   ,  )
												lowerCAmelCase__			  =  sd_pipe.to(_UpperCamelCase      )
												sd_pipe.set_progress_bar_config(disable=_UpperCamelCase      )

												lowerCAmelCase__			  =  'A painting of a squirrel eating a burger'
												lowerCAmelCase__			  =  sd_pipe([prompt]   ,  num_inference_steps=2   ,  output_type='np'      ).images

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




@nightly
@require_torch_gpu
class 			__SCREAMING_SNAKE_CASE    (      unittest.TestCase):






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  'cuda'
												lowerCAmelCase__			  =  StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'      )
												lowerCAmelCase__			  =  pipe.to(_UpperCamelCase      )
												pipe.set_progress_bar_config(disable=_UpperCamelCase      )

												lowerCAmelCase__			  =  'a photo of a cat'
												lowerCAmelCase__			  =  {
												    'editing_prompt': ['sunglasses'],
												    'reverse_editing_direction': [False],
												    'edit_warmup_steps': 10,
												    'edit_guidance_scale': 6,
												    'edit_threshold': 0.95,
												    'edit_momentum_scale': 0.5,
												    'edit_mom_beta': 0.6,
												}

												lowerCAmelCase__			  =  3
												lowerCAmelCase__			  =  7

												# no sega enabled
												lowerCAmelCase__			  =  torch.Generator(_UpperCamelCase      )
												generator.manual_seed(_UpperCamelCase      )
												lowerCAmelCase__			  =  pipe(
												    [prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=_UpperCamelCase   ,  num_inference_steps=50   ,  output_type='np'   ,  width=5_12   ,  height=5_12   ,  )

												lowerCAmelCase__			  =  output.images
												lowerCAmelCase__			  =  image[0, -3:, -3:, -1]
												lowerCAmelCase__			  =  [
												    0.34_67_31_13,
												    0.38_49_27_33,
												    0.37_59_73_52,
												    0.34_08_63_35,
												    0.35_65_07_48,
												    0.35_57_92_05,
												    0.3_38_47_63,
												    0.34_34_02_36,
												    0.3_57_32_71,
												]

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

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

												# with sega enabled
												# generator = torch.manual_seed(seed)
												generator.manual_seed(_UpperCamelCase      )
												lowerCAmelCase__			  =  pipe(
												    [prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=_UpperCamelCase   ,  num_inference_steps=50   ,  output_type='np'   ,  width=5_12   ,  height=5_12   ,  **_UpperCamelCase   ,  )

												lowerCAmelCase__			  =  output.images
												lowerCAmelCase__			  =  image[0, -3:, -3:, -1]
												lowerCAmelCase__			  =  [
												    0.41_88_78_26,
												    0.37_72_87_66,
												    0.30_13_82_72,
												    0.41_41_63_35,
												    0.41_66_49_85,
												    0.36_28_33_92,
												    0.36_19_12_46,
												    0.43_36_44_65,
												    0.43_00_17_32,
												]

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

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






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  'cuda'
												lowerCAmelCase__			  =  StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'      )
												lowerCAmelCase__			  =  pipe.to(_UpperCamelCase      )
												pipe.set_progress_bar_config(disable=_UpperCamelCase      )

												lowerCAmelCase__			  =  'an image of a crowded boulevard, realistic, 4k'
												lowerCAmelCase__			  =  {
												    'editing_prompt': 'crowd, crowded, people',
												    'reverse_editing_direction': True,
												    'edit_warmup_steps': 10,
												    'edit_guidance_scale': 8.3,
												    'edit_threshold': 0.9,
												    'edit_momentum_scale': 0.5,
												    'edit_mom_beta': 0.6,
												}

												lowerCAmelCase__			  =  9
												lowerCAmelCase__			  =  7

												# no sega enabled
												lowerCAmelCase__			  =  torch.Generator(_UpperCamelCase      )
												generator.manual_seed(_UpperCamelCase      )
												lowerCAmelCase__			  =  pipe(
												    [prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=_UpperCamelCase   ,  num_inference_steps=50   ,  output_type='np'   ,  width=5_12   ,  height=5_12   ,  )

												lowerCAmelCase__			  =  output.images
												lowerCAmelCase__			  =  image[0, -3:, -3:, -1]
												lowerCAmelCase__			  =  [
												    0.43_49_79_98,
												    0.91_81_40_65,
												    0.7_54_07_39,
												    0.55_58_02_05,
												    0.8_46_72_65,
												    0.5_38_96_91,
												    0.62_57_45_06,
												    0.58_89_77_63,
												    0.50_92_67_57,
												]

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

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

												# with sega enabled
												# generator = torch.manual_seed(seed)
												generator.manual_seed(_UpperCamelCase      )
												lowerCAmelCase__			  =  pipe(
												    [prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=_UpperCamelCase   ,  num_inference_steps=50   ,  output_type='np'   ,  width=5_12   ,  height=5_12   ,  **_UpperCamelCase   ,  )

												lowerCAmelCase__			  =  output.images
												lowerCAmelCase__			  =  image[0, -3:, -3:, -1]
												lowerCAmelCase__			  =  [
												    0.3_08_97_19,
												    0.30_50_01_44,
												    0.29_01_60_42,
												    0.30_63_09_64,
												    0.32_56_87,
												    0.29_41_92_25,
												    0.2_90_80_91,
												    0.28_72_35_98,
												    0.27_69_62_94,
												]

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

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






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  'cuda'
												lowerCAmelCase__			  =  StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'      )
												lowerCAmelCase__			  =  pipe.to(_UpperCamelCase      )
												pipe.set_progress_bar_config(disable=_UpperCamelCase      )

												lowerCAmelCase__			  =  'a castle next to a river'
												lowerCAmelCase__			  =  {
												    'editing_prompt': ['boat on a river, boat', 'monet, impression, sunrise'],
												    'reverse_editing_direction': False,
												    'edit_warmup_steps': [15, 18],
												    'edit_guidance_scale': 6,
												    'edit_threshold': [0.9, 0.8],
												    'edit_momentum_scale': 0.5,
												    'edit_mom_beta': 0.6,
												}

												lowerCAmelCase__			  =  48
												lowerCAmelCase__			  =  7

												# no sega enabled
												lowerCAmelCase__			  =  torch.Generator(_UpperCamelCase      )
												generator.manual_seed(_UpperCamelCase      )
												lowerCAmelCase__			  =  pipe(
												    [prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=_UpperCamelCase   ,  num_inference_steps=50   ,  output_type='np'   ,  width=5_12   ,  height=5_12   ,  )

												lowerCAmelCase__			  =  output.images
												lowerCAmelCase__			  =  image[0, -3:, -3:, -1]
												lowerCAmelCase__			  =  [
												    0.75_16_35_55,
												    0.76_03_71_45,
												    0.6_17_85,
												    0.9_18_96_73,
												    0.8_62_77_01,
												    0.85_18_96_94,
												    0.8_51_28_13,
												    0.87_01_20_76,
												    0.8_31_28_57,
												]

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

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

												# with sega enabled
												# generator = torch.manual_seed(seed)
												generator.manual_seed(_UpperCamelCase      )
												lowerCAmelCase__			  =  pipe(
												    [prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=_UpperCamelCase   ,  num_inference_steps=50   ,  output_type='np'   ,  width=5_12   ,  height=5_12   ,  **_UpperCamelCase   ,  )

												lowerCAmelCase__			  =  output.images
												lowerCAmelCase__			  =  image[0, -3:, -3:, -1]
												lowerCAmelCase__			  =  [
												    0.73_55_33_65,
												    0.7_53_72_71,
												    0.74_34_19_05,
												    0.66_48_03_56,
												    0.6_47_29_25,
												    0.63_03_94_16,
												    0.64_81_29_05,
												    0.6_74_97_17,
												    0.6_51_71_02,
												]

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

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







						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  'cuda'
												lowerCAmelCase__			  =  StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5'   ,  torch_dtype=torch.floataa      )
												lowerCAmelCase__			  =  pipe.to(_UpperCamelCase      )
												pipe.set_progress_bar_config(disable=_UpperCamelCase      )

												lowerCAmelCase__			  =  'a photo of a cat'
												lowerCAmelCase__			  =  {
												    'editing_prompt': ['sunglasses'],
												    'reverse_editing_direction': [False],
												    'edit_warmup_steps': 10,
												    'edit_guidance_scale': 6,
												    'edit_threshold': 0.95,
												    'edit_momentum_scale': 0.5,
												    'edit_mom_beta': 0.6,
												}

												lowerCAmelCase__			  =  3
												lowerCAmelCase__			  =  7

												# no sega enabled
												lowerCAmelCase__			  =  torch.Generator(_UpperCamelCase      )
												generator.manual_seed(_UpperCamelCase      )
												lowerCAmelCase__			  =  pipe(
												    [prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=_UpperCamelCase   ,  num_inference_steps=50   ,  output_type='np'   ,  width=5_12   ,  height=5_12   ,  )

												lowerCAmelCase__			  =  output.images
												lowerCAmelCase__			  =  image[0, -3:, -3:, -1]
												lowerCAmelCase__			  =  [
												    0.34_88_76_95,
												    0.3_87_69_53,
												    0.3_75,
												    0.34_42_38_28,
												    0.3_58_15_43,
												    0.35_71_77_73,
												    0.3_38_37_89,
												    0.34_57_03_12,
												    0.35_93_75,
												]

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

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

												# with sega enabled
												# generator = torch.manual_seed(seed)
												generator.manual_seed(_UpperCamelCase      )
												lowerCAmelCase__			  =  pipe(
												    [prompt]   ,  generator=_UpperCamelCase   ,  guidance_scale=_UpperCamelCase   ,  num_inference_steps=50   ,  output_type='np'   ,  width=5_12   ,  height=5_12   ,  **_UpperCamelCase   ,  )

												lowerCAmelCase__			  =  output.images
												lowerCAmelCase__			  =  image[0, -3:, -3:, -1]
												lowerCAmelCase__			  =  [
												    0.42_28_51_56,
												    0.36_91_40_62,
												    0.29_07_71_48,
												    0.42_04_10_16,
												    0.41_91_89_45,
												    0.35_49_80_47,
												    0.3_61_81_64,
												    0.4_42_38_28,
												    0.43_11_52_34,
												]

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

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













import csv
import dataclasses
import json
from dataclasses import dataclass
from typing import List, Optional, Union

from ...utils import is_tf_available, is_torch_available, logging


__snake_case			:			Union[str, Any]			   =   logging.get_logger(__name__)


@dataclass
class 			__SCREAMING_SNAKE_CASE    :
						_SCREAMING_SNAKE_CASE		:				str
						_SCREAMING_SNAKE_CASE		:				str
						_SCREAMING_SNAKE_CASE		:				Optional[str]            =    None
						_SCREAMING_SNAKE_CASE		:				Optional[str]            =    None






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												return json.dumps(dataclasses.asdict(self      )   ,  indent=2      ) + "\n"




@dataclass(frozen=__lowercase)
class 			__SCREAMING_SNAKE_CASE    :
						_SCREAMING_SNAKE_CASE		:				List[int]
						_SCREAMING_SNAKE_CASE		:				Optional[List[int]]            =    None
						_SCREAMING_SNAKE_CASE		:				Optional[List[int]]            =    None
						_SCREAMING_SNAKE_CASE		:				Optional[Union[int, float]]            =    None






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												return json.dumps(dataclasses.asdict(self      )      ) + "\n"




class 			__SCREAMING_SNAKE_CASE    :






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase      ):






												"""simple docstring"""
												raise NotImplementedError()






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase      ):






												"""simple docstring"""
												raise NotImplementedError()






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase      ):






												"""simple docstring"""
												raise NotImplementedError()






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase      ):






												"""simple docstring"""
												raise NotImplementedError()






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												raise NotImplementedError()






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase      ):






												"""simple docstring"""
												if len(self.get_labels()      ) > 1:
																		lowerCAmelCase__			  =  self.get_labels()[int(example.label      )]
												return example







						@classmethod
						def 					UpperCamelCase__					(		cls   ,  _UpperCamelCase   ,  _UpperCamelCase=None      ):






												"""simple docstring"""
												with open(_UpperCamelCase   ,  'r'   ,  encoding='utf-8-sig'      ) as f:
																		return list(csv.reader(_UpperCamelCase   ,  delimiter='\t'   ,  quotechar=_UpperCamelCase      )      )




class 			__SCREAMING_SNAKE_CASE    (      __lowercase):






						def __init__(		self   ,  _UpperCamelCase=None   ,  _UpperCamelCase=None   ,  _UpperCamelCase="classification"   ,  _UpperCamelCase=False      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  [] if labels is None else labels
												lowerCAmelCase__			  =  [] if examples is None else examples
												lowerCAmelCase__			  =  mode
												lowerCAmelCase__			  =  verbose






						def __len__(		self      ):






												"""simple docstring"""
												return len(self.examples      )






						def __getitem__(		self   ,  _UpperCamelCase      ):






												"""simple docstring"""
												if isinstance(_UpperCamelCase   ,  _UpperCamelCase      ):
																		return SingleSentenceClassificationProcessor(labels=self.labels   ,  examples=self.examples[idx]      )
												return self.examples[idx]






						@classmethod
						def 					UpperCamelCase__					(		cls   ,  _UpperCamelCase   ,  _UpperCamelCase=""   ,  _UpperCamelCase=0   ,  _UpperCamelCase=1   ,  _UpperCamelCase=None   ,  _UpperCamelCase=False   ,  **_UpperCamelCase      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  cls(**_UpperCamelCase      )
												processor.add_examples_from_csv(
												    _UpperCamelCase   ,  split_name=_UpperCamelCase   ,  column_label=_UpperCamelCase   ,  column_text=_UpperCamelCase   ,  column_id=_UpperCamelCase   ,  skip_first_row=_UpperCamelCase   ,  overwrite_labels=_UpperCamelCase   ,  overwrite_examples=_UpperCamelCase   ,  )
												return processor






						@classmethod
						def 					UpperCamelCase__					(		cls   ,  _UpperCamelCase   ,  _UpperCamelCase=None   ,  **_UpperCamelCase      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  cls(**_UpperCamelCase      )
												processor.add_examples(_UpperCamelCase   ,  labels=_UpperCamelCase      )
												return processor






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase=""   ,  _UpperCamelCase=0   ,  _UpperCamelCase=1   ,  _UpperCamelCase=None   ,  _UpperCamelCase=False   ,  _UpperCamelCase=False   ,  _UpperCamelCase=False   ,  ):






												"""simple docstring"""
												lowerCAmelCase__			  =  self._read_tsv(_UpperCamelCase      )
												if skip_first_row:
																		lowerCAmelCase__			  =  lines[1:]
												lowerCAmelCase__			  =  []
												lowerCAmelCase__			  =  []
												lowerCAmelCase__			  =  []
												for i, line in enumerate(_UpperCamelCase      ):
																		texts.append(line[column_text]      )
																		labels.append(line[column_label]      )
																		if column_id is not None:
																								ids.append(line[column_id]      )
																		else:
																								lowerCAmelCase__			  =  F"{split_name}-{i}" if split_name else str(_UpperCamelCase      )
																								ids.append(_UpperCamelCase      )

												return self.add_examples(
												    _UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  overwrite_labels=_UpperCamelCase   ,  overwrite_examples=_UpperCamelCase      )






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase=None   ,  _UpperCamelCase=None   ,  _UpperCamelCase=False   ,  _UpperCamelCase=False      ):






												"""simple docstring"""
												if labels is not None and len(_UpperCamelCase      ) != len(_UpperCamelCase      ):
																		raise ValueError(
																		    F"Text and labels have mismatched lengths {len(_UpperCamelCase      )} and {len(_UpperCamelCase      )}"      )
												if ids is not None and len(_UpperCamelCase      ) != len(_UpperCamelCase      ):
																		raise ValueError(F"Text and ids have mismatched lengths {len(_UpperCamelCase      )} and {len(_UpperCamelCase      )}"      )
												if ids is None:
																		lowerCAmelCase__			  =  [None] * len(_UpperCamelCase      )
												if labels is None:
																		lowerCAmelCase__			  =  [None] * len(_UpperCamelCase      )
												lowerCAmelCase__			  =  []
												lowerCAmelCase__			  =  set()
												for text_or_text_and_label, label, guid in zip(_UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      ):
																		if isinstance(_UpperCamelCase   ,  (tuple, list)      ) and label is None:
																								lowerCAmelCase__  ,				lowerCAmelCase__			  =  text_or_text_and_label
																		else:
																								lowerCAmelCase__			  =  text_or_text_and_label
																		added_labels.add(_UpperCamelCase      )
																		examples.append(InputExample(guid=_UpperCamelCase   ,  text_a=_UpperCamelCase   ,  text_b=_UpperCamelCase   ,  label=_UpperCamelCase      )      )

												# Update examples
												if overwrite_examples:
																		lowerCAmelCase__			  =  examples
												else:
																		self.examples.extend(_UpperCamelCase      )

												# Update labels
												if overwrite_labels:
																		lowerCAmelCase__			  =  list(_UpperCamelCase      )
												else:
																		lowerCAmelCase__			  =  list(set(self.labels      ).union(_UpperCamelCase      )      )

												return self.examples







						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase=None   ,  _UpperCamelCase=False   ,  _UpperCamelCase=0   ,  _UpperCamelCase=True   ,  _UpperCamelCase=None   ,  ):






												"""simple docstring"""
												if max_length is None:
																		lowerCAmelCase__			  =  tokenizer.max_len

												lowerCAmelCase__			  =  {label: i for i, label in enumerate(self.labels      )}

												lowerCAmelCase__			  =  []
												for ex_index, example in enumerate(self.examples      ):
																		if ex_index % 1_00_00 == 0:
																								logger.info(F"Tokenizing example {ex_index}"      )

																		lowerCAmelCase__			  =  tokenizer.encode(
																		    example.text_a   ,  add_special_tokens=_UpperCamelCase   ,  max_length=min(_UpperCamelCase   ,  tokenizer.max_len      )   ,  )
																		all_input_ids.append(_UpperCamelCase      )

												lowerCAmelCase__			  =  max(len(_UpperCamelCase      ) for input_ids in all_input_ids      )

												lowerCAmelCase__			  =  []
												for ex_index, (input_ids, example) in enumerate(zip(_UpperCamelCase   ,  self.examples      )      ):
																		if ex_index % 1_00_00 == 0:
																								logger.info(F"Writing example {ex_index}/{len(self.examples      )}"      )
																		# The mask has 1 for real tokens and 0 for padding tokens. Only real
																		# tokens are attended to.
																		lowerCAmelCase__			  =  [1 if mask_padding_with_zero else 0] * len(_UpperCamelCase      )

																		# Zero-pad up to the sequence length.
																		lowerCAmelCase__			  =  batch_length - len(_UpperCamelCase      )
																		if pad_on_left:
																								lowerCAmelCase__			  =  ([pad_token] * padding_length) + input_ids
																								lowerCAmelCase__			  =  ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
																		else:
																								lowerCAmelCase__			  =  input_ids + ([pad_token] * padding_length)
																								lowerCAmelCase__			  =  attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

																		if len(_UpperCamelCase      ) != batch_length:
																								raise ValueError(F"Error with input length {len(_UpperCamelCase      )} vs {batch_length}"      )
																		if len(_UpperCamelCase      ) != batch_length:
																								raise ValueError(F"Error with input length {len(_UpperCamelCase      )} vs {batch_length}"      )

																		if self.mode == "classification":
																								lowerCAmelCase__			  =  label_map[example.label]
																		elif self.mode == "regression":
																								lowerCAmelCase__			  =  float(example.label      )
																		else:
																								raise ValueError(self.mode      )

																		if ex_index < 5 and self.verbose:
																								logger.info('*** Example ***'      )
																								logger.info(F"guid: {example.guid}"      )
																								logger.info(F"input_ids: {' '.join([str(_UpperCamelCase      ) for x in input_ids]      )}"      )
																								logger.info(F"attention_mask: {' '.join([str(_UpperCamelCase      ) for x in attention_mask]      )}"      )
																								logger.info(F"label: {example.label} (id = {label})"      )

																		features.append(InputFeatures(input_ids=_UpperCamelCase   ,  attention_mask=_UpperCamelCase   ,  label=_UpperCamelCase      )      )

												if return_tensors is None:
																		return features
												elif return_tensors == "tf":
																		if not is_tf_available():
																								raise RuntimeError('return_tensors set to \'tf\' but TensorFlow 2.0 can\'t be imported'      )
																		import tensorflow as tf

																		def gen():
																								for ex in features:
																														yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)

																		lowerCAmelCase__			  =  tf.data.Dataset.from_generator(
																		    _UpperCamelCase   ,  ({'input_ids': tf.intaa, 'attention_mask': tf.intaa}, tf.intaa)   ,  ({'input_ids': tf.TensorShape([None]      ), 'attention_mask': tf.TensorShape([None]      )}, tf.TensorShape([]      ))   ,  )
																		return dataset
												elif return_tensors == "pt":
																		if not is_torch_available():
																								raise RuntimeError('return_tensors set to \'pt\' but PyTorch can\'t be imported'      )
																		import torch
																		from torch.utils.data import TensorDataset

																		lowerCAmelCase__			  =  torch.tensor([f.input_ids for f in features]   ,  dtype=torch.long      )
																		lowerCAmelCase__			  =  torch.tensor([f.attention_mask for f in features]   ,  dtype=torch.long      )
																		if self.mode == "classification":
																								lowerCAmelCase__			  =  torch.tensor([f.label for f in features]   ,  dtype=torch.long      )
																		elif self.mode == "regression":
																								lowerCAmelCase__			  =  torch.tensor([f.label for f in features]   ,  dtype=torch.float      )

																		lowerCAmelCase__			  =  TensorDataset(_UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      )
																		return dataset
												else:
																		raise ValueError('return_tensors should be one of \'tf\' or \'pt\''      )





