
"""simple docstring"""







from random import randint, random
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE = False							,    SCREAMING_SNAKE_CASE = False							,    SCREAMING_SNAKE_CASE = 5							,    )  ->     list:







							"""simple docstring"""


							__snake_case     			=       [[-1] * number_of_cells]  # Create a highway without any car
							__snake_case     			=       0
							__snake_case     			=       max(SCREAMING_SNAKE_CASE							,    0 )
							while i < number_of_cells:
														__snake_case     			=       (
														    randint(0							,    SCREAMING_SNAKE_CASE ) if random_speed else initial_speed
														)  # Place the cars
														i += (
														    randint(1							,    max_speed * 2 ) if random_frequency else frequency
														)  # Arbitrary number, may need tuning
							return highway
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     int:







							"""simple docstring"""


							__snake_case     			=       0
							__snake_case     			=       highway_now[car_index + 1 :]
							for cell in range(len(SCREAMING_SNAKE_CASE ) ):  # May need a better name for this
														if cells[cell] != -1:  # If the cell is not empty then
																					return distance  # we have the distance we wanted
														distance += 1
							# Here if the car is near the end of the highway
							return distance + get_distance(SCREAMING_SNAKE_CASE							,    -1 )
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     list:







							"""simple docstring"""


							__snake_case     			=       len(SCREAMING_SNAKE_CASE )
							# Beforce calculations, the highway is empty
							__snake_case     			=       [-1] * number_of_cells

							for car_index in range(SCREAMING_SNAKE_CASE ):
														if highway_now[car_index] != -1:
																					# Add 1 to the current speed of the car and cap the speed
																					__snake_case     			=       min(highway_now[car_index] + 1							,    SCREAMING_SNAKE_CASE )
																					# Number of empty cell before the next car
																					__snake_case     			=       get_distance(SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE ) - 1
																					# We can't have the car causing an accident
																					__snake_case     			=       min(next_highway[car_index]							,    SCREAMING_SNAKE_CASE )
																					if random() < probability:
																												# Randomly, a driver will slow down
																												__snake_case     			=       max(next_highway[car_index] - 1							,    0 )
							return next_highway
def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )  ->     list:







							"""simple docstring"""


							__snake_case     			=       len(highway[0] )

							for i in range(SCREAMING_SNAKE_CASE ):
														__snake_case     			=       update(highway[i]							,    SCREAMING_SNAKE_CASE							,    SCREAMING_SNAKE_CASE )
														__snake_case     			=       [-1] * number_of_cells

														for car_index in range(SCREAMING_SNAKE_CASE ):
																					__snake_case     			=       next_speeds_calculated[car_index]
																					if speed != -1:
																												# Change the position based on the speed (with % to create the loop)
																												__snake_case     			=       (car_index + speed) % number_of_cells
																												# Commit the change of position
																												__snake_case     			=       speed
														highway.append(SCREAMING_SNAKE_CASE )

							return highway


if __name__ == "__main__":
				import doctest

				doctest.testmod()


"""simple docstring"""







import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    MODEL_FOR_PRETRAINING_MAPPING,
		    MobileBertForMaskedLM,
		    MobileBertForMultipleChoice,
		    MobileBertForNextSentencePrediction,
		    MobileBertForPreTraining,
		    MobileBertForQuestionAnswering,
		    MobileBertForSequenceClassification,
		    MobileBertForTokenClassification,
		    MobileBertModel,
		)







