


from dataclasses import dataclass
from typing import Optional

import numpy as np
import torch
import torch.nn as nn

from ..utils import BaseOutput, is_torch_version, randn_tensor
from .attention_processor import SpatialNorm
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block


@dataclass
class 							__a							(  __lowercase						):





		'''simple docstring'''



		UpperCAmelCase__      :				torch.FloatTensor






class 							__a							(  nn.Module						):





		'''simple docstring'''



		def __init__(						self ,    UpperCamelCase__=3 ,    UpperCamelCase__=3 ,    UpperCamelCase__=("DownEncoderBlock2D",) ,    UpperCamelCase__=(64,) ,    UpperCamelCase__=2 ,    UpperCamelCase__=32 ,    UpperCamelCase__="silu" ,    UpperCamelCase__=True ,    ):
						super().__init__()
						SCREAMING_SNAKE_CASE_   :	Dict													=   layers_per_block

						SCREAMING_SNAKE_CASE_   :	List[Any]													=   torch.nn.Convad(
						    __a ,    block_out_channels[0] ,    kernel_size=3 ,    stride=1 ,    padding=1 ,    )

						SCREAMING_SNAKE_CASE_   :	str													=   None
						SCREAMING_SNAKE_CASE_   :	str													=   nn.ModuleList([]       )

						# down
						SCREAMING_SNAKE_CASE_   :	List[str]													=   block_out_channels[0]
						for i, down_block_type in enumerate(__a       ):
										SCREAMING_SNAKE_CASE_   :	Optional[int]													=   output_channel
										SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   block_out_channels[i]
										SCREAMING_SNAKE_CASE_   :	List[str]													=   i == len(__a       ) - 1

										SCREAMING_SNAKE_CASE_   :	str													=   get_down_block(
										    __a ,    num_layers=self.layers_per_block ,    in_channels=__a ,    out_channels=__a ,    add_downsample=not is_final_block ,    resnet_eps=1E-6 ,    downsample_padding=0 ,    resnet_act_fn=__a ,    resnet_groups=__a ,    attention_head_dim=__a ,    temb_channels=__a ,    )
										self.down_blocks.append(__a       )

						# mid
						SCREAMING_SNAKE_CASE_   :	int													=   UNetMidBlockaD(
						    in_channels=block_out_channels[-1] ,    resnet_eps=1E-6 ,    resnet_act_fn=__a ,    output_scale_factor=1 ,    resnet_time_scale_shift='default' ,    attention_head_dim=block_out_channels[-1] ,    resnet_groups=__a ,    temb_channels=__a ,    )

						# out
						SCREAMING_SNAKE_CASE_   :	Tuple													=   nn.GroupNorm(num_channels=block_out_channels[-1] ,    num_groups=__a ,    eps=1E-6       )
						SCREAMING_SNAKE_CASE_   :	int													=   nn.SiLU()

						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   2 * out_channels if double_z else out_channels
						SCREAMING_SNAKE_CASE_   :	List[str]													=   nn.Convad(block_out_channels[-1] ,    __a ,    3 ,    padding=1       )

						SCREAMING_SNAKE_CASE_   :	str													=   False






		def   __snake_case			(						self ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	Tuple													=   x
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.conv_in(__a       )

						if self.training and self.gradient_checkpointing:

										def create_custom_forward(UpperCamelCase__       ):
														def custom_forward(*UpperCamelCase__       ):
																		return module(*__a       )

														return custom_forward

										# down
										if is_torch_version('>=' ,    '1.11.0'       ):
														for down_block in self.down_blocks:
																		SCREAMING_SNAKE_CASE_   :	Dict													=   torch.utils.checkpoint.checkpoint(
																		    create_custom_forward(__a       ) ,    __a ,    use_reentrant=__a       )
														# middle
														SCREAMING_SNAKE_CASE_   :	Optional[int]													=   torch.utils.checkpoint.checkpoint(
														    create_custom_forward(self.mid_block       ) ,    __a ,    use_reentrant=__a       )
										else:
														for down_block in self.down_blocks:
																		SCREAMING_SNAKE_CASE_   :	int													=   torch.utils.checkpoint.checkpoint(create_custom_forward(__a       ) ,    __a       )
														# middle
														SCREAMING_SNAKE_CASE_   :	List[Any]													=   torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block       ) ,    __a       )

						else:
										# down
										for down_block in self.down_blocks:
														SCREAMING_SNAKE_CASE_   :	Tuple													=   down_block(__a       )

										# middle
										SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.mid_block(__a       )

						# post-process
						SCREAMING_SNAKE_CASE_   :	Tuple													=   self.conv_norm_out(__a       )
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   self.conv_act(__a       )
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   self.conv_out(__a       )

						return sample






