File size: 2,573 Bytes
ab621ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The G1 evaluation adapters of the baselines (evaluation/README.md#the-model-interface): `make_model('fm_dit')`
returns the `Model` class that `evaluation/policy/FMDiT/model.py` exports (likewise DP, ACT).

Observation: the current ego_view and chest frames (480x640 RGB, resized as the training cache) and state44; no past
actions or earlier frames (the baselines read one frame). Output: the checkpoint's whole chunk (16 rows of action76,
row 0 executed at the request's frame), `execution_steps` rows of it executed before the next query (deploy.yml
`config: execution_steps`, default the whole chunk), the request's seed for the sampling noise (FM-DiT, DP).
"""
from __future__ import annotations

from .runtime import BaselinePolicy

WIDTH = {'sonic_dfx6': 76}


def require(condition, message):
    if not condition:
        raise RuntimeError(message)


def make_model(policy_name: str):
    class Model:
        history_length = 1  # the current head and chest frames only
        video_frames = 0

        def __init__(self, cfg):
            self.policy = BaselinePolicy(cfg['checkpoint_path'], device=cfg.get('device', 'cuda:0'),
                                         policy=policy_name, robot='g1')
            contract = self.policy.contract
            require(cfg['action_space'] in WIDTH and contract['action_space'] == cfg['action_space']
                    and self.policy.action_dim == WIDTH[cfg['action_space']] and self.policy.state_dim == 44
                    and list(self.policy.cameras) == ['ego_view', 'chest'],
                    f"checkpoint contract {contract['name']} does not serve {cfg['action_space']}")
            extra = cfg.get('config') or {}
            if extra.get('check_task', True):
                require(cfg['task'].strip() == (self.policy.task_text or '').strip(),
                        f'task text {cfg["task"]!r} is not the training task {self.policy.task_text!r}')
            self.execution_steps = int(extra.get('execution_steps') or self.policy.horizon)
            require(1 <= self.execution_steps <= self.policy.horizon, f'execution_steps {self.execution_steps}')
            self.info = dict(self.policy.info(), execution_steps=self.execution_steps)

        def get_action(self, obs):
            action, _ = self.policy.predict(obs['images'], obs['state'], seed=obs['seed'])
            return dict(action=action, execution_steps=self.execution_steps,
                        sampling_steps=self.policy.sampling_steps)

    Model.__name__ = Model.__qualname__ = f'Model_{policy_name}'
    return Model