
import unittest

import numpy as np

from transformers.file_utils import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import DPTImageProcessor




class 	__snake_case	( unittest.TestCase		):





      '''simple docstring'''






      def __init__(					self     :				int    ,  _UpperCamelCase     :				List[Any]    ,  _UpperCamelCase     :				int=7    ,  _UpperCamelCase     :				Tuple=3    ,  _UpperCamelCase     :				Optional[int]=18    ,  _UpperCamelCase     :				Any=30    ,  _UpperCamelCase     :				Optional[Any]=400    ,  _UpperCamelCase     :				int=True    ,  _UpperCamelCase     :				Optional[int]=None    ,  _UpperCamelCase     :				Any=True    ,  _UpperCamelCase     :				str=[0.5, 0.5, 0.5]    ,  _UpperCamelCase     :				int=[0.5, 0.5, 0.5]    ,  )				->Tuple:


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						size if size is not None else {"""height""": 18, """width""": 18}
          _lowerCamelCase     :		Dict 						=						parent
          _lowerCamelCase     :		List[str] 						=						batch_size
          _lowerCamelCase     :		Union[str, Any] 						=						num_channels
          _lowerCamelCase     :		Dict 						=						image_size
          _lowerCamelCase     :		List[Any] 						=						min_resolution
          _lowerCamelCase     :		Tuple 						=						max_resolution
          _lowerCamelCase     :		int 						=						do_resize
          _lowerCamelCase     :		Tuple 						=						size
          _lowerCamelCase     :		Union[str, Any] 						=						do_normalize
          _lowerCamelCase     :		Dict 						=						image_mean
          _lowerCamelCase     :		str 						=						image_std




      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any])				->str:


          """simple docstring"""


          return {
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_normalize": self.do_normalize,
              "do_resize": self.do_resize,
              "size": self.size,
          }





@require_torch
@require_vision
class 	__snake_case	( __lowerCAmelCase ,  unittest.TestCase		):





      '''simple docstring'''






      _snake_case    		=       DPTImageProcessor if is_vision_available() else None



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[str])				->Dict:


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						DPTImageProcessingTester(self)



      @property
      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->Optional[int]:


          """simple docstring"""


          return self.image_processor_tester.prepare_image_processor_dict()



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Optional[int])				->int:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(_UpperCamelCase    ,  """image_mean"""))
          self.assertTrue(hasattr(_UpperCamelCase    ,  """image_std"""))
          self.assertTrue(hasattr(_UpperCamelCase    ,  """do_normalize"""))
          self.assertTrue(hasattr(_UpperCamelCase    ,  """do_resize"""))
          self.assertTrue(hasattr(_UpperCamelCase    ,  """size"""))



      def 	_SCREAMING_SNAKE_CASE    (					self     :				int)				->Dict:


          """simple docstring"""


          _lowerCamelCase     :		Optional[Any] 						=						self.image_processing_class.from_dict(self.image_processor_dict)
          self.assertEqual(image_processor.size    ,  {"""height""": 18, """width""": 18})

          _lowerCamelCase     :		Optional[Any] 						=						self.image_processing_class.from_dict(self.image_processor_dict    ,  size=42)
          self.assertEqual(image_processor.size    ,  {"""height""": 42, """width""": 42})



      def 	_SCREAMING_SNAKE_CASE    (					self     :				List[Any])				->List[str]:


          """simple docstring"""


          _lowerCamelCase     :		str 						=						self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowerCamelCase     :		Dict 						=						prepare_image_inputs(self.image_processor_tester    ,  equal_resolution=_UpperCamelCase)
          for image in image_inputs:
              self.assertIsInstance(_UpperCamelCase    ,  Image.Image)

          # Test not batched input
          _lowerCamelCase     :		Optional[int] 						=						image_processing(image_inputs[0]    ,  return_tensors="""pt""").pixel_values
          self.assertEqual(
              encoded_images.shape    ,  (
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size["""height"""],
                  self.image_processor_tester.size["""width"""],
              )    ,  )

          # Test batched
          _lowerCamelCase     :		Optional[int] 						=						image_processing(_UpperCamelCase    ,  return_tensors="""pt""").pixel_values
          self.assertEqual(
              encoded_images.shape    ,  (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size["""height"""],
                  self.image_processor_tester.size["""width"""],
              )    ,  )



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Any)				->List[str]:


          """simple docstring"""


          _lowerCamelCase     :		List[str] 						=						self.image_processing_class(**self.image_processor_dict)
          # create random numpy tensors
          _lowerCamelCase     :		List[str] 						=						prepare_image_inputs(self.image_processor_tester    ,  equal_resolution=_UpperCamelCase    ,  numpify=_UpperCamelCase)
          for image in image_inputs:
              self.assertIsInstance(_UpperCamelCase    ,  np.ndarray)

          # Test not batched input
          _lowerCamelCase     :		List[Any] 						=						image_processing(image_inputs[0]    ,  return_tensors="""pt""").pixel_values
          self.assertEqual(
              encoded_images.shape    ,  (
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size["""height"""],
                  self.image_processor_tester.size["""width"""],
              )    ,  )

          # Test batched
          _lowerCamelCase     :		List[str] 						=						image_processing(_UpperCamelCase    ,  return_tensors="""pt""").pixel_values
          self.assertEqual(
              encoded_images.shape    ,  (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size["""height"""],
                  self.image_processor_tester.size["""width"""],
              )    ,  )




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


          """simple docstring"""


          _lowerCamelCase     :		Any 						=						self.image_processing_class(**self.image_processor_dict)
          # create random PyTorch tensors
          _lowerCamelCase     :		Optional[int] 						=						prepare_image_inputs(self.image_processor_tester    ,  equal_resolution=_UpperCamelCase    ,  torchify=_UpperCamelCase)
          for image in image_inputs:
              self.assertIsInstance(_UpperCamelCase    ,  torch.Tensor)

          # Test not batched input
          _lowerCamelCase     :		Tuple 						=						image_processing(image_inputs[0]    ,  return_tensors="""pt""").pixel_values
          self.assertEqual(
              encoded_images.shape    ,  (
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size["""height"""],
                  self.image_processor_tester.size["""width"""],
              )    ,  )

          # Test batched
          _lowerCamelCase     :		str 						=						image_processing(_UpperCamelCase    ,  return_tensors="""pt""").pixel_values
          self.assertEqual(
              encoded_images.shape    ,  (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.size["""height"""],
                  self.image_processor_tester.size["""width"""],
              )    ,  )





