




"""simple docstring"""





import unittest

from transformers import GPTNeoXJapaneseConfig, is_torch_available
from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel



class      a__				:


			def __init__(      self      :    List[Any]      ,  UpperCamelCase_      :    Dict      ,  UpperCamelCase_      :    List[str]=13      ,  UpperCamelCase_      :    str=7      ,  UpperCamelCase_      :    Optional[int]=True      ,  UpperCamelCase_      :    Optional[int]=True      ,  UpperCamelCase_      :    Tuple=True      ,  UpperCamelCase_      :    Optional[int]=True      ,  UpperCamelCase_      :    List[Any]=99      ,  UpperCamelCase_      :    str=32      ,  UpperCamelCase_      :    str=5      ,  UpperCamelCase_      :    Tuple=4      ,  UpperCamelCase_      :    Dict=4      ,  UpperCamelCase_      :    Tuple="gelu"      ,  UpperCamelCase_      :    Optional[Any]=0.0      ,  UpperCamelCase_      :    List[Any]=0.1      ,  UpperCamelCase_      :    Tuple=True      ,  UpperCamelCase_      :    List[Any]=512      ,  UpperCamelCase_      :    Tuple=16      ,  UpperCamelCase_      :    int=2      ,  UpperCamelCase_      :    Union[str, Any]=0.02      ,  UpperCamelCase_      :    str=3      ,  UpperCamelCase_      :    Any=4      ,  UpperCamelCase_      :    List[Any]=None      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: int		=					parent
						__UpperCAmelCase	: Optional[int]		=					batch_size
						__UpperCAmelCase	: Tuple		=					seq_length
						__UpperCAmelCase	: str		=					is_training
						__UpperCAmelCase	: str		=					use_input_mask
						__UpperCAmelCase	: List[Any]		=					use_token_type_ids
						__UpperCAmelCase	: Union[str, Any]		=					use_labels
						__UpperCAmelCase	: Tuple		=					vocab_size
						__UpperCAmelCase	: int		=					hidden_size
						__UpperCAmelCase	: Optional[Any]		=					num_hidden_layers
						__UpperCAmelCase	: List[Any]		=					num_attention_heads
						__UpperCAmelCase	: List[str]		=					intermediate_multiple_size
						__UpperCAmelCase	: List[Any]		=					hidden_act
						__UpperCAmelCase	: Any		=					hidden_dropout
						__UpperCAmelCase	: List[str]		=					attention_dropout
						__UpperCAmelCase	: int		=					weight_tying
						__UpperCAmelCase	: Any		=					max_position_embeddings
						__UpperCAmelCase	: Dict		=					type_vocab_size
						__UpperCAmelCase	: Any		=					type_sequence_label_size
						__UpperCAmelCase	: Union[str, Any]		=					initializer_range
						__UpperCAmelCase	: int		=					num_labels
						__UpperCAmelCase	: List[str]		=					num_choices
						__UpperCAmelCase	: Optional[int]		=					scope


			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Tuple		=					ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size)

						__UpperCAmelCase	: Optional[int]		=					None
						if self.use_input_mask:
									__UpperCAmelCase	: str		=					random_attention_mask([self.batch_size, self.seq_length])

						__UpperCAmelCase	: List[Any]		=					None
						if self.use_labels:
									__UpperCAmelCase	: int		=					ids_tensor([self.batch_size, self.seq_length]      ,  self.num_labels)

						__UpperCAmelCase	: List[Any]		=					self.get_config()

						return config, input_ids, input_mask, token_labels


			def       a_     (      self      :    Dict):


						"""simple docstring"""




						return GPTNeoXJapaneseConfig(
						    vocab_size=self.vocab_size      ,  hidden_size=self.hidden_size      ,  num_hidden_layers=self.num_hidden_layers      ,  num_attention_heads=self.num_attention_heads      ,  intermediate_multiple_size=self.intermediate_multiple_size      ,  hidden_act=self.hidden_act      ,  hidden_dropout=self.hidden_dropout      ,  attention_dropout=self.attention_dropout      ,  weight_tying=self.weight_tying      ,  max_position_embeddings=self.max_position_embeddings      ,  type_vocab_size=self.type_vocab_size      ,  is_decoder=UpperCamelCase_      ,  initializer_range=self.initializer_range      ,  )


			def       a_     (      self      :    int):


						"""simple docstring"""




						__UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	: Union[str, Any]		=					self.prepare_config_and_inputs()

						__UpperCAmelCase	: Union[str, Any]		=					True

						return config, input_ids, input_mask, token_labels


			def       a_     (      self      :    Dict      ,  UpperCamelCase_      :    Any      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    Any):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					GPTNeoXJapaneseModel(config=UpperCamelCase_)
						model.to(UpperCamelCase_)
						model.eval()
						__UpperCAmelCase	: Optional[Any]		=					model(UpperCamelCase_      ,  attention_mask=UpperCamelCase_)
						__UpperCAmelCase	: Union[str, Any]		=					model(UpperCamelCase_)
						self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size))


			def       a_     (      self      :    int      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    Any      ,  UpperCamelCase_      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					True
						__UpperCAmelCase	: str		=					GPTNeoXJapaneseModel(UpperCamelCase_)
						model.to(UpperCamelCase_)
						model.eval()
						__UpperCAmelCase	: Optional[int]		=					model(UpperCamelCase_      ,  attention_mask=UpperCamelCase_)
						self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size))


			def       a_     (      self      :    Union[str, Any]      ,  UpperCamelCase_      :    List[Any]      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    int):


						"""simple docstring"""




						__UpperCAmelCase	: str		=					GPTNeoXJapaneseForCausalLM(config=UpperCamelCase_)
						model.to(UpperCamelCase_)
						model.eval()
						__UpperCAmelCase	: str		=					model(UpperCamelCase_      ,  attention_mask=UpperCamelCase_      ,  labels=UpperCamelCase_)
						self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length, self.vocab_size))


			def       a_     (      self      :    Union[str, Any]      ,  UpperCamelCase_      :    Union[str, Any]      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					True
						__UpperCAmelCase	: Union[str, Any]		=					GPTNeoXJapaneseForCausalLM(config=UpperCamelCase_)
						model.to(UpperCamelCase_)
						model.eval()

						# first forward pass
						__UpperCAmelCase	: int		=					model(UpperCamelCase_      ,  attention_mask=UpperCamelCase_      ,  use_cache=UpperCamelCase_)
						__UpperCAmelCase	: Optional[int]		=					outputs.past_key_values

						# create hypothetical multiple next token and extent to next_input_ids
						__UpperCAmelCase	: str		=					ids_tensor((self.batch_size, 3)      ,  config.vocab_size)
						__UpperCAmelCase	: Union[str, Any]		=					ids_tensor((self.batch_size, 3)      ,  vocab_size=2)

						# append to next input_ids and
						__UpperCAmelCase	: List[str]		=					torch.cat([input_ids, next_tokens]      ,  dim=-1)
						__UpperCAmelCase	: List[Any]		=					torch.cat([input_mask, next_mask]      ,  dim=-1)

						__UpperCAmelCase	: Any		=					model(UpperCamelCase_      ,  attention_mask=UpperCamelCase_      ,  output_hidden_states=UpperCamelCase_)
						__UpperCAmelCase	: List[str]		=					output_from_no_past["hidden_states"][0]
						__UpperCAmelCase	: Union[str, Any]		=					model(
						    UpperCamelCase_      ,  attention_mask=UpperCamelCase_      ,  past_key_values=UpperCamelCase_      ,  output_hidden_states=UpperCamelCase_      ,  )["hidden_states"][0]

						# select random slice
						__UpperCAmelCase	: Optional[int]		=					ids_tensor((1,)      ,  output_from_past.shape[-1]).item()
						__UpperCAmelCase	: str		=					output_from_no_past[:, -3:, random_slice_idx].detach()
						__UpperCAmelCase	: Optional[Any]		=					output_from_past[:, :, random_slice_idx].detach()

						self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1])

						# test that outputs are equal for slice
						self.parent.assertTrue(torch.allclose(UpperCamelCase_      ,  UpperCamelCase_      ,  atol=1e-3))


			def       a_     (      self      :    List[str]):


						"""simple docstring"""




						__UpperCAmelCase	: Dict		=					self.prepare_config_and_inputs()
						__UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	: Optional[Any]		=					config_and_inputs
						__UpperCAmelCase	: Tuple		=					{"input_ids": input_ids, "attention_mask": input_mask}
						return config, inputs_dict