class 							__magic_name__     :





					def __init__(				self   :							Optional[int]							,						snake_case_   :							List[str]							,						snake_case_   :							str=13							,						snake_case_   :							Optional[int]=7							,						snake_case_   :							Optional[Any]=True							,						snake_case_   :							Union[str, Any]=True							,						snake_case_   :							Tuple=True							,						snake_case_   :							int=True							,						snake_case_   :							int=99							,						snake_case_   :							Optional[int]=64							,						snake_case_   :							Dict=32							,						snake_case_   :							Dict=5							,						snake_case_   :							List[str]=4							,						snake_case_   :							List[Any]=37							,						snake_case_   :							Optional[Any]="gelu"							,						snake_case_   :							Optional[Any]=0.1							,						snake_case_   :							List[Any]=0.1							,						snake_case_   :							Union[str, Any]=512							,						snake_case_   :							int=16							,						snake_case_   :							List[str]=2							,						snake_case_   :							Union[str, Any]=0.02							,						snake_case_   :							Union[str, Any]=3							,						snake_case_   :							str=4							,						snake_case_   :							int=None							,						):
												__snake_case     			=       parent
												__snake_case     			=       batch_size
												__snake_case     			=       seq_length
												__snake_case     			=       is_training
												__snake_case     			=       use_input_mask
												__snake_case     			=       use_token_type_ids
												__snake_case     			=       use_labels
												__snake_case     			=       vocab_size
												__snake_case     			=       hidden_size
												__snake_case     			=       embedding_size
												__snake_case     			=       num_hidden_layers
												__snake_case     			=       num_attention_heads
												__snake_case     			=       intermediate_size
												__snake_case     			=       hidden_act
												__snake_case     			=       hidden_dropout_prob
												__snake_case     			=       attention_probs_dropout_prob
												__snake_case     			=       max_position_embeddings
												__snake_case     			=       type_vocab_size
												__snake_case     			=       type_sequence_label_size
												__snake_case     			=       initializer_range
												__snake_case     			=       num_labels
												__snake_case     			=       num_choices
												__snake_case     			=       scope





					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												__snake_case     			=       ids_tensor([self.batch_size, self.seq_length]							,						self.vocab_size    )

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

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

												__snake_case     			=       None
												__snake_case     			=       None
												__snake_case     			=       None
												if self.use_labels:
																			__snake_case     			=       ids_tensor([self.batch_size]							,						self.type_sequence_label_size    )
																			__snake_case     			=       ids_tensor([self.batch_size, self.seq_length]							,						self.num_labels    )
																			__snake_case     			=       ids_tensor([self.batch_size]							,						self.num_choices    )

												__snake_case     			=       self.get_config()

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





					def 							lowerCAmelCase      (				self   :							Union[str, Any]    ):
												return MobileBertConfig(
												    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							,						embedding_size=self.embedding_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=snake_case_							,						initializer_range=self.initializer_range							,						)





					def 							lowerCAmelCase      (				self   :							Optional[Any]							,						snake_case_   :							Any							,						snake_case_   :							Optional[Any]							,						snake_case_   :							Tuple							,						snake_case_   :							Any							,						snake_case_   :							str							,						snake_case_   :							Dict							,						snake_case_   :							List[str]    ):
												__snake_case     			=       MobileBertModel(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_    )
												__snake_case     			=       model(snake_case_							,						token_type_ids=snake_case_    )
												__snake_case     			=       model(snake_case_    )

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





					def 							lowerCAmelCase      (				self   :							List[str]							,						snake_case_   :							List[Any]							,						snake_case_   :							Any							,						snake_case_   :							Optional[int]							,						snake_case_   :							int							,						snake_case_   :							int							,						snake_case_   :							str							,						snake_case_   :							str    ):
												__snake_case     			=       MobileBertForMaskedLM(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_    )
												self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.seq_length, self.vocab_size)    )





					def 							lowerCAmelCase      (				self   :							Optional[Any]							,						snake_case_   :							Optional[int]							,						snake_case_   :							Union[str, Any]							,						snake_case_   :							List[str]							,						snake_case_   :							int							,						snake_case_   :							int							,						snake_case_   :							int							,						snake_case_   :							Any    ):
												__snake_case     			=       MobileBertForNextSentencePrediction(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(
												    snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_							,						)
												self.parent.assertEqual(result.logits.shape							,						(self.batch_size, 2)    )





					def 							lowerCAmelCase      (				self   :							Optional[int]							,						snake_case_   :							List[str]							,						snake_case_   :							List[str]							,						snake_case_   :							str							,						snake_case_   :							Optional[Any]							,						snake_case_   :							List[str]							,						snake_case_   :							List[str]							,						snake_case_   :							Dict    ):
												__snake_case     			=       MobileBertForPreTraining(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(
												    snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_							,						next_sentence_label=snake_case_							,						)
												self.parent.assertEqual(result.prediction_logits.shape							,						(self.batch_size, self.seq_length, self.vocab_size)    )
												self.parent.assertEqual(result.seq_relationship_logits.shape							,						(self.batch_size, 2)    )





					def 							lowerCAmelCase      (				self   :							Any							,						snake_case_   :							List[str]							,						snake_case_   :							int							,						snake_case_   :							Any							,						snake_case_   :							Union[str, Any]							,						snake_case_   :							Dict							,						snake_case_   :							int							,						snake_case_   :							int    ):
												__snake_case     			=       MobileBertForQuestionAnswering(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(
												    snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						start_positions=snake_case_							,						end_positions=snake_case_							,						)
												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   :							str							,						snake_case_   :							Tuple							,						snake_case_   :							int							,						snake_case_   :							str							,						snake_case_   :							Dict							,						snake_case_   :							Optional[int]							,						snake_case_   :							str							,						snake_case_   :							List[str]    ):
												__snake_case     			=       self.num_labels
												__snake_case     			=       MobileBertForSequenceClassification(snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_    )
												self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.num_labels)    )





					def 							lowerCAmelCase      (				self   :							Optional[Any]							,						snake_case_   :							Tuple							,						snake_case_   :							str							,						snake_case_   :							Union[str, Any]							,						snake_case_   :							Tuple							,						snake_case_   :							Optional[int]							,						snake_case_   :							Tuple							,						snake_case_   :							List[Any]    ):
												__snake_case     			=       self.num_labels
												__snake_case     			=       MobileBertForTokenClassification(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       model(snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_    )
												self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.seq_length, self.num_labels)    )





					def 							lowerCAmelCase      (				self   :							Tuple							,						snake_case_   :							Union[str, Any]							,						snake_case_   :							Tuple							,						snake_case_   :							Optional[Any]							,						snake_case_   :							Any							,						snake_case_   :							Any							,						snake_case_   :							Dict							,						snake_case_   :							Union[str, Any]    ):
												__snake_case     			=       self.num_choices
												__snake_case     			=       MobileBertForMultipleChoice(config=snake_case_    )
												model.to(snake_case_    )
												model.eval()
												__snake_case     			=       input_ids.unsqueeze(1    ).expand(-1							,						self.num_choices							,						-1    ).contiguous()
												__snake_case     			=       token_type_ids.unsqueeze(1    ).expand(-1							,						self.num_choices							,						-1    ).contiguous()
												__snake_case     			=       input_mask.unsqueeze(1    ).expand(-1							,						self.num_choices							,						-1    ).contiguous()
												__snake_case     			=       model(
												    snake_case_							,						attention_mask=snake_case_							,						token_type_ids=snake_case_							,						labels=snake_case_							,						)
												self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.num_choices)    )





					def 							lowerCAmelCase      (				self   :							List[Any]    ):
												__snake_case     			=       self.prepare_config_and_inputs()
												(
												    (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   (
												    __snake_case
												)			,   
												)     			=       config_and_inputs
												__snake_case     			=       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
												return config, inputs_dict







@require_torch
class 							__magic_name__     (		lowercase__      ,		lowercase__      ,		unittest.TestCase   ):
					_SCREAMING_SNAKE_CASE       :					Dict     							=   (
					    (
					        MobileBertModel,
					        MobileBertForMaskedLM,
					        MobileBertForMultipleChoice,
					        MobileBertForNextSentencePrediction,
					        MobileBertForPreTraining,
					        MobileBertForQuestionAnswering,
					        MobileBertForSequenceClassification,
					        MobileBertForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					_SCREAMING_SNAKE_CASE       :					Optional[int]     							=   (
					    {
					        'feature-extraction': MobileBertModel,
					        'fill-mask': MobileBertForMaskedLM,
					        'question-answering': MobileBertForQuestionAnswering,
					        'text-classification': MobileBertForSequenceClassification,
					        'token-classification': MobileBertForTokenClassification,
					        'zero-shot': MobileBertForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					_SCREAMING_SNAKE_CASE       :					int     							=   True





					def 							lowerCAmelCase      (				self   :							List[str]							,						snake_case_   :							List[str]							,						snake_case_   :							str							,						snake_case_   :							str=False    ):
												__snake_case     			=       super()._prepare_for_class(snake_case_							,						snake_case_							,						return_labels=snake_case_    )

												if return_labels:
																			if model_class in get_values(snake_case_    ):
																										__snake_case     			=       torch.zeros(
																										    (self.model_tester.batch_size, self.model_tester.seq_length)							,						dtype=torch.long							,						device=snake_case_    )
																										__snake_case     			=       torch.zeros(
																										    self.model_tester.batch_size							,						dtype=torch.long							,						device=snake_case_    )
												return inputs_dict





					def 							lowerCAmelCase      (				self   :							Optional[Any]    ):
												__snake_case     			=       MobileBertModelTester(self    )
												__snake_case     			=       ConfigTester(self							,						config_class=snake_case_							,						hidden_size=37    )





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





					def 							lowerCAmelCase      (				self   :							Tuple    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_model(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							Optional[Any]    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_masked_lm(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							Tuple    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_multiple_choice(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							Any    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							Any    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_pretraining(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							List[str]    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_question_answering(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							List[Any]    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_sequence_classification(*snake_case_    )





					def 							lowerCAmelCase      (				self   :							str    ):
												__snake_case     			=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_token_classification(*snake_case_    )




def   __UpperCamelCase   (					SCREAMING_SNAKE_CASE )  ->     List[Any]:







							"""simple docstring"""


							return torch.tensor(
							    SCREAMING_SNAKE_CASE							,    dtype=torch.long							,    device=SCREAMING_SNAKE_CASE							,    )


_SCREAMING_SNAKE_CASE	       =      1E-3







@require_torch
@require_sentencepiece
@require_tokenizers
class 							__magic_name__     (		unittest.TestCase   ):





					@slow
					def 							lowerCAmelCase      (				self   :							Tuple    ):
												__snake_case     			=       MobileBertModel.from_pretrained("google/mobilebert-uncased"    ).to(snake_case_    )
												__snake_case     			=       _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]    )
												with torch.no_grad():
																			__snake_case     			=       model(snake_case_    )[0]
												__snake_case     			=       torch.Size((1, 9, 512)    )
												self.assertEqual(output.shape							,						snake_case_    )
												__snake_case     			=       torch.tensor(
												    [
												        [
												            [-2.4_73_65_26e07, 8.2_69_16_56e04, 1.6_52_18_38e05],
												            [-5.7_54_17_04e-01, 3.9_05_60_22e00, 4.4_01_15_07e00],
												            [2.6_04_73_59e00, 1.5_67_76_52e00, -1.7_32_41_88e-01],
												        ]
												    ]							,						device=snake_case_							,						)

												# MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
												# ~1 difference, it's therefore not a good idea to measure using addition.
												# Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
												# result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
												__snake_case     			=       torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE    )
												__snake_case     			=       torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE    )

												self.assertTrue(lower_bound and upper_bound    )
