


from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer
@dataclass
class 	UpperCAmelCase_					(					UpperCamelCase    ):

   '''simple docstring'''




   __A :      torch.FloatTensor



class 	UpperCAmelCase_					(					UpperCamelCase		, UpperCamelCase    ):

   '''simple docstring'''




   @register_to_config
   def __init__(		self						,				__A = 3						,				__A = 3						,				__A = ("DownEncoderBlock2D",)						,				__A = ("UpDecoderBlock2D",)						,				__A = (64,)						,				__A = 1						,				__A = "silu"						,				__A = 3						,				__A = 32						,				__A = 256						,				__A = 32						,				__A = None						,				__A = 0.18215						,				__A = "group"						,				):






     """simple docstring"""





     super().__init__()

     # pass init params to Encoder
     lowerCamelCase :					Dict    =				Encoder(
         in_channels=__A						,				out_channels=__A						,				down_block_types=__A						,				block_out_channels=__A						,				layers_per_block=__A						,				act_fn=__A						,				norm_num_groups=__A						,				double_z=__A						,				)

     lowerCamelCase :					Union[str, Any]    =				vq_embed_dim if vq_embed_dim is not None else latent_channels

     lowerCamelCase :					Any    =				nn.Convad(__A						,				__A						,				1 )
     lowerCamelCase :					Dict    =				VectorQuantizer(__A						,				__A						,				beta=0.25						,				remap=__A						,				sane_index_shape=__A )
     lowerCamelCase :					Optional[int]    =				nn.Convad(__A						,				__A						,				1 )

     # pass init params to Decoder
     lowerCamelCase :					Optional[Any]    =				Decoder(
         in_channels=__A						,				out_channels=__A						,				up_block_types=__A						,				block_out_channels=__A						,				layers_per_block=__A						,				act_fn=__A						,				norm_num_groups=__A						,				norm_type=__A						,				)






   @apply_forward_hook
   def _snake_case    (		self						,				__A						,				__A = True ):






     """simple docstring"""





     lowerCamelCase :					Dict    =				self.encoder(__A )
     lowerCamelCase :					Union[str, Any]    =				self.quant_conv(__A )

     if not return_dict:
       return (h,)

     return VQEncoderOutput(latents=__A )






   @apply_forward_hook
   def _snake_case    (		self						,				__A						,				__A = False						,				__A = True ):






     """simple docstring"""





     if not force_not_quantize:
       lowerCamelCase    ,	lowerCamelCase    ,	lowerCamelCase :					Any    =				self.quantize(__A )
     else:
       lowerCamelCase :					Dict    =				h
     lowerCamelCase :					Tuple    =				self.post_quant_conv(__A )
     lowerCamelCase :					Any    =				self.decoder(__A						,				quant if self.config.norm_type == "spatial" else None )

     if not return_dict:
       return (dec,)

     return DecoderOutput(sample=__A )






   def _snake_case    (		self						,				__A						,				__A = True ):






     """simple docstring"""





     lowerCamelCase :					int    =				sample
     lowerCamelCase :					Optional[Any]    =				self.encode(__A ).latents
     lowerCamelCase :					List[str]    =				self.decode(__A ).sample

     if not return_dict:
       return (dec,)

     return DecoderOutput(sample=__A )







from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import numpy as np
   import tensorflow as tf

   from transformers import (
       TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
       FlaubertConfig,
       TFFlaubertForMultipleChoice,
       TFFlaubertForQuestionAnsweringSimple,
       TFFlaubertForSequenceClassification,
       TFFlaubertForTokenClassification,
       TFFlaubertModel,
       TFFlaubertWithLMHeadModel,
   )
