

"""simple docstring"""





from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers.models.distilbert.modeling_tf_distilbert import (
							    TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TFDistilBertForMaskedLM,
							    TFDistilBertForMultipleChoice,
							    TFDistilBertForQuestionAnswering,
							    TFDistilBertForSequenceClassification,
							    TFDistilBertForTokenClassification,
							    TFDistilBertModel,
							)







class 				SCREAMING_SNAKE_CASE					:






					"""simple docstring"""



					def __init__(       self			:				Any   ,lowercase_			:				str   ,):
						lowerCAmelCase__	:						int  				=	parent
						lowerCAmelCase__	:						List[Any]  				=	1_3
						lowerCAmelCase__	:						str  				=	7
						lowerCAmelCase__	:						Dict  				=	True
						lowerCAmelCase__	:						List[str]  				=	True
						lowerCAmelCase__	:						Union[str, Any]  				=	False
						lowerCAmelCase__	:						Any  				=	True
						lowerCAmelCase__	:						Union[str, Any]  				=	9_9
						lowerCAmelCase__	:						Optional[int]  				=	3_2
						lowerCAmelCase__	:						List[str]  				=	2
						lowerCAmelCase__	:						int  				=	4
						lowerCAmelCase__	:						Optional[int]  				=	3_7
						lowerCAmelCase__	:						Tuple  				=	'''gelu'''
						lowerCAmelCase__	:						int  				=	0.1
						lowerCAmelCase__	:						List[str]  				=	0.1
						lowerCAmelCase__	:						Tuple  				=	5_1_2
						lowerCAmelCase__	:						Optional[Any]  				=	1_6
						lowerCAmelCase__	:						str  				=	2
						lowerCAmelCase__	:						Optional[int]  				=	0.02
						lowerCAmelCase__	:						str  				=	3
						lowerCAmelCase__	:						List[Any]  				=	4
						lowerCAmelCase__	:						Union[str, Any]  				=	None







					def        __lowerCAmelCase (       self			:				Tuple		):
						lowerCAmelCase__	:						Dict  				=	ids_tensor([self.batch_size, self.seq_length]   ,self.vocab_size		)

						lowerCAmelCase__	:						Tuple  				=	None
						if self.use_input_mask:
							lowerCAmelCase__	:						Any  				=	random_attention_mask([self.batch_size, self.seq_length]		)

						lowerCAmelCase__	:						str  				=	None
						lowerCAmelCase__	:						Tuple  				=	None
						lowerCAmelCase__	:						Tuple  				=	None
						if self.use_labels:
							lowerCAmelCase__	:						List[Any]  				=	ids_tensor([self.batch_size]   ,self.type_sequence_label_size		)
							lowerCAmelCase__	:						List[Any]  				=	ids_tensor([self.batch_size, self.seq_length]   ,self.num_labels		)
							lowerCAmelCase__	:						Dict  				=	ids_tensor([self.batch_size]   ,self.num_choices		)

						lowerCAmelCase__	:						List[Any]  				=	DistilBertConfig(
						    vocab_size=self.vocab_size   ,dim=self.hidden_size   ,n_layers=self.num_hidden_layers   ,n_heads=self.num_attention_heads   ,hidden_dim=self.intermediate_size   ,hidden_act=self.hidden_act   ,dropout=self.hidden_dropout_prob   ,attention_dropout=self.attention_probs_dropout_prob   ,max_position_embeddings=self.max_position_embeddings   ,initializer_range=self.initializer_range   ,)

						return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels







					def        __lowerCAmelCase (       self			:				Optional[Any]   ,lowercase_			:				Optional[Any]   ,lowercase_			:				List[Any]   ,lowercase_			:				List[Any]   ,lowercase_			:				int   ,lowercase_			:				Optional[int]   ,lowercase_			:				List[str]		):
						lowerCAmelCase__	:						List[str]  				=	TFDistilBertModel(config=lowercase_		)
						lowerCAmelCase__	:						Any  				=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}

						lowerCAmelCase__	:						List[str]  				=	model(lowercase_		)

						lowerCAmelCase__	:						Dict  				=	[input_ids, input_mask]

						lowerCAmelCase__	:						Tuple  				=	model(lowercase_		)

						self.parent.assertEqual(result.last_hidden_state.shape   ,(self.batch_size, self.seq_length, self.hidden_size)		)







					def        __lowerCAmelCase (       self			:				str   ,lowercase_			:				Optional[int]   ,lowercase_			:				Tuple   ,lowercase_			:				List[str]   ,lowercase_			:				Any   ,lowercase_			:				Dict   ,lowercase_			:				List[Any]		):
						lowerCAmelCase__	:						int  				=	TFDistilBertForMaskedLM(config=lowercase_		)
						lowerCAmelCase__	:						List[str]  				=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
						lowerCAmelCase__	:						Optional[Any]  				=	model(lowercase_		)
						self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.seq_length, self.vocab_size)		)







					def        __lowerCAmelCase (       self			:				int   ,lowercase_			:				int   ,lowercase_			:				int   ,lowercase_			:				List[str]   ,lowercase_			:				Union[str, Any]   ,lowercase_			:				Dict   ,lowercase_			:				Optional[int]		):
						lowerCAmelCase__	:						Any  				=	TFDistilBertForQuestionAnswering(config=lowercase_		)
						lowerCAmelCase__	:						Tuple  				=	{
						    '''input_ids''': input_ids,
						    '''attention_mask''': input_mask,
						}
						lowerCAmelCase__	:						List[Any]  				=	model(lowercase_		)
						self.parent.assertEqual(result.start_logits.shape   ,(self.batch_size, self.seq_length)		)
						self.parent.assertEqual(result.end_logits.shape   ,(self.batch_size, self.seq_length)		)







					def        __lowerCAmelCase (       self			:				Union[str, Any]   ,lowercase_			:				Dict   ,lowercase_			:				Union[str, Any]   ,lowercase_			:				Any   ,lowercase_			:				List[str]   ,lowercase_			:				Optional[Any]   ,lowercase_			:				Dict		):
						lowerCAmelCase__	:						Tuple  				=	self.num_labels
						lowerCAmelCase__	:						Optional[int]  				=	TFDistilBertForSequenceClassification(lowercase_		)
						lowerCAmelCase__	:						Optional[Any]  				=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
						lowerCAmelCase__	:						str  				=	model(lowercase_		)
						self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_labels)		)







					def        __lowerCAmelCase (       self			:				List[str]   ,lowercase_			:				Tuple   ,lowercase_			:				Any   ,lowercase_			:				Any   ,lowercase_			:				Dict   ,lowercase_			:				Optional[int]   ,lowercase_			:				Any		):
						lowerCAmelCase__	:						List[str]  				=	self.num_choices
						lowerCAmelCase__	:						Optional[int]  				=	TFDistilBertForMultipleChoice(lowercase_		)
						lowerCAmelCase__	:						List[Any]  				=	tf.tile(tf.expand_dims(lowercase_   ,1		)   ,(1, self.num_choices, 1)		)
						lowerCAmelCase__	:						List[Any]  				=	tf.tile(tf.expand_dims(lowercase_   ,1		)   ,(1, self.num_choices, 1)		)
						lowerCAmelCase__	:						Optional[Any]  				=	{
						    '''input_ids''': multiple_choice_inputs_ids,
						    '''attention_mask''': multiple_choice_input_mask,
						}
						lowerCAmelCase__	:						List[Any]  				=	model(lowercase_		)
						self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_choices)		)







					def        __lowerCAmelCase (       self			:				List[str]   ,lowercase_			:				List[str]   ,lowercase_			:				Optional[int]   ,lowercase_			:				Optional[Any]   ,lowercase_			:				Optional[int]   ,lowercase_			:				Tuple   ,lowercase_			:				Optional[int]		):
						lowerCAmelCase__	:						Optional[Any]  				=	self.num_labels
						lowerCAmelCase__	:						Optional[int]  				=	TFDistilBertForTokenClassification(lowercase_		)
						lowerCAmelCase__	:						Any  				=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
						lowerCAmelCase__	:						List[Any]  				=	model(lowercase_		)
						self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.seq_length, self.num_labels)		)







					def        __lowerCAmelCase (       self			:				Any		):
						lowerCAmelCase__	:						str  				=	self.prepare_config_and_inputs()
						((lowerCAmelCase__)				,(lowerCAmelCase__)				,(lowerCAmelCase__)				,(lowerCAmelCase__)				,(lowerCAmelCase__)				,(lowerCAmelCase__))	:						int  				=	config_and_inputs
						lowerCAmelCase__	:						Dict  				=	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
						return config, inputs_dict







