
'''simple docstring'''





import copy
import os
from typing import TYPE_CHECKING, List, Union


if TYPE_CHECKING:
				pass

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


lowercase__							:							Optional[Any]									=			logging.get_logger(__name__)

lowercase__							:							Tuple									=			{
    "kakaobrain/align-base": "https://huggingface.co/kakaobrain/align-base/resolve/main/config.json",
}






class        SCREAMING_SNAKE_CASE__    (							UpperCamelCase__    ):

							"""simple docstring"""






							_snake_case     		=  'align_text_model'



							def __init__(  self     ,	SCREAMING_SNAKE_CASE_=30522     ,	SCREAMING_SNAKE_CASE_=768     ,	SCREAMING_SNAKE_CASE_=12     ,	SCREAMING_SNAKE_CASE_=12     ,	SCREAMING_SNAKE_CASE_=3072     ,	SCREAMING_SNAKE_CASE_="gelu"     ,	SCREAMING_SNAKE_CASE_=0.1     ,	SCREAMING_SNAKE_CASE_=0.1     ,	SCREAMING_SNAKE_CASE_=512     ,	SCREAMING_SNAKE_CASE_=2     ,	SCREAMING_SNAKE_CASE_=0.0_2     ,	SCREAMING_SNAKE_CASE_=1E-12     ,	SCREAMING_SNAKE_CASE_=0     ,	SCREAMING_SNAKE_CASE_="absolute"     ,	SCREAMING_SNAKE_CASE_=True     ,	**SCREAMING_SNAKE_CASE_     ,	)->   Dict:





													'''simple docstring'''






													super().__init__(**_a			)

													__UpperCamelCase       =			vocab_size
													__UpperCamelCase       =			hidden_size
													__UpperCamelCase       =			num_hidden_layers
													__UpperCamelCase       =			num_attention_heads
													__UpperCamelCase       =			hidden_act
													__UpperCamelCase       =			intermediate_size
													__UpperCamelCase       =			hidden_dropout_prob
													__UpperCamelCase       =			attention_probs_dropout_prob
													__UpperCamelCase       =			max_position_embeddings
													__UpperCamelCase       =			type_vocab_size
													__UpperCamelCase       =			initializer_range
													__UpperCamelCase       =			layer_norm_eps
													__UpperCamelCase       =			position_embedding_type
													__UpperCamelCase       =			use_cache
													__UpperCamelCase       =			pad_token_id



							@classmethod
							def     A__		(  cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)->   "PretrainedConfig":





													'''simple docstring'''






													cls._set_token_in_kwargs(_a			)

													__UpperCamelCase       =			cls.get_config_dict(_a     ,	**_a			)

													# get the text config dict if we are loading from AlignConfig
													if config_dict.get('''model_type'''			) == "align":
																			__UpperCamelCase       =			config_dict["""text_config"""]

													if "model_type" in config_dict and hasattr(cls     ,	'''model_type'''			) and config_dict["model_type"] != cls.model_type:
																			logger.warning(
																			    F"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
																			    F"{cls.model_type}. This is not supported for all configurations of models and can yield errors."			)

													return cls.from_dict(_a     ,	**_a			)






