





import argparse
import json
import os
import sys
import tempfile
import unittest
from argparse import Namespace
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import List, Literal, Optional

import yaml

from transformers import HfArgumentParser, TrainingArguments
from transformers.hf_argparser import make_choice_type_function, string_to_bool


# Since Python 3.10, we can use the builtin `|` operator for Union types
# See PEP 604: https://peps.python.org/pep-0604
snake_case 				=       sys.version_info >= (3, 1_0)

def 							SCREAMING_SNAKE_CASE__       (  snake_case__  :Optional[int]=None			,							snake_case__  :List[Any]=None     )      ->						List[Any]:
  return field(default_factory=lambda: default			,							metadata=snake_case__     )





@dataclass
class 				A_				:







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : int
       SCREAMING_SNAKE_CASE_      : float
       SCREAMING_SNAKE_CASE_      : str
       SCREAMING_SNAKE_CASE_      : bool





@dataclass
class 				A_				:







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : int      =  4_2
       SCREAMING_SNAKE_CASE_      : str      =  field(default='''toto'''					,	metadata={'''help''': '''help message'''}	)





@dataclass
class 				A_				:







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : bool      =  False
       SCREAMING_SNAKE_CASE_      : bool      =  True
       SCREAMING_SNAKE_CASE_      : Optional[bool]      =  None