import argparse
import os

import torch

from transformers import (
    XLNetConfig,
    XLNetForQuestionAnswering,
    XLNetForSequenceClassification,
    XLNetLMHeadModel,
    load_tf_weights_in_xlnet,
)
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


lowerCAmelCase			:							int     ={
    "cola": 2,
    "mnli": 3,
    "mrpc": 2,
    "sst-2": 2,
    "sts-b": 1,
    "qqp": 2,
    "qnli": 2,
    "rte": 2,
    "wnli": 2,
}


logging.set_verbosity_info()







def 						A__  (					__A   , __A   , __A   , __A=None	):







    '''simple docstring'''
    # Initialise PyTorch model
    _lowerCamelCase     :		Tuple 						=						XLNetConfig.from_json_file(__A	)

    _lowerCamelCase     :		List[Any] 						=						finetuning_task.lower() if finetuning_task is not None else """"""
    if finetuning_task in GLUE_TASKS_NUM_LABELS:
        print(F"""Building PyTorch XLNetForSequenceClassification model from configuration: {config}"""	)
        _lowerCamelCase     :		int 						=						finetuning_task
        _lowerCamelCase     :		Union[str, Any] 						=						GLUE_TASKS_NUM_LABELS[finetuning_task]
        _lowerCamelCase     :		int 						=						XLNetForSequenceClassification(__A	)
    elif "squad" in finetuning_task:
        _lowerCamelCase     :		Dict 						=						finetuning_task
        _lowerCamelCase     :		Optional[Any] 						=						XLNetForQuestionAnswering(__A	)
    else:
        _lowerCamelCase     :		Any 						=						XLNetLMHeadModel(__A	)

    # Load weights from tf checkpoint
    load_tf_weights_in_xlnet(__A   , __A   , __A	)

    # Save pytorch-model
    _lowerCamelCase     :		Optional[Any] 						=						os.path.join(__A   , __A	)
    _lowerCamelCase     :		Any 						=						os.path.join(__A   , __A	)
    print(F"""Save PyTorch model to {os.path.abspath(__A	)}"""	)
    torch.save(model.state_dict()   , __A	)
    print(F"""Save configuration file to {os.path.abspath(__A	)}"""	)
    with open(__A   , """w"""   , encoding="""utf-8"""	) as f:
        f.write(config.to_json_string()	)


if __name__ == "__main__":
    lowerCAmelCase			:							Dict     =argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
    )
    parser.add_argument(
        "--xlnet_config_file",
        default=None,
        type=str,
        required=True,
        help=(
            "The config json file corresponding to the pre-trained XLNet model. \n"
            "This specifies the model architecture."
        ),
    )
    parser.add_argument(
        "--pytorch_dump_folder_path",
        default=None,
        type=str,
        required=True,
        help="Path to the folder to store the PyTorch model or dataset/vocab.",
    )
    parser.add_argument(
        "--finetuning_task",
        default=None,
        type=str,
        help="Name of a task on which the XLNet TensorFlow model was fine-tuned",
    )
    lowerCAmelCase			:							Union[str, Any]     =parser.parse_args()
    print(args)

    convert_xlnet_checkpoint_to_pytorch(
        args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task
    )




