'''simple docstring'''




import unittest

import numpy as np

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

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 PoolFormerImageProcessor


class       UpperCAmelCase__  (       unittest.TestCase			):






   """simple docstring"""
   def __init__(self       ,				_a       ,				_a=7       ,				_a=3       ,				_a=30       ,				_a=400       ,				_a=True       ,				_a=None       ,				_a=0.9       ,				_a=None       ,				_a=True       ,				_a=[0.5, 0.5, 0.5]       ,				_a=[0.5, 0.5, 0.5]       ,				)    ->     Optional[Any]:
        lowercase_					:       str     				=					size if size is not None else {"""shortest_edge""": 30}
        lowercase_					:       List[Any]     				=					crop_size if crop_size is not None else {"""height""": 30, """width""": 30}
        lowercase_					:       Tuple     				=					parent
        lowercase_					:       str     				=					batch_size
        lowercase_					:       Any     				=					num_channels
        lowercase_					:       List[str]     				=					min_resolution
        lowercase_					:       int     				=					max_resolution
        lowercase_					:       str     				=					do_resize_and_center_crop
        lowercase_					:       List[str]     				=					size
        lowercase_					:       List[Any]     				=					crop_pct
        lowercase_					:       Any     				=					crop_size
        lowercase_					:       Dict     				=					do_normalize
        lowercase_					:       Union[str, Any]     				=					image_mean
        lowercase_					:       Optional[Any]     				=					image_std





   def 		_lowerCamelCase (self				)    ->     List[str]:
        return {
            "size": self.size,
            "do_resize_and_center_crop": self.do_resize_and_center_crop,
            "crop_pct": self.crop_pct,
            "crop_size": self.crop_size,
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
        }



@require_torch
@require_vision
class       UpperCAmelCase__  (       _UpperCAmelCase					,     unittest.TestCase			):






   """simple docstring"""
   A							:       List[Any]     =    PoolFormerImageProcessor if is_vision_available() else None
   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        lowercase_					:       Union[str, Any]     				=					PoolFormerImageProcessingTester(self				)
   @property
   def 		_lowerCamelCase (self				)    ->     int:
        return self.image_processor_tester.prepare_image_processor_dict()
   def 		_lowerCamelCase (self				)    ->     int:
        lowercase_					:       int     				=					self.image_processing_class(**self.image_processor_dict				)
        self.assertTrue(hasattr(lowercase_       ,				'do_resize_and_center_crop'				)				)
        self.assertTrue(hasattr(lowercase_       ,				'size'				)				)
        self.assertTrue(hasattr(lowercase_       ,				'crop_pct'				)				)
        self.assertTrue(hasattr(lowercase_       ,				'do_normalize'				)				)
        self.assertTrue(hasattr(lowercase_       ,				'image_mean'				)				)
        self.assertTrue(hasattr(lowercase_       ,				'image_std'				)				)
   def 		_lowerCamelCase (self				)    ->     Any:
        lowercase_					:       List[Any]     				=					self.image_processing_class.from_dict(self.image_processor_dict				)
        self.assertEqual(image_processor.size       ,				{'shortest_edge': 30}				)
        self.assertEqual(image_processor.crop_size       ,				{'height': 30, 'width': 30}				)

        lowercase_					:       Optional[int]     				=					self.image_processing_class.from_dict(self.image_processor_dict       ,				size=42       ,				crop_size=84				)
        self.assertEqual(image_processor.size       ,				{'shortest_edge': 42}				)
        self.assertEqual(image_processor.crop_size       ,				{'height': 84, 'width': 84}				)
   def 		_lowerCamelCase (self				)    ->     str:
        pass
   def 		_lowerCamelCase (self				)    ->     Any:
        # Initialize image_processing
        lowercase_					:       int     				=					self.image_processing_class(**self.image_processor_dict				)
        # create random PIL images
        lowercase_					:       Union[str, Any]     				=					prepare_image_inputs(self.image_processor_tester       ,				equal_resolution=lowercase_				)
        for image in image_inputs:
             self.assertIsInstance(lowercase_       ,				Image.Image				)

        # Test not batched input
        lowercase_					:       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.crop_size['height'],
                self.image_processor_tester.crop_size['width'],
            )       ,				)

        # Test batched
        lowercase_					:       Union[str, Any]     				=					image_processing(lowercase_       ,				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.crop_size['height'],
                self.image_processor_tester.crop_size['width'],
            )       ,				)
   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        # Initialize image_processing
        lowercase_					:       Union[str, Any]     				=					self.image_processing_class(**self.image_processor_dict				)
        # create random numpy tensors
        lowercase_					:       Any     				=					prepare_image_inputs(self.image_processor_tester       ,				equal_resolution=lowercase_       ,				numpify=lowercase_				)
        for image in image_inputs:
             self.assertIsInstance(lowercase_       ,				np.ndarray				)

        # Test not batched input
        lowercase_					:       str     				=					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.crop_size['height'],
                self.image_processor_tester.crop_size['width'],
            )       ,				)

        # Test batched
        lowercase_					:       Union[str, Any]     				=					image_processing(lowercase_       ,				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.crop_size['height'],
                self.image_processor_tester.crop_size['width'],
            )       ,				)





   def 		_lowerCamelCase (self				)    ->     List[Any]:
        # Initialize image_processing
        lowercase_					:       Any     				=					self.image_processing_class(**self.image_processor_dict				)
        # create random PyTorch tensors
        lowercase_					:       int     				=					prepare_image_inputs(self.image_processor_tester       ,				equal_resolution=lowercase_       ,				torchify=lowercase_				)
        for image in image_inputs:
             self.assertIsInstance(lowercase_       ,				torch.Tensor				)

        # Test not batched input
        lowercase_					:       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.crop_size['height'],
                self.image_processor_tester.crop_size['width'],
            )       ,				)

        # Test batched
        lowercase_					:       Dict     				=					image_processing(lowercase_       ,				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.crop_size['height'],
                self.image_processor_tester.crop_size['width'],
            )       ,				)





