





'''simple docstring'''







import inspect
import unittest

from transformers import ResNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
	import jax
	import jax.numpy as jnp

	from transformers.models.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor





class 					_UpperCAmelCase							(		unittest.TestCase     ):
				def __init__(						self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE=3,__SCREAMING_SNAKE_CASE=32,__SCREAMING_SNAKE_CASE=3,__SCREAMING_SNAKE_CASE=10,__SCREAMING_SNAKE_CASE=[10, 20, 30, 40],__SCREAMING_SNAKE_CASE=[1, 1, 2, 1],__SCREAMING_SNAKE_CASE=True,__SCREAMING_SNAKE_CASE=True,__SCREAMING_SNAKE_CASE="relu",__SCREAMING_SNAKE_CASE=3,__SCREAMING_SNAKE_CASE=None,):



							'''simple docstring'''

							__lowerCAmelCase      		=       parent
							__lowerCAmelCase      		=       batch_size
							__lowerCAmelCase      		=       image_size
							__lowerCAmelCase      		=       num_channels
							__lowerCAmelCase      		=       embeddings_size
							__lowerCAmelCase      		=       hidden_sizes
							__lowerCAmelCase      		=       depths
							__lowerCAmelCase      		=       is_training
							__lowerCAmelCase      		=       use_labels
							__lowerCAmelCase      		=       hidden_act
							__lowerCAmelCase      		=       num_labels
							__lowerCAmelCase      		=       scope
							__lowerCAmelCase      		=       len(__A    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

							__lowerCAmelCase      		=       self.get_config()

							return config, pixel_values
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							return ResNetConfig(
							    num_channels=self.num_channels,embeddings_size=self.embeddings_size,hidden_sizes=self.hidden_sizes,depths=self.depths,hidden_act=self.hidden_act,num_labels=self.num_labels,image_size=self.image_size,)
				def 						lowerCamelCase__    (						self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       FlaxResNetModel(config=__A    )
							__lowerCAmelCase      		=       model(__A    )

							# Output shape (b, c, h, w)
							self.parent.assertEqual(
							    result.last_hidden_state.shape,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32),)
				def 						lowerCamelCase__    (						self,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       self.num_labels
							__lowerCAmelCase      		=       FlaxResNetForImageClassification(config=__A    )
							__lowerCAmelCase      		=       model(__A    )
							self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels)    )







				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       self.prepare_config_and_inputs()
							__lowerCAmelCase		, __lowerCAmelCase      		=       config_and_inputs
							__lowerCAmelCase      		=       {"""pixel_values""": pixel_values}
							return config, inputs_dict





