
'''simple docstring'''







import argparse
import json

import torch

from diffusers import DDPMScheduler, LDMPipeline, UNetaDModel, VQModel
def 					snake_case_		(				__SCREAMING_SNAKE_CASE					: Optional[int]      ,      __SCREAMING_SNAKE_CASE					: int=1							):



						"""simple docstring"""





						if n_shave_prefix_segments >= 0:
												return ".".join(path.split('''.'''							)[n_shave_prefix_segments:]							)
						else:
												return ".".join(path.split('''.'''							)[:n_shave_prefix_segments]							)
def 					snake_case_		(				__SCREAMING_SNAKE_CASE					: List[Any]      ,      __SCREAMING_SNAKE_CASE					: int=0							):



						"""simple docstring"""





						lowercase_	:     Union[str, Any]													=       []
						for old_item in old_list:
												lowercase_	:     Optional[Any]													=       old_item.replace('''in_layers.0'''      ,      '''norm1'''							)
												lowercase_	:     Dict													=       new_item.replace('''in_layers.2'''      ,      '''conv1'''							)

												lowercase_	:     int													=       new_item.replace('''out_layers.0'''      ,      '''norm2'''							)
												lowercase_	:     Union[str, Any]													=       new_item.replace('''out_layers.3'''      ,      '''conv2'''							)

												lowercase_	:     List[Any]													=       new_item.replace('''emb_layers.1'''      ,      '''time_emb_proj'''							)
												lowercase_	:     int													=       new_item.replace('''skip_connection'''      ,      '''conv_shortcut'''							)

												lowercase_	:     Union[str, Any]													=       shave_segments(__SCREAMING_SNAKE_CASE      ,      n_shave_prefix_segments=__SCREAMING_SNAKE_CASE							)

												mapping.append({'''old''': old_item, '''new''': new_item}							)

						return mapping
def 					snake_case_		(				__SCREAMING_SNAKE_CASE					: int      ,      __SCREAMING_SNAKE_CASE					: Optional[Any]=0							):



						"""simple docstring"""





						lowercase_	:     List[Any]													=       []
						for old_item in old_list:
												lowercase_	:     Union[str, Any]													=       old_item

												lowercase_	:     Optional[Any]													=       new_item.replace('''norm.weight'''      ,      '''group_norm.weight'''							)
												lowercase_	:     List[str]													=       new_item.replace('''norm.bias'''      ,      '''group_norm.bias'''							)

												lowercase_	:     Tuple													=       new_item.replace('''proj_out.weight'''      ,      '''proj_attn.weight'''							)
												lowercase_	:     List[Any]													=       new_item.replace('''proj_out.bias'''      ,      '''proj_attn.bias'''							)

												lowercase_	:     Tuple													=       shave_segments(__SCREAMING_SNAKE_CASE      ,      n_shave_prefix_segments=__SCREAMING_SNAKE_CASE							)

												mapping.append({'''old''': old_item, '''new''': new_item}							)

						return mapping