class        SCREAMING_SNAKE_CASE__    (							UpperCamelCase__    ):

							"""simple docstring"""






							_snake_case     		=  'align_vision_model'



							def __init__(  self     ,	SCREAMING_SNAKE_CASE_ = 3     ,	SCREAMING_SNAKE_CASE_ = 600     ,	SCREAMING_SNAKE_CASE_ = 2.0     ,	SCREAMING_SNAKE_CASE_ = 3.1     ,	SCREAMING_SNAKE_CASE_ = 8     ,	SCREAMING_SNAKE_CASE_ = [3, 3, 5, 3, 5, 5, 3]     ,	SCREAMING_SNAKE_CASE_ = [32, 16, 24, 40, 80, 112, 192]     ,	SCREAMING_SNAKE_CASE_ = [16, 24, 40, 80, 112, 192, 320]     ,	SCREAMING_SNAKE_CASE_ = []     ,	SCREAMING_SNAKE_CASE_ = [1, 2, 2, 2, 1, 2, 1]     ,	SCREAMING_SNAKE_CASE_ = [1, 2, 2, 3, 3, 4, 1]     ,	SCREAMING_SNAKE_CASE_ = [1, 6, 6, 6, 6, 6, 6]     ,	SCREAMING_SNAKE_CASE_ = 0.2_5     ,	SCREAMING_SNAKE_CASE_ = "swish"     ,	SCREAMING_SNAKE_CASE_ = 2560     ,	SCREAMING_SNAKE_CASE_ = "mean"     ,	SCREAMING_SNAKE_CASE_ = 0.0_2     ,	SCREAMING_SNAKE_CASE_ = 0.0_0_1     ,	SCREAMING_SNAKE_CASE_ = 0.9_9     ,	SCREAMING_SNAKE_CASE_ = 0.2     ,	**SCREAMING_SNAKE_CASE_     ,	)->   str:





													'''simple docstring'''






													super().__init__(**_a			)

													__UpperCamelCase       =			num_channels
													__UpperCamelCase       =			image_size
													__UpperCamelCase       =			width_coefficient
													__UpperCamelCase       =			depth_coefficient
													__UpperCamelCase       =			depth_divisor
													__UpperCamelCase       =			kernel_sizes
													__UpperCamelCase       =			in_channels
													__UpperCamelCase       =			out_channels
													__UpperCamelCase       =			depthwise_padding
													__UpperCamelCase       =			strides
													__UpperCamelCase       =			num_block_repeats
													__UpperCamelCase       =			expand_ratios
													__UpperCamelCase       =			squeeze_expansion_ratio
													__UpperCamelCase       =			hidden_act
													__UpperCamelCase       =			hidden_dim
													__UpperCamelCase       =			pooling_type
													__UpperCamelCase       =			initializer_range
													__UpperCamelCase       =			batch_norm_eps
													__UpperCamelCase       =			batch_norm_momentum
													__UpperCamelCase       =			drop_connect_rate
													__UpperCamelCase       =			sum(_a			) * 4



							@classmethod
							def     A__		(  cls     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)->   "PretrainedConfig":





													'''simple docstring'''






													cls._set_token_in_kwargs(_a			)

													__UpperCamelCase       =			cls.get_config_dict(_a     ,	**_a			)

													# get the vision config dict if we are loading from AlignConfig
													if config_dict.get('''model_type'''			) == "align":
																			__UpperCamelCase       =			config_dict["""vision_config"""]

													if "model_type" in config_dict and hasattr(cls     ,	'''model_type'''			) and config_dict["model_type"] != cls.model_type:
																			logger.warning(
																			    F"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
																			    F"{cls.model_type}. This is not supported for all configurations of models and can yield errors."			)

													return cls.from_dict(_a     ,	**_a			)






class        SCREAMING_SNAKE_CASE__    (							UpperCamelCase__    ):

							"""simple docstring"""






							_snake_case     		=  'align'
							_snake_case     		=  True



							def __init__(  self     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=None     ,	SCREAMING_SNAKE_CASE_=640     ,	SCREAMING_SNAKE_CASE_=1.0     ,	SCREAMING_SNAKE_CASE_=0.0_2     ,	**SCREAMING_SNAKE_CASE_     ,	)->   List[str]:





													'''simple docstring'''






													super().__init__(**_a			)

													if text_config is None:
																			__UpperCamelCase       =			{}
																			logger.info('''text_config is None. Initializing the AlignTextConfig with default values.'''			)

													if vision_config is None:
																			__UpperCamelCase       =			{}
																			logger.info('''vision_config is None. Initializing the AlignVisionConfig with default values.'''			)

													__UpperCamelCase       =			AlignTextConfig(**_a			)
													__UpperCamelCase       =			AlignVisionConfig(**_a			)

													__UpperCamelCase       =			projection_dim
													__UpperCamelCase       =			temperature_init_value
													__UpperCamelCase       =			initializer_range



							@classmethod
							def     A__		(  cls     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)->   Tuple:





													'''simple docstring'''






													return cls(text_config=text_config.to_dict()     ,	vision_config=vision_config.to_dict()     ,	**_a			)



							def     A__		(  self			)->   str:





													'''simple docstring'''






													__UpperCamelCase       =			copy.deepcopy(self.__dict__			)
													__UpperCamelCase       =			self.text_config.to_dict()
													__UpperCamelCase       =			self.vision_config.to_dict()
													__UpperCamelCase       =			self.__class__.model_type
													return output






