ReinFlow: Fine-tuning Flow Matching Policies

repository·release·Indexed 43 days ago

https://github.com/reinflow/reinflow

A policy gradient framework for fine-tuning flow matching policies using online reinforcement learning, optimized for robotic tasks and large-scale Vision-Language-Action (VLA) models. ReinFlow supports 1-Rectified Flow, Shortcut Models, and ODE-defined policies, featuring a noise injection network to make policy probabilities tractable across various denoising steps. It is compatible with OpenAI Gym, Robomimic, LIBERO, ManiSkill, and MetaWorld domains.

Tokens
10.8K
Snippets
36
Records
52
Agent score
57%

What's inside ReinFlow

  1. Overview of ReinFlow

    release

    ReinFlow is a policy gradient framework designed for fine-tuning flow matching policies using online reinforcement learning.

    Workflow

    1. Imitation Learning: Train flow policies using behavior cloning (SFT).
    2. Reinforcement Learning: Fine-tune the trained policies using ReinFlow.

    Key Features

    • Flexible Denoising: Supports fine-tuning at any denoising step.
    • Model Support: Compatible with 1-Rectified Flow, Shortcut Models, and any policy defined by ODEs.
    • Noise Injection Network: Trains a noise injection network end-to-end, making policy probabilities tractable even with very few denoising steps (e.g., 1, 2, or 4 steps).
    • Scalability: Capable of scaling to large Vision-Language-Action (VLA) models (up to 3B parameters) via the RLinf infrastructure.

    Supported Task Domains

    • Legged Locomotion (OpenAI Gym)
    • State-based manipulation (Franka Kitchen)
    • Visual manipulation (Robomimic, LIBERO, ManiSkill, MetaWorld)
  2. Manage WandB logging

    release

    ReinFlow supports Weights & Biases (WandB) for experiment tracking.

    Key Features:

    • Offline Mode: Use wandb.offline_mode=True to log locally without internet. Sync later using wandb sync <directory>.
    • Disable Logging: Set wandb=null to skip WandB creation and save memory.
    • Data Recovery: If wandb=null was used, training records are still saved as .pkl files. You can recover them using util/pkl2wandb.py.
    # Sync offline WandB runs in a batch
    for dir in ./wandb_offline/wandb/offline-run-20250510*; do wandb sync "${dir}"; done
  3. Integrate custom environments

    release

    ReinFlow interacts with environments using the OpenAI Gym format. To add a new environment, ensure it is compatible with the following patterns:

    • Vectorization: Environments are initialized as vectorized environments via make_async (found in env/gym_utils/__init__.py).
    • History & Multi-step: The multi_step wrapper (env/gym_utils/wrapper/multi_step.py) is used to handle history observations and multi-environment-step action execution.
    • Normalization: Use environment-specific wrappers (like robomimic_lowdim or furniture) to handle observation and action normalization. You can implement a new wrapper to support your specific environment's requirements.
  4. Install IsaacGym

    release

    IsaacGym is a required dependency. You can download the installer from the NVIDIA website or use the provided AWS S3 link. After downloading, unzip the package and install it in editable mode using pip.

    Note: It is recommended to use the --no-cache-dir and --force-reinstall flags during installation to avoid common issues. Do not upgrade pip if prompted, as the current version is required for codebase compatibility.

    # Download the package
    cd ~
    wget https://iai-robust-rearrangement.s3.us-east-2.amazonaws.com/IsaacGym_Preview_4_Package.tar.gz
    
    # Unzip the package
    tar -xzf IsaacGym_Preview_4_Package.tar.gz
    
    # Install the package
    cd isaacgym
    pip install -e python --no-cache-dir --force-reinstall
  5. Prepare Franka Kitchen or Robomimic datasets

    release

    For Franka Kitchen and Robomimic datasets, the pre-training commands handle downloading and normalization automatically.

    • Franka Kitchen: Uses D4RL datasets (complete, mixed, or partial observations) via DPPO's approach.
    • Robomimic: Uses DPPO's simplified pixel datasets. Note that these are smaller and simpler than official Robomimic data; using official data may require larger models and more training time but can improve success rates.
  6. Install MuJoCo 2.1.0 on Linux

    release

    To use MuJoCo with ReinFlow, you must install the MuJoCo 2.1.0 binaries and configure your environment variables. This involves downloading the tarball, extracting it to ~/.mujoco, and updating your ~/.bashrc to include the MuJoCo binary directory in your LD_LIBRARY_PATH and PATH.

    cd ~
    wget https://mujoco.org/download/mujoco210-linux-x86_64.tar.gz
    mkdir $HOME/.mujoco
    tar -xvf mujoco210-linux-x86_64.tar.gz -C ~/.mujoco/
    echo -e 'export LD_LIBRARY_PATH=$HOME/.mujoco/mujoco210/bin' >> ~/.bashrc
    echo -e 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/nvidia' >> ~/.bashrc
    echo -e 'export PATH="$LD_LIBRARY_PATH:$PATH"' >> ~/.bashrc
  7. Use DPPO's version of OpenAI Gym datasets (Deprecated)

    release

    DPPO's version of the datasets is older and less recommended. It downloads automatically when use_d4rl_dataset=False. Use this only if you need to match exact results from DPPO research. If you use both D4RL and DPPO datasets, store them in separate directories and manually update train_dataset_path and normalization_path in your execution commands.

    python script/run.py --config-dir=cfg/gym/pretrain/walker2d-medium-v2 --config-name=pre_reflow_mlp device=cuda:0 sim_device=cuda:0
  8. Install Furniture-Bench

    release

    Furniture-Bench is a required dependency. Install it by cloning the repository (via SSH or HTTPS) and installing it in editable mode using pip install -e ..

    # Using SSH
    git clone git@github.com:ankile/furniture-bench.git
    
    # OR using HTTPS
    git clone https://github.com/ankile/furniture-bench.git
    
    # Install the package
    cd furniture-bench
    pip install -e .
  9. Setup Environment for OpenAI Gym (MuJoCo)

    release

    Installing mujoco_py requires setting up a .mujoco directory, downloading MuJoCo binaries, and configuring environment variables.

    Installation Steps

    1. Create ~/.mujoco and download mujoco210 and mujoco-py.
    2. Unzip them to your root directory.
    3. Add MuJoCo to your LD_LIBRARY_PATH in ~/.bashrc.
    4. Install cython<3.0.0.
    5. Install mujoco-py from its source directory.

    Non-Root/Container Workaround

    If you lack root privileges, install dependencies via Conda:

    pip install patchelf
    conda install -c conda-forge glew
    conda install -c conda-forge mesalib
    conda install -c menpo glfw3
    echo -e 'CPATH=$CONDA_PREFIX/include' >> ~/.bashrc
    source ~/.bashrc

    Root Installation

    If you have root privileges, use apt-get:

    sudo apt-get install patchelf
    sudo apt-get install python3-dev build-essential libssl-dev libffi-dev libxml2-dev  
    sudo apt install libosmesa6-dev libgl1-mesa-glx libglfw3
    # make mujoco directory
    mkdir $HOME/.mujoco
    cd ~/.mujoco 
    # download mujoco210 and mujoco-py from this source
    # unzip them to your root directory
    unzip ./mujoco210.zip
    unzip ./mujoco-py.zip
    # add link to mujoco
    echo -e '# link to mujoco
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/.mujoco/mujoco210/bin
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/nvidia 
    export PATH="$LD_LIBRARY_PATH:$PATH" ' >> ~/.bashrc
    source ~/.bashrc
    conda activate reinflow
    # install cython
    pip install 'cython<3.0.0' -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    # install mujoco-py
    cd ~/.mujoco/mujoco-py
    pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
    pip install -r requirements.dev.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
    pip3 install -e . --no-cache -i https://pypi.tuna.tsinghua.edu.cn/simple
  10. Prepare data for reproducing paper plots

    release

    To recreate the plots from the ReinFlow paper, you must first obtain the necessary data files. Download all files from the visualize branch of the ReinFlow repository and unzip them into the <REINFLOW_DIR>/visualize/ directory.

    mkdir -p <REINFLOW_DIR>/visualize/
    # Unzip files from 'visualize' branch into this directory
  11. Prepare OpenAI Gym datasets for pre-training

    release

    If you are reproducing pre-training results or training an offline RL agent (like FQL), you need to prepare OpenAI Gym datasets. You can use either the recommended D4RL version or the deprecated DPPO version.

    Option 1: Automatic Download Use the pre-training command with the flag use_d4rl_dataset=True. This will automatically download and prepare train.npz and normalization.npz files.

    Option 2: Manual Processing This method is available for hopper, walker2d, and ant tasks.

    1. Download raw .hdf5 files from Hugging Face.
    2. Inspect the .hdf5 structure using data_process/read_hdf5.py.
    3. Convert to .npz and scale demonstrations to [-1, 1] using data_process/hdf5_to_npz.py. You can optionally use --max_episodes (default: all) and --val_split (default: 0.0).
    4. Verify the output using data_process/read_npz.py.
    5. Move the resulting files to /data/gym/<TASK_NAME>.
    # Example: Automatic download for walker2d-medium-v2
    python script/run.py --config-dir=cfg/gym/pretrain/walker2d-medium-v2 --config-name=pre_reflow_mlp
    
    # Example: Manual processing steps
    # 1. Download
    wget https://huggingface.co/datasets/imone/D4RL/resolve/main/hopper_medium-v2.hdf5
    
    # 2. Inspect HDF5
    python data_process/read_hdf5.py --file_path=<PATH_TO_YOUR_HDF5>
    
    # 3. Convert and Normalize
    python data_process/hdf5_to_npz.py --data_path=<PATH_TO_YOUR_HDF5>
    
    # 4. Inspect NPZ
    python data_process/read_npz.py --data_path=normalization.npz