class 				A_				(      UpperCAmelCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : Tuple      =  '''titi'''
       SCREAMING_SNAKE_CASE_      : int      =  '''toto'''





class 				A_				(      UpperCAmelCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : List[str]      =  '''titi'''
       SCREAMING_SNAKE_CASE_      : Dict      =  '''toto'''
       SCREAMING_SNAKE_CASE_      : List[str]      =  4_2





@dataclass
class 				A_				:







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : BasicEnum      =  "toto"







       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					Optional[Any]:
         _lowercase							= BasicEnum(self.foo  )





@dataclass
class 				A_				:







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : MixedTypeEnum      =  "toto"







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					Optional[int]:
         _lowercase							= MixedTypeEnum(self.foo  )





@dataclass
class 				A_				:







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : Optional[int]      =  None
       SCREAMING_SNAKE_CASE_      : Optional[float]      =  field(default=UpperCAmelCase					,	metadata={'''help''': '''help message'''}	)
       SCREAMING_SNAKE_CASE_      : Optional[str]      =  None
       SCREAMING_SNAKE_CASE_      : Optional[List[str]]      =  list_field(default=[]	)
       SCREAMING_SNAKE_CASE_      : Optional[List[int]]      =  list_field(default=[]	)





@dataclass
class 				A_				:







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : List[int]      =  list_field(default=[]	)
       SCREAMING_SNAKE_CASE_      : List[int]      =  list_field(default=[1, 2, 3]	)
       SCREAMING_SNAKE_CASE_      : List[str]      =  list_field(default=['''Hallo''', '''Bonjour''', '''Hello''']	)
       SCREAMING_SNAKE_CASE_      : List[float]      =  list_field(default=[0.1, 0.2, 0.3]	)





@dataclass
class 				A_				:







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : List[int]      =  field()
       SCREAMING_SNAKE_CASE_      : str      =  field()
       SCREAMING_SNAKE_CASE_      : BasicEnum      =  field()







       def    __UpperCAmelCase (    self	:	List[str]  )						->					Any:
         _lowercase							= BasicEnum(self.required_enum  )





@dataclass
class 				A_				:







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : int
       SCREAMING_SNAKE_CASE_      : "BasicEnum"      =  field()
       SCREAMING_SNAKE_CASE_      : "Optional[bool]"      =  None
       SCREAMING_SNAKE_CASE_      : "str"      =  field(default='''toto'''					,	metadata={'''help''': '''help message'''}	)
       SCREAMING_SNAKE_CASE_      : "List[str]"      =  list_field(default=['''Hallo''', '''Bonjour''', '''Hello''']	)


if is_python_no_less_than_3_10:





      @dataclass
      class 				A_				:







             """simple docstring"""





             SCREAMING_SNAKE_CASE_      : bool      =  False
             SCREAMING_SNAKE_CASE_      : bool      =  True
             SCREAMING_SNAKE_CASE_      : bool | None      =  None





      @dataclass
      class 				A_				:







             """simple docstring"""





             SCREAMING_SNAKE_CASE_      : int | None      =  None
             SCREAMING_SNAKE_CASE_      : float | None      =  field(default=UpperCAmelCase					,	metadata={'''help''': '''help message'''}	)
             SCREAMING_SNAKE_CASE_      : str | None      =  None
             SCREAMING_SNAKE_CASE_      : list[str] | None      =  list_field(default=[]	)
             SCREAMING_SNAKE_CASE_      : list[int] | None      =  list_field(default=[]	)







class 				A_				(      unittest.TestCase	):







       """simple docstring"""





       def    __UpperCAmelCase (    self	:	Tuple						,__A	:	argparse.ArgumentParser						,__A	:	argparse.ArgumentParser  )						->					List[Any]:
         self.assertEqual(len(a._actions  )						,len(b._actions  )  )
         for x, y in zip(a._actions						,b._actions  ):
           _lowercase							= {k: v for k, v in vars(__A  ).items() if k != 'container'}
           _lowercase							= {k: v for k, v in vars(__A  ).items() if k != 'container'}

           # Choices with mixed type have custom function as "type"
           # So we need to compare results directly for equality
           if xx.get('choices'						,__A  ) and yy.get('choices'						,__A  ):
             for expected_choice in yy["choices"] + xx["choices"]:
               self.assertEqual(xx['type'](__A  )						,yy['type'](__A  )  )
             del xx["type"], yy["type"]

           self.assertEqual(__A						,__A  )







       def    __UpperCAmelCase (    self	:	List[str]  )						->					Union[str, Any]:
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= argparse.ArgumentParser()
         expected.add_argument('--foo'						,type=__A						,required=__A  )
         expected.add_argument('--bar'						,type=__A						,required=__A  )
         expected.add_argument('--baz'						,type=__A						,required=__A  )
         expected.add_argument('--flag'						,type=__A						,default=__A						,const=__A						,nargs='?'  )
         self.argparsersEqual(__A						,__A  )

         _lowercase							= ['--foo', '1', '--baz', 'quux', '--bar', '0.5']
         ((_lowercase)						,		)							= parser.parse_args_into_dataclasses(__A						,look_for_args_file=__A  )
         self.assertFalse(example.flag  )







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					int:
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= argparse.ArgumentParser()
         expected.add_argument('--foo'						,default=42						,type=__A  )
         expected.add_argument('--baz'						,default='toto'						,type=__A						,help='help message'  )
         self.argparsersEqual(__A						,__A  )







       def    __UpperCAmelCase (    self	:	Any  )						->					Tuple:
         _lowercase							= argparse.ArgumentParser()
         expected.add_argument('--foo'						,type=__A						,default=__A						,const=__A						,nargs='?'  )
         expected.add_argument('--baz'						,type=__A						,default=__A						,const=__A						,nargs='?'  )
         # A boolean no_* argument always has to come after its "default: True" regular counter-part
         # and its default must be set to False
         expected.add_argument('--no_baz'						,action='store_false'						,default=__A						,dest='baz'  )
         expected.add_argument('--opt'						,type=__A						,default=__A  )

         _lowercase							= [WithDefaultBoolExample]
         if is_python_no_less_than_3_10:
           dataclass_types.append(__A  )

         for dataclass_type in dataclass_types:
           _lowercase							= HfArgumentParser(__A  )
           self.argparsersEqual(__A						,__A  )

           _lowercase							= parser.parse_args([]  )
           self.assertEqual(__A						,Namespace(foo=__A						,baz=__A						,opt=__A  )  )

           _lowercase							= parser.parse_args(['--foo', '--no_baz']  )
           self.assertEqual(__A						,Namespace(foo=__A						,baz=__A						,opt=__A  )  )

           _lowercase							= parser.parse_args(['--foo', '--baz']  )
           self.assertEqual(__A						,Namespace(foo=__A						,baz=__A						,opt=__A  )  )

           _lowercase							= parser.parse_args(['--foo', 'True', '--baz', 'True', '--opt', 'True']  )
           self.assertEqual(__A						,Namespace(foo=__A						,baz=__A						,opt=__A  )  )

           _lowercase							= parser.parse_args(['--foo', 'False', '--baz', 'False', '--opt', 'False']  )
           self.assertEqual(__A						,Namespace(foo=__A						,baz=__A						,opt=__A  )  )







       def    __UpperCAmelCase (    self	:	List[Any]  )						->					List[str]:
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= argparse.ArgumentParser()
         expected.add_argument(
             '--foo'						,default='toto'						,choices=['titi', 'toto', 42]						,type=make_choice_type_function(['titi', 'toto', 42]  )						,)
         self.argparsersEqual(__A						,__A  )

         _lowercase							= parser.parse_args([]  )
         self.assertEqual(args.foo						,'toto'  )
         _lowercase							= parser.parse_args_into_dataclasses([]  )[0]
         self.assertEqual(enum_ex.foo						,MixedTypeEnum.toto  )

         _lowercase							= parser.parse_args(['--foo', 'titi']  )
         self.assertEqual(args.foo						,'titi'  )
         _lowercase							= parser.parse_args_into_dataclasses(['--foo', 'titi']  )[0]
         self.assertEqual(enum_ex.foo						,MixedTypeEnum.titi  )

         _lowercase							= parser.parse_args(['--foo', '42']  )
         self.assertEqual(args.foo						,42  )
         _lowercase							= parser.parse_args_into_dataclasses(['--foo', '42']  )[0]
         self.assertEqual(enum_ex.foo						,MixedTypeEnum.fourtytwo  )







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					Tuple:





         @dataclass
         class 				A_				:







                """simple docstring"""





                SCREAMING_SNAKE_CASE_      : Literal["titi", "toto", 4_2]      =  "toto"
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= argparse.ArgumentParser()
         expected.add_argument(
             '--foo'						,default='toto'						,choices=('titi', 'toto', 42)						,type=make_choice_type_function(['titi', 'toto', 42]  )						,)
         self.argparsersEqual(__A						,__A  )

         _lowercase							= parser.parse_args([]  )
         self.assertEqual(args.foo						,'toto'  )

         _lowercase							= parser.parse_args(['--foo', 'titi']  )
         self.assertEqual(args.foo						,'titi'  )

         _lowercase							= parser.parse_args(['--foo', '42']  )
         self.assertEqual(args.foo						,42  )







       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					Tuple:
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= argparse.ArgumentParser()
         expected.add_argument('--foo_int'						,nargs='+'						,default=[]						,type=__A  )
         expected.add_argument('--bar_int'						,nargs='+'						,default=[1, 2, 3]						,type=__A  )
         expected.add_argument('--foo_str'						,nargs='+'						,default=['Hallo', 'Bonjour', 'Hello']						,type=__A  )
         expected.add_argument('--foo_float'						,nargs='+'						,default=[0.1, 0.2, 0.3]						,type=__A  )

         self.argparsersEqual(__A						,__A  )

         _lowercase							= parser.parse_args([]  )
         self.assertEqual(
             __A						,Namespace(foo_int=[]						,bar_int=[1, 2, 3]						,foo_str=['Hallo', 'Bonjour', 'Hello']						,foo_float=[0.1, 0.2, 0.3]  )						,)

         _lowercase							= parser.parse_args('--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7'.split()  )
         self.assertEqual(__A						,Namespace(foo_int=[1]						,bar_int=[2, 3]						,foo_str=['a', 'b', 'c']						,foo_float=[0.1, 0.7]  )  )







       def    __UpperCAmelCase (    self	:	int  )						->					Optional[int]:
         _lowercase							= argparse.ArgumentParser()
         expected.add_argument('--foo'						,default=__A						,type=__A  )
         expected.add_argument('--bar'						,default=__A						,type=__A						,help='help message'  )
         expected.add_argument('--baz'						,default=__A						,type=__A  )
         expected.add_argument('--ces'						,nargs='+'						,default=[]						,type=__A  )
         expected.add_argument('--des'						,nargs='+'						,default=[]						,type=__A  )

         _lowercase							= [OptionalExample]
         if is_python_no_less_than_3_10:
           dataclass_types.append(__A  )

         for dataclass_type in dataclass_types:
           _lowercase							= HfArgumentParser(__A  )

           self.argparsersEqual(__A						,__A  )

           _lowercase							= parser.parse_args([]  )
           self.assertEqual(__A						,Namespace(foo=__A						,bar=__A						,baz=__A						,ces=[]						,des=[]  )  )

           _lowercase							= parser.parse_args('--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'.split()  )
           self.assertEqual(__A						,Namespace(foo=12						,bar=3.14						,baz='42'						,ces=['a', 'b', 'c']						,des=[1, 2, 3]  )  )







       def    __UpperCAmelCase (    self	:	str  )						->					List[Any]:
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= argparse.ArgumentParser()
         expected.add_argument('--required_list'						,nargs='+'						,type=__A						,required=__A  )
         expected.add_argument('--required_str'						,type=__A						,required=__A  )
         expected.add_argument(
             '--required_enum'						,type=make_choice_type_function(['titi', 'toto']  )						,choices=['titi', 'toto']						,required=__A						,)
         self.argparsersEqual(__A						,__A  )







       def    __UpperCAmelCase (    self	:	Dict  )						->					Optional[int]:
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= argparse.ArgumentParser()
         expected.add_argument('--foo'						,type=__A						,required=__A  )
         expected.add_argument(
             '--required_enum'						,type=make_choice_type_function(['titi', 'toto']  )						,choices=['titi', 'toto']						,required=__A						,)
         expected.add_argument('--opt'						,type=__A						,default=__A  )
         expected.add_argument('--baz'						,default='toto'						,type=__A						,help='help message'  )
         expected.add_argument('--foo_str'						,nargs='+'						,default=['Hallo', 'Bonjour', 'Hello']						,type=__A  )
         self.argparsersEqual(__A						,__A  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					Dict:
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= {
             'foo': 12,
             'bar': 3.14,
             'baz': '42',
             'flag': True,
         }

         _lowercase							= parser.parse_dict(__A  )[0]
         _lowercase							= BasicExample(**__A  )
         self.assertEqual(__A						,__A  )







       def    __UpperCAmelCase (    self	:	int  )						->					Dict:
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= {
             'foo': 12,
             'bar': 3.14,
             'baz': '42',
             'flag': True,
             'extra': 42,
         }

         self.assertRaises(__A						,parser.parse_dict						,__A						,allow_extra_keys=__A  )







       def    __UpperCAmelCase (    self	:	Any  )						->					Optional[int]:
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= {
             'foo': 12,
             'bar': 3.14,
             'baz': '42',
             'flag': True,
         }
         with tempfile.TemporaryDirectory() as tmp_dir:
           _lowercase							= os.path.join(__A						,'temp_json'  )
           os.mkdir(__A  )
           with open(temp_local_path + '.json'						,'w+'  ) as f:
             json.dump(__A						,__A  )
           _lowercase							= parser.parse_yaml_file(Path(temp_local_path + '.json'  )  )[0]

         _lowercase							= BasicExample(**__A  )
         self.assertEqual(__A						,__A  )







       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					Optional[int]:
         _lowercase							= HfArgumentParser(__A  )

         _lowercase							= {
             'foo': 12,
             'bar': 3.14,
             'baz': '42',
             'flag': True,
         }
         with tempfile.TemporaryDirectory() as tmp_dir:
           _lowercase							= os.path.join(__A						,'temp_yaml'  )
           os.mkdir(__A  )
           with open(temp_local_path + '.yaml'						,'w+'  ) as f:
             yaml.dump(__A						,__A  )
           _lowercase							= parser.parse_yaml_file(Path(temp_local_path + '.yaml'  )  )[0]
         _lowercase							= BasicExample(**__A  )
         self.assertEqual(__A						,__A  )







       def    __UpperCAmelCase (    self	:	int  )						->					Any:
         _lowercase							= HfArgumentParser(__A  )
         self.assertIsNotNone(__A  )






import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
      import torch

      from transformers import BertGenerationDecoder, BertGenerationEncoder





class 				A_				:







       """simple docstring"""





       def __init__(    self	:	List[Any]						,__A	:	str						,__A	:	Dict=13						,__A	:	Dict=7						,__A	:	List[Any]=True						,__A	:	Union[str, Any]=True						,__A	:	Any=99						,__A	:	Optional[Any]=32						,__A	:	Any=5						,__A	:	int=4						,__A	:	Dict=37						,__A	:	Dict="gelu"						,__A	:	Union[str, Any]=0.1						,__A	:	List[Any]=0.1						,__A	:	str=50						,__A	:	str=0.02						,__A	:	str=True						,__A	:	List[Any]=None						,)						->					Optional[Any]:
         _lowercase							= parent
         _lowercase							= batch_size
         _lowercase							= seq_length
         _lowercase							= is_training
         _lowercase							= use_input_mask
         _lowercase							= vocab_size
         _lowercase							= hidden_size
         _lowercase							= num_hidden_layers
         _lowercase							= num_attention_heads
         _lowercase							= intermediate_size
         _lowercase							= hidden_act
         _lowercase							= hidden_dropout_prob
         _lowercase							= attention_probs_dropout_prob
         _lowercase							= max_position_embeddings
         _lowercase							= initializer_range
         _lowercase							= use_labels
         _lowercase							= scope







       def    __UpperCAmelCase (    self	:	int  )						->					Optional[Any]:
         _lowercase							= ids_tensor([self.batch_size, self.seq_length]						,self.vocab_size  )

         _lowercase							= None
         if self.use_input_mask:
           _lowercase							= random_attention_mask([self.batch_size, self.seq_length]  )

         if self.use_labels:
           _lowercase							= ids_tensor([self.batch_size, self.seq_length]						,self.vocab_size  )

         _lowercase							= self.get_config()

         return config, input_ids, input_mask, token_labels







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					int:
         return BertGenerationConfig(
             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_size=self.intermediate_size						,hidden_act=self.hidden_act						,hidden_dropout_prob=self.hidden_dropout_prob						,attention_probs_dropout_prob=self.attention_probs_dropout_prob						,max_position_embeddings=self.max_position_embeddings						,is_decoder=__A						,initializer_range=self.initializer_range						,)







       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					int:
         (
             (
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		
         )							= self.prepare_config_and_inputs()

         _lowercase							= True
         _lowercase							= floats_tensor([self.batch_size, self.seq_length, self.hidden_size]  )
         _lowercase							= ids_tensor([self.batch_size, self.seq_length]						,vocab_size=2  )

         return (
             config,
             input_ids,
             input_mask,
             token_labels,
             encoder_hidden_states,
             encoder_attention_mask,
         )







       def    __UpperCAmelCase (    self	:	Dict						,__A	:	List[Any]						,__A	:	Optional[int]						,__A	:	Optional[int]						,__A	:	int						,**__A	:	Union[str, Any]						,)						->					List[Any]:
         _lowercase							= BertGenerationEncoder(config=__A  )
         model.to(__A  )
         model.eval()
         _lowercase							= model(__A						,attention_mask=__A  )
         _lowercase							= model(__A  )
         self.parent.assertEqual(result.last_hidden_state.shape						,(self.batch_size, self.seq_length, self.hidden_size)  )







       def    __UpperCAmelCase (    self	:	List[str]						,__A	:	Optional[Any]						,__A	:	int						,__A	:	Dict						,__A	:	int						,__A	:	Tuple						,__A	:	str						,**__A	:	int						,)						->					Optional[Any]:
         _lowercase							= True
         _lowercase							= BertGenerationEncoder(config=__A  )
         model.to(__A  )
         model.eval()
         _lowercase							= model(
             __A						,attention_mask=__A						,encoder_hidden_states=__A						,encoder_attention_mask=__A						,)
         _lowercase							= model(
             __A						,attention_mask=__A						,encoder_hidden_states=__A						,)
         self.parent.assertEqual(result.last_hidden_state.shape						,(self.batch_size, self.seq_length, self.hidden_size)  )







       def    __UpperCAmelCase (    self	:	List[str]						,__A	:	List[str]						,__A	:	List[str]						,__A	:	Optional[Any]						,__A	:	List[Any]						,__A	:	Optional[int]						,__A	:	Optional[int]						,**__A	:	Dict						,)						->					List[Any]:
         _lowercase							= True
         _lowercase							= True
         _lowercase							= BertGenerationDecoder(config=__A  ).to(__A  ).eval()

         # first forward pass
         _lowercase							= model(
             __A						,attention_mask=__A						,encoder_hidden_states=__A						,encoder_attention_mask=__A						,use_cache=__A						,)
         _lowercase							= outputs.past_key_values

         # create hypothetical multiple next token and extent to next_input_ids
         _lowercase							= ids_tensor((self.batch_size, 3)						,config.vocab_size  )
         _lowercase							= ids_tensor((self.batch_size, 3)						,vocab_size=2  )

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

         _lowercase							= model(
             __A						,attention_mask=__A						,encoder_hidden_states=__A						,encoder_attention_mask=__A						,output_hidden_states=__A						,)['hidden_states'][0]
         _lowercase							= model(
             __A						,attention_mask=__A						,encoder_hidden_states=__A						,encoder_attention_mask=__A						,past_key_values=__A						,output_hidden_states=__A						,)['hidden_states'][0]

         # select random slice
         _lowercase							= ids_tensor((1,)						,output_from_past.shape[-1]  ).item()
         _lowercase							= output_from_no_past[:, -3:, random_slice_idx].detach()
         _lowercase							= 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(__A						,__A						,atol=1e-3  )  )







       def    __UpperCAmelCase (    self	:	List[str]						,__A	:	List[str]						,__A	:	List[str]						,__A	:	List[Any]						,__A	:	List[str]						,*__A	:	List[Any]						,)						->					Tuple:
         _lowercase							= BertGenerationDecoder(__A  )
         model.to(__A  )
         model.eval()
         _lowercase							= model(__A						,attention_mask=__A						,labels=__A  )
         self.parent.assertEqual(result.logits.shape						,(self.batch_size, self.seq_length, self.vocab_size)  )







       def    __UpperCAmelCase (    self	:	List[Any]  )						->					Dict:
         _lowercase						,		_lowercase						,		_lowercase						,		_lowercase							= self.prepare_config_and_inputs()
         _lowercase							= {'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict





@require_torch
class 				A_				(      UpperCAmelCase					,	UpperCAmelCase					,	UpperCAmelCase					,	unittest.TestCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : List[str]      =  (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
       SCREAMING_SNAKE_CASE_      : Union[str, Any]      =  (BertGenerationDecoder,) if is_torch_available() else ()
       SCREAMING_SNAKE_CASE_      : Dict      =  (
           {'''feature-extraction''': BertGenerationEncoder, '''text-generation''': BertGenerationDecoder}
           if is_torch_available()
           else {}
       )







       def    __UpperCAmelCase (    self	:	Any  )						->					Union[str, Any]:
         _lowercase							= BertGenerationEncoderTester(self  )
         _lowercase							= ConfigTester(self						,config_class=__A						,hidden_size=37  )







       def    __UpperCAmelCase (    self	:	int  )						->					int:
         self.config_tester.run_common_tests()







       def    __UpperCAmelCase (    self	:	Tuple  )						->					Any:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__A  )







       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					Dict:
         _lowercase						,		_lowercase						,		_lowercase						,		_lowercase							= self.model_tester.prepare_config_and_inputs()
         _lowercase							= 'bert'
         self.model_tester.create_and_check_model(__A						,__A						,__A						,__A  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					List[Any]:
         _lowercase							= self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_model_as_decoder(*__A  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					Union[str, Any]:
         _lowercase							= self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_decoder_model_past_large_inputs(*__A  )







       def    __UpperCAmelCase (    self	:	Any  )						->					Union[str, Any]:
         # This regression test was failing with PyTorch < 1.3
         (
             (
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		(
             _lowercase
         )						,		
         )							= self.model_tester.prepare_config_and_inputs_for_decoder()

         _lowercase							= None

         self.model_tester.create_and_check_model_as_decoder(
             __A						,__A						,__A						,__A						,__A						,__A						,)







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Union[str, Any]:
         _lowercase							= self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_for_causal_lm(*__A  )







       @slow
       def    __UpperCAmelCase (    self	:	List[Any]  )						->					str:
         _lowercase							= BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'  )
         self.assertIsNotNone(__A  )





@require_torch
class 				A_				(      unittest.TestCase	):







       """simple docstring"""





       @slow
       def    __UpperCAmelCase (    self	:	Tuple  )						->					Union[str, Any]:
         _lowercase							= BertGenerationEncoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'  )
         _lowercase							= torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 1_0140, 102]]  )
         with torch.no_grad():
           _lowercase							= model(__A  )[0]
         _lowercase							= torch.Size([1, 8, 1024]  )
         self.assertEqual(output.shape						,__A  )
         _lowercase							= torch.tensor(
             [[[0.1775, 0.0083, -0.0321], [1.6002, 0.1287, 0.3912], [2.1473, 0.5791, 0.6066]]]  )
         self.assertTrue(torch.allclose(output[:, :3, :3]						,__A						,atol=1e-4  )  )





@require_torch
class 				A_				(      unittest.TestCase	):







       """simple docstring"""





       @slow
       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Union[str, Any]:
         _lowercase							= BertGenerationDecoder.from_pretrained('google/bert_for_seq_generation_L-24_bbc_encoder'  )
         _lowercase							= torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 1_0140, 102]]  )
         with torch.no_grad():
           _lowercase							= model(__A  )[0]
         _lowercase							= torch.Size([1, 8, 5_0358]  )
         self.assertEqual(output.shape						,__A  )
         _lowercase							= torch.tensor(
             [[[-0.5788, -2.5994, -3.7054], [0.0438, 4.7997, 1.8795], [1.5862, 6.6409, 4.4638]]]  )
         self.assertTrue(torch.allclose(output[:, :3, :3]						,__A						,atol=1e-4  )  )