






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


SCREAMING_SNAKE_CASE__      :			int	=		get_logger(__name__)


SCREAMING_SNAKE_CASE__      :			List[Any]	=		r'''
    Args:
        input_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`):
            Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam
            search or log softmax for each vocabulary token when using beam search
        kwargs (`Dict[str, Any]`, *optional*):
            Additional logits processor specific kwargs.

    Return:
        `jnp.ndarray` of shape `(batch_size, config.vocab_size)`: The processed prediction scores.

'''
class 						lowerCamelCase_				:







				@add_start_docstrings(_A     )
				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								raise NotImplementedError(
								    F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''     )







class 						lowerCamelCase_				:







				@add_start_docstrings(_A     )
				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								raise NotImplementedError(
								    F'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''     )







class 						lowerCamelCase_				(      snake_case__ ):







				@add_start_docstrings(_A     )
				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					**__lowerCAmelCase     ):
								"""simple docstring"""
								for processor in self:
												__magic_name__		:str        =      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.'''     )
																__magic_name__		:Any        =      processor(_A       ,					_A       ,					_A       ,					**_A     )
												else:
																__magic_name__		:Union[str, Any]        =      processor(_A       ,					_A       ,					_A     )
								return scores







class 						lowerCamelCase_				(      snake_case__ ):







				def __init__(						self       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								if not isinstance(_A       ,					_A     ) or not (temperature > 0):
												raise ValueError(F'''`temperature` has to be a strictly positive float, but is {temperature}'''     )

								__magic_name__		:Optional[int]        =      temperature







				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Dict        =      scores / self.temperature
								return scores







class 						lowerCamelCase_				(      snake_case__ ):







				def __init__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase = -float('''Inf'''     )       ,					__lowerCAmelCase = 1     ):
								"""simple docstring"""
								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}'''     )

								__magic_name__		:Optional[Any]        =      top_p
								__magic_name__		:Optional[Any]        =      filter_value
								__magic_name__		:List[str]        =      min_tokens_to_keep







				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]        =      lax.top_k(_A       ,					scores.shape[-1]     )

								__magic_name__		:Union[str, Any]        =      jnp.full_like(_A       ,					self.filter_value     )
								__magic_name__		:int        =      jax.nn.softmax(_A       ,					axis=-1     ).cumsum(axis=-1     )
								__magic_name__		:Union[str, Any]        =      cumulative_probs < self.top_p

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

								# min tokens to keep
								__magic_name__		:int        =      score_mask.at[:, : self.min_tokens_to_keep].set(_A     )

								__magic_name__		:Tuple        =      jnp.where(_A       ,					_A       ,					_A     )
								__magic_name__		:List[str]        =      jax.lax.sort_key_val(_A       ,					_A     )[-1]

								return next_scores







class 						lowerCamelCase_				(      snake_case__ ):







				def __init__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase = -float('''Inf'''     )       ,					__lowerCAmelCase = 1     ):
								"""simple docstring"""
								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}'''     )

								__magic_name__		:Dict        =      max(_A       ,					_A     )
								__magic_name__		:Tuple        =      filter_value







				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      scores.shape
								__magic_name__		:List[Any]        =      jnp.full(batch_size * vocab_size       ,					self.filter_value     )

								__magic_name__		:Optional[int]        =      min(self.top_k       ,					scores.shape[-1]     )  # Safety check
								__magic_name__		:Dict        =      lax.top_k(_A       ,					_A     )
								__magic_name__		:Optional[Any]        =      jnp.broadcast_to((jnp.arange(_A     ) * vocab_size)[:, None]       ,					(batch_size, topk)     ).flatten()
								__magic_name__		:Union[str, Any]        =      topk_scores.flatten()
								__magic_name__		:List[Any]        =      topk_indices.flatten() + shift

								__magic_name__		:Dict        =      next_scores_flat.at[topk_indices_flat].set(_A     )
								__magic_name__		:Optional[Any]        =      next_scores_flat.reshape(_A       ,					_A     )
								return next_scores







class 						lowerCamelCase_				(      snake_case__ ):







				def __init__(						self       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Optional[int]        =      bos_token_id







				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Optional[Any]        =      jnp.full(scores.shape       ,					-float('''inf'''     )     )

								__magic_name__		:List[Any]        =      1 - jnp.bool_(cur_len - 1     )

								__magic_name__		:Union[str, Any]        =      jnp.where(_A       ,					new_scores.at[:, self.bos_token_id].set(0     )       ,					_A     )

								return scores







class 						lowerCamelCase_				(      snake_case__ ):







				def __init__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]        =      max_length
								__magic_name__		:Union[str, Any]        =      eos_token_id







				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Optional[int]        =      jnp.full(scores.shape       ,					-float('''inf'''     )     )

								__magic_name__		:str        =      1 - jnp.bool_(cur_len - self.max_length + 1     )

								__magic_name__		:Optional[Any]        =      jnp.where(_A       ,					new_scores.at[:, self.eos_token_id].set(0     )       ,					_A     )

								return scores







class 						lowerCamelCase_				(      snake_case__ ):







				def __init__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								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}'''     )

								__magic_name__		:Tuple        =      min_length
								__magic_name__		:List[Any]        =      eos_token_id







				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								# create boolean flag to decide if min length penalty should be applied
								__magic_name__		:Optional[Any]        =      1 - jnp.clip(cur_len - self.min_length       ,					0       ,					1     )

								__magic_name__		:Union[str, Any]        =      jnp.where(_A       ,					scores.at[:, self.eos_token_id].set(-float('''inf'''     )     )       ,					_A     )

								return scores







