



"""simple docstring"""
from __future__ import annotations

import unittest

from transformers import AutoTokenizer, MBartConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
from transformers.utils import cached_property

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


if is_tf_available():
  import tensorflow as tf

  from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel
@require_tf
class 							_UpperCAmelCase							:





     '''simple docstring'''



     a__									=MBartConfig
     a__									={}
     a__									='''gelu'''





     def __init__(					self       ,	A       ,	A=1_3       ,	A=7       ,	A=True       ,	A=False       ,	A=9_9       ,	A=3_2       ,	A=2       ,	A=4       ,	A=3_7       ,	A=0.1       ,	A=0.1       ,	A=2_0       ,	A=2       ,	A=1       ,	A=0       ,	)							->					List[Any]:
         _UpperCAmelCase							:  str											=    parent
         _UpperCAmelCase							:  str											=    batch_size
         _UpperCAmelCase							:  Union[str, Any]											=    seq_length
         _UpperCAmelCase							:  Optional[int]											=    is_training
         _UpperCAmelCase							:  Union[str, Any]											=    use_labels
         _UpperCAmelCase							:  Any											=    vocab_size
         _UpperCAmelCase							:  Dict											=    hidden_size
         _UpperCAmelCase							:  Optional[Any]											=    num_hidden_layers
         _UpperCAmelCase							:  Optional[Any]											=    num_attention_heads
         _UpperCAmelCase							:  List[Any]											=    intermediate_size
         _UpperCAmelCase							:  int											=    hidden_dropout_prob
         _UpperCAmelCase							:  Optional[Any]											=    attention_probs_dropout_prob
         _UpperCAmelCase							:  Dict											=    max_position_embeddings
         _UpperCAmelCase							:  str											=    eos_token_id
         _UpperCAmelCase							:  Tuple											=    pad_token_id
         _UpperCAmelCase							:  Optional[Any]											=    bos_token_id





     def  __lowerCAmelCase	(					self		)							->					Union[str, Any]:
         _UpperCAmelCase							:  str											=    ids_tensor([self.batch_size, self.seq_length - 1]       ,	self.vocab_size		)
         _UpperCAmelCase							:  Optional[Any]											=    tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size		)       ,	1		)
         _UpperCAmelCase							:  int											=    tf.concat([input_ids, eos_tensor]       ,	axis=1		)

         _UpperCAmelCase							:  int											=    ids_tensor([self.batch_size, self.seq_length]       ,	self.vocab_size		)

         _UpperCAmelCase							:  Optional[Any]											=    self.config_cls(
             vocab_size=self.vocab_size       ,	d_model=self.hidden_size       ,	encoder_layers=self.num_hidden_layers       ,	decoder_layers=self.num_hidden_layers       ,	encoder_attention_heads=self.num_attention_heads       ,	decoder_attention_heads=self.num_attention_heads       ,	encoder_ffn_dim=self.intermediate_size       ,	decoder_ffn_dim=self.intermediate_size       ,	dropout=self.hidden_dropout_prob       ,	attention_dropout=self.attention_probs_dropout_prob       ,	max_position_embeddings=self.max_position_embeddings       ,	eos_token_ids=[2]       ,	bos_token_id=self.bos_token_id       ,	pad_token_id=self.pad_token_id       ,	decoder_start_token_id=self.pad_token_id       ,	**self.config_updates       ,	)
         _UpperCAmelCase							:  List[str]											=    prepare_mbart_inputs_dict(A       ,	A       ,	A		)
         return config, inputs_dict





     def  __lowerCAmelCase	(					self       ,	A       ,	A		)							->					str:
         _UpperCAmelCase							:  Tuple											=    TFMBartModel(config=A		).get_decoder()
         _UpperCAmelCase							:  Optional[int]											=    inputs_dict['''input_ids''']

         _UpperCAmelCase							:  Tuple											=    input_ids[:1, :]
         _UpperCAmelCase							:  int											=    inputs_dict['''attention_mask'''][:1, :]
         _UpperCAmelCase							:  int											=    inputs_dict['''head_mask''']
         _UpperCAmelCase							:  int											=    1

         # first forward pass
         _UpperCAmelCase							:  str											=    model(A       ,	attention_mask=A       ,	head_mask=A       ,	use_cache=A		)

         _UpperCAmelCase							,				_UpperCAmelCase							:  Union[str, Any]											=    outputs.to_tuple()
         _UpperCAmelCase							:  Optional[int]											=    past_key_values[1]





