




import inspect
import tempfile
import unittest

from huggingface_hub import hf_hub_download

from transformers import is_torch_available
from transformers.testing_utils import is_flaky, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


lowerCamelCase    :		Tuple			   =       1e-4

if is_torch_available():
 import torch

 from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel
 from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder







@require_torch
class   A:



 '''simple docstring'''


 def __init__(    self :  Optional[int]						,							A_ :  str						,							A_ :  Dict=16						,							A_ :  int=13						,							A_ :  Tuple=7						,							A_ :  Tuple=14						,							A_ :  List[Any]=10						,							A_ :  Union[str, Any]=19						,							A_ :  List[Any]=5						,							A_ :  Union[str, Any]=4						,							A_ :  int=True						,							A_ :  List[Any]=16						,							A_ :  Dict=2						,							A_ :  Union[str, Any]=4						,							A_ :  Any=4						,							A_ :  Any="gelu"						,							A_ :  Optional[int]=0.1						,							A_ :  List[Any]=0.1						,							A_ :  Optional[int]=[1, 2, 3, 4, 5]						,							A_ :  Optional[Any]=25						,							A_ :  int=5						,							)       ->     Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_							=   d_model
       lowerCamelCase_							=   parent
       lowerCamelCase_							=   batch_size
       lowerCamelCase_							=   prediction_length
       lowerCamelCase_							=   context_length
       lowerCamelCase_							=   cardinality
       lowerCamelCase_							=   num_time_features
       lowerCamelCase_							=   lags_sequence
       lowerCamelCase_							=   embedding_dimension
       lowerCamelCase_							=   is_training
       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_							=   context_length
       lowerCamelCase_							=   prediction_length + label_length
       lowerCamelCase_							=   label_length

       lowerCamelCase_							=   moving_average
       lowerCamelCase_							=   autocorrelation_factor

 def     a__			(    self :  Tuple  )       ->     Any:

       """simple docstring"""






       return AutoformerConfig(
           d_model=self.d_model						,							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						,							prediction_length=self.prediction_length						,							context_length=self.context_length						,							label_length=self.label_length						,							lags_sequence=self.lags_sequence						,							num_time_features=self.num_time_features						,							num_static_categorical_features=1						,							cardinality=[self.cardinality]						,							embedding_dimension=[self.embedding_dimension]						,							moving_average=self.moving_average						,							)

 def     a__			(    self :  Dict						,							A_ :  List[str]  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_							=   config.context_length + max(config.lags_sequence  )

       lowerCamelCase_							=   ids_tensor([self.batch_size, 1]						,							config.cardinality[0]  )
       lowerCamelCase_							=   floats_tensor([self.batch_size, _past_length, config.num_time_features]  )
       lowerCamelCase_							=   floats_tensor([self.batch_size, _past_length]  )
       lowerCamelCase_							=   floats_tensor([self.batch_size, _past_length]  ) > 0.5

       # decoder inputs
       lowerCamelCase_							=   floats_tensor([self.batch_size, config.prediction_length, config.num_time_features]  )
       lowerCamelCase_							=   floats_tensor([self.batch_size, config.prediction_length]  )

       lowerCamelCase_							=   {
           'past_values': past_values,
           'static_categorical_features': static_categorical_features,
           'past_time_features': past_time_features,
           'past_observed_mask': past_observed_mask,
           'future_time_features': future_time_features,
           'future_values': future_values,
       }
       return inputs_dict

 def     a__			(    self :  str  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_							=   self.get_config()
       lowerCamelCase_							=   self.prepare_autoformer_inputs_dict(A_  )
       return config, inputs_dict

 def     a__			(    self :  List[str]  )       ->     Optional[Any]:

       """simple docstring"""






       lowerCamelCase_					,    lowerCamelCase_							=   self.prepare_config_and_inputs()
       return config, inputs_dict





 def     a__			(    self :  str						,							A_ :  str						,							A_ :  List[str]  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_							=   AutoformerModel(config=A_  ).to(A_  ).eval()
       lowerCamelCase_							=   model(**A_  )

       lowerCamelCase_							=   outputs.encoder_last_hidden_state
       lowerCamelCase_							=   outputs.last_hidden_state

       with tempfile.TemporaryDirectory() as tmpdirname:
             lowerCamelCase_							=   model.get_encoder()
             encoder.save_pretrained(A_  )
             lowerCamelCase_							=   AutoformerEncoder.from_pretrained(A_  ).to(A_  )

       lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_							=   model.create_network_inputs(**A_  )
       lowerCamelCase_					,    lowerCamelCase_							=   model.decomposition_layer(transformer_inputs[:, : config.context_length, ...]  )

       lowerCamelCase_							=   torch.cat(
           (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...])						,							dim=-1						,							)
       lowerCamelCase_							=   encoder(inputs_embeds=A_  )[0]
       self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1E-3  )

       lowerCamelCase_							=   (
           torch.mean(transformer_inputs[:, : config.context_length, ...]						,							dim=1  )
           .unsqueeze(1  )
           .repeat(1						,							config.prediction_length						,							1  )
       )
       lowerCamelCase_							=   torch.zeros(
           [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]]						,							device=enc_input.device						,							)

       lowerCamelCase_							=   torch.cat(
           (
               torch.cat((seasonal_input[:, -config.label_length :, ...], zeros)						,							dim=1  ),
               feature[:, config.context_length - config.label_length :, ...],
           )						,							dim=-1						,							)
       lowerCamelCase_							=   torch.cat(
           (
               torch.cat((trend_input[:, -config.label_length :, ...], mean)						,							dim=1  ),
               feature[:, config.context_length - config.label_length :, ...],
           )						,							dim=-1						,							)

       with tempfile.TemporaryDirectory() as tmpdirname:
             lowerCamelCase_							=   model.get_decoder()
             decoder.save_pretrained(A_  )
             lowerCamelCase_							=   AutoformerDecoder.from_pretrained(A_  ).to(A_  )

       lowerCamelCase_							=   decoder(
           trend=A_						,							inputs_embeds=A_						,							encoder_hidden_states=A_						,							)[0]

       self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1E-3  )