class 	UpperCAmelCase_					:

   '''simple docstring'''




   def __init__(		self						,				__A						,				):






     """simple docstring"""





     lowerCamelCase :					str    =				parent
     lowerCamelCase :					Union[str, Any]    =				13
     lowerCamelCase :					Optional[Any]    =				7
     lowerCamelCase :					List[str]    =				True
     lowerCamelCase :					Optional[int]    =				True
     lowerCamelCase :					Union[str, Any]    =				True
     lowerCamelCase :					List[Any]    =				True
     lowerCamelCase :					Tuple    =				True
     lowerCamelCase :					Any    =				False
     lowerCamelCase :					int    =				False
     lowerCamelCase :					Tuple    =				False
     lowerCamelCase :					Union[str, Any]    =				2
     lowerCamelCase :					Dict    =				99
     lowerCamelCase :					Tuple    =				0
     lowerCamelCase :					Any    =				32
     lowerCamelCase :					List[Any]    =				2
     lowerCamelCase :					Tuple    =				4
     lowerCamelCase :					List[str]    =				0.1
     lowerCamelCase :					int    =				0.1
     lowerCamelCase :					int    =				512
     lowerCamelCase :					List[Any]    =				16
     lowerCamelCase :					Any    =				2
     lowerCamelCase :					Any    =				0.02
     lowerCamelCase :					List[str]    =				3
     lowerCamelCase :					Tuple    =				4
     lowerCamelCase :					int    =				"last"
     lowerCamelCase :					int    =				True
     lowerCamelCase :					Dict    =				None
     lowerCamelCase :					Tuple    =				0






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					str    =				ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size )
     lowerCamelCase :					Tuple    =				random_attention_mask([self.batch_size, self.seq_length]						,				dtype=tf.floataa )

     lowerCamelCase :					Tuple    =				None
     if self.use_input_lengths:
       lowerCamelCase :					Optional[Any]    =				(
           ids_tensor([self.batch_size]						,				vocab_size=2 ) + self.seq_length - 2
       )  # small variation of seq_length

     lowerCamelCase :					str    =				None
     if self.use_token_type_ids:
       lowerCamelCase :					Optional[int]    =				ids_tensor([self.batch_size, self.seq_length]						,				self.n_langs )

     lowerCamelCase :					Dict    =				None
     lowerCamelCase :					Dict    =				None
     lowerCamelCase :					Tuple    =				None
     if self.use_labels:
       lowerCamelCase :					str    =				ids_tensor([self.batch_size]						,				self.type_sequence_label_size )
       lowerCamelCase :					Union[str, Any]    =				ids_tensor([self.batch_size, self.seq_length]						,				self.num_labels )
       lowerCamelCase :					int    =				ids_tensor([self.batch_size]						,				2						,				dtype=tf.floataa )
       lowerCamelCase :					Union[str, Any]    =				ids_tensor([self.batch_size]						,				self.num_choices )

     lowerCamelCase :					List[Any]    =				FlaubertConfig(
         vocab_size=self.vocab_size						,				n_special=self.n_special						,				emb_dim=self.hidden_size						,				n_layers=self.num_hidden_layers						,				n_heads=self.num_attention_heads						,				dropout=self.hidden_dropout_prob						,				attention_dropout=self.attention_probs_dropout_prob						,				gelu_activation=self.gelu_activation						,				sinusoidal_embeddings=self.sinusoidal_embeddings						,				asm=self.asm						,				causal=self.causal						,				n_langs=self.n_langs						,				max_position_embeddings=self.max_position_embeddings						,				initializer_range=self.initializer_range						,				summary_type=self.summary_type						,				use_proj=self.use_proj						,				bos_token_id=self.bos_token_id						,				)

     return (
         config,
         input_ids,
         token_type_ids,
         input_lengths,
         sequence_labels,
         token_labels,
         is_impossible_labels,
         choice_labels,
         input_mask,
     )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				):






     """simple docstring"""





     lowerCamelCase :					Optional[Any]    =				TFFlaubertModel(config=__A )
     lowerCamelCase :					Any    =				{"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
     lowerCamelCase :					Dict    =				model(__A )

     lowerCamelCase :					Any    =				[input_ids, input_mask]
     lowerCamelCase :					Tuple    =				model(__A )
     self.parent.assertEqual(result.last_hidden_state.shape						,				(self.batch_size, self.seq_length, self.hidden_size) )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				):






     """simple docstring"""





     lowerCamelCase :					int    =				TFFlaubertWithLMHeadModel(__A )

     lowerCamelCase :					List[str]    =				{"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
     lowerCamelCase :					int    =				model(__A )

     self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.seq_length, self.vocab_size) )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				):






     """simple docstring"""





     lowerCamelCase :					Union[str, Any]    =				TFFlaubertForQuestionAnsweringSimple(__A )

     lowerCamelCase :					Optional[int]    =				{"input_ids": input_ids, "lengths": input_lengths}

     lowerCamelCase :					Union[str, Any]    =				model(__A )

     self.parent.assertEqual(result.start_logits.shape						,				(self.batch_size, self.seq_length) )
     self.parent.assertEqual(result.end_logits.shape						,				(self.batch_size, self.seq_length) )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				):






     """simple docstring"""





     lowerCamelCase :					Optional[int]    =				TFFlaubertForSequenceClassification(__A )

     lowerCamelCase :					str    =				{"input_ids": input_ids, "lengths": input_lengths}

     lowerCamelCase :					Union[str, Any]    =				model(__A )

     self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.type_sequence_label_size) )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				):






     """simple docstring"""





     lowerCamelCase :					Tuple    =				self.num_labels
     lowerCamelCase :					Optional[Any]    =				TFFlaubertForTokenClassification(config=__A )
     lowerCamelCase :					int    =				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
     lowerCamelCase :					Union[str, Any]    =				model(__A )
     self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.seq_length, self.num_labels) )






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				__A						,				):






     """simple docstring"""





     lowerCamelCase :					Any    =				self.num_choices
     lowerCamelCase :					Optional[Any]    =				TFFlaubertForMultipleChoice(config=__A )
     lowerCamelCase :					Tuple    =				tf.tile(tf.expand_dims(__A						,				1 )						,				(1, self.num_choices, 1) )
     lowerCamelCase :					int    =				tf.tile(tf.expand_dims(__A						,				1 )						,				(1, self.num_choices, 1) )
     lowerCamelCase :					List[str]    =				tf.tile(tf.expand_dims(__A						,				1 )						,				(1, self.num_choices, 1) )
     lowerCamelCase :					Optional[int]    =				{
         "input_ids": multiple_choice_inputs_ids,
         "attention_mask": multiple_choice_input_mask,
         "token_type_ids": multiple_choice_token_type_ids,
     }
     lowerCamelCase :					Union[str, Any]    =				model(__A )
     self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_choices) )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Dict    =				self.prepare_config_and_inputs()
     (
         (
         lowerCamelCase
     )    ,	(
         lowerCamelCase
     )    ,	(
         lowerCamelCase
     )    ,	(
         lowerCamelCase
     )    ,	(
         lowerCamelCase
     )    ,	(
         lowerCamelCase
     )    ,	(
         lowerCamelCase
     )    ,	(
         lowerCamelCase
     )    ,	(
         lowerCamelCase
     )    ,	
     ) :					Optional[Any]    =				config_and_inputs
     lowerCamelCase :					List[Any]    =				{
         "input_ids": input_ids,
         "token_type_ids": token_type_ids,
         "langs": token_type_ids,
         "lengths": input_lengths,
     }
     return config, inputs_dict



