





import unittest

from transformers import TrOCRConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM







@require_torch
class     __snake_case       :





       '''simple docstring'''







       def __init__(  self :    Union[str, Any]							,				A :    Optional[Any]							,				A :    Dict=99							,				A :    Tuple=13							,				A :    str=16							,				A :    str=7							,				A :    int=True							,				A :    Optional[int]=True							,				A :    int=True							,				A :    Tuple=False							,				A :    str=True							,				A :    List[str]=2							,				A :    List[Any]=32							,				A :    List[str]=4							,				A :    Any=4							,				A :    Any=30							,				A :    Optional[Any]=0							,				A :    List[Any]=1							,				A :    Tuple=2							,				A :    Optional[Any]=None							,				):
         __snake_case:						Any   =						parent
         __snake_case:						Optional[Any]   =						batch_size
         __snake_case:						Dict   =						decoder_seq_length
         # For common tests
         __snake_case:						List[str]   =						self.decoder_seq_length
         __snake_case:						List[Any]   =						is_training
         __snake_case:						Dict   =						use_attention_mask
         __snake_case:						str   =						use_labels

         __snake_case:						List[Any]   =						vocab_size
         __snake_case:						List[str]   =						d_model
         __snake_case:						str   =						d_model
         __snake_case:						str   =						decoder_layers
         __snake_case:						List[Any]   =						decoder_layers
         __snake_case:						str   =						decoder_ffn_dim
         __snake_case:						Optional[Any]   =						decoder_attention_heads
         __snake_case:						Dict   =						decoder_attention_heads
         __snake_case:						Optional[Any]   =						eos_token_id
         __snake_case:						int   =						bos_token_id
         __snake_case:						List[str]   =						pad_token_id
         __snake_case:						Optional[Any]   =						decoder_start_token_id
         __snake_case:						Optional[Any]   =						use_cache
         __snake_case:						List[Any]   =						max_position_embeddings

         __snake_case:						Optional[Any]   =						None
         __snake_case:						Dict   =						decoder_seq_length
         __snake_case:						Any   =						2
         __snake_case:						Any   =						1




       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         __snake_case:						Dict   =						ids_tensor([self.batch_size, self.decoder_seq_length]							,				self.vocab_size		)

         __snake_case:						Dict   =						None
         if self.use_attention_mask:
           __snake_case:						Optional[Any]   =						ids_tensor([self.batch_size, self.decoder_seq_length]							,				vocab_size=2		)

         __snake_case:						Dict   =						None
         if self.use_labels:
           __snake_case:						List[Any]   =						ids_tensor([self.batch_size, self.decoder_seq_length]							,				self.vocab_size		)

         __snake_case:						List[str]   =						TrOCRConfig(
             vocab_size=self.vocab_size							,				d_model=self.d_model							,				decoder_layers=self.decoder_layers							,				decoder_ffn_dim=self.decoder_ffn_dim							,				decoder_attention_heads=self.decoder_attention_heads							,				eos_token_id=self.eos_token_id							,				bos_token_id=self.bos_token_id							,				use_cache=self.use_cache							,				pad_token_id=self.pad_token_id							,				decoder_start_token_id=self.decoder_start_token_id							,				max_position_embeddings=self.max_position_embeddings							,				)

         return (config, input_ids, attention_mask, lm_labels)




       def 							UpperCAmelCase__ (  self :    Union[str, Any]							,				A :    Optional[Any]							,				A :    Tuple							,				A :    Any							,				A :    List[str]							,				):
         __snake_case:						List[str]   =						True
         __snake_case:						Any   =						TrOCRDecoder(config=A		).to(A		).eval()
         __snake_case:						str   =						input_ids[:2]

         input_ids[input_ids == 0] += 1
         # first forward pass
         __snake_case:						Optional[int]   =						model(A							,				use_cache=A		)
         __snake_case:						List[str]   =						model(A		)
         __snake_case:						Optional[Any]   =						model(A							,				use_cache=A		)

         self.parent.assertTrue(len(A		) == len(A		)		)
         self.parent.assertTrue(len(A		) == len(A		) + 1		)

         __snake_case:						Any   =						outputs["""past_key_values"""]

         # create hypothetical next token and extent to next_input_ids
         __snake_case:						Any   =						ids_tensor((2, 1)							,				config.vocab_size - 1		) + 1

         # append to next input_ids and
         __snake_case:						Any   =						torch.cat([input_ids, next_tokens]							,				dim=-1		)

         __snake_case:						int   =						model(A		)["""last_hidden_state"""]
         __snake_case:						int   =						model(A							,				past_key_values=A		)["""last_hidden_state"""]

         # select random slice
         __snake_case:						Optional[int]   =						ids_tensor((1,)							,				output_from_past.shape[-1]		).item()
         __snake_case:						List[str]   =						output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
         __snake_case:						Optional[int]   =						output_from_past[:, 0, random_slice_idx].detach()

         # test that outputs are equal for slice
         assert torch.allclose(A							,				A							,				atol=1E-3		)






       def 							UpperCAmelCase__ (  self :    Tuple		):
         __snake_case:						int   =						self.prepare_config_and_inputs()
         __snake_case  ,   __snake_case  ,   __snake_case  ,   __snake_case:						List[str]   =						config_and_inputs

         __snake_case:						int   =						{"""input_ids""": input_ids, """attention_mask""": attention_mask}
         return config, inputs_dict







