File size: 4,592 Bytes
0f5d220
 
 
 
 
 
 
 
 
c2b53b9
0f5d220
 
 
 
 
 
 
 
 
 
 
 
3e119c8
 
 
0f5d220
 
c2b53b9
 
0f5d220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e119c8
 
 
 
 
 
0f5d220
c2b53b9
 
0f5d220
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Use pinned OpenPI conversion with a strict, recorded key audit.

No missing inference weight is accepted. The expert's unused vocabulary head
is zero-filled explicitly; tied aliases are populated from their source weight.
"""
import argparse,hashlib,json,pathlib,sys,subprocess,dataclasses

def main():
    p=argparse.ArgumentParser();p.add_argument('--openpi-source',required=True);p.add_argument('--checkpoint-dir',required=True);p.add_argument('--output',required=True)
    p.add_argument('--model-profile',choices=('pi05_base_libero_protocol','legacy_pi05_libero'),default='pi05_base_libero_protocol')
    args=p.parse_args();source=pathlib.Path(args.openpi_source).resolve()
    revision=subprocess.check_output(['git','-C',str(source),'rev-parse','HEAD'],text=True).strip()
    if revision!='215abfb217dbac7d5f1273282331b9b1866c0479':raise ValueError('Unvalidated OpenPI revision')
    subprocess.run(['git','-C',str(source),'diff','--quiet'],check=True)
    sys.path[:0]=[str(source/'src'),str(source/'packages/openpi-client/src')]
    import torch
    from openpi.models.pi0_config import Pi0Config
    script=source/'examples/convert_jax_model_to_pytorch.py'
    text=script.read_text().replace('import openpi.training.config as _config','')
    needle='    pi0_model.load_state_dict(all_params, strict=False)'
    if text.count(needle)!=1:raise ValueError('Pinned converter changed')
    text=text.replace(needle,'    audit_and_load(pi0_model, all_params)')
    save_needle='    safetensors.torch.save_model(pi0_model,'
    if text.count(save_needle)!=1:raise ValueError('Pinned converter save changed')
    text=text.replace(save_needle,'    apply_runtime_precision(pi0_model)\n'+save_needle)
    out=pathlib.Path(args.output);out.mkdir(parents=True,exist_ok=True)
    if (out/'model.safetensors').exists():raise FileExistsError('Converted output already exists')
    checkpoint_name='pi05_libero' if args.model_profile=='legacy_pi05_libero' else 'pi05_base'
    audit={'source_revision':revision,'saved_dtype':'float32_before_runtime_mixed_cast','source_url':'gs://openpi-assets/checkpoints/'+checkpoint_name,'model_profile':args.model_profile,'converter_sha256':hashlib.sha256(script.read_bytes()).hexdigest(),'tied_aliases':{},'unused_zero_filled':[]}
    def load(model,state):
        # Preserve original FP32 tensors before the runtime mixed-precision cast.
        model.to(torch.float32)
        expected=model.state_dict();groups={}
        for n,v in expected.items():groups.setdefault(v.data_ptr(),[]).append(n)
        for names in groups.values():
            supplied=[n for n in names if n in state]
            if supplied:
                for name in names:
                    if name not in state:
                        state[name]=state[supplied[0]];audit['tied_aliases'][name]=supplied[0]
        missing=set(expected)-set(state);unexpected=set(state)-set(expected)
        unused={'paligemma_with_expert.gemma_expert.lm_head.weight'}
        audit.update(missing_before_unused_fill=sorted(missing),unexpected=sorted(unexpected))
        (out/'loading-audit.json').write_text(json.dumps(audit,indent=2))
        if unexpected or missing-unused:raise ValueError('Conversion has missing/unexpected inference weights: '+str((missing,unexpected)))
        for n in missing:
            state[n]=torch.zeros_like(expected[n]);audit['unused_zero_filled'].append(n)
        model.load_state_dict(state,strict=True)
        audit.update(strict_load=True,tensor_count=len(state),finite=all(bool(torch.isfinite(v).all()) for v in state.values()))
        if not audit['finite']:raise ValueError('Nonfinite converted weights')
        (out/'loading-audit.json').write_text(json.dumps(audit,indent=2))
    def runtime_precision(model):
        if args.model_profile=='legacy_pi05_libero':
            model.paligemma_with_expert.to_bfloat16_for_selected_params('bfloat16')
            audit['saved_dtype']='runtime_mixed_bfloat16_float32'
            (out/'loading-audit.json').write_text(json.dumps(audit,indent=2))
    ns={'__name__':'pinned_openpi_conversion','audit_and_load':load,'apply_runtime_precision':runtime_precision}
    exec(compile(text,str(script),'exec'),ns)
    legacy=args.model_profile=='legacy_pi05_libero'
    config=Pi0Config(pi05=True,action_horizon=10,max_token_len=200 if legacy else 256,discrete_state_input=not legacy,pytorch_compile_mode=None)
    audit['architecture']=dataclasses.asdict(config)
    ns['convert_pi0_checkpoint'](str(pathlib.Path(args.checkpoint_dir).resolve()),'float32',str(out),config)

if __name__=='__main__':main()