
    ބi                         d Z ddlZddlmZ ddlmZ ddlZddlZddlm	Z	 ddl
mZ ddlmZ ded	efd
Zddedej        defdZdS )z
Load a UVA checkpoint into policy (MAR + VAE) for video-only sampling.
Self-contained: no imports from unified_video_action.
Uses the simplified video-only MAR (no text, proprio, action, wrist).
    N)Path)SimpleNamespace)	OmegaConf)AutoencoderKL)mar_base_video_only	repo_rootreturnc                     t          |          }t          j                            |          r|S t          | |z            S N)strospathisabs)r   raw_pathps      >/data/cameron/vidgen/unified_video_action/simple_uva/loader.py_resolve_pathr      s:    HA	w}}Q y1}    	ckpt_pathdevicec                    |p0t          t                                                    j        d         }t	          | d          5 }t          j        ||t                    }ddd           n# 1 swxY w Y   |d         }|d                             d          p|d                             d          }|t          d	          |j
        j        }t          |j        d
d          }|rt          ||          nd|j        j        d}	t          j                    5  t#          di |	}
ddd           n# 1 swxY w Y   |
                                 |
                                D ]	}d|_        
|j        }t-          |j        |j        |j        |j        |j        |j        |j        |j        |j        |j         tC          |j"                  |j#                  }d |$                                D             }d |$                                D             }|%                    |d           |
%                    |d           tM                      }||_
        |
|_'        |j
        (                    |           |j'        (                    |           |j
                                         |j'                                         ||fS )zFLoad policy and cfg from a full UVA checkpoint. Returns (policy, cfg).   rb)map_locationpickle_moduleNcfgstate_dicts	ema_modelmodelzHCheckpoint must contain state_dicts['ema_model'] or state_dicts['model']autoencoder_path)r    ddconfigF)img_size
vae_stride
patch_sizevae_embed_dimmask_ratio_minlabel_drop_probattn_dropoutproj_dropout
diffloss_d
diffloss_wnum_sampling_stepsgrad_checkpointingc                 T    i | ]%\  }}|                     d           |dd         |&S )zmodel.   N
startswith.0kvs      r   
<dictcomp>z/load_policy_from_checkpoint.<locals>.<dictcomp>G   s7    RRRTQ1<<;Q;QR!""qRRRr   c                 T    i | ]%\  }}|                     d           |dd         |&S )z
vae_model.
   Nr0   r2   s      r   r6   z/load_policy_from_checkpoint.<locals>.<dictcomp>H   s7    UUUDAq!,,|:T:TUafaUUUr   )strict ))r   __file__resolveparentsopentorchloaddillgetKeyErrorr   policygetattrvae_model_paramsr   r!   no_gradr   eval
parametersrequires_gradautoregressive_model_paramsr   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r   r,   r-   itemsload_state_dictr   	vae_modelto)r   r   r   fpayloadr   
state_dict
policy_cfgae_path
vae_paramsrN   r   	ar_params	mar_modelmodel_sdvae_sdrD   s                    r   load_policy_from_checkpointrZ      sO   @T(^^3355=a@I	i		 I!*QV4HHHI I I I I I I I I I I I I I I %.C'++K88_GM<R<V<VW^<_<_Jabbb !Jj13EtLLGAHRM)W===d/8 J 
 0 0!//J//	0 0 0 0 0 0 0 0 0 0 0 0 0 0 0NN!!##     6I##''- /!1++''y;<<$7  I  SRZ%5%5%7%7RRRHUUJ$4$4$6$6UUUFhu555fU333FFL F
LOOF

L
3;s$   A--A14A1D66D:=D:r   )__doc__r   pathlibr   typesr   rA   r?   	omegaconfr   simple_uva.vaer   simple_uva.modelr   r   r   r   rZ   r:   r   r   <module>ra      s     
			       ! ! ! ! ! !         ( ( ( ( ( ( 0 0 0 0 0 0T     ; ;4 ; ;RV ; ; ; ; ; ;r   