





from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import TFCvtForImageClassification, TFCvtModel
					from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import AutoImageProcessor







class 				lowercase       ( lowercase_    ):




				def 					a							(      self ):
											snake_case_           =					self.config_class(**self.inputs_dict )
											self.parent.assertTrue(hasattr(snake_case   ,     'embed_dim' ) )
											self.parent.assertTrue(hasattr(snake_case   ,     'num_heads' ) )







class 				lowercase       :




				def __init__(      self   ,     snake_case   ,     snake_case=13   ,     snake_case=64   ,     snake_case=3   ,     snake_case=[16, 48, 96]   ,     snake_case=[1, 3, 6]   ,     snake_case=[1, 2, 10]   ,     snake_case=[7, 3, 3]   ,     snake_case=[4, 2, 2]   ,     snake_case=[2, 1, 1]   ,     snake_case=[2, 2, 2]   ,     snake_case=[False, False, True]   ,     snake_case=[0.0, 0.0, 0.0]   ,     snake_case=0.02   ,     snake_case=1e-1_2   ,     snake_case=True   ,     snake_case=True   ,     snake_case=2   ,     ):
											snake_case_           =					parent
											snake_case_           =					batch_size
											snake_case_           =					image_size
											snake_case_           =					patch_sizes
											snake_case_           =					patch_stride
											snake_case_           =					patch_padding
											snake_case_           =					is_training
											snake_case_           =					use_labels
											snake_case_           =					num_labels
											snake_case_           =					num_channels
											snake_case_           =					embed_dim
											snake_case_           =					num_heads
											snake_case_           =					stride_kv
											snake_case_           =					depth
											snake_case_           =					cls_token
											snake_case_           =					attention_drop_rate
											snake_case_           =					initializer_range
											snake_case_           =					layer_norm_eps




				def 					a							(      self ):
											snake_case_           =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

											snake_case_           =					None
											if self.use_labels:
																		# create a random int32 tensor of given shape
																		snake_case_           =					ids_tensor([self.batch_size]   ,     self.num_labels )

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




				def 					a							(      self ):
											return CvtConfig(
											    image_size=self.image_size   ,     num_labels=self.num_labels   ,     num_channels=self.num_channels   ,     embed_dim=self.embed_dim   ,     num_heads=self.num_heads   ,     patch_sizes=self.patch_sizes   ,     patch_padding=self.patch_padding   ,     patch_stride=self.patch_stride   ,     stride_kv=self.stride_kv   ,     depth=self.depth   ,     cls_token=self.cls_token   ,     attention_drop_rate=self.attention_drop_rate   ,     initializer_range=self.initializer_range   ,     )




				def 					a							(      self   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					TFCvtModel(config=snake_case )
											snake_case_           =					model(snake_case   ,     training=snake_case )
											snake_case_           =					(self.image_size, self.image_size)
											snake_case_       ,					snake_case_           =					image_size[0], image_size[1]
											for i in range(len(self.depth ) ):
																		snake_case_           =					floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
																		snake_case_           =					floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
											self.parent.assertEqual(result.last_hidden_state.shape   ,     (self.batch_size, self.embed_dim[-1], height, width) )




				def 					a							(      self   ,     snake_case   ,     snake_case   ,     snake_case ):
											snake_case_           =					self.num_labels
											snake_case_           =					TFCvtForImageClassification(snake_case )
											snake_case_           =					model(snake_case   ,     labels=snake_case   ,     training=snake_case )
											self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.num_labels) )




				def 					a							(      self ):
											snake_case_           =					self.prepare_config_and_inputs()
											snake_case_       ,					snake_case_       ,					snake_case_           =					config_and_inputs
											snake_case_           =					{'pixel_values': pixel_values}
											return config, inputs_dict