class 							__a							(  nn.Module						):





		'''simple docstring'''



		def __init__(						self ,    UpperCamelCase__=3 ,    UpperCamelCase__=3 ,    UpperCamelCase__=("UpDecoderBlock2D",) ,    UpperCamelCase__=(64,) ,    UpperCamelCase__=2 ,    UpperCamelCase__=32 ,    UpperCamelCase__="silu" ,    UpperCamelCase__="group" ,    ):
						super().__init__()
						SCREAMING_SNAKE_CASE_   :	Dict													=   layers_per_block

						SCREAMING_SNAKE_CASE_   :	List[str]													=   nn.Convad(
						    __a ,    block_out_channels[-1] ,    kernel_size=3 ,    stride=1 ,    padding=1 ,    )

						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   None
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   nn.ModuleList([]       )

						SCREAMING_SNAKE_CASE_   :	Any													=   in_channels if norm_type == """spatial""" else None

						# mid
						SCREAMING_SNAKE_CASE_   :	List[str]													=   UNetMidBlockaD(
						    in_channels=block_out_channels[-1] ,    resnet_eps=1E-6 ,    resnet_act_fn=__a ,    output_scale_factor=1 ,    resnet_time_scale_shift='default' if norm_type == 'group' else norm_type ,    attention_head_dim=block_out_channels[-1] ,    resnet_groups=__a ,    temb_channels=__a ,    )

						# up
						SCREAMING_SNAKE_CASE_   :	str													=   list(reversed(__a       )       )
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   reversed_block_out_channels[0]
						for i, up_block_type in enumerate(__a       ):
										SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   output_channel
										SCREAMING_SNAKE_CASE_   :	str													=   reversed_block_out_channels[i]

										SCREAMING_SNAKE_CASE_   :	int													=   i == len(__a       ) - 1

										SCREAMING_SNAKE_CASE_   :	str													=   get_up_block(
										    __a ,    num_layers=self.layers_per_block + 1 ,    in_channels=__a ,    out_channels=__a ,    prev_output_channel=__a ,    add_upsample=not is_final_block ,    resnet_eps=1E-6 ,    resnet_act_fn=__a ,    resnet_groups=__a ,    attention_head_dim=__a ,    temb_channels=__a ,    resnet_time_scale_shift=__a ,    )
										self.up_blocks.append(__a       )
										SCREAMING_SNAKE_CASE_   :	Tuple													=   output_channel

						# out
						if norm_type == "spatial":
										SCREAMING_SNAKE_CASE_   :	int													=   SpatialNorm(block_out_channels[0] ,    __a       )
						else:
										SCREAMING_SNAKE_CASE_   :	str													=   nn.GroupNorm(num_channels=block_out_channels[0] ,    num_groups=__a ,    eps=1E-6       )
						SCREAMING_SNAKE_CASE_   :	int													=   nn.SiLU()
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   nn.Convad(block_out_channels[0] ,    __a ,    3 ,    padding=1       )

						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   False






		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__=None       ):
						SCREAMING_SNAKE_CASE_   :	List[str]													=   z
						SCREAMING_SNAKE_CASE_   :	str													=   self.conv_in(__a       )

						SCREAMING_SNAKE_CASE_   :	str													=   next(iter(self.up_blocks.parameters()       )       ).dtype
						if self.training and self.gradient_checkpointing:

										def create_custom_forward(UpperCamelCase__       ):
														def custom_forward(*UpperCamelCase__       ):
																		return module(*__a       )

														return custom_forward

										if is_torch_version('>=' ,    '1.11.0'       ):
														# middle
														SCREAMING_SNAKE_CASE_   :	List[str]													=   torch.utils.checkpoint.checkpoint(
														    create_custom_forward(self.mid_block       ) ,    __a ,    __a ,    use_reentrant=__a       )
														SCREAMING_SNAKE_CASE_   :	Dict													=   sample.to(__a       )

														# up
														for up_block in self.up_blocks:
																		SCREAMING_SNAKE_CASE_   :	List[Any]													=   torch.utils.checkpoint.checkpoint(
																		    create_custom_forward(__a       ) ,    __a ,    __a ,    use_reentrant=__a       )
										else:
														# middle
														SCREAMING_SNAKE_CASE_   :	Optional[int]													=   torch.utils.checkpoint.checkpoint(
														    create_custom_forward(self.mid_block       ) ,    __a ,    __a       )
														SCREAMING_SNAKE_CASE_   :	Any													=   sample.to(__a       )

														# up
														for up_block in self.up_blocks:
																		SCREAMING_SNAKE_CASE_   :	List[str]													=   torch.utils.checkpoint.checkpoint(create_custom_forward(__a       ) ,    __a ,    __a       )
						else:
										# middle
										SCREAMING_SNAKE_CASE_   :	Tuple													=   self.mid_block(__a ,    __a       )
										SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   sample.to(__a       )

										# up
										for up_block in self.up_blocks:
														SCREAMING_SNAKE_CASE_   :	str													=   up_block(__a ,    __a       )

        # post-process
						if latent_embeds is None:
										SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   self.conv_norm_out(__a       )
						else:
										SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   self.conv_norm_out(__a ,    __a       )
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.conv_act(__a       )
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   self.conv_out(__a       )

						return sample






