

'''simple docstring'''







import copy
import os
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
import pyarrow as pa
import pyarrow.parquet as pq
import pytest

from datasets.arrow_writer import ArrowWriter, OptimizedTypedSequence, ParquetWriter, TypedSequence
from datasets.features import ArrayaD, ClassLabel, Features, Image, Value
from datasets.features.features import ArrayaDExtensionType, cast_to_python_objects
from datasets.keyhash import DuplicatedKeysError, InvalidKeyError

from .utils import require_pil


class        A__   (   _snake_case      ):



    def        snake_case_      (       self							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   pa.array(TypedSequence([1, 2, 3]							)							)
           self.assertEqual(arr.type		,					pa.intaa()							)



    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           with self.assertRaises(UpperCamelCase__							):
                  A_						       =   pa.array(TypedSequence([1, 2, 3]							)		,					type=pa.intaa()							)



    def        snake_case_      (       self							)    ->   List[Any]:





           '''simple docstring'''
           with self.assertRaises(UpperCamelCase__							):
                  A_						       =   pa.array(TypedSequence([1, 2, 3]		,					try_type=Value("""bool"""							)		,					type=Value("""int64"""							)							)							)



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_						       =   pa.array(TypedSequence([1, 2, 3]		,					type=Value("""int32"""							)							)							)
           self.assertEqual(arr.type		,					pa.intaa()							)



    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           with self.assertRaises((TypeError, pa.lib.ArrowInvalid)							):
                  A_						       =   pa.array(TypedSequence(["""foo""", """bar"""]		,					type=Value("""int64"""							)							)							)



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_						       =   pa.array(TypedSequence([1, 2, 3]		,					try_type=Value("""int32"""							)							)							)
           self.assertEqual(arr.type		,					pa.intaa()							)



    def        snake_case_      (       self							)    ->   List[str]:





           '''simple docstring'''
           A_						       =   pa.array(TypedSequence(["""foo""", """bar"""]		,					try_type=Value("""int64"""							)							)							)
           self.assertEqual(arr.type		,					pa.string()							)



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_						       =   pa.array(TypedSequence([[[1, 2, 3]]]		,					type=ArrayaD((1, 3)		,					"""int64"""							)							)							)
           self.assertEqual(arr.type		,					ArrayaDExtensionType((1, 3)		,					"""int64"""							)							)



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





           '''simple docstring'''
           with self.assertRaises((TypeError, pa.lib.ArrowInvalid)							):
                  A_						       =   pa.array(TypedSequence(["""foo""", """bar"""]		,					type=ArrayaD((1, 3)		,					"""int64"""							)							)							)



    def        snake_case_      (       self							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   pa.array(TypedSequence([[[1, 2, 3]]]		,					try_type=ArrayaD((1, 3)		,					"""int64"""							)							)							)
           self.assertEqual(arr.type		,					ArrayaDExtensionType((1, 3)		,					"""int64"""							)							)



    def        snake_case_      (       self							)    ->   List[str]:





           '''simple docstring'''
           A_						       =   pa.array(TypedSequence(["""foo""", """bar"""]		,					try_type=ArrayaD((1, 3)		,					"""int64"""							)							)							)
           self.assertEqual(arr.type		,					pa.string()							)







    @require_pil
    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           import PIL.Image

           A_						       =   PIL.Image.fromarray(np.arange(10		,					dtype=np.uinta							).reshape(2		,					5							)							)
           with patch(
               """datasets.arrow_writer.cast_to_python_objects"""		,					side_effect=UpperCamelCase__							) as mock_cast_to_python_objects:
                  A_						       =   pa.array(TypedSequence([{"""path""": None, """bytes""": B"""image_bytes"""}, pil_image]		,					type=Image()							)							)
                  A_       ,      A_						       =   mock_cast_to_python_objects.call_args_list[-1]
                  self.assertIn("""optimize_list_casting"""		,					UpperCamelCase__							)
                  self.assertFalse(kwargs["""optimize_list_casting"""]							)

def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__					)      ->      Tuple:
       A_						       =   pa.BufferReader(UpperCAmelCase__					) if isinstance(UpperCAmelCase__,							pa.Buffer					) else pa.memory_map(UpperCAmelCase__					)
       A_						       =   pa.ipc.open_stream(UpperCAmelCase__					)
       A_						       =   f.read_all()
       assert len(pa_table.to_batches()					) == expected_num_chunks
       assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]}
       del pa_table
