
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_clip import CLIPTokenizer


A__       :   Dict   						=					logging.get_logger(__name__)

A__       :   Union[str, Any]   						=					{"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

A__       :   List[str]   						=					{
    """vocab_file""": {
        """openai/clip-vit-base-patch32""": """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """openai/clip-vit-base-patch32""": """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """openai/clip-vit-base-patch32""": (
            """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/tokenizer.json"""
        ),
    },
}

A__       :   Optional[int]   						=					{
    """openai/clip-vit-base-patch32""": 7_7,
}





class 						lowercase       (  __UpperCamelCase					):
					__a					=    VOCAB_FILES_NAMES
					__a					=    PRETRAINED_VOCAB_FILES_MAP
					__a					=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					__a					=    ["""input_ids""", """attention_mask"""]
					__a					=    CLIPTokenizer
					def __init__(		self ,	SCREAMING_SNAKE_CASE__=None ,	SCREAMING_SNAKE_CASE__=None ,	SCREAMING_SNAKE_CASE__=None ,	SCREAMING_SNAKE_CASE__="<|endoftext|>" ,	SCREAMING_SNAKE_CASE__="<|startoftext|>" ,	SCREAMING_SNAKE_CASE__="<|endoftext|>" ,	SCREAMING_SNAKE_CASE__="<|endoftext|>" ,	**SCREAMING_SNAKE_CASE__ ,	):
								"""simple docstring"""







								super().__init__(
								    SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ ,	tokenizer_file=SCREAMING_SNAKE_CASE__ ,	unk_token=SCREAMING_SNAKE_CASE__ ,	bos_token=SCREAMING_SNAKE_CASE__ ,	eos_token=SCREAMING_SNAKE_CASE__ ,	pad_token=SCREAMING_SNAKE_CASE__ ,	**SCREAMING_SNAKE_CASE__ ,	)

								if not isinstance(self.backend_tokenizer.pre_tokenizer ,	pre_tokenizers.Sequence    ):
											raise ValueError(
											    '''The `backend_tokenizer` provided does not match the expected format. The CLIP tokenizer has been'''
											    ''' heavily modified from transformers version 4.17.0. You need to convert the tokenizer you are using'''
											    ''' to be compatible with this version.The easiest way to do so is'''
											    ''' `CLIPTokenizerFast.from_pretrained("path_to_local_folder_or_hub_repo, from_slow=True)`. If you want'''
											    ''' to use your existing tokenizer, you will have to revert to a version prior to 4.17.0 of'''
											    ''' transformers.'''    )

								self._wrap_decode_method_backend_tokenizer()
					def lowercase_			(		self    ):
								"""simple docstring"""







								lowerCAmelCase__ :    List[Any]				      =			self.backend_tokenizer.decode

								def new_decode_method(*SCREAMING_SNAKE_CASE__ ,	**SCREAMING_SNAKE_CASE__    ):
											lowerCAmelCase__ :    Optional[int]				      =			orig_decode_method(*SCREAMING_SNAKE_CASE__ ,	**SCREAMING_SNAKE_CASE__    )
											lowerCAmelCase__ :    Any				      =			text.replace(self.backend_tokenizer.model.end_of_word_suffix ,	''' '''    ).strip()
											return text

								lowerCAmelCase__ :    str				      =			new_decode_method
					def lowercase_			(		self ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ = None    ):
								"""simple docstring"""







								lowerCAmelCase__ :    int				      =			[self.bos_token_id]
								lowerCAmelCase__ :    Optional[int]				      =			[self.eos_token_id]

								if token_ids_a is None:
											return bos_token + token_ids_a + eos_token
								return bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token
					def lowercase_			(		self ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ = None    ):
								"""simple docstring"""







								lowerCAmelCase__ :    Union[str, Any]				      =			[self.bos_token_id]
								lowerCAmelCase__ :    Any				      =			[self.eos_token_id]

								if token_ids_a is None:
											return len(bos_token + token_ids_a + eos_token    ) * [0]
								return len(bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token    ) * [0]
					def lowercase_			(		self ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ = None    ):
								"""simple docstring"""







								lowerCAmelCase__ :    List[str]				      =			self._tokenizer.model.save(SCREAMING_SNAKE_CASE__ ,	name=SCREAMING_SNAKE_CASE__    )
								return tuple(SCREAMING_SNAKE_CASE__    )





import torch

from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest





class 						lowercase       (  __UpperCamelCase					):
					__a					=    (HeunDiscreteScheduler,)
					__a					=    10
					def lowercase_			(		self ,	**SCREAMING_SNAKE_CASE__    ):
								"""simple docstring"""







								lowerCAmelCase__ :    Any				      =			{
								    '''num_train_timesteps''': 1100,
								    '''beta_start''': 0.0_001,
								    '''beta_end''': 0.02,
								    '''beta_schedule''': '''linear''',
								}

								config.update(**SCREAMING_SNAKE_CASE__    )
								return config
					def lowercase_			(		self    ):
								"""simple docstring"""







								for timesteps in [10, 50, 100, 1000]:
											self.check_over_configs(num_train_timesteps=SCREAMING_SNAKE_CASE__    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								for beta_start, beta_end in zip([0.00_001, 0.0_001, 0.001] ,	[0.0_002, 0.002, 0.02]    ):
											self.check_over_configs(beta_start=SCREAMING_SNAKE_CASE__ ,	beta_end=SCREAMING_SNAKE_CASE__    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								for schedule in ["linear", "scaled_linear", "exp"]:
											self.check_over_configs(beta_schedule=SCREAMING_SNAKE_CASE__    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								for clip_sample_range in [1.0, 2.0, 3.0]:
											self.check_over_configs(clip_sample_range=SCREAMING_SNAKE_CASE__ ,	clip_sample=SCREAMING_SNAKE_CASE__    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								for prediction_type in ["epsilon", "v_prediction", "sample"]:
											self.check_over_configs(prediction_type=SCREAMING_SNAKE_CASE__    )
					def lowercase_			(		self    ):
								"""simple docstring"""







								lowerCAmelCase__ :    List[str]				      =			self.scheduler_classes[0]
								lowerCAmelCase__ :    Optional[Any]				      =			self.get_scheduler_config()
								lowerCAmelCase__ :    Optional[Any]				      =			scheduler_class(**SCREAMING_SNAKE_CASE__    )

								scheduler.set_timesteps(self.num_inference_steps    )

								lowerCAmelCase__ :    str				      =			self.dummy_model()
								lowerCAmelCase__ :    Optional[int]				      =			self.dummy_sample_deter * scheduler.init_noise_sigma
								lowerCAmelCase__ :    Optional[Any]				      =			sample.to(SCREAMING_SNAKE_CASE__    )

								for i, t in enumerate(scheduler.timesteps    ):
											lowerCAmelCase__ :    List[Any]				      =			scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    str				      =			model(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Tuple				      =			scheduler.step(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )
											lowerCAmelCase__ :    Union[str, Any]				      =			output.prev_sample

								lowerCAmelCase__ :    Any				      =			torch.sum(torch.abs(SCREAMING_SNAKE_CASE__    )    )
								lowerCAmelCase__ :    Union[str, Any]				      =			torch.mean(torch.abs(SCREAMING_SNAKE_CASE__    )    )

								if torch_device in ["cpu", "mps"]:
											assert abs(result_sum.item() - 0.1_233    ) < 1E-2
											assert abs(result_mean.item() - 0.0_002    ) < 1E-3
								else:
											# CUDA
											assert abs(result_sum.item() - 0.1_233    ) < 1E-2
											assert abs(result_mean.item() - 0.0_002    ) < 1E-3
					def lowercase_			(		self    ):
								"""simple docstring"""







								lowerCAmelCase__ :    Optional[Any]				      =			self.scheduler_classes[0]
								lowerCAmelCase__ :    str				      =			self.get_scheduler_config(prediction_type='''v_prediction'''    )
								lowerCAmelCase__ :    Any				      =			scheduler_class(**SCREAMING_SNAKE_CASE__    )

								scheduler.set_timesteps(self.num_inference_steps    )

								lowerCAmelCase__ :    int				      =			self.dummy_model()
								lowerCAmelCase__ :    List[str]				      =			self.dummy_sample_deter * scheduler.init_noise_sigma
								lowerCAmelCase__ :    List[Any]				      =			sample.to(SCREAMING_SNAKE_CASE__    )

								for i, t in enumerate(scheduler.timesteps    ):
											lowerCAmelCase__ :    int				      =			scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Optional[Any]				      =			model(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Optional[int]				      =			scheduler.step(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )
											lowerCAmelCase__ :    Tuple				      =			output.prev_sample

								lowerCAmelCase__ :    List[Any]				      =			torch.sum(torch.abs(SCREAMING_SNAKE_CASE__    )    )
								lowerCAmelCase__ :    List[str]				      =			torch.mean(torch.abs(SCREAMING_SNAKE_CASE__    )    )

								if torch_device in ["cpu", "mps"]:
											assert abs(result_sum.item() - 4.6934E-07    ) < 1E-2
											assert abs(result_mean.item() - 6.1112E-10    ) < 1E-3
								else:
											# CUDA
											assert abs(result_sum.item() - 4.693_4286_5017_0972E-07    ) < 1E-2
											assert abs(result_mean.item() - 0.0_002    ) < 1E-3
					def lowercase_			(		self    ):
								"""simple docstring"""







								lowerCAmelCase__ :    Optional[int]				      =			self.scheduler_classes[0]
								lowerCAmelCase__ :    Optional[Any]				      =			self.get_scheduler_config()
								lowerCAmelCase__ :    Union[str, Any]				      =			scheduler_class(**SCREAMING_SNAKE_CASE__    )

								scheduler.set_timesteps(self.num_inference_steps ,	device=SCREAMING_SNAKE_CASE__    )

								lowerCAmelCase__ :    Optional[Any]				      =			self.dummy_model()
								lowerCAmelCase__ :    Dict				      =			self.dummy_sample_deter.to(SCREAMING_SNAKE_CASE__    ) * scheduler.init_noise_sigma

								for t in scheduler.timesteps:
											lowerCAmelCase__ :    int				      =			scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Dict				      =			model(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Any				      =			scheduler.step(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )
											lowerCAmelCase__ :    List[Any]				      =			output.prev_sample

								lowerCAmelCase__ :    Dict				      =			torch.sum(torch.abs(SCREAMING_SNAKE_CASE__    )    )
								lowerCAmelCase__ :    List[str]				      =			torch.mean(torch.abs(SCREAMING_SNAKE_CASE__    )    )

								if str(SCREAMING_SNAKE_CASE__    ).startswith('''cpu'''    ):
											# The following sum varies between 148 and 156 on mps. Why?
											assert abs(result_sum.item() - 0.1_233    ) < 1E-2
											assert abs(result_mean.item() - 0.0_002    ) < 1E-3
								elif str(SCREAMING_SNAKE_CASE__    ).startswith('''mps'''    ):
											# Larger tolerance on mps
											assert abs(result_mean.item() - 0.0_002    ) < 1E-2
								else:
											# CUDA
											assert abs(result_sum.item() - 0.1_233    ) < 1E-2
											assert abs(result_mean.item() - 0.0_002    ) < 1E-3
					def lowercase_			(		self    ):
								"""simple docstring"""







								lowerCAmelCase__ :    Any				      =			self.scheduler_classes[0]
								lowerCAmelCase__ :    Optional[Any]				      =			self.get_scheduler_config()
								lowerCAmelCase__ :    str				      =			scheduler_class(**SCREAMING_SNAKE_CASE__ ,	use_karras_sigmas=SCREAMING_SNAKE_CASE__    )

								scheduler.set_timesteps(self.num_inference_steps ,	device=SCREAMING_SNAKE_CASE__    )

								lowerCAmelCase__ :    str				      =			self.dummy_model()
								lowerCAmelCase__ :    Any				      =			self.dummy_sample_deter.to(SCREAMING_SNAKE_CASE__    ) * scheduler.init_noise_sigma
								lowerCAmelCase__ :    List[Any]				      =			sample.to(SCREAMING_SNAKE_CASE__    )

								for t in scheduler.timesteps:
											lowerCAmelCase__ :    Union[str, Any]				      =			scheduler.scale_model_input(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    Any				      =			model(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )

											lowerCAmelCase__ :    str				      =			scheduler.step(SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__ ,	SCREAMING_SNAKE_CASE__    )
											lowerCAmelCase__ :    List[Any]				      =			output.prev_sample

								lowerCAmelCase__ :    int				      =			torch.sum(torch.abs(SCREAMING_SNAKE_CASE__    )    )
								lowerCAmelCase__ :    Dict				      =			torch.mean(torch.abs(SCREAMING_SNAKE_CASE__    )    )

								assert abs(result_sum.item() - 0.00_015    ) < 1E-2
								assert abs(result_mean.item() - 1.9869_5545_3503_4695E-07    ) < 1E-2



