
"""simple docstring"""
from __future__ import annotations

import unittest

from transformers import AutoTokenizer, GPTJConfig, is_tf_available
from transformers.testing_utils import require_tf, slow, tooslow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin
from ...utils.test_modeling_tf_core import TFCoreModelTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers.models.gptj.modeling_tf_gptj import (
        TFGPTJForCausalLM,
        TFGPTJForQuestionAnswering,
        TFGPTJForSequenceClassification,
        TFGPTJModel,
        shape_list,
    )






class   _UpperCAmelCase					:
    def __init__(     self    ,     snake_case_  ):
      _snake_case  :    Any            = parent
      _snake_case  :    Optional[Any]            = 13
      _snake_case  :    Union[str, Any]            = 7
      _snake_case  :    Dict            = True
      _snake_case  :    Tuple            = True
      _snake_case  :    Optional[Any]            = True
      _snake_case  :    int            = True
      _snake_case  :    Dict            = True
      _snake_case  :    Any            = 99
      _snake_case  :    str            = 32
      _snake_case  :    str            = 4
      _snake_case  :    Any            = 2
      _snake_case  :    Tuple            = 4
      _snake_case  :    Optional[int]            = 37
      _snake_case  :    Tuple            = "gelu"
      _snake_case  :    int            = 0.1
      _snake_case  :    Union[str, Any]            = 0.1
      _snake_case  :    Tuple            = 5_12
      _snake_case  :    Optional[Any]            = 16
      _snake_case  :    Optional[int]            = 2
      _snake_case  :    Any            = 0.02
      _snake_case  :    List[Any]            = 3
      _snake_case  :    Optional[int]            = 4
      _snake_case  :    Any            = None
      _snake_case  :    Union[str, Any]            = self.vocab_size - 1
      _snake_case  :    List[Any]            = self.vocab_size - 1
      _snake_case  :    Optional[Any]            = self.vocab_size - 1
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Optional[Any]            = ids_tensor([self.batch_size, self.seq_length]    ,     self.vocab_size  )

      _snake_case  :    Dict            = None
      if self.use_input_mask:
        _snake_case  :    List[str]            = random_attention_mask([self.batch_size, self.seq_length]  )

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

      _snake_case  :    str            = None
      if self.use_mc_token_ids:
        _snake_case  :    Tuple            = ids_tensor([self.batch_size, self.num_choices]    ,     self.seq_length  )

      _snake_case  :    List[str]            = None
      _snake_case  :    Optional[Any]            = None
      _snake_case  :    Tuple            = None
      if self.use_labels:
        _snake_case  :    Optional[Any]            = ids_tensor([self.batch_size]    ,     self.type_sequence_label_size  )
        _snake_case  :    Any            = ids_tensor([self.batch_size, self.seq_length]    ,     self.num_labels  )
        _snake_case  :    List[Any]            = ids_tensor([self.batch_size]    ,     self.num_choices  )

      _snake_case  :    Optional[Any]            = GPTJConfig(
          vocab_size=self.vocab_size    ,     n_embd=self.hidden_size    ,     n_layer=self.num_hidden_layers    ,     n_head=self.num_attention_heads    ,     intermediate_size=self.intermediate_size    ,     hidden_act=self.hidden_act    ,     hidden_dropout_prob=self.hidden_dropout_prob    ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,     n_positions=self.max_position_embeddings    ,     type_vocab_size=self.type_vocab_size    ,     initializer_range=self.initializer_range    ,     bos_token_id=self.bos_token_id    ,     eos_token_id=self.eos_token_id    ,     pad_token_id=self.pad_token_id    ,     rotary_dim=self.rotary_dim    ,     return_dict=snake_case_    ,     )

      _snake_case  :    Union[str, Any]            = ids_tensor([self.num_hidden_layers, self.num_attention_heads]    ,     2  )

      return (
          config,
          input_ids,
          input_mask,
          head_mask,
          token_type_ids,
          mc_token_ids,
          sequence_labels,
          token_labels,
          choice_labels,
      )
    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     *snake_case_  ):
      _snake_case  :    Optional[int]            = TFGPTJModel(config=snake_case_  )
      _snake_case  :    Tuple            = {
          "input_ids": input_ids,
          "attention_mask": input_mask,
          "token_type_ids": token_type_ids,
      }
      _snake_case  :    int            = model(snake_case_  )

      _snake_case  :    List[Any]            = [input_ids, None, input_mask]  # None is the input for 'past'
      _snake_case  :    List[Any]            = model(snake_case_  )

      _snake_case  :    Optional[Any]            = model(snake_case_  )

      self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, self.seq_length, self.hidden_size)  )
    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     *snake_case_  ):
      _snake_case  :    List[str]            = TFGPTJModel(config=snake_case_  )

      # first forward pass
      _snake_case  :    Tuple            = model(snake_case_    ,     token_type_ids=snake_case_    ,     use_cache=snake_case_  )
      _snake_case  :    Dict            = model(snake_case_    ,     token_type_ids=snake_case_  )
      _snake_case  :    Optional[int]            = model(snake_case_    ,     token_type_ids=snake_case_    ,     use_cache=snake_case_  )

      self.parent.assertTrue(len(snake_case_  ) == len(snake_case_  )  )
      self.parent.assertTrue(len(snake_case_  ) == len(snake_case_  ) + 1  )

      _snake_case      ,       _snake_case  :    Any            = outputs.to_tuple()

      # create hypothetical next token and extent to next_input_ids
      _snake_case  :    int            = ids_tensor((self.batch_size, 1)    ,     config.vocab_size  )
      _snake_case  :    Optional[int]            = ids_tensor([self.batch_size, 1]    ,     self.type_vocab_size  )

      # append to next input_ids and token_type_ids
      _snake_case  :    Dict            = tf.concat([input_ids, next_tokens]    ,     axis=-1  )
      _snake_case  :    Optional[Any]            = tf.concat([token_type_ids, next_token_types]    ,     axis=-1  )

      _snake_case  :    Union[str, Any]            = model(snake_case_    ,     token_type_ids=snake_case_  )["last_hidden_state"]
      _snake_case  :    Union[str, Any]            = model(snake_case_    ,     token_type_ids=snake_case_    ,     past_key_values=snake_case_  )[
          "last_hidden_state"
      ]

      # select random slice
      _snake_case  :    Dict            = int(ids_tensor((1,)    ,     shape_list(snake_case_  )[-1]  )  )
      _snake_case  :    Any            = output_from_no_past[:, -1, random_slice_idx]
      _snake_case  :    Dict            = output_from_past[:, 0, random_slice_idx]

      # test that outputs are equal for slice
      tf.debugging.assert_near(snake_case_    ,     snake_case_    ,     rtol=1E-6  )
    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     *snake_case_  ):
      _snake_case  :    Optional[int]            = TFGPTJModel(config=snake_case_  )

      # create attention mask
      _snake_case  :    Dict            = self.seq_length // 2
      _snake_case  :    Optional[int]            = tf.ones((self.batch_size, half_seq_length)    ,     dtype=tf.intaa  )
      _snake_case  :    Any            = tf.zeros((self.batch_size, self.seq_length - half_seq_length)    ,     dtype=tf.intaa  )
      _snake_case  :    Optional[Any]            = tf.concat([attn_mask_begin, attn_mask_end]    ,     axis=1  )

      # first forward pass
      _snake_case      ,       _snake_case  :    Tuple            = model(snake_case_    ,     attention_mask=snake_case_  ).to_tuple()

      # create hypothetical next token and extent to next_input_ids
      _snake_case  :    Dict            = ids_tensor((self.batch_size, 1)    ,     config.vocab_size  )

      # change a random masked slice from input_ids
      _snake_case  :    List[str]            = ids_tensor((1,)    ,     snake_case_  ).numpy() + 1
      _snake_case  :    Dict            = ids_tensor((self.batch_size, self.seq_length)    ,     config.vocab_size  )
      _snake_case  :    Union[str, Any]            = tf.range(self.seq_length  ) == (self.seq_length - random_seq_idx_to_change)
      _snake_case  :    List[str]            = tf.transpose(
          tf.broadcast_to(tf.expand_dims(snake_case_    ,     -1  )    ,     (self.seq_length, self.batch_size)  )  )
      _snake_case  :    List[Any]            = tf.where(snake_case_    ,     snake_case_    ,     snake_case_  )

      # append to next input_ids and attn_mask
      _snake_case  :    int            = tf.concat([input_ids, next_tokens]    ,     axis=-1  )
      _snake_case  :    str            = tf.concat([attn_mask, tf.ones((shape_list(snake_case_  )[0], 1)    ,     dtype=tf.intaa  )]    ,     axis=1  )

      # get two different outputs
      _snake_case  :    Tuple            = model(snake_case_    ,     attention_mask=snake_case_  )["last_hidden_state"]
      _snake_case  :    Union[str, Any]            = model(snake_case_    ,     past_key_values=snake_case_    ,     attention_mask=snake_case_  )[
          "last_hidden_state"
      ]

      # select random slice
      _snake_case  :    Optional[int]            = int(ids_tensor((1,)    ,     shape_list(snake_case_  )[-1]  )  )
      _snake_case  :    List[str]            = output_from_no_past[:, -1, random_slice_idx]
      _snake_case  :    int            = output_from_past[:, 0, random_slice_idx]

      # test that outputs are equal for slice
      tf.debugging.assert_near(snake_case_    ,     snake_case_    ,     rtol=1E-12  )
    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     *snake_case_  ):
      _snake_case  :    List[str]            = TFGPTJModel(config=snake_case_  )

      _snake_case  :    Dict            = input_ids[:1, :]
      _snake_case  :    List[Any]            = input_mask[:1, :]
      _snake_case  :    Tuple            = token_type_ids[:1, :]
      _snake_case  :    Union[str, Any]            = 1

      # first forward pass
      _snake_case  :    int            = model(snake_case_    ,     attention_mask=snake_case_    ,     token_type_ids=snake_case_    ,     use_cache=snake_case_  )

      _snake_case      ,       _snake_case  :    List[str]            = outputs.to_tuple()

      # create hypothetical next token and extent to next_input_ids
      _snake_case  :    int            = ids_tensor((self.batch_size, 3)    ,     config.vocab_size  )
      _snake_case  :    Optional[int]            = ids_tensor((self.batch_size, 3)    ,     2  )
      _snake_case  :    Dict            = ids_tensor((self.batch_size, 3)    ,     self.type_vocab_size  )

      # append to next input_ids and token_type_ids
      _snake_case  :    Optional[Any]            = tf.concat([input_ids, next_tokens]    ,     axis=-1  )
      _snake_case  :    Tuple            = tf.concat([input_mask, next_attn_mask]    ,     axis=-1  )
      _snake_case  :    Optional[Any]            = tf.concat([token_type_ids, next_token_types]    ,     axis=-1  )

      _snake_case  :    str            = model(
          snake_case_    ,     token_type_ids=snake_case_    ,     attention_mask=snake_case_  )["last_hidden_state"]
      _snake_case  :    Tuple            = model(
          snake_case_    ,     token_type_ids=snake_case_    ,     attention_mask=snake_case_    ,     past_key_values=snake_case_    ,     )["last_hidden_state"]
      self.parent.assertTrue(output_from_past.shape[1] == next_tokens.shape[1]  )

      # select random slice
      _snake_case  :    Any            = int(ids_tensor((1,)    ,     shape_list(snake_case_  )[-1]  )  )
      _snake_case  :    str            = output_from_no_past[:, -3:, random_slice_idx]
      _snake_case  :    List[Any]            = output_from_past[:, :, random_slice_idx]

      # test that outputs are equal for slice
      tf.debugging.assert_near(snake_case_    ,     snake_case_    ,     rtol=1E-3  )
    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     *snake_case_  ):
      _snake_case  :    Dict            = TFGPTJForCausalLM(config=snake_case_  )
      _snake_case  :    Optional[Any]            = {
          "input_ids": input_ids,
          "attention_mask": input_mask,
          "token_type_ids": token_type_ids,
      }
      _snake_case  :    List[Any]            = model(snake_case_  )
      self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.seq_length, self.vocab_size)  )


    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Tuple            = self.prepare_config_and_inputs()

      (
          (
          _snake_case
      )      ,       (
          _snake_case
      )      ,       (
          _snake_case
      )      ,       (
          _snake_case
      )      ,       (
          _snake_case
      )      ,       (
          _snake_case
      )      ,       (
          _snake_case
      )      ,       (
          _snake_case
      )      ,       (
          _snake_case
      )      ,       
      )  :    Tuple            = config_and_inputs

      _snake_case  :    Any            = {
          "input_ids": input_ids,
          "token_type_ids": token_type_ids,
          "attention_mask": input_mask,
      }
      return config, inputs_dict