@require_torch
class   A(						UpperCamelCase							,				UpperCamelCase							,				unittest.TestCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						(AutoformerModel, AutoformerForPrediction) if is_torch_available() else ()
 UpperCamelCase						       =						(AutoformerForPrediction,) if is_torch_available() else ()
 UpperCamelCase						       =						{'''feature-extraction''': AutoformerModel} if is_torch_available() else {}
 UpperCamelCase						       =						False
 UpperCamelCase						       =						False
 UpperCamelCase						       =						False
 UpperCamelCase						       =						False
 UpperCamelCase						       =						False
 UpperCamelCase						       =						False

 def     a__			(    self :  List[str]  )       ->     Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_							=   AutoformerModelTester(self  )
       lowerCamelCase_							=   ConfigTester(self						,							config_class=A_						,							has_text_modality=A_  )

 def     a__			(    self :  int  )       ->     Dict:

       """simple docstring"""






       self.config_tester.run_common_tests()

 def     a__			(    self :  int  )       ->     Tuple:

       """simple docstring"""






       lowerCamelCase_					,    lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
       for model_class in self.all_model_classes:
             lowerCamelCase_							=   model_class(A_  )

             with tempfile.TemporaryDirectory() as tmpdirname:
                   model.save_pretrained(A_  )
                   lowerCamelCase_					,    lowerCamelCase_							=   model_class.from_pretrained(A_						,							output_loading_info=A_  )
             self.assertEqual(info['missing_keys']						,							[]  )

 def     a__			(    self :  int  )       ->     Union[str, Any]:

       """simple docstring"""






       lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.check_encoder_decoder_model_standalone(*A_  )

 @unittest.skip(reason='Model has no tokens embeddings'  )
 def     a__			(    self :  Union[str, Any]  )       ->     List[str]:

       """simple docstring"""






       pass

 def     a__			(    self :  Any  )       ->     int:

       """simple docstring"""






       lowerCamelCase_							=   inspect.signature(getattr(A_						,							'forward'  )  )
       # The main input is the name of the argument after `self`
       lowerCamelCase_							=   list(model_signature.parameters.keys()  )[1]
       self.assertEqual(AutoformerModel.main_input_name						,							A_  )

 def     a__			(    self :  Tuple  )       ->     int:

       """simple docstring"""






       lowerCamelCase_					,    lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             lowerCamelCase_							=   model_class(A_  )
             lowerCamelCase_							=   inspect.signature(model.forward  )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             lowerCamelCase_							=   [*signature.parameters.keys()]

             lowerCamelCase_							=   [
                 'past_values',
                 'past_time_features',
                 'past_observed_mask',
                 'static_categorical_features',
                 'static_real_features',
                 'future_values',
                 'future_time_features',
             ]

             if model.__class__.__name__ in ["AutoformerForPrediction"]:
                   expected_arg_names.append('future_observed_mask'  )

             expected_arg_names.extend(
                 [
                     'decoder_attention_mask',
                     'head_mask',
                     'decoder_head_mask',
                     'cross_attn_head_mask',
                     'encoder_outputs',
                     'past_key_values',
                     'output_hidden_states',
                     'output_attentions',
                     'use_cache',
                     'return_dict',
                 ]  )

             self.assertListEqual(arg_names[: len(A_  )]						,							A_  )

 def     a__			(    self :  Union[str, Any]  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_					,    lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs_for_common()
       lowerCamelCase_							=   True

       lowerCamelCase_							=   getattr(self.model_tester						,							'seq_length'						,							A_  )
       lowerCamelCase_							=   getattr(self.model_tester						,							'decoder_seq_length'						,							A_  )
       lowerCamelCase_							=   getattr(self.model_tester						,							'encoder_seq_length'						,							A_  )
       lowerCamelCase_							=   getattr(self.model_tester						,							'd_model'						,							A_  )
       lowerCamelCase_							=   getattr(self.model_tester						,							'num_attention_heads'						,							A_  )
       lowerCamelCase_							=   d_model // num_attention_heads

       for model_class in self.all_model_classes:
             lowerCamelCase_							=   True
             lowerCamelCase_							=   False
             lowerCamelCase_							=   True
             lowerCamelCase_							=   model_class(A_  )
             model.to(A_  )
             model.eval()
             with torch.no_grad():
                   lowerCamelCase_							=   model(**self._prepare_for_class(A_						,							A_  )  )
             lowerCamelCase_							=   outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
             self.assertEqual(len(A_  )						,							self.model_tester.num_hidden_layers  )

             # check that output_attentions also work using config
             del inputs_dict["output_attentions"]
             lowerCamelCase_							=   True
             lowerCamelCase_							=   model_class(A_  )
             model.to(A_  )
             model.eval()
             with torch.no_grad():
                   lowerCamelCase_							=   model(**self._prepare_for_class(A_						,							A_  )  )
             lowerCamelCase_							=   outputs.encoder_attentions
             self.assertEqual(len(A_  )						,							self.model_tester.num_hidden_layers  )

             self.assertListEqual(
                 list(attentions[0].shape[-3:]  )						,							[self.model_tester.num_attention_heads, encoder_seq_length, dim]						,							)
             lowerCamelCase_							=   len(A_  )

             lowerCamelCase_							=   7

             if "last_hidden_state" in outputs:
                   correct_outlen += 1

             if "trend" in outputs:
                   correct_outlen += 1

             if "past_key_values" in outputs:
                   correct_outlen += 1  # past_key_values have been returned

             if "loss" in outputs:
                   correct_outlen += 1

             if "params" in outputs:
                   correct_outlen += 1

             self.assertEqual(A_						,							A_  )

             # decoder attentions
             lowerCamelCase_							=   outputs.decoder_attentions
             self.assertIsInstance(A_						,							(list, tuple)  )
             self.assertEqual(len(A_  )						,							self.model_tester.num_hidden_layers  )
             self.assertListEqual(
                 list(decoder_attentions[0].shape[-3:]  )						,							[self.model_tester.num_attention_heads, decoder_seq_length, dim]						,							)

             # cross attentions
             lowerCamelCase_							=   outputs.cross_attentions
             self.assertIsInstance(A_						,							(list, tuple)  )
             self.assertEqual(len(A_  )						,							self.model_tester.num_hidden_layers  )
             self.assertListEqual(
                 list(cross_attentions[0].shape[-3:]  )						,							[self.model_tester.num_attention_heads, decoder_seq_length, dim]						,							)

       # Check attention is always last and order is fine
       lowerCamelCase_							=   True
       lowerCamelCase_							=   True
       lowerCamelCase_							=   model_class(A_  )
       model.to(A_  )
       model.eval()
       with torch.no_grad():
             lowerCamelCase_							=   model(**self._prepare_for_class(A_						,							A_  )  )

       self.assertEqual(out_len + 2						,							len(A_  )  )

       lowerCamelCase_							=   outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

       self.assertEqual(len(A_  )						,							self.model_tester.num_hidden_layers  )
       self.assertListEqual(
           list(self_attentions[0].shape[-3:]  )						,							[self.model_tester.num_attention_heads, encoder_seq_length, dim]						,							)





 @is_flaky()
 def     a__			(    self :  Optional[int]  )       ->     Any:

       """simple docstring"""






       super().test_retain_grad_hidden_states_attentions()

def       _SCREAMING_SNAKE_CASE      (							lowercase     :	int="train-batch.pt"					):


      '''simple docstring'''



      lowerCamelCase_							=   hf_hub_download(repo_id='hf-internal-testing/tourism-monthly-batch'     ,							filename=lowercase     ,							repo_type='dataset'					)
      lowerCamelCase_							=   torch.load(lowercase     ,							map_location=lowercase					)
      return batch







@require_torch
@slow
class   A(						unittest.TestCase       ):



 '''simple docstring'''


 def     a__			(    self :  Any  )       ->     str:

       """simple docstring"""






       lowerCamelCase_							=   AutoformerModel.from_pretrained('huggingface/autoformer-tourism-monthly'  ).to(A_  )
       lowerCamelCase_							=   prepare_batch()

       with torch.no_grad():
             lowerCamelCase_							=   model(
                 past_values=batch['past_values']						,							past_time_features=batch['past_time_features']						,							past_observed_mask=batch['past_observed_mask']						,							static_categorical_features=batch['static_categorical_features']						,							future_values=batch['future_values']						,							future_time_features=batch['future_time_features']						,							)[0]

       lowerCamelCase_							=   torch.Size(
           (64, model.config.prediction_length + model.config.label_length, model.config.feature_size)  )
       self.assertEqual(output.shape						,							A_  )

       lowerCamelCase_							=   torch.tensor(
           [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]]						,							device=A_  )
       self.assertTrue(torch.allclose(output[0, :3, :3]						,							A_						,							atol=A_  )  )

 def     a__			(    self :  str  )       ->     str:

       """simple docstring"""






       lowerCamelCase_							=   AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly'  ).to(A_  )
       lowerCamelCase_							=   prepare_batch('val-batch.pt'  )
       with torch.no_grad():
             lowerCamelCase_							=   model(
                 past_values=batch['past_values']						,							past_time_features=batch['past_time_features']						,							past_observed_mask=batch['past_observed_mask']						,							static_categorical_features=batch['static_categorical_features']						,							).encoder_last_hidden_state
       lowerCamelCase_							=   torch.Size((64, model.config.context_length, model.config.d_model)  )
       self.assertEqual(output.shape						,							A_  )

       lowerCamelCase_							=   torch.tensor(
           [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]]						,							device=A_  )
       self.assertTrue(torch.allclose(output[0, :3, :3]						,							A_						,							atol=A_  )  )





 def     a__			(    self :  Optional[Any]  )       ->     Tuple:

       """simple docstring"""






       lowerCamelCase_							=   AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly'  ).to(A_  )
       lowerCamelCase_							=   prepare_batch('val-batch.pt'  )
       with torch.no_grad():
             lowerCamelCase_							=   model.generate(
                 static_categorical_features=batch['static_categorical_features']						,							past_time_features=batch['past_time_features']						,							past_values=batch['past_values']						,							future_time_features=batch['future_time_features']						,							past_observed_mask=batch['past_observed_mask']						,							)
       lowerCamelCase_							=   torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length)  )
       self.assertEqual(outputs.sequences.shape						,							A_  )

       lowerCamelCase_							=   torch.tensor([3130.6763, 4056.5293, 7053.0786]						,							device=A_  )
       lowerCamelCase_							=   outputs.sequences.mean(dim=1  )
       self.assertTrue(torch.allclose(mean_prediction[0, -3:]						,							A_						,							rtol=1E-1  )  )









