






"""simple docstring"""







import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
   from transformers import SpeechaTextFeatureExtractor

a			:    str				 =		random.Random()





def 						lowercase__(A ,		A=1.0 ,		A=None ,		A=None						)    ->Union[str, Any]:




      """simple docstring"""



      if rng is None:
            lowercase__    :				Tuple=	global_rng

      lowercase__    :				Tuple=	[]
      for batch_idx in range(shape[0]						):
            values.append([]						)
            for _ in range(shape[1]						):
                  values[-1].append(rng.random() * scale						)

      return values


@require_torch
@require_torchaudio
class    __UpperCAmelCase(       unittest.TestCase							):




      """simple docstring"""




      def __init__(       self	,  snake_case__	,  snake_case__=7	,  snake_case__=400	,  snake_case__=2000	,  snake_case__=24	,  snake_case__=24	,  snake_case__=0.0	,  snake_case__=16000	,  snake_case__=True	,  snake_case__=True	,  ):
            '''simple docstring'''
            lowercase__    :				Dict=	parent
            lowercase__    :				Union[str, Any]=	batch_size
            lowercase__    :				Optional[Any]=	min_seq_length
            lowercase__    :				Union[str, Any]=	max_seq_length
            lowercase__    :				Optional[int]=	(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
            lowercase__    :				Any=	feature_size
            lowercase__    :				Optional[Any]=	num_mel_bins
            lowercase__    :				Tuple=	padding_value
            lowercase__    :				Optional[int]=	sampling_rate
            lowercase__    :				str=	return_attention_mask
            lowercase__    :				List[str]=	do_normalize




      def 		UpperCAmelCase_	(       self      ):
            '''simple docstring'''
            return {
                "feature_size": self.feature_size,
                "num_mel_bins": self.num_mel_bins,
                "padding_value": self.padding_value,
                "sampling_rate": self.sampling_rate,
                "return_attention_mask": self.return_attention_mask,
                "do_normalize": self.do_normalize,
            }




      def 		UpperCAmelCase_	(       self	,  snake_case__=False	,  snake_case__=False      ):
            '''simple docstring'''



            def _flatten(snake_case__      ):
                  return list(itertools.chain(*a_      )      )

            if equal_length:
                  lowercase__    :				Tuple=	[floats_list((self.max_seq_length, self.feature_size)      ) for _ in range(self.batch_size      )]
            else:
                  # make sure that inputs increase in size
                  lowercase__    :				Union[str, Any]=	[
                      floats_list((x, self.feature_size)      )
                      for x in range(self.min_seq_length	,  self.max_seq_length	,  self.seq_length_diff      )
                  ]
            if numpify:
                  lowercase__    :				str=	[np.asarray(a_      ) for x in speech_inputs]
            return speech_inputs


@require_torch
@require_torchaudio
class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__  ,  unittest.TestCase							):




      """simple docstring"""




      __lowerCamelCase    	= SpeechaTextFeatureExtractor if is_speech_available() else None




      def 		UpperCAmelCase_	(       self      ):
            '''simple docstring'''
            lowercase__    :				Union[str, Any]=	SpeechaTextFeatureExtractionTester(self      )




      def 		UpperCAmelCase_	(       self	,  snake_case__      ):
            '''simple docstring'''
            self.assertTrue(np.all(np.mean(a_	,  axis=0      ) < 1e-3      )      )
            self.assertTrue(np.all(np.abs(np.var(a_	,  axis=0      ) - 1      ) < 1e-3      )      )




      def 		UpperCAmelCase_	(       self      ):
            '''simple docstring'''
            lowercase__    :				List[str]=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
            # create three inputs of length 800, 1000, and 1200
            lowercase__    :				Dict=	[floats_list((1, x)      )[0] for x in range(800	,  1400	,  200      )]
            lowercase__    :				Union[str, Any]=	[np.asarray(a_      ) for speech_input in speech_inputs]

            # Test feature size
            lowercase__    :				int=	feature_extractor(a_	,  padding=a_	,  return_tensors="np"      ).input_features
            self.assertTrue(input_features.ndim == 3      )
            self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size      )

            # Test not batched input
            lowercase__    :				Union[str, Any]=	feature_extractor(speech_inputs[0]	,  return_tensors="np"      ).input_features
            lowercase__    :				str=	feature_extractor(np_speech_inputs[0]	,  return_tensors="np"      ).input_features
            self.assertTrue(np.allclose(a_	,  a_	,  atol=1e-3      )      )

            # Test batched
            lowercase__    :				Union[str, Any]=	feature_extractor(a_	,  return_tensors="np"      ).input_features
            lowercase__    :				int=	feature_extractor(a_	,  return_tensors="np"      ).input_features
            for enc_seq_a, enc_seq_a in zip(a_	,  a_      ):
                  self.assertTrue(np.allclose(a_	,  a_	,  atol=1e-3      )      )

            # Test 2-D numpy arrays are batched.
            lowercase__    :				List[Any]=	[floats_list((1, x)      )[0] for x in (800, 800, 800)]
            lowercase__    :				Tuple=	np.asarray(a_      )
            lowercase__    :				Tuple=	feature_extractor(a_	,  return_tensors="np"      ).input_features
            lowercase__    :				Optional[Any]=	feature_extractor(a_	,  return_tensors="np"      ).input_features
            for enc_seq_a, enc_seq_a in zip(a_	,  a_      ):
                  self.assertTrue(np.allclose(a_	,  a_	,  atol=1e-3      )      )




      def 		UpperCAmelCase_	(       self      ):
            '''simple docstring'''
            lowercase__    :				Tuple=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
            lowercase__    :				Optional[int]=	[floats_list((1, x)      )[0] for x in range(800	,  1400	,  200      )]

            lowercase__    :				Any=	["longest", "max_length", "do_not_pad"]
            lowercase__    :				Tuple=	[None, 16, None]
            for max_length, padding in zip(a_	,  a_      ):
                  lowercase__    :				Optional[Any]=	feature_extractor(
                      a_	,  padding=a_	,  max_length=a_	,  return_attention_mask=a_      )
                  lowercase__    :				Tuple=	inputs.input_features
                  lowercase__    :				Dict=	inputs.attention_mask
                  lowercase__    :				Optional[Any]=	[np.sum(a_      ) for x in attention_mask]

                  self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]      )
                  self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]      )
                  self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]      )




      def 		UpperCAmelCase_	(       self      ):
            '''simple docstring'''
            lowercase__    :				Optional[Any]=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
            lowercase__    :				Optional[int]=	[floats_list((1, x)      )[0] for x in range(800	,  1400	,  200      )]

            lowercase__    :				Dict=	["longest", "max_length", "do_not_pad"]
            lowercase__    :				List[str]=	[None, 16, None]
            for max_length, padding in zip(a_	,  a_      ):
                  lowercase__    :				Tuple=	feature_extractor(
                      a_	,  max_length=a_	,  padding=a_	,  return_tensors="np"	,  return_attention_mask=a_      )
                  lowercase__    :				Dict=	inputs.input_features
                  lowercase__    :				int=	inputs.attention_mask
                  lowercase__    :				List[Any]=	[np.sum(a_      ) for x in attention_mask]

                  self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]      )
                  self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6      )
                  self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]      )
                  self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6      )
                  self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]      )




      def 		UpperCAmelCase_	(       self      ):
            '''simple docstring'''
            lowercase__    :				Dict=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
            lowercase__    :				str=	[floats_list((1, x)      )[0] for x in range(800	,  1400	,  200      )]
            lowercase__    :				Any=	feature_extractor(
                a_	,  padding="max_length"	,  max_length=4	,  truncation=a_	,  return_tensors="np"	,  return_attention_mask=a_	,  )
            lowercase__    :				Union[str, Any]=	inputs.input_features
            lowercase__    :				List[Any]=	inputs.attention_mask
            lowercase__    :				Dict=	np.sum(attention_mask == 1	,  axis=1      )

            self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]      )
            self._check_zero_mean_unit_variance(input_features[1]      )
            self._check_zero_mean_unit_variance(input_features[2]      )




      def 		UpperCAmelCase_	(       self      ):
            '''simple docstring'''
            lowercase__    :				Tuple=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
            lowercase__    :				Optional[Any]=	[floats_list((1, x)      )[0] for x in range(800	,  1400	,  200      )]
            lowercase__    :				List[str]=	feature_extractor(
                a_	,  padding="longest"	,  max_length=4	,  truncation=a_	,  return_tensors="np"	,  return_attention_mask=a_	,  )
            lowercase__    :				Any=	inputs.input_features
            lowercase__    :				str=	inputs.attention_mask
            lowercase__    :				int=	np.sum(attention_mask == 1	,  axis=1      )

            self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]      )
            self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]      )
            self._check_zero_mean_unit_variance(input_features[2]      )

            # make sure that if max_length < longest -> then pad to max_length
            self.assertEqual(input_features.shape	,  (3, 4, 24)      )

            lowercase__    :				Optional[Any]=	[floats_list((1, x)      )[0] for x in range(800	,  1400	,  200      )]
            lowercase__    :				Any=	feature_extractor(
                a_	,  padding="longest"	,  max_length=16	,  truncation=a_	,  return_tensors="np"	,  return_attention_mask=a_	,  )
            lowercase__    :				List[str]=	inputs.input_features
            lowercase__    :				Optional[int]=	inputs.attention_mask
            lowercase__    :				List[Any]=	np.sum(attention_mask == 1	,  axis=1      )

            self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]      )
            self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]      )
            self._check_zero_mean_unit_variance(input_features[2]      )

            # make sure that if max_length < longest -> then pad to max_length
            self.assertEqual(input_features.shape	,  (3, 6, 24)      )




      def 		UpperCAmelCase_	(       self      ):
            '''simple docstring'''
            import torch

            lowercase__    :				int=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
            lowercase__    :				Dict=	np.random.rand(100	,  32      ).astype(np.floataa      )
            lowercase__    :				Dict=	np_speech_inputs.tolist()

            for inputs in [py_speech_inputs, np_speech_inputs]:
                  lowercase__    :				Tuple=	feature_extractor.pad([{"input_features": inputs}]	,  return_tensors="np"      )
                  self.assertTrue(np_processed.input_features.dtype == np.floataa      )
                  lowercase__    :				Dict=	feature_extractor.pad([{"input_features": inputs}]	,  return_tensors="pt"      )
                  self.assertTrue(pt_processed.input_features.dtype == torch.floataa      )




      def 		UpperCAmelCase_	(       self	,  snake_case__      ):
            '''simple docstring'''
            from datasets import load_dataset

            lowercase__    :				Dict=	load_dataset("hf-internal-testing/librispeech_asr_dummy"	,  "clean"	,  split="validation"      )
            # automatic decoding with librispeech
            lowercase__    :				Union[str, Any]=	ds.sort("id"      ).select(range(a_      )      )[:num_samples]["audio"]

            return [x["array"] for x in speech_samples]




      def 		UpperCAmelCase_	(       self      ):
            '''simple docstring'''
            lowercase__    :				str=	np.array([
                -1.57_45, -1.77_13, -1.70_20, -1.60_69, -1.22_50, -1.11_05, -0.90_72, -0.82_41,
                -1.23_10, -0.80_98, -0.33_20, -0.41_01, -0.79_85, -0.49_96, -0.82_13, -0.91_28,
                -1.04_20, -1.12_86, -1.04_40, -0.79_99, -0.84_05, -1.22_75, -1.54_43, -1.46_25,
            ]      )
            # fmt: on

            lowercase__    :				Tuple=	self._load_datasamples(1      )
            lowercase__    :				Optional[int]=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
            lowercase__    :				Optional[int]=	feature_extractor(a_	,  return_tensors="pt"      ).input_features
            self.assertEquals(input_features.shape	,  (1, 584, 24)      )
            self.assertTrue(np.allclose(input_features[0, 0, :30]	,  a_	,  atol=1e-4      )      )














