





'''simple docstring'''







import inspect

import jax
import jax.lax as lax
import jax.numpy as jnp

from ..utils import add_start_docstrings
from ..utils.logging import get_logger


lowerCAmelCase_					:    Optional[Any]      	=			get_logger(__name__)


lowerCAmelCase_					:    Tuple      	=			R'\n    Args:\n        input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`):\n            Indices of input sequence tokens in the vocabulary.\n\n            Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and\n            [`PreTrainedTokenizer.__call__`] for details.\n\n            [What are input IDs?](../glossary#input-ids)\n        scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`):\n            Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam\n            search or log softmax for each vocabulary token when using beam search\n        kwargs (`Dict[str, Any]`, *optional*):\n            Additional logits processor specific kwargs.\n\n    Return:\n        `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores.\n\n'
class 				__SCREAMING_SNAKE_CASE  :






				"""simple docstring"""







				@add_start_docstrings(__a					)
				def __call__(  self     :					Dict					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					):
								raise NotImplementedError(
								    f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'					)


class 				__SCREAMING_SNAKE_CASE  :






				"""simple docstring"""







				@add_start_docstrings(__a					)
				def __call__(  self     :					List[str]					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					):
								raise NotImplementedError(
								    f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'					)


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				@add_start_docstrings(__a					)
				def __call__(  self     :					Tuple					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					,				__a     :					int					,				**__a     :					Union[str, Any]					):
								for processor in self:
												_a								= inspect.signature(processor.__call__					).parameters
												if len(__a					) > 3:
																if not all(arg in kwargs for arg in list(function_args.keys()					)[2:]					):
																				raise ValueError(
																				    f'Make sure that all the required parameters: {list(function_args.keys()					)} for '
																				    f'{processor.__class__} are passed to the logits processor.'					)
																_a								= processor(__a					,				__a					,				__a					,				**__a					)
												else:
																_a								= processor(__a					,				__a					,				__a					)
								return scores


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				def __init__(  self     :					Dict					,				__a     :					float					):
								if not isinstance(__a					,				__a					) or not (temperature > 0):
												raise ValueError(f'`temperature` has to be a strictly positive float, but is {temperature}'					)

								_a								= temperature




				def __call__(  self     :					int					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					,				__a     :					int					):
								_a								= scores / self.temperature
								return scores


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				def __init__(  self     :					Optional[int]					,				__a     :					float					,				__a     :					float = -float("Inf"					)					,				__a     :					int = 1					):
								if not isinstance(__a					,				__a					) or (top_p < 0 or top_p > 1.0):
												raise ValueError(f'`top_p` has to be a float > 0 and < 1, but is {top_p}'					)
								if not isinstance(__a					,				__a					) or (min_tokens_to_keep < 1):
												raise ValueError(f'`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}'					)

								_a								= top_p
								_a								= filter_value
								_a								= min_tokens_to_keep




				def __call__(  self     :					Dict					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					,				__a     :					int					):
								_a	,					_a								= lax.top_k(__a					,				scores.shape[-1]					)

								_a								= jnp.full_like(__a					,				self.filter_value					)
								_a								= jax.nn.softmax(__a					,				axis=-1					).cumsum(axis=-1					)
								_a								= cumulative_probs < self.top_p

								# include the token that is higher than top_p as well
								_a								= jnp.roll(__a					,				1					)
								score_mask |= score_mask.at[:, 0].set(__a					)

								# min tokens to keep
								_a								= score_mask.at[:, : self.min_tokens_to_keep].set(__a					)

								_a								= jnp.where(__a					,				__a					,				__a					)
								_a								= jax.lax.sort_key_val(__a					,				__a					)[-1]

								return next_scores


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				def __init__(  self     :					Optional[Any]					,				__a     :					int					,				__a     :					float = -float("Inf"					)					,				__a     :					int = 1					):
								if not isinstance(__a					,				__a					) or top_k <= 0:
												raise ValueError(f'`top_k` has to be a strictly positive integer, but is {top_k}'					)

								_a								= max(__a					,				__a					)
								_a								= filter_value




				def __call__(  self     :					Dict					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					,				__a     :					int					):
								_a	,					_a								= scores.shape
								_a								= jnp.full(batch_size * vocab_size					,				self.filter_value					)

								_a								= min(self.top_k					,				scores.shape[-1]					)  # Safety check
								_a	,					_a								= lax.top_k(__a					,				__a					)
								_a								= jnp.broadcast_to((jnp.arange(__a					) * vocab_size)[:, None]					,				(batch_size, topk)					).flatten()
								_a								= topk_scores.flatten()
								_a								= topk_indices.flatten() + shift

								_a								= next_scores_flat.at[topk_indices_flat].set(__a					)
								_a								= next_scores_flat.reshape(__a					,				__a					)
								return next_scores


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				def __init__(  self     :					Union[str, Any]					,				__a     :					int					):
								_a								= bos_token_id




				def __call__(  self     :					Optional[int]					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					,				__a     :					int					):
								_a								= jnp.full(scores.shape					,				-float("inf"					)					)

								_a								= 1 - jnp.bool_(cur_len - 1					)

								_a								= jnp.where(__a					,				new_scores.at[:, self.bos_token_id].set(0					)					,				__a					)

								return scores


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				def __init__(  self     :					Tuple					,				__a     :					int					,				__a     :					int					):
								_a								= max_length
								_a								= eos_token_id




				def __call__(  self     :					List[Any]					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					,				__a     :					int					):
								_a								= jnp.full(scores.shape					,				-float("inf"					)					)

								_a								= 1 - jnp.bool_(cur_len - self.max_length + 1					)

								_a								= jnp.where(__a					,				new_scores.at[:, self.eos_token_id].set(0					)					,				__a					)

								return scores


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				def __init__(  self     :					List[str]					,				__a     :					int					,				__a     :					int					):
								if not isinstance(__a					,				__a					) or min_length < 0:
												raise ValueError(f'`min_length` has to be a positive integer, but is {min_length}'					)

								if not isinstance(__a					,				__a					) or eos_token_id < 0:
												raise ValueError(f'`eos_token_id` has to be a positive integer, but is {eos_token_id}'					)

								_a								= min_length
								_a								= eos_token_id




				def __call__(  self     :					Dict					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					,				__a     :					int					):
								# create boolean flag to decide if min length penalty should be applied
								_a								= 1 - jnp.clip(cur_len - self.min_length					,				0					,				1					)

								_a								= jnp.where(__a					,				scores.at[:, self.eos_token_id].set(-float("inf"					)					)					,				__a					)

								return scores


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				def __init__(  self     :					Any					,				__a     :					Any					,				__a     :					Union[str, Any]					):
								_a								= list(__a					)
								_a								= begin_index




				def __call__(  self     :					int					,				__a     :					List[Any]					,				__a     :					List[Any]					,				__a     :					int					):
								_a								= 1 - jnp.bool_(cur_len - self.begin_index					)

								_a								= jnp.where(__a					,				scores.at[:, self.begin_suppress_tokens].set(-float("inf"					)					)					,				__a					)

								return scores


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				def __init__(  self     :					Union[str, Any]					,				__a     :					list					):
								_a								= list(__a					)




				def __call__(  self     :					Tuple					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					,				__a     :					int					):
								_a								= scores.at[..., self.suppress_tokens].set(-float("inf"					)					)

								return scores


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				def __init__(  self     :					int					,				__a     :					int					):
								_a								= dict(__a					)
								# Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the
								# index of the array corresponds to the index of the token to be forced, for XLA compatibility.
								# Indexes without forced tokens will have a negative value.
								_a								= jnp.ones((max(force_token_map.keys()					) + 1)					,				dtype=jnp.intaa					) * -1
								for index, token in force_token_map.items():
												if token is not None:
																_a								= force_token_array.at[index].set(__a					)
								_a								= jnp.intaa(__a					)




				def __call__(  self     :					List[Any]					,				__a     :					jnp.ndarray					,				__a     :					jnp.ndarray					,				__a     :					int					):
								def _force_token(__a     :					Dict					):
												_a								= scores.shape[0]
												_a								= self.force_token_array[generation_idx]

												_a								= jnp.ones_like(__a					,				dtype=scores.dtype					) * -float("inf"					)
												_a								= jnp.zeros((batch_size, 1)					,				dtype=scores.dtype					)
												_a								= lax.dynamic_update_slice(__a					,				__a					,				(0, current_token)					)
												return new_scores

								_a								= lax.cond(
								    cur_len >= self.force_token_array.shape[0]					,				lambda: scores					,				lambda: lax.cond(
								        self.force_token_array[cur_len] >= 0					,				lambda: _force_token(__a					)					,				lambda: scores					,				)					,				)
								return scores


class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				def __init__(  self     :					str					,				__a     :					int					,				__a     :					Optional[int]					,				__a     :					List[str]					):
								_a								= generate_config.eos_token_id
								_a								= generate_config.no_timestamps_token_id
								_a								= generate_config.no_timestamps_token_id + 1

								_a								= decoder_input_length + 1

								if generate_config.is_multilingual:
												# room for language token and task token
												self.begin_index += 2
								if hasattr(__a					,				"max_initial_timestamp_index"					):
												_a								= generate_config.max_initial_timestamp_index
								else:
												_a								= model_config.vocab_size
								if self.max_initial_timestamp_index is None:
												_a								= model_config.vocab_size




				def __call__(  self     :					List[Any]					,				__a     :					Union[str, Any]					,				__a     :					int					,				__a     :					Tuple					):
								# suppress <|notimestamps|> which is handled by without_timestamps
								_a								= scores.at[:, self.no_timestamps_token_id].set(-float("inf"					)					)

								def handle_pairs(__a     :					Tuple					,				__a     :					str					):
												_a								= jnp.where((cur_len - self.begin_index) >= 1					,				__a					,				__a					)
												_a								= jnp.where(
												    input_ids_k[cur_len - 1] >= self.timestamp_begin					,				True and last_was_timestamp					,				__a					,				)

												_a								= jnp.where((cur_len - self.begin_index) < 2					,				__a					,				__a					)
												_a								= jnp.where(
												    input_ids_k[cur_len - 2] >= self.timestamp_begin					,				__a					,				__a					,				)

												return jnp.where(
												    __a					,				jnp.where(
												        penultimate_was_timestamp > 0					,				scores_k.at[self.timestamp_begin :].set(-float("inf"					)					)					,				scores_k.at[: self.eos_token_id].set(-float("inf"					)					)					,				)					,				__a					,				)

								_a								= jax.vmap(__a					)(__a					,				__a					)

								_a								= jnp.where(cur_len == self.begin_index					,				__a					,				__a					)
								_a								= jnp.where(
								    self.max_initial_timestamp_index is not None					,				True and apply_max_initial_timestamp					,				__a					,				)

								_a								= self.timestamp_begin + self.max_initial_timestamp_index

								_a								= jnp.where(
								    __a					,				scores.at[:, last_allowed + 1 :].set(-float("inf"					)					)					,				__a					,				)

								# if sum of probability over timestamps is above any other token, sample timestamp
								_a								= jax.nn.log_softmax(__a					,				axis=-1					)

								def handle_cumulative_probs(__a     :					List[Any]					,				__a     :					Tuple					):
												_a								= jax.nn.logsumexp(logprobs_k[self.timestamp_begin :]					,				axis=-1					)
												_a								= jnp.max(logprobs_k[: self.timestamp_begin]					)
												return jnp.where(
												    timestamp_logprob > max_text_token_logprob					,				scores_k.at[: self.timestamp_begin].set(-float("inf"					)					)					,				__a					,				)

								_a								= jax.vmap(__a					)(__a					,				__a					)

								return scores







'''simple docstring'''







import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin
class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_       ,							lowerCamelCase_      ):






				"""simple docstring"""







				@register_to_config
				def __init__(  self     :					Dict					,				*,
				    __a     :					int = 4					,				__a     :					int = 7_68					,				__a     :					int					,				__a     :					int					,				):
								super().__init__()

								_a								= nn.Parameter(torch.zeros(__a					)					)

								# parameters for additional clip time embeddings
								_a								= nn.Linear(__a					,				__a					)
								_a								= nn.Linear(__a					,				__a					)

								# parameters for encoder hidden states
								_a								= clip_extra_context_tokens
								_a								= nn.Linear(
								    __a					,				self.clip_extra_context_tokens * cross_attention_dim					)
								_a								= nn.Linear(__a					,				__a					)
								_a								= nn.LayerNorm(__a					)




				def   UpperCamelCase__   (  self     :					Optional[Any]					,				*, __a     :					Tuple					,				__a     :					Union[str, Any]					,				__a     :					Any					,				__a     :					List[Any]					):
								if do_classifier_free_guidance:
												# Add the classifier free guidance embeddings to the image embeddings
												_a								= image_embeddings.shape[0]
												_a								= self.learned_classifier_free_guidance_embeddings.unsqueeze(0					)
												_a								= classifier_free_guidance_embeddings.expand(
												    __a					,				-1					)
												_a								= torch.cat([classifier_free_guidance_embeddings, image_embeddings]					,				dim=0					)

								# The image embeddings batch size and the text embeddings batch size are equal
								assert image_embeddings.shape[0] == prompt_embeds.shape[0]

								_a								= prompt_embeds.shape[0]

								# "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
								# adding CLIP embeddings to the existing timestep embedding, ...
								_a								= self.embedding_proj(__a					)
								_a								= self.clip_image_embeddings_project_to_time_embeddings(__a					)
								_a								= time_projected_image_embeddings + time_projected_prompt_embeds

								# ... and by projecting CLIP embeddings into four
								# extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
								_a								= self.clip_extra_context_tokens_proj(__a					)
								_a								= clip_extra_context_tokens.reshape(__a					,				-1					,				self.clip_extra_context_tokens					)
								_a								= clip_extra_context_tokens.permute(0					,				2					,				1					)

								_a								= self.encoder_hidden_states_proj(__a					)
								_a								= self.text_encoder_hidden_states_norm(__a					)
								_a								= torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]					,				dim=1					)

								return text_encoder_hidden_states, additive_clip_time_embeddings