import io
import math
from typing import Dict, Optional, Union

import numpy as np
from huggingface_hub import hf_hub_download

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import convert_to_rgb, normalize, to_channel_dimension_format, to_pil_image
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    get_image_size,
    infer_channel_dimension_format,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_vision_available, logging
from ...utils.import_utils import requires_backends


if is_vision_available():
 import textwrap

 from PIL import Image, ImageDraw, ImageFont

if is_torch_available():
 import torch

 from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
 lowerCamelCase    :		Optional[Any]			   =       False


lowerCamelCase    :		Union[str, Any]			   =       logging.get_logger(__name__)
lowerCamelCase    :		Any			   =       "ybelkada/fonts"

def       _SCREAMING_SNAKE_CASE      (							):


      '''simple docstring'''



      if is_torch_available() and not is_torch_greater_or_equal_than_1_11:
            raise ImportError(
                f"""You are using torch=={torch.__version__}, but torch>=1.11.0 is required to use """
                'Pix2StructImageProcessor. Please upgrade torch.'					)

def       _SCREAMING_SNAKE_CASE      (							lowercase     :	Dict     ,							lowercase     :	List[str]     ,							lowercase     :	List[Any]					):


      '''simple docstring'''



      requires_backends(lowercase     ,							['torch']					)
      _check_torch_version()

      lowerCamelCase_							=   image_tensor.unsqueeze(0					)
      lowerCamelCase_							=   torch.nn.functional.unfold(lowercase     ,							(patch_height, patch_width)     ,							stride=(patch_height, patch_width)					)
      lowerCamelCase_							=   patches.reshape(image_tensor.size(0					)     ,							image_tensor.size(1					)     ,							lowercase     ,							lowercase     ,							-1					)
      lowerCamelCase_							=   patches.permute(0     ,							4     ,							2     ,							3     ,							1					).reshape(
          image_tensor.size(2					) // patch_height     ,							image_tensor.size(3					) // patch_width     ,							image_tensor.size(1					) * patch_height * patch_width     ,							)
      return patches.unsqueeze(0					)

def       _SCREAMING_SNAKE_CASE      (							lowercase     :	str     ,							lowercase     :	int = 36     ,							lowercase     :	str = "black"     ,							lowercase     :	str = "white"     ,							lowercase     :	int = 5     ,							lowercase     :	int = 5     ,							lowercase     :	int = 5     ,							lowercase     :	int = 5     ,							lowercase     :	Optional[bytes] = None     ,							lowercase     :	Optional[str] = None     ,							):


      '''simple docstring'''



      requires_backends(lowercase     ,							'vision'					)
      # Add new lines so that each line is no more than 80 characters.

      lowerCamelCase_							=   textwrap.TextWrapper(width=80					)
      lowerCamelCase_							=   wrapper.wrap(text=lowercase					)
      lowerCamelCase_							=   '\n'.join(lowercase					)

      if font_bytes is not None and font_path is None:
            lowerCamelCase_							=   io.BytesIO(lowercase					)
      elif font_path is not None:
            lowerCamelCase_							=   font_path
      else:
            lowerCamelCase_							=   hf_hub_download(lowercase     ,							'Arial.TTF'					)
      lowerCamelCase_							=   ImageFont.truetype(lowercase     ,							encoding='UTF-8'     ,							size=lowercase					)

      # Use a temporary canvas to determine the width and height in pixels when
      # rendering the text.
      lowerCamelCase_							=   ImageDraw.Draw(Image.new('RGB'     ,							(1, 1)     ,							lowercase					)					)
      lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_							=   temp_draw.textbbox((0, 0)     ,							lowercase     ,							lowercase					)

      # Create the actual image with a bit of padding around the text.
      lowerCamelCase_							=   text_width + left_padding + right_padding
      lowerCamelCase_							=   text_height + top_padding + bottom_padding
      lowerCamelCase_							=   Image.new('RGB'     ,							(image_width, image_height)     ,							lowercase					)
      lowerCamelCase_							=   ImageDraw.Draw(lowercase					)
      draw.text(xy=(left_padding, top_padding)     ,							text=lowercase     ,							fill=lowercase     ,							font=lowercase					)
      return image

def       _SCREAMING_SNAKE_CASE      (							lowercase     :	np.ndarray     ,							lowercase     :	str     ,							**lowercase     :	List[Any]					):


      '''simple docstring'''



      requires_backends(lowercase     ,							'vision'					)

      # Convert to PIL image if necessary
      lowerCamelCase_							=   to_pil_image(lowercase					)

      lowerCamelCase_							=   render_text(lowercase     ,							**lowercase					)
      lowerCamelCase_							=   max(header_image.width     ,							image.width					)

      lowerCamelCase_							=   int(image.height * (new_width / image.width)					)
      lowerCamelCase_							=   int(header_image.height * (new_width / header_image.width)					)

      lowerCamelCase_							=   Image.new('RGB'     ,							(new_width, new_height + new_header_height)     ,							'white'					)
      new_image.paste(header_image.resize((new_width, new_header_height)					)     ,							(0, 0)					)
      new_image.paste(image.resize((new_width, new_height)					)     ,							(0, new_header_height)					)

      # Convert back to the original framework if necessary
      lowerCamelCase_							=   to_numpy_array(lowercase					)

      if infer_channel_dimension_format(lowercase					) == ChannelDimension.LAST:
            lowerCamelCase_							=   to_channel_dimension_format(lowercase     ,							ChannelDimension.LAST					)

      return new_image







class   A(						UpperCamelCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						['''flattened_patches''']

 def __init__(    self :  Dict						,							A_ :  bool = True						,							A_ :  bool = True						,							A_ :  Dict[str, int] = None						,							A_ :  int = 2048						,							A_ :  bool = False						,							**A_ :  str						,							)       ->     None:

       """simple docstring"""






       super().__init__(**A_  )
       lowerCamelCase_							=   patch_size if patch_size is not None else {'height': 16, 'width': 16}
       lowerCamelCase_							=   do_normalize
       lowerCamelCase_							=   do_convert_rgb
       lowerCamelCase_							=   max_patches
       lowerCamelCase_							=   is_vqa

 def     a__			(    self :  Union[str, Any]						,							A_ :  np.ndarray						,							A_ :  int						,							A_ :  dict						,							**A_ :  Any  )       ->     np.ndarray:

       """simple docstring"""






       requires_backends(self.extract_flattened_patches						,							'torch'  )
       _check_torch_version()

       # convert to torch
       lowerCamelCase_							=   to_channel_dimension_format(A_						,							ChannelDimension.FIRST  )
       lowerCamelCase_							=   torch.from_numpy(A_  )

       lowerCamelCase_					,    lowerCamelCase_							=   patch_size['height'], patch_size['width']
       lowerCamelCase_					,    lowerCamelCase_							=   get_image_size(A_  )

       # maximize scale s.t.
       lowerCamelCase_							=   math.sqrt(max_patches * (patch_height / image_height) * (patch_width / image_width)  )
       lowerCamelCase_							=   max(min(math.floor(scale * image_height / patch_height  )						,							A_  )						,							1  )
       lowerCamelCase_							=   max(min(math.floor(scale * image_width / patch_width  )						,							A_  )						,							1  )
       lowerCamelCase_							=   max(num_feasible_rows * patch_height						,							1  )
       lowerCamelCase_							=   max(num_feasible_cols * patch_width						,							1  )

       lowerCamelCase_							=   torch.nn.functional.interpolate(
           image.unsqueeze(0  )						,							size=(resized_height, resized_width)						,							mode='bilinear'						,							align_corners=A_						,							antialias=A_						,							).squeeze(0  )

       # [1, rows, columns, patch_height * patch_width * image_channels]
       lowerCamelCase_							=   torch_extract_patches(A_						,							A_						,							A_  )

       lowerCamelCase_							=   patches.shape
       lowerCamelCase_							=   patches_shape[1]
       lowerCamelCase_							=   patches_shape[2]
       lowerCamelCase_							=   patches_shape[3]

       # [rows * columns, patch_height * patch_width * image_channels]
       lowerCamelCase_							=   patches.reshape([rows * columns, depth]  )

       # [rows * columns, 1]
       lowerCamelCase_							=   torch.arange(A_  ).reshape([rows, 1]  ).repeat(1						,							A_  ).reshape([rows * columns, 1]  )
       lowerCamelCase_							=   torch.arange(A_  ).reshape([1, columns]  ).repeat(A_						,							1  ).reshape([rows * columns, 1]  )

       # Offset by 1 so the ids do not contain zeros, which represent padding.
       row_ids += 1
       col_ids += 1

       # Prepare additional patch features.
       # [rows * columns, 1]
       lowerCamelCase_							=   row_ids.to(torch.floataa  )
       lowerCamelCase_							=   col_ids.to(torch.floataa  )

       # [rows * columns, 2 + patch_height * patch_width * image_channels]
       lowerCamelCase_							=   torch.cat([row_ids, col_ids, patches]						,							-1  )

       # [max_patches, 2 + patch_height * patch_width * image_channels]
       lowerCamelCase_							=   torch.nn.functional.pad(A_						,							[0, 0, 0, max_patches - (rows * columns)]  ).float()

       lowerCamelCase_							=   to_numpy_array(A_  )

       return result

 def     a__			(    self :  Optional[Any]						,							A_ :  np.ndarray						,							A_ :  Optional[Union[str, ChannelDimension]] = None						,							**A_ :  str  )       ->     np.ndarray:

       """simple docstring"""






       if image.dtype == np.uinta:
             lowerCamelCase_							=   image.astype(np.floataa  )

       # take mean across the whole `image`
       lowerCamelCase_							=   np.mean(A_  )
       lowerCamelCase_							=   np.std(A_  )
       lowerCamelCase_							=   max(A_						,							1.0 / math.sqrt(np.prod(image.shape  )  )  )

       return normalize(A_						,							mean=A_						,							std=A_						,							**A_  )





 def     a__			(    self :  Optional[Any]						,							A_ :  ImageInput						,							A_ :  Optional[str] = None						,							A_ :  bool = None						,							A_ :  Optional[bool] = None						,							A_ :  Optional[int] = None						,							A_ :  Optional[Dict[str, int]] = None						,							A_ :  Optional[Union[str, TensorType]] = None						,							A_ :  ChannelDimension = ChannelDimension.FIRST						,							**A_ :  Optional[int]						,							)       ->     ImageInput:

       """simple docstring"""






       lowerCamelCase_							=   do_normalize if do_normalize is not None else self.do_normalize
       lowerCamelCase_							=   do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb
       lowerCamelCase_							=   patch_size if patch_size is not None else self.patch_size
       lowerCamelCase_							=   max_patches if max_patches is not None else self.max_patches
       lowerCamelCase_							=   self.is_vqa

       if kwargs.get('data_format'						,							A_  ) is not None:
             raise ValueError('data_format is not an accepted input as the outputs are '  )

       lowerCamelCase_							=   make_list_of_images(A_  )

       if not valid_images(A_  ):
             raise ValueError(
                 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
                 'torch.Tensor, tf.Tensor or jax.ndarray.'  )

       # PIL RGBA images are converted to RGB
       if do_convert_rgb:
             lowerCamelCase_							=   [convert_to_rgb(A_  ) for image in images]

       # All transformations expect numpy arrays.
       lowerCamelCase_							=   [to_numpy_array(A_  ) for image in images]

       if is_vqa:
             if header_text is None:
                   raise ValueError('A header text must be provided for VQA models.'  )
             lowerCamelCase_							=   kwargs.pop('font_bytes'						,							A_  )
             lowerCamelCase_							=   kwargs.pop('font_path'						,							A_  )

             if isinstance(A_						,							A_  ):
                   lowerCamelCase_							=   [header_text] * len(A_  )

             lowerCamelCase_							=   [
                 render_header(A_						,							header_text[i]						,							font_bytes=A_						,							font_path=A_  )
                 for i, image in enumerate(A_  )
             ]

       if do_normalize:
             lowerCamelCase_							=   [self.normalize(image=A_  ) for image in images]

       # convert to torch tensor and permute
       lowerCamelCase_							=   [
           self.extract_flattened_patches(image=A_						,							max_patches=A_						,							patch_size=A_  )
           for image in images
       ]

       # create attention mask in numpy
       lowerCamelCase_							=   [(image.sum(axis=-1  ) != 0).astype(np.floataa  ) for image in images]

       lowerCamelCase_							=   BatchFeature(
           data={'flattened_patches': images, 'attention_mask': attention_masks}						,							tensor_type=A_  )

       return encoded_outputs