@require_tf
class 				lowercase       ( lowercase_			,						lowercase_			,						unittest.TestCase    ):
				__SCREAMING_SNAKE_CASE      :				Union[str, Any]     =    (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
				__SCREAMING_SNAKE_CASE      :				Dict     =    (
				    {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification}
				    if is_tf_available()
				    else {}
				)
				__SCREAMING_SNAKE_CASE      :				Union[str, Any]     =    False
				__SCREAMING_SNAKE_CASE      :				str     =    False
				__SCREAMING_SNAKE_CASE      :				Optional[int]     =    False
				__SCREAMING_SNAKE_CASE      :				Dict     =    False
				__SCREAMING_SNAKE_CASE      :				List[Any]     =    False




				def 					a							(      self ):
											snake_case_           =					TFCvtModelTester(self )
											snake_case_           =					TFCvtConfigTester(self   ,     config_class=snake_case   ,     has_text_modality=snake_case   ,     hidden_size=37 )




				def 					a							(      self ):
											self.config_tester.create_and_test_config_common_properties()
											self.config_tester.create_and_test_config_to_json_string()
											self.config_tester.create_and_test_config_to_json_file()
											self.config_tester.create_and_test_config_from_and_save_pretrained()
											self.config_tester.create_and_test_config_with_num_labels()
											self.config_tester.check_config_can_be_init_without_params()
											self.config_tester.check_config_arguments_init()




				@unittest.skip(reason='Cvt does not output attentions' )
				def 					a							(      self ):
											pass




				@unittest.skip(reason='Cvt does not use inputs_embeds' )
				def 					a							(      self ):
											pass




				@unittest.skip(reason='Cvt does not support input and output embeddings' )
				def 					a							(      self ):
											pass




				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices('GPU' ) ) == 0   ,     reason='TF does not support backprop for grouped convolutions on CPU.'   ,     )
				def 					a							(      self ):
											super().test_dataset_conversion()




				@unittest.skipIf(
				    not is_tf_available() or len(tf.config.list_physical_devices('GPU' ) ) == 0   ,     reason='TF does not support backprop for grouped convolutions on CPU.'   ,     )
				@slow
				def 					a							(      self ):
											super().test_keras_fit()




				@unittest.skip(reason='Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8' )
				def 					a							(      self ):
											snake_case_           =					tf.keras.mixed_precision.Policy('mixed_float16' )
											tf.keras.mixed_precision.set_global_policy(snake_case )
											super().test_keras_fit()
											tf.keras.mixed_precision.set_global_policy('float32' )




				def 					a							(      self ):
											snake_case_       ,					snake_case_           =					self.model_tester.prepare_config_and_inputs_for_common()

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

																		snake_case_           =					['pixel_values']
																		self.assertListEqual(arg_names[:1]   ,     snake_case )




				def 					a							(      self ):
											def check_hidden_states_output(snake_case   ,     snake_case   ,     snake_case ):
																		snake_case_           =					model_class(snake_case )

																		snake_case_           =					model(**self._prepare_for_class(snake_case   ,     snake_case ) )
																		snake_case_           =					outputs.hidden_states

																		snake_case_           =					len(self.model_tester.depth )
																		self.assertEqual(len(snake_case )   ,     snake_case )

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

											snake_case_       ,					snake_case_           =					self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		snake_case_           =					True
																		check_hidden_states_output(snake_case   ,     snake_case   ,     snake_case )

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

																		check_hidden_states_output(snake_case   ,     snake_case   ,     snake_case )




				def 					a							(      self ):
											snake_case_           =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*snake_case )




				def 					a							(      self ):
											snake_case_           =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*snake_case )




				@slow
				def 					a							(      self ):
											for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		snake_case_           =					TFCvtModel.from_pretrained(snake_case )
																		self.assertIsNotNone(snake_case )





def       __lowerCamelCase			(				):







							'''simple docstring'''






							snake_case_           =					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   )
							return image







@require_tf
@require_vision
class 				lowercase       ( unittest.TestCase    ):




				@cached_property
				def 					a							(      self ):
											return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )




				@slow
				def 					a							(      self ):
											snake_case_           =					TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

											snake_case_           =					self.default_image_processor
											snake_case_           =					prepare_img()
											snake_case_           =					image_processor(images=snake_case   ,     return_tensors='tf' )

											# forward pass
											snake_case_           =					model(**snake_case )

											# verify the logits
											snake_case_           =					tf.TensorShape((1, 1000) )
											self.assertEqual(outputs.logits.shape   ,     snake_case )

											snake_case_           =					tf.constant([0.92_85, 0.90_15, -0.31_50] )
											self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()   ,     snake_case   ,     atol=1e-4 ) )







import os
from typing import BinaryIO, Optional, Union

import numpy as np
import pyarrow.parquet as pq

from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config
from ..features.features import FeatureType, _visit
from ..formatting import query_table
from ..packaged_modules import _PACKAGED_DATASETS_MODULES
from ..packaged_modules.parquet.parquet import Parquet
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader





