





import os
import sys
import warnings
from dataclasses import dataclass, field
from io import BytesIO
from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union

import numpy as np
import pyarrow as pa

from .. import config
from ..download.streaming_download_manager import xopen
from ..table import array_cast
from ..utils.file_utils import is_local_path
from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict


if TYPE_CHECKING:
			import PIL.Image

			from .features import FeatureType


_UpperCAmelCase			:		Optional[List[str]]   				=    None
_UpperCAmelCase			:		Tuple   				=    '<' if sys.byteorder == 'little' else '>'
# Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image
_UpperCAmelCase			:		int   				=    [
    np.dtype("|b1"),
    np.dtype("|u1"),
    np.dtype("<u2"),
    np.dtype(">u2"),
    np.dtype("<i2"),
    np.dtype(">i2"),
    np.dtype("<u4"),
    np.dtype(">u4"),
    np.dtype("<i4"),
    np.dtype(">i4"),
    np.dtype("<f4"),
    np.dtype(">f4"),
    np.dtype("<f8"),
    np.dtype(">f8"),
]
@dataclass
class  lowerCAmelCase_       :
							UpperCamelCase_		:bool		      =    True
							UpperCamelCase_		:Optional[str]		      =    None
							# Automatically constructed
							UpperCamelCase_		:ClassVar[str]		      =    "PIL.Image.Image"
							UpperCamelCase_		:ClassVar[Any]		      =    pa.struct({'bytes': pa.binary(), 'path': pa.string()}					)
							UpperCamelCase_		:str		      =    field(default='Image'     ,    init=snake_case__     ,    repr=snake_case__					)




							def __call__(   self     :   int	):
											return self.pa_type




							def 						__snake_case					(   self     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Union[str, bytes, dict, np.ndarray, "PIL.Image.Image"]	):
											if config.PIL_AVAILABLE:
															import PIL.Image
											else:
															raise ImportError('''To support encoding images, please install \'Pillow\'.'''	)

											if isinstance(UpperCAmelCase__ ,							UpperCAmelCase__	):
															lowerCAmelCase__      			=						np.array(UpperCAmelCase__	)

											if isinstance(UpperCAmelCase__ ,							UpperCAmelCase__	):
															return {"path": value, "bytes": None}
											elif isinstance(UpperCAmelCase__ ,							UpperCAmelCase__	):
															return {"path": None, "bytes": value}
											elif isinstance(UpperCAmelCase__ ,							np.ndarray	):
															# convert the image array to PNG/TIFF bytes
															return encode_np_array(UpperCAmelCase__	)
											elif isinstance(UpperCAmelCase__ ,							PIL.Image.Image	):
															# convert the PIL image to bytes (default format is PNG/TIFF)
															return encode_pil_image(UpperCAmelCase__	)
											elif value.get('''path'''	) is not None and os.path.isfile(value['''path''']	):
															# we set "bytes": None to not duplicate the data if they're already available locally
															return {"bytes": None, "path": value.get('''path'''	)}
											elif value.get('''bytes'''	) is not None or value.get('''path'''	) is not None:
															# store the image bytes, and path is used to infer the image format using the file extension
															return {"bytes": value.get('''bytes'''	), "path": value.get('''path'''	)}
											else:
															raise ValueError(
															    f'An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.'	)




							def 						__snake_case					(   self     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   dict ,							SCREAMING_SNAKE_CASE_     :   Any=None	):
											if not self.decode:
															raise RuntimeError('''Decoding is disabled for this feature. Please use Image(decode=True) instead.'''	)

											if config.PIL_AVAILABLE:
															import PIL.Image
											else:
															raise ImportError('''To support decoding images, please install \'Pillow\'.'''	)

											if token_per_repo_id is None:
															lowerCAmelCase__      			=						{}

											lowerCAmelCase__      			=						value['''path'''], value['''bytes''']
											if bytes_ is None:
															if path is None:
																			raise ValueError(f'An image should have one of \'path\' or \'bytes\' but both are None in {value}.'	)
															else:
																			if is_local_path(UpperCAmelCase__	):
																							lowerCAmelCase__      			=						PIL.Image.open(UpperCAmelCase__	)
																			else:
																							lowerCAmelCase__      			=						path.split('''::'''	)[-1]
																							try:
																											lowerCAmelCase__      			=						string_to_dict(UpperCAmelCase__ ,							config.HUB_DATASETS_URL	)['''repo_id''']
																											lowerCAmelCase__      			=						token_per_repo_id.get(UpperCAmelCase__	)
																							except ValueError:
																											lowerCAmelCase__      			=						None
																							with xopen(UpperCAmelCase__ ,							'''rb''' ,							use_auth_token=UpperCAmelCase__	) as f:
																											lowerCAmelCase__      			=						BytesIO(f.read()	)
																							lowerCAmelCase__      			=						PIL.Image.open(bytes_	)
											else:
															lowerCAmelCase__      			=						PIL.Image.open(BytesIO(bytes_	)	)
											image.load()  # to avoid "Too many open files" errors
											return image




							def 						__snake_case					(   self     :   List[Any]	):
											from .features import Value

											return (
											    self
											    if self.decode
											    else {
											        "bytes": Value('''binary'''	),
											        "path": Value('''string'''	),
											    }
											)




							def 						__snake_case					(   self     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   Union[pa.StringArray, pa.StructArray, pa.ListArray]	):
											if pa.types.is_string(storage.type	):
															lowerCAmelCase__      			=						pa.array([None] * len(UpperCAmelCase__	) ,							type=pa.binary()	)
															lowerCAmelCase__      			=						pa.StructArray.from_arrays([bytes_array, storage] ,							['''bytes''', '''path'''] ,							mask=storage.is_null()	)
											elif pa.types.is_binary(storage.type	):
															lowerCAmelCase__      			=						pa.array([None] * len(UpperCAmelCase__	) ,							type=pa.string()	)
															lowerCAmelCase__      			=						pa.StructArray.from_arrays([storage, path_array] ,							['''bytes''', '''path'''] ,							mask=storage.is_null()	)
											elif pa.types.is_struct(storage.type	):
															if storage.type.get_field_index('''bytes'''	) >= 0:
																			lowerCAmelCase__      			=						storage.field('''bytes'''	)
															else:
																			lowerCAmelCase__      			=						pa.array([None] * len(UpperCAmelCase__	) ,							type=pa.binary()	)
															if storage.type.get_field_index('''path'''	) >= 0:
																			lowerCAmelCase__      			=						storage.field('''path'''	)
															else:
																			lowerCAmelCase__      			=						pa.array([None] * len(UpperCAmelCase__	) ,							type=pa.string()	)
															lowerCAmelCase__      			=						pa.StructArray.from_arrays([bytes_array, path_array] ,							['''bytes''', '''path'''] ,							mask=storage.is_null()	)
											elif pa.types.is_list(storage.type	):
															lowerCAmelCase__      			=						pa.array(
															    [encode_np_array(np.array(UpperCAmelCase__	)	)['''bytes'''] if arr is not None else None for arr in storage.to_pylist()] ,							type=pa.binary() ,							)
															lowerCAmelCase__      			=						pa.array([None] * len(UpperCAmelCase__	) ,							type=pa.string()	)
															lowerCAmelCase__      			=						pa.StructArray.from_arrays(
															    [bytes_array, path_array] ,							['''bytes''', '''path'''] ,							mask=bytes_array.is_null()	)
											return array_cast(UpperCAmelCase__ ,							self.pa_type	)




							def 						__snake_case					(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   pa.StructArray	):

											@no_op_if_value_is_null
											def path_to_bytes(SCREAMING_SNAKE_CASE_     :   List[Any]	):
															with xopen(UpperCAmelCase__ ,							'''rb'''	) as f:
																			lowerCAmelCase__      			=						f.read()
															return bytes_

											lowerCAmelCase__      			=						pa.array(
											    [
											        (path_to_bytes(x['''path''']	) if x['''bytes'''] is None else x['''bytes''']) if x is not None else None
											        for x in storage.to_pylist()
											    ] ,							type=pa.binary() ,							)
											lowerCAmelCase__      			=						pa.array(
											    [os.path.basename(UpperCAmelCase__	) if path is not None else None for path in storage.field('''path'''	).to_pylist()] ,							type=pa.string() ,							)
											lowerCAmelCase__      			=						pa.StructArray.from_arrays([bytes_array, path_array] ,							['''bytes''', '''path'''] ,							mask=bytes_array.is_null()	)
											return array_cast(UpperCAmelCase__ ,							self.pa_type	)