def 					snake_case_		(				__SCREAMING_SNAKE_CASE					: Union[str, Any]      ,      __SCREAMING_SNAKE_CASE					: Optional[Any]      ,      __SCREAMING_SNAKE_CASE					: str      ,      __SCREAMING_SNAKE_CASE					: str=None      ,      __SCREAMING_SNAKE_CASE					: Dict=None      ,      __SCREAMING_SNAKE_CASE					: Dict=None							):



						"""simple docstring"""





						assert isinstance(__SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE							), "Paths should be a list of dicts containing 'old' and 'new' keys."

						# Splits the attention layers into three variables.
						if attention_paths_to_split is not None:
												for path, path_map in attention_paths_to_split.items():
																		lowercase_	:     Tuple													=       old_checkpoint[path]
																		lowercase_	:     str													=       old_tensor.shape[0] // 3

																		lowercase_	:     List[str]													=       (-1, channels) if len(old_tensor.shape							) == 3 else (-1)

																		lowercase_	:     List[Any]													=       old_tensor.shape[0] // config['''num_head_channels'''] // 3

																		lowercase_	:     int													=       old_tensor.reshape((num_heads, 3 * channels // num_heads) + old_tensor.shape[1:]							)
																		lowercase_						, lowercase_						, lowercase_	:     Optional[int]													=       old_tensor.split(channels // num_heads      ,      dim=1							)

																		lowercase_	:     Tuple													=       query.reshape(__SCREAMING_SNAKE_CASE							)
																		lowercase_	:     Any													=       key.reshape(__SCREAMING_SNAKE_CASE							)
																		lowercase_	:     int													=       value.reshape(__SCREAMING_SNAKE_CASE							)

						for path in paths:
												lowercase_	:     List[str]													=       path['''new''']

												# These have already been assigned
												if attention_paths_to_split is not None and new_path in attention_paths_to_split:
																		continue

												# Global renaming happens here
												lowercase_	:     str													=       new_path.replace('''middle_block.0'''      ,      '''mid_block.resnets.0'''							)
												lowercase_	:     List[Any]													=       new_path.replace('''middle_block.1'''      ,      '''mid_block.attentions.0'''							)
												lowercase_	:     Dict													=       new_path.replace('''middle_block.2'''      ,      '''mid_block.resnets.1'''							)

												if additional_replacements is not None:
																		for replacement in additional_replacements:
																								lowercase_	:     Dict													=       new_path.replace(replacement['''old''']      ,      replacement['''new''']							)

        # proj_attn.weight has to be converted from conv 1D to linear
												if "proj_attn.weight" in new_path:
																		lowercase_	:     Optional[Any]													=       old_checkpoint[path['''old''']][:, :, 0]
												else:
																		lowercase_	:     Optional[Any]													=       old_checkpoint[path['''old''']]
def 					snake_case_		(				__SCREAMING_SNAKE_CASE					: str      ,      __SCREAMING_SNAKE_CASE					: List[Any]							):



						"""simple docstring"""





						lowercase_	:     List[Any]													=       {}

						lowercase_	:     Optional[int]													=       checkpoint['''time_embed.0.weight''']
						lowercase_	:     Optional[Any]													=       checkpoint['''time_embed.0.bias''']
						lowercase_	:     Optional[Any]													=       checkpoint['''time_embed.2.weight''']
						lowercase_	:     int													=       checkpoint['''time_embed.2.bias''']

						lowercase_	:     Optional[Any]													=       checkpoint['''input_blocks.0.0.weight''']
						lowercase_	:     List[str]													=       checkpoint['''input_blocks.0.0.bias''']

						lowercase_	:     int													=       checkpoint['''out.0.weight''']
						lowercase_	:     Tuple													=       checkpoint['''out.0.bias''']
						lowercase_	:     Dict													=       checkpoint['''out.2.weight''']
						lowercase_	:     Any													=       checkpoint['''out.2.bias''']

						# Retrieves the keys for the input blocks only
						lowercase_	:     int													=       len({'''.'''.join(layer.split('''.'''							)[:2]							) for layer in checkpoint if '''input_blocks''' in layer}							)
						lowercase_	:     Union[str, Any]													=       {
						    layer_id: [key for key in checkpoint if F'''input_blocks.{layer_id}''' in key]
						    for layer_id in range(__SCREAMING_SNAKE_CASE							)
						}

						# Retrieves the keys for the middle blocks only
						lowercase_	:     int													=       len({'''.'''.join(layer.split('''.'''							)[:2]							) for layer in checkpoint if '''middle_block''' in layer}							)
						lowercase_	:     str													=       {
						    layer_id: [key for key in checkpoint if F'''middle_block.{layer_id}''' in key]
						    for layer_id in range(__SCREAMING_SNAKE_CASE							)
						}

						# Retrieves the keys for the output blocks only
						lowercase_	:     List[Any]													=       len({'''.'''.join(layer.split('''.'''							)[:2]							) for layer in checkpoint if '''output_blocks''' in layer}							)
						lowercase_	:     str													=       {
						    layer_id: [key for key in checkpoint if F'''output_blocks.{layer_id}''' in key]
						    for layer_id in range(__SCREAMING_SNAKE_CASE							)
						}

						for i in range(1      ,      __SCREAMING_SNAKE_CASE							):
												lowercase_	:     Any													=       (i - 1) // (config['''num_res_blocks'''] + 1)
												lowercase_	:     Any													=       (i - 1) % (config['''num_res_blocks'''] + 1)

												lowercase_	:     List[str]													=       [key for key in input_blocks[i] if F'''input_blocks.{i}.0''' in key]
												lowercase_	:     List[Any]													=       [key for key in input_blocks[i] if F'''input_blocks.{i}.1''' in key]

												if F'''input_blocks.{i}.0.op.weight''' in checkpoint:
																		lowercase_	:     List[Any]													=       checkpoint[
																		    F'''input_blocks.{i}.0.op.weight'''
																		]
																		lowercase_	:     List[str]													=       checkpoint[
																		    F'''input_blocks.{i}.0.op.bias'''
																		]
																		continue

												lowercase_	:     Union[str, Any]													=       renew_resnet_paths(__SCREAMING_SNAKE_CASE							)
												lowercase_	:     Optional[int]													=       {'''old''': F'''input_blocks.{i}.0''', '''new''': F'''down_blocks.{block_id}.resnets.{layer_in_block_id}'''}
												lowercase_	:     List[Any]													=       {'''old''': '''resnets.2.op''', '''new''': '''downsamplers.0.op'''}
												assign_to_checkpoint(
												    __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      additional_replacements=[meta_path, resnet_op]      ,      config=__SCREAMING_SNAKE_CASE							)

												if len(__SCREAMING_SNAKE_CASE							):
																		lowercase_	:     Optional[Any]													=       renew_attention_paths(__SCREAMING_SNAKE_CASE							)
																		lowercase_	:     List[str]													=       {
																		    '''old''': F'''input_blocks.{i}.1''',
																		    '''new''': F'''down_blocks.{block_id}.attentions.{layer_in_block_id}''',
																		}
																		lowercase_	:     Dict													=       {
																		    F'''input_blocks.{i}.1.qkv.bias''': {
																		        '''key''': F'''down_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias''',
																		        '''query''': F'''down_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias''',
																		        '''value''': F'''down_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias''',
																		    },
																		    F'''input_blocks.{i}.1.qkv.weight''': {
																		        '''key''': F'''down_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight''',
																		        '''query''': F'''down_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight''',
																		        '''value''': F'''down_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight''',
																		    },
																		}
																		assign_to_checkpoint(
																		    __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      additional_replacements=[meta_path]      ,      attention_paths_to_split=__SCREAMING_SNAKE_CASE      ,      config=__SCREAMING_SNAKE_CASE      ,      )

						lowercase_	:     int													=       middle_blocks[0]
						lowercase_	:     Union[str, Any]													=       middle_blocks[1]
						lowercase_	:     Optional[Any]													=       middle_blocks[2]

						lowercase_	:     Dict													=       renew_resnet_paths(__SCREAMING_SNAKE_CASE							)
						assign_to_checkpoint(__SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      config=__SCREAMING_SNAKE_CASE							)

						lowercase_	:     List[str]													=       renew_resnet_paths(__SCREAMING_SNAKE_CASE							)
						assign_to_checkpoint(__SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      config=__SCREAMING_SNAKE_CASE							)

						lowercase_	:     Dict													=       renew_attention_paths(__SCREAMING_SNAKE_CASE							)
						lowercase_	:     Optional[Any]													=       {
						    '''middle_block.1.qkv.bias''': {
						        '''key''': '''mid_block.attentions.0.key.bias''',
						        '''query''': '''mid_block.attentions.0.query.bias''',
						        '''value''': '''mid_block.attentions.0.value.bias''',
						    },
						    '''middle_block.1.qkv.weight''': {
						        '''key''': '''mid_block.attentions.0.key.weight''',
						        '''query''': '''mid_block.attentions.0.query.weight''',
						        '''value''': '''mid_block.attentions.0.value.weight''',
						    },
						}
						assign_to_checkpoint(
						    __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      attention_paths_to_split=__SCREAMING_SNAKE_CASE      ,      config=__SCREAMING_SNAKE_CASE							)

						for i in range(__SCREAMING_SNAKE_CASE							):
												lowercase_	:     List[Any]													=       i // (config['''num_res_blocks'''] + 1)
												lowercase_	:     int													=       i % (config['''num_res_blocks'''] + 1)
												lowercase_	:     Optional[Any]													=       [shave_segments(__SCREAMING_SNAKE_CASE      ,      2							) for name in output_blocks[i]]
												lowercase_	:     Any													=       {}

												for layer in output_block_layers:
																		lowercase_						, lowercase_	:     Any													=       layer.split('''.'''							)[0], shave_segments(__SCREAMING_SNAKE_CASE      ,      1							)
																		if layer_id in output_block_list:
																								output_block_list[layer_id].append(__SCREAMING_SNAKE_CASE							)
																		else:
																								lowercase_	:     Optional[Any]													=       [layer_name]

												if len(__SCREAMING_SNAKE_CASE							) > 1:
																		lowercase_	:     List[Any]													=       [key for key in output_blocks[i] if F'''output_blocks.{i}.0''' in key]
																		lowercase_	:     Union[str, Any]													=       [key for key in output_blocks[i] if F'''output_blocks.{i}.1''' in key]

																		lowercase_	:     Optional[Any]													=       renew_resnet_paths(__SCREAMING_SNAKE_CASE							)
																		lowercase_	:     List[Any]													=       renew_resnet_paths(__SCREAMING_SNAKE_CASE							)

																		lowercase_	:     List[Any]													=       {'''old''': F'''output_blocks.{i}.0''', '''new''': F'''up_blocks.{block_id}.resnets.{layer_in_block_id}'''}
																		assign_to_checkpoint(__SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      additional_replacements=[meta_path]      ,      config=__SCREAMING_SNAKE_CASE							)

																		if ["conv.weight", "conv.bias"] in output_block_list.values():
																								lowercase_	:     Any													=       list(output_block_list.values()							).index(['''conv.weight''', '''conv.bias''']							)
																								lowercase_	:     str													=       checkpoint[
																								    F'''output_blocks.{i}.{index}.conv.weight'''
																								]
																								lowercase_	:     Union[str, Any]													=       checkpoint[
																								    F'''output_blocks.{i}.{index}.conv.bias'''
																								]

																								# Clear attentions as they have been attributed above.
																								if len(__SCREAMING_SNAKE_CASE							) == 2:
																														lowercase_	:     Tuple													=       []

																		if len(__SCREAMING_SNAKE_CASE							):
																								lowercase_	:     Union[str, Any]													=       renew_attention_paths(__SCREAMING_SNAKE_CASE							)
																								lowercase_	:     Any													=       {
																								    '''old''': F'''output_blocks.{i}.1''',
																								    '''new''': F'''up_blocks.{block_id}.attentions.{layer_in_block_id}''',
																								}
																								lowercase_	:     List[str]													=       {
																								    F'''output_blocks.{i}.1.qkv.bias''': {
																								        '''key''': F'''up_blocks.{block_id}.attentions.{layer_in_block_id}.key.bias''',
																								        '''query''': F'''up_blocks.{block_id}.attentions.{layer_in_block_id}.query.bias''',
																								        '''value''': F'''up_blocks.{block_id}.attentions.{layer_in_block_id}.value.bias''',
																								    },
																								    F'''output_blocks.{i}.1.qkv.weight''': {
																								        '''key''': F'''up_blocks.{block_id}.attentions.{layer_in_block_id}.key.weight''',
																								        '''query''': F'''up_blocks.{block_id}.attentions.{layer_in_block_id}.query.weight''',
																								        '''value''': F'''up_blocks.{block_id}.attentions.{layer_in_block_id}.value.weight''',
																								    },
																								}
																								assign_to_checkpoint(
																								    __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE      ,      additional_replacements=[meta_path]      ,      attention_paths_to_split=to_split if any('''qkv''' in key for key in attentions							) else None      ,      config=__SCREAMING_SNAKE_CASE      ,      )
												else:
																		lowercase_	:     int													=       renew_resnet_paths(__SCREAMING_SNAKE_CASE      ,      n_shave_prefix_segments=1							)
																		for path in resnet_0_paths:
																								lowercase_	:     Optional[int]													=       '''.'''.join(['''output_blocks''', str(__SCREAMING_SNAKE_CASE							), path['''old''']]							)
																								lowercase_	:     Union[str, Any]													=       '''.'''.join(['''up_blocks''', str(__SCREAMING_SNAKE_CASE							), '''resnets''', str(__SCREAMING_SNAKE_CASE							), path['''new''']]							)

																								lowercase_	:     Optional[Any]													=       checkpoint[old_path]

						return new_checkpoint


if __name__ == "__main__":
							_lowercase :  Union[str, Any]    							=       argparse.ArgumentParser()

							parser.add_argument(
							    "--checkpoint_path", default=None, type=str, required=True, help="Path to the checkpoint to convert."
							)

							parser.add_argument(
							    "--config_file",
							    default=None,
							    type=str,
							    required=True,
							    help="The config json file corresponding to the architecture.",
							)

							parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.")

							_lowercase :  int    							=       parser.parse_args()

							_lowercase :  Dict    							=       torch.load(args.checkpoint_path)

							with open(args.config_file) as f:
														_lowercase :  str    							=       json.loads(f.read())

							_lowercase :  Union[str, Any]    							=       convert_ldm_checkpoint(checkpoint, config)

							if "ldm" in config:
														del config["ldm"]

							_lowercase :  Any    							=       UNetaDModel(**config)
							model.load_state_dict(converted_checkpoint)

							try:
														_lowercase :  str    							=       DDPMScheduler.from_config("/".join(args.checkpoint_path.split("/")[:-1]))
														_lowercase :  List[str]    							=       VQModel.from_pretrained("/".join(args.checkpoint_path.split("/")[:-1]))

														_lowercase :  Optional[Any]    							=       LDMPipeline(unet=model, scheduler=scheduler, vae=vqvae)
														pipe.save_pretrained(args.dump_path)
							except:  # noqa: E722
														model.save_pretrained(args.dump_path)





'''simple docstring'''







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  lowerCAmelCase__   (  lowerCamelCase_     ):






					def __init__(   self				,							*__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=None				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											super().__init__(*__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Tuple													=       eval_examples
											lowercase_	:     Tuple													=       post_process_function






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE = None				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE = None				,							__SCREAMING_SNAKE_CASE = "eval"				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											lowercase_	:     Optional[int]													=       gen_kwargs.copy()
											lowercase_	:     List[str]													=       (
											    gen_kwargs['''max_length'''] if gen_kwargs.get('''max_length'''		) is not None else self.args.generation_max_length
											)
											lowercase_	:     str													=       (
											    gen_kwargs['''num_beams'''] if gen_kwargs.get('''num_beams'''		) is not None else self.args.generation_num_beams
											)
											lowercase_	:     Dict													=       gen_kwargs

											lowercase_	:     List[Any]													=       self.eval_dataset if eval_dataset is None else eval_dataset
											lowercase_	:     List[str]													=       self.get_eval_dataloader(__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Optional[Any]													=       self.eval_examples if eval_examples is None else eval_examples

											# Temporarily disable metric computation, we will do it in the loop here.
											lowercase_	:     Union[str, Any]													=       self.compute_metrics
											lowercase_	:     Optional[int]													=       None
											lowercase_	:     Tuple													=       time.time()
											lowercase_	:     Tuple													=       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
											try:
																	lowercase_	:     str													=       eval_loop(
																	    __SCREAMING_SNAKE_CASE				,							description='''Evaluation'''				,							prediction_loss_only=True if compute_metrics is None else None				,							ignore_keys=__SCREAMING_SNAKE_CASE				,							metric_key_prefix=__SCREAMING_SNAKE_CASE				,							)
											finally:
																	lowercase_	:     Any													=       compute_metrics
											lowercase_	:     Any													=       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(
											        __SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							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
																	lowercase_	:     Optional[Any]													=       self.post_process_function(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		)
																	lowercase_	:     Optional[int]													=       self.compute_metrics(__SCREAMING_SNAKE_CASE		)

																	# Prefix all keys with metric_key_prefix + '_'
																	for key in list(metrics.keys()		):
																							if not key.startswith(F'''{metric_key_prefix}_'''		):
																													lowercase_	:     List[Any]													=       metrics.pop(__SCREAMING_SNAKE_CASE		)

																	metrics.update(output.metrics		)
											else:
																	lowercase_	:     List[Any]													=       output.metrics

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

											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()		)

											lowercase_	:     List[Any]													=       self.callback_handler.on_evaluate(self.args				,							self.state				,							self.control				,							__SCREAMING_SNAKE_CASE		)
											return metrics






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE = "test"				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_	:     Union[str, Any]													=       gen_kwargs.copy()

											lowercase_	:     Tuple													=       self.get_test_dataloader(__SCREAMING_SNAKE_CASE		)

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

											lowercase_	:     Tuple													=       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(
											        __SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							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

											lowercase_	:     Any													=       self.post_process_function(__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							'''predict'''		)
											lowercase_	:     str													=       self.compute_metrics(__SCREAMING_SNAKE_CASE		)

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