def       __lowerCamelCase			(				UpperCamelCase__   ):







							'''simple docstring'''






							snake_case_           =					np.inf

							def set_batch_size(UpperCamelCase__   ) -> None:
														nonlocal batch_size
														if isinstance(UpperCamelCase__     ,							UpperCamelCase__   ):
																					snake_case_           =					min(UpperCamelCase__     ,							config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS   )
														elif isinstance(UpperCamelCase__     ,							UpperCamelCase__   ):
																					snake_case_           =					min(UpperCamelCase__     ,							config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS   )
														elif isinstance(UpperCamelCase__     ,							UpperCamelCase__   ) and feature.dtype == "binary":
																					snake_case_           =					min(UpperCamelCase__     ,							config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS   )

							_visit(UpperCamelCase__     ,							UpperCamelCase__   )

							return None if batch_size is np.inf else batch_size







class 				lowercase       ( lowercase_    ):




				def __init__(      self   ,     snake_case   ,     snake_case = None   ,     snake_case = None   ,     snake_case = None   ,     snake_case = False   ,     snake_case = False   ,     snake_case = None   ,     **snake_case   ,     ):
											super().__init__(
											    snake_case   ,     split=snake_case   ,     features=snake_case   ,     cache_dir=snake_case   ,     keep_in_memory=snake_case   ,     streaming=snake_case   ,     num_proc=snake_case   ,     **snake_case   ,     )
											snake_case_           =					path_or_paths if isinstance(snake_case   ,     snake_case ) else {self.split: path_or_paths}
											snake_case_           =					_PACKAGED_DATASETS_MODULES['parquet'][1]
											snake_case_           =					Parquet(
											    cache_dir=snake_case   ,     data_files=snake_case   ,     features=snake_case   ,     hash=snake_case   ,     **snake_case   ,     )




				def 					a							(      self ):
											# Build iterable dataset
											if self.streaming:
																		snake_case_           =					self.builder.as_streaming_dataset(split=self.split )
											# Build regular (map-style) dataset
											else:
																		snake_case_           =					None
																		snake_case_           =					None
																		snake_case_           =					None
																		snake_case_           =					None

																		self.builder.download_and_prepare(
																		    download_config=snake_case   ,     download_mode=snake_case   ,     verification_mode=snake_case   ,     base_path=snake_case   ,     num_proc=self.num_proc   ,     )
																		snake_case_           =					self.builder.as_dataset(
																		    split=self.split   ,     verification_mode=snake_case   ,     in_memory=self.keep_in_memory )
											return dataset







class 				lowercase       :




				def __init__(      self   ,     snake_case   ,     snake_case   ,     snake_case = None   ,     **snake_case   ,     ):
											snake_case_           =					dataset
											snake_case_           =					path_or_buf
											snake_case_           =					batch_size or get_writer_batch_size(dataset.features )
											snake_case_           =					parquet_writer_kwargs




				def 					a							(      self ):
											snake_case_           =					self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE

											if isinstance(self.path_or_buf   ,     (str, bytes, os.PathLike) ):
																		with open(self.path_or_buf   ,     'wb+' ) as buffer:
																									snake_case_           =					self._write(file_obj=snake_case   ,     batch_size=snake_case   ,     **self.parquet_writer_kwargs )
											else:
																		snake_case_           =					self._write(file_obj=self.path_or_buf   ,     batch_size=snake_case   ,     **self.parquet_writer_kwargs )
											return written




				def 					a							(      self   ,     snake_case   ,     snake_case   ,     **snake_case ):
											snake_case_           =					0
											snake_case_           =					parquet_writer_kwargs.pop('path_or_buf'   ,     snake_case )
											snake_case_           =					self.dataset.features.arrow_schema

											snake_case_           =					pq.ParquetWriter(snake_case   ,     schema=snake_case   ,     **snake_case )

											for offset in logging.tqdm(
											    range(0   ,     len(self.dataset )   ,     snake_case )   ,     unit='ba'   ,     disable=not logging.is_progress_bar_enabled()   ,     desc='Creating parquet from Arrow format'   ,     ):
																		snake_case_           =					query_table(
																		    table=self.dataset._data   ,     key=slice(snake_case   ,     offset + batch_size )   ,     indices=self.dataset._indices if self.dataset._indices is not None else None   ,     )
																		writer.write_table(snake_case )
																		written += batch.nbytes
											writer.close()
											return written




