
    0h<L                     :   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z
ddlZddlmc mZ ddlmZ ddlmZ ddlmZ d ZddZddZ ed	       G d
 d             Zd ZddZd Zd Zd Zd Z	 ddZ d Z!d Z"d Z# G d de
jH                        Z%y)z&Python utilities required by TF-Keras.    N)io_utils)
tf_inspect)keras_exportc                    t         j                  dk(  rUt        j                  | j                        j                  dd      }t        j                  |d      j                  d      }nDt        j                  | j                        }t        j                  |d      j                  d      }| j                  }| j                  rt        d | j                  D              }nd}|||fS )zSerializes a user defined function.

    Args:
        func: the function to serialize.

    Returns:
        A tuple `(code, defaults, closure)`.
    nt   \   /base64asciic              3   4   K   | ]  }|j                     y wN)cell_contents).0cs     Z/var/www/html/engine/venv/lib/python3.12/site-packages/tf_keras/src/utils/generic_utils.py	<genexpr>zfunc_dump.<locals>.<genexpr>6   s     BABs   N)osnamemarshaldumps__code__replacecodecsencodedecode__defaults____closure__tuple)funcraw_codecodedefaultsclosures        r   	func_dumpr$   %   s     
ww$==/77tD}}Xx077@==/}}Xx077@  HB1A1ABB7""    c                    t        | t        t        f      r!| \  } }}t        |t              rt        |      }d |t        fd|D              }	 t        j                  | j                  d      d      }t        j                  |      } |
t               }t        j                  | || j                  ||      S # t        t        j                  f$ r | j                  d      }Y qw xY w)a	  Deserializes a user defined function.

    Args:
        code: bytecode of the function.
        defaults: defaults of the function.
        closure: closure of the function.
        globs: dictionary of global objects.

    Returns:
        A function object.
    c                 ^      fd}|j                   d   }t         t        |            s|S  S )zEnsures that a value is converted to a python cell object.

        Args:
            value: Any value that needs to be casted to the cell type

        Returns:
            A value wrapped as a cell object (see function "func_load")
        c                  
      y r    )values   r   dummy_fnz9func_load.<locals>.ensure_value_to_cell.<locals>.dummy_fnW   s    r%   r   )r   
isinstancetype)r*   r+   
cell_values   `  r   ensure_value_to_cellz'func_load.<locals>.ensure_value_to_cellM   s4    	 ))!,
%j!12r%   c              3   .   K   | ]  } |        y wr   r)   )r   _r/   s     r   r   zfunc_load.<locals>.<genexpr>`   s     AA,Q/As   r   r
   raw_unicode_escape)r   argdefsr#   )r,   r   listr   r   r   UnicodeEncodeErrorbinasciiErrorr   loadsglobalspython_typesFunctionTypeco_name)r!   r"   r#   globsr    r/   s        @r   	func_loadr>   <   s     $&"&hh%XH$ AAA5==W!5x@ =="D}	$$e$,,'  / 5;;345s   %B= =*C*)C*c                     t        j                  |       }|r|j                  y||j                  v xs ||j                  v S )a  Checks if a callable accepts a given keyword argument.

    Args:
        fn: Callable to inspect.
        name: Check if `fn` can be called with `name` as a keyword argument.
        accept_all: What to return if there is no parameter called `name` but
          the function accepts a `**kwargs` argument.

    Returns:
        bool, whether `fn` accepts a `name` keyword argument.
    T)r   getfullargspecvarkwargs