@pytest.mark.parametrize("""writer_batch_size""",							[None, 1, 10]					)
@pytest.mark.parametrize(
    """fields""",							[None, {"""col_1""": pa.string(), """col_2""": pa.intaa()}, {"""col_1""": pa.string(), """col_2""": pa.intaa()}]					)
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__					)      ->      Optional[Any]:
       A_						       =   pa.BufferOutputStream()
       A_						       =   pa.schema(UpperCAmelCase__					) if fields else None
       with ArrowWriter(stream=UpperCAmelCase__,							schema=UpperCAmelCase__,							writer_batch_size=UpperCAmelCase__					) as writer:
              writer.write({"""col_1""": """foo""", """col_2""": 1}					)
              writer.write({"""col_1""": """bar""", """col_2""": 2}					)
              A_       ,      A_						       =   writer.finalize()
       assert num_examples == 2
       assert num_bytes > 0
       if not fields:
              A_						       =   {"""col_1""": pa.string(), """col_2""": pa.intaa()}
       assert writer._schema == pa.schema(UpperCAmelCase__,							metadata=writer._schema.metadata					)
       _check_output(output.getvalue(),							expected_num_chunks=num_examples if writer_batch_size == 1 else 1					)
def    UpperCAmelCase__     (   )      ->      Any:
       A_						       =   pa.BufferOutputStream()
       A_						       =   Features({"""labels""": ClassLabel(names=["""neg""", """pos"""]					)}					)
       with ArrowWriter(stream=UpperCAmelCase__,							features=UpperCAmelCase__					) as writer:
              writer.write({"""labels""": 0}					)
              writer.write({"""labels""": 1}					)
              A_       ,      A_						       =   writer.finalize()
       assert num_examples == 2
       assert num_bytes > 0
       assert writer._schema == features.arrow_schema
       assert writer._schema.metadata == features.arrow_schema.metadata
       A_						       =   pa.BufferReader(output.getvalue()					)
       A_						       =   pa.ipc.open_stream(UpperCAmelCase__					)
       A_						       =   f.read_all()
       A_						       =   pa_table.schema
       assert pa_table.num_rows == 2
       assert schema == features.arrow_schema
       assert schema.metadata == features.arrow_schema.metadata
       assert features == Features.from_arrow_schema(UpperCAmelCase__					)
@pytest.mark.parametrize("""writer_batch_size""",							[None, 1, 10]					)
def    UpperCAmelCase__     (   UpperCAmelCase__					)      ->      str:
       A_						       =   pa.BufferOutputStream()
       with ArrowWriter(
           stream=UpperCAmelCase__,							writer_batch_size=UpperCAmelCase__,							hash_salt="""split_name""",							check_duplicates=UpperCAmelCase__,							) as writer:
              with pytest.raises(UpperCAmelCase__					):
                     writer.write({"""col_1""": """foo""", """col_2""": 1},							key=[1, 2]					)
                     A_       ,      A_						       =   writer.finalize()
@pytest.mark.parametrize("""writer_batch_size""",							[None, 2, 10]					)
def    UpperCAmelCase__     (   UpperCAmelCase__					)      ->      Any:
       A_						       =   pa.BufferOutputStream()
       with ArrowWriter(
           stream=UpperCAmelCase__,							writer_batch_size=UpperCAmelCase__,							hash_salt="""split_name""",							check_duplicates=UpperCAmelCase__,							) as writer:
              with pytest.raises(UpperCAmelCase__					):
                     writer.write({"""col_1""": """foo""", """col_2""": 1},							key=10					)
                     writer.write({"""col_1""": """bar""", """col_2""": 2},							key=10					)
                     A_       ,      A_						       =   writer.finalize()
