Heterogenous Pre-trained Transformers (HPT)

repository·main·Indexed 19 days ago

https://github.com/liruiw/hpt

A PyTorch implementation for pre-training transformers on a mixture of embodiment datasets to align different robot embodiments into a shared latent space. HPT enables scalable policy learning and provides several pre-trained model versions (Small, Base, Large, XLarge) available via Hugging Face. The library includes tools for training policies using hpt.run, evaluating models via run_eval.py, and integrating with MuJoCo simulations through dm_control.

Tokens
4.6K
Snippets
16
Records
20
Agent score
69%

What's inside hpt

  1. Train on a custom dataset

    main

    To use your own dataset for training, follow these requirements:

    1. Dataset Conversion: Implement a convert_dataset function to pack your data. See env/realworld for an example implementation.
    2. Disk Usage: Add dataset.use_disk=True to your configuration to save and load the dataset from disk.
    3. Model Customization: If you need to change perception stem networks or action head networks, modify the configuration files in experiments/configs/. Refer to experiments/configs/env/realrobot_image.yaml for a real-world example.

    For evaluation of custom datasets, you must provide:

    1. A rollout_runner.py file specific to your benchmark.
    2. A learner_trajectory_generator evaluation function that provides rollouts.
  2. Train policies using hpt.run

    main

    To train policies on specific environments, use the hpt.run module. You can append +mode=debug to the command for debugging purposes.

    To load a pre-trained trunk transformer, modify the train.pretrained_dir configuration key. This can point to a local checkpoint folder or a Hugging Face repository (e.g., hf://liruiw/hpt-xlarge).

    python -m hpt.run
  3. Configure Mujoco environment variables

    main

    If installing an older version of Mujoco, add these lines to your ~/.bashrc to ensure the libraries and EGL are correctly located:

    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${HOME}/.mujoco/mujoco210/bin
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/nvidia
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64
    export MUJOCO_GL=egl
  4. Install Heterogenous Pre-trained Transformers (HPT)

    main

    Install the HPT package in editable mode using pip:

    pip install -e .

    If you are using an older version of Mujoco, you may need to perform the following manual setup:

    1. Create a .mujoco directory in your home folder.
    2. Download and extract mujoco210-linux-x86_64.tar.gz into that directory.
    3. Update your ~/.bashrc with the necessary LD_LIBRARY_PATH and MUJOCO_GL environment variables.
  5. Import dm_control components for MuJoCo simulation

    main

    To use dm_control for MuJoCo simulations, you need to import the core wrapper, MJCF (MuJoCo XML) tools, and specific task suites.

    Key modules include:

    • dm_control.mujoco: The primary wrapper for physics simulation.
    • dm_control.mujoco.wrapper.mjbindings: Provides access to enums and mjlib functions.
    • dm_control.mjcf: Tools for manipulating MuJoCo XML models.
    • dm_control.composer: High-level tools for composing environments and variations.
    • dm_control.suite: Pre-defined control suites.
    from dm_control import mujoco
    from dm_control.mujoco.wrapper.mjbindings import enums, mjlib
    from dm_control import mjcf
    from dm_control import composer
    from dm_control import suite
  6. Set up HPT Config and Models

    main

    To initialize a policy and its environment-specific components, follow these steps:

    1. Load a Pretrained Policy: Use Policy.from_pretrained with a Hugging Face path (e.g., hf://liruiw/hpt-base).
    2. Initialize Configuration: Use hydra.initialize and hydra.compose to load configurations from the experiments/configs directory. You can override the env parameter to specify the domain (e.g., mujoco_metaworld).
    3. Instantiate Dataset: Use hydra.utils.instantiate on the cfg.dataset object, providing the dataset_name, env_rollout_fn, and unpacking **cfg.dataset.
    4. Configure Model Architecture:
      • Use utils.update_network_dim(cfg, dataset, policy) to align dimensions.
      • Call policy.init_domain_stem(domain, cfg.stem) to initialize the stem.
      • Call policy.init_domain_head(domain, normalizer, cfg.head) to initialize the head using the dataset's normalizer.
      • Call policy.finalize_modules() to complete the setup.
    5. Move to Device: Use policy.to(device) to move the model to a GPU or CPU.
    from hydra import compose, initialize
    from hpt.models.policy import Policy
    from hpt.utils import utils
    
    # 1. Load policy
    policy = Policy.from_pretrained("hf://liruiw/hpt-base")
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    domain = "mujoco_metaworld"
    
    # 2. Setup Config
    with initialize(version_base="1.2", config_path="experiments/configs"):
        cfg = compose(config_name="config", overrides=[f"env={domain}"])
    
    # 3. Setup Dataset
    cfg.dataset.episode_cnt = 10
    dataset = hydra.utils.instantiate(
        cfg.dataset, 
        dataset_name=domain, 
        env_rollout_fn=cfg.dataset_generator_func, 
        **cfg.dataset
    )
    normalizer = dataset.get_normalizer()
    
    # 4. Setup Model
    utils.update_network_dim(cfg, dataset, policy)
    policy.init_domain_stem(domain, cfg.stem)
    policy.init_domain_head(domain, normalizer, cfg.head)
    policy.finalize_modules()
    policy.to(device)
  7. Run policy rollout in an environment

    main

    To run a policy in a simulation environment (rollout):

    1. Initialize Environment: Instantiate the environment from the ALL_ENVS registry.
    2. Prepare Observation: Create an OrderedDict containing the required observation keys (e.g., state and image).
    3. Reset Policy: Call policy.reset() before starting the loop.
    4. Step Loop: In each step of env.max_path_length:
      • Get action: a = policy.get_action(step_data).
      • Step environment: o, r, done, info = env.step(a).
      • Update observation: Render the new image and update step_data.
    from collections import OrderedDict
    from env.mujoco.metaworld.envs.mujoco.sawyer_xyz.test_scripted_policies import ALL_ENVS
    
    # Setup
    env = ALL_ENVS["reach-v2"]()
    env._partially_observable = False
    env._freeze_rand_vec = False
    env._set_task_called = True
    
    # Initial observation
    img = env.sim.render(128, 128, mode="offscreen", camera_name="view_1")[:, :, ::-1].copy()
    o = env.reset()
    step_data = OrderedDict({"state": o, "image": img})
    
    policy.reset()
    
    # Rollout loop
    for _ in range(env.max_path_length):
        a = policy.get_action(step_data)
        o, r, done, info = env.step(a)
        img = env.sim.render(128, 128, mode="offscreen", camera_name="view_1")[:, :, ::-1]
        step_data = OrderedDict({"state": o, "image": img})
        if done:
            break
  8. Run Metaworld experiments

    main

    To run experiments for Metaworld, use the provided shell scripts. For example, to run a 20-task fine-tuning experiment using the HPT-Base model from Hugging Face:

    bash experiments/scripts/metaworld/train_test_metaworld_20task_finetune.sh hf://liruiw/hpt-base
  9. Configure training via Hydra

    main

    The training process is controlled via a Hydra configuration file (defaulting to config.yaml in ../experiments/configs). Key configuration sections include:

    • train: Controls pretraining paths (pretrained_dir), total iterations (total_iters), and whether to freeze the trunk (freeze_trunk).
    • network: Defines the model architecture and whether to finetune encoders (finetune_encoder).
    • dataset: Configuration for the dataset instantiation, including dataset_name and env_rollout_fn.
    • dataloader / val_dataloader: Standard PyTorch DataLoader parameters.
    • optimizer / optimizer_misc: Parameters for the optimizer.
    • lr_scheduler: Parameters for the learning rate scheduler.
    • warmup_lr: Configuration for the linear warmup steps (step).
    • domains: A comma-separated string of domains used to select the active domain.
    • output_dir: The directory where logs and the final model.pth are saved.
  10. Configure evaluation via Hydra

    main

    The evaluation script relies on a Hydra configuration hierarchy. Key configuration keys required by run_eval.py include:

    • seed: Integer used for reproducibility.
    • output_dir: Directory where results are saved (appended with the seed).
    • domains: A comma-separated string of domain names (the first domain in the list is used for initialization).
    • network: A configuration object that can be instantiated via hydra.utils.instantiate. It must support:
      • finetune_encoder: Boolean flag.
      • stem: Configuration for the domain stem.
      • head: Configuration for the domain head.
    • dataset.image_encoder: Configuration for the image encoder.
    • train.pretrained_dir: The directory containing the model.pth file.

    Example configuration structure (conceptual):

    seed: 42
    output_dir: "./results"
    domains: "domain1,domain2"
    network:
      _target_: hpt.networks.YourPolicyClass
      finetune_encoder: true
      stem: { ... }
      head: { ... }
    train:
      pretrained_dir: "/path/to/checkpoints"
    dataset:
      image_encoder: { ... }
  11. Reference: HPT Model Checkpoints

    main

    HPT provides several pre-trained model versions available on Hugging Face:

    ModelSize
    HPT-XLarge226.8M Params
    HPT-Large50.5M Params
    HPT-Base12.6M Params
    HPT-Small3.1M Params
    HPT-Base (With Language)50.6M Params