@require_tf
class 	UpperCAmelCase_					(					UpperCamelCase		, UpperCamelCase		, unittest.TestCase    ):

   '''simple docstring'''




   __A :      str     		=		(
       (
           TFFlaubertModel,
           TFFlaubertWithLMHeadModel,
           TFFlaubertForSequenceClassification,
           TFFlaubertForQuestionAnsweringSimple,
           TFFlaubertForTokenClassification,
           TFFlaubertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   __A :      Dict     		=		(
       (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
   )  # TODO (PVP): Check other models whether language generation is also applicable
   __A :      Any     		=		(
       {
           "feature-extraction": TFFlaubertModel,
           "fill-mask": TFFlaubertWithLMHeadModel,
           "question-answering": TFFlaubertForQuestionAnsweringSimple,
           "text-classification": TFFlaubertForSequenceClassification,
           "token-classification": TFFlaubertForTokenClassification,
           "zero-shot": TFFlaubertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   __A :      List[str]     		=		False
   __A :      List[str]     		=		False






   def _snake_case    (		self						,				__A						,				__A						,				__A						,				__A						,				__A ):






     """simple docstring"""





     if (
         pipeline_test_casse_name == "QAPipelineTests"
         and tokenizer_name is not None
         and not tokenizer_name.endswith("Fast" )
     ):
       # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
       # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
       # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
       return True

     return False






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Tuple    =				TFFlaubertModelTester(self )
     lowerCamelCase :					Optional[int]    =				ConfigTester(self						,				config_class=__A						,				emb_dim=37 )






   def _snake_case    (		self ):






     """simple docstring"""





     self.config_tester.run_common_tests()






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Union[str, Any]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_model(*__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Union[str, Any]    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_lm_head(*__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					int    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_qa(*__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Tuple    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_sequence_classif(*__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Dict    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_for_token_classification(*__A )






   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					str    =				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_flaubert_for_multiple_choice(*__A )






   @slow
   def _snake_case    (		self ):






     """simple docstring"""





     for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       lowerCamelCase :					int    =				TFFlaubertModel.from_pretrained(__A )
       self.assertIsNotNone(__A )



@require_tf
@require_sentencepiece
@require_tokenizers
class 	UpperCAmelCase_					(					unittest.TestCase    ):

   '''simple docstring'''




   @slow
   def _snake_case    (		self ):






     """simple docstring"""





     lowerCamelCase :					Optional[int]    =				TFFlaubertModel.from_pretrained("jplu/tf-flaubert-small-cased" )

     lowerCamelCase :					str    =				tf.convert_to_tensor(
         [[0, 158, 735, 2592, 1424, 6727, 82, 1]]						,				dtype=tf.intaa						,				)  # "J'aime flaubert !"

     lowerCamelCase :					Dict    =				model(__A )[0]
     lowerCamelCase :					List[str]    =				tf.TensorShape((1, 8, 512) )
     self.assertEqual(output.shape						,				__A )
     # compare the actual values for a slice.
     lowerCamelCase :					Tuple    =				tf.convert_to_tensor(
         [
             [
                 [-1.8768773, -1.566555, 0.27072418],
                 [-1.6920038, -0.5873505, 1.9329599],
                 [-2.9563985, -1.6993835, 1.7972052],
             ]
         ]						,				dtype=tf.floataa						,				)

     self.assertTrue(np.allclose(output[:, :3, :3].numpy()						,				expected_slice.numpy()						,				atol=1e-4 ) )



