



"""simple docstring"""
import unittest

from transformers import FunnelConfig, FunnelTokenizer, 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    MODEL_FOR_PRETRAINING_MAPPING,
			    FunnelBaseModel,
			    FunnelForMaskedLM,
			    FunnelForMultipleChoice,
			    FunnelForPreTraining,
			    FunnelForQuestionAnswering,
			    FunnelForSequenceClassification,
			    FunnelForTokenClassification,
			    FunnelModel,
			)


class 							lowerCamelCase_:





					'''simple docstring'''



					def __init__(			self ,				lowerCamelCase__ ,				lowerCamelCase__=1_3 ,				lowerCamelCase__=7 ,				lowerCamelCase__=True ,				lowerCamelCase__=True ,				lowerCamelCase__=True ,				lowerCamelCase__=True ,				lowerCamelCase__=9_9 ,				lowerCamelCase__=[1, 1, 2] ,				lowerCamelCase__=1 ,				lowerCamelCase__=3_2 ,				lowerCamelCase__=4 ,				lowerCamelCase__=8 ,				lowerCamelCase__=3_7 ,				lowerCamelCase__="gelu_new" ,				lowerCamelCase__=0.1 ,				lowerCamelCase__=0.1 ,				lowerCamelCase__=0.0 ,				lowerCamelCase__=5_1_2 ,				lowerCamelCase__=3 ,				lowerCamelCase__=0.0_2 ,				lowerCamelCase__=3 ,				lowerCamelCase__=4 ,				lowerCamelCase__=None ,				lowerCamelCase__=False ,				):
									_lowerCamelCase					     = parent
									_lowerCamelCase					     = batch_size
									_lowerCamelCase					     = seq_length
									_lowerCamelCase					     = is_training
									_lowerCamelCase					     = use_input_mask
									_lowerCamelCase					     = use_token_type_ids
									_lowerCamelCase					     = use_labels
									_lowerCamelCase					     = vocab_size
									_lowerCamelCase					     = block_sizes
									_lowerCamelCase					     = num_decoder_layers
									_lowerCamelCase					     = d_model
									_lowerCamelCase					     = n_head
									_lowerCamelCase					     = d_head
									_lowerCamelCase					     = d_inner
									_lowerCamelCase					     = hidden_act
									_lowerCamelCase					     = hidden_dropout
									_lowerCamelCase					     = attention_dropout
									_lowerCamelCase					     = activation_dropout
									_lowerCamelCase					     = max_position_embeddings
									_lowerCamelCase					     = type_vocab_size
									_lowerCamelCase					     = 2
									_lowerCamelCase					     = num_labels
									_lowerCamelCase					     = num_choices
									_lowerCamelCase					     = scope
									_lowerCamelCase					     = initializer_std

									# Used in the tests to check the size of the first attention layer
									_lowerCamelCase					     = n_head
									# Used in the tests to check the size of the first hidden state
									_lowerCamelCase					     = self.d_model
									# Used in the tests to check the number of output hidden states/attentions
									_lowerCamelCase					     = sum(self.block_sizes      ) + (0 if base else self.num_decoder_layers)
									# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
									# the last hidden state of the first block (which is the first hidden state of the decoder).
									if not base:
													_lowerCamelCase					     = self.num_hidden_layers + 2

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size      )

									_lowerCamelCase					     = None
									if self.use_input_mask:
													_lowerCamelCase					     = ids_tensor([self.batch_size, self.seq_length] ,				vocab_size=2      )

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

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

									_lowerCamelCase					     = self.get_config()

									return (
									    config,
									    input_ids,
									    token_type_ids,
									    input_mask,
									    sequence_labels,
									    token_labels,
									    choice_labels,
									    fake_token_labels,
									)

					def 	snake_case__					(			self      ):
									return FunnelConfig(
									    vocab_size=self.vocab_size ,				block_sizes=self.block_sizes ,				num_decoder_layers=self.num_decoder_layers ,				d_model=self.d_model ,				n_head=self.n_head ,				d_head=self.d_head ,				d_inner=self.d_inner ,				hidden_act=self.hidden_act ,				hidden_dropout=self.hidden_dropout ,				attention_dropout=self.attention_dropout ,				activation_dropout=self.activation_dropout ,				max_position_embeddings=self.max_position_embeddings ,				type_vocab_size=self.type_vocab_size ,				initializer_std=self.initializer_std ,				)

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				):
									_lowerCamelCase					     = FunnelModel(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				token_type_ids=lowerCamelCase__      )
									_lowerCamelCase					     = model(lowerCamelCase__ ,				token_type_ids=lowerCamelCase__      )
									_lowerCamelCase					     = model(lowerCamelCase__      )
									self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.d_model)      )

									_lowerCamelCase					     = False
									_lowerCamelCase					     = model(lowerCamelCase__      )
									self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.d_model)      )

									_lowerCamelCase					     = False
									_lowerCamelCase					     = model(lowerCamelCase__      )
									self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.d_model)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				):
									_lowerCamelCase					     = FunnelBaseModel(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				token_type_ids=lowerCamelCase__      )
									_lowerCamelCase					     = model(lowerCamelCase__ ,				token_type_ids=lowerCamelCase__      )
									_lowerCamelCase					     = model(lowerCamelCase__      )
									self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, 2, self.d_model)      )

									_lowerCamelCase					     = False
									_lowerCamelCase					     = model(lowerCamelCase__      )
									self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, 3, self.d_model)      )

									_lowerCamelCase					     = False
									_lowerCamelCase					     = model(lowerCamelCase__      )
									self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, 2, self.d_model)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				):
									_lowerCamelCase					     = self.num_labels
									_lowerCamelCase					     = FunnelForPreTraining(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				token_type_ids=lowerCamelCase__ ,				labels=lowerCamelCase__      )
									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				):
									_lowerCamelCase					     = FunnelForMaskedLM(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				token_type_ids=lowerCamelCase__ ,				labels=lowerCamelCase__      )
									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.vocab_size)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				):
									_lowerCamelCase					     = self.num_labels
									_lowerCamelCase					     = FunnelForSequenceClassification(lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				token_type_ids=lowerCamelCase__ ,				labels=lowerCamelCase__      )
									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				):
									_lowerCamelCase					     = self.num_choices
									_lowerCamelCase					     = FunnelForMultipleChoice(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = input_ids.unsqueeze(1      ).expand(-1 ,				self.num_choices ,				-1      ).contiguous()
									_lowerCamelCase					     = token_type_ids.unsqueeze(1      ).expand(-1 ,				self.num_choices ,				-1      ).contiguous()
									_lowerCamelCase					     = input_mask.unsqueeze(1      ).expand(-1 ,				self.num_choices ,				-1      ).contiguous()
									_lowerCamelCase					     = model(
									    lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				token_type_ids=lowerCamelCase__ ,				labels=lowerCamelCase__ ,				)
									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_choices)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				):
									_lowerCamelCase					     = self.num_labels
									_lowerCamelCase					     = FunnelForTokenClassification(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				token_type_ids=lowerCamelCase__ ,				labels=lowerCamelCase__      )
									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.num_labels)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				):
									_lowerCamelCase					     = FunnelForQuestionAnswering(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(
									    lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				token_type_ids=lowerCamelCase__ ,				start_positions=lowerCamelCase__ ,				end_positions=lowerCamelCase__ ,				)
									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 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.prepare_config_and_inputs()
									(
									    (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   
									)					     = config_and_inputs
									_lowerCamelCase					     = {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
									return config, inputs_dict


@require_torch
class 							lowerCamelCase_(      A__, A__, unittest.TestCase			):





					'''simple docstring'''



					lowercase__      :				int        =       False
					lowercase__      :				List[Any]        =       False
					lowercase__      :				List[str]        =       (
					    (
					        FunnelModel,
					        FunnelForMaskedLM,
					        FunnelForPreTraining,
					        FunnelForQuestionAnswering,
					        FunnelForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					lowercase__      :				Tuple        =       (
					    {
					        'feature-extraction': (FunnelBaseModel, FunnelModel),
					        'fill-mask': FunnelForMaskedLM,
					        'question-answering': FunnelForQuestionAnswering,
					        'text-classification': FunnelForSequenceClassification,
					        'token-classification': FunnelForTokenClassification,
					        'zero-shot': FunnelForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__=False      ):
									_lowerCamelCase					     = super()._prepare_for_class(lowerCamelCase__ ,				lowerCamelCase__ ,				return_labels=lowerCamelCase__      )

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

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = FunnelModelTester(self      )
									_lowerCamelCase					     = ConfigTester(self ,				config_class=lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									self.config_tester.run_common_tests()

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_pretraining(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__      )





					def 	snake_case__					(			self ,				lowerCamelCase__      ):
									if hasattr(lowerCamelCase__ ,				'''weight'''      ) and module.weight is not None:
													module.weight.data.fill_(3      )
									if hasattr(lowerCamelCase__ ,				'''bias'''      ) and module.bias is not None:
													module.bias.data.fill_(3      )

									for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
													if hasattr(lowerCamelCase__ ,				lowerCamelCase__      ) and getattr(lowerCamelCase__ ,				lowerCamelCase__      ) is not None:
																	_lowerCamelCase					     = getattr(lowerCamelCase__ ,				lowerCamelCase__      )
																	weight.data.fill_(3      )


@require_torch
class 							lowerCamelCase_(      A__, unittest.TestCase			):





					'''simple docstring'''



					lowercase__      :				List[Any]        =       False
					lowercase__      :				List[str]        =       False
					lowercase__      :				int        =       (
					    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
					)

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = FunnelModelTester(self ,				base=lowerCamelCase__      )
									_lowerCamelCase					     = ConfigTester(self ,				config_class=lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									self.config_tester.run_common_tests()

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_base_model(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase     ,   _lowerCamelCase					     = self.model_tester.prepare_config_and_inputs_for_common()
									_lowerCamelCase					     = True

									for model_class in self.all_model_classes:
													if model_class.__name__ == "FunnelBaseModel":
																	continue
													_lowerCamelCase					     = model_class(lowerCamelCase__      )
													model.to(lowerCamelCase__      )
													model.train()
													_lowerCamelCase					     = self._prepare_for_class(lowerCamelCase__ ,				lowerCamelCase__ ,				return_labels=lowerCamelCase__      )
													_lowerCamelCase					     = model(**lowerCamelCase__      ).loss
													loss.backward()





					def 	snake_case__					(			self ,				lowerCamelCase__      ):
									if hasattr(lowerCamelCase__ ,				'''weight'''      ) and module.weight is not None:
													module.weight.data.fill_(3      )
									if hasattr(lowerCamelCase__ ,				'''bias'''      ) and module.bias is not None:
													module.bias.data.fill_(3      )

									for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
													if hasattr(lowerCamelCase__ ,				lowerCamelCase__      ) and getattr(lowerCamelCase__ ,				lowerCamelCase__      ) is not None:
																	_lowerCamelCase					     = getattr(lowerCamelCase__ ,				lowerCamelCase__      )
																	weight.data.fill_(3      )


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





					'''simple docstring'''



					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = 1_3
									_lowerCamelCase					     = 7
									_lowerCamelCase					     = torch.arange(0 ,				batch_size * sequence_length      ).long().reshape(lowerCamelCase__ ,				lowerCamelCase__      )
									_lowerCamelCase					     = [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
									_lowerCamelCase					     = torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]      )

									_lowerCamelCase					     = FunnelModel.from_pretrained('''sgugger/funnel-random-tiny'''      )
									_lowerCamelCase					     = model(lowerCamelCase__ ,				token_type_ids=lowerCamelCase__      )[0].abs()

									_lowerCamelCase					     = torch.tensor(2_3_4_4.8_3_5_2      )
									_lowerCamelCase					     = torch.tensor(0.8_0_5_2      )
									self.assertTrue(torch.allclose(output.sum() ,				lowerCamelCase__ ,				atol=1e-4      )      )
									self.assertTrue(torch.allclose(output.mean() ,				lowerCamelCase__ ,				atol=1e-4      )      )

									_lowerCamelCase					     = torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]      )
									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				token_type_ids=lowerCamelCase__      )[0].abs()

									_lowerCamelCase					     = torch.tensor(2_3_4_3.8_4_2_5      )
									_lowerCamelCase					     = torch.tensor(0.8_0_4_9      )
									self.assertTrue(torch.allclose(output.sum() ,				lowerCamelCase__ ,				atol=1e-4      )      )
									self.assertTrue(torch.allclose(output.mean() ,				lowerCamelCase__ ,				atol=1e-4      )      )





					@slow
					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = FunnelTokenizer.from_pretrained('''huggingface/funnel-small'''      )
									_lowerCamelCase					     = FunnelModel.from_pretrained('''huggingface/funnel-small'''      )
									_lowerCamelCase					     = tokenizer('''Hello! I am the Funnel Transformer model.''' ,				return_tensors='''pt'''      )
									_lowerCamelCase					     = model(**lowerCamelCase__      )[0]

									_lowerCamelCase					     = torch.tensor(2_3_5.7_2_4_6      )
									_lowerCamelCase					     = torch.tensor(0.0_2_5_6      )
									self.assertTrue(torch.allclose(output.sum() ,				lowerCamelCase__ ,				atol=1e-4      )      )
									self.assertTrue(torch.allclose(output.mean() ,				lowerCamelCase__ ,				atol=1e-4      )      )


"""simple docstring"""

import doctest

import projectq
from projectq.ops import H, Measure




def 			lowercase__  (    lowerCAmelCase :    projectq.cengines._main.MainEngine       )       ->					int:






    """simple docstring"""




    UpperCAmelCase  	=    quantum_engine.allocate_qubit()
    H | qubit
    Measure | qubit
    return int(lowerCAmelCase       )


if __name__ == "__main__":
   doctest.testmod()

   # initialises a new quantum backend
   SCREAMING_SNAKE_CASE_			    =  projectq.MainEngine()

   # Generate a list of 10 random numbers
   SCREAMING_SNAKE_CASE_			    =  [get_random_number(quantum_engine) for _ in range(10)]

   # Flushes the quantum engine from memory
   quantum_engine.flush()

   print('''Random numbers''', random_numbers_list)