@pytest.mark.parametrize("""writer_batch_size""",							[None, 2, 10]					)
def    UpperCAmelCase__     (   UpperCAmelCase__					)      ->      Any:
       A_						       =   pa.BufferOutputStream()
       with ArrowWriter(
           stream=UpperCAmelCase__,							writer_batch_size=UpperCAmelCase__,							hash_salt="""split_name""",							check_duplicates=UpperCAmelCase__,							) as writer:
              writer.write({"""col_1""": """foo""", """col_2""": 1},							key=1					)
              writer.write({"""col_1""": """bar""", """col_2""": 2},							key=2					)
              A_       ,      A_						       =   writer.finalize()
       assert num_examples == 2
       assert num_bytes > 0
       _check_output(output.getvalue(),							expected_num_chunks=num_examples if writer_batch_size == 1 else 1					)
@pytest.mark.parametrize("""writer_batch_size""",							[None, 1, 10]					)
@pytest.mark.parametrize(
    """fields""",							[None, {"""col_1""": pa.string(), """col_2""": pa.intaa()}, {"""col_1""": pa.string(), """col_2""": pa.intaa()}]					)
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__					)      ->      Optional[Any]:
       A_						       =   pa.BufferOutputStream()
       A_						       =   pa.schema(UpperCAmelCase__					) if fields else None
       with ArrowWriter(stream=UpperCAmelCase__,							schema=UpperCAmelCase__,							writer_batch_size=UpperCAmelCase__					) as writer:
              writer.write_batch({"""col_1""": ["""foo""", """bar"""], """col_2""": [1, 2]}					)
              writer.write_batch({"""col_1""": [], """col_2""": []}					)
              A_       ,      A_						       =   writer.finalize()
       assert num_examples == 2
       assert num_bytes > 0
       if not fields:
              A_						       =   {"""col_1""": pa.string(), """col_2""": pa.intaa()}
       assert writer._schema == pa.schema(UpperCAmelCase__,							metadata=writer._schema.metadata					)
       _check_output(output.getvalue(),							expected_num_chunks=num_examples if writer_batch_size == 1 else 1					)
@pytest.mark.parametrize("""writer_batch_size""",							[None, 1, 10]					)
@pytest.mark.parametrize(
    """fields""",							[None, {"""col_1""": pa.string(), """col_2""": pa.intaa()}, {"""col_1""": pa.string(), """col_2""": pa.intaa()}]					)
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__					)      ->      List[Any]:
       A_						       =   pa.BufferOutputStream()
       A_						       =   pa.schema(UpperCAmelCase__					) if fields else None
       with ArrowWriter(stream=UpperCAmelCase__,							schema=UpperCAmelCase__,							writer_batch_size=UpperCAmelCase__					) as writer:
              writer.write_table(pa.Table.from_pydict({"""col_1""": ["""foo""", """bar"""], """col_2""": [1, 2]}					)					)
              A_       ,      A_						       =   writer.finalize()
       assert num_examples == 2
       assert num_bytes > 0
       if not fields:
              A_						       =   {"""col_1""": pa.string(), """col_2""": pa.intaa()}
       assert writer._schema == pa.schema(UpperCAmelCase__,							metadata=writer._schema.metadata					)
       _check_output(output.getvalue(),							expected_num_chunks=num_examples if writer_batch_size == 1 else 1					)