import math
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils import SchedulerMixin, SchedulerOutput






class        SCREAMING_SNAKE_CASE__    (							SCREAMING_SNAKE_CASE_   ,		SCREAMING_SNAKE_CASE_    ):

							"""simple docstring"""






							_snake_case     		=  1



							@register_to_config
							def __init__(  self     ,	SCREAMING_SNAKE_CASE_ = 1000     ,	SCREAMING_SNAKE_CASE_ = None			)->   Tuple:





													'''simple docstring'''






													self.set_timesteps(SCREAMING_SNAKE_CASE_			)

													# standard deviation of the initial noise distribution
													__UpperCamelCase       =			1.0

													# For now we only support F-PNDM, i.e. the runge-kutta method
													# For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf
													# mainly at formula (9), (12), (13) and the Algorithm 2.
													__UpperCamelCase       =			4

													# running values
													__UpperCamelCase       =			[]



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_ = None			)->   Optional[int]:





													'''simple docstring'''






													__UpperCamelCase       =			num_inference_steps
													__UpperCamelCase       =			torch.linspace(1     ,	0     ,	num_inference_steps + 1			)[:-1]
													__UpperCamelCase       =			torch.cat([steps, torch.tensor([0.0]			)]			)

													if self.config.trained_betas is not None:
																			__UpperCamelCase       =			torch.tensor(self.config.trained_betas     ,	dtype=torch.floataa			)
													else:
																			__UpperCamelCase       =			torch.sin(steps * math.pi / 2			) ** 2

													__UpperCamelCase       =			(1.0 - self.betas**2) ** 0.5

													__UpperCamelCase       =			(torch.atana(self.betas     ,	self.alphas			) / math.pi * 2)[:-1]
													__UpperCamelCase       =			timesteps.to(SCREAMING_SNAKE_CASE_			)

													__UpperCamelCase       =			[]



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_ = True     ,	)->   Union[SchedulerOutput, Tuple]:





													'''simple docstring'''






													if self.num_inference_steps is None:
																			raise ValueError(
																			    '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler'''			)

													__UpperCamelCase       =			(self.timesteps == timestep).nonzero().item()
													__UpperCamelCase       =			timestep_index + 1

													__UpperCamelCase       =			sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index]
													self.ets.append(SCREAMING_SNAKE_CASE_			)

													if len(self.ets			) == 1:
																			__UpperCamelCase       =			self.ets[-1]
													elif len(self.ets			) == 2:
																			__UpperCamelCase       =			(3 * self.ets[-1] - self.ets[-2]) / 2
													elif len(self.ets			) == 3:
																			__UpperCamelCase       =			(23 * self.ets[-1] - 16 * self.ets[-2] + 5 * self.ets[-3]) / 12
													else:
																			__UpperCamelCase       =			(1 / 24) * (55 * self.ets[-1] - 59 * self.ets[-2] + 37 * self.ets[-3] - 9 * self.ets[-4])

													__UpperCamelCase       =			self._get_prev_sample(SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			)

													if not return_dict:
																			return (prev_sample,)

													return SchedulerOutput(prev_sample=SCREAMING_SNAKE_CASE_			)



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_     ,	*SCREAMING_SNAKE_CASE_     ,	**SCREAMING_SNAKE_CASE_			)->   torch.FloatTensor:





													'''simple docstring'''






													return sample



							def     A__		(  self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_			)->   Optional[int]:





													'''simple docstring'''






													__UpperCamelCase       =			self.alphas[timestep_index]
													__UpperCamelCase       =			self.betas[timestep_index]

													__UpperCamelCase       =			self.alphas[prev_timestep_index]
													__UpperCamelCase       =			self.betas[prev_timestep_index]

													__UpperCamelCase       =			(sample - sigma * ets) / max(SCREAMING_SNAKE_CASE_     ,	1E-8			)
													__UpperCamelCase       =			next_alpha * pred + ets * next_sigma

													return prev_sample



							def __len__(  self			)->   Optional[int]:





													'''simple docstring'''






													return self.config.num_train_timesteps