@require_tf
class 				SCREAMING_SNAKE_CASE					(		a_    ,							a_    ,							unittest.TestCase       ):






					"""simple docstring"""



					lowercase__				 =       (
					    (
					        TFDistilBertModel,
					        TFDistilBertForMaskedLM,
					        TFDistilBertForQuestionAnswering,
					        TFDistilBertForSequenceClassification,
					        TFDistilBertForTokenClassification,
					        TFDistilBertForMultipleChoice,
					    )
					    if is_tf_available()
					    else None
					)
					lowercase__				 =       (
					    {
					        "feature-extraction": TFDistilBertModel,
					        "fill-mask": TFDistilBertForMaskedLM,
					        "question-answering": TFDistilBertForQuestionAnswering,
					        "text-classification": TFDistilBertForSequenceClassification,
					        "token-classification": TFDistilBertForTokenClassification,
					        "zero-shot": TFDistilBertForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)
					lowercase__				 =       False
					lowercase__				 =       False







					def        __lowerCAmelCase (       self			:				Union[str, Any]		):
						lowerCAmelCase__	:						Dict  				=	TFDistilBertModelTester(self		)
						lowerCAmelCase__	:						Optional[Any]  				=	ConfigTester(self   ,config_class=lowercase_   ,dim=3_7		)







					def        __lowerCAmelCase (       self			:				List[Any]		):
						self.config_tester.run_common_tests()







					def        __lowerCAmelCase (       self			:				Union[str, Any]		):
						lowerCAmelCase__	:						Tuple  				=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_model(*lowercase_		)







					def        __lowerCAmelCase (       self			:				str		):
						lowerCAmelCase__	:						List[str]  				=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_masked_lm(*lowercase_		)







					def        __lowerCAmelCase (       self			:				Union[str, Any]		):
						lowerCAmelCase__	:						Optional[int]  				=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_question_answering(*lowercase_		)







					def        __lowerCAmelCase (       self			:				List[str]		):
						lowerCAmelCase__	:						int  				=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowercase_		)







					def        __lowerCAmelCase (       self			:				List[str]		):
						lowerCAmelCase__	:						Tuple  				=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowercase_		)







					def        __lowerCAmelCase (       self			:				Union[str, Any]		):
						lowerCAmelCase__	:						Tuple  				=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_distilbert_for_token_classification(*lowercase_		)







					@slow
					def        __lowerCAmelCase (       self			:				Tuple		):
						for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]		):
							lowerCAmelCase__	:						Optional[int]  				=	TFDistilBertModel.from_pretrained(lowercase_		)
							self.assertIsNotNone(lowercase_		)