'''simple docstring'''




import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin


class       UpperCAmelCase__  (       _snake_case					,     unittest.TestCase			):






   """simple docstring"""
   A							:       str     =    TransfoXLTokenizer
   A							:       Optional[int]     =    False
   A							:       List[str]     =    False
   def 		_lowerCamelCase (self				)    ->     List[Any]:
        super().setUp()

        lowercase_					:       Any     				=					[
            '<unk>',
            '[CLS]',
            '[SEP]',
            'want',
            'unwanted',
            'wa',
            'un',
            'running',
            ',',
            'low',
            'l',
        ]
        lowercase_					:       Any     				=					os.path.join(self.tmpdirname       ,				VOCAB_FILES_NAMES['vocab_file']				)
        with open(self.vocab_file       ,				'w'       ,				encoding='utf-8'				) as vocab_writer:
             vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]				)				)
   def 		_lowerCamelCase (self       ,				**_a				)    ->     Union[str, Any]:
        lowercase_					:       str     				=					True
        return TransfoXLTokenizer.from_pretrained(self.tmpdirname       ,				**_a				)
   def 		_lowerCamelCase (self       ,				_a				)    ->     Union[str, Any]:
        lowercase_					:       int     				=					'<unk> UNwanted , running'
        lowercase_					:       int     				=					'<unk> unwanted, running'
        return input_text, output_text
   def 		_lowerCamelCase (self				)    ->     Tuple:
        lowercase_					:       Any     				=					TransfoXLTokenizer(vocab_file=self.vocab_file       ,				lower_case=_a				)

        lowercase_					:       Optional[Any]     				=					tokenizer.tokenize('<unk> UNwanted , running'				)
        self.assertListEqual(_a       ,				['<unk>', 'unwanted', ',', 'running']				)

        self.assertListEqual(tokenizer.convert_tokens_to_ids(_a				)       ,				[0, 4, 8, 7]				)
   def 		_lowerCamelCase (self				)    ->     Union[str, Any]:
        lowercase_					:       List[str]     				=					TransfoXLTokenizer(lower_case=_a				)

        self.assertListEqual(
            tokenizer.tokenize(' \tHeLLo ! how  \n Are yoU ?  '				)       ,				['hello', '!', 'how', 'are', 'you', '?']				)
   def 		_lowerCamelCase (self				)    ->     int:
        lowercase_					:       Dict     				=					TransfoXLTokenizer(lower_case=_a				)

        self.assertListEqual(
            tokenizer.tokenize(' \tHeLLo ! how  \n Are yoU ?  '				)       ,				['HeLLo', '!', 'how', 'Are', 'yoU', '?']				)
   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        lowercase_					:       Dict     				=					TransfoXLTokenizer(lower_case=_a				)
        lowercase_					:       int     				=					'Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?'
        lowercase_					:       List[str]     				=					[
            'Hello',
            '(',
            'bracket',
            ')',
            'and',
            'side',
            '@-@',
            'scrolled',
            '[',
            'and',
            ']',
            'Henry',
            '\'s',
            '$',
            '5',
            '@,@',
            '000',
            'with',
            '3',
            '@.@',
            '34',
            'm',
            '.',
            'What',
            '\'s',
            'up',
            '!',
            '?',
        ]

        self.assertListEqual(tokenizer.tokenize(_a				)       ,				_a				)

        self.assertEqual(tokenizer.convert_tokens_to_string(_a				)       ,				_a				)





   def 		_lowerCamelCase (self				)    ->     Any:
        lowercase_					:       Optional[int]     				=					self.get_tokenizer()
        lowercase_					:       str     				=					len(_a				)

        tokenizer.add_tokens(['new1', 'new2']				)
        tokenizer.move_added_token('new1'       ,				1				)

        # Check that moved token is not copied (duplicate)
        self.assertEqual(len(_a				)       ,				original_len + 2				)
        # Check that token is moved to specified id
        self.assertEqual(tokenizer.encode('new1'				)       ,				[1]				)
        self.assertEqual(tokenizer.decode([1]				)       ,				'new1'				)