"""simple docstring"""







import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import (
			    AutoProcessor,
			    BertTokenizerFast,
			    BlipImageProcessor,
			    GPTaTokenizer,
			    InstructBlipProcessor,
			    PreTrainedTokenizerFast,
			)


@require_vision
class    __UpperCAmelCase(       unittest.TestCase							):




						"""simple docstring"""




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[str]=	tempfile.mkdtemp()

												lowercase__    :				Union[str, Any]=	BlipImageProcessor()
												lowercase__    :				Any=	GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model"      )
												lowercase__    :				Any=	BertTokenizerFast.from_pretrained("hf-internal-testing/tiny-random-bert"      )

												lowercase__    :				str=	InstructBlipProcessor(snake_case__	,  snake_case__	,  snake_case__      )

												processor.save_pretrained(self.tmpdirname      )




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return AutoProcessor.from_pretrained(self.tmpdirname	,  **snake_case__      ).tokenizer




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return AutoProcessor.from_pretrained(self.tmpdirname	,  **snake_case__      ).image_processor




						def 		UpperCAmelCase_	(       self	,  **snake_case__      ):
												'''simple docstring'''
												return AutoProcessor.from_pretrained(self.tmpdirname	,  **snake_case__      ).qformer_tokenizer




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												shutil.rmtree(self.tmpdirname      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Union[str, Any]=	[np.random.randint(255	,  size=(3, 30, 400)	,  dtype=np.uinta      )]

												lowercase__    :				Union[str, Any]=	[Image.fromarray(np.moveaxis(snake_case__	,  0	,  -1      )      ) for x in image_inputs]

												return image_inputs




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				str=	InstructBlipProcessor(
												    tokenizer=self.get_tokenizer()	,  image_processor=self.get_image_processor()	,  qformer_tokenizer=self.get_qformer_tokenizer()	,  )
												processor.save_pretrained(self.tmpdirname      )

												lowercase__    :				Optional[Any]=	self.get_tokenizer(bos_token="(BOS)"	,  eos_token="(EOS)"      )
												lowercase__    :				Optional[Any]=	self.get_image_processor(do_normalize=snake_case__	,  padding_value=1.0      )

												lowercase__    :				Optional[Any]=	InstructBlipProcessor.from_pretrained(
												    self.tmpdirname	,  bos_token="(BOS)"	,  eos_token="(EOS)"	,  do_normalize=snake_case__	,  padding_value=1.0      )

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

												self.assertEqual(processor.image_processor.to_json_string()	,  image_processor_add_kwargs.to_json_string()      )
												self.assertIsInstance(processor.image_processor	,  snake_case__      )
												self.assertIsInstance(processor.qformer_tokenizer	,  snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	self.get_image_processor()
												lowercase__    :				Optional[int]=	self.get_tokenizer()
												lowercase__    :				Any=	self.get_qformer_tokenizer()

												lowercase__    :				Any=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Tuple=	self.prepare_image_inputs()

												lowercase__    :				int=	image_processor(snake_case__	,  return_tensors="np"      )
												lowercase__    :				List[str]=	processor(images=snake_case__	,  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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	self.get_image_processor()
												lowercase__    :				List[str]=	self.get_tokenizer()
												lowercase__    :				Optional[Any]=	self.get_qformer_tokenizer()

												lowercase__    :				Optional[Any]=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Optional[Any]=	"lower newer"

												lowercase__    :				Any=	processor(text=snake_case__      )

												lowercase__    :				Union[str, Any]=	tokenizer(snake_case__	,  return_token_type_ids=snake_case__      )
												lowercase__    :				List[Any]=	qformer_tokenizer(snake_case__	,  return_token_type_ids=snake_case__      )

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

												for key in encoded_tokens_qformer.keys():
																		self.assertListEqual(encoded_tokens_qformer[key]	,  encoded_processor["qformer_" + key]      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				int=	self.get_image_processor()
												lowercase__    :				Optional[int]=	self.get_tokenizer()
												lowercase__    :				Any=	self.get_qformer_tokenizer()

												lowercase__    :				Optional[int]=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				List[str]=	"lower newer"
												lowercase__    :				Optional[int]=	self.prepare_image_inputs()

												lowercase__    :				Dict=	processor(text=snake_case__	,  images=snake_case__      )

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

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




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Optional[Any]=	self.get_image_processor()
												lowercase__    :				int=	self.get_tokenizer()
												lowercase__    :				Optional[int]=	self.get_qformer_tokenizer()

												lowercase__    :				Any=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Dict=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

												lowercase__    :				Optional[Any]=	processor.batch_decode(snake_case__      )
												lowercase__    :				List[Any]=	tokenizer.batch_decode(snake_case__      )

												self.assertListEqual(snake_case__	,  snake_case__      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				str=	self.get_image_processor()
												lowercase__    :				Optional[int]=	self.get_tokenizer()
												lowercase__    :				Optional[Any]=	self.get_qformer_tokenizer()

												lowercase__    :				int=	InstructBlipProcessor(
												    tokenizer=snake_case__	,  image_processor=snake_case__	,  qformer_tokenizer=snake_case__      )

												lowercase__    :				Union[str, Any]=	"lower newer"
												lowercase__    :				int=	self.prepare_image_inputs()

												lowercase__    :				Dict=	processor(text=snake_case__	,  images=snake_case__      )

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