@pytest.mark.parametrize("""writer_batch_size""",							[None, 1, 10]					)
@pytest.mark.parametrize(
    """fields""",							[None, {"""col_1""": pa.string(), """col_2""": pa.intaa()}, {"""col_1""": pa.string(), """col_2""": pa.intaa()}]					)
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__					)      ->      Optional[Any]:
       A_						       =   pa.BufferOutputStream()
       A_						       =   pa.schema(UpperCAmelCase__					) if fields else None
       with ArrowWriter(stream=UpperCAmelCase__,							schema=UpperCAmelCase__,							writer_batch_size=UpperCAmelCase__					) as writer:
              writer.write_row(pa.Table.from_pydict({"""col_1""": ["""foo"""], """col_2""": [1]}					)					)
              writer.write_row(pa.Table.from_pydict({"""col_1""": ["""bar"""], """col_2""": [2]}					)					)
              A_       ,      A_						       =   writer.finalize()
       assert num_examples == 2
       assert num_bytes > 0
       if not fields:
              A_						       =   {"""col_1""": pa.string(), """col_2""": pa.intaa()}
       assert writer._schema == pa.schema(UpperCAmelCase__,							metadata=writer._schema.metadata					)
       _check_output(output.getvalue(),							expected_num_chunks=num_examples if writer_batch_size == 1 else 1					)
def    UpperCAmelCase__     (   )      ->      Optional[Any]:
       with tempfile.TemporaryDirectory() as tmp_dir:
              A_						       =   {"""col_1""": pa.string(), """col_2""": pa.intaa()}
              A_						       =   os.path.join(UpperCAmelCase__,							"""test.arrow"""					)
              with ArrowWriter(path=UpperCAmelCase__,							schema=pa.schema(UpperCAmelCase__					)					) as writer:
                     writer.write_batch({"""col_1""": ["""foo""", """bar"""], """col_2""": [1, 2]}					)
                     A_       ,      A_						       =   writer.finalize()
              assert num_examples == 2
              assert num_bytes > 0
              assert writer._schema == pa.schema(UpperCAmelCase__,							metadata=writer._schema.metadata					)
              _check_output(UpperCAmelCase__,							1					)
def    UpperCAmelCase__     (   UpperCAmelCase__					)      ->      int:
       if pa.types.is_list(UpperCAmelCase__					):
              return get_base_dtype(arr_type.value_type					)
       else:
              return arr_type
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__					)      ->      str:
       if isinstance(lst[0],							UpperCAmelCase__					):
              change_first_primitive_element_in_list(lst[0],							UpperCAmelCase__					)
       else:
              A_						       =   value
@pytest.mark.parametrize("""optimized_int_type, expected_dtype""",							[(None, pa.intaa()), (Value("""int32"""					), pa.intaa())]					)
@pytest.mark.parametrize("""sequence""",							[[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]]					)
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__,							UpperCAmelCase__					)      ->      Union[str, Any]:
       A_						       =   pa.array(TypedSequence(UpperCAmelCase__,							optimized_int_type=UpperCAmelCase__					)					)
       assert get_base_dtype(arr.type					) == expected_dtype
@pytest.mark.parametrize(
    """col, expected_dtype""",							[
        ("""attention_mask""", pa.inta()),
        ("""special_tokens_mask""", pa.inta()),
        ("""token_type_ids""", pa.inta()),
        ("""input_ids""", pa.intaa()),
        ("""other""", pa.intaa()),
    ],							)
@pytest.mark.parametrize("""sequence""",							[[1, 2, 3], [[1, 2, 3]], [[[1, 2, 3]]]]					)
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__,							UpperCAmelCase__					)      ->      Tuple:
       # in range
       A_						       =   pa.array(OptimizedTypedSequence(UpperCAmelCase__,							col=UpperCAmelCase__					)					)
       assert get_base_dtype(arr.type					) == expected_dtype

       # not in range
       if col != "other":
              # avoids errors due to in-place modifications
              A_						       =   copy.deepcopy(UpperCAmelCase__					)
              A_						       =   np.iinfo(expected_dtype.to_pandas_dtype()					).max + 1
              change_first_primitive_element_in_list(UpperCAmelCase__,							UpperCAmelCase__					)
              A_						       =   pa.array(OptimizedTypedSequence(UpperCAmelCase__,							col=UpperCAmelCase__					)					)
              assert get_base_dtype(arr.type					) == pa.intaa()