def 	lowerCAmelCase_		()		->    List[str]:


				'''simple docstring'''




				if config.PIL_AVAILABLE:
								import PIL.Image
				else:
								raise ImportError('''To support encoding images, please install \'Pillow\'.'''       )

				global _IMAGE_COMPRESSION_FORMATS
				if _IMAGE_COMPRESSION_FORMATS is None:
								PIL.Image.init()
								lowerCAmelCase__      			=						list(set(PIL.Image.OPEN.keys()       ) & set(PIL.Image.SAVE.keys()       )       )
				return _IMAGE_COMPRESSION_FORMATS





def 	lowerCAmelCase_		(lowercase__						:       Optional[Any]       )		->    bytes:


				'''simple docstring'''




				lowerCAmelCase__      			=						BytesIO()
				if image.format in list_image_compression_formats():
								lowerCAmelCase__      			=						image.format
				else:
								lowerCAmelCase__      			=						'''PNG''' if image.mode in ['''1''', '''L''', '''LA''', '''RGB''', '''RGBA'''] else '''TIFF'''
				image.save(lowercase__     ,					format=lowercase__       )
				return buffer.getvalue()





def 	lowerCAmelCase_		(lowercase__						:       Tuple       )		->    dict:


				'''simple docstring'''




				if hasattr(lowercase__     ,					'''filename'''       ) and image.filename != "":
								return {"path": image.filename, "bytes": None}
				else:
								return {"path": None, "bytes": image_to_bytes(lowercase__       )}