kwonlyargs)fnr   
accept_allarg_specs       r   has_argrG   m   sB     ((,Hhnn08== ?DH,?,?$??r%   zkeras.utils.Progbarc                   D    e Zd ZdZ	 	 	 	 	 d	dZd
dZddZd Zd Zd Z	y)Progbara[  Displays a progress bar.

    Args:
        target: Total number of steps expected, None if unknown.
        width: Progress bar width on screen.
        verbose: Verbosity mode, 0 (silent), 1 (verbose), 2 (semi-verbose)
        stateful_metrics: Iterable of string names of metrics that should *not*
          be averaged over time. Metrics in this list will be displayed as-is.
          All others will be averaged by the progbar before display.
        interval: Minimum visual progress update interval (in seconds).
        unit_name: Display name for step counts (usually "step" or "sample").
    Nc                 :   || _         || _        || _        || _        || _        |rt        |      | _        nt               | _        t        t        j                  d      xr t        j                  j                         xs: dt        j                  v xs& dt        j                  v xs dt        j                  v | _        d| _        d| _        i | _        g | _        t'        j&                         | _        d| _        | j(                  | _        d | _        d | _        y )Nisatty	ipykernelposixPYCHARM_HOSTEDr   )targetwidthverboseinterval	unit_namesetstateful_metricshasattrsysstdoutrK   modulesr   environ_dynamic_display_total_width_seen_so_far_values_values_ordertime_start_last_update_time_at_epoch_start_time_at_epoch_end_time_after_first_step)selfrO   rP   rQ   rR   rU   rS   s          r   __init__zProgbar.__init__   s     
 "$'(8$9D!$'ED! SZZ*Bszz/@/@/B .ckk).#++%.  2::-	 	  iik$(KK!"&&*#r%   c           	      4   || j                   d}n|| j                   k\  }|xs g }|D ]  \  }}|| j                  vr| j                  j                  |       || j                  vrtt	        || j
                  z
  d      }|| j                  vr||z  |g| j                  |<   y| j                  |   dxx   ||z  z  cc<   | j                  |   dxx   |z  cc<   |dg| j                  |<    || _        d}t        j                         }d|| j                  z
  dd}	|| j                   k(  r|| _	        | j                  dk(  r|| j                  z
  | j                  k  r|sy| j                  }
| j                  r|d	|
z  z  }|d
z  }n|dz  }| j                   t        t!        j"                  | j                               dz   }dt%        |      z   dz   || j                   fz  }t'        |      | j                   z  }t        | j(                  |z        }|dkD  r%|d|dz
  z  z  }|| j                   k  r|dz  }n|dz  }|d| j(                  |z
  z  z  }|dz  }nd|z  }t+        |      | _        ||z  }| j-                  ||      }| j                   |r |	| j/                  || j0                        z  }	nI|| j                   |z
  z  }|dkD  rd|dz  |dz  dz  |dz  fz  }n|dkD  rd|dz  |dz  fz  }nd|z  }d| }	| j                  D ]  }|	d| dz  }	t3        | j                  |   t4              rdt!        j6                  | j                  |   d   t	        d| j                  |   d         z        }t9        |      dkD  r
|	d|dz  }	|	d|dz  }	|	d| j                  |    z  }	 | xj                  t+        |	      z  c_        |
| j                  kD  r|	d|
| j                  z
  z  z  }	|r|	dz  }	||	z  }t;        j<                  |d       d}|| _        y| j                  dk(  rf|rct        t!        j"                  | j                               dz   }dt%        |      z   d z   || j                   fz  }||	z   }	| j                  D ]e  }|	d| dz  }	t!        j6                  | j                  |   d   t	        d| j                  |   d         z        }|dkD  r
|	d|dz  }	]|	d|dz  }	g | j                  ru| j                  | j>                  z
  }|| j                   z  }|| _        d| _	        |	d!| j/                  |d"      z   z  }	|	d!| j/                  || j0                        z   z  }	|	dz  }	||	z  }t;        j<                  |d       d}|| _        y)#a  Updates the progress bar.

        Args:
            current: Index of current step.
            values: List of tuples: `(name, value_for_last_step)`. If `name` is
              in `stateful_metrics`, `value_for_last_step` will be displayed
              as-is. Else, an average of the metric over time will be
              displayed.
            finalize: Whether this is the last update for the progress bar. If
              `None`, uses `current >= self.target`. Defaults to `None`.
        NF   r    z - .0fs