class 							__a							(  nn.Module						):





		'''simple docstring'''



		def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__=None ,    UpperCamelCase__="random" ,    UpperCamelCase__=False ,    UpperCamelCase__=True       ):
						super().__init__()
						SCREAMING_SNAKE_CASE_   :	str													=   n_e
						SCREAMING_SNAKE_CASE_   :	str													=   vq_embed_dim
						SCREAMING_SNAKE_CASE_   :	int													=   beta
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   legacy

						SCREAMING_SNAKE_CASE_   :	List[Any]													=   nn.Embedding(self.n_e ,    self.vq_embed_dim       )
						self.embedding.weight.data.uniform_(-1.0 / self.n_e ,    1.0 / self.n_e       )

						SCREAMING_SNAKE_CASE_   :	int													=   remap
						if self.remap is not None:
										self.register_buffer('used' ,    torch.tensor(np.load(self.remap       )       )       )
										SCREAMING_SNAKE_CASE_   :	str													=   self.used.shape[0]
										SCREAMING_SNAKE_CASE_   :	Dict													=   unknown_index  # "random" or "extra" or integer
										if self.unknown_index == "extra":
														SCREAMING_SNAKE_CASE_   :	Tuple													=   self.re_embed
														SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.re_embed + 1
										print(
										    F'''Remapping {self.n_e} indices to {self.re_embed} indices. '''
										    F'''Using {self.unknown_index} for unknown indices.'''       )
						else:
										SCREAMING_SNAKE_CASE_   :	Tuple													=   n_e

						SCREAMING_SNAKE_CASE_   :	List[Any]													=   sane_index_shape


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	str													=   inds.shape
						assert len(__a       ) > 1
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   inds.reshape(ishape[0] ,    -1       )
						SCREAMING_SNAKE_CASE_   :	str													=   self.used.to(__a       )
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   (inds[:, :, None] == used[None, None, ...]).long()
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   match.argmax(-1       )
						SCREAMING_SNAKE_CASE_   :	Tuple													=   match.sum(2       ) < 1
						if self.unknown_index == "random":
										SCREAMING_SNAKE_CASE_   :	Dict													=   torch.randint(0 ,    self.re_embed ,    size=new[unknown].shape       ).to(device=new.device       )
						else:
										SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.unknown_index
						return new.reshape(__a       )


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   inds.shape
						assert len(__a       ) > 1
						SCREAMING_SNAKE_CASE_   :	Any													=   inds.reshape(ishape[0] ,    -1       )
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   self.used.to(__a       )
						if self.re_embed > self.used.shape[0]:  # extra token
										SCREAMING_SNAKE_CASE_   :	int													=   0  # simply set to zero
						SCREAMING_SNAKE_CASE_   :	Dict													=   torch.gather(used[None, :][inds.shape[0] * [0], :] ,    1 ,    __a       )
						return back.reshape(__a       )


		def   __snake_case			(						self ,    UpperCamelCase__       ):
						# reshape z -> (batch, height, width, channel) and flatten
						SCREAMING_SNAKE_CASE_   :	Dict													=   z.permute(0 ,    2 ,    3 ,    1       ).contiguous()
						SCREAMING_SNAKE_CASE_   :	int													=   z.view(-1 ,    self.vq_embed_dim       )

						# distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   torch.argmin(torch.cdist(__a ,    self.embedding.weight       ) ,    dim=1       )

						SCREAMING_SNAKE_CASE_   :	Any													=   self.embedding(__a       ).view(z.shape       )
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   None
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   None

						# compute loss for embedding
						if not self.legacy:
										SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.beta * torch.mean((z_q.detach() - z) ** 2       ) + torch.mean((z_q - z.detach()) ** 2       )
						else:
										SCREAMING_SNAKE_CASE_   :	Tuple													=   torch.mean((z_q.detach() - z) ** 2       ) + self.beta * torch.mean((z_q - z.detach()) ** 2       )

						# preserve gradients
						SCREAMING_SNAKE_CASE_   :	List[str]													=   z + (z_q - z).detach()

						# reshape back to match original input shape
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   z_q.permute(0 ,    3 ,    1 ,    2       ).contiguous()

						if self.remap is not None:
										SCREAMING_SNAKE_CASE_   :	Dict													=   min_encoding_indices.reshape(z.shape[0] ,    -1       )  # add batch axis
										SCREAMING_SNAKE_CASE_   :	int													=   self.remap_to_used(__a       )
										SCREAMING_SNAKE_CASE_   :	Optional[int]													=   min_encoding_indices.reshape(-1 ,    1       )  # flatten

						if self.sane_index_shape:
										SCREAMING_SNAKE_CASE_   :	Tuple													=   min_encoding_indices.reshape(z_q.shape[0] ,    z_q.shape[2] ,    z_q.shape[3]       )

						return z_q, loss, (perplexity, min_encodings, min_encoding_indices)






		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__       ):
						# shape specifying (batch, height, width, channel)
						if self.remap is not None:
										SCREAMING_SNAKE_CASE_   :	List[Any]													=   indices.reshape(shape[0] ,    -1       )  # add batch axis
										SCREAMING_SNAKE_CASE_   :	str													=   self.unmap_to_all(__a       )
										SCREAMING_SNAKE_CASE_   :	List[str]													=   indices.reshape(-1       )  # flatten again

						# get quantized latent vectors
						SCREAMING_SNAKE_CASE_   :	Dict													=   self.embedding(__a       )

						if shape is not None:
										SCREAMING_SNAKE_CASE_   :	List[str]													=   z_q.view(__a       )
										# reshape back to match original input shape
										SCREAMING_SNAKE_CASE_   :	Optional[int]													=   z_q.permute(0 ,    3 ,    1 ,    2       ).contiguous()

						return z_q






