
    ij                     T    d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 d Zd Zd
d	Zy)z'Orbax checkpoint loading functionality.    N)backend)distribution)
file_utils)ocpc                     t        j                         rt        j                         sy	 t        j                         }h d}|j	                  |      syt         fd|D              S # t        t        f$ r Y yw xY w)zCheck if the given path is an Orbax checkpoint directory.

    This function implements custom detection logic instead of relying on
    Orbax APIs which may be unreliable in some environments.
    F>   orbax.checkpointpytree.orbax-checkpointcheckpoint_metadataTc              3      K   | ]E  }d |v xs; |j                         xr) t        j                  t        j                  |             G yw)z.orbax-checkpoint-tmpN)isdigitr   isdirjoin).0itemfilepaths     p/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/saving/orbax_util.py	<genexpr>z&is_orbax_checkpoint.<locals>.<genexpr>$   sR      
  $t+  F$$Z__Xt%DE
s   AA)r   existsr   listdir
isdisjointanyOSErrorPermissionError)r   contentsorbax_indicatorss   `  r   is_orbax_checkpointr      s     X&j.>.>x.H%%h/
  **84  
 !
 
 	
 _% s   *A- A- -A?>A?c                     t         j                  j                  |       }|j                  }|t	        d|        t
        j                  j                  | t        |j                              S )z<Find the latest checkpoint in an Orbax checkpoint directory.)	directoryzNo valid checkpoints found in )
r   trainingCheckpointerlatest
ValueErrorospathr   strstep)checkpoint_dircheckpointerr!   s      r   find_latest_orbax_checkpointr)   2   s[    <<,,~,FL  F~9.9IJKK77<<FKK(899    c                 ,   t        j                          dk7  ryt               yddlt        j                  | d      j
                  }dfd	|j                         D ci c]"  \  }}| ||xs i j                  |            $ c}}S c c}}w )u  Build an abstract pytree for Orbax loading with target shardings.

    On JAX with an active distribution, returns a pytree of
    `jax.ShapeDtypeStruct` so that Orbax reshards arrays onto the
    current distribution layout instead of restoring saved shardings.
    On all other backends, or when no distribution is active, returns
    `None` (Orbax will use saved shardings — fine when the topology
    hasn't changed).

    Args:
        checkpoint_path: Path to a specific Orbax checkpoint step
            directory (e.g. `<root>/2`).
        ref_state: Optional reference state tree (from
            `model.get_state_tree()`) whose variables carry the
            target shardings. If `None`, shardings default to
            `None` per leaf (Orbax uses saved shardings).

    Returns:
        A pytree of `jax.ShapeDtypeStruct` matching the checkpoint
        structure, or `None` when resharding is not needed.
    jaxNr   pytree)checkpointable_namec                 h   t        | d      rAt        | d      r5t        |dd      }j                  | j                  | j                  |      S t        | t              rNt        |t              r|ni }| j                         D ci c]  \  }}| ||j                  |              c}}S yc c}}w )z>Convert metadata leaf to `jax.ShapeDtypeStruct` with sharding.shapedtypeshardingN)r2   )	hasattrgetattrShapeDtypeStructr0   r1   
isinstancedictitemsget)metarefr2   rkv_to_abstractr,   s         r   r?   z1build_orbax_abstract_pytree.<locals>._to_abstract]   s    4!gdG&<sJ5H''

DJJ (   dD!!#t,"A=AZZ\JTQA|AquuQx00JJ Ks   #B.N)r   get_distributionr,   r   pytree_metadatametadatar8   r9   )checkpoint_path	ref_statepytree_metakeyvalr?   r,   s        @@r   build_orbax_abstract_pytreerI   ;   s    , E!!%%Xh 
 $))+C 	\#	R44S9::  s   %'Br@   )__doc__r#   	keras.srcr   keras.src.distributionr   rA   keras.src.utilsr   keras.src.utils.module_utilsr   r   r)   rI    r*   r   <module>rP      s'    - 	  C & ,$N:1r*   