-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(policies): Allow users to register 3rd party policies - pip install lerobot_policy_mypolicy
#2308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielsanjosepro
wants to merge
13
commits into
huggingface:main
Choose a base branch
from
danielsanjosepro:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+271
−14
Open
feat(policies): Allow users to register 3rd party policies - pip install lerobot_policy_mypolicy
#2308
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e8b83f3
feat: Register external policies
danielsanjosepro fbc619d
ruff fix
danielsanjosepro cbbafd1
move policy util functions to policy factory
danielsanjosepro 625fb2d
refactor register_third_party_devices -> register_third_party_plugins
danielsanjosepro 3f8a7ce
feat: Update docs with bring your own policies
danielsanjosepro 8da0115
Merge branch 'huggingface:main' into main
danielsanjosepro 9054030
Improve docs for new policies
danielsanjosepro 00f0551
Merge branch 'main' into main
danielsanjosepro 80951f5
fix: Inconsistent quotation marks
danielsanjosepro d73e12d
fix: Remove print statement
danielsanjosepro 6228cc2
fix: wrong base class name in documentation
danielsanjosepro 925acd0
fix: Handle better how the models are parsed
danielsanjosepro 00e4c99
fix: precommit passing
danielsanjosepro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| # Bring Your Own Policies | ||
|
|
||
| This tutorial explains how to integrate your own custom policy implementations into the LeRobot ecosystem, allowing you to leverage all LeRobot tools for training, evaluation, and deployment while using your own algorithms. | ||
|
|
||
| ## Step 1: Create a Policy Package | ||
|
|
||
| Your custom policy should be organized as an installable Python package following LeRobot's plugin conventions. | ||
|
|
||
| ### Package Structure | ||
|
|
||
| Create a package with the prefix `lerobot_policy_` (IMPORTANT!) followed by your policy name: | ||
|
|
||
| ```bash | ||
| lerobot_policy_my_custom_policy/ | ||
| ├── pyproject.toml | ||
| └── src/ | ||
| └── lerobot_policy_my_custom_policy/ | ||
| ├── __init__.py | ||
| ├── configuration_my_custom_policy.py | ||
| ├── modeling_my_custom_policy.py | ||
| └── processor_my_custom_policy.py | ||
| ``` | ||
|
|
||
| ### Package Configuration | ||
|
|
||
| Set up your `pyproject.toml`: | ||
|
|
||
| ```toml | ||
| [project] | ||
| name = "lerobot_policy_my_custom_policy" | ||
| version = "0.1.0" | ||
| dependencies = [ | ||
| # your policy-specific dependencies | ||
| ] | ||
| requires-python = ">= 3.11" | ||
|
|
||
| [build-system] | ||
| build-backend = "hatchling.build" | ||
| requires = ["hatchling"] | ||
| ``` | ||
|
|
||
| ## Step 2: Define the Policy Configuration | ||
|
|
||
| Create a configuration class that inherits from `PreTrainedConfig` and registers your policy type: | ||
|
|
||
| ```python | ||
| # configuration_my_custom_policy.py | ||
| from dataclasses import dataclass, field | ||
| from lerobot.configs.policies import PreTrainedConfig | ||
| from lerobot.configs.types import NormalizationMode | ||
|
|
||
| @PreTrainedConfig.register_subclass("my_custom_policy") | ||
| @dataclass | ||
| class MyCustomPolicyConfig(PreTrainedConfig): | ||
| """Configuration class for MyCustomPolicy. | ||
| Args: | ||
| n_obs_steps: Number of observation steps to use as input | ||
| horizon: Action prediction horizon | ||
| n_action_steps: Number of action steps to execute | ||
| hidden_dim: Hidden dimension for the policy network | ||
| # Add your policy-specific parameters here | ||
| """ | ||
| # ...PreTrainedConfig fields... | ||
| pass | ||
|
|
||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
| # Add any validation logic here | ||
|
|
||
| def validate_features(self) -> None: | ||
| """Validate input/output feature compatibility.""" | ||
| # Implement validation logic for your policy's requirements | ||
| pass | ||
| ``` | ||
|
|
||
| ## Step 3: Implement the Policy Class | ||
|
|
||
| Create your policy implementation by inheriting from LeRobot's base policy class: | ||
|
|
||
| ```python | ||
| # modeling_my_custom_policy.py | ||
| import torch | ||
| import torch.nn as nn | ||
| from typing import Dict, Any | ||
|
|
||
| from lerobot.models.policies.base import LeRobotPolicy | ||
| from .configuration_my_custom_policy import MyCustomPolicyConfig | ||
|
|
||
| class MyCustomPolicy(LeRobotPolicy): | ||
danielsanjosepro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| config_class = MyCustomPolicyConfig | ||
| name = "my_custom_policy" | ||
|
|
||
| def __init__(self, config: MyCustomPolicyConfig, dataset_stats: Dict[str, Any] = None): | ||
| super().__init__(config, dataset_stats) | ||
| ... | ||
| ``` | ||
|
|
||
| ## Step 4: Add Data Processors | ||
|
|
||
| If your policy requires special data preprocessing, create processor functions: | ||
|
|
||
| ```python | ||
| # processor_my_custom_policy.py | ||
| from typing import Dict, Any | ||
| import torch | ||
|
|
||
| def make_my_custom_policy_pre_post_processors( | ||
| config, | ||
| ) -> tuple[ | ||
| PolicyProcessorPipeline[dict[str, Any], dict[str, Any]], | ||
| PolicyProcessorPipeline[PolicyAction, PolicyAction], | ||
| ]: | ||
|
|
||
| """Create preprocessing and postprocessing functions for your policy.""" | ||
| # Define your preprocessing and postprocessing logic here | ||
| pass | ||
|
|
||
| ``` | ||
|
|
||
| ## Step 5: Package Initialization | ||
|
|
||
| Expose your classes in the package's `__init__.py`: | ||
|
|
||
| ```python | ||
| # __init__.py | ||
| """Custom policy package for LeRobot.""" | ||
|
|
||
| try: | ||
| import lerobot # noqa: F401 | ||
| except ImportError: | ||
| raise ImportError( | ||
| "lerobot is not installed. Please install lerobot to use this policy package." | ||
| ) | ||
|
|
||
| from .configuration_my_custom_policy import MyCustomPolicyConfig | ||
| from .modeling_my_custom_policy import MyCustomPolicy | ||
| from .processor_my_custom_policy import make_my_custom_policy_pre_post_processors | ||
|
|
||
| __all__ = [ | ||
| "MyCustomPolicyConfig", | ||
| "MyCustomPolicy", | ||
| "make_my_custom_policy_pre_post_processors", | ||
| ] | ||
| ``` | ||
|
|
||
| ## Step 6: Installation and Usage | ||
|
|
||
| ### Install Your Policy Package | ||
|
|
||
| ```bash | ||
| cd lerobot_policy_my_custom_policy | ||
| pip install -e . | ||
|
|
||
| # Or install from PyPI | ||
| pip install lerobot_policy_my_custom_policy | ||
| ``` | ||
|
|
||
| ### Use Your Policy | ||
|
|
||
| Once installed, your policy automatically integrates with LeRobot's training and evaluation tools: | ||
|
|
||
| ```bash | ||
| lerobot-train \ | ||
| --policy.type my_custom_policy \ | ||
danielsanjosepro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| --env.type pusht \ | ||
| --steps 200000 | ||
| ``` | ||
|
|
||
| ## Examples and Community Contributions | ||
|
|
||
| Check out these example policy implementations: | ||
|
|
||
| - [DiTFlow Policy](https://github.com/danielsanjosepro/lerobot_policy_ditflow) - Diffusion Transformer policy with flow-matching objective. | ||
| - Try it out in this example: [DiTFlow Example](https://github.com/danielsanjosepro/test_lerobot_policy_ditflow) | ||
|
|
||
| Share your policy implementations with the community! 🤗 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.