
import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import BertGenerationDecoder, BertGenerationEncoder

class       a		:







							def __init__(						self  :  Any      ,				snake_case__  :  int      ,				snake_case__  :  Union[str, Any]=13      ,				snake_case__  :  Any=7      ,				snake_case__  :  Tuple=True      ,				snake_case__  :  Dict=True      ,				snake_case__  :  str=99      ,				snake_case__  :  List[str]=32      ,				snake_case__  :  int=5      ,				snake_case__  :  int=4      ,				snake_case__  :  Optional[int]=37      ,				snake_case__  :  str="gelu"      ,				snake_case__  :  int=0.1      ,				snake_case__  :  int=0.1      ,				snake_case__  :  str=50      ,				snake_case__  :  Optional[int]=0.0_2      ,				snake_case__  :  Dict=True      ,				snake_case__  :  Optional[int]=None      ,				):
								"""simple docstring"""







								__lowerCAmelCase         =							parent
								__lowerCAmelCase         =							batch_size
								__lowerCAmelCase         =							seq_length
								__lowerCAmelCase         =							is_training
								__lowerCAmelCase         =							use_input_mask
								__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         =							initializer_range
								__lowerCAmelCase         =							use_labels
								__lowerCAmelCase         =							scope







							def       UpperCAmelCase__					(						self  :  List[Any] ):
								"""simple docstring"""







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

								__lowerCAmelCase         =							None
								if self.use_input_mask:
									__lowerCAmelCase         =							random_attention_mask([self.batch_size, self.seq_length] )

								if self.use_labels:
									__lowerCAmelCase         =							ids_tensor([self.batch_size, self.seq_length]      ,				self.vocab_size )

								__lowerCAmelCase         =							self.get_config()

								return config, input_ids, input_mask, token_labels







							def       UpperCAmelCase__					(						self  :  List[Any] ):
								"""simple docstring"""







								return BertGenerationConfig(
								    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      ,				is_decoder=snake_case__      ,				initializer_range=self.initializer_range      ,				)







							def       UpperCAmelCase__					(						self  :  str ):
								"""simple docstring"""







								(
								    (
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						
								)         =							self.prepare_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,
								    input_mask,
								    token_labels,
								    encoder_hidden_states,
								    encoder_attention_mask,
								)







							def       UpperCAmelCase__					(						self  :  Dict      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  int      ,				snake_case__  :  Dict      ,				snake_case__  :  str      ,				**snake_case__  :  List[str]      ,				):
								"""simple docstring"""







								__lowerCAmelCase         =							BertGenerationEncoder(config=snake_case__ )
								model.to(snake_case__ )
								model.eval()
								__lowerCAmelCase         =							model(snake_case__      ,				attention_mask=snake_case__ )
								__lowerCAmelCase         =							model(snake_case__ )
								self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, self.seq_length, self.hidden_size) )







							def       UpperCAmelCase__					(						self  :  Optional[int]      ,				snake_case__  :  str      ,				snake_case__  :  List[str]      ,				snake_case__  :  List[Any]      ,				snake_case__  :  Union[str, Any]      ,				snake_case__  :  List[str]      ,				snake_case__  :  Optional[int]      ,				**snake_case__  :  Union[str, Any]      ,				):
								"""simple docstring"""







								__lowerCAmelCase         =							True
								__lowerCAmelCase         =							BertGenerationEncoder(config=snake_case__ )
								model.to(snake_case__ )
								model.eval()
								__lowerCAmelCase         =							model(
								    snake_case__      ,				attention_mask=snake_case__      ,				encoder_hidden_states=snake_case__      ,				encoder_attention_mask=snake_case__      ,				)
								__lowerCAmelCase         =							model(
								    snake_case__      ,				attention_mask=snake_case__      ,				encoder_hidden_states=snake_case__      ,				)
								self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, self.seq_length, self.hidden_size) )







							def       UpperCAmelCase__					(						self  :  int      ,				snake_case__  :  Any      ,				snake_case__  :  List[Any]      ,				snake_case__  :  Optional[int]      ,				snake_case__  :  Tuple      ,				snake_case__  :  List[str]      ,				snake_case__  :  str      ,				**snake_case__  :  int      ,				):
								"""simple docstring"""







								__lowerCAmelCase         =							True
								__lowerCAmelCase         =							True
								__lowerCAmelCase         =							BertGenerationDecoder(config=snake_case__ ).to(snake_case__ ).eval()

								# first forward pass
								__lowerCAmelCase         =							model(
								    snake_case__      ,				attention_mask=snake_case__      ,				encoder_hidden_states=snake_case__      ,				encoder_attention_mask=snake_case__      ,				use_cache=snake_case__      ,				)
								__lowerCAmelCase         =							outputs.past_key_values

								# create hypothetical multiple next token and extent to next_input_ids
								__lowerCAmelCase         =							ids_tensor((self.batch_size, 3)      ,				config.vocab_size )
								__lowerCAmelCase         =							ids_tensor((self.batch_size, 3)      ,				vocab_size=2 )

								# append to next input_ids and
								__lowerCAmelCase         =							torch.cat([input_ids, next_tokens]      ,				dim=-1 )
								__lowerCAmelCase         =							torch.cat([input_mask, next_mask]      ,				dim=-1 )

								__lowerCAmelCase         =							model(
								    snake_case__      ,				attention_mask=snake_case__      ,				encoder_hidden_states=snake_case__      ,				encoder_attention_mask=snake_case__      ,				output_hidden_states=snake_case__      ,				)["hidden_states"][0]
								__lowerCAmelCase         =							model(
								    snake_case__      ,				attention_mask=snake_case__      ,				encoder_hidden_states=snake_case__      ,				encoder_attention_mask=snake_case__      ,				past_key_values=snake_case__      ,				output_hidden_states=snake_case__      ,				)["hidden_states"][0]

								# select random slice
								__lowerCAmelCase         =							ids_tensor((1,)      ,				output_from_past.shape[-1] ).item()
								__lowerCAmelCase         =							output_from_no_past[:, -3:, random_slice_idx].detach()
								__lowerCAmelCase         =							output_from_past[:, :, random_slice_idx].detach()

								self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] )

								# test that outputs are equal for slice
								self.parent.assertTrue(torch.allclose(snake_case__      ,				snake_case__      ,				atol=1E-3 ) )







							def       UpperCAmelCase__					(						self  :  Optional[int]      ,				snake_case__  :  Any      ,				snake_case__  :  List[str]      ,				snake_case__  :  Dict      ,				snake_case__  :  List[Any]      ,				*snake_case__  :  int      ,				):
								"""simple docstring"""







								__lowerCAmelCase         =							BertGenerationDecoder(snake_case__ )
								model.to(snake_case__ )
								model.eval()
								__lowerCAmelCase         =							model(snake_case__      ,				attention_mask=snake_case__      ,				labels=snake_case__ )
								self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.seq_length, self.vocab_size) )







							def       UpperCAmelCase__					(						self  :  Any ):
								"""simple docstring"""







								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							self.prepare_config_and_inputs()
								__lowerCAmelCase         =							{"input_ids": input_ids, "attention_mask": input_mask}
								return config, inputs_dict