@pytest.mark.parametrize("""raise_exception""",							[False, True]					)
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__					)      ->      List[Any]:
       A_						       =   str(tmp_path / """dataset-train.arrow"""					)
       try:
              with ArrowWriter(path=UpperCAmelCase__					) as writer:
                     if raise_exception:
                            raise pa.lib.ArrowInvalid()
                     else:
                            writer.stream.close()
       except pa.lib.ArrowInvalid:
              pass
       finally:
              assert writer.stream.closed
def    UpperCAmelCase__     (   UpperCAmelCase__					)      ->      Dict:
       A_						       =   """mock://dataset-train.arrow"""
       with ArrowWriter(path=UpperCAmelCase__,							storage_options=mockfs.storage_options					) as writer:
              assert isinstance(writer._fs,							type(UpperCAmelCase__					)					)
              assert writer._fs.storage_options == mockfs.storage_options
              writer.write({"""col_1""": """foo""", """col_2""": 1}					)
              writer.write({"""col_1""": """bar""", """col_2""": 2}					)
              A_       ,      A_						       =   writer.finalize()
       assert num_examples == 2
       assert num_bytes > 0
       assert mockfs.exists(UpperCAmelCase__					)
def    UpperCAmelCase__     (   )      ->      int:
       A_						       =   pa.BufferOutputStream()
       with ParquetWriter(stream=UpperCAmelCase__					) as writer:
              writer.write({"""col_1""": """foo""", """col_2""": 1}					)
              writer.write({"""col_1""": """bar""", """col_2""": 2}					)
              A_       ,      A_						       =   writer.finalize()
       assert num_examples == 2
       assert num_bytes > 0
       A_						       =   pa.BufferReader(output.getvalue()					)
       A_						       =   pq.read_table(UpperCAmelCase__					)
       assert pa_table.to_pydict() == {"col_1": ["foo", "bar"], "col_2": [1, 2]}
@require_pil
@pytest.mark.parametrize("""embed_local_files""",							[False, True]					)
def    UpperCAmelCase__     (   UpperCAmelCase__,							UpperCAmelCase__					)      ->      int:
       import PIL.Image

       A_						       =   str(tmp_path / """test_image_rgb.jpg"""					)
       PIL.Image.fromarray(np.zeros((5, 5),							dtype=np.uinta					)					).save(UpperCAmelCase__,							format="""png"""					)
       A_						       =   pa.BufferOutputStream()
       with ParquetWriter(
           stream=UpperCAmelCase__,							features=Features({"""image""": Image()}					),							embed_local_files=UpperCAmelCase__					) as writer:
              writer.write({"""image""": image_path}					)
              writer.finalize()
       A_						       =   pa.BufferReader(output.getvalue()					)
       A_						       =   pq.read_table(UpperCAmelCase__					)
       A_						       =   pa_table.to_pydict()
       if embed_local_files:
              assert isinstance(out["""image"""][0]["""path"""],							UpperCAmelCase__					)
              with open(UpperCAmelCase__,							"""rb"""					) as f:
                     assert out["image"][0]["bytes"] == f.read()
       else:
              assert out["image"][0]["path"] == image_path
              assert out["image"][0]["bytes"] is None







def    UpperCAmelCase__     (   )      ->      List[str]:
       A_						       =   pa.schema([pa.field("""col_1""",							pa.string(),							nullable=UpperCAmelCase__					)]					)
       A_						       =   pa.BufferOutputStream()
       with ArrowWriter(stream=UpperCAmelCase__					) as writer:
              writer._build_writer(inferred_schema=UpperCAmelCase__					)
              assert writer._schema == pa.schema([pa.field("""col_1""",							pa.string()					)]					)



'''simple docstring'''







import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          MODEL_MAPPING,
          SegformerForImageClassification,
          SegformerForSemanticSegmentation,
          SegformerModel,
      )
      from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import SegformerImageProcessor