@require_tf
class 				SCREAMING_SNAKE_CASE					(		unittest.TestCase       ):






					"""simple docstring"""



					@slow
					def        __lowerCAmelCase (       self			:				Optional[Any]		):
						lowerCAmelCase__	:						Tuple  				=	TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''		)
						lowerCAmelCase__	:						Optional[Any]  				=	tf.constant([[0, 1, 2, 3, 4, 5]]		)
						lowerCAmelCase__	:						Union[str, Any]  				=	model(lowercase_		)[0]

						lowerCAmelCase__	:						str  				=	[1, 6, 7_6_8]
						self.assertEqual(output.shape   ,lowercase_		)

						lowerCAmelCase__	:						str  				=	tf.constant(
						    [
						        [
						            [0.1926_1885, -0.1373_2955, 0.411_9799],
						            [0.2215_0156, -0.0742_2661, 0.3903_7204],
						            [0.2275_6018, -0.089_6414, 0.370_1467],
						        ]
						    ]		)
						tf.debugging.assert_near(output[:, :3, :3]   ,lowercase_   ,atol=1E-4		)







"""simple docstring"""





import importlib
import math
import os
from dataclasses import dataclass
from enum import Enum
from typing import Any, Dict, Optional, Tuple, Union

import flax
import jax.numpy as jnp

