


"""simple docstring"""







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 				UpperCamelCase_			:

	"""simple docstring"""





	def __init__(				self    :       int    ,    UpperCAmelCase__    :       Any    ,    UpperCAmelCase__    :       Any=9_9    ,    UpperCAmelCase__    :       List[str]=1_3    ,    UpperCAmelCase__    :       int=1_6    ,    UpperCAmelCase__    :       Optional[int]=7    ,    UpperCAmelCase__    :       List[Any]=True    ,    UpperCAmelCase__    :       List[str]=True    ,    UpperCAmelCase__    :       int=True    ,    UpperCAmelCase__    :       List[Any]=False    ,    UpperCAmelCase__    :       List[Any]=True    ,    UpperCAmelCase__    :       Optional[int]=2    ,    UpperCAmelCase__    :       Any=3_2    ,    UpperCAmelCase__    :       Union[str, Any]=4    ,    UpperCAmelCase__    :       List[str]=4    ,    UpperCAmelCase__    :       Any=3_0    ,    UpperCAmelCase__    :       Any=0    ,    UpperCAmelCase__    :       int=1    ,    UpperCAmelCase__    :       Optional[int]=2    ,    UpperCAmelCase__    :       Union[str, Any]=None    ,    ) ->			List[str]:
		__SCREAMING_SNAKE_CASE		     =       parent
		__SCREAMING_SNAKE_CASE		     =       batch_size
		__SCREAMING_SNAKE_CASE		     =       decoder_seq_length
		# For common tests
		__SCREAMING_SNAKE_CASE		     =       self.decoder_seq_length
		__SCREAMING_SNAKE_CASE		     =       is_training
		__SCREAMING_SNAKE_CASE		     =       use_attention_mask
		__SCREAMING_SNAKE_CASE		     =       use_labels

		__SCREAMING_SNAKE_CASE		     =       vocab_size
		__SCREAMING_SNAKE_CASE		     =       d_model
		__SCREAMING_SNAKE_CASE		     =       d_model
		__SCREAMING_SNAKE_CASE		     =       decoder_layers
		__SCREAMING_SNAKE_CASE		     =       decoder_layers
		__SCREAMING_SNAKE_CASE		     =       decoder_ffn_dim
		__SCREAMING_SNAKE_CASE		     =       decoder_attention_heads
		__SCREAMING_SNAKE_CASE		     =       decoder_attention_heads
		__SCREAMING_SNAKE_CASE		     =       eos_token_id
		__SCREAMING_SNAKE_CASE		     =       bos_token_id
		__SCREAMING_SNAKE_CASE		     =       pad_token_id
		__SCREAMING_SNAKE_CASE		     =       decoder_start_token_id
		__SCREAMING_SNAKE_CASE		     =       use_cache
		__SCREAMING_SNAKE_CASE		     =       max_position_embeddings

		__SCREAMING_SNAKE_CASE		     =       None
		__SCREAMING_SNAKE_CASE		     =       decoder_seq_length
		__SCREAMING_SNAKE_CASE		     =       2
		__SCREAMING_SNAKE_CASE		     =       1




	def 						UpperCAmelCase_   (				self    :       Optional[int]     ) ->			Union[str, Any]:
		__SCREAMING_SNAKE_CASE		     =       ids_tensor([self.batch_size, self.decoder_seq_length]    ,    self.vocab_size     )

		__SCREAMING_SNAKE_CASE		     =       None
		if self.use_attention_mask:
			__SCREAMING_SNAKE_CASE		     =       ids_tensor([self.batch_size, self.decoder_seq_length]    ,    vocab_size=2     )

		__SCREAMING_SNAKE_CASE		     =       None
		if self.use_labels:
			__SCREAMING_SNAKE_CASE		     =       ids_tensor([self.batch_size, self.decoder_seq_length]    ,    self.vocab_size     )

		__SCREAMING_SNAKE_CASE		     =       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    :       List[str]    ,    UpperCAmelCase__    :       Optional[int]    ,    UpperCAmelCase__    :       Optional[Any]    ,    UpperCAmelCase__    :       str    ,    UpperCAmelCase__    :       str    ,    ) ->			List[str]:
		__SCREAMING_SNAKE_CASE		     =       True
		__SCREAMING_SNAKE_CASE		     =       TrOCRDecoder(config=UpperCAmelCase__     ).to(UpperCAmelCase__     ).eval()
		__SCREAMING_SNAKE_CASE		     =       input_ids[:2]

		input_ids[input_ids == 0] += 1
		# first forward pass
		__SCREAMING_SNAKE_CASE		     =       model(UpperCAmelCase__    ,    use_cache=UpperCAmelCase__     )
		__SCREAMING_SNAKE_CASE		     =       model(UpperCAmelCase__     )
		__SCREAMING_SNAKE_CASE		     =       model(UpperCAmelCase__    ,    use_cache=UpperCAmelCase__     )

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

		__SCREAMING_SNAKE_CASE		     =       outputs["past_key_values"]

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

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

		__SCREAMING_SNAKE_CASE		     =       model(UpperCAmelCase__     )["last_hidden_state"]
		__SCREAMING_SNAKE_CASE		     =       model(UpperCAmelCase__    ,    past_key_values=UpperCAmelCase__     )["last_hidden_state"]

		# select random slice
		__SCREAMING_SNAKE_CASE		     =       ids_tensor((1,)    ,    output_from_past.shape[-1]     ).item()
		__SCREAMING_SNAKE_CASE		     =       output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
		__SCREAMING_SNAKE_CASE		     =       output_from_past[:, 0, random_slice_idx].detach()

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




	def 						UpperCAmelCase_   (				self    :       Dict     ) ->			Dict:
		__SCREAMING_SNAKE_CASE		     =       self.prepare_config_and_inputs()
		__SCREAMING_SNAKE_CASE		     =       config_and_inputs

		__SCREAMING_SNAKE_CASE		     =       {"input_ids": input_ids, "attention_mask": attention_mask}
		return config, inputs_dict




