-
Notifications
You must be signed in to change notification settings - Fork 15
feat: Robot() factory + top-level lazy imports #86
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
cagataycali
wants to merge
3
commits into
strands-labs:main
Choose a base branch
from
cagataycali:feat/robot-factory
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,11 +11,12 @@ | |
| - Clean separation between robot control and policy inference | ||
| - Direct policy injection for maximum flexibility | ||
| - Multi-camera support with rich configuration options | ||
| - MuJoCo simulation backend (no GPU required) | ||
|
|
||
| Lazy Loading: | ||
| Heavy imports (Robot, tools, Gr00tPolicy) are deferred until first access. | ||
| Heavy imports are deferred so ``import strands_robots`` stays fast when lerobot/torch | ||
| are installed but not yet needed. | ||
| Heavy imports (Robot, tools, Gr00tPolicy, Simulation) are deferred until | ||
| first access. Heavy imports are deferred so ``import strands_robots`` stays | ||
| fast when lerobot/torch/mujoco are installed but not yet needed. | ||
|
|
||
| Light-weight symbols (Policy, MockPolicy, create_policy) are available | ||
| immediately since they don't pull in torch/lerobot. | ||
|
|
@@ -25,15 +26,31 @@ | |
| import warnings as _warnings | ||
| from typing import Any | ||
|
|
||
| # Light-weight imports - no torch / lerobot dependency | ||
| # ------------------------------------------------------------------ | ||
| # Light-weight imports — no torch / lerobot / mujoco dependency | ||
| # ------------------------------------------------------------------ | ||
| from strands_robots.policies import MockPolicy, Policy, create_policy # noqa: F401 | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Lazy-loaded heavy symbols | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Maps public name -> (module_path, attribute_name) | ||
| _LAZY_IMPORTS: dict[str, tuple[str, str]] = { | ||
| # Hardware robot | ||
| "Robot": ("strands_robots.robot", "Robot"), | ||
| "list_robots": ("strands_robots.registry", "list_robots"), | ||
| # Policies | ||
| "Gr00tPolicy": ("strands_robots.policies.groot", "Gr00tPolicy"), | ||
| # Simulation (MuJoCo) | ||
| "Simulation": ("strands_robots.simulation", "Simulation"), | ||
| "create_simulation": ("strands_robots.simulation.factory", "create_simulation"), | ||
| "list_backends": ("strands_robots.simulation.factory", "list_backends"), | ||
| "register_backend": ("strands_robots.simulation.factory", "register_backend"), | ||
| "SimWorld": ("strands_robots.simulation", "SimWorld"), | ||
| "SimRobot": ("strands_robots.simulation", "SimRobot"), | ||
| "SimObject": ("strands_robots.simulation", "SimObject"), | ||
| "SimCamera": ("strands_robots.simulation", "SimCamera"), | ||
| # Tools | ||
| "gr00t_inference": ("strands_robots.tools.gr00t_inference", "gr00t_inference"), | ||
| "lerobot_calibrate": ("strands_robots.tools.lerobot_calibrate", "lerobot_calibrate"), | ||
| "lerobot_camera": ("strands_robots.tools.lerobot_camera", "lerobot_camera"), | ||
|
|
@@ -50,6 +67,11 @@ | |
| # Lazy-loaded | ||
| "Robot", | ||
| "Gr00tPolicy", | ||
| "Simulation", | ||
| "SimWorld", | ||
| "SimRobot", | ||
| "SimObject", | ||
| "SimCamera", | ||
| "gr00t_inference", | ||
| "lerobot_camera", | ||
| "lerobot_teleoperate", | ||
|
|
@@ -59,12 +81,32 @@ | |
| ] | ||
|
|
||
|
|
||
| # Auto-configure MuJoCo GL backend for headless environments BEFORE any | ||
| # module imports mujoco at the top level. MuJoCo locks the OpenGL backend | ||
| # at import time, so MUJOCO_GL must be set first. | ||
| # | ||
| # WHY EAGER: This MUST run at module import time, not lazily, because: | ||
| # 1. MuJoCo reads MUJOCO_GL only on first `import mujoco` | ||
| # 2. Any downstream code doing `from strands_robots.simulation import ...` | ||
| # triggers mujoco import via the lazy-load chain | ||
| # 3. If we defer to first use, the env var would be set too late | ||
| # This is the canonical location — strands_robots/simulation/__init__.py | ||
| # intentionally does NOT duplicate this call. | ||
| try: | ||
| from strands_robots.simulation.mujoco.backend import _configure_gl_backend | ||
|
|
||
| _configure_gl_backend() | ||
| except (ImportError, AttributeError, OSError): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eager import on every
Not a blocker, but |
||
| pass | ||
|
|
||
|
|
||
| def __getattr__(name: str) -> Any: # noqa: N807 | ||
| """Lazy-load heavy modules on first attribute access. | ||
|
|
||
| This avoids importing torch, lerobot, numpy, pyserial, etc. at | ||
| This avoids importing torch, lerobot, numpy, mujoco, pyserial, etc. at | ||
| ``import strands_robots`` time. The first access to e.g. | ||
| ``strands_robots.Robot`` triggers the real import. | ||
| ``strands_robots.Robot`` or ``strands_robots.Simulation`` triggers the | ||
| real import. | ||
| """ | ||
| if name in _LAZY_IMPORTS: | ||
| module_path, attr_name = _LAZY_IMPORTS[name] | ||
|
|
||
Oops, something went wrong.
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.