from ..utils import BaseOutput


__UpperCamelCase  :      Optional[Any]							      =		'''scheduler_config.json'''







class 				SCREAMING_SNAKE_CASE					(		a_       ):






					"""simple docstring"""



					lowercase__				 =       1
					lowercase__				 =       2
					lowercase__				 =       3
					lowercase__				 =       4
					lowercase__				 =       5







@dataclass
class 				SCREAMING_SNAKE_CASE					(		a_       ):






					"""simple docstring"""



					lowercase__				 =       42







class 				SCREAMING_SNAKE_CASE					:






					"""simple docstring"""



					lowercase__				 =       SCHEDULER_CONFIG_NAME
					lowercase__				 =       ["dtype"]
					lowercase__				 =       []
					lowercase__				 =       True







					@classmethod
					def        __lowerCAmelCase (       cls			:				List[Any]   ,lowercase_			:				Dict[str, Any] = None   ,lowercase_			:				Optional[str] = None   ,lowercase_			:				Optional[int]=False   ,**lowercase_			:				Any   ,):
						lowerCAmelCase__				,lowerCAmelCase__	:						Dict  				=	cls.load_config(
						    pretrained_model_name_or_path=lowercase_   ,subfolder=lowercase_   ,return_unused_kwargs=lowercase_   ,**lowercase_   ,)
						lowerCAmelCase__				,lowerCAmelCase__	:						Union[str, Any]  				=	cls.from_config(lowercase_   ,return_unused_kwargs=lowercase_   ,**lowercase_		)

						if hasattr(lowercase_   ,'''create_state'''		) and getattr(lowercase_   ,'''has_state'''   ,lowercase_		):
							lowerCAmelCase__	:						List[Any]  				=	scheduler.create_state()

						if return_unused_kwargs:
							return scheduler, state, unused_kwargs

						return scheduler, state







					def        __lowerCAmelCase (       self			:				Tuple   ,lowercase_			:				Union[str, os.PathLike]   ,lowercase_			:				bool = False   ,**lowercase_			:				str		):
						self.save_config(save_directory=lowercase_   ,push_to_hub=lowercase_   ,**lowercase_		)







					@property
					def        __lowerCAmelCase (       self			:				List[str]		):
						return self._get_compatibles()







					@classmethod
					def        __lowerCAmelCase (       cls			:				List[Any]		):
						lowerCAmelCase__	:						Tuple  				=	list(set([cls.__name__] + cls._compatibles		)		)
						lowerCAmelCase__	:						Tuple  				=	importlib.import_module(__name__.split('''.'''		)[0]		)
						lowerCAmelCase__	:						Union[str, Any]  				=	[
						    getattr(lowercase_   ,lowercase_		) for c in compatible_classes_str if hasattr(lowercase_   ,lowercase_		)
						]
						return compatible_classes

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_ ):
	assert len(A_ ) >= x.ndim
	return jnp.broadcast_to(x.reshape(x.shape + (1,) * (len(A_ ) - x.ndim) )						,     A_ )

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_=0.999						,     A_=jnp.floataa ):

	def alpha_bar(A_ ):
		return math.cos((time_step + 0.008) / 1.008 * math.pi / 2 ) ** 2

	lowerCAmelCase__	:						Optional[Any]  				=	[]
	for i in range(A_ ):
		lowerCAmelCase__	:						str  				=	i / num_diffusion_timesteps
		lowerCAmelCase__	:						Union[str, Any]  				=	(i + 1) / num_diffusion_timesteps
		betas.append(min(1 - alpha_bar(A_ ) / alpha_bar(A_ )						,     A_ ) )
	return jnp.array(A_						,     dtype=A_ )