class 							__a							(  __lowercase						):





		'''simple docstring'''



		def __init__(						self ,    UpperCamelCase__ ,    UpperCamelCase__=False       ):
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   parameters
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   torch.chunk(__a ,    2 ,    dim=1       )
						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   torch.clamp(self.logvar ,    -30.0 ,    20.0       )
						SCREAMING_SNAKE_CASE_   :	Dict													=   deterministic
						SCREAMING_SNAKE_CASE_   :	Tuple													=   torch.exp(0.5 * self.logvar       )
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   torch.exp(self.logvar       )
						if self.deterministic:
										SCREAMING_SNAKE_CASE_   :	Dict													=   torch.zeros_like(
										    self.mean ,    device=self.parameters.device ,    dtype=self.parameters.dtype       )


		def   __snake_case			(						self ,    UpperCamelCase__ = None       ):
						# make sure sample is on the same device as the parameters and has same dtype
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   randn_tensor(
						    self.mean.shape ,    generator=__a ,    device=self.parameters.device ,    dtype=self.parameters.dtype       )
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   self.mean + self.std * sample
						return x


		def   __snake_case			(						self ,    UpperCamelCase__=None       ):
						if self.deterministic:
										return torch.Tensor([0.0]       )
						else:
										if other is None:
														return 0.5 * torch.sum(torch.pow(self.mean ,    2       ) + self.var - 1.0 - self.logvar ,    dim=[1, 2, 3]       )
										else:
														return 0.5 * torch.sum(
														    torch.pow(self.mean - other.mean ,    2       ) / other.var
														    + self.var / other.var
														    - 1.0
														    - self.logvar
														    + other.logvar ,    dim=[1, 2, 3] ,    )


		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__=[1, 2, 3]       ):
						if self.deterministic:
										return torch.Tensor([0.0]       )
						SCREAMING_SNAKE_CASE_   :	List[str]													=   np.log(2.0 * np.pi       )
						return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean ,    2       ) / self.var ,    dim=__a       )






		def   __snake_case			(						self       ):
						return self.mean







