
"""simple docstring"""
import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
      from PIL import Image

      from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor
@require_tokenizers
@require_vision
class 			UpperCamelCase      (							unittest.TestCase  ):
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				int							    = tempfile.mkdtemp()

       # fmt: off
       _SCREAMING_SNAKE_CASE   :				Dict							    = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest"]
       # fmt: on
       _SCREAMING_SNAKE_CASE   :				str							    = 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]				)				)

       _SCREAMING_SNAKE_CASE   :				List[Any]							    = {
           "do_resize": True,
           "size": {"height": 18, "width": 18},
           "do_normalize": True,
           "image_mean": [0.5, 0.5, 0.5],
           "image_std": [0.5, 0.5, 0.5],
       }
       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = os.path.join(self.tmpdirname  ,     snake_case__				)
       with open(self.image_processor_file  ,     "w"  ,     encoding="utf-8"				) as fp:
         json.dump(snake_case__  ,     snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     **snake_case__				):




       """simple docstring"""


       return BertTokenizer.from_pretrained(self.tmpdirname  ,     **snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     **snake_case__				):




       """simple docstring"""


       return ViTImageProcessor.from_pretrained(self.tmpdirname  ,     **snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       shutil.rmtree(self.tmpdirname				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				int							    = [np.random.randint(255  ,     size=(3, 30, 400)  ,     dtype=np.uinta				)]

       _SCREAMING_SNAKE_CASE   :				Any							    = [Image.fromarray(np.moveaxis(snake_case__  ,     0  ,     -1				)				) for x in image_inputs]

       return image_inputs
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				int							    = self.get_tokenizer()
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = self.get_image_processor()

       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = VisionTextDualEncoderProcessor(tokenizer=snake_case__  ,     image_processor=snake_case__				)

       processor.save_pretrained(self.tmpdirname				)
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname				)

       self.assertEqual(processor.tokenizer.get_vocab()  ,     tokenizer.get_vocab()				)
       self.assertIsInstance(processor.tokenizer  ,     (BertTokenizer, BertTokenizerFast)				)

       self.assertEqual(processor.image_processor.to_json_string()  ,     image_processor.to_json_string()				)
       self.assertIsInstance(processor.image_processor  ,     snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = VisionTextDualEncoderProcessor(
           tokenizer=self.get_tokenizer()  ,     image_processor=self.get_image_processor()				)
       processor.save_pretrained(self.tmpdirname				)

       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = self.get_tokenizer(bos_token="(BOS)"  ,     eos_token="(EOS)"				)
       _SCREAMING_SNAKE_CASE   :				Tuple							    = self.get_image_processor(do_normalize=snake_case__  ,     padding_value=1.0				)

       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = VisionTextDualEncoderProcessor.from_pretrained(
           self.tmpdirname  ,     bos_token="(BOS)"  ,     eos_token="(EOS)"  ,     do_normalize=snake_case__  ,     padding_value=1.0				)

       self.assertEqual(processor.tokenizer.get_vocab()  ,     tokenizer_add_kwargs.get_vocab()				)
       self.assertIsInstance(processor.tokenizer  ,     (BertTokenizer, BertTokenizerFast)				)

       self.assertEqual(processor.image_processor.to_json_string()  ,     image_processor_add_kwargs.to_json_string()				)
       self.assertIsInstance(processor.image_processor  ,     snake_case__				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[Any]							    = self.get_image_processor()
       _SCREAMING_SNAKE_CASE   :				str							    = self.get_tokenizer()

       _SCREAMING_SNAKE_CASE   :				Any							    = VisionTextDualEncoderProcessor(tokenizer=snake_case__  ,     image_processor=snake_case__				)

       _SCREAMING_SNAKE_CASE   :				str							    = self.prepare_image_inputs()

       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = image_processor(snake_case__  ,     return_tensors="np"				)
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = processor(images=snake_case__  ,     return_tensors="np"				)

       for key in input_feat_extract.keys():
         self.assertAlmostEqual(input_feat_extract[key].sum()  ,     input_processor[key].sum()  ,     delta=1E-2				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[str]							    = self.get_image_processor()
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = self.get_tokenizer()

       _SCREAMING_SNAKE_CASE   :				Tuple							    = VisionTextDualEncoderProcessor(tokenizer=snake_case__  ,     image_processor=snake_case__				)

       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = "lower newer"

       _SCREAMING_SNAKE_CASE   :				str							    = processor(text=snake_case__				)

       _SCREAMING_SNAKE_CASE   :				Any							    = tokenizer(snake_case__				)

       for key in encoded_tok.keys():
         self.assertListEqual(encoded_tok[key]  ,     encoded_processor[key]				)
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Any							    = self.get_image_processor()
       _SCREAMING_SNAKE_CASE   :				List[str]							    = self.get_tokenizer()

       _SCREAMING_SNAKE_CASE   :				int							    = VisionTextDualEncoderProcessor(tokenizer=snake_case__  ,     image_processor=snake_case__				)

       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = "lower newer"
       _SCREAMING_SNAKE_CASE   :				List[Any]							    = self.prepare_image_inputs()

       _SCREAMING_SNAKE_CASE   :				List[Any]							    = processor(text=snake_case__  ,     images=snake_case__				)

       self.assertListEqual(list(inputs.keys()				)  ,     ["input_ids", "token_type_ids", "attention_mask", "pixel_values"]				)

       # test if it raises when no input is passed
       with self.assertRaises(snake_case__				):
         processor()
     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				List[Any]							    = self.get_image_processor()
       _SCREAMING_SNAKE_CASE   :				int							    = self.get_tokenizer()

       _SCREAMING_SNAKE_CASE   :				List[str]							    = VisionTextDualEncoderProcessor(tokenizer=snake_case__  ,     image_processor=snake_case__				)

       _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       _SCREAMING_SNAKE_CASE   :				int							    = processor.batch_decode(snake_case__				)
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = tokenizer.batch_decode(snake_case__				)

       self.assertListEqual(snake_case__  ,     snake_case__				)




     def 				__SCREAMING_SNAKE_CASE							(     self				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Tuple							    = self.get_image_processor()
       _SCREAMING_SNAKE_CASE   :				Optional[int]							    = self.get_tokenizer()

       _SCREAMING_SNAKE_CASE   :				List[str]							    = VisionTextDualEncoderProcessor(tokenizer=snake_case__  ,     image_processor=snake_case__				)

       _SCREAMING_SNAKE_CASE   :				Union[str, Any]							    = "lower newer"
       _SCREAMING_SNAKE_CASE   :				Tuple							    = self.prepare_image_inputs()

       _SCREAMING_SNAKE_CASE   :				List[Any]							    = processor(text=snake_case__  ,     images=snake_case__				)

       self.assertListEqual(list(inputs.keys()				)  ,     processor.model_input_names				)





"""simple docstring"""
# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


lowercase_       :						Tuple     					=	[
    '''EAGER''',
    '''AOT_EAGER''',
    '''INDUCTOR''',
    '''NVFUSER''',
    '''AOT_NVFUSER''',
    '''AOT_CUDAGRAPHS''',
    '''OFI''',
    '''FX2TRT''',
    '''ONNXRT''',
    '''IPEX''',
]





def    _lowerCAmelCase    (     lowerCamelCase__							:							Dict,						lowerCamelCase__							:							int=None,						lowerCamelCase__							:							Any=None,						lowerCamelCase__							:							Any=None				)  ->    Tuple:
  _SCREAMING_SNAKE_CASE   :				List[Any]							    = True
  while ask_again:
    _SCREAMING_SNAKE_CASE   :				List[str]							    = input(lowerCamelCase__				)
    try:
      if default is not None and len(lowerCamelCase__				) == 0:
        return default
      return convert_value(lowerCamelCase__				) if convert_value is not None else result
    except Exception:
      if error_message is not None:
        print(lowerCamelCase__				)





def    _lowerCAmelCase    (     lowerCamelCase__							:							Any,						lowerCamelCase__							:							Dict=[],						lowerCamelCase__							:							Optional[int]=None,						lowerCamelCase__							:							str=0				)  ->    str:
  _SCREAMING_SNAKE_CASE   :				int							    = BulletMenu(lowerCamelCase__,						lowerCamelCase__				)
  _SCREAMING_SNAKE_CASE   :				str							    = menu.run(default_choice=lowerCamelCase__				)
  return convert_value(lowerCamelCase__				) if convert_value is not None else result





def    _lowerCAmelCase    (     lowerCamelCase__							:							Optional[int]				)  ->    Optional[int]:
  _SCREAMING_SNAKE_CASE   :				str							    = int(lowerCamelCase__				)
  return ComputeEnvironment(["LOCAL_MACHINE", "AMAZON_SAGEMAKER"][value]				)





def    _lowerCAmelCase    (     lowerCamelCase__							:							str				)  ->    List[Any]:
  _SCREAMING_SNAKE_CASE   :				str							    = int(lowerCamelCase__				)
  return DistributedType(["NO", "MULTI_CPU", "MULTI_XPU", "MULTI_GPU", "MULTI_NPU", "TPU"][value]				)





def    _lowerCAmelCase    (     lowerCamelCase__							:							Optional[Any]				)  ->    List[Any]:
  _SCREAMING_SNAKE_CASE   :				List[Any]							    = int(lowerCamelCase__				)
  return DynamoBackend(DYNAMO_BACKENDS[value]				).value





def    _lowerCAmelCase    (     lowerCamelCase__							:							int				)  ->    Dict:
  _SCREAMING_SNAKE_CASE   :				int							    = int(lowerCamelCase__				)
  return PrecisionType(["no", "fp16", "bf16", "fp8"][value]				)





def    _lowerCAmelCase    (     lowerCamelCase__							:							List[Any]				)  ->    Any:
  _SCREAMING_SNAKE_CASE   :				Optional[Any]							    = int(lowerCamelCase__				)
  return SageMakerDistributedType(["NO", "DATA_PARALLEL", "MODEL_PARALLEL"][value]				)





def    _lowerCAmelCase    (     lowerCamelCase__							:							List[Any]				)  ->    Optional[Any]:
  return {"yes": True, "no": False}[value.lower()]





class 			UpperCamelCase      (							argparse.RawDescriptionHelpFormatter  ):
     def 				__SCREAMING_SNAKE_CASE							(     self  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				):




       """simple docstring"""


       _SCREAMING_SNAKE_CASE   :				Any							    = super()._format_usage(snake_case__  ,     snake_case__  ,     snake_case__  ,     snake_case__				)
       _SCREAMING_SNAKE_CASE   :				Any							    = usage.replace("<command> [<args>] "  ,     ""				)
       return usage