def 	lowerCAmelCase_		(lowercase__						:       str       )		->    dict:


				'''simple docstring'''




				if config.PIL_AVAILABLE:
								import PIL.Image
				else:
								raise ImportError('''To support encoding images, please install \'Pillow\'.'''       )

				lowerCAmelCase__      			=						array.dtype
				lowerCAmelCase__      			=						dtype.byteorder if dtype.byteorder != '''=''' else _NATIVE_BYTEORDER
				lowerCAmelCase__      			=						dtype.kind
				lowerCAmelCase__      			=						dtype.itemsize

				lowerCAmelCase__      			=						None

				# Multi-channel array case (only np.dtype("|u1") is allowed)
				if array.shape[2:]:
								lowerCAmelCase__      			=						np.dtype('''|u1'''       )
								if dtype_kind not in ["u", "i"]:
												raise TypeError(
												    f'Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.'       )
								if dtype is not dest_dtype:
												warnings.warn(f'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\''       )
    # Exact match
				elif dtype in _VALID_IMAGE_ARRAY_DTPYES:
								lowerCAmelCase__      			=						dtype
				else:  # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually)
								while dtype_itemsize >= 1:
												lowerCAmelCase__      			=						dtype_byteorder + dtype_kind + str(lowercase__       )
												lowerCAmelCase__      			=						np.dtype(lowercase__       )
												if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES:
																warnings.warn(f'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\''       )
																break
												else:
																dtype_itemsize //= 2

				if dest_dtype is None:
								raise TypeError(
								    f'Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}'       )
				lowerCAmelCase__      			=						PIL.Image.fromarray(array.astype(lowercase__       )       )
				return {"path": None, "bytes": image_to_bytes(lowercase__       )}





