






"""simple docstring"""



import pytest

from datasets.splits import SplitDict, SplitInfo
from datasets.utils.py_utils import asdict



@pytest.mark.parametrize(
    '''split_dict'''		,					[
        SplitDict(),
        SplitDict({'''train''': SplitInfo(name='''train'''		,					num_bytes=1_3_3_7		,					num_examples=4_2		,					dataset_name='''my_dataset'''	)}	),
        SplitDict({'''train''': SplitInfo(name='''train'''		,					num_bytes=1_3_3_7		,					num_examples=4_2	)}	),
        SplitDict({'''train''': SplitInfo()}	),
    ]		,					)
def 						lowercase    (				__snake_case				: str	):
		lowercase_ : int							       =   split_dict._to_yaml_list()
		assert len(_A	) == len(_A	)
		lowercase_ : Union[str, Any]							       =   SplitDict._from_yaml_list(_A	)
		for split_name, split_info in split_dict.items():
				# dataset_name field is deprecated, and is therefore not part of the YAML dump
				lowercase_ : List[Any]							       =   None
				# the split name of split_dict takes over the name of the split info object
				lowercase_ : List[Any]							       =   split_name
		assert split_dict == reloaded






@pytest.mark.parametrize(
    '''split_info'''		,					[SplitInfo(), SplitInfo(dataset_name=_A	), SplitInfo(dataset_name='''my_dataset'''	)]	)
def 						lowercase    (				__snake_case				: Any	):
		# For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name"
		# field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files
		lowercase_ : Union[str, Any]							       =   asdict(SplitDict({'''train''': split_info}	)	)
		assert "dataset_name" in split_dict_asdict["train"]
		assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name









import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
						import torch

						from transformers import AutoModelForImageClassification

if is_vision_available():
						from transformers import AutoImageProcessor


@require_torch
@require_vision
class  A		(  unittest.TestCase     ):



		@slow
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								lowerCAmelCase_      			=  AutoImageProcessor.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''   )
								lowerCAmelCase_      			=  AutoModelForImageClassification.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''   )
								model.to(UpperCamelCase__   )

								from datasets import load_dataset

								lowerCAmelCase_      			=  load_dataset('''nielsr/rvlcdip-demo'''   )

								lowerCAmelCase_      			=  dataset['''train'''][0]['''image'''].convert('''RGB'''   )

								lowerCAmelCase_      			=  image_processor(UpperCamelCase__,	return_tensors='''pt'''   ).to(UpperCamelCase__   )

								# forward pass
								with torch.no_grad():
														lowerCAmelCase_      			=  model(**UpperCamelCase__   )
														lowerCAmelCase_      			=  outputs.logits

								lowerCAmelCase_      			=  torch.Size((1, 16)   )
								self.assertEqual(logits.shape,	UpperCamelCase__   )

								lowerCAmelCase_      			=  torch.tensor(
								    [-0.4_158, -0.4_092, -0.4_347],	device=UpperCamelCase__,	dtype=torch.float,	)
								self.assertTrue(torch.allclose(logits[0, :3],	UpperCamelCase__,	atol=1E-4   )   )