@require_torch
class      a__				(	__magic_name__							,    __magic_name__							,    unittest.TestCase   ):
			lowercase_    =	(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
			lowercase_    =	(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
			lowercase_    =	(
			    {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
			    if is_torch_available()
			    else {}
			)
			lowercase_    =	False
			lowercase_    =	False
			lowercase_    =	False
			lowercase_    =	False


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: str		=					GPTNeoXJapaneseModelTester(self)
						__UpperCAmelCase	: int		=					ConfigTester(self      ,  config_class=UpperCamelCase_      ,  hidden_size=37)


			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						self.config_tester.run_common_tests()


			def       a_     (      self      :    Dict):


						"""simple docstring"""




						__UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	: Optional[Any]		=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(UpperCamelCase_      ,  UpperCamelCase_      ,  UpperCamelCase_)


			def       a_     (      self      :    int):


						"""simple docstring"""




						__UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	: Optional[int]		=					self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_model_as_decoder(UpperCamelCase_      ,  UpperCamelCase_      ,  UpperCamelCase_)


			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




						__UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	: Union[str, Any]		=					self.model_tester.prepare_config_and_inputs_for_decoder()

						__UpperCAmelCase	: str		=					None

						self.model_tester.create_and_check_model_as_decoder(UpperCamelCase_      ,  UpperCamelCase_      ,  UpperCamelCase_)


			def       a_     (      self      :    Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	: List[str]		=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_decoder_model_past_large_inputs(UpperCamelCase_      ,  UpperCamelCase_      ,  UpperCamelCase_)


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_causal_lm(*UpperCamelCase_)


			@slow
			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: Dict		=					"abeja/gpt-neox-japanese-2.7b"

						__UpperCAmelCase	: List[str]		=					["データサイエンティストとは、", "100年後に必要とされる会社は、", "フルリモートの環境で働くために必要なことは、", "国境の長いトンネルを抜けると", "美味しい日本食といえば、"]

						__UpperCAmelCase	: Any		=					[
						    "データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。",
						    "100年後に必要とされる会社は、「人」が中心の会社です。",
						    "フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。",
						    "国境の長いトンネルを抜けると、そこは雪国だった。",
						    "美味しい日本食といえば、やっぱりお寿司ですよね。",
						]

						__UpperCAmelCase	: str		=					GPTNeoXJapaneseTokenizer.from_pretrained(UpperCamelCase_)
						__UpperCAmelCase	: Optional[Any]		=					GPTNeoXJapaneseForCausalLM.from_pretrained(UpperCamelCase_)

						__UpperCAmelCase	: Tuple		=					[]
						for prompt in prompts:
									__UpperCAmelCase	: List[str]		=					tokenizer(UpperCamelCase_      ,  return_tensors="pt").input_ids
									__UpperCAmelCase	: Tuple		=					model.generate(UpperCamelCase_      ,  max_length=50)
									__UpperCAmelCase	: List[Any]		=					tokenizer.batch_decode(UpperCamelCase_      ,  skip_special_tokens=UpperCamelCase_)
									predicted_outputs += generated_string
						self.assertListEqual(UpperCamelCase_      ,  UpperCamelCase_)








"""simple docstring"""





from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


A   =       logging.get_logger(__name__)

A   =       {
    """microsoft/table-transformer-detection""": (
        """https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json"""
    ),
}



class      a__				(	__magic_name__   ):
			lowercase_    =	"table-transformer"
			lowercase_    =	["past_key_values"]
			lowercase_    =	{
			    "hidden_size": "d_model",
			    "num_attention_heads": "encoder_attention_heads",
			}


			def __init__(      self      :    List[Any]      ,  UpperCamelCase_      :    int=True      ,  UpperCamelCase_      :    Tuple=None      ,  UpperCamelCase_      :    Optional[int]=3      ,  UpperCamelCase_      :    Any=100      ,  UpperCamelCase_      :    Dict=6      ,  UpperCamelCase_      :    List[str]=2048      ,  UpperCamelCase_      :    str=8      ,  UpperCamelCase_      :    Any=6      ,  UpperCamelCase_      :    Dict=2048      ,  UpperCamelCase_      :    str=8      ,  UpperCamelCase_      :    Any=0.0      ,  UpperCamelCase_      :    str=0.0      ,  UpperCamelCase_      :    Union[str, Any]=True      ,  UpperCamelCase_      :    Optional[int]="relu"      ,  UpperCamelCase_      :    str=256      ,  UpperCamelCase_      :    str=0.1      ,  UpperCamelCase_      :    Tuple=0.0      ,  UpperCamelCase_      :    Dict=0.0      ,  UpperCamelCase_      :    Dict=0.02      ,  UpperCamelCase_      :    Optional[Any]=1.0      ,  UpperCamelCase_      :    Optional[int]=False      ,  UpperCamelCase_      :    int="sine"      ,  UpperCamelCase_      :    Optional[Any]="resnet50"      ,  UpperCamelCase_      :    List[str]=True      ,  UpperCamelCase_      :    int=False      ,  UpperCamelCase_      :    Tuple=1      ,  UpperCamelCase_      :    Any=5      ,  UpperCamelCase_      :    Optional[Any]=2      ,  UpperCamelCase_      :    int=1      ,  UpperCamelCase_      :    List[str]=1      ,  UpperCamelCase_      :    List[str]=5      ,  UpperCamelCase_      :    Union[str, Any]=2      ,  UpperCamelCase_      :    Optional[int]=0.1      ,  **UpperCamelCase_      :    int      ,  ):


						"""simple docstring"""




						if backbone_config is not None and use_timm_backbone:
									raise ValueError("You can't specify both `backbone_config` and `use_timm_backbone`.")

						if not use_timm_backbone:
									if backbone_config is None:
												logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.")
												__UpperCAmelCase	: int		=					CONFIG_MAPPING["resnet"](out_features=["stage4"])
									elif isinstance(UpperCamelCase_      ,  UpperCamelCase_):
												__UpperCAmelCase	: List[Any]		=					backbone_config.get("model_type")
												__UpperCAmelCase	: List[Any]		=					CONFIG_MAPPING[backbone_model_type]
												__UpperCAmelCase	: Optional[int]		=					config_class.from_dict(UpperCamelCase_)
									# set timm attributes to None
									__UpperCAmelCase	,     __UpperCAmelCase	,     __UpperCAmelCase	: List[Any]		=					None, None, None

						__UpperCAmelCase	: str		=					use_timm_backbone
						__UpperCAmelCase	: int		=					backbone_config
						__UpperCAmelCase	: Dict		=					num_channels
						__UpperCAmelCase	: Optional[Any]		=					num_queries
						__UpperCAmelCase	: List[Any]		=					d_model
						__UpperCAmelCase	: Union[str, Any]		=					encoder_ffn_dim
						__UpperCAmelCase	: int		=					encoder_layers
						__UpperCAmelCase	: Union[str, Any]		=					encoder_attention_heads
						__UpperCAmelCase	: str		=					decoder_ffn_dim
						__UpperCAmelCase	: Any		=					decoder_layers
						__UpperCAmelCase	: Dict		=					decoder_attention_heads
						__UpperCAmelCase	: List[Any]		=					dropout
						__UpperCAmelCase	: int		=					attention_dropout
						__UpperCAmelCase	: str		=					activation_dropout
						__UpperCAmelCase	: Union[str, Any]		=					activation_function
						__UpperCAmelCase	: Optional[int]		=					init_std
						__UpperCAmelCase	: Dict		=					init_xavier_std
						__UpperCAmelCase	: Tuple		=					encoder_layerdrop
						__UpperCAmelCase	: Any		=					decoder_layerdrop
						__UpperCAmelCase	: Tuple		=					encoder_layers
						__UpperCAmelCase	: Optional[int]		=					auxiliary_loss
						__UpperCAmelCase	: List[str]		=					position_embedding_type
						__UpperCAmelCase	: List[str]		=					backbone
						__UpperCAmelCase	: Optional[int]		=					use_pretrained_backbone
						__UpperCAmelCase	: Union[str, Any]		=					dilation
						# Hungarian matcher
						__UpperCAmelCase	: List[Any]		=					class_cost
						__UpperCAmelCase	: Tuple		=					bbox_cost
						__UpperCAmelCase	: int		=					giou_cost
						# Loss coefficients
						__UpperCAmelCase	: Any		=					mask_loss_coefficient
						__UpperCAmelCase	: Optional[Any]		=					dice_loss_coefficient
						__UpperCAmelCase	: Any		=					bbox_loss_coefficient
						__UpperCAmelCase	: int		=					giou_loss_coefficient
						__UpperCAmelCase	: List[Any]		=					eos_coefficient
						super().__init__(is_encoder_decoder=UpperCamelCase_      ,  **UpperCamelCase_)


			@property
			def       a_     (      self      :    int):


						"""simple docstring"""




						return self.encoder_attention_heads


			@property
			def       a_     (      self      :    int):


						"""simple docstring"""




						return self.d_model






class      a__				(	__magic_name__   ):
			lowercase_    =	version.parse("1.11"   )


			@property
			def       a_     (      self      :    Union[str, Any]):


						"""simple docstring"""




						return OrderedDict(
						    [
						        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
						        ("pixel_mask", {0: "batch"}),
						    ])


			@property
			def       a_     (      self      :    int):


						"""simple docstring"""




						return 1e-5


			@property
			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						return 12


