
    qi                     <   d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	c m
Z d dlmc mZ d dlmc mZ d dlmZ d dlmZ d dlmZ dddddddej        dej        ez  dej        dz  d	eeef         dz  d
edz  deeej        f         dz  dedz  dej         fdZ!dS )    N)Any)checkpoints)config)repack_transformssample_kwargsdefault_prompt
norm_statspytorch_devicetrain_configcheckpoint_dirr   r   r   r	   r
   returnc          	      x   |pt          j                    }t          j        t	          |                    }t
          j                            |d          }t
          j                            |          }t          j
        d           |r6| j                            | |          }	|	j                            d           n;| j                            t!          j        |dz  t$          j                            }	| j                            | j        | j                  }
|3|
j        t1          d          t3          j        |dz  |
j                  }|r7|5	 d	dl}|j                                        rd
nd}n# t<          $ r d}Y nw xY wt?          j         |	g |j!        t          j"        |          |
j#        j!        t          j$        ||
j%                  |
j&        j!        g |
j&        j'        t          j(        ||
j%                  |
j#        j'        |j'        || j)        ||r|nd          S )a=  Create a policy from a trained checkpoint.

    Args:
        train_config: The training config to use to create the model.
        checkpoint_dir: The directory to load the model from.
        repack_transforms: Optional transforms that will be applied before any other transforms.
        sample_kwargs: The kwargs to pass to the `sample_actions` method. If not provided, the default
            kwargs will be used.
        default_prompt: The default prompt to use for the policy. Will inject the prompt into the input
            data if it doesn't already exist.
        norm_stats: The norm stats to use for the policy. If not provided, the norm stats will be loaded
            from the checkpoint directory.
        pytorch_device: Device to use for PyTorch models (e.g., "cpu", "cuda", "cuda:0").
                      If None and is_pytorch=True, will use "cuda" if available, otherwise "cpu".

    Note:
        The function automatically detects whether the model is PyTorch-based by checking for the
        presence of "model.safensors" in the checkpoint directory.
    zmodel.safetensorszLoading model...bfloat16params)dtypeNz(Asset id is required to load norm stats.assetsr   cudacpu)use_quantiles)
transformsoutput_transformsr   metadata
is_pytorchr
   )*r   Groupdownloadmaybe_downloadstrospathjoinexistslogginginfomodelload_pytorchpaligemma_with_expertto_bfloat16_for_selected_paramsload_modelrestore_paramsjnpr   datacreateassets_dirsasset_id
ValueError_checkpointsload_norm_statstorchr   is_availableImportError_policyPolicyinputsInjectDefaultPromptdata_transforms	Normalizeuse_quantile_normmodel_transformsoutputsUnnormalizepolicy_metadata)r   r   r   r   r   r	   r
   weight_pathr   r$   data_configr3   s               >/home/robot-lab/Pi0.5_yam/src/openpi/policies/policy_config.pycreate_trained_policyrD      sp   : *?Z-=-?-?,S-@-@AAN ',,~/BCCK,,JL#$$$ n"//kJJ#CCJOOOO"''(=nx>W_b_k(l(l(lmm#**<+C\EWXXK 'GHHH!1.82K[Mabb
  #n,	#LLL',z'>'>'@'@KVVeNN 	# 	# 	#"NNN	# >
%
*>::
 (/
  ;;XYYY	

 )0

)1
":[=Z[[[
 (0
 &	
 $-)3=~~%   s   !E> >FF)"r"   r   pathlibtypingr   	jax.numpynumpyr+   openpi.models.modelmodelsr$   r)   openpi.policies.policypoliciespolicyr6   openpi.shared.downloadsharedr   openpi.trainingr   r1   r   _configopenpi.transformsr   TrainConfigPathr   r   dict	NormStatsr7   rD        rC   <module>rY      s    				              $ $ $ $ $ $ $ $ $ ( ( ( ( ( ( ( ( ( ) ) ) ) ) ) ) ) ) 7 7 7 7 7 7 - - - - - - & & & & & & 26+/!%9=!%N N N%NL3&N "'$.	N
 S>D(N $JN S*../$6N $JN ^N N N N N NrX   