



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__	(						_UpperCamelCase	:       Any							):
   __lowerCamelCase  							=			int(__a							)
   __lowerCamelCase  							=			t // 36_00, (t // 60) % 60, t % 60
   return F"""{h}:{m:02d}:{s:02d}""" if h != 0 else F"""{m:02d}:{s:02d}"""





def 						a__	(						_UpperCamelCase	:       Any       ,_UpperCamelCase	:       Union[str, Any]       ,_UpperCamelCase	:       Optional[int]       ,_UpperCamelCase	:       Optional[int]       ,_UpperCamelCase	:       Any=3_00							):
   return F"""
    <div>
      {prefix}
      <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress>
      {label}
    </div>
    """





def 						a__	(						_UpperCamelCase	:       Optional[int]							):
   __lowerCamelCase  							=			'''<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:
         __lowerCamelCase  							=			F"""{elt:.6f}""" if isinstance(__a       ,__a							) else str(__a							)
         html_code += F"""      <td>{elt}</td>\n"""
      html_code += "    </tr>\n"
   html_code += "  </tbody>\n</table><p>"
   return html_code



class        __lowerCAmelCase   :
 lowerCAmelCase__				    =     5
 lowerCAmelCase__				    =     0.2




 def __init__(  self		, __UpperCAmelCase		, __UpperCAmelCase = None		, __UpperCAmelCase = True		, __UpperCAmelCase = None		, __UpperCAmelCase = 300		, ):



    '''simple docstring'''
    __lowerCamelCase  							=			total
    __lowerCamelCase  							=			'''''' if prefix is None else prefix
    __lowerCamelCase  							=			leave
    __lowerCamelCase  							=			parent
    __lowerCamelCase  							=			width
    __lowerCamelCase  							=			None
    __lowerCamelCase  							=			None
    __lowerCamelCase  							=			None




 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase = False		, __UpperCAmelCase = None ):



    '''simple docstring'''
    __lowerCamelCase  							=			value
    if comment is not None:
       __lowerCamelCase  							=			comment
    if self.last_value is None:
       __lowerCamelCase  							=			time.time()
       __lowerCamelCase  							=			value
       __lowerCamelCase  							=			None
       __lowerCamelCase  							=			self.warmup
       __lowerCamelCase  							=			1
       self.update_bar(_a )
    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
       __lowerCamelCase  							=			time.time()
       __lowerCamelCase  							=			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:
          __lowerCamelCase  							=			self.elapsed_time / (value - self.start_value)
       else:
          __lowerCamelCase  							=			None
       if value >= self.total:
          __lowerCamelCase  							=			self.total
          __lowerCamelCase  							=			None
          if not self.leave:
             self.close()
       elif self.average_time_per_item is not None:
          __lowerCamelCase  							=			self.average_time_per_item * (self.total - value)
       self.update_bar(_a )
       __lowerCamelCase  							=			value
       __lowerCamelCase  							=			current_time
       if self.average_time_per_item is None:
          __lowerCamelCase  							=			1
       else:
          __lowerCamelCase  							=			max(int(self.update_every / self.average_time_per_item )		, 1 )




 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase=None ):



    '''simple docstring'''
    __lowerCamelCase  							=			''' ''' * (len(str(self.total ) ) - len(str(_a ) )) + str(_a )
    if self.elapsed_time is None:
       __lowerCamelCase  							=			F"""[{spaced_value}/{self.total} : < :"""
    elif self.predicted_remaining is None:
       __lowerCamelCase  							=			F"""[{spaced_value}/{self.total} {format_time(self.elapsed_time )}"""
    else:
       __lowerCamelCase  							=			(
           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 ):



    '''simple docstring'''
    __lowerCamelCase  							=			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:
       __lowerCamelCase  							=			disp.display(disp.HTML(self.html_code )		, display_id=_a )
    else:
       self.output.update(disp.HTML(self.html_code ) )




 def 				lowerCamelCase					(  self ):



    '''simple docstring'''
    if self.parent is None and self.output is not None:
       self.output.update(disp.HTML('''''' ) )



class        __lowerCAmelCase   (		__lowercase      ):




 def __init__(  self		, __UpperCAmelCase		, __UpperCAmelCase=None ):



    '''simple docstring'''
    super().__init__(_a )
    __lowerCamelCase  							=			None if column_names is None else [column_names]
    __lowerCamelCase  							=			None




 def 				lowerCamelCase					(  self ):



    '''simple docstring'''
    __lowerCamelCase  							=			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:
       __lowerCamelCase  							=			disp.display(disp.HTML(self.html_code )		, display_id=_a )
    else:
       self.output.update(disp.HTML(self.html_code ) )




 def 				lowerCamelCase					(  self		, __UpperCAmelCase ):



    '''simple docstring'''
    if self.inner_table is None:
       __lowerCamelCase  							=			[list(values.keys() ), list(values.values() )]
    else:
       __lowerCamelCase  							=			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(_a )
          __lowerCamelCase  							=			columns
       self.inner_table.append([values[c] for c in columns] )




 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase=None		, __UpperCAmelCase=300 ):



    '''simple docstring'''
    __lowerCamelCase  							=			NotebookProgressBar(_a		, prefix=_a		, parent=self		, width=_a )
    return self.child_bar




 def 				lowerCamelCase					(  self ):



    '''simple docstring'''
    __lowerCamelCase  							=			None
    self.display()



class        __lowerCAmelCase   (		__lowercase      ):




 def __init__(  self ):



    '''simple docstring'''
    __lowerCamelCase  							=			None
    __lowerCamelCase  							=			None
    __lowerCamelCase  							=			False




 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase		, **__UpperCAmelCase ):



    '''simple docstring'''
    __lowerCamelCase  							=			'''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step'''
    __lowerCamelCase  							=			0
    __lowerCamelCase  							=			0
    __lowerCamelCase  							=			[self.first_column] + ['''Training Loss''']
    if args.evaluation_strategy != IntervalStrategy.NO:
       column_names.append('''Validation Loss''' )
    __lowerCamelCase  							=			NotebookTrainingTracker(state.max_steps		, _a )




 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase		, **__UpperCAmelCase ):



    '''simple docstring'''
    __lowerCamelCase  							=			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		, )
    __lowerCamelCase  							=			False




 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase=None		, **__UpperCAmelCase ):



    '''simple docstring'''
    if not has_length(_a ):
       return
    if self.prediction_bar is None:
       if self.training_tracker is not None:
          __lowerCamelCase  							=			self.training_tracker.add_child(len(_a ) )
       else:
          __lowerCamelCase  							=			NotebookProgressBar(len(_a ) )
       self.prediction_bar.update(1 )
    else:
       self.prediction_bar.update(self.prediction_bar.value + 1 )




 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase		, **__UpperCAmelCase ):



    '''simple docstring'''
    if self.prediction_bar is not None:
       self.prediction_bar.close()
    __lowerCamelCase  							=			None




 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase=None		, **__UpperCAmelCase ):



    '''simple docstring'''
    # Only for when there is no evaluation
    if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
       __lowerCamelCase  							=			{'''Training Loss''': logs['''loss''']}
       # First column is necessarily Step sine we're not in epoch eval strategy
       __lowerCamelCase  							=			state.global_step
       self.training_tracker.write_line(_a )




 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase=None		, **__UpperCAmelCase ):



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

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




 def 				lowerCamelCase					(  self		, __UpperCAmelCase		, __UpperCAmelCase		, __UpperCAmelCase		, **__UpperCAmelCase ):



    '''simple docstring'''
    self.training_tracker.update(
        state.global_step		, comment=F"""Epoch {int(state.epoch )}/{state.num_train_epochs}"""		, force_update=_a )
    __lowerCamelCase  							=			None









a__				=		'''0.18.2'''

from .configuration_utils import ConfigMixin
from .utils import (
    OptionalDependencyNotAvailable,
    is_flax_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_onnx_available,
    is_scipy_available,
    is_torch_available,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    logging,
)


try:
			if not is_onnx_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_onnx_objects import *  # noqa F403
else:
			from .pipelines import OnnxRuntimeModel

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_pt_objects import *  # noqa F403
else:
			from .models import (
			    AutoencoderKL,
			    ControlNetModel,
			    ModelMixin,
			    PriorTransformer,
			    TaFilmDecoder,
			    TransformeraDModel,
			    UNetaDModel,
			    UNetaDConditionModel,
			    UNetaDModel,
			    UNetaDConditionModel,
			    VQModel,
			)
			from .optimization import (
			    get_constant_schedule,
			    get_constant_schedule_with_warmup,
			    get_cosine_schedule_with_warmup,
			    get_cosine_with_hard_restarts_schedule_with_warmup,
			    get_linear_schedule_with_warmup,
			    get_polynomial_decay_schedule_with_warmup,
			    get_scheduler,
			)
			from .pipelines import (
			    AudioPipelineOutput,
			    ConsistencyModelPipeline,
			    DanceDiffusionPipeline,
			    DDIMPipeline,
			    DDPMPipeline,
			    DiffusionPipeline,
			    DiTPipeline,
			    ImagePipelineOutput,
			    KarrasVePipeline,
			    LDMPipeline,
			    LDMSuperResolutionPipeline,
			    PNDMPipeline,
			    RePaintPipeline,
			    ScoreSdeVePipeline,
			)
			from .schedulers import (
			    CMStochasticIterativeScheduler,
			    DDIMInverseScheduler,
			    DDIMParallelScheduler,
			    DDIMScheduler,
			    DDPMParallelScheduler,
			    DDPMScheduler,
			    DEISMultistepScheduler,
			    DPMSolverMultistepInverseScheduler,
			    DPMSolverMultistepScheduler,
			    DPMSolverSinglestepScheduler,
			    EulerAncestralDiscreteScheduler,
			    EulerDiscreteScheduler,
			    HeunDiscreteScheduler,
			    IPNDMScheduler,
			    KarrasVeScheduler,
			    KDPMaAncestralDiscreteScheduler,
			    KDPMaDiscreteScheduler,
			    PNDMScheduler,
			    RePaintScheduler,
			    SchedulerMixin,
			    ScoreSdeVeScheduler,
			    UnCLIPScheduler,
			    UniPCMultistepScheduler,
			    VQDiffusionScheduler,
			)
			from .training_utils import EMAModel

try:
			if not (is_torch_available() and is_scipy_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_torch_and_scipy_objects import *  # noqa F403
else:
			from .schedulers import LMSDiscreteScheduler

try:
			if not (is_torch_available() and is_torchsde_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_torch_and_torchsde_objects import *  # noqa F403
else:
			from .schedulers import DPMSolverSDEScheduler

try:
			if not (is_torch_available() and is_transformers_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
			from .pipelines import (
			    AltDiffusionImgaImgPipeline,
			    AltDiffusionPipeline,
			    AudioLDMPipeline,
			    CycleDiffusionPipeline,
			    IFImgaImgPipeline,
			    IFImgaImgSuperResolutionPipeline,
			    IFInpaintingPipeline,
			    IFInpaintingSuperResolutionPipeline,
			    IFPipeline,
			    IFSuperResolutionPipeline,
			    ImageTextPipelineOutput,
			    KandinskyImgaImgPipeline,
			    KandinskyInpaintPipeline,
			    KandinskyPipeline,
			    KandinskyPriorPipeline,
			    KandinskyVaaControlnetImgaImgPipeline,
			    KandinskyVaaControlnetPipeline,
			    KandinskyVaaImgaImgPipeline,
			    KandinskyVaaInpaintPipeline,
			    KandinskyVaaPipeline,
			    KandinskyVaaPriorEmbaEmbPipeline,
			    KandinskyVaaPriorPipeline,
			    LDMTextToImagePipeline,
			    PaintByExamplePipeline,
			    SemanticStableDiffusionPipeline,
			    ShapEImgaImgPipeline,
			    ShapEPipeline,
			    StableDiffusionAttendAndExcitePipeline,
			    StableDiffusionControlNetImgaImgPipeline,
			    StableDiffusionControlNetInpaintPipeline,
			    StableDiffusionControlNetPipeline,
			    StableDiffusionDepthaImgPipeline,
			    StableDiffusionDiffEditPipeline,
			    StableDiffusionImageVariationPipeline,
			    StableDiffusionImgaImgPipeline,
			    StableDiffusionInpaintPipeline,
			    StableDiffusionInpaintPipelineLegacy,
			    StableDiffusionInstructPixaPixPipeline,
			    StableDiffusionLatentUpscalePipeline,
			    StableDiffusionLDMaDPipeline,
			    StableDiffusionModelEditingPipeline,
			    StableDiffusionPanoramaPipeline,
			    StableDiffusionParadigmsPipeline,
			    StableDiffusionPipeline,
			    StableDiffusionPipelineSafe,
			    StableDiffusionPixaPixZeroPipeline,
			    StableDiffusionSAGPipeline,
			    StableDiffusionUpscalePipeline,
			    StableUnCLIPImgaImgPipeline,
			    StableUnCLIPPipeline,
			    TextToVideoSDPipeline,
			    TextToVideoZeroPipeline,
			    UnCLIPImageVariationPipeline,
			    UnCLIPPipeline,
			    UniDiffuserModel,
			    UniDiffuserPipeline,
			    UniDiffuserTextDecoder,
			    VersatileDiffusionDualGuidedPipeline,
			    VersatileDiffusionImageVariationPipeline,
			    VersatileDiffusionPipeline,
			    VersatileDiffusionTextToImagePipeline,
			    VideoToVideoSDPipeline,
			    VQDiffusionPipeline,
			)

try:
			if not (is_torch_available() and is_transformers_available() and is_invisible_watermark_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_torch_and_transformers_and_invisible_watermark_objects import *  # noqa F403
else:
			from .pipelines import StableDiffusionXLImgaImgPipeline, StableDiffusionXLPipeline

try:
			if not (is_torch_available() and is_transformers_available() and is_k_diffusion_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_torch_and_transformers_and_k_diffusion_objects import *  # noqa F403
else:
			from .pipelines import StableDiffusionKDiffusionPipeline

try:
			if not (is_torch_available() and is_transformers_available() and is_onnx_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_torch_and_transformers_and_onnx_objects import *  # noqa F403
else:
			from .pipelines import (
			    OnnxStableDiffusionImgaImgPipeline,
			    OnnxStableDiffusionInpaintPipeline,
			    OnnxStableDiffusionInpaintPipelineLegacy,
			    OnnxStableDiffusionPipeline,
			    OnnxStableDiffusionUpscalePipeline,
			    StableDiffusionOnnxPipeline,
			)

try:
			if not (is_torch_available() and is_librosa_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_torch_and_librosa_objects import *  # noqa F403
else:
			from .pipelines import AudioDiffusionPipeline, Mel

try:
			if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
			from .pipelines import SpectrogramDiffusionPipeline

try:
			if not is_flax_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_flax_objects import *  # noqa F403
else:
			from .models.controlnet_flax import FlaxControlNetModel
			from .models.modeling_flax_utils import FlaxModelMixin
			from .models.unet_ad_condition_flax import FlaxUNetaDConditionModel
			from .models.vae_flax import FlaxAutoencoderKL
			from .pipelines import FlaxDiffusionPipeline
			from .schedulers import (
			    FlaxDDIMScheduler,
			    FlaxDDPMScheduler,
			    FlaxDPMSolverMultistepScheduler,
			    FlaxKarrasVeScheduler,
			    FlaxLMSDiscreteScheduler,
			    FlaxPNDMScheduler,
			    FlaxSchedulerMixin,
			    FlaxScoreSdeVeScheduler,
			)


try:
			if not (is_flax_available() and is_transformers_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_flax_and_transformers_objects import *  # noqa F403
else:
			from .pipelines import (
			    FlaxStableDiffusionControlNetPipeline,
			    FlaxStableDiffusionImgaImgPipeline,
			    FlaxStableDiffusionInpaintPipeline,
			    FlaxStableDiffusionPipeline,
			)

try:
			if not (is_note_seq_available()):
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			from .utils.dummy_note_seq_objects import *  # noqa F403
else:
			from .pipelines import MidiProcessor