@require_torch
class     __snake_case       (							__lowerCamelCase   ,			__lowerCamelCase   ,			__lowerCamelCase   ,			unittest.TestCase			):





       '''simple docstring'''







       lowerCAmelCase__      		=     (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
       lowerCAmelCase__      		=     (TrOCRForCausalLM,) if is_torch_available() else ()
       lowerCAmelCase__      		=     {"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {}
       lowerCAmelCase__      		=     True
       lowerCAmelCase__      		=     False




       def 							UpperCAmelCase__ (  self :    Union[str, Any]		):
         __snake_case:						List[Any]   =						TrOCRStandaloneDecoderModelTester(self							,				is_training=A		)
         __snake_case:						List[Any]   =						ConfigTester(self							,				config_class=A		)




       def 							UpperCAmelCase__ (  self :    List[Any]		):
         pass




       def 							UpperCAmelCase__ (  self :    Any		):
         pass




       def 							UpperCAmelCase__ (  self :    Tuple		):
         pass




       def 							UpperCAmelCase__ (  self :    Any		):
         self.config_tester.run_common_tests()




       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         __snake_case:						Union[str, Any]   =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past(*A		)




       def 							UpperCAmelCase__ (  self :    List[str]		):
         return






       @unittest.skip("""The model doesn't support left padding"""		)  # and it's not used enough to be worth fixing :)
       def 							UpperCAmelCase__ (  self :    Tuple		):
         pass













import pytest


__UpperCAmelCase       :   Optional[Any]				       =							"__dummy_dataset1__"

__UpperCAmelCase       :   List[str]				       =							"\nimport json\nimport os\n\nimport datasets\n\n\nREPO_URL = \"https://huggingface.co/datasets/albertvillanova/tests-raw-jsonl/resolve/main/\"\nURLS = {\"train\": REPO_URL + \"wikiann-bn-train.jsonl\", \"validation\": REPO_URL + \"wikiann-bn-validation.jsonl\"}\n\n\nclass __DummyDataset1__(datasets.GeneratorBasedBuilder):\n\n    def _info(self):\n        features = datasets.Features(\n            {\n                \"tokens\": datasets.Sequence(datasets.Value(\"string\")),\n                \"ner_tags\": datasets.Sequence(\n                    datasets.features.ClassLabel(\n                        names=[\n                            \"O\",\n                            \"B-PER\",\n                            \"I-PER\",\n                            \"B-ORG\",\n                            \"I-ORG\",\n                            \"B-LOC\",\n                            \"I-LOC\",\n                        ]\n                    )\n                ),\n                \"langs\": datasets.Sequence(datasets.Value(\"string\")),\n                \"spans\": datasets.Sequence(datasets.Value(\"string\")),\n            }\n        )\n        return datasets.DatasetInfo(features=features)\n\n    def _split_generators(self, dl_manager):\n        dl_path = dl_manager.download(URLS)\n        return [\n            datasets.SplitGenerator(datasets.Split.TRAIN, gen_kwargs={\"filepath\": dl_path[\"train\"]}),\n            datasets.SplitGenerator(datasets.Split.VALIDATION, gen_kwargs={\"filepath\": dl_path[\"validation\"]}),\n        ]\n\n    def _generate_examples(self, filepath):\n        with open(filepath, \"r\", encoding=\"utf-8\") as f:\n            for i, line in enumerate(f):\n                yield i, json.loads(line)\n"







@pytest.fixture
def 			A__							(    )   ->    Optional[int]:
  return DATASET_LOADING_SCRIPT_NAME







@pytest.fixture
def 			A__							(    )   ->    Tuple:
  return DATASET_LOADING_SCRIPT_CODE







@pytest.fixture
def 			A__							(    SCREAMING_SNAKE_CASE__					,					SCREAMING_SNAKE_CASE__					,					SCREAMING_SNAKE_CASE__)   ->    Tuple:
  __snake_case:						List[Any]   =						dataset_loading_script_name
  __snake_case:						Any   =						tmp_path / """datasets""" / script_name
  script_dir.mkdir(parents=SCREAMING_SNAKE_CASE__)
  __snake_case:						int   =						script_dir / F'''{script_name}.py'''
  with open(SCREAMING_SNAKE_CASE__					,					"""w""") as f:
    f.write(SCREAMING_SNAKE_CASE__)
  return str(SCREAMING_SNAKE_CASE__)