def 	lowerCAmelCase_		(lowercase__						:       str       )		->    List[dict]:


				'''simple docstring'''




				if config.PIL_AVAILABLE:
								import PIL.Image
				else:
								raise ImportError('''To support encoding images, please install \'Pillow\'.'''       )

				if objs:
								lowerCAmelCase__      			=						first_non_null_value(lowercase__       )
								if isinstance(lowercase__     ,					lowercase__       ):
												return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs]
								if isinstance(lowercase__     ,					np.ndarray       ):
												lowerCAmelCase__      			=						no_op_if_value_is_null(lowercase__       )
												return [obj_to_image_dict_func(lowercase__       ) for obj in objs]
								elif isinstance(lowercase__     ,					PIL.Image.Image       ):
												lowerCAmelCase__      			=						no_op_if_value_is_null(lowercase__       )
												return [obj_to_image_dict_func(lowercase__       ) for obj in objs]
								else:
												return objs
				else:
								return objs













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




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




							def 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						ids_tensor([self.batch_size, self.seq_length] ,							self.vocab_size	)

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

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

											lowerCAmelCase__      			=						self.get_config()

											return config, input_ids, input_mask, token_labels




							def 						__snake_case					(   self     :   List[str]	):
											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=SCREAMING_SNAKE_CASE_ ,							initializer_range=self.initializer_range ,							)




							def 						__snake_case					(   self     :   str	):
											(
											    (
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		
											)      			=						self.prepare_config_and_inputs()

											lowerCAmelCase__      			=						True
											lowerCAmelCase__      			=						floats_tensor([self.batch_size, self.seq_length, self.hidden_size]	)
											lowerCAmelCase__      			=						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 						__snake_case					(   self     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							**SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							):
											lowerCAmelCase__      			=						BertGenerationEncoder(config=SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, self.seq_length, self.hidden_size)	)




							def 						__snake_case					(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							**SCREAMING_SNAKE_CASE_     :   str ,							):
											lowerCAmelCase__      			=						True
											lowerCAmelCase__      			=						BertGenerationEncoder(config=SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(
											    SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_ ,							encoder_hidden_states=SCREAMING_SNAKE_CASE_ ,							encoder_attention_mask=SCREAMING_SNAKE_CASE_ ,							)
											lowerCAmelCase__      			=						model(
											    SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_ ,							encoder_hidden_states=SCREAMING_SNAKE_CASE_ ,							)
											self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, self.seq_length, self.hidden_size)	)




							def 						__snake_case					(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							**SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							):
											lowerCAmelCase__      			=						True
											lowerCAmelCase__      			=						True
											lowerCAmelCase__      			=						BertGenerationDecoder(config=SCREAMING_SNAKE_CASE_	).to(SCREAMING_SNAKE_CASE_	).eval()

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

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

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

											lowerCAmelCase__      			=						model(
											    SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_ ,							encoder_hidden_states=SCREAMING_SNAKE_CASE_ ,							encoder_attention_mask=SCREAMING_SNAKE_CASE_ ,							output_hidden_states=SCREAMING_SNAKE_CASE_ ,							)['''hidden_states'''][0]
											lowerCAmelCase__      			=						model(
											    SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_ ,							encoder_hidden_states=SCREAMING_SNAKE_CASE_ ,							encoder_attention_mask=SCREAMING_SNAKE_CASE_ ,							past_key_values=SCREAMING_SNAKE_CASE_ ,							output_hidden_states=SCREAMING_SNAKE_CASE_ ,							)['''hidden_states'''][0]

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




							def 						__snake_case					(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							*SCREAMING_SNAKE_CASE_     :   Tuple ,							):
											lowerCAmelCase__      			=						BertGenerationDecoder(SCREAMING_SNAKE_CASE_	)
											model.to(SCREAMING_SNAKE_CASE_	)
											model.eval()
											lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_ ,							attention_mask=SCREAMING_SNAKE_CASE_ ,							labels=SCREAMING_SNAKE_CASE_	)
											self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.seq_length, self.vocab_size)	)




							def 						__snake_case					(   self     :   List[Any]	):
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						self.prepare_config_and_inputs()
											lowerCAmelCase__      			=						{'''input_ids''': input_ids, '''attention_mask''': input_mask}
											return config, inputs_dict