@flax.struct.dataclass
class 				SCREAMING_SNAKE_CASE					:






					"""simple docstring"""



					lowercase__				 =       42
					lowercase__				 =       42
					lowercase__				 =       42







					@classmethod
					def        __lowerCAmelCase (       cls			:				Union[str, Any]   ,lowercase_			:				List[Any]		):
						lowerCAmelCase__	:						Optional[int]  				=	scheduler.config

						if config.trained_betas is not None:
							lowerCAmelCase__	:						Any  				=	jnp.asarray(config.trained_betas   ,dtype=scheduler.dtype		)
						elif config.beta_schedule == "linear":
							lowerCAmelCase__	:						Union[str, Any]  				=	jnp.linspace(config.beta_start   ,config.beta_end   ,config.num_train_timesteps   ,dtype=scheduler.dtype		)
						elif config.beta_schedule == "scaled_linear":
							# this schedule is very specific to the latent diffusion model.
							lowerCAmelCase__	:						int  				=	(
							    jnp.linspace(
							        config.beta_start**0.5   ,config.beta_end**0.5   ,config.num_train_timesteps   ,dtype=scheduler.dtype		)
							    ** 2
							)
						elif config.beta_schedule == "squaredcos_cap_v2":
							# Glide cosine schedule
							lowerCAmelCase__	:						List[Any]  				=	betas_for_alpha_bar(config.num_train_timesteps   ,dtype=scheduler.dtype		)
						else:
							raise NotImplementedError(
							    F'beta_schedule {config.beta_schedule} is not implemented for scheduler {scheduler.__class__.__name__}'		)

						lowerCAmelCase__	:						str  				=	1.0 - betas

						lowerCAmelCase__	:						Union[str, Any]  				=	jnp.cumprod(lowercase_   ,axis=0		)

						return cls(
						    alphas=lowercase_   ,betas=lowercase_   ,alphas_cumprod=lowercase_   ,)

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_						,     A_						,     A_ ):
	lowerCAmelCase__	:						Any  				=	state.alphas_cumprod

	lowerCAmelCase__	:						Optional[Any]  				=	alphas_cumprod[timesteps] ** 0.5
	lowerCAmelCase__	:						Tuple  				=	sqrt_alpha_prod.flatten()
	lowerCAmelCase__	:						str  				=	broadcast_to_shape_from_left(A_						,     original_samples.shape )

	lowerCAmelCase__	:						Optional[Any]  				=	(1 - alphas_cumprod[timesteps]) ** 0.5
	lowerCAmelCase__	:						Optional[Any]  				=	sqrt_one_minus_alpha_prod.flatten()
	lowerCAmelCase__	:						Optional[int]  				=	broadcast_to_shape_from_left(A_						,     original_samples.shape )

	return sqrt_alpha_prod, sqrt_one_minus_alpha_prod

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_						,     A_						,     A_ ):
	lowerCAmelCase__				,lowerCAmelCase__	:						List[Any]  				=	get_sqrt_alpha_prod(A_						,     A_						,     A_						,     A_ )
	lowerCAmelCase__	:						str  				=	sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise
	return noisy_samples





def   __SCREAMING_SNAKE_CASE  (      A_						,     A_						,     A_						,     A_ ):
	lowerCAmelCase__				,lowerCAmelCase__	:						List[Any]  				=	get_sqrt_alpha_prod(A_						,     A_						,     A_						,     A_ )
	lowerCAmelCase__	:						Union[str, Any]  				=	sqrt_alpha_prod * noise - sqrt_one_minus_alpha_prod * sample
	return velocity