class 						lowerCamelCase_				(      snake_case__ ):







				def __init__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:List[str]        =      list(_A     )
								__magic_name__		:List[Any]        =      begin_index







				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Optional[Any]        =      1 - jnp.bool_(cur_len - self.begin_index     )

								__magic_name__		:Tuple        =      jnp.where(_A       ,					scores.at[:, self.begin_suppress_tokens].set(-float('''inf'''     )     )       ,					_A     )

								return scores







class 						lowerCamelCase_				(      snake_case__ ):







				def __init__(						self       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:List[str]        =      list(_A     )







				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      scores.at[..., self.suppress_tokens].set(-float('''inf'''     )     )

								return scores







class 						lowerCamelCase_				(      snake_case__ ):







				def __init__(						self       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Dict        =      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.
								__magic_name__		:Optional[Any]        =      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:
																__magic_name__		:Tuple        =      force_token_array.at[index].set(_A     )
								__magic_name__		:Union[str, Any]        =      jnp.intaa(_A     )







				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								def _force_token(__lowerCAmelCase     ):
												__magic_name__		:int        =      scores.shape[0]
												__magic_name__		:int        =      self.force_token_array[generation_idx]

												__magic_name__		:Tuple        =      jnp.ones_like(_A       ,					dtype=scores.dtype     ) * -float('''inf'''     )
												__magic_name__		:Tuple        =      jnp.zeros((batch_size, 1)       ,					dtype=scores.dtype     )
												__magic_name__		:str        =      lax.dynamic_update_slice(_A       ,					_A       ,					(0, current_token)     )
												return new_scores

								__magic_name__		:int        =      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 						lowerCamelCase_				(      snake_case__ ):







				def __init__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:str        =      generate_config.eos_token_id
								__magic_name__		:Any        =      generate_config.no_timestamps_token_id
								__magic_name__		:List[str]        =      generate_config.no_timestamps_token_id + 1

								__magic_name__		:List[Any]        =      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'''     ):
												__magic_name__		:List[Any]        =      generate_config.max_initial_timestamp_index
								else:
												__magic_name__		:Tuple        =      model_config.vocab_size
								if self.max_initial_timestamp_index is None:
												__magic_name__		:Tuple        =      model_config.vocab_size







				def __call__(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								# suppress <|notimestamps|> which is handled by without_timestamps
								__magic_name__		:Any        =      scores.at[:, self.no_timestamps_token_id].set(-float('''inf'''     )     )

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

												__magic_name__		:List[str]        =      jnp.where((cur_len - self.begin_index) < 2       ,					_A       ,					_A     )
												__magic_name__		:int        =      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       ,					)

								__magic_name__		:Tuple        =      jax.vmap(_A     )(_A       ,					_A     )

								__magic_name__		:List[Any]        =      jnp.where(cur_len == self.begin_index       ,					_A       ,					_A     )
								__magic_name__		:str        =      jnp.where(
								    self.max_initial_timestamp_index is not None       ,					True and apply_max_initial_timestamp       ,					_A       ,					)

								__magic_name__		:Any        =      self.timestamp_begin + self.max_initial_timestamp_index

								__magic_name__		:Tuple        =      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
								__magic_name__		:Optional[int]        =      jax.nn.log_softmax(_A       ,					axis=-1     )

								def handle_cumulative_probs(__lowerCAmelCase       ,					__lowerCAmelCase     ):
												__magic_name__		:Any        =      jax.nn.logsumexp(logprobs_k[self.timestamp_begin :]       ,					axis=-1     )
												__magic_name__		:Optional[Any]        =      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       ,					)

								__magic_name__		:Union[str, Any]        =      jax.vmap(_A     )(_A       ,					_A     )

								return scores














import unittest

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

from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()
class 						lowerCamelCase_				(      unittest.TestCase ):







				@property
				def 			A	(						self     ):
								"""simple docstring"""
								torch.manual_seed(0     )
								__magic_name__		:Dict        =      UNetaDModel(
								    block_out_channels=(3_2, 6_4)       ,					layers_per_block=2       ,					sample_size=3_2       ,					in_channels=3       ,					out_channels=3       ,					down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')       ,					up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')       ,					)
								return model







				@property
				def 			A	(						self     ):
								"""simple docstring"""
								torch.manual_seed(0     )
								__magic_name__		:List[str]        =      VQModel(
								    block_out_channels=[3_2, 6_4]       ,					in_channels=3       ,					out_channels=3       ,					down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']       ,					up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']       ,					latent_channels=3       ,					)
								return model







				@property
				def 			A	(						self     ):
								"""simple docstring"""
								torch.manual_seed(0     )
								__magic_name__		:int        =      CLIPTextConfig(
								    bos_token_id=0       ,					eos_token_id=2       ,					hidden_size=3_2       ,					intermediate_size=3_7       ,					layer_norm_eps=1E-05       ,					num_attention_heads=4       ,					num_hidden_layers=5       ,					pad_token_id=1       ,					vocab_size=1_0_0_0       ,					)
								return CLIPTextModel(__lowerCAmelCase     )







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Optional[Any]        =      self.dummy_uncond_unet
								__magic_name__		:Optional[int]        =      DDIMScheduler()
								__magic_name__		:List[str]        =      self.dummy_vq_model

								__magic_name__		:Tuple        =      LDMPipeline(unet=__lowerCAmelCase       ,					vqvae=__lowerCAmelCase       ,					scheduler=__lowerCAmelCase     )
								ldm.to(__lowerCAmelCase     )
								ldm.set_progress_bar_config(disable=__lowerCAmelCase     )

								__magic_name__		:List[Any]        =      torch.manual_seed(0     )
								__magic_name__		:List[str]        =      ldm(generator=__lowerCAmelCase       ,					num_inference_steps=2       ,					output_type='''numpy'''     ).images

								__magic_name__		:List[Any]        =      torch.manual_seed(0     )
								__magic_name__		:Any        =      ldm(generator=__lowerCAmelCase       ,					num_inference_steps=2       ,					output_type='''numpy'''       ,					return_dict=__lowerCAmelCase     )[0]

								__magic_name__		:Any        =      image[0, -3:, -3:, -1]
								__magic_name__		:Dict        =      image_from_tuple[0, -3:, -3:, -1]

								assert image.shape == (1, 6_4, 6_4, 3)
								__magic_name__		:Union[str, Any]        =      np.array([0.8512, 0.818, 0.6411, 0.6808, 0.4465, 0.5618, 0.46, 0.6231, 0.5172]     )
								__magic_name__		:Any        =      1E-2 if torch_device != '''mps''' else 3E-2

								assert np.abs(image_slice.flatten() - expected_slice     ).max() < tolerance
								assert np.abs(image_from_tuple_slice.flatten() - expected_slice     ).max() < tolerance







@slow
@require_torch
class 						lowerCamelCase_				(      unittest.TestCase ):







				def 			A	(						self     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      LDMPipeline.from_pretrained('''CompVis/ldm-celebahq-256'''     )
								ldm.to(__lowerCAmelCase     )
								ldm.set_progress_bar_config(disable=__lowerCAmelCase     )

								__magic_name__		:Optional[Any]        =      torch.manual_seed(0     )
								__magic_name__		:Optional[int]        =      ldm(generator=__lowerCAmelCase       ,					num_inference_steps=5       ,					output_type='''numpy'''     ).images

								__magic_name__		:Optional[Any]        =      image[0, -3:, -3:, -1]

								assert image.shape == (1, 2_5_6, 2_5_6, 3)
								__magic_name__		:List[str]        =      np.array([0.4399, 0.44975, 0.46825, 0.474, 0.4359, 0.4581, 0.45095, 0.4341, 0.4447]     )
								__magic_name__		:Tuple        =      1E-2 if torch_device != '''mps''' else 3E-2

								assert np.abs(image_slice.flatten() - expected_slice     ).max() < tolerance






