
    qi                        d dl Z d dlZd dlZd dlmZmZ d dlZd dlZ	d dl
mc mZ d dlmc mZ d dlmc mZ  ej        e          Ze G d de                      Z e j        d           G d de                      Z e j        d           G d	 d
e                      Z e j        d           G d de                      Zdej        dej        dedej        fdZdS )    N)Protocolruntime_checkablec                   2    e Zd Zdej        dej        fdZdS )WeightLoaderparamsreturnc                     dS )a  Loads the model weights.

        Args:
            params: Parameters of the model. This is a nested structure of array-like objects that
                represent the model's parameters.

        Returns:
            Loaded parameters. The structure must be identical to `params`. If returning a subset of
            the parameters the loader must merge the loaded parameters with `params`.
        N selfr   s     ?/home/robot-lab/Pi0.5_yam/src/openpi/training/weight_loaders.pyloadzWeightLoader.load   s          N__name__
__module____qualname__atParamsr   r
   r   r   r   r      s:        
29 
 
 
 
 
 
 
r   r   T)frozenc                   2    e Zd Zdej        dej        fdZdS )NoOpWeightLoaderr   r   c                     |S )Nr
   r   s     r   r   zNoOpWeightLoader.load!   s    r   Nr   r
   r   r   r   r      s:        29       r   r   c                   B    e Zd ZU dZeed<   dej        dej        fdZdS )CheckpointWeightLoadera  Loads an entire set of weights from a checkpoint.

    Compatible with:
      trained checkpoints:
        example: "./checkpoints/<config>/<exp>/<step>/params"
      released checkpoints:
        example: "gs://openpi-assets/checkpoints/<model>/params"
    params_pathr   r   c                     t          j        t          j        | j                  t
          j                  }t          ||d          S )N)restore_typez.*lora.*missing_regex)_modelrestore_paramsdownloadmaybe_downloadr   npndarray_merge_params)r   r   loaded_paramss      r   r   zCheckpointWeightLoader.load2   s>    -h.EdFV.W.Wfhfpqqq]F*MMMMr   N)	r   r   r   __doc__str__annotations__r   r   r   r
   r   r   r   r   %   s\           N29 N N N N N N Nr   r   c                   6    e Zd ZdZdej        dej        fdZdS )PaliGemmaWeightLoaderzLoads weights from the official PaliGemma checkpoint.

    This will overwrite existing weights with similar names while keeping all extra weights intact.
    This allows us to support the action expert which is used by the Pi0 model.
    r   r   c                 H   t          j        dddi          }|                    d          5 }t          t	          j        |d                    }d d d            n# 1 swxY w Y   dt          j                            |d	
          d         i}t          ||d          S )Nz:gs://vertex-model-garden-paligemma-us/paligemma/pt_224.npztokenanon)gsrbF)allow_pickle	PaliGemma/sepr   z.*r   )
r#   r$   opendictr%   r   flaxtraverse_utilunflatten_dictr'   )r   r   pathfflat_paramsr(   s         r   r   zPaliGemmaWeightLoader.loadA   s    &HgW]M^
 
 
 YYt__ 	?rwqu===>>K	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?$d&8&G&GY\&G&]&]^f&gh]F$GGGGs   $AA"%A"N)r   r   r   r)   r   r   r   r
   r   r   r-   r-   9   sL         H29 H H H H H H Hr   r-   r(   r   r    r   c                   t           j                            |d          }t           j                            | d          }i }|                                D ]D\  }}||v r;|j        ||         j        k    r |                    ||         j                  n|||<   E|                                 t          j        |          fd|D             D ]}||vr||         ||<   t           j        	                    |d          S )aY  Merges the loaded parameters with the reference parameters.

    Args:
        loaded_params: The parameters to merge.
        params: The reference parameters.
        missing_regex: A regex pattern for all missing keys that should be merged from the reference parameters.

    Returns:
        A new dictionary with the merged parameters.
    r5   r6   c                 >    h | ]}                     |          |S r
   )	fullmatch).0kpatterns     r   	<setcomp>z _merge_params.<locals>.<setcomp>d   s,    :::AW%6%6q%9%9:a:::r   )
r:   r;   flatten_dictitemsdtypeastypeclearrecompiler<   )	r(   r   r    flat_refflat_loadedresultrD   vrE   s	           @r   r'   r'   L   s    !..v3.??H$11-S1IIK F!!## [ [1==78w(1+BS7S7S!!2333YZF1I j''G::::::: $ $F?? F1I,,V,===r   )dataclassesloggingrL   typingr   r   flax.traverse_utilr:   numpyr%   openpi.models.modelmodelsmodelr!   openpi.shared.array_typingsharedarray_typingr   openpi.shared.downloadr#   	getLoggerr   loggerr   	dataclassr   r   r-   r   r*   r'   r
   r   r   <module>ra      s        				 . . . . . . . .         $ $ $ $ $ $ $ $ $ ' ' ' ' ' ' ' ' ' ) ) ) ) ) ) ) ) )		8	$	$     8    d###    |   $#
 d###N N N N N\ N N $#N& d###H H H H HL H H $#H$> >BI >QT >Y[Yb > > > > > >r   