






'''simple docstring'''





import unittest

import numpy as np

from transformers import RobertaPreLayerNormConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
	import jax.numpy as jnp

	from transformers.models.roberta_prelayernorm.modeling_flax_roberta_prelayernorm import (
	    FlaxRobertaPreLayerNormForCausalLM,
	    FlaxRobertaPreLayerNormForMaskedLM,
	    FlaxRobertaPreLayerNormForMultipleChoice,
	    FlaxRobertaPreLayerNormForQuestionAnswering,
	    FlaxRobertaPreLayerNormForSequenceClassification,
	    FlaxRobertaPreLayerNormForTokenClassification,
	    FlaxRobertaPreLayerNormModel,
	)


class      lowercase__     (			unittest.TestCase	):

	def __init__(							self		,    __UpperCAmelCase		,    __UpperCAmelCase=13		,    __UpperCAmelCase=7		,    __UpperCAmelCase=True		,    __UpperCAmelCase=True		,    __UpperCAmelCase=True		,    __UpperCAmelCase=True		,    __UpperCAmelCase=99		,    __UpperCAmelCase=32		,    __UpperCAmelCase=5		,    __UpperCAmelCase=4		,    __UpperCAmelCase=37		,    __UpperCAmelCase="gelu"		,    __UpperCAmelCase=0.1		,    __UpperCAmelCase=0.1		,    __UpperCAmelCase=512		,    __UpperCAmelCase=16		,    __UpperCAmelCase=2		,    __UpperCAmelCase=0.02		,    __UpperCAmelCase=4		,    )->					Optional[int]:







							'''simple docstring'''





							lowerCAmelCase__       =						parent
							lowerCAmelCase__       =						batch_size
							lowerCAmelCase__       =						seq_length
							lowerCAmelCase__       =						is_training
							lowerCAmelCase__       =						use_attention_mask
							lowerCAmelCase__       =						use_token_type_ids
							lowerCAmelCase__       =						use_labels
							lowerCAmelCase__       =						vocab_size
							lowerCAmelCase__       =						hidden_size
							lowerCAmelCase__       =						num_hidden_layers
							lowerCAmelCase__       =						num_attention_heads
							lowerCAmelCase__       =						intermediate_size
							lowerCAmelCase__       =						hidden_act
							lowerCAmelCase__       =						hidden_dropout_prob
							lowerCAmelCase__       =						attention_probs_dropout_prob
							lowerCAmelCase__       =						max_position_embeddings
							lowerCAmelCase__       =						type_vocab_size
							lowerCAmelCase__       =						type_sequence_label_size
							lowerCAmelCase__       =						initializer_range
							lowerCAmelCase__       =						num_choices

	def 		UpperCAmelCase  (							self       )->					Union[str, Any]:







							'''simple docstring'''





							lowerCAmelCase__       =						ids_tensor([self.batch_size, self.seq_length]		,    self.vocab_size       )

							lowerCAmelCase__       =						None
							if self.use_attention_mask:
													lowerCAmelCase__       =						random_attention_mask([self.batch_size, self.seq_length]       )

							lowerCAmelCase__       =						None
							if self.use_token_type_ids:
													lowerCAmelCase__       =						ids_tensor([self.batch_size, self.seq_length]		,    self.type_vocab_size       )

							lowerCAmelCase__       =						RobertaPreLayerNormConfig(
							    vocab_size=self.vocab_size		,    hidden_size=self.hidden_size		,    num_hidden_layers=self.num_hidden_layers		,    num_attention_heads=self.num_attention_heads		,    intermediate_size=self.intermediate_size		,    hidden_act=self.hidden_act		,    hidden_dropout_prob=self.hidden_dropout_prob		,    attention_probs_dropout_prob=self.attention_probs_dropout_prob		,    max_position_embeddings=self.max_position_embeddings		,    type_vocab_size=self.type_vocab_size		,    is_decoder=__UpperCAmelCase		,    initializer_range=self.initializer_range		,    )

							return config, input_ids, token_type_ids, attention_mask

	def 		UpperCAmelCase  (							self       )->					Union[str, Any]:







							'''simple docstring'''





							lowerCAmelCase__       =						self.prepare_config_and_inputs()
							lowerCAmelCase__      ,							lowerCAmelCase__      ,							lowerCAmelCase__      ,							lowerCAmelCase__       =						config_and_inputs
							lowerCAmelCase__       =						{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
							return config, inputs_dict


	def 		UpperCAmelCase  (							self       )->					List[str]:







							'''simple docstring'''





							lowerCAmelCase__       =						self.prepare_config_and_inputs()
							lowerCAmelCase__      ,							lowerCAmelCase__      ,							lowerCAmelCase__      ,							lowerCAmelCase__       =						config_and_inputs

							lowerCAmelCase__       =						True
							lowerCAmelCase__       =						floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
							lowerCAmelCase__       =						ids_tensor([self.batch_size, self.seq_length]		,    vocab_size=2       )

							return (
							    config,
							    input_ids,
							    token_type_ids,
							    encoder_hidden_states,
							    encoder_attention_mask,
							)





@require_flax
# Copied from tests.models.roberta.test_modelling_flax_roberta.FlaxRobertaPreLayerNormModelTest with ROBERTA->ROBERTA_PRELAYERNORM,Roberta->RobertaPreLayerNorm,roberta-base->andreasmadsen/efficient_mlm_m0.40
class      lowercase__     (			_UpperCAmelCase,					unittest.TestCase	):
	a_         =True

	a_         =(
	    (
	        FlaxRobertaPreLayerNormModel,
	        FlaxRobertaPreLayerNormForCausalLM,
	        FlaxRobertaPreLayerNormForMaskedLM,
	        FlaxRobertaPreLayerNormForSequenceClassification,
	        FlaxRobertaPreLayerNormForTokenClassification,
	        FlaxRobertaPreLayerNormForMultipleChoice,
	        FlaxRobertaPreLayerNormForQuestionAnswering,
	    )
	    if is_flax_available()
	    else ()
	)

	def 		UpperCAmelCase  (							self       )->					List[Any]:







							'''simple docstring'''





							lowerCAmelCase__       =						FlaxRobertaPreLayerNormModelTester(self       )


	@slow
	def 		UpperCAmelCase  (							self       )->					List[Any]:







							'''simple docstring'''





							for model_class_name in self.all_model_classes:
													lowerCAmelCase__       =						model_class_name.from_pretrained("andreasmadsen/efficient_mlm_m0.40"		,    from_pt=__UpperCAmelCase       )
													lowerCAmelCase__       =						model(np.ones((1, 1)       )       )
													self.assertIsNotNone(__UpperCAmelCase       )





@require_flax
class      lowercase__     (			unittest.TestCase	):

	@slow
	def 		UpperCAmelCase  (							self       )->					Optional[Any]:







							'''simple docstring'''





							lowerCAmelCase__       =						FlaxRobertaPreLayerNormForMaskedLM.from_pretrained("andreasmadsen/efficient_mlm_m0.40"		,    from_pt=__UpperCAmelCase       )

							lowerCAmelCase__       =						np.array([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]		,    dtype=jnp.intaa       )
							lowerCAmelCase__       =						model(__UpperCAmelCase       )[0]
							lowerCAmelCase__       =						[1, 11, 50265]
							self.assertEqual(list(output.shape       )		,    __UpperCAmelCase       )
							# compare the actual values for a slice.
							lowerCAmelCase__       =						np.array(
							    [[[40.4_880, 18.0_199, -5.2_367], [-1.8_877, -4.0_885, 10.7_085], [-2.2_613, -5.6_110, 7.2_665]]]		,    dtype=np.floataa       )
							self.assertTrue(np.allclose(output[:, :3, :3]		,    __UpperCAmelCase		,    atol=1E-4       )       )


	@slow
	def 		UpperCAmelCase  (							self       )->					Optional[Any]:







							'''simple docstring'''





							lowerCAmelCase__       =						FlaxRobertaPreLayerNormModel.from_pretrained("andreasmadsen/efficient_mlm_m0.40"		,    from_pt=__UpperCAmelCase       )

							lowerCAmelCase__       =						np.array([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]		,    dtype=jnp.intaa       )
							lowerCAmelCase__       =						model(__UpperCAmelCase       )[0]
							# compare the actual values for a slice.
							lowerCAmelCase__       =						np.array(
							    [[[0.0_208, -0.0_356, 0.0_237], [-0.1_569, -0.0_411, -0.2_626], [0.1_879, 0.0_125, -0.0_089]]]		,    dtype=np.floataa       )
							self.assertTrue(np.allclose(output[:, :3, :3]		,    __UpperCAmelCase		,    atol=1E-4       )       )














import unittest

from datasets import load_dataset

from transformers import BloomTokenizerFast
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class      lowercase__     (			_UpperCAmelCase,					unittest.TestCase	):
	a_         =None
	a_         =BloomTokenizerFast
	a_         =BloomTokenizerFast
	a_         =True
	a_         =False
	a_         ="""tokenizer_file"""
	a_         ={"""bos_token""": """<s>""", """eos_token""": """</s>""", """unk_token""": """<unk>""", """pad_token""": """<pad>"""}

	def 		UpperCAmelCase  (							self       )->					int:







							'''simple docstring'''





							super().setUp()
							lowerCAmelCase__       =						BloomTokenizerFast.from_pretrained("bigscience/tokenizer"       )
							tokenizer.save_pretrained(self.tmpdirname       )

	def 		UpperCAmelCase  (							self		,    **__UpperCAmelCase       )->					int:







							'''simple docstring'''





							kwargs.update(self.special_tokens_map       )
							return BloomTokenizerFast.from_pretrained(self.tmpdirname		,    **__UpperCAmelCase       )

	def 		UpperCAmelCase  (							self       )->					Any:







							'''simple docstring'''





							lowerCAmelCase__       =						self.get_rust_tokenizer()

							lowerCAmelCase__       =						["The quick brown fox</s>", "jumps over the lazy dog</s>"]
							lowerCAmelCase__       =						[[2175, 23714, 73173, 144252, 2], [77, 132619, 3478, 368, 109586, 35433, 2]]

							lowerCAmelCase__       =						tokenizer.batch_encode_plus(__UpperCAmelCase       )["input_ids"]
							self.assertListEqual(__UpperCAmelCase		,    __UpperCAmelCase       )

							lowerCAmelCase__       =						tokenizer.batch_decode(__UpperCAmelCase       )
							self.assertListEqual(__UpperCAmelCase		,    __UpperCAmelCase       )

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase=6       )->					str:







							'''simple docstring'''





							for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
													with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})"       ):
																			lowerCAmelCase__       =						self.rust_tokenizer_class.from_pretrained(__UpperCAmelCase		,    **__UpperCAmelCase       )
																			# tokenizer_r.pad_token = None # Hotfixing padding = None
																			# Simple input
																			lowerCAmelCase__       =						"This is a simple input"
																			lowerCAmelCase__       =						["This is a simple input 1", "This is a simple input 2"]
																			lowerCAmelCase__       =						("This is a simple input", "This is a pair")
																			lowerCAmelCase__       =						[
																			    ("This is a simple input 1", "This is a simple input 2"),
																			    ("This is a simple pair 1", "This is a simple pair 2"),
																			]

																			# Simple input tests
																			try:
																									tokenizer_r.encode(__UpperCAmelCase		,    max_length=__UpperCAmelCase       )
																									tokenizer_r.encode_plus(__UpperCAmelCase		,    max_length=__UpperCAmelCase       )

																									tokenizer_r.batch_encode_plus(__UpperCAmelCase		,    max_length=__UpperCAmelCase       )
																									tokenizer_r.encode(__UpperCAmelCase		,    max_length=__UpperCAmelCase       )
																									tokenizer_r.batch_encode_plus(__UpperCAmelCase		,    max_length=__UpperCAmelCase       )
																			except ValueError:
																									self.fail("Bloom Tokenizer should be able to deal with padding"       )

																			lowerCAmelCase__       =						None  # Hotfixing padding = None
																			self.assertRaises(__UpperCAmelCase		,    tokenizer_r.encode		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"       )

																			# Simple input
																			self.assertRaises(__UpperCAmelCase		,    tokenizer_r.encode_plus		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"       )

																			# Simple input
																			self.assertRaises(
																			    __UpperCAmelCase		,    tokenizer_r.batch_encode_plus		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"		,    )

																			# Pair input
																			self.assertRaises(__UpperCAmelCase		,    tokenizer_r.encode		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"       )

																			# Pair input
																			self.assertRaises(__UpperCAmelCase		,    tokenizer_r.encode_plus		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"       )

																			# Pair input
																			self.assertRaises(
																			    __UpperCAmelCase		,    tokenizer_r.batch_encode_plus		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"		,    )

	def 		UpperCAmelCase  (							self       )->					int:







							'''simple docstring'''





							lowerCAmelCase__       =						self.get_rust_tokenizer()
							lowerCAmelCase__       =						load_dataset("xnli"		,    "all_languages"		,    split="test"		,    streaming=__UpperCAmelCase       )

							lowerCAmelCase__       =						next(iter(__UpperCAmelCase       )       )["premise"]  # pick up one data
							lowerCAmelCase__       =						list(sample_data.values()       )

							lowerCAmelCase__       =						list(map(tokenizer.encode		,    __UpperCAmelCase       )       )
							lowerCAmelCase__       =						[tokenizer.decode(__UpperCAmelCase		,    clean_up_tokenization_spaces=__UpperCAmelCase       ) for x in output_tokens]
							self.assertListEqual(__UpperCAmelCase		,    __UpperCAmelCase       )


	def 		UpperCAmelCase  (							self       )->					List[str]:







							'''simple docstring'''





							self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map       )		,    1       )
							self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values()       )[0]       )		,    1       )






