





import unittest

from transformers import SpeechaTextaConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

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


if is_torch_available():
  import torch

  from transformers.models.speech_to_text_a.modeling_speech_to_text_a import (
      SpeechaTextaDecoder,
      SpeechaTextaForCausalLM,
  )

@require_torch
class 		__a							:



       def __init__(     self	:   int					,			UpperCAmelCase	:   int					,			UpperCAmelCase	:   List[Any]=99					,			UpperCAmelCase	:   str=13					,			UpperCAmelCase	:   List[str]=16					,			UpperCAmelCase	:   Optional[int]=7					,			UpperCAmelCase	:   Optional[Any]=True					,			UpperCAmelCase	:   Dict=True					,			UpperCAmelCase	:   List[str]=True					,			UpperCAmelCase	:   int=False					,			UpperCAmelCase	:   Optional[Any]=True					,			UpperCAmelCase	:   Optional[Any]=2					,			UpperCAmelCase	:   Dict=32					,			UpperCAmelCase	:   List[str]=4					,			UpperCAmelCase	:   int=4					,			UpperCAmelCase	:   Optional[Any]=30					,			UpperCAmelCase	:   List[Any]=0					,			UpperCAmelCase	:   str=1					,			UpperCAmelCase	:   Dict=2					,			UpperCAmelCase	:   Any=None					,			):
           lowerCAmelCase_					:	List[Any]						=   parent
           lowerCAmelCase_					:	List[str]						=   batch_size
           lowerCAmelCase_					:	Optional[Any]						=   decoder_seq_length
           # For common tests
           lowerCAmelCase_					:	List[Any]						=   self.decoder_seq_length
           lowerCAmelCase_					:	str						=   is_training
           lowerCAmelCase_					:	List[str]						=   use_attention_mask
           lowerCAmelCase_					:	List[str]						=   use_labels

           lowerCAmelCase_					:	Dict						=   vocab_size
           lowerCAmelCase_					:	str						=   d_model
           lowerCAmelCase_					:	List[str]						=   d_model
           lowerCAmelCase_					:	str						=   decoder_layers
           lowerCAmelCase_					:	str						=   decoder_layers
           lowerCAmelCase_					:	Union[str, Any]						=   decoder_ffn_dim
           lowerCAmelCase_					:	str						=   decoder_attention_heads
           lowerCAmelCase_					:	Union[str, Any]						=   decoder_attention_heads
           lowerCAmelCase_					:	List[str]						=   eos_token_id
           lowerCAmelCase_					:	Optional[int]						=   bos_token_id
           lowerCAmelCase_					:	int						=   pad_token_id
           lowerCAmelCase_					:	int						=   decoder_start_token_id
           lowerCAmelCase_					:	Optional[Any]						=   use_cache
           lowerCAmelCase_					:	List[str]						=   max_position_embeddings

           lowerCAmelCase_					:	Optional[Any]						=   None
           lowerCAmelCase_					:	Any						=   decoder_seq_length
           lowerCAmelCase_					:	Union[str, Any]						=   2
           lowerCAmelCase_					:	Tuple						=   1



       def  A							(     self	:   Tuple			):
           lowerCAmelCase_					:	List[str]						=   ids_tensor([self.batch_size, self.decoder_seq_length]					,			self.vocab_size			)

           lowerCAmelCase_					:	Optional[int]						=   None
           if self.use_attention_mask:
               lowerCAmelCase_					:	List[str]						=   ids_tensor([self.batch_size, self.decoder_seq_length]					,			vocab_size=2			)

           lowerCAmelCase_					:	List[str]						=   None
           if self.use_labels:
               lowerCAmelCase_					:	List[str]						=   ids_tensor([self.batch_size, self.decoder_seq_length]					,			self.vocab_size			)

           lowerCAmelCase_					:	Union[str, Any]						=   SpeechaTextaConfig(
               vocab_size=self.vocab_size					,			d_model=self.d_model					,			decoder_layers=self.decoder_layers					,			decoder_ffn_dim=self.decoder_ffn_dim					,			decoder_attention_heads=self.decoder_attention_heads					,			eos_token_id=self.eos_token_id					,			bos_token_id=self.bos_token_id					,			use_cache=self.use_cache					,			pad_token_id=self.pad_token_id					,			decoder_start_token_id=self.decoder_start_token_id					,			max_position_embeddings=self.max_position_embeddings					,			)

           return (
               config,
               input_ids,
               attention_mask,
               lm_labels,
           )



       def  A							(     self	:   Tuple					,			UpperCAmelCase	:   Optional[int]					,			UpperCAmelCase	:   Union[str, Any]					,			UpperCAmelCase	:   Union[str, Any]					,			UpperCAmelCase	:   List[str]					,			):
           lowerCAmelCase_					:	str						=   True
           lowerCAmelCase_					:	Optional[Any]						=   SpeechaTextaDecoder(config=UpperCAmelCase_			).to(UpperCAmelCase_			).eval()
           lowerCAmelCase_					:	Union[str, Any]						=   input_ids[:2]

           input_ids[input_ids == 0] += 1
           # first forward pass
           lowerCAmelCase_					:	List[Any]						=   model(UpperCAmelCase_					,			use_cache=UpperCAmelCase_			)
           lowerCAmelCase_					:	List[Any]						=   model(UpperCAmelCase_			)
           lowerCAmelCase_					:	str						=   model(UpperCAmelCase_					,			use_cache=UpperCAmelCase_			)

           self.parent.assertTrue(len(UpperCAmelCase_			) == len(UpperCAmelCase_			)			)
           self.parent.assertTrue(len(UpperCAmelCase_			) == len(UpperCAmelCase_			) + 1			)

           lowerCAmelCase_					:	Tuple						=   outputs['past_key_values']

           # create hypothetical next token and extent to next_input_ids
           lowerCAmelCase_					:	Union[str, Any]						=   ids_tensor((2, 1)					,			config.vocab_size - 1			) + 1

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

           print(UpperCAmelCase_			)

           lowerCAmelCase_					:	Union[str, Any]						=   model(UpperCAmelCase_			)['last_hidden_state']
           lowerCAmelCase_					:	Optional[int]						=   model(UpperCAmelCase_					,			past_key_values=UpperCAmelCase_			)['last_hidden_state']

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

           # test that outputs are equal for slice
           assert torch.allclose(UpperCAmelCase_					,			UpperCAmelCase_					,			atol=1e-3			)







       def  A							(     self	:   Optional[int]			):
           lowerCAmelCase_					:	int						=   self.prepare_config_and_inputs()
           (
               lowerCAmelCase_  
           )					:	Any						=   config_and_inputs

           lowerCAmelCase_					:	Union[str, Any]						=   {
               'input_ids': input_ids,
               'attention_mask': attention_mask,
           }
           return config, inputs_dict