@require_torch
class       a		(   __UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	unittest.TestCase					):
							lowercase_		:						Optional[Any]   							=  (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
							lowercase_		:						Tuple   							=  (BertGenerationDecoder,) if is_torch_available() else ()
							lowercase_		:						List[Any]   							=  (
							    {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder}
							    if is_torch_available()
							    else {}
							)







							def       UpperCAmelCase__					(						self  :  str ):
								"""simple docstring"""







								__lowerCAmelCase         =							BertGenerationEncoderTester(self )
								__lowerCAmelCase         =							ConfigTester(self      ,				config_class=snake_case__      ,				hidden_size=37 )







							def       UpperCAmelCase__					(						self  :  List[Any] ):
								"""simple docstring"""







								self.config_tester.run_common_tests()







							def       UpperCAmelCase__					(						self  :  Optional[Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*snake_case__ )







							def       UpperCAmelCase__					(						self  :  str ):
								"""simple docstring"""







								__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase   ,						__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								__lowerCAmelCase         =							"bert"
								self.model_tester.create_and_check_model(snake_case__      ,				snake_case__      ,				snake_case__      ,				snake_case__ )







							def       UpperCAmelCase__					(						self  :  str ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_model_as_decoder(*snake_case__ )







							def       UpperCAmelCase__					(						self  :  Optional[Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_decoder_model_past_large_inputs(*snake_case__ )







							def       UpperCAmelCase__					(						self  :  List[Any] ):
								"""simple docstring"""







								(
								    (
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						(
								    __lowerCAmelCase
								)   ,						
								)         =							self.model_tester.prepare_config_and_inputs_for_decoder()

								__lowerCAmelCase         =							None

								self.model_tester.create_and_check_model_as_decoder(
								    snake_case__      ,				snake_case__      ,				snake_case__      ,				snake_case__      ,				snake_case__      ,				snake_case__      ,				)







							def       UpperCAmelCase__					(						self  :  Union[str, Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_for_causal_lm(*snake_case__ )







							@slow
							def       UpperCAmelCase__					(						self  :  List[str] ):
								"""simple docstring"""







								__lowerCAmelCase         =							BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder" )
								self.assertIsNotNone(snake_case__ )

@require_torch
class       a		(   unittest.TestCase					):







							@slow
							def       UpperCAmelCase__					(						self  :  Any ):
								"""simple docstring"""







								__lowerCAmelCase         =							BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder" )
								__lowerCAmelCase         =							torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]] )
								with torch.no_grad():
									__lowerCAmelCase         =							model(snake_case__ )[0]
								__lowerCAmelCase         =							torch.Size([1, 8, 1_024] )
								self.assertEqual(output.shape      ,				snake_case__ )
								__lowerCAmelCase         =							torch.tensor(
								    [[[0.1_7_7_5, 0.0_0_8_3, -0.0_3_2_1], [1.6_0_0_2, 0.1_2_8_7, 0.3_9_1_2], [2.1_4_7_3, 0.5_7_9_1, 0.6_0_6_6]]] )
								self.assertTrue(torch.allclose(output[:, :3, :3]      ,				snake_case__      ,				atol=1E-4 ) )

@require_torch
class       a		(   unittest.TestCase					):







							@slow
							def       UpperCAmelCase__					(						self  :  Tuple ):
								"""simple docstring"""







								__lowerCAmelCase         =							BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder" )
								__lowerCAmelCase         =							torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]] )
								with torch.no_grad():
									__lowerCAmelCase         =							model(snake_case__ )[0]
								__lowerCAmelCase         =							torch.Size([1, 8, 50_358] )
								self.assertEqual(output.shape      ,				snake_case__ )
								__lowerCAmelCase         =							torch.tensor(
								    [[[-0.5_7_8_8, -2.5_9_9_4, -3.7_0_5_4], [0.0_4_3_8, 4.7_9_9_7, 1.8_7_9_5], [1.5_8_6_2, 6.6_4_0_9, 4.4_6_3_8]]] )
								self.assertTrue(torch.allclose(output[:, :3, :3]      ,				snake_case__      ,				atol=1E-4 ) )








from __future__ import annotations

UpperCamelCase_   	=	[True] * 1_0_0_0_0_0_1
UpperCamelCase_   	=	2
while i * i <= 1_0_0_0_0_0_0:
					if seive[i]:
										for j in range(i * i, 1_0_0_0_0_0_1, i):
															UpperCamelCase_   	=	False
					i += 1






def 		_UpperCAmelCase   (	UpperCamelCase:     int  ):






	"""simple docstring"""


	return seive[n]






def 		_UpperCAmelCase   (	UpperCamelCase:     int  ):






	"""simple docstring"""


	return any(digit in "02468" for digit in str(UpperCamelCase  )  )






def 		_UpperCAmelCase   (	UpperCamelCase:     int = 1_0_0_0_0_0_0  ):






	"""simple docstring"""


	__lowerCAmelCase         =							[2]  # result already includes the number 2.
	for num in range(3			,							limit + 1			,							2  ):
		if is_prime(UpperCamelCase  ) and not contains_an_even_digit(UpperCamelCase  ):
			__lowerCAmelCase         =							str(UpperCamelCase  )
			__lowerCAmelCase         =							[int(str_num[j:] + str_num[:j]  ) for j in range(len(UpperCamelCase  )  )]
			if all(is_prime(UpperCamelCase  ) for i in list_nums  ):
				result.append(UpperCamelCase  )
	return result






def 		_UpperCAmelCase   (	):






	"""simple docstring"""


	return len(find_circular_primes()  )


if __name__ == "__main__":
					print(f'''{len(find_circular_primes()) = }''')






