

import os
import tempfile
import unittest

from transformers import DistilBertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, 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 (
           DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
           DistilBertForMaskedLM,
           DistilBertForMultipleChoice,
           DistilBertForQuestionAnswering,
           DistilBertForSequenceClassification,
           DistilBertForTokenClassification,
           DistilBertModel,
       )



class       __snake_case  (    lowerCAmelCase__       ):

  def __init__(					self						,		_A						,		_A=13						,		_A=7						,		_A=True						,		_A=True						,		_A=False						,		_A=True						,		_A=99						,		_A=32						,		_A=5						,		_A=4						,		_A=37						,		_A="gelu"						,		_A=0.1						,		_A=0.1						,		_A=512						,		_A=16						,		_A=2						,		_A=0.0_2						,		_A=3						,		_A=4						,		_A=None						,		):
      SCREAMING_SNAKE_CASE_									=						parent
      SCREAMING_SNAKE_CASE_									=						batch_size
      SCREAMING_SNAKE_CASE_									=						seq_length
      SCREAMING_SNAKE_CASE_									=						is_training
      SCREAMING_SNAKE_CASE_									=						use_input_mask
      SCREAMING_SNAKE_CASE_									=						use_token_type_ids
      SCREAMING_SNAKE_CASE_									=						use_labels
      SCREAMING_SNAKE_CASE_									=						vocab_size
      SCREAMING_SNAKE_CASE_									=						hidden_size
      SCREAMING_SNAKE_CASE_									=						num_hidden_layers
      SCREAMING_SNAKE_CASE_									=						num_attention_heads
      SCREAMING_SNAKE_CASE_									=						intermediate_size
      SCREAMING_SNAKE_CASE_									=						hidden_act
      SCREAMING_SNAKE_CASE_									=						hidden_dropout_prob
      SCREAMING_SNAKE_CASE_									=						attention_probs_dropout_prob
      SCREAMING_SNAKE_CASE_									=						max_position_embeddings
      SCREAMING_SNAKE_CASE_									=						type_vocab_size
      SCREAMING_SNAKE_CASE_									=						type_sequence_label_size
      SCREAMING_SNAKE_CASE_									=						initializer_range
      SCREAMING_SNAKE_CASE_									=						num_labels
      SCREAMING_SNAKE_CASE_									=						num_choices
      SCREAMING_SNAKE_CASE_									=						scope

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

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

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

      SCREAMING_SNAKE_CASE_									=						self.get_config()

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

  def 						lowerCAmelCase__     (					self):
      return 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						,		)

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		_A						,		_A):
      SCREAMING_SNAKE_CASE_									=						DistilBertModel(config=_A)
      model.to(_A)
      model.eval()
      SCREAMING_SNAKE_CASE_									=						model(_A						,		_A)
      SCREAMING_SNAKE_CASE_									=						model(_A)
      self.parent.assertEqual(result.last_hidden_state.shape						,		(self.batch_size, self.seq_length, self.hidden_size))

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		_A						,		_A):
      SCREAMING_SNAKE_CASE_									=						DistilBertForMaskedLM(config=_A)
      model.to(_A)
      model.eval()
      SCREAMING_SNAKE_CASE_									=						model(_A						,		attention_mask=_A						,		labels=_A)
      self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.seq_length, self.vocab_size))

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		_A						,		_A):
      SCREAMING_SNAKE_CASE_									=						DistilBertForQuestionAnswering(config=_A)
      model.to(_A)
      model.eval()
      SCREAMING_SNAKE_CASE_									=						model(
          _A						,		attention_mask=_A						,		start_positions=_A						,		end_positions=_A)
      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						,		_A						,		_A						,		_A						,		_A						,		_A						,		_A):
      SCREAMING_SNAKE_CASE_									=						self.num_labels
      SCREAMING_SNAKE_CASE_									=						DistilBertForSequenceClassification(_A)
      model.to(_A)
      model.eval()
      SCREAMING_SNAKE_CASE_									=						model(_A						,		attention_mask=_A						,		labels=_A)
      self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_labels))

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		_A						,		_A):
      SCREAMING_SNAKE_CASE_									=						self.num_labels
      SCREAMING_SNAKE_CASE_									=						DistilBertForTokenClassification(config=_A)
      model.to(_A)
      model.eval()

      SCREAMING_SNAKE_CASE_									=						model(_A						,		attention_mask=_A						,		labels=_A)
      self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.seq_length, self.num_labels))

  def 						lowerCAmelCase__     (					self						,		_A						,		_A						,		_A						,		_A						,		_A						,		_A):
      SCREAMING_SNAKE_CASE_									=						self.num_choices
      SCREAMING_SNAKE_CASE_									=						DistilBertForMultipleChoice(config=_A)
      model.to(_A)
      model.eval()
      SCREAMING_SNAKE_CASE_									=						input_ids.unsqueeze(1).expand(-1						,		self.num_choices						,		-1).contiguous()
      SCREAMING_SNAKE_CASE_									=						input_mask.unsqueeze(1).expand(-1						,		self.num_choices						,		-1).contiguous()
      SCREAMING_SNAKE_CASE_									=						model(
          _A						,		attention_mask=_A						,		labels=_A						,		)
      self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_choices))


  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.prepare_config_and_inputs()
      ((SCREAMING_SNAKE_CASE_)							,			(SCREAMING_SNAKE_CASE_)							,			(SCREAMING_SNAKE_CASE_)							,			(SCREAMING_SNAKE_CASE_)							,			(SCREAMING_SNAKE_CASE_)							,			(SCREAMING_SNAKE_CASE_))									=						config_and_inputs
      SCREAMING_SNAKE_CASE_									=						{'input_ids': input_ids, 'attention_mask': input_mask}
      return config, inputs_dict