@require_torch
class 				UpperCamelCase_			(				__lowercase    ,						__lowercase    ,						__lowercase    ,						unittest.TestCase):

	"""simple docstring"""





	snake_case__       :						Dict					      =    (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
	snake_case__       :						str					      =    (TrOCRForCausalLM,) if is_torch_available() else ()
	snake_case__       :						str					      =    {"text-generation": TrOCRForCausalLM} if is_torch_available() else {}
	snake_case__       :						Dict					      =    True
	snake_case__       :						int					      =    False




	def 						UpperCAmelCase_   (				self    :       Union[str, Any]     ) ->			Optional[int]:
		__SCREAMING_SNAKE_CASE		     =       TrOCRStandaloneDecoderModelTester(self    ,    is_training=UpperCAmelCase__     )
		__SCREAMING_SNAKE_CASE		     =       ConfigTester(self    ,    config_class=UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       Union[str, Any]     ) ->			Tuple:
		pass




	def 						UpperCAmelCase_   (				self    :       str     ) ->			Optional[Any]:
		pass




	def 						UpperCAmelCase_   (				self    :       Optional[int]     ) ->			str:
		pass




	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			Any:
		self.config_tester.run_common_tests()




	def 						UpperCAmelCase_   (				self    :       List[str]     ) ->			Optional[int]:
		__SCREAMING_SNAKE_CASE		     =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_decoder_model_past(*UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       Tuple     ) ->			Dict:
		return




	@unittest.skip("The model doesn't support left padding"     )  # and it's not used enough to be worth fixing :)
	def 						UpperCAmelCase_   (				self    :       Optional[int]     ) ->			Optional[Any]:
		pass




"""simple docstring"""







import platform
from argparse import ArgumentParser

import huggingface_hub

from .. import __version__ as version
from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available
from . import BaseDiffusersCLICommand




def      UpperCAmelCase__ (lowerCAmelCase_     ):




					'''simple docstring'''


					return EnvironmentCommand()




class 				UpperCamelCase_			(				UpperCamelCase):

	"""simple docstring"""





	@staticmethod
	def 						UpperCAmelCase_   (				UpperCAmelCase__    :       ArgumentParser     ) ->			Optional[Any]:
						__SCREAMING_SNAKE_CASE		     =       parser.add_parser("env"     )
						download_parser.set_defaults(func=UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       List[Any]     ) ->			List[str]:
						__SCREAMING_SNAKE_CASE		     =       huggingface_hub.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						__SCREAMING_SNAKE_CASE		     =       "NA"
						if is_torch_available():
											import torch

											__SCREAMING_SNAKE_CASE		     =       torch.__version__
											__SCREAMING_SNAKE_CASE		     =       torch.cuda.is_available()

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_transformers_available():
											import transformers

											__SCREAMING_SNAKE_CASE		     =       transformers.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_accelerate_available():
											import accelerate

											__SCREAMING_SNAKE_CASE		     =       accelerate.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_xformers_available():
											import xformers

											__SCREAMING_SNAKE_CASE		     =       xformers.__version__

						__SCREAMING_SNAKE_CASE		     =       {
						    "`diffusers` version": version,
						    "Platform": platform.platform(),
						    "Python version": platform.python_version(),
						    "PyTorch version (GPU?)": F"""{pt_version} ({pt_cuda_available})""",
						    "Huggingface_hub version": hub_version,
						    "Transformers version": transformers_version,
						    "Accelerate version": accelerate_version,
						    "xFormers version": xformers_version,
						    "Using GPU in script?": "<fill in>",
						    "Using distributed or parallel set-up in script?": "<fill in>",
						}

						print("\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n"     )
						print(self.format_dict(UpperCAmelCase__     )     )

						return info




	@staticmethod
	def 						UpperCAmelCase_   (				UpperCAmelCase__    :       Optional[Any]     ) ->			str:
						return "\n".join([F"""- {prop}: {val}""" for prop, val in d.items()]     ) + "\n"