@require_torch
class  lowerCAmelCase_       (				snake_case__     ,    snake_case__     ,    snake_case__     ,    unittest.TestCase					):
							UpperCamelCase_		:Dict		      =    (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
							UpperCamelCase_		:str		      =    (BertGenerationDecoder,) if is_torch_available() else ()
							UpperCamelCase_		:List[str]		      =    (
							    {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder}
							    if is_torch_available()
							    else {}
							)




							def 						__snake_case					(   self     :   List[str]	):
											lowerCAmelCase__      			=						BertGenerationEncoderTester(self	)
											lowerCAmelCase__      			=						ConfigTester(self ,							config_class=SCREAMING_SNAKE_CASE_ ,							hidden_size=37	)




							def 						__snake_case					(   self     :   Optional[Any]	):
											self.config_tester.run_common_tests()




							def 						__snake_case					(   self     :   List[Any]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   str	):
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											lowerCAmelCase__      			=						'''bert'''
											self.model_tester.create_and_check_model(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_model_as_decoder(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   List[Any]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Any	):
											# This regression test was failing with PyTorch < 1.3
											(
											    (
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		(
											    lowerCAmelCase__
											)			,		
											)      			=						self.model_tester.prepare_config_and_inputs_for_decoder()

											lowerCAmelCase__      			=						None

											self.model_tester.create_and_check_model_as_decoder(
											    SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							)




							def 						__snake_case					(   self     :   int	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_for_causal_lm(*SCREAMING_SNAKE_CASE_	)




							@slow
							def 						__snake_case					(   self     :   str	):
											lowerCAmelCase__      			=						BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''	)
											self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)



@require_torch
class  lowerCAmelCase_       (				unittest.TestCase					):




							@slow
							def 						__snake_case					(   self     :   Tuple	):
											lowerCAmelCase__      			=						BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''	)
											lowerCAmelCase__      			=						torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]	)
											with torch.no_grad():
															lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)[0]
											lowerCAmelCase__      			=						torch.Size([1, 8, 1_024]	)
											self.assertEqual(output.shape ,							SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						torch.tensor(
											    [[[0.1_775, 0.0_083, -0.0_321], [1.6_002, 0.1_287, 0.3_912], [2.1_473, 0.5_791, 0.6_066]]]	)
											self.assertTrue(torch.allclose(output[:, :3, :3] ,							SCREAMING_SNAKE_CASE_ ,							atol=1e-4	)	)



@require_torch
class  lowerCAmelCase_       (				unittest.TestCase					):




							@slow
							def 						__snake_case					(   self     :   Any	):
											lowerCAmelCase__      			=						BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''	)
											lowerCAmelCase__      			=						torch.tensor([[101, 7_592, 1_010, 2_026, 3_899, 2_003, 10_140, 102]]	)
											with torch.no_grad():
															lowerCAmelCase__      			=						model(SCREAMING_SNAKE_CASE_	)[0]
											lowerCAmelCase__      			=						torch.Size([1, 8, 50_358]	)
											self.assertEqual(output.shape ,							SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						torch.tensor(
											    [[[-0.5_788, -2.5_994, -3.7_054], [0.0_438, 4.7_997, 1.8_795], [1.5_862, 6.6_409, 4.4_638]]]	)
											self.assertTrue(torch.allclose(output[:, :3, :3] ,							SCREAMING_SNAKE_CASE_ ,							atol=1e-4	)	)