def  lowerCamelCase_						(UpperCamelCase__		:		Optional[int]     ,       UpperCamelCase__		:		Union[str, Any]     ,       UpperCamelCase__		:		List[Any]     ,       UpperCamelCase__		:		Union[str, Any]=None     ,       UpperCamelCase__		:		List[Any]=None     ,       UpperCamelCase__		:		Any=None     ,       UpperCamelCase__		:		Union[str, Any]=None     ,       UpperCamelCase__		:		str=None     ,       ):
    if attention_mask is None:
        _UpperCAmelCase							:  int											=    tf.cast(tf.math.not_equal(UpperCamelCase__     ,       config.pad_token_id							)     ,       tf.inta							)
    if decoder_attention_mask is None:
        _UpperCAmelCase							:  Any											=    tf.concat(
            [
                tf.ones(decoder_input_ids[:, :1].shape     ,       dtype=tf.inta							),
                tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]     ,       config.pad_token_id							)     ,       tf.inta							),
            ]     ,       axis=-1     ,       )
    if head_mask is None:
        _UpperCAmelCase							:  Optional[Any]											=    tf.ones((config.encoder_layers, config.encoder_attention_heads)							)
    if decoder_head_mask is None:
        _UpperCAmelCase							:  Any											=    tf.ones((config.decoder_layers, config.decoder_attention_heads)							)
    if cross_attn_head_mask is None:
        _UpperCAmelCase							:  Union[str, Any]											=    tf.ones((config.decoder_layers, config.decoder_attention_heads)							)
    return {
        "input_ids": input_ids,
        "decoder_input_ids": decoder_input_ids,
        "attention_mask": attention_mask,
        "decoder_attention_mask": decoder_attention_mask,
        "head_mask": head_mask,
        "decoder_head_mask": decoder_head_mask,
        "cross_attn_head_mask": cross_attn_head_mask,
    }
@require_tf
class 							_UpperCAmelCase							( a		,a		,unittest.TestCase      ):





     '''simple docstring'''



     a__									=(TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
     a__									=(TFMBartForConditionalGeneration,) if is_tf_available() else ()
     a__									=(
         {
             '''conversational''': TFMBartForConditionalGeneration,
             '''feature-extraction''': TFMBartModel,
             '''summarization''': TFMBartForConditionalGeneration,
             '''text2text-generation''': TFMBartForConditionalGeneration,
             '''translation''': TFMBartForConditionalGeneration,
         }
         if is_tf_available()
         else {}
     )
     a__									=True
     a__									=False
     a__									=False





     def  __lowerCAmelCase	(					self       ,	A       ,	A       ,	A       ,	A       ,	A		)							->					str:
         if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
             # Exception encountered when calling layer '...'
             return True

         return False





     def  __lowerCAmelCase	(					self		)							->					str:
         _UpperCAmelCase							:  Union[str, Any]											=    TFMBartModelTester(self		)
         _UpperCAmelCase							:  Dict											=    ConfigTester(self       ,	config_class=A		)





     def  __lowerCAmelCase	(					self		)							->					List[str]:
         self.config_tester.run_common_tests()





     def  __lowerCAmelCase	(					self		)							->					Optional[int]:
         _UpperCAmelCase							:  str											=    self.model_tester.prepare_config_and_inputs_for_common()
         self.model_tester.check_decoder_model_past_large_inputs(*A		)



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





     '''simple docstring'''



     a__									=[
         ''' UN Chief Says There Is No Military Solution in Syria''',
     ]
     a__									=[
         '''Şeful ONU declară că nu există o soluţie militară în Siria''',
     ]
     a__									='''facebook/mbart-large-en-ro'''





     @cached_property
     def  __lowerCAmelCase	(					self		)							->					Dict:
         return AutoTokenizer.from_pretrained(self.model_name		)





     @cached_property
     def  __lowerCAmelCase	(					self		)							->					Dict:
         _UpperCAmelCase							:  Optional[Any]											=    TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name		)
         return model





     def  __lowerCAmelCase	(					self       ,	**A		)							->					str:
         _UpperCAmelCase							:  Tuple											=    self.translate_src_text(**A		)
         self.assertListEqual(self.expected_text       ,	A		)





     def  __lowerCAmelCase	(					self       ,	**A		)							->					Dict:
         _UpperCAmelCase							:  Union[str, Any]											=    self.tokenizer(self.src_text       ,	**A       ,	return_tensors='''tf'''		)
         _UpperCAmelCase							:  str											=    self.model.generate(
             model_inputs.input_ids       ,	attention_mask=model_inputs.attention_mask       ,	num_beams=2		)
         _UpperCAmelCase							:  Union[str, Any]											=    self.tokenizer.batch_decode(A       ,	skip_special_tokens=A		)
         return generated_words





     @slow
     def  __lowerCAmelCase	(					self		)							->					Optional[Any]:
         self._assert_generated_batch_equal_expected()







"""simple docstring"""
def  lowerCamelCase_						(UpperCamelCase__		:		int     ,       UpperCamelCase__		:		int							):
    if a < 0 or b < 0:
        raise ValueError('''the value of both inputs must be positive'''							)
    _UpperCAmelCase							:  List[str]											=    str(bin(UpperCamelCase__							)							)[2:]  # remove the leading "0b"
    _UpperCAmelCase							:  str											=    str(bin(UpperCamelCase__							)							)[2:]
    _UpperCAmelCase							:  List[str]											=    max(len(UpperCamelCase__							)     ,       len(UpperCamelCase__							)							)
    return "0b" + "".join(
        str(int('''1''' in (char_a, char_b)							)							)
        for char_a, char_b in zip(a_binary.zfill(UpperCamelCase__							)     ,       b_binary.zfill(UpperCamelCase__							)							)							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()


