

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__					(			__A  :						str			)					->int:
      __A    =int(__A			)
      __A    ,		__A    ,		__A    =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__					(			__A  :						List[Any]     ,				__A  :						str     ,				__A  :						List[str]     ,				__A  :						Union[str, Any]     ,				__A  :						List[Any]=3_00			)					->List[str]:
      # 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__					(			__A  :						Optional[Any]			)					->Tuple:
      __A    ='''<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:
                  __A    =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__  :



       '''simple docstring'''







       lowercase_						=    5
       lowercase_						=    0.2

       def __init__(							self ,   lowercase__ ,   lowercase__ = None ,   lowercase__ = True ,   lowercase__ = None ,   lowercase__ = 3_0_0 ,   ):







             '''simple docstring'''



             __A    =total
             __A    ='''''' if prefix is None else prefix
             __A    =leave
             __A    =parent
             __A    =width
             __A    =None
             __A    =None
             __A    =None

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ = False ,   lowercase__ = None     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__=None     ):







             '''simple docstring'''



             __A    =''' ''' * (len(str(self.total     )     ) - len(str(lowercase__     )     )) + str(lowercase__     )
             if self.elapsed_time is None:
                   __A    =f'''[{spaced_value}/{self.total} : < :'''
             elif self.predicted_remaining is None:
                   __A    =f'''[{spaced_value}/{self.total} {format_time(self.elapsed_time     )}'''
             else:
                   __A    =(
                       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     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =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:
                   __A    =disp.display(disp.HTML(self.html_code     ) ,   display_id=lowercase__     )
             else:
                   self.output.update(disp.HTML(self.html_code     )     )


       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



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





class        lowerCAmelCase__  (					__magic_name__			):



       '''simple docstring'''







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







             '''simple docstring'''



             super().__init__(lowercase__     )
             __A    =None if column_names is None else [column_names]
             __A    =None

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =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:
                   __A    =disp.display(disp.HTML(self.html_code     ) ,   display_id=lowercase__     )
             else:
                   self.output.update(disp.HTML(self.html_code     )     )

       def     __UpperCamelCase     (							self ,   lowercase__     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__=None ,   lowercase__=3_0_0     ):







             '''simple docstring'''



             __A    =NotebookProgressBar(lowercase__ ,   prefix=lowercase__ ,   parent=self ,   width=lowercase__     )
             return self.child_bar


       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =None
             self.display()





class        lowerCAmelCase__  (					__magic_name__			):



       '''simple docstring'''







       def __init__(							self     ):







             '''simple docstring'''



             __A    =None
             __A    =None
             __A    =False

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__ ,   **lowercase__     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__ ,   **lowercase__     ):







             '''simple docstring'''



             __A    =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 ,   )
             __A    =False

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__ ,   lowercase__=None ,   **lowercase__     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__ ,   **lowercase__     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__ ,   lowercase__=None ,   **lowercase__     ):







             '''simple docstring'''



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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__ ,   lowercase__=None ,   **lowercase__     ):







             '''simple docstring'''



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

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


       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__ ,   **lowercase__     ):







             '''simple docstring'''



             self.training_tracker.update(
                 state.global_step ,   comment=f'''Epoch {int(state.epoch     )}/{state.num_train_epochs}''' ,   force_update=lowercase__     )
             __A    =None





import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType





class        lowerCAmelCase__  (					__magic_name__			):



       '''simple docstring'''







       lowercase_						=    ["""image_processor""", """tokenizer"""]
       lowercase_						=    """LayoutLMv2ImageProcessor"""
       lowercase_						=    ("""LayoutXLMTokenizer""", """LayoutXLMTokenizerFast""")

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







             '''simple docstring'''



             if "feature_extractor" in kwargs:
                   warnings.warn(
                       '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
                       ''' instead.''' ,   lowercase__ ,   )
                   __A    =kwargs.pop('''feature_extractor'''     )

             __A    =image_processor if image_processor is not None else feature_extractor
             if image_processor is None:
                   raise ValueError('''You need to specify an `image_processor`.'''     )
             if tokenizer is None:
                   raise ValueError('''You need to specify a `tokenizer`.'''     )

             super().__init__(lowercase__ ,   lowercase__     )

       def __call__(							self ,   lowercase__ ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = True ,   lowercase__ = False ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = 0 ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = None ,   lowercase__ = False ,   lowercase__ = False ,   lowercase__ = False ,   lowercase__ = False ,   lowercase__ = True ,   lowercase__ = None ,   **lowercase__ ,   ):







             '''simple docstring'''



             if self.image_processor.apply_ocr and (boxes is not None):
                   raise ValueError(
                       '''You cannot provide bounding boxes '''
                       '''if you initialized the image processor with apply_ocr set to True.'''     )

             if self.image_processor.apply_ocr and (word_labels is not None):
                   raise ValueError(
                       '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.'''     )

             if return_overflowing_tokens is True and return_offsets_mapping is False:
                   raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.'''     )

             # first, apply the image processor
             __A    =self.image_processor(images=lowercase__ ,   return_tensors=lowercase__     )

             # second, apply the tokenizer
             if text is not None and self.image_processor.apply_ocr and text_pair is None:
                   if isinstance(lowercase__ ,   lowercase__     ):
                         __A    =[text]  # add batch dimension (as the image processor always adds a batch dimension)
                   __A    =features['''words''']

             __A    =self.tokenizer(
                 text=text if text is not None else features['''words'''] ,   text_pair=text_pair if text_pair is not None else None ,   boxes=boxes if boxes is not None else features['''boxes'''] ,   word_labels=lowercase__ ,   add_special_tokens=lowercase__ ,   padding=lowercase__ ,   truncation=lowercase__ ,   max_length=lowercase__ ,   stride=lowercase__ ,   pad_to_multiple_of=lowercase__ ,   return_token_type_ids=lowercase__ ,   return_attention_mask=lowercase__ ,   return_overflowing_tokens=lowercase__ ,   return_special_tokens_mask=lowercase__ ,   return_offsets_mapping=lowercase__ ,   return_length=lowercase__ ,   verbose=lowercase__ ,   return_tensors=lowercase__ ,   **lowercase__ ,   )

             # add pixel values
             __A    =features.pop('''pixel_values'''     )
             if return_overflowing_tokens is True:
                   __A    =self.get_overflowing_images(lowercase__ ,   encoded_inputs['''overflow_to_sample_mapping''']     )
             __A    =images

             return encoded_inputs

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__     ):







             '''simple docstring'''



             __A    =[]
             for sample_idx in overflow_to_sample_mapping:
                   images_with_overflow.append(images[sample_idx]     )

             if len(lowercase__     ) != len(lowercase__     ):
                   raise ValueError(
                       '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'''
                       f''' {len(lowercase__     )} and {len(lowercase__     )}'''     )

             return images_with_overflow

       def     __UpperCamelCase     (							self ,   *lowercase__ ,   **lowercase__     ):







             '''simple docstring'''



             return self.tokenizer.batch_decode(*lowercase__ ,   **lowercase__     )

       def     __UpperCamelCase     (							self ,   *lowercase__ ,   **lowercase__     ):







             '''simple docstring'''



             return self.tokenizer.decode(*lowercase__ ,   **lowercase__     )

       @property
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             return ["input_ids", "bbox", "attention_mask", "image"]

       @property
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             warnings.warn(
                 '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' ,   lowercase__ ,   )
             return self.image_processor_class


       @property
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             warnings.warn(
                 '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' ,   lowercase__ ,   )
             return self.image_processor