@require_torch
class       __snake_case  (    lowerCAmelCase__     , lowerCAmelCase__     , unittest.TestCase       ):
  __lowerCAmelCase     :	Tuple						 =  (
      (
          DistilBertModel,
          DistilBertForMaskedLM,
          DistilBertForMultipleChoice,
          DistilBertForQuestionAnswering,
          DistilBertForSequenceClassification,
          DistilBertForTokenClassification,
      )
      if is_torch_available()
      else None
  )
  __lowerCAmelCase     :	int						 =  (
      {
          'feature-extraction': DistilBertModel,
          'fill-mask': DistilBertForMaskedLM,
          'question-answering': DistilBertForQuestionAnswering,
          'text-classification': DistilBertForSequenceClassification,
          'token-classification': DistilBertForTokenClassification,
          'zero-shot': DistilBertForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  __lowerCAmelCase     :	str						 =  True
  __lowerCAmelCase     :	Optional[Any]						 =  True
  __lowerCAmelCase     :	Optional[int]						 =  True
  __lowerCAmelCase     :	int						 =  True

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						DistilBertModelTester(self)
      SCREAMING_SNAKE_CASE_									=						ConfigTester(self						,		config_class=_A						,		dim=37)

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

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_model(*_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_masked_lm(*_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_question_answering(*_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_sequence_classification(*_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_token_classification(*_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_distilbert_for_multiple_choice(*_A)

  @slow
  def 						lowerCAmelCase__     (					self):
      for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          SCREAMING_SNAKE_CASE_									=						DistilBertModel.from_pretrained(_A)
          self.assertIsNotNone(_A)


  @slow
  @require_torch_gpu
  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_							,			SCREAMING_SNAKE_CASE_									=						self.model_tester.prepare_config_and_inputs_for_common()
      for model_class in self.all_model_classes:
          # BertForMultipleChoice behaves incorrectly in JIT environments.
          if model_class == DistilBertForMultipleChoice:
              return

          SCREAMING_SNAKE_CASE_									=						True
          SCREAMING_SNAKE_CASE_									=						model_class(config=_A)

          SCREAMING_SNAKE_CASE_									=						self._prepare_for_class(_A						,		_A)
          SCREAMING_SNAKE_CASE_									=						torch.jit.trace(
              _A						,		(inputs_dict['input_ids'].to('cpu'), inputs_dict['attention_mask'].to('cpu')))

          with tempfile.TemporaryDirectory() as tmp:
              torch.jit.save(_A						,		os.path.join(_A						,		'traced_model.pt'))
              SCREAMING_SNAKE_CASE_									=						torch.jit.load(os.path.join(_A						,		'traced_model.pt')						,		map_location=_A)
              loaded(inputs_dict['input_ids'].to(_A)						,		inputs_dict['attention_mask'].to(_A))






@require_torch
class       __snake_case  (    unittest.TestCase       ):

  @slow
  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						DistilBertModel.from_pretrained('distilbert-base-uncased')
      SCREAMING_SNAKE_CASE_									=						torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]])
      SCREAMING_SNAKE_CASE_									=						torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
      with torch.no_grad():
          SCREAMING_SNAKE_CASE_									=						model(_A						,		attention_mask=_A)[0]
      SCREAMING_SNAKE_CASE_									=						torch.Size((1, 11, 768))
      self.assertEqual(output.shape						,		_A)
      SCREAMING_SNAKE_CASE_									=						torch.tensor(
          [[[-0.1_6_3_9, 0.3_2_9_9, 0.1_6_4_8], [-0.1_7_4_6, 0.3_2_8_9, 0.1_7_1_0], [-0.1_8_8_4, 0.3_3_5_7, 0.1_8_1_0]]])

      self.assertTrue(torch.allclose(output[:, 1:4, 1:4]						,		_A						,		atol=1E-4))





from multiprocessing import Lock, Pipe, Process

# lock used to ensure that two processes do not access a pipe at the same time
UpperCamelCase__  :      int			=    Lock()


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		str ,   _SCREAMING_SNAKE_CASE					:		Union[str, Any] ,   _SCREAMING_SNAKE_CASE					:		Dict ,   _SCREAMING_SNAKE_CASE					:		List[Any] ,   _SCREAMING_SNAKE_CASE					:		Optional[int] ,   _SCREAMING_SNAKE_CASE					:		Optional[int] ,   _SCREAMING_SNAKE_CASE					:		Any       ):





    """simple docstring"""


    global process_lock

    # we perform n swaps since after n swaps we know we are sorted
    # we *could* stop early if we are sorted already, but it takes as long to
    # find out we are sorted as it does to sort the list with this algorithm
    for i in range(0 ,   10       ):
        if (i + position) % 2 == 0 and r_send is not None:
            # send your value to your right neighbor
            process_lock.acquire()
            r_send[1].send(_SCREAMING_SNAKE_CASE       )
            process_lock.release()

            # receive your right neighbor's value
            process_lock.acquire()
            SCREAMING_SNAKE_CASE_									=						rr_cv[0].recv()
            process_lock.release()

            # take the lower value since you are on the left
            SCREAMING_SNAKE_CASE_									=						min(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       )
        elif (i + position) % 2 != 0 and l_send is not None:
            # send your value to your left neighbor
            process_lock.acquire()
            l_send[1].send(_SCREAMING_SNAKE_CASE       )
            process_lock.release()

            # receive your left neighbor's value
            process_lock.acquire()
            SCREAMING_SNAKE_CASE_									=						lr_cv[0].recv()
            process_lock.release()

            # take the higher value since you are on the right
            SCREAMING_SNAKE_CASE_									=						max(_SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE       )
    # after all swaps are performed, send the values back to main
    result_pipe[1].send(_SCREAMING_SNAKE_CASE       )


def 					_UpperCAmelCase       (  _SCREAMING_SNAKE_CASE					:		Union[str, Any]       ):





    """simple docstring"""


    SCREAMING_SNAKE_CASE_									=						[]
    SCREAMING_SNAKE_CASE_									=						[]
    # initialize the list of pipes where the values will be retrieved
    for _ in arr:
        result_pipe.append(Pipe()       )
    # creates the processes
    # the first and last process only have one neighbor so they are made outside
    # of the loop
    SCREAMING_SNAKE_CASE_									=						Pipe()
    SCREAMING_SNAKE_CASE_									=						Pipe()
    process_array_.append(
        Process(
            target=_SCREAMING_SNAKE_CASE ,   args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0]) ,   )       )
    SCREAMING_SNAKE_CASE_									=						temp_rs
    SCREAMING_SNAKE_CASE_									=						temp_rr

    for i in range(1 ,   len(_SCREAMING_SNAKE_CASE       ) - 1       ):
        SCREAMING_SNAKE_CASE_									=						Pipe()
        SCREAMING_SNAKE_CASE_									=						Pipe()
        process_array_.append(
            Process(
                target=_SCREAMING_SNAKE_CASE ,   args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i]) ,   )       )
        SCREAMING_SNAKE_CASE_									=						temp_rs
        SCREAMING_SNAKE_CASE_									=						temp_rr

    process_array_.append(
        Process(
            target=_SCREAMING_SNAKE_CASE ,   args=(
                len(_SCREAMING_SNAKE_CASE       ) - 1,
                arr[len(_SCREAMING_SNAKE_CASE       ) - 1],
                temp_ls,
                None,
                temp_lr,
                None,
                result_pipe[len(_SCREAMING_SNAKE_CASE       ) - 1],
            ) ,   )       )

    # start the processes
    for p in process_array_:
        p.start()

    # wait for the processes to end and write their values to the list
    for p in range(0 ,   len(_SCREAMING_SNAKE_CASE       )       ):
        SCREAMING_SNAKE_CASE_									=						result_pipe[p][0].recv()
        process_array_[p].join()
    return arr


def 					_UpperCAmelCase       (  ):





    """simple docstring"""


    SCREAMING_SNAKE_CASE_									=						list(range(10 ,   0 ,   -1       )       )
    print('Initial List'       )
    print(*_SCREAMING_SNAKE_CASE       )
    SCREAMING_SNAKE_CASE_									=						odd_even_transposition(_SCREAMING_SNAKE_CASE       )
    print('Sorted List\n'       )
    print(*_SCREAMING_SNAKE_CASE       )


if __name__ == "__main__":
       main()