@require_tf
class   _UpperCAmelCase					(       _snake_case		,       _snake_case		,       _snake_case		,       unittest.TestCase):
    __lowercase				:			Dict	=       (
        (TFGPTJForCausalLM, TFGPTJForSequenceClassification, TFGPTJForQuestionAnswering, TFGPTJModel)
        if is_tf_available()
        else ()
    )

    __lowercase				:			Optional[Any]	=       (TFGPTJForCausalLM,) if is_tf_available() else ()
    __lowercase				:			List[Any]	=       (
        {
            """feature-extraction""": TFGPTJModel,
            """question-answering""": TFGPTJForQuestionAnswering,
            """text-classification""": TFGPTJForSequenceClassification,
            """text-generation""": TFGPTJForCausalLM,
            """zero-shot""": TFGPTJForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    __lowercase				:			Any	=       False
    __lowercase				:			str	=       False
    __lowercase				:			Optional[int]	=       False
    __lowercase				:			Optional[Any]	=       False
    def        lowerCamelCase__						(     self    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_    ,     snake_case_  ):
      if (
          pipeline_test_casse_name == "QAPipelineTests"
          and tokenizer_name is not None
          and not tokenizer_name.endswith("Fast"  )
      ):
        # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
        # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
        # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
        return True

      return False
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    List[Any]            = TFGPTJModelTester(self  )
      _snake_case  :    List[str]            = ConfigTester(self    ,     config_class=snake_case_    ,     n_embd=37  )
    def        lowerCamelCase__						(     self  ):
      self.config_tester.run_common_tests()
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    List[str]            = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_gptj_model(*snake_case_  )
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Tuple            = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_gptj_model_past(*snake_case_  )
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Optional[Any]            = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_gptj_model_attention_mask_past(*snake_case_  )
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Tuple            = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_gptj_model_past_large_inputs(*snake_case_  )
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Dict            = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_gptj_lm_head_model(*snake_case_  )
    @slow
    @unittest.skipIf(
        not is_tf_available() or len(tf.config.list_physical_devices("GPU"  )  ) > 0    ,     "skip testing on GPU for now to avoid GPU OOM."    ,     )
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Tuple            = TFGPTJModel.from_pretrained("EleutherAI/gpt-j-6B"    ,     from_pt=snake_case_  )
      self.assertIsNotNone(snake_case_  )


    @unittest.skip(reason="Currently, model embeddings are going to undergo a major refactor."  )
    def        lowerCamelCase__						(     self  ):
      super().test_resize_token_embeddings()






@require_tf
@tooslow
# Marked as @tooslow due to GPU OOM -- but still useful to run locally. Requires ~39GB of RAM.
class   _UpperCAmelCase					(       unittest.TestCase):
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Optional[Any]            = TFGPTJForCausalLM.from_pretrained("EleutherAI/gpt-j-6B"    ,     from_pt=snake_case_  )
      _snake_case  :    int            = tf.convert_to_tensor([[4_64, 32_90]]    ,     dtype=tf.intaa  )  # The dog
      # fmt: off
      # The dog is a man's best friend. It is a loyal companion, and it is a friend
      _snake_case  :    List[Any]            = [4_64, 32_90, 3_18, 2_57, 5_82, 3_38, 12_66, 15_45, 13, 6_32, 3_18, 2_57, 91_12, 1_51_85, 11, 2_90, 3_40, 3_18, 2_57, 15_45]
      # fmt: on
      _snake_case  :    Optional[Any]            = model.generate(snake_case_    ,     do_sample=snake_case_  )
      self.assertListEqual(output_ids[0].numpy().tolist()    ,     snake_case_  )
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    Dict            = AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B"    ,     revision="float16"  )
      _snake_case  :    Optional[Any]            = TFGPTJForCausalLM.from_pretrained("EleutherAI/gpt-j-6B"    ,     revision="float16"    ,     from_pt=snake_case_  )

      _snake_case  :    Tuple            = tokenizer("Today is a nice day and"    ,     return_tensors="tf"  )
      # forces the generation to happen on CPU, to avoid GPU-related quirks
      with tf.device(":/CPU:0"  ):
        _snake_case  :    Any            = model.generate(**snake_case_    ,     do_sample=snake_case_    ,     seed=[42, 0]  )
      _snake_case  :    List[Any]            = tokenizer.decode(output_ids[0]    ,     skip_special_tokens=snake_case_  )

      _snake_case  :    List[str]            = "Today is a nice day and I’m going to go for a walk. I’"
      self.assertEqual(snake_case_    ,     snake_case_  )
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    int            = TFGPTJForCausalLM.from_pretrained("EleutherAI/gpt-j-6B"    ,     revision="float16"    ,     from_pt=snake_case_  )
      _snake_case  :    Union[str, Any]            = AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B"    ,     revision="float16"  )

      _snake_case  :    str            = "left"

      # Define PAD Token = EOS Token = 50256
      _snake_case  :    Tuple            = tokenizer.eos_token
      _snake_case  :    Any            = model.config.eos_token_id

      # use different length sentences to test batching
      _snake_case  :    Optional[int]            = [
          "Hello, my dog is a little",
          "Today, I",
      ]
      _snake_case  :    List[Any]            = [
          "Hello, my dog is a little over a year old and has been diagnosed with hip dysplasia",
          "Today, I’m going to be talking about a topic that’",
      ]
      return model, tokenizer, sentences, expected_output_sentences
    def        lowerCamelCase__						(     self  ):
      # Confirms that we get the expected results with left-padded beam search
      _snake_case      ,       _snake_case      ,       _snake_case      ,       _snake_case  :    Optional[int]            = self._get_beam_search_test_objects()

      _snake_case  :    Tuple            = tokenizer(snake_case_    ,     return_tensors="tf"    ,     padding=snake_case_  )
      _snake_case  :    int            = model.generate(**snake_case_    ,     do_sample=snake_case_    ,     num_beams=2  )
      _snake_case  :    List[str]            = tokenizer.batch_decode(snake_case_    ,     skip_special_tokens=snake_case_  )
      self.assertListEqual(snake_case_    ,     snake_case_  )
    def        lowerCamelCase__						(     self  ):
      # Confirms that left-padding is working properly
      _snake_case      ,       _snake_case      ,       _snake_case      ,       _snake_case  :    Any            = self._get_beam_search_test_objects()

      _snake_case  :    Dict            = tokenizer(snake_case_    ,     return_tensors="tf"    ,     padding=snake_case_  )
      _snake_case  :    int            = tokenizer(sentences[0]    ,     return_tensors="tf"  )
      _snake_case  :    List[Any]            = model.generate(**snake_case_    ,     do_sample=snake_case_    ,     num_beams=2  )
      _snake_case  :    Tuple            = (
          shape_list(inputs_non_padded["input_ids"]  )[-1]
          - tf.reduce_sum(tf.cast(inputs["attention_mask"][-1]    ,     tf.intaa  )  ).numpy()
      )
      _snake_case  :    Tuple            = tokenizer(sentences[1]    ,     return_tensors="tf"  )
      _snake_case  :    List[str]            = model.generate(
          **snake_case_    ,     do_sample=snake_case_    ,     num_beams=2    ,     max_length=model.config.max_length - num_paddings  )
      _snake_case  :    Tuple            = tokenizer.decode(output_non_padded[0]    ,     skip_special_tokens=snake_case_  )
      _snake_case  :    List[str]            = tokenizer.decode(output_padded[0]    ,     skip_special_tokens=snake_case_  )
      self.assertListEqual(snake_case_    ,     [non_padded_sentence, padded_sentence]  )


    def        lowerCamelCase__						(     self  ):
      # Confirms that XLA is working properly
      _snake_case      ,       _snake_case      ,       _snake_case      ,       _snake_case  :    Optional[int]            = self._get_beam_search_test_objects()

      _snake_case  :    Union[str, Any]            = tokenizer(snake_case_    ,     return_tensors="tf"    ,     padding=snake_case_  )
      _snake_case  :    Union[str, Any]            = tf.function(model.generate    ,     jit_compile=snake_case_  )
      _snake_case  :    int            = xla_generate(**snake_case_    ,     do_sample=snake_case_    ,     num_beams=2  )
      _snake_case  :    Optional[Any]            = tokenizer.batch_decode(snake_case_    ,     skip_special_tokens=snake_case_  )
      self.assertListEqual(snake_case_    ,     snake_case_  )



"""simple docstring"""
import unittest

import numpy as np
from datasets import load_dataset

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 SegformerImageProcessor






class   _UpperCAmelCase					(       unittest.TestCase):
    def __init__(     self    ,     snake_case_    ,     snake_case_=7    ,     snake_case_=3    ,     snake_case_=30    ,     snake_case_=4_00    ,     snake_case_=True    ,     snake_case_=None    ,     snake_case_=True    ,     snake_case_=[0.5, 0.5, 0.5]    ,     snake_case_=[0.5, 0.5, 0.5]    ,     snake_case_=False    ,     ):
      _snake_case  :    List[str]            = size if size is not None else {"height": 30, "width": 30}
      _snake_case  :    Optional[int]            = parent
      _snake_case  :    Dict            = batch_size
      _snake_case  :    Any            = num_channels
      _snake_case  :    Optional[Any]            = min_resolution
      _snake_case  :    int            = max_resolution
      _snake_case  :    int            = do_resize
      _snake_case  :    List[Any]            = size
      _snake_case  :    int            = do_normalize
      _snake_case  :    Any            = image_mean
      _snake_case  :    str            = image_std
      _snake_case  :    List[Any]            = do_reduce_labels


    def        lowerCamelCase__						(     self  ):
      return {
          "do_resize": self.do_resize,
          "size": self.size,
          "do_normalize": self.do_normalize,
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_reduce_labels": self.do_reduce_labels,
      }


def 					a__ ( ):

  """simple docstring"""







  _snake_case  :    List[str]            = load_dataset("hf-internal-testing/fixtures_ade20k"							,  split="test"		)

  _snake_case  :    Any            = Image.open(dataset[0]["file"]		)
  _snake_case  :    Optional[int]            = Image.open(dataset[1]["file"]		)

  return image, map
def 					a__ ( ):

  """simple docstring"""







  _snake_case  :    Optional[int]            = load_dataset("hf-internal-testing/fixtures_ade20k"							,  split="test"		)

  _snake_case  :    Optional[int]            = Image.open(dataset[0]["file"]		)
  _snake_case  :    List[str]            = Image.open(dataset[1]["file"]		)
  _snake_case  :    Dict            = Image.open(dataset[2]["file"]		)
  _snake_case  :    List[str]            = Image.open(dataset[3]["file"]		)

  return [imagea, imagea], [mapa, mapa]






@require_torch
@require_vision
class   _UpperCAmelCase					(       _snake_case		,       unittest.TestCase):
    __lowercase				:			Dict	=       SegformerImageProcessor if is_vision_available() else None
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    List[str]            = SegformerImageProcessingTester(self  )
    @property
    def        lowerCamelCase__						(     self  ):
      return self.image_processor_tester.prepare_image_processor_dict()
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    List[Any]            = self.image_processing_class(**self.image_processor_dict  )
      self.assertTrue(hasattr(snake_case_    ,     "do_resize"  )  )
      self.assertTrue(hasattr(snake_case_    ,     "size"  )  )
      self.assertTrue(hasattr(snake_case_    ,     "do_normalize"  )  )
      self.assertTrue(hasattr(snake_case_    ,     "image_mean"  )  )
      self.assertTrue(hasattr(snake_case_    ,     "image_std"  )  )
      self.assertTrue(hasattr(snake_case_    ,     "do_reduce_labels"  )  )
    def        lowerCamelCase__						(     self  ):
      _snake_case  :    List[str]            = self.image_processing_class.from_dict(self.image_processor_dict  )
      self.assertEqual(image_processor.size    ,     {"height": 30, "width": 30}  )
      self.assertEqual(image_processor.do_reduce_labels    ,     snake_case_  )

      _snake_case  :    int            = self.image_processing_class.from_dict(self.image_processor_dict    ,     size=42    ,     reduce_labels=snake_case_  )
      self.assertEqual(image_processor.size    ,     {"height": 42, "width": 42}  )
      self.assertEqual(image_processor.do_reduce_labels    ,     snake_case_  )
    def        lowerCamelCase__						(     self  ):
      pass
    def        lowerCamelCase__						(     self  ):
      # Initialize image_processing
      _snake_case  :    List[Any]            = self.image_processing_class(**self.image_processor_dict  )
      # create random PIL images
      _snake_case  :    List[str]            = prepare_image_inputs(self.image_processor_tester    ,     equal_resolution=snake_case_  )
      for image in image_inputs:
        self.assertIsInstance(snake_case_    ,     Image.Image  )

      # Test not batched input
      _snake_case  :    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
      _snake_case  :    List[Any]            = image_processing(snake_case_    ,     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        lowerCamelCase__						(     self  ):
      # Initialize image_processing
      _snake_case  :    Optional[Any]            = self.image_processing_class(**self.image_processor_dict  )
      # create random numpy tensors
      _snake_case  :    Any            = prepare_image_inputs(self.image_processor_tester    ,     equal_resolution=snake_case_    ,     numpify=snake_case_  )
      for image in image_inputs:
        self.assertIsInstance(snake_case_    ,     np.ndarray  )

      # Test not batched input
      _snake_case  :    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
      _snake_case  :    Optional[int]            = image_processing(snake_case_    ,     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        lowerCamelCase__						(     self  ):
      # Initialize image_processing
      _snake_case  :    Optional[int]            = self.image_processing_class(**self.image_processor_dict  )
      # create random PyTorch tensors
      _snake_case  :    Optional[Any]            = prepare_image_inputs(self.image_processor_tester    ,     equal_resolution=snake_case_    ,     torchify=snake_case_  )
      for image in image_inputs:
        self.assertIsInstance(snake_case_    ,     torch.Tensor  )

      # Test not batched input
      _snake_case  :    Dict            = 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
      _snake_case  :    List[Any]            = image_processing(snake_case_    ,     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        lowerCamelCase__						(     self  ):
      # Initialize image_processing
      _snake_case  :    int            = self.image_processing_class(**self.image_processor_dict  )
      # create random PyTorch tensors
      _snake_case  :    List[str]            = prepare_image_inputs(self.image_processor_tester    ,     equal_resolution=snake_case_    ,     torchify=snake_case_  )
      _snake_case  :    Optional[int]            = []
      for image in image_inputs:
        self.assertIsInstance(snake_case_    ,     torch.Tensor  )
        maps.append(torch.zeros(image.shape[-2:]  ).long()  )

      # Test not batched input
      _snake_case  :    Optional[Any]            = image_processing(image_inputs[0]    ,     maps[0]    ,     return_tensors="pt"  )
      self.assertEqual(
          encoding["pixel_values"].shape    ,     (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["height"],
              self.image_processor_tester.size["width"],
          )    ,     )
      self.assertEqual(
          encoding["labels"].shape    ,     (
              1,
              self.image_processor_tester.size["height"],
              self.image_processor_tester.size["width"],
          )    ,     )
      self.assertEqual(encoding["labels"].dtype    ,     torch.long  )
      self.assertTrue(encoding["labels"].min().item() >= 0  )
      self.assertTrue(encoding["labels"].max().item() <= 2_55  )

      # Test batched
      _snake_case  :    List[Any]            = image_processing(snake_case_    ,     snake_case_    ,     return_tensors="pt"  )
      self.assertEqual(
          encoding["pixel_values"].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"],
          )    ,     )
      self.assertEqual(
          encoding["labels"].shape    ,     (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.size["height"],
              self.image_processor_tester.size["width"],
          )    ,     )
      self.assertEqual(encoding["labels"].dtype    ,     torch.long  )
      self.assertTrue(encoding["labels"].min().item() >= 0  )
      self.assertTrue(encoding["labels"].max().item() <= 2_55  )

      # Test not batched input (PIL images)
      _snake_case      ,       _snake_case  :    int            = prepare_semantic_single_inputs()

      _snake_case  :    List[str]            = image_processing(snake_case_    ,     snake_case_    ,     return_tensors="pt"  )
      self.assertEqual(
          encoding["pixel_values"].shape    ,     (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["height"],
              self.image_processor_tester.size["width"],
          )    ,     )
      self.assertEqual(
          encoding["labels"].shape    ,     (
              1,
              self.image_processor_tester.size["height"],
              self.image_processor_tester.size["width"],
          )    ,     )
      self.assertEqual(encoding["labels"].dtype    ,     torch.long  )
      self.assertTrue(encoding["labels"].min().item() >= 0  )
      self.assertTrue(encoding["labels"].max().item() <= 2_55  )

      # Test batched input (PIL images)
      _snake_case      ,       _snake_case  :    str            = prepare_semantic_batch_inputs()

      _snake_case  :    List[Any]            = image_processing(snake_case_    ,     snake_case_    ,     return_tensors="pt"  )
      self.assertEqual(
          encoding["pixel_values"].shape    ,     (
              2,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size["height"],
              self.image_processor_tester.size["width"],
          )    ,     )
      self.assertEqual(
          encoding["labels"].shape    ,     (
              2,
              self.image_processor_tester.size["height"],
              self.image_processor_tester.size["width"],
          )    ,     )
      self.assertEqual(encoding["labels"].dtype    ,     torch.long  )
      self.assertTrue(encoding["labels"].min().item() >= 0  )
      self.assertTrue(encoding["labels"].max().item() <= 2_55  )


    def        lowerCamelCase__						(     self  ):
      # Initialize image_processing
      _snake_case  :    List[Any]            = self.image_processing_class(**self.image_processor_dict  )

      # ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
      _snake_case      ,       _snake_case  :    List[Any]            = prepare_semantic_single_inputs()
      _snake_case  :    Dict            = image_processing(snake_case_    ,     snake_case_    ,     return_tensors="pt"  )
      self.assertTrue(encoding["labels"].min().item() >= 0  )
      self.assertTrue(encoding["labels"].max().item() <= 1_50  )

      _snake_case  :    Tuple            = True
      _snake_case  :    Union[str, Any]            = image_processing(snake_case_    ,     snake_case_    ,     return_tensors="pt"  )
      self.assertTrue(encoding["labels"].min().item() >= 0  )
      self.assertTrue(encoding["labels"].max().item() <= 2_55  )