class        A__   (   _snake_case      ):



    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           A_						       =   self.config_class(**self.inputs_dict							)
           self.parent.assertTrue(hasattr(UpperCamelCase__		,					"""hidden_sizes"""							)							)
           self.parent.assertTrue(hasattr(UpperCamelCase__		,					"""num_attention_heads"""							)							)
           self.parent.assertTrue(hasattr(UpperCamelCase__		,					"""num_encoder_blocks"""							)							)


class        A__   :



    def __init__(       self		,					UpperCamelCase__		,					UpperCamelCase__=13		,					UpperCamelCase__=64		,					UpperCamelCase__=3		,					UpperCamelCase__=4		,					UpperCamelCase__=[2, 2, 2, 2]		,					UpperCamelCase__=[8, 4, 2, 1]		,					UpperCamelCase__=[16, 32, 64, 128]		,					UpperCamelCase__=[1, 4, 8, 16]		,					UpperCamelCase__=[1, 2, 4, 8]		,					UpperCamelCase__=True		,					UpperCamelCase__=True		,					UpperCamelCase__="gelu"		,					UpperCamelCase__=0.1		,					UpperCamelCase__=0.1		,					UpperCamelCase__=0.02		,					UpperCamelCase__=3		,					UpperCamelCase__=None		,					)    ->   Tuple:





           '''simple docstring'''
           A_						       =   parent
           A_						       =   batch_size
           A_						       =   image_size
           A_						       =   num_channels
           A_						       =   num_encoder_blocks
           A_						       =   sr_ratios
           A_						       =   depths
           A_						       =   hidden_sizes
           A_						       =   downsampling_rates
           A_						       =   num_attention_heads
           A_						       =   is_training
           A_						       =   use_labels
           A_						       =   hidden_act
           A_						       =   hidden_dropout_prob
           A_						       =   attention_probs_dropout_prob
           A_						       =   initializer_range
           A_						       =   num_labels
           A_						       =   scope



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_						       =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

           A_						       =   None
           if self.use_labels:
                  A_						       =   ids_tensor([self.batch_size, self.image_size, self.image_size]		,					self.num_labels							)

           A_						       =   self.get_config()
           return config, pixel_values, labels



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





           '''simple docstring'''
           return SegformerConfig(
               image_size=self.image_size		,					num_channels=self.num_channels		,					num_encoder_blocks=self.num_encoder_blocks		,					depths=self.depths		,					hidden_sizes=self.hidden_sizes		,					num_attention_heads=self.num_attention_heads		,					hidden_act=self.hidden_act		,					hidden_dropout_prob=self.hidden_dropout_prob		,					attention_probs_dropout_prob=self.attention_probs_dropout_prob		,					initializer_range=self.initializer_range		,					)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   SegformerModel(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__							)
           A_						       =   A_						       =   self.image_size // (self.downsampling_rates[-1] * 2)
           self.parent.assertEqual(
               result.last_hidden_state.shape		,					(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Any:





           '''simple docstring'''
           A_						       =   self.num_labels
           A_						       =   SegformerForSemanticSegmentation(UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__							)
           self.parent.assertEqual(
               result.logits.shape		,					(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)							)
           A_						       =   model(UpperCamelCase__		,					labels=UpperCamelCase__							)
           self.parent.assertEqual(
               result.logits.shape		,					(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)							)
           self.parent.assertGreater(result.loss		,					0.0							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Dict:





           '''simple docstring'''
           A_						       =   1
           A_						       =   SegformerForSemanticSegmentation(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   torch.randint(0		,					1		,					(self.batch_size, self.image_size, self.image_size)							).to(UpperCamelCase__							)
           A_						       =   model(UpperCamelCase__		,					labels=UpperCamelCase__							)
           self.parent.assertGreater(result.loss		,					0.0							)







    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   self.prepare_config_and_inputs()
           A_       ,      A_       ,      A_						       =   config_and_inputs
           A_						       =   {"""pixel_values""": pixel_values}
           return config, inputs_dict


@require_torch
class        A__   (   _snake_case    ,    _snake_case    ,    unittest.TestCase      ):
    lowercase						       =		(
        (
            SegformerModel,
            SegformerForSemanticSegmentation,
            SegformerForImageClassification,
        )
        if is_torch_available()
        else ()
    )
    lowercase						       =		(
        {
            "feature-extraction": SegformerModel,
            "image-classification": SegformerForImageClassification,
            "image-segmentation": SegformerForSemanticSegmentation,
        }
        if is_torch_available()
        else {}
    )

    lowercase						       =		True
    lowercase						       =		False
    lowercase						       =		False
    lowercase						       =		False



    def        snake_case_      (       self							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   SegformerModelTester(self							)
           A_						       =   SegformerConfigTester(self		,					config_class=UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           self.config_tester.run_common_tests()



    def        snake_case_      (       self							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_binary_image_segmentation(*UpperCamelCase__							)



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





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_segmentation(*UpperCamelCase__							)



    @unittest.skip("""SegFormer does not use inputs_embeds"""							)
    def        snake_case_      (       self							)    ->   List[Any]:





           '''simple docstring'''
           pass



    @unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods"""							)
    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           pass



    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A_						       =   model_class(UpperCamelCase__							)
                  A_						       =   inspect.signature(model.forward							)
                  # signature.parameters is an OrderedDict => so arg_names order is deterministic
                  A_						       =   [*signature.parameters.keys()]

                  A_						       =   ["""pixel_values"""]
                  self.assertListEqual(arg_names[:1]		,					UpperCamelCase__							)



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





           '''simple docstring'''
           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()
           A_						       =   True

           for model_class in self.all_model_classes:
                  A_						       =   True
                  A_						       =   False
                  A_						       =   True
                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()
                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)
                  A_						       =   outputs.attentions

                  A_						       =   sum(self.model_tester.depths							)
                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)

                  # check that output_attentions also work using config
                  del inputs_dict["output_attentions"]
                  A_						       =   True
                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()
                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)
                  A_						       =   outputs.attentions

                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)

                  # verify the first attentions (first block, first layer)
                  A_						       =   (self.model_tester.image_size // 4) ** 2
                  A_						       =   (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
                  self.assertListEqual(
                      list(attentions[0].shape[-3:]							)		,					[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]		,					)

                  # verify the last attentions (last block, last layer)
                  A_						       =   (self.model_tester.image_size // 32) ** 2
                  A_						       =   (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
                  self.assertListEqual(
                      list(attentions[-1].shape[-3:]							)		,					[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]		,					)
                  A_						       =   len(UpperCamelCase__							)

                  # Check attention is always last and order is fine
                  A_						       =   True
                  A_						       =   True
                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()
                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)

                  self.assertEqual(out_len + 1		,					len(UpperCamelCase__							)							)

                  A_						       =   outputs.attentions

                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)
                  # verify the first attentions (first block, first layer)
                  A_						       =   (self.model_tester.image_size // 4) ** 2
                  A_						       =   (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
                  self.assertListEqual(
                      list(self_attentions[0].shape[-3:]							)		,					[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]		,					)



    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           def check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							):
                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()

                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)

                  A_						       =   outputs.hidden_states

                  A_						       =   self.model_tester.num_encoder_blocks
                  self.assertEqual(len(UpperCamelCase__							)		,					UpperCamelCase__							)

                  # verify the first hidden states (first block)
                  self.assertListEqual(
                      list(hidden_states[0].shape[-3:]							)		,					[
                          self.model_tester.hidden_sizes[0],
                          self.model_tester.image_size // 4,
                          self.model_tester.image_size // 4,
                      ]		,					)

           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A_						       =   True
                  check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  A_						       =   True

                  check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)



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





           '''simple docstring'''
           if not self.model_tester.is_training:
                  return

           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()
           A_						       =   True

           for model_class in self.all_model_classes:
                  if model_class in get_values(UpperCamelCase__							):
                         continue

                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.train()
                  A_						       =   self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__		,					return_labels=UpperCamelCase__							)
                  A_						       =   model(**UpperCamelCase__							).loss
                  loss.backward()



    @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""							)
    def        snake_case_      (       self							)    ->   List[str]:





           '''simple docstring'''
           pass







    @slow
    def        snake_case_      (       self							)    ->   List[Any]:





           '''simple docstring'''
           for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  A_						       =   SegformerModel.from_pretrained(UpperCamelCase__							)
                  self.assertIsNotNone(UpperCamelCase__							)

def    UpperCAmelCase__     (   )      ->      Any:
       A_						       =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
       return image







@require_torch
class        A__   (   unittest.TestCase      ):



    @slow
    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           # only resize + normalize
           A_						       =   SegformerImageProcessor(
               image_scale=(512, 512)		,					keep_ratio=UpperCamelCase__		,					align=UpperCamelCase__		,					do_random_crop=UpperCamelCase__							)
           A_						       =   SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""							).to(
               UpperCamelCase__							)

           A_						       =   prepare_img()
           A_						       =   image_processor(images=UpperCamelCase__		,					return_tensors="""pt"""							)
           A_						       =   encoded_inputs.pixel_values.to(UpperCamelCase__							)

           with torch.no_grad():
                  A_						       =   model(UpperCamelCase__							)

           A_						       =   torch.Size((1, model.config.num_labels, 128, 128)							)
           self.assertEqual(outputs.logits.shape		,					UpperCamelCase__							)

           A_						       =   torch.tensor(
               [
                   [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
                   [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]],
                   [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]],
               ]							).to(UpperCamelCase__							)
           self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]		,					UpperCamelCase__		,					atol=1e-4							)							)



    @slow
    def        snake_case_      (       self							)    ->   Optional[Any]:





           '''simple docstring'''
           # only resize + normalize
           A_						       =   SegformerImageProcessor(
               image_scale=(512, 512)		,					keep_ratio=UpperCamelCase__		,					align=UpperCamelCase__		,					do_random_crop=UpperCamelCase__							)
           A_						       =   SegformerForSemanticSegmentation.from_pretrained(
               """nvidia/segformer-b1-finetuned-cityscapes-1024-1024"""							).to(UpperCamelCase__							)

           A_						       =   prepare_img()
           A_						       =   image_processor(images=UpperCamelCase__		,					return_tensors="""pt"""							)
           A_						       =   encoded_inputs.pixel_values.to(UpperCamelCase__							)

           with torch.no_grad():
                  A_						       =   model(UpperCamelCase__							)

           A_						       =   torch.Size((1, model.config.num_labels, 128, 128)							)
           self.assertEqual(outputs.logits.shape		,					UpperCamelCase__							)

           A_						       =   torch.tensor(
               [
                   [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]],
                   [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]],
                   [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
               ]							).to(UpperCamelCase__							)
           self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]		,					UpperCamelCase__		,					atol=1e-1							)							)







    @slow
    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           # only resize + normalize
           A_						       =   SegformerImageProcessor(
               image_scale=(512, 512)		,					keep_ratio=UpperCamelCase__		,					align=UpperCamelCase__		,					do_random_crop=UpperCamelCase__							)
           A_						       =   SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""							).to(
               UpperCamelCase__							)

           A_						       =   prepare_img()
           A_						       =   image_processor(images=UpperCamelCase__		,					return_tensors="""pt"""							)
           A_						       =   encoded_inputs.pixel_values.to(UpperCamelCase__							)

           with torch.no_grad():
                  A_						       =   model(UpperCamelCase__							)

           A_						       =   outputs.logits.detach().cpu()

           A_						       =   image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase__		,					target_sizes=[(500, 300)]							)
           A_						       =   torch.Size((500, 300)							)
           self.assertEqual(segmentation[0].shape		,					UpperCamelCase__							)

           A_						       =   image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase__							)
           A_						       =   torch.Size((128, 128)							)
           self.assertEqual(segmentation[0].shape		,					UpperCamelCase__							)