%zd/%d [=>.]z%7d/Unknowni  z%d:%02d:%02d<   z%d:%02dz%dsz - ETA: :MbP? z.4fz.4e)
line_break   zd/%dz -epoch) rO   r_   appendrU   maxr]   r^   r`   ra   rd   rQ   rb   rR   r\   r[   intnplog10strfloatrP   len_estimate_step_duration_format_timerS   r,   r4   meanabsr   	print_msgrc   )rf   currentvaluesfinalizekv
value_basemessagenowinfoprev_total_width	numdigitsbarprog
prog_widthtime_per_uniteta
eta_formatavgcounttime_per_epochavg_time_per_steps                         r   updatezProgbar.update   s    {{" "dkk12 	)DAq***""))!,--- !4+<+<!<a@
DLL('(:~z&BDLLOLLOA&!j.8&LLOA&*4&
 $%a&Q'	)( $iikS4;;&s+1-dkk!&)D#<<1T&&&6x#00$$4"22244{{& 56:	S^+h67DKK:PPW~3 d!23
>3*q.11C,s
s
sdjj:566s
#g- #CDsNG 88#FM{{"h))-HH#t{{W'<=:!/tt*b3 "J
 2X!*cRir-B!BJ!&J!*.'' 2#aS
"dll1ot4''Q*SDLLOA4F-GGC 3x$!C9-!C9-aQ011D2 T*$"3"33/$2C2CCDDtOGw59G@  = \\Q 56:	s9~-67DKK:PPt|++ .Ac!AJ&D''Q*SDLLOA4F-GGC Tz!C9-!C9-. **//$2K2KK # )7(D%03D-.2D+D4#4#4^W#MMMDD4#4#4)4>>$  D DLD4""7u=r%   c                 B    | j                  | j                  |z   |       y r   )r   r]   )rf   nr   s      r   addzProgbar.addJ  s    D%%)62r%   c                     d}|dk\  s|dk(  r|d|dd| z  }|S |dk\  r|d|dz  dd	| z  }|S |d|d
z  dd| z  }|S )a  format a given duration to display to the user.

        Given the duration, this function formats it in either milliseconds
        or seconds and displays the unit (i.e. ms/step or s/epoch)
        Args:
          time_per_unit: the duration to display
          unit_name: the name of the unit to display
        Returns:
          a string with the correctly formatted duration and units
        rj   ri   r   rx   rk   zs/rw   g     @@zms/g    .Azus/r)   )rf   r   rS   	formatteds       r   r   zProgbar._format_timeM  s     	A!!31]3/r)==I
 	 d"1]V3C8I;GGI  1]Y6s;3ykJJIr%   c                     |rG| j                   |dkD  r|| j                   z
  |dz
  z  }n|| j                  z
  |z  }|dk(  r|| _         |S y)a  Estimate the duration of a single step.

        Given the step number `current` and the corresponding time `now` this
        function returns an estimate for how long a single step takes. If this
        is called before one step has been completed (i.e. `current == 0`) then
        zero is given as an estimate. The duration estimate ignores the duration
        of the (assumed to be non-representative) first step for estimates when
        more steps are available (i.e. `current>1`).

        Args:
          current: Index of current step.
          now: The current time.

        Returns: Estimate of the duration of a single step.
        ri   r   )re   ra   )rf   r   r   r   s       r   r   zProgbar._estimate_step_durationa  sd       **67Q;!$t'B'B!BaK! "%t{{!2g =!|.1+  r%   c                 D    | j                   j                  |      | _         y r   )rU   union)rf   rU   s     r   _update_stateful_metricsz Progbar._update_stateful_metrics  s     $ 5 5 ; ;<L Mr%   )   ri   g?NstepNNr   )
__name__
__module____qualname____doc__rg   r   r   r   r   r   r)   r%   r   rI   rI      s;      #+JU n3("HNr%   rI   c           	          t        t        j                  | t        |      z              }t	        d|      D cg c]  }||z  t        | |dz   |z        f c}S c c}w )zReturns a list of batch indices (tuples of indices).

    Args:
        size: Integer, total size of the data to slice into batches.
        batch_size: Integer, batch size.

    Returns:
        A list of tuples of array indices.
    r   ri   )r~   r   ceilr   rangemin)size
batch_sizenum_batchesis       r   make_batchesr     sc     bggdU:%6678K q+& 
ZTAEZ#789  s   Ac                    | dgS t        |t              r|t        d| d|       t        | t              rdt        |d      r3t        |d      r|j	                         }| D cg c]  }|dn||    c}S | D cg c]  }|dnt        |d      sdn|||  c}S t        |d      r!t        |d      r|j	                         }| |   S t        |d      r| || S dgS c c}w c c}w )a  Slice an array or list of arrays.

    This takes an array-like, or a list of
    array-likes, and outputs:
        - arrays[start:stop] if `arrays` is an array-like
        - [x[start:stop] for x in arrays] if `arrays` is a list

    Can also work on list/array of indices: `slice_arrays(x, indices)`

    Args:
        arrays: Single array or list of arrays.
        start: can be an integer index (start index) or a list/array of indices
        stop: integer (stop index); should be None if `start` was a list.

    Returns:
        A slice of the array(s).

    Raises:
        ValueError: If the value of start is a list and stop is not None.
    NzQThe stop argument has to be None if the value of start is a list. Received start=z, stop=__len__shape__getitem__)r,   r4   
ValueErrorrV   tolist)arraysstartstopxs       r   slice_arraysr     s#   * ~v%4#3)).wtf>
 	
 
FD	!5)$ug&=CDAID1U83DD 
 	 y  1m, 5	
 	
 5)$ug&%= 5-(%%%v! E
s   %C<C#c                 ,    t        | t              r| S | gS )zNormalizes a list/tensor into a list.

    If a tensor is passed, we return
    a list of size 1 containing the tensor.

    Args:
        x: target object to be normalized.

    Returns:
        A list.
    )r,   r4   )r   s    r   to_listr     s     !T3Jr%   c                     t        j                  dd|       }t        j                  dd|      j                         }|d   dk7  r|S d|z   S )Nz(.)([A-Z][a-z]+)z\1_\2z([a-z])([A-Z])r   r1   private)resublower)r   intermediateinsecures      r   to_snake_caser     sO    66,h=Lvv&,?EEGH {cxr%   c                 X    t         j                  j                  |       }|D ]  }| y y)NFT)tfnestflatten)	structureiterableelements      r   is_all_noner     s1    wwy)H  r%   c                     t        |j                               j                  |      }|rt        d|  dt	        |       d|       y )NzUnknown entries in z dictionary: z . Only expected following keys: )rT   keys
differencer   r4   )r   
input_dictexpected_valuesunknowns       r   check_for_unexpected_keysr     sU    *//#$//@G!$}T']O D--<,=?
 	
 r%   c                 2    | D ]  }||vst        ||       y)zAChecks that all keyword arguments are in the set of allowed keys.N)	TypeError)kwargsallowed_kwargserror_messagekwargs       r   validate_kwargsr     s(      2&M5112r%   c                     d| _         | S )z5Decorates a method to detect overrides in subclasses.T)_is_defaultmethods    r   defaultr     s    FMr%   c                     t        | dd      S )z:Check if a method is decorated with the `default` wrapper.r   F)getattrr   s    r   
is_defaultr   
  s    6=%00r%   c                 f    |D ],  }t        |      D ]  }t        ||      } ||      s|| |<    . y r   )dirr   )target_dictrY   
obj_filtermoduler   objs         r   !populate_dict_with_module_objectsr     sA     (K 	(D&$'C#$'D!	((r%   c                   .     e Zd ZdZ fdZd Zd Z xZS )
LazyLoaderzFLazily import a module, mainly to avoid pulling in large dependencies.c                 @    || _         || _        t        |   |       y r   )_local_name_parent_module_globalssuperrg   )rf   
local_nameparent_module_globalsr   	__class__s       r   rg   zLazyLoader.__init__  s     %&;#r%   c                     t        j                  | j                        }|| j                  | j                  <   | j
                  j                  |j
                         |S )z8Load the module and insert it into the parent's globals.)	importlibimport_moduler   r   r   __dict__r   )rf   r   s     r   _loadzLazyLoader._load  sK     ((78>##D$4$45 	V__-r%   c                 :    | j                         }t        ||      S r   )r   r   )rf   itemr   s      r   __getattr__zLazyLoader.__getattr__*  s    vt$$r%   )r   r   r   r   rg   r   r  __classcell__)r   s   @r   r   r     s    P
	%r%   r   )NNN)Fr   )z Keyword argument not understood:)&r   r6   r   r   r   r   r   rW   r`   typesr:   numpyr   tensorflow.compat.v2compatv2r   tf_keras.src.utilsr   r    tensorflow.python.util.tf_exportr   r$   r>   rG   rI   r   r   r   r   r   r   r   r   r   r   
ModuleTyper   r)   r%   r   <module>r     s    -     	 	 
    ! ! ' ) :#..b@$ #$FN FN %FNR"1h" 
 +M21
(%(( %r%   