@require_flax
class 					_UpperCAmelCase							(		_SCREAMING_SNAKE_CASE			,      unittest.TestCase     ):
				a     :     Optional[Any]     						=(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

				a     :     str     						=False
				a     :     Tuple     						=False
				a     :     Optional[int]     						=False
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       FlaxResNetModelTester(self    )
							__lowerCAmelCase      		=       ConfigTester(self,config_class=__A,has_text_modality=__A    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							self.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()
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							return
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__A    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__A    )
				@unittest.skip(reason="""ResNet does not use inputs_embeds"""    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							pass
				@unittest.skip(reason="""ResNet does not support input and output embeddings"""    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							pass
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase		, __lowerCAmelCase      		=       self.model_tester.prepare_config_and_inputs_for_common()

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

										__lowerCAmelCase      		=       ["""pixel_values"""]
										self.assertListEqual(arg_names[:1],__A    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							def check_hidden_states_output(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    ):
										__lowerCAmelCase      		=       model_class(__A    )

										__lowerCAmelCase      		=       model(**self._prepare_for_class(__A,__A    )    )

										__lowerCAmelCase      		=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

										__lowerCAmelCase      		=       self.model_tester.num_stages
										self.assertEqual(len(__A    ),expected_num_stages + 1    )
				@unittest.skip(reason="""ResNet does not use feedforward chunking"""    )
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							pass







				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase		, __lowerCAmelCase      		=       self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										with self.subTest(model_class.__name__    ):
													__lowerCAmelCase      		=       self._prepare_for_class(__A,__A    )
													__lowerCAmelCase      		=       model_class(__A    )

													@jax.jit
													def model_jitted(__SCREAMING_SNAKE_CASE,**__SCREAMING_SNAKE_CASE    ):
																return model(pixel_values=__A,**__A    )

													with self.subTest("""JIT Enabled"""    ):
																__lowerCAmelCase      		=       model_jitted(**__A    ).to_tuple()

													with self.subTest("""JIT Disabled"""    ):
																with jax.disable_jit():
																			__lowerCAmelCase      		=       model_jitted(**__A    ).to_tuple()

													self.assertEqual(len(__A    ),len(__A    )    )
													for jitted_output, output in zip(__A,__A    ):
																self.assertEqual(jitted_output.shape,output.shape    )





def      _lowerCAmelCase		(		)      ->      Union[str, Any]:
			__lowerCAmelCase      		=       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
			return image







@require_flax
class 					_UpperCAmelCase							(		unittest.TestCase     ):
				@cached_property
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							return AutoImageProcessor.from_pretrained("""microsoft/resnet-50"""    ) if is_vision_available() else None







				@slow
				def 						lowerCamelCase__    (						self    ):



							'''simple docstring'''

							__lowerCAmelCase      		=       FlaxResNetForImageClassification.from_pretrained("""microsoft/resnet-50"""    )

							__lowerCAmelCase      		=       self.default_image_processor
							__lowerCAmelCase      		=       prepare_img()
							__lowerCAmelCase      		=       image_processor(images=__A,return_tensors="""np"""    )

							__lowerCAmelCase      		=       model(**__A    )

							# verify the logits
							__lowerCAmelCase      		=       (1, 10_00)
							self.assertEqual(outputs.logits.shape,__A    )

							__lowerCAmelCase      		=       jnp.array([-11.1069, -9.7877, -8.3777]    )

							self.assertTrue(jnp.allclose(outputs.logits[0, :3],__A,atol=1e-4    )    )








'''simple docstring'''







import itertools
from dataclasses import dataclass
from io import StringIO
from typing import Optional

import pyarrow as pa

import datasets
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


_a      :				Tuple											=					datasets.utils.logging.get_logger(__name__)





@dataclass
class 					_UpperCAmelCase							(		datasets.BuilderConfig     ):
		a     :     Optional[datasets.Features]     						=None
		a     :     str     						="utf-8"
		a     :     Optional[str]     						=None
		a     :     int     						=10 << 20  # 10MB
		a     :     bool     						=False
		a     :     str     						="line"





class 					_UpperCAmelCase							(		datasets.ArrowBasedBuilder     ):
		a     :     Optional[int]     						=TextConfig
		def 						lowerCamelCase__    (						self    ):



					'''simple docstring'''

					return datasets.DatasetInfo(features=self.config.features    )
		def 						lowerCamelCase__    (						self,__SCREAMING_SNAKE_CASE    ):



					'''simple docstring'''

					if not self.config.data_files:
								raise ValueError(f'At least one data file must be specified, but got data_files={self.config.data_files}'    )
					__lowerCAmelCase      		=       dl_manager.download_and_extract(self.config.data_files    )
					if isinstance(__SCREAMING_SNAKE_CASE,(str, list, tuple)    ):
								__lowerCAmelCase      		=       data_files
								if isinstance(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    ):
											__lowerCAmelCase      		=       [files]
								__lowerCAmelCase      		=       [dl_manager.iter_files(__SCREAMING_SNAKE_CASE    ) for file in files]
								return [datasets.SplitGenerator(name=datasets.Split.TRAIN,gen_kwargs={"""files""": files}    )]
					__lowerCAmelCase      		=       []
					for split_name, files in data_files.items():
								if isinstance(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    ):
											__lowerCAmelCase      		=       [files]
								__lowerCAmelCase      		=       [dl_manager.iter_files(__SCREAMING_SNAKE_CASE    ) for file in files]
								splits.append(datasets.SplitGenerator(name=__SCREAMING_SNAKE_CASE,gen_kwargs={"""files""": files}    )    )
					return splits
		def 						lowerCamelCase__    (						self,__SCREAMING_SNAKE_CASE    ):



					'''simple docstring'''

					if self.config.features is not None:
								__lowerCAmelCase      		=       self.config.features.arrow_schema
								if all(not require_storage_cast(__SCREAMING_SNAKE_CASE    ) for feature in self.config.features.values()    ):
											# cheaper cast
											__lowerCAmelCase      		=       pa_table.cast(__SCREAMING_SNAKE_CASE    )
								else:
											# more expensive cast; allows str <-> int/float or str to Audio for example
											__lowerCAmelCase      		=       table_cast(__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE    )
								return pa_table
					else:
								return pa_table.cast(pa.schema({"""text""": pa.string()}    )    )







		def 						lowerCamelCase__    (						self,__SCREAMING_SNAKE_CASE    ):



					'''simple docstring'''

					__lowerCAmelCase      		=       list(self.config.features    ) if self.config.features is not None else ["""text"""]
					for file_idx, file in enumerate(itertools.chain.from_iterable(__SCREAMING_SNAKE_CASE    )    ):
								# open in text mode, by default translates universal newlines ("\n", "\r\n" and "\r") into "\n"
								with open(__SCREAMING_SNAKE_CASE,encoding=self.config.encoding,errors=self.config.errors    ) as f:
											if self.config.sample_by == "line":
														__lowerCAmelCase      		=       0
														while True:
																	__lowerCAmelCase      		=       f.read(self.config.chunksize    )
																	if not batch:
																				break
																	batch += f.readline()  # finish current line
																	# StringIO.readlines, by default splits only on "\n" (and keeps line breaks)
																	__lowerCAmelCase      		=       StringIO(__SCREAMING_SNAKE_CASE    ).readlines()
																	if not self.config.keep_linebreaks:
																				__lowerCAmelCase      		=       [line.rstrip("""\n"""    ) for line in batch]
																	__lowerCAmelCase      		=       pa.Table.from_arrays([pa.array(__SCREAMING_SNAKE_CASE    )],names=__SCREAMING_SNAKE_CASE    )
																	# Uncomment for debugging (will print the Arrow table size and elements)
																	# logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
																	# logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
																	yield (file_idx, batch_idx), self._cast_table(__SCREAMING_SNAKE_CASE    )
																	batch_idx += 1
											elif self.config.sample_by == "paragraph":
														__lowerCAmelCase      		=       0
														__lowerCAmelCase      		=       """"""
														while True:
																	__lowerCAmelCase      		=       f.read(self.config.chunksize    )
																	if not new_batch:
																				break
																	batch += new_batch
																	batch += f.readline()  # finish current line
																	__lowerCAmelCase      		=       batch.split("""\n\n"""    )
																	__lowerCAmelCase      		=       pa.Table.from_arrays(
																	    [pa.array([example for example in batch[:-1] if example]    )],names=__SCREAMING_SNAKE_CASE    )
																	# Uncomment for debugging (will print the Arrow table size and elements)
																	# logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
																	# logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
																	yield (file_idx, batch_idx), self._cast_table(__SCREAMING_SNAKE_CASE    )
																	batch_idx += 1
																	__lowerCAmelCase      		=       batch[-1]
														if batch:
																	__lowerCAmelCase      		=       pa.Table.from_arrays([pa.array([batch]    )],names=__SCREAMING_SNAKE_CASE    )
																	yield (file_idx, batch_idx), self._cast_table(__SCREAMING_SNAKE_CASE    )
											elif self.config.sample_by == "document":
														__lowerCAmelCase      		=       f.read()
														__lowerCAmelCase      		=       pa.Table.from_arrays([pa.array([text]    )],names=__SCREAMING_SNAKE_CASE    )
														yield file_idx, self._cast_table(__SCREAMING_SNAKE_CASE    )