import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
				import torch_xla.core.xla_model as xm
				import torch_xla.debug.metrics as met


class 							__a							(  __A						):





		'''simple docstring'''



		def __init__(						self ,    *UpperCamelCase__ ,    UpperCamelCase__=None ,    UpperCamelCase__=None ,    **UpperCamelCase__       ):
						super().__init__(*UpperCamelCase__ ,    **UpperCamelCase__       )
						SCREAMING_SNAKE_CASE_   :	Dict													=   eval_examples
						SCREAMING_SNAKE_CASE_   :	int													=   post_process_function


		def   __snake_case			(						self ,    UpperCamelCase__ = None ,    UpperCamelCase__=None ,    UpperCamelCase__ = None ,    UpperCamelCase__ = "eval" ,    **UpperCamelCase__ ,    ):
						SCREAMING_SNAKE_CASE_   :	int													=   gen_kwargs.copy()
						SCREAMING_SNAKE_CASE_   :	Tuple													=   (
						    gen_kwargs['max_length'] if gen_kwargs.get('max_length'       ) is not None else self.args.generation_max_length
						)
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   (
						    gen_kwargs['num_beams'] if gen_kwargs.get('num_beams'       ) is not None else self.args.generation_num_beams
						)
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   gen_kwargs

						SCREAMING_SNAKE_CASE_   :	Tuple													=   self.eval_dataset if eval_dataset is None else eval_dataset
						SCREAMING_SNAKE_CASE_   :	int													=   self.get_eval_dataloader(UpperCamelCase__       )
						SCREAMING_SNAKE_CASE_   :	Any													=   self.eval_examples if eval_examples is None else eval_examples

						# Temporarily disable metric computation, we will do it in the loop here.
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   self.compute_metrics
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   None
						SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   time.time()
						SCREAMING_SNAKE_CASE_   :	int													=   self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
						try:
										SCREAMING_SNAKE_CASE_   :	List[Any]													=   eval_loop(
										    UpperCamelCase__ ,    description='Evaluation' ,    prediction_loss_only=True if compute_metrics is None else None ,    ignore_keys=UpperCamelCase__ ,    metric_key_prefix=UpperCamelCase__ ,    )
						finally:
										SCREAMING_SNAKE_CASE_   :	Optional[int]													=   compute_metrics
						SCREAMING_SNAKE_CASE_   :	Dict													=   self.args.eval_batch_size * self.args.world_size
						if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
										start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
						output.metrics.update(
						    speed_metrics(
						        UpperCamelCase__ ,    UpperCamelCase__ ,    num_samples=output.num_samples ,    num_steps=math.ceil(output.num_samples / total_batch_size       ) ,    )       )

						if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
										# Only the main node write the results by default
										SCREAMING_SNAKE_CASE_   :	List[str]													=   self.post_process_function(UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__       )
										SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   self.compute_metrics(UpperCamelCase__       )

										# Prefix all keys with metric_key_prefix + '_'
										for key in list(metrics.keys()       ):
														if not key.startswith(F'''{metric_key_prefix}_'''       ):
																		SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   metrics.pop(UpperCamelCase__       )

										metrics.update(output.metrics       )
						else:
										SCREAMING_SNAKE_CASE_   :	Tuple													=   output.metrics

						if self.args.should_log:
										# Only the main node log the results by default
										self.log(UpperCamelCase__       )

						if self.args.tpu_metrics_debug or self.args.debug:
										# tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
										xm.master_print(met.metrics_report()       )

						SCREAMING_SNAKE_CASE_   :	Dict													=   self.callback_handler.on_evaluate(self.args ,    self.state ,    self.control ,    UpperCamelCase__       )
						return metrics






		def   __snake_case			(						self ,    UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__=None ,    UpperCamelCase__ = "test" ,    **UpperCamelCase__       ):
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   gen_kwargs.copy()

						SCREAMING_SNAKE_CASE_   :	Any													=   self.get_test_dataloader(UpperCamelCase__       )

						# Temporarily disable metric computation, we will do it in the loop here.
						SCREAMING_SNAKE_CASE_   :	Tuple													=   self.compute_metrics
						SCREAMING_SNAKE_CASE_   :	str													=   None
						SCREAMING_SNAKE_CASE_   :	Optional[Any]													=   time.time()
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
						try:
										SCREAMING_SNAKE_CASE_   :	List[str]													=   eval_loop(
										    UpperCamelCase__ ,    description='Prediction' ,    prediction_loss_only=True if compute_metrics is None else None ,    ignore_keys=UpperCamelCase__ ,    metric_key_prefix=UpperCamelCase__ ,    )
						finally:
										SCREAMING_SNAKE_CASE_   :	Union[str, Any]													=   compute_metrics

						SCREAMING_SNAKE_CASE_   :	List[str]													=   self.args.eval_batch_size * self.args.world_size
						if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
										start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
						output.metrics.update(
						    speed_metrics(
						        UpperCamelCase__ ,    UpperCamelCase__ ,    num_samples=output.num_samples ,    num_steps=math.ceil(output.num_samples / total_batch_size       ) ,    )       )
						if self.post_process_function is None or self.compute_metrics is None:
										return output

						SCREAMING_SNAKE_CASE_   :	Optional[int]													=   self.post_process_function(UpperCamelCase__ ,    UpperCamelCase__ ,    UpperCamelCase__ ,    'predict'       )
						SCREAMING_SNAKE_CASE_   :	List[Any]													=   self.compute_metrics(UpperCamelCase__       )

						# Prefix all keys with metric_key_prefix + '_'
						for key in list(metrics.keys()       ):
										if not key.startswith(F'''{metric_key_prefix}_'''       ):
														SCREAMING_SNAKE_CASE_   :	Optional[int]													=   metrics.pop(UpperCamelCase__       )
						metrics.update(output.metrics       )
						return PredictionOutput(predictions=predictions.predictions ,    label_ids=predictions.label_ids ,    metrics=UpperCamelCase__       )