


"""simple docstring"""
import re
import time
from typing import Optional

import IPython.display as disp

from ..trainer_callback import TrainerCallback
from ..trainer_utils import IntervalStrategy, has_length







def 						_a				(      _SCREAMING_SNAKE_CASE				)    ->				Any:
							snake_case_           =		int(_SCREAMING_SNAKE_CASE				)
							snake_case_       ,						snake_case_       ,						snake_case_           =		t // 3_600, (t // 60) % 60, t % 60
							return f"""{h}:{m:02d}:{s:02d}""" if h != 0 else f"""{m:02d}:{s:02d}"""







def 						_a				(      _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE=300				)    ->				Any:
							# docstyle-ignore
							return f"""
    <div>
      {prefix}
      <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress>
      {label}
    </div>
    """







def 						_a				(      _SCREAMING_SNAKE_CASE				)    ->				Tuple:
							snake_case_           =		"""<table border=\"1\" class=\"dataframe\">\n"""
							html_code += """  <thead>\n <tr style="text-align: left;">\n"""
							for i in items[0]:
														html_code += f"""      <th>{i}</th>\n"""
							html_code += "    </tr>\n  </thead>\n  <tbody>\n"
							for line in items[1:]:
														html_code += "    <tr>\n"
														for elt in line:
																					snake_case_           =		f"""{elt:.6f}""" if isinstance(_SCREAMING_SNAKE_CASE				, _SCREAMING_SNAKE_CASE				) else str(_SCREAMING_SNAKE_CASE				)
																					html_code += f"""      <td>{elt}</td>\n"""
														html_code += "    </tr>\n"
							html_code += "  </tbody>\n</table><p>"
							return html_code





class      __A     :





							'''simple docstring'''

							__lowercase:		List[str]							      =    5
							__lowercase:		Union[str, Any]							      =    0.2



							def __init__(				self							:      List[Any]		,       UpperCAmelCase_							:      int		,       UpperCAmelCase_							:      Optional[str] = None		,       UpperCAmelCase_							:      bool = True		,       UpperCAmelCase_							:      Optional["NotebookTrainingTracker"] = None		,       UpperCAmelCase_							:      int = 300		,       )					->List[str]:



														"""simple docstring"""



														snake_case_           =		total
														snake_case_           =		"""""" if prefix is None else prefix
														snake_case_           =		leave
														snake_case_           =		parent
														snake_case_           =		width
														snake_case_           =		None
														snake_case_           =		None
														snake_case_           =		None



							def 		lowerCAmelCase       (				self							:      Optional[Any]		,       UpperCAmelCase_							:      int		,       UpperCAmelCase_							:      bool = False		,       UpperCAmelCase_							:      str = None       )					->Dict:



														"""simple docstring"""



														snake_case_           =		value
														if comment is not None:
																					snake_case_           =		comment
														if self.last_value is None:
																					snake_case_           =		snake_case_           =		time.time()
																					snake_case_           =		snake_case_           =		value
																					snake_case_           =		snake_case_           =		None
																					snake_case_           =		self.warmup
																					snake_case_           =		1
																					self.update_bar(UpperCAmelCase_       )
														elif value <= self.last_value and not force_update:
																					return
														elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for		,       self.total       ):
																					if self.first_calls > 0:
																												self.first_calls -= 1
																					snake_case_           =		time.time()
																					snake_case_           =		current_time - self.start_time
																					# We could have value = self.start_value if the update is called twixe with the same start value.
																					if value > self.start_value:
																												snake_case_           =		self.elapsed_time / (value - self.start_value)
																					else:
																												snake_case_           =		None
																					if value >= self.total:
																												snake_case_           =		self.total
																												snake_case_           =		None
																												if not self.leave:
																																			self.close()
																					elif self.average_time_per_item is not None:
																												snake_case_           =		self.average_time_per_item * (self.total - value)
																					self.update_bar(UpperCAmelCase_       )
																					snake_case_           =		value
																					snake_case_           =		current_time
																					if self.average_time_per_item is None:
																												snake_case_           =		1
																					else:
																												snake_case_           =		max(int(self.update_every / self.average_time_per_item       )		,       1       )



							def 		lowerCAmelCase       (				self							:      List[str]		,       UpperCAmelCase_							:      List[Any]		,       UpperCAmelCase_							:      Union[str, Any]=None       )					->int:



														"""simple docstring"""



														snake_case_           =		""" """ * (len(str(self.total       )       ) - len(str(UpperCAmelCase_       )       )) + str(UpperCAmelCase_       )
														if self.elapsed_time is None:
																					snake_case_           =		F"""[{spaced_value}/{self.total} : < :"""
														elif self.predicted_remaining is None:
																					snake_case_           =		F"""[{spaced_value}/{self.total} {format_time(self.elapsed_time       )}"""
														else:
																					snake_case_           =		(
																					    F"""[{spaced_value}/{self.total} {format_time(self.elapsed_time       )} <"""
																					    F""" {format_time(self.predicted_remaining       )}"""
																					)
																					self.label += F""", {1/self.average_time_per_item:.2f} it/s"""
														self.label += "]" if self.comment is None or len(self.comment       ) == 0 else F""", {self.comment}]"""
														self.display()



							def 		lowerCAmelCase       (				self							:      Any       )					->Optional[int]:



														"""simple docstring"""



														snake_case_           =		html_progress_bar(self.value		,       self.total		,       self.prefix		,       self.label		,       self.width       )
														if self.parent is not None:
																					# If this is a child bar, the parent will take care of the display.
																					self.parent.display()
																					return
														if self.output is None:
																					snake_case_           =		disp.display(disp.HTML(self.html_code       )		,       display_id=UpperCAmelCase_       )
														else:
																					self.output.update(disp.HTML(self.html_code       )       )



							def 		lowerCAmelCase       (				self							:      Optional[int]       )					->Optional[Any]:



														"""simple docstring"""



														if self.parent is None and self.output is not None:
																					self.output.update(disp.HTML(""""""       )       )





class      __A     (snake_case__):





							'''simple docstring'''

							def __init__(				self							:      Tuple		,       UpperCAmelCase_							:      Any		,       UpperCAmelCase_							:      Any=None       )					->Tuple:



														"""simple docstring"""



														super().__init__(UpperCAmelCase_       )
														snake_case_           =		None if column_names is None else [column_names]
														snake_case_           =		None



							def 		lowerCAmelCase       (				self							:      List[str]       )					->Optional[int]:



														"""simple docstring"""



														snake_case_           =		html_progress_bar(self.value		,       self.total		,       self.prefix		,       self.label		,       self.width       )
														if self.inner_table is not None:
																					self.html_code += text_to_html_table(self.inner_table       )
														if self.child_bar is not None:
																					self.html_code += self.child_bar.html_code
														if self.output is None:
																					snake_case_           =		disp.display(disp.HTML(self.html_code       )		,       display_id=UpperCAmelCase_       )
														else:
																					self.output.update(disp.HTML(self.html_code       )       )



							def 		lowerCAmelCase       (				self							:      Optional[int]		,       UpperCAmelCase_							:      Any       )					->int:



														"""simple docstring"""



														if self.inner_table is None:
																					snake_case_           =		[list(values.keys()       ), list(values.values()       )]
														else:
																					snake_case_           =		self.inner_table[0]
																					if len(self.inner_table       ) == 1:
																												# We give a chance to update the column names at the first iteration
																												for key in values.keys():
																																			if key not in columns:
																																										columns.append(UpperCAmelCase_       )
																												snake_case_           =		columns
																					self.inner_table.append([values[c] for c in columns]       )



							def 		lowerCAmelCase       (				self							:      Optional[Any]		,       UpperCAmelCase_							:      Dict		,       UpperCAmelCase_							:      Any=None		,       UpperCAmelCase_							:      List[Any]=300       )					->Tuple:



														"""simple docstring"""



														snake_case_           =		NotebookProgressBar(UpperCAmelCase_		,       prefix=UpperCAmelCase_		,       parent=self		,       width=UpperCAmelCase_       )
														return self.child_bar



							def 		lowerCAmelCase       (				self							:      int       )					->List[str]:



														"""simple docstring"""



														snake_case_           =		None
														self.display()





class      __A     (snake_case__):





							'''simple docstring'''

							def __init__(				self							:      Optional[Any]       )					->Dict:



														"""simple docstring"""



														snake_case_           =		None
														snake_case_           =		None
														snake_case_           =		False



							def 		lowerCAmelCase       (				self							:      List[str]		,       UpperCAmelCase_							:      str		,       UpperCAmelCase_							:      Any		,       UpperCAmelCase_							:      List[Any]		,       **UpperCAmelCase_							:      List[str]       )					->List[Any]:



														"""simple docstring"""



														snake_case_           =		"""Epoch""" if args.evaluation_strategy == IntervalStrategy.EPOCH else """Step"""
														snake_case_           =		0
														snake_case_           =		0
														snake_case_           =		[self.first_column] + ["""Training Loss"""]
														if args.evaluation_strategy != IntervalStrategy.NO:
																					column_names.append("""Validation Loss"""       )
														snake_case_           =		NotebookTrainingTracker(state.max_steps		,       UpperCAmelCase_       )



							def 		lowerCAmelCase       (				self							:      Optional[int]		,       UpperCAmelCase_							:      List[str]		,       UpperCAmelCase_							:      List[str]		,       UpperCAmelCase_							:      str		,       **UpperCAmelCase_							:      Optional[Any]       )					->Dict:



														"""simple docstring"""



														snake_case_           =		int(state.epoch       ) if int(state.epoch       ) == state.epoch else F"""{state.epoch:.2f}"""
														self.training_tracker.update(
														    state.global_step + 1		,       comment=F"""Epoch {epoch}/{state.num_train_epochs}"""		,       force_update=self._force_next_update		,       )
														snake_case_           =		False



							def 		lowerCAmelCase       (				self							:      Union[str, Any]		,       UpperCAmelCase_							:      Dict		,       UpperCAmelCase_							:      Optional[int]		,       UpperCAmelCase_							:      List[Any]		,       UpperCAmelCase_							:      List[Any]=None		,       **UpperCAmelCase_							:      Dict       )					->Optional[int]:



														"""simple docstring"""



														if not has_length(UpperCAmelCase_       ):
																					return
														if self.prediction_bar is None:
																					if self.training_tracker is not None:
																												snake_case_           =		self.training_tracker.add_child(len(UpperCAmelCase_       )       )
																					else:
																												snake_case_           =		NotebookProgressBar(len(UpperCAmelCase_       )       )
																					self.prediction_bar.update(1       )
														else:
																					self.prediction_bar.update(self.prediction_bar.value + 1       )



							def 		lowerCAmelCase       (				self							:      Union[str, Any]		,       UpperCAmelCase_							:      Dict		,       UpperCAmelCase_							:      int		,       UpperCAmelCase_							:      List[str]		,       **UpperCAmelCase_							:      Union[str, Any]       )					->Tuple:



														"""simple docstring"""



														if self.prediction_bar is not None:
																					self.prediction_bar.close()
														snake_case_           =		None



							def 		lowerCAmelCase       (				self							:      List[str]		,       UpperCAmelCase_							:      str		,       UpperCAmelCase_							:      List[Any]		,       UpperCAmelCase_							:      str		,       UpperCAmelCase_							:      List[Any]=None		,       **UpperCAmelCase_							:      List[Any]       )					->Union[str, Any]:



														"""simple docstring"""



														if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
																					snake_case_           =		{"""Training Loss""": logs["""loss"""]}
																					# First column is necessarily Step sine we're not in epoch eval strategy
																					snake_case_           =		state.global_step
																					self.training_tracker.write_line(UpperCAmelCase_       )



							def 		lowerCAmelCase       (				self							:      List[str]		,       UpperCAmelCase_							:      str		,       UpperCAmelCase_							:      List[str]		,       UpperCAmelCase_							:      Any		,       UpperCAmelCase_							:      List[Any]=None		,       **UpperCAmelCase_							:      Optional[Any]       )					->int:



														"""simple docstring"""



														if self.training_tracker is not None:
																					snake_case_           =		{"""Training Loss""": """No log""", """Validation Loss""": """No log"""}
																					for log in reversed(state.log_history       ):
																												if "loss" in log:
																																			snake_case_           =		log["""loss"""]
																																			break

																					if self.first_column == "Epoch":
																												snake_case_           =		int(state.epoch       )
																					else:
																												snake_case_           =		state.global_step
																					snake_case_           =		"""eval"""
																					for k in metrics:
																												if k.endswith("""_loss"""       ):
																																			snake_case_           =		re.sub(R"""\_loss$"""		,       """"""		,       UpperCAmelCase_       )
																					snake_case_           =		metrics.pop("""total_flos"""		,       UpperCAmelCase_       )
																					snake_case_           =		metrics.pop("""epoch"""		,       UpperCAmelCase_       )
																					snake_case_           =		metrics.pop(F"""{metric_key_prefix}_runtime"""		,       UpperCAmelCase_       )
																					snake_case_           =		metrics.pop(F"""{metric_key_prefix}_samples_per_second"""		,       UpperCAmelCase_       )
																					snake_case_           =		metrics.pop(F"""{metric_key_prefix}_steps_per_second"""		,       UpperCAmelCase_       )
																					snake_case_           =		metrics.pop(F"""{metric_key_prefix}_jit_compilation_time"""		,       UpperCAmelCase_       )
																					for k, v in metrics.items():
																												if k == F"""{metric_key_prefix}_loss""":
																																			snake_case_           =		v
																												else:
																																			snake_case_           =		k.split("""_"""       )
																																			snake_case_           =		""" """.join([part.capitalize() for part in splits[1:]]       )
																																			snake_case_           =		v
																					self.training_tracker.write_line(UpperCAmelCase_       )
																					self.training_tracker.remove_child()
																					snake_case_           =		None
																					# Evaluation takes a long time so we should force the next update.
																					snake_case_           =		True



							def 		lowerCAmelCase       (				self							:      int		,       UpperCAmelCase_							:      Union[str, Any]		,       UpperCAmelCase_							:      Any		,       UpperCAmelCase_							:      int		,       **UpperCAmelCase_							:      Optional[Any]       )					->Tuple:



														"""simple docstring"""



														self.training_tracker.update(
														    state.global_step		,       comment=F"""Epoch {int(state.epoch       )}/{state.num_train_epochs}"""		,       force_update=UpperCAmelCase_       )
														snake_case_           =		None







"""simple docstring"""
import json
import os
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
				import torch

if is_vision_available():
				from PIL import Image

				from transformers import ImageGPTImageProcessor





class      __A     (unittest.TestCase):





							'''simple docstring'''

							def __init__(				self							:      int		,       UpperCAmelCase_							:      List[str]		,       UpperCAmelCase_							:      Tuple=7		,       UpperCAmelCase_							:      Dict=3		,       UpperCAmelCase_							:      Tuple=18		,       UpperCAmelCase_							:      Optional[Any]=30		,       UpperCAmelCase_							:      str=400		,       UpperCAmelCase_							:      int=True		,       UpperCAmelCase_							:      Optional[int]=None		,       UpperCAmelCase_							:      Optional[Any]=True		,       )					->Optional[Any]:



														"""simple docstring"""



														snake_case_           =		size if size is not None else {"""height""": 18, """width""": 18}
														snake_case_           =		parent
														snake_case_           =		batch_size
														snake_case_           =		num_channels
														snake_case_           =		image_size
														snake_case_           =		min_resolution
														snake_case_           =		max_resolution
														snake_case_           =		do_resize
														snake_case_           =		size
														snake_case_           =		do_normalize



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



														"""simple docstring"""



														return {
														    # here we create 2 clusters for the sake of simplicity
														    "clusters": np.asarray(
														        [
														            [0.8_866_443_634_033_203, 0.6_618_829_369_544_983, 0.3_891_746_401_786_804],
														            [-0.6_042_559_146_881_104, -0.02_295_008_860_528_469, 0.5_423_797_369_003_296],
														        ]       ),
														    "do_resize": self.do_resize,
														    "size": self.size,
														    "do_normalize": self.do_normalize,
														}





@require_torch
@require_vision
class      __A     (snake_case__						,		unittest.TestCase):





							'''simple docstring'''

							__lowercase:		List[Any]							      =    ImageGPTImageProcessor if is_vision_available() else None



							def 		lowerCAmelCase       (				self							:      Optional[int]       )					->Optional[int]:



														"""simple docstring"""



														snake_case_           =		ImageGPTImageProcessingTester(self       )



							@property
							def 		lowerCAmelCase       (				self							:      Tuple       )					->List[Any]:



														"""simple docstring"""



														return self.image_processor_tester.prepare_image_processor_dict()



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



														"""simple docstring"""



														snake_case_           =		self.image_processing_class(**self.image_processor_dict       )
														self.assertTrue(hasattr(UpperCAmelCase_		,       """clusters"""       )       )
														self.assertTrue(hasattr(UpperCAmelCase_		,       """do_resize"""       )       )
														self.assertTrue(hasattr(UpperCAmelCase_		,       """size"""       )       )
														self.assertTrue(hasattr(UpperCAmelCase_		,       """do_normalize"""       )       )



							def 		lowerCAmelCase       (				self							:      Optional[int]       )					->Optional[Any]:



														"""simple docstring"""



														snake_case_           =		self.image_processing_class.from_dict(self.image_processor_dict       )
														self.assertEqual(image_processor.size		,       {"""height""": 18, """width""": 18}       )

														snake_case_           =		self.image_processing_class.from_dict(self.image_processor_dict		,       size=42       )
														self.assertEqual(image_processor.size		,       {"""height""": 42, """width""": 42}       )



							def 		lowerCAmelCase       (				self							:      Any       )					->List[Any]:



														"""simple docstring"""



														snake_case_           =		self.image_processing_class(**self.image_processor_dict       )
														snake_case_           =		json.loads(image_processor.to_json_string()       )
														for key, value in self.image_processor_dict.items():
																					if key == "clusters":
																												self.assertTrue(np.array_equal(UpperCAmelCase_		,       obj[key]       )       )
																					else:
																												self.assertEqual(obj[key]		,       UpperCAmelCase_       )



							def 		lowerCAmelCase       (				self							:      Optional[int]       )					->Dict:



														"""simple docstring"""



														snake_case_           =		self.image_processing_class(**self.image_processor_dict       )

														with tempfile.TemporaryDirectory() as tmpdirname:
																					snake_case_           =		os.path.join(UpperCAmelCase_		,       """image_processor.json"""       )
																					image_processor_first.to_json_file(UpperCAmelCase_       )
																					snake_case_           =		self.image_processing_class.from_json_file(UpperCAmelCase_       ).to_dict()

														snake_case_           =		image_processor_first.to_dict()
														for key, value in image_processor_first.items():
																					if key == "clusters":
																												self.assertTrue(np.array_equal(UpperCAmelCase_		,       image_processor_second[key]       )       )
																					else:
																												self.assertEqual(image_processor_first[key]		,       UpperCAmelCase_       )



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



														"""simple docstring"""



														snake_case_           =		self.image_processing_class(**self.image_processor_dict       )

														with tempfile.TemporaryDirectory() as tmpdirname:
																					image_processor_first.save_pretrained(UpperCAmelCase_       )
																					snake_case_           =		self.image_processing_class.from_pretrained(UpperCAmelCase_       ).to_dict()

														snake_case_           =		image_processor_first.to_dict()
														for key, value in image_processor_first.items():
																					if key == "clusters":
																												self.assertTrue(np.array_equal(UpperCAmelCase_		,       image_processor_second[key]       )       )
																					else:
																												self.assertEqual(image_processor_first[key]		,       UpperCAmelCase_       )



							@unittest.skip("""ImageGPT requires clusters at initialization"""       )
							def 		lowerCAmelCase       (				self							:      List[Any]       )					->Tuple:



														"""simple docstring"""



														pass







def 						_a				(      )    ->				str:
							snake_case_           =		load_dataset("""hf-internal-testing/fixtures_image_utils"""				, split="""test"""				)

							snake_case_           =		Image.open(dataset[4]["""file"""]				)
							snake_case_           =		Image.open(dataset[5]["""file"""]				)

							snake_case_           =		[imagea, imagea]

							return images





@require_vision
@require_torch
class      __A     (unittest.TestCase):





							'''simple docstring'''

							@slow
							def 		lowerCAmelCase       (				self							:      Tuple       )					->List[str]:



														"""simple docstring"""



														snake_case_           =		ImageGPTImageProcessor.from_pretrained("""openai/imagegpt-small"""       )

														snake_case_           =		prepare_images()

														# test non-batched
														snake_case_           =		image_processing(images[0]		,       return_tensors="""pt"""       )

														self.assertIsInstance(encoding.input_ids		,       torch.LongTensor       )
														self.assertEqual(encoding.input_ids.shape		,       (1, 1_024)       )

														snake_case_           =		[306, 191, 191]
														self.assertEqual(encoding.input_ids[0, :3].tolist()		,       UpperCAmelCase_       )

														# test batched
														snake_case_           =		image_processing(UpperCAmelCase_		,       return_tensors="""pt"""       )

														self.assertIsInstance(encoding.input_ids		,       torch.LongTensor       )
														self.assertEqual(encoding.input_ids.shape		,       (2, 1_024)       )

														snake_case_           =		[303, 13, 13]
														self.assertEqual(encoding.input_ids[1, -3:].tolist()		,       UpperCAmelCase_       )