@require_torch
class 		__a							(	__UpperCamelCase			,__UpperCamelCase			,__UpperCamelCase			,unittest.TestCase      ):
       __snake_case						:   List[Any]      =		(SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
       __snake_case						:   int      =		(SpeechaTextaForCausalLM,) if is_torch_available() else ()
       __snake_case						:   List[Any]      =		{"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
       __snake_case						:   int      =		True
       __snake_case						:   Dict      =		False



       def  A							(     self	:   Optional[Any]					,			):
           lowerCAmelCase_					:	Optional[Any]						=   SpeechaTextaStandaloneDecoderModelTester(self					,			is_training=UpperCAmelCase_			)
           lowerCAmelCase_					:	Dict						=   ConfigTester(self					,			config_class=UpperCAmelCase_			)



       def  A							(     self	:   Optional[int]			):
           pass



       def  A							(     self	:   Any			):
           pass



       def  A							(     self	:   Dict			):
           pass



       def  A							(     self	:   Dict			):
           self.config_tester.run_common_tests()



       def  A							(     self	:   Optional[int]			):
           lowerCAmelCase_					:	int						=   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_decoder_model_past(*UpperCAmelCase_			)







       def  A							(     self	:   Tuple			):
           return









import json
import os
import random
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
  from PIL import Image

  from transformers import FlavaImageProcessor, FlavaProcessor
  from transformers.models.flava.image_processing_flava import (
      FLAVA_CODEBOOK_MEAN,
      FLAVA_CODEBOOK_STD,
      FLAVA_IMAGE_MEAN,
      FLAVA_IMAGE_STD,
  )

@require_vision
class 		__a							(	unittest.TestCase      ):



       def  A							(     self	:   List[str]			):
           lowerCAmelCase_					:	Dict						=   tempfile.mkdtemp()

           # fmt: off
           lowerCAmelCase_					:	Dict						=   ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""]
           # fmt: on
           lowerCAmelCase_					:	Any						=   os.path.join(self.tmpdirname					,			VOCAB_FILES_NAMES["""vocab_file"""]			)

           with open(self.vocab_file					,			"""w"""					,			encoding="""utf-8"""			) as fp:
               fp.write("""""".join([x + """\n""" for x in vocab_tokens]			)			)

           lowerCAmelCase_					:	int						=   {
               """image_mean""": FLAVA_IMAGE_MEAN,
               """image_std""": FLAVA_IMAGE_STD,
               """do_normalize""": True,
               """do_resize""": True,
               """size""": 2_24,
               """do_center_crop""": True,
               """crop_size""": 2_24,
               """input_size_patches""": 14,
               """total_mask_patches""": 75,
               """mask_group_max_patches""": None,
               """mask_group_min_patches""": 16,
               """mask_group_min_aspect_ratio""": 0.3,
               """mask_group_max_aspect_ratio""": None,
               """codebook_do_resize""": True,
               """codebook_size""": 1_12,
               """codebook_do_center_crop""": True,
               """codebook_crop_size""": 1_12,
               """codebook_do_map_pixels""": True,
               """codebook_do_normalize""": True,
               """codebook_image_mean""": FLAVA_CODEBOOK_MEAN,
               """codebook_image_std""": FLAVA_CODEBOOK_STD,
           }

           lowerCAmelCase_					:	Union[str, Any]						=   os.path.join(self.tmpdirname					,			UpperCAmelCase			)
           with open(self.image_processor_file					,			"""w"""					,			encoding="""utf-8"""			) as fp:
               json.dump(UpperCAmelCase					,			UpperCAmelCase			)



       def  A							(     self	:   Any					,			**UpperCAmelCase	:   Union[str, Any]			):
           return BertTokenizer.from_pretrained(self.tmpdirname					,			**UpperCAmelCase			)



       def  A							(     self	:   List[str]					,			**UpperCAmelCase	:   str			):
           return BertTokenizerFast.from_pretrained(self.tmpdirname					,			**UpperCAmelCase			)



       def  A							(     self	:   Any					,			**UpperCAmelCase	:   int			):
           return FlavaImageProcessor.from_pretrained(self.tmpdirname					,			**UpperCAmelCase			)



       def  A							(     self	:   Optional[int]			):
           shutil.rmtree(self.tmpdirname			)



       def  A							(     self	:   str			):
           lowerCAmelCase_					:	Union[str, Any]						=   [np.random.randint(2_55					,			size=(3, 30, 4_00)					,			dtype=np.uinta			)]

           lowerCAmelCase_					:	int						=   [Image.fromarray(np.moveaxis(UpperCAmelCase					,			0					,			-1			)			) for x in image_inputs]

           return image_inputs



       def  A							(     self	:   Tuple			):
           lowerCAmelCase_					:	str						=   self.get_tokenizer()
           lowerCAmelCase_					:	Dict						=   self.get_rust_tokenizer()
           lowerCAmelCase_					:	Optional[int]						=   self.get_image_processor()

           lowerCAmelCase_					:	str						=   FlavaProcessor(tokenizer=UpperCAmelCase					,			image_processor=UpperCAmelCase			)
           processor_slow.save_pretrained(self.tmpdirname			)
           lowerCAmelCase_					:	List[str]						=   FlavaProcessor.from_pretrained(self.tmpdirname					,			use_fast=UpperCAmelCase			)

           lowerCAmelCase_					:	List[Any]						=   FlavaProcessor(tokenizer=UpperCAmelCase					,			image_processor=UpperCAmelCase			)
           processor_fast.save_pretrained(self.tmpdirname			)
           lowerCAmelCase_					:	List[str]						=   FlavaProcessor.from_pretrained(self.tmpdirname			)

           self.assertEqual(processor_slow.tokenizer.get_vocab()					,			tokenizer_slow.get_vocab()			)
           self.assertEqual(processor_fast.tokenizer.get_vocab()					,			tokenizer_fast.get_vocab()			)
           self.assertEqual(tokenizer_slow.get_vocab()					,			tokenizer_fast.get_vocab()			)
           self.assertIsInstance(processor_slow.tokenizer					,			UpperCAmelCase			)
           self.assertIsInstance(processor_fast.tokenizer					,			UpperCAmelCase			)

           self.assertEqual(processor_slow.image_processor.to_json_string()					,			image_processor.to_json_string()			)
           self.assertEqual(processor_fast.image_processor.to_json_string()					,			image_processor.to_json_string()			)
           self.assertIsInstance(processor_slow.image_processor					,			UpperCAmelCase			)
           self.assertIsInstance(processor_fast.image_processor					,			UpperCAmelCase			)



       def  A							(     self	:   Any			):
           lowerCAmelCase_					:	Any						=   FlavaProcessor(tokenizer=self.get_tokenizer()					,			image_processor=self.get_image_processor()			)
           processor.save_pretrained(self.tmpdirname			)

           lowerCAmelCase_					:	Union[str, Any]						=   self.get_tokenizer(bos_token="""(BOS)"""					,			eos_token="""(EOS)"""			)
           lowerCAmelCase_					:	Optional[Any]						=   self.get_image_processor(do_normalize=UpperCAmelCase					,			padding_value=1.0			)

           lowerCAmelCase_					:	Any						=   FlavaProcessor.from_pretrained(
               self.tmpdirname					,			bos_token="""(BOS)"""					,			eos_token="""(EOS)"""					,			do_normalize=UpperCAmelCase					,			padding_value=1.0			)

           self.assertEqual(processor.tokenizer.get_vocab()					,			tokenizer_add_kwargs.get_vocab()			)
           self.assertIsInstance(processor.tokenizer					,			UpperCAmelCase			)

           self.assertEqual(processor.image_processor.to_json_string()					,			image_processor_add_kwargs.to_json_string()			)
           self.assertIsInstance(processor.image_processor					,			UpperCAmelCase			)



       def  A							(     self	:   str			):
           lowerCAmelCase_					:	Tuple						=   self.get_image_processor()
           lowerCAmelCase_					:	Union[str, Any]						=   self.get_tokenizer()

           lowerCAmelCase_					:	str						=   FlavaProcessor(tokenizer=UpperCAmelCase					,			image_processor=UpperCAmelCase			)

           lowerCAmelCase_					:	Tuple						=   self.prepare_image_inputs()

           lowerCAmelCase_					:	List[str]						=   image_processor(UpperCAmelCase					,			return_tensors="""np"""			)
           lowerCAmelCase_					:	Tuple						=   processor(images=UpperCAmelCase					,			return_tensors="""np"""			)

           for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum()					,			input_processor[key].sum()					,			delta=1e-2			)

           # With rest of the args
           random.seed(12_34			)
           lowerCAmelCase_					:	Optional[Any]						=   image_processor(
               UpperCAmelCase					,			return_image_mask=UpperCAmelCase					,			return_codebook_pixels=UpperCAmelCase					,			return_tensors="""np"""			)
           random.seed(12_34			)
           lowerCAmelCase_					:	Dict						=   processor(
               images=UpperCAmelCase					,			return_image_mask=UpperCAmelCase					,			return_codebook_pixels=UpperCAmelCase					,			return_tensors="""np"""			)

           for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum()					,			input_processor[key].sum()					,			delta=1e-2			)



       def  A							(     self	:   str			):
           lowerCAmelCase_					:	Tuple						=   self.get_image_processor()
           lowerCAmelCase_					:	List[Any]						=   self.get_tokenizer()

           lowerCAmelCase_					:	str						=   FlavaProcessor(tokenizer=UpperCAmelCase					,			image_processor=UpperCAmelCase			)

           lowerCAmelCase_					:	str						=   """lower newer"""

           lowerCAmelCase_					:	Dict						=   processor(text=UpperCAmelCase			)

           lowerCAmelCase_					:	List[Any]						=   tokenizer(UpperCAmelCase			)

           for key in encoded_tok.keys():
               self.assertListEqual(encoded_tok[key]					,			encoded_processor[key]			)



       def  A							(     self	:   Optional[Any]			):
           lowerCAmelCase_					:	Optional[int]						=   self.get_image_processor()
           lowerCAmelCase_					:	List[Any]						=   self.get_tokenizer()

           lowerCAmelCase_					:	str						=   FlavaProcessor(tokenizer=UpperCAmelCase					,			image_processor=UpperCAmelCase			)

           lowerCAmelCase_					:	str						=   """lower newer"""
           lowerCAmelCase_					:	Any						=   self.prepare_image_inputs()

           lowerCAmelCase_					:	Optional[int]						=   processor(text=UpperCAmelCase					,			images=UpperCAmelCase			)

           self.assertListEqual(list(inputs.keys()			)					,			["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""]			)

           # add extra args
           lowerCAmelCase_					:	str						=   processor(text=UpperCAmelCase					,			images=UpperCAmelCase					,			return_codebook_pixels=UpperCAmelCase					,			return_image_mask=UpperCAmelCase			)

           self.assertListEqual(
               list(inputs.keys()			)					,			[
                   """input_ids""",
                   """token_type_ids""",
                   """attention_mask""",
                   """pixel_values""",
                   """codebook_pixel_values""",
                   """bool_masked_pos""",
               ]					,			)

           # test if it raises when no input is passed
           with pytest.raises(UpperCAmelCase			):
               processor()



       def  A							(     self	:   List[Any]			):
           lowerCAmelCase_					:	str						=   self.get_image_processor()
           lowerCAmelCase_					:	int						=   self.get_tokenizer()

           lowerCAmelCase_					:	Union[str, Any]						=   FlavaProcessor(tokenizer=UpperCAmelCase					,			image_processor=UpperCAmelCase			)

           lowerCAmelCase_					:	Optional[Any]						=   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

           lowerCAmelCase_					:	List[str]						=   processor.batch_decode(UpperCAmelCase			)
           lowerCAmelCase_					:	Tuple						=   tokenizer.batch_decode(UpperCAmelCase			)

           self.assertListEqual(UpperCAmelCase					,			UpperCAmelCase			)



       def  A							(     self	:   List[Any]			):
           lowerCAmelCase_					:	Tuple						=   self.get_image_processor()
           lowerCAmelCase_					:	Union[str, Any]						=   self.get_tokenizer()

           lowerCAmelCase_					:	int						=   FlavaProcessor(tokenizer=UpperCAmelCase					,			image_processor=UpperCAmelCase			)

           lowerCAmelCase_					:	List[str]						=   """lower newer"""
           lowerCAmelCase_					:	Dict						=   self.prepare_image_inputs()

           lowerCAmelCase_					:	Optional[int]						=   processor(text=UpperCAmelCase					,			images=UpperCAmelCase			)

           self.assertListEqual(list(inputs.keys()			)					,			processor.model_input_names			)


