



"""simple docstring"""

from unittest import TestCase

from datasets import Sequence, Value
from datasets.arrow_dataset import Dataset




class  _UpperCAmelCase (    lowerCAmelCase__):

 def 		_snake_case      (  self	: str       ):
     return [
         {"col_1": 3, "col_2": "a"},
         {"col_1": 2, "col_2": "b"},
         {"col_1": 1, "col_2": "c"},
         {"col_1": 0, "col_2": "d"},
     ]

 def 		_snake_case      (  self	: List[str]       ):
     snake_case_				:  Optional[int]       =			{'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']}
     return Dataset.from_dict(lowercase_       )

 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  str       =			self._create_example_records()
     snake_case_				:  List[Any]       =			Dataset.from_list(lowercase_       )
     self.assertListEqual(dset.column_names						,  ['''col_1''', '''col_2''']       )
     for i, r in enumerate(lowercase_       ):
         self.assertDictEqual(lowercase_						,  example_records[i]       )

 def 		_snake_case      (  self	: Optional[Any]       ):
     snake_case_				:  Tuple       =			self._create_example_records()
     snake_case_				:  List[Any]       =			Dataset.from_list(lowercase_       )
     snake_case_				:  Union[str, Any]       =			Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]}       )
     self.assertEqual(dset.info						,  dset_from_dict.info       )

 def 		_snake_case      (  self	: Dict       ):  # checks what happens with missing columns
     snake_case_				:  List[Any]       =			[{'''col_1''': 1}, {'''col_2''': '''x'''}]
     snake_case_				:  List[Any]       =			Dataset.from_list(lowercase_       )
     self.assertDictEqual(dset[0]						,  {'''col_1''': 1}       )
     self.assertDictEqual(dset[1]						,  {'''col_1''': None}       )  # NB: first record is used for columns

 def 		_snake_case      (  self	: int       ):  # checks if the type can be inferred from the second record
     snake_case_				:  Union[str, Any]       =			[{'''col_1''': []}, {'''col_1''': [1, 2]}]
     snake_case_				:  Tuple       =			Dataset.from_list(lowercase_       )
     self.assertEqual(dset.info.features['''col_1''']						,  Sequence(Value('''int64'''       )       )       )






 def 		_snake_case      (  self	: Tuple       ):
     snake_case_				:  Dict       =			Dataset.from_list([]       )
     self.assertEqual(len(lowercase_       )						,  0       )
     self.assertListEqual(dset.column_names						,  []       )










"""simple docstring"""

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  _UpperCAmelCase (    unittest.TestCase):

 @slow
 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  List[Any]       =			AutoImageProcessor.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''       )
     snake_case_				:  List[Any]       =			AutoModelForImageClassification.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''       )
     model.to(lowercase_       )

     from datasets import load_dataset

     snake_case_				:  str       =			load_dataset('''nielsr/rvlcdip-demo'''       )

     snake_case_				:  Tuple       =			dataset['''train'''][0]['''image'''].convert('''RGB'''       )

     snake_case_				:  Union[str, Any]       =			image_processor(lowercase_						,  return_tensors='''pt'''       ).to(lowercase_       )

     # forward pass
     with torch.no_grad():
         snake_case_				:  List[Any]       =			model(**lowercase_       )
         snake_case_				:  int       =			outputs.logits

     snake_case_				:  Any       =			torch.Size((1, 16)       )
     self.assertEqual(logits.shape						,  lowercase_       )

     snake_case_				:  Any       =			torch.tensor(
         [-0.41_58, -0.40_92, -0.43_47]						,  device=lowercase_						,  dtype=torch.float						,  )
     self.assertTrue(torch.allclose(logits[0, :3]						,  lowercase_						,  atol=1E-4       )       )





