solarian-diffusion 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. diffusion_policy/__init__.py +0 -0
  2. diffusion_policy/codecs/__init__.py +0 -0
  3. diffusion_policy/codecs/imagecodecs_numcodecs.py +1408 -0
  4. diffusion_policy/common/__init__.py +0 -0
  5. diffusion_policy/common/checkpoint_util.py +64 -0
  6. diffusion_policy/common/json_logger.py +117 -0
  7. diffusion_policy/common/normalize_util.py +251 -0
  8. diffusion_policy/common/pose_repr_util.py +112 -0
  9. diffusion_policy/common/pytorch_util.py +82 -0
  10. diffusion_policy/common/q_critic_util.py +135 -0
  11. diffusion_policy/common/replay_buffer.py +672 -0
  12. diffusion_policy/common/sampler.py +204 -0
  13. diffusion_policy/config/legacy/task/blockpush_lowdim_seed.yaml +34 -0
  14. diffusion_policy/config/legacy/task/blockpush_lowdim_seed_abs.yaml +34 -0
  15. diffusion_policy/config/legacy/task/can_image.yaml +64 -0
  16. diffusion_policy/config/legacy/task/can_image_abs.yaml +64 -0
  17. diffusion_policy/config/legacy/task/can_lowdim.yaml +45 -0
  18. diffusion_policy/config/legacy/task/can_lowdim_abs.yaml +46 -0
  19. diffusion_policy/config/legacy/task/kitchen_lowdim.yaml +33 -0
  20. diffusion_policy/config/legacy/task/kitchen_lowdim_abs.yaml +38 -0
  21. diffusion_policy/config/legacy/task/lift_abs.yaml +72 -0
  22. diffusion_policy/config/legacy/task/lift_image.yaml +64 -0
  23. diffusion_policy/config/legacy/task/lift_image_abs.yaml +63 -0
  24. diffusion_policy/config/legacy/task/lift_lowdim.yaml +46 -0
  25. diffusion_policy/config/legacy/task/lift_lowdim_abs.yaml +47 -0
  26. diffusion_policy/config/legacy/task/pusht_image.yaml +40 -0
  27. diffusion_policy/config/legacy/task/pusht_lowdim.yaml +34 -0
  28. diffusion_policy/config/legacy/task/real_pusht_image.yaml +47 -0
  29. diffusion_policy/config/legacy/task/square_abs.yaml +72 -0
  30. diffusion_policy/config/legacy/task/square_image.yaml +64 -0
  31. diffusion_policy/config/legacy/task/square_image_abs.yaml +64 -0
  32. diffusion_policy/config/legacy/task/square_lowdim.yaml +46 -0
  33. diffusion_policy/config/legacy/task/square_lowdim_abs.yaml +47 -0
  34. diffusion_policy/config/legacy/task/tool_hang_abs.yaml +71 -0
  35. diffusion_policy/config/legacy/task/tool_hang_image.yaml +63 -0
  36. diffusion_policy/config/legacy/task/tool_hang_image_abs.yaml +63 -0
  37. diffusion_policy/config/legacy/task/tool_hang_lowdim.yaml +45 -0
  38. diffusion_policy/config/legacy/task/tool_hang_lowdim_abs.yaml +46 -0
  39. diffusion_policy/config/legacy/task/transport_image.yaml +75 -0
  40. diffusion_policy/config/legacy/task/transport_image_abs.yaml +75 -0
  41. diffusion_policy/config/legacy/task/transport_lowdim.yaml +49 -0
  42. diffusion_policy/config/legacy/task/transport_lowdim_abs.yaml +49 -0
  43. diffusion_policy/config/legacy/task/umi.yaml +45 -0
  44. diffusion_policy/config/legacy/task/umi_image.yaml +36 -0
  45. diffusion_policy/config/legacy/task/umi_teleop_image.yaml +41 -0
  46. diffusion_policy/config/legacy/train_bet_lowdim_workspace.yaml +132 -0
  47. diffusion_policy/config/legacy/train_diffusion_transformer_hybrid_workspace.yaml +141 -0
  48. diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_kitchen_workspace.yaml +146 -0
  49. diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_pusht_workspace.yaml +146 -0
  50. diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_workspace.yaml +146 -0
  51. diffusion_policy/config/legacy/train_diffusion_transformer_real_hybrid_workspace.yaml +144 -0
  52. diffusion_policy/config/legacy/train_diffusion_transformer_umi_teleop_hybrid_workspace.yaml +156 -0
  53. diffusion_policy/config/legacy/train_diffusion_unet_ddim_hybrid_workspace.yaml +140 -0
  54. diffusion_policy/config/legacy/train_diffusion_unet_ddim_lowdim_workspace.yaml +146 -0
  55. diffusion_policy/config/legacy/train_diffusion_unet_hybrid_workspace.yaml +137 -0
  56. diffusion_policy/config/legacy/train_diffusion_unet_image_pretrained_workspace.yaml +149 -0
  57. diffusion_policy/config/legacy/train_diffusion_unet_image_workspace.yaml +150 -0
  58. diffusion_policy/config/legacy/train_diffusion_unet_lowdim_workspace.yaml +143 -0
  59. diffusion_policy/config/legacy/train_diffusion_unet_real_hybrid_workspace.yaml +141 -0
  60. diffusion_policy/config/legacy/train_diffusion_unet_real_image_workspace.yaml +152 -0
  61. diffusion_policy/config/legacy/train_diffusion_unet_real_pretrained_workspace.yaml +152 -0
  62. diffusion_policy/config/legacy/train_diffusion_unet_timm_workspace.yaml +145 -0
  63. diffusion_policy/config/legacy/train_diffusion_unet_umi_hybrid_workspace.yaml +141 -0
  64. diffusion_policy/config/legacy/train_diffusion_unet_umi_teleop_hybrid_workspace.yaml +153 -0
  65. diffusion_policy/config/legacy/train_diffusion_unet_umi_timm_workspace.yaml +145 -0
  66. diffusion_policy/config/legacy/train_diffusion_unet_umi_workspace.yaml +150 -0
  67. diffusion_policy/config/legacy/train_diffusion_unet_video_workspace.yaml +128 -0
  68. diffusion_policy/config/legacy/train_ibc_dfo_hybrid_workspace.yaml +111 -0
  69. diffusion_policy/config/legacy/train_ibc_dfo_lowdim_workspace.yaml +109 -0
  70. diffusion_policy/config/legacy/train_ibc_dfo_real_hybrid_workspace.yaml +111 -0
  71. diffusion_policy/config/legacy/train_robomimic_image_workspace.yaml +92 -0
  72. diffusion_policy/config/legacy/train_robomimic_lowdim_workspace.yaml +92 -0
  73. diffusion_policy/config/legacy/train_robomimic_real_image_workspace.yaml +92 -0
  74. diffusion_policy/config/rl_annotate.yaml +265 -0
  75. diffusion_policy/config/rl_annotate_per_frame.yaml +162 -0
  76. diffusion_policy/config/rl_annotate_per_frame_v4.yaml +162 -0
  77. diffusion_policy/config/rl_annotate_v2.yaml +141 -0
  78. diffusion_policy/config/rl_annotate_v3.yaml +165 -0
  79. diffusion_policy/config/task/lift.yaml +89 -0
  80. diffusion_policy/config/task/square.yaml +92 -0
  81. diffusion_policy/config/task/tool_hang.yaml +93 -0
  82. diffusion_policy/config/task/tool_hang_abs.yaml +71 -0
  83. diffusion_policy/config/task/umi.yaml +105 -0
  84. diffusion_policy/config/task/umi_bimanual.yaml +135 -0
  85. diffusion_policy/config/task/umi_cup_wild.yaml +106 -0
  86. diffusion_policy/config/task/umi_image.yaml +36 -0
  87. diffusion_policy/config/task/umi_pick_lego.yaml +106 -0
  88. diffusion_policy/config/task/umi_pick_single_lego_175.yaml +106 -0
  89. diffusion_policy/config/task/umi_pick_single_lego_bbox.yaml +135 -0
  90. diffusion_policy/config/task/umi_teleop_image.yaml +41 -0
  91. diffusion_policy/config/train_diffusion_transformer_umi_bimanual_workspace.yaml +185 -0
  92. diffusion_policy/config/train_diffusion_transformer_umi_workspace.yaml +185 -0
  93. diffusion_policy/config/train_diffusion_unet_image_workspace.yaml +152 -0
  94. diffusion_policy/config/train_diffusion_unet_timm_robomimic_workspace.yaml +152 -0
  95. diffusion_policy/config/train_diffusion_unet_timm_umi_cup_wild.yaml +260 -0
  96. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox.yaml +265 -0
  97. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_awr.yaml +324 -0
  98. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_parl.yaml +399 -0
  99. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego.yaml +258 -0
  100. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_125.yaml +260 -0
  101. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_175.yaml +260 -0
  102. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_bbox.yaml +265 -0
  103. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_two_legos_bbox.yaml +265 -0
  104. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace.yaml +224 -0
  105. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego.yaml +213 -0
  106. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego_resume.yaml +213 -0
  107. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_pick_lego.yaml +254 -0
  108. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_red_cup.yaml +219 -0
  109. diffusion_policy/config/train_diffusion_unet_umi_bimanual_workspace.yaml +203 -0
  110. diffusion_policy/config/train_iql_umi.yaml +186 -0
  111. diffusion_policy/config/train_iql_umi_v2.yaml +212 -0
  112. diffusion_policy/config/train_iql_umi_v3.yaml +212 -0
  113. diffusion_policy/dataset/__init__.py +0 -0
  114. diffusion_policy/dataset/base_dataset.py +76 -0
  115. diffusion_policy/dataset/umi_dataset.py +652 -0
  116. diffusion_policy/env_runner/__init__.py +0 -0
  117. diffusion_policy/env_runner/base_image_runner.py +9 -0
  118. diffusion_policy/env_runner/real_pusht_image_runner.py +10 -0
  119. diffusion_policy/model/__init__.py +0 -0
  120. diffusion_policy/model/common/__init__.py +0 -0
  121. diffusion_policy/model/common/dict_of_tensor_mixin.py +38 -0
  122. diffusion_policy/model/common/lr_scheduler.py +46 -0
  123. diffusion_policy/model/common/module_attr_mixin.py +14 -0
  124. diffusion_policy/model/common/normalizer.py +353 -0
  125. diffusion_policy/model/common/tensor_util.py +960 -0
  126. diffusion_policy/model/diffusion/__init__.py +0 -0
  127. diffusion_policy/model/diffusion/conditional_unet1d.py +238 -0
  128. diffusion_policy/model/diffusion/conv1d_components.py +46 -0
  129. diffusion_policy/model/diffusion/ema_model.py +88 -0
  130. diffusion_policy/model/diffusion/mask_generator.py +221 -0
  131. diffusion_policy/model/diffusion/positional_embedding.py +17 -0
  132. diffusion_policy/model/vision/__init__.py +0 -0
  133. diffusion_policy/model/vision/crop_randomizer.py +288 -0
  134. diffusion_policy/model/vision/multi_image_obs_encoder.py +195 -0
  135. diffusion_policy/model/vision/timm_obs_encoder.py +525 -0
  136. diffusion_policy/policy/__init__.py +0 -0
  137. diffusion_policy/policy/base_image_policy.py +27 -0
  138. diffusion_policy/policy/diffusion_unet_image_policy.py +264 -0
  139. diffusion_policy/policy/diffusion_unet_timm_policy.py +607 -0
  140. diffusion_policy/policy/parl_action_optimizer.py +675 -0
  141. diffusion_policy/workspace/__init__.py +0 -0
  142. diffusion_policy/workspace/base_workspace.py +146 -0
  143. diffusion_policy/workspace/train_diffusion_unet_image_workspace.py +1585 -0
  144. solarian_diffusion/__init__.py +32 -0
  145. solarian_diffusion/config.py +31 -0
  146. solarian_diffusion/policy_server.py +316 -0
  147. solarian_diffusion/rtc.py +272 -0
  148. solarian_diffusion/service.py +16 -0
  149. solarian_diffusion/trainer.py +213 -0
  150. solarian_diffusion-0.1.0.dist-info/METADATA +126 -0
  151. solarian_diffusion-0.1.0.dist-info/RECORD +159 -0
  152. solarian_diffusion-0.1.0.dist-info/WHEEL +5 -0
  153. solarian_diffusion-0.1.0.dist-info/entry_points.txt +8 -0
  154. solarian_diffusion-0.1.0.dist-info/licenses/LICENSE +201 -0
  155. solarian_diffusion-0.1.0.dist-info/licenses/NOTICE +45 -0
  156. solarian_diffusion-0.1.0.dist-info/top_level.txt +3 -0
  157. umi/__init__.py +0 -0
  158. umi/common/__init__.py +0 -0
  159. umi/common/pose_util.py +115 -0
@@ -0,0 +1,32 @@
1
+ """solarian-diffusion — UMI diffusion policy plugin.
2
+
3
+ A *policy plugin* (keyed by model family, not by skill) that owns both halves
4
+ of a UMI ``DiffusionUnetTimmPolicy``:
5
+
6
+ * :class:`DiffusionTrainer` — a :class:`solarian.core.ai.system1.PolicyTrainer`
7
+ that trains the policy by reusing the UMI ``diffusion_policy`` workspace
8
+ (the accelerate multi-GPU loop). Discovered by ``solarian train`` via the
9
+ ``solarian.trainers`` entry point (``trainer: diffusion``).
10
+ * :class:`DiffusionPolicyServer` — a :class:`solarian.services.PolicyServer`
11
+ that serves a trained checkpoint over HTTP, advertised as the
12
+ ``diffusion-policy`` service (``solarian run-service diffusion-policy``).
13
+
14
+ Skills (e.g. ``solarian-skill-cup-arrange``) consume the served policy over
15
+ HTTP via ``PolicyClient`` and stay independent of this plugin.
16
+ """
17
+
18
+ __all__ = ["DiffusionTrainer", "DiffusionPolicyServer"]
19
+
20
+
21
+ def __getattr__(name):
22
+ # Lazy attribute access so importing the package (e.g. for entry-point
23
+ # discovery) doesn't drag in torch / diffusion_policy unless actually used.
24
+ if name == "DiffusionTrainer":
25
+ from solarian_diffusion.trainer import DiffusionTrainer
26
+
27
+ return DiffusionTrainer
28
+ if name == "DiffusionPolicyServer":
29
+ from solarian_diffusion.policy_server import DiffusionPolicyServer
30
+
31
+ return DiffusionPolicyServer
32
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
@@ -0,0 +1,31 @@
1
+ """Config schema for the diffusion trainer.
2
+
3
+ Extends the framework's thin :class:`~solarian.core.configs.TrainConfig` with
4
+ the few knobs needed to drive the UMI Hydra workspace. Everything else
5
+ (model architecture, optimizer, scheduler, EMA, ...) lives in the bundled /
6
+ ``diffusion_policy``-package Hydra config and is overridden via ``overrides``.
7
+ """
8
+
9
+ from typing import List, Optional
10
+
11
+ from solarian.core.configs import TrainConfig
12
+
13
+
14
+ class DiffusionTrainConfig(TrainConfig):
15
+ """Validated config for :class:`DiffusionTrainer`."""
16
+
17
+ #: Name of the UMI workspace Hydra config to compose (resolved from the
18
+ #: installed ``diffusion_policy`` package's ``config/`` directory).
19
+ config_name: str = "train_diffusion_unet_timm_umi_workspace"
20
+
21
+ #: Explicit dataset path (a ``.zarr.zip``). Falls back to ``train_dataset``;
22
+ #: a ``local@<name>`` value resolves under ``$SOLARIAN_PROJECT_PATH/datasets``.
23
+ dataset_path: Optional[str] = None
24
+
25
+ #: Number of training epochs (maps to ``training.num_epochs``). UMI trains
26
+ #: by epochs, so this takes precedence over the step-based ``steps`` field.
27
+ num_epochs: Optional[int] = None
28
+
29
+ #: Raw Hydra override strings appended last, e.g.
30
+ #: ``["policy.obs_encoder.model_name=vit_base_patch16_clip_224.openai"]``.
31
+ overrides: List[str] = []
@@ -0,0 +1,316 @@
1
+ """
2
+ DiffusionPolicyServer — serves a UMI diffusion policy over HTTP.
3
+
4
+ Wraps the UMI ``DiffusionUnetTimmPolicy`` (loaded from a training checkpoint)
5
+ in the generic :class:`~solarian.services.policy.PolicyServer` interface, so
6
+ the heavy inference stack (PyTorch + the ``diffusion_policy`` package) lives
7
+ on the server side and any skill can drive it over HTTP via
8
+ :class:`~solarian.services.policy.PolicyClient`.
9
+
10
+ Run it standalone (on the robot, the controller, or a dedicated GPU box)::
11
+
12
+ solarian run-service diffusion-policy --checkpoint data/models/cup_wild_vit_l_1img.ckpt
13
+ # or
14
+ python -m solarian_diffusion.policy_server -i <ckpt> --port 8001
15
+
16
+ Install the inference extra to get the heavy deps::
17
+
18
+ pip install "solarian-diffusion[serve]"
19
+ """
20
+
21
+ import logging
22
+ import os
23
+ from typing import Any, Dict
24
+
25
+ import numpy as np
26
+
27
+ from solarian.services import PolicyServer
28
+
29
+ logger = logging.getLogger(__name__)
30
+
31
+
32
+ class DiffusionPolicyServer(PolicyServer):
33
+ """Policy server hosting a UMI ``DiffusionUnetTimmPolicy``."""
34
+
35
+ title = "UMI Diffusion Policy Server"
36
+
37
+ def __init__(
38
+ self,
39
+ checkpoint: str,
40
+ num_inference_steps: int = 16,
41
+ fast_init: bool = True,
42
+ device: str | None = None,
43
+ ):
44
+ super().__init__(
45
+ checkpoint=checkpoint,
46
+ num_inference_steps=num_inference_steps,
47
+ fast_init=fast_init,
48
+ )
49
+ self.checkpoint = checkpoint
50
+ self.num_inference_steps = num_inference_steps
51
+ self.fast_init = fast_init
52
+ self._device_arg = device
53
+
54
+ self.policy = None
55
+ self.cfg = None
56
+ self.device = None
57
+ self._obs_pose_repr = None
58
+ self._action_pose_repr = None
59
+ self._checkpoint_file = None
60
+
61
+ # ------------------------------------------------------------------
62
+ # Loading
63
+ # ------------------------------------------------------------------
64
+
65
+ def load(self) -> None:
66
+ import dill
67
+ import hydra
68
+ import torch
69
+ from omegaconf import OmegaConf, open_dict
70
+
71
+ # Imported for its side effect of registering hydra targets, and to
72
+ # type-hint the workspace below.
73
+ from diffusion_policy.workspace.base_workspace import BaseWorkspace # noqa: F401
74
+
75
+ ckpt_path = self.checkpoint
76
+ if not ckpt_path.endswith(".ckpt"):
77
+ ckpt_path = os.path.join(ckpt_path, "checkpoints", "latest.ckpt")
78
+ self._checkpoint_file = os.path.basename(ckpt_path)
79
+
80
+ logger.info("Loading checkpoint from %s", ckpt_path)
81
+ with open(ckpt_path, "rb") as f:
82
+ payload = torch.load(f, map_location="cpu", pickle_module=dill)
83
+ cfg = payload["cfg"]
84
+ self.cfg = cfg
85
+ logger.info("Model: %s", OmegaConf.select(cfg, "policy.obs_encoder.model_name"))
86
+
87
+ # Don't fetch pretrained backbone weights — the checkpoint has them.
88
+ try:
89
+ if OmegaConf.select(cfg, "policy.obs_encoder.pretrained") is True:
90
+ with open_dict(cfg):
91
+ cfg.policy.obs_encoder.pretrained = False
92
+ except Exception as exc: # noqa: BLE001
93
+ logger.warning("Could not disable obs_encoder.pretrained: %s", exc)
94
+
95
+ original_use_ema = bool(OmegaConf.select(cfg, "training.use_ema"))
96
+ if self.fast_init and original_use_ema:
97
+ try:
98
+ with open_dict(cfg):
99
+ cfg.training.use_ema = False
100
+ except Exception as exc: # noqa: BLE001
101
+ logger.warning("fast_init: could not suppress EMA deepcopy: %s", exc)
102
+
103
+ workspace_cls = hydra.utils.get_class(cfg._target_)
104
+ workspace = workspace_cls(cfg)
105
+
106
+ if self.fast_init:
107
+ sds = payload["state_dicts"]
108
+ if original_use_ema and "ema_model" in sds:
109
+ src_key = "ema_model"
110
+ elif "model" in sds:
111
+ src_key = "model"
112
+ else:
113
+ raise RuntimeError(
114
+ f"Checkpoint has no 'model' or 'ema_model' state_dict; "
115
+ f"got {list(sds.keys())}"
116
+ )
117
+ logger.info("fast_init: loading '%s' state_dict into model", src_key)
118
+ workspace.model.load_state_dict(sds[src_key], strict=False)
119
+ policy = workspace.model
120
+ else:
121
+ workspace.load_payload(payload, exclude_keys=None, include_keys=None)
122
+ policy = workspace.model
123
+ if cfg.training.use_ema:
124
+ logger.info("Using EMA model")
125
+ policy = workspace.ema_model
126
+
127
+ policy.num_inference_steps = self.num_inference_steps
128
+ self._obs_pose_repr = cfg.task.pose_repr.obs_pose_repr
129
+ self._action_pose_repr = cfg.task.pose_repr.action_pose_repr
130
+
131
+ # Device selection: explicit override, else CUDA > MPS > CPU.
132
+ if self._device_arg is not None:
133
+ device = torch.device(self._device_arg)
134
+ elif torch.cuda.is_available():
135
+ device = torch.device("cuda")
136
+ elif torch.backends.mps.is_available():
137
+ device = torch.device("mps")
138
+ else:
139
+ device = torch.device("cpu")
140
+ self.device = device
141
+
142
+ policy.eval().to(device)
143
+ # RTC guidance backpropagates to the action trajectory at every
144
+ # denoising step. Only the trajectory needs a gradient, so dropping it
145
+ # from the weights keeps that graph to the activations alone.
146
+ for param in policy.parameters():
147
+ param.requires_grad_(False)
148
+ self.policy = policy
149
+ logger.info(
150
+ "Policy loaded on %s (obs_pose_repr=%s action_pose_repr=%s steps=%d)",
151
+ device,
152
+ self._obs_pose_repr,
153
+ self._action_pose_repr,
154
+ self.num_inference_steps,
155
+ )
156
+
157
+ # ------------------------------------------------------------------
158
+ # Inference
159
+ # ------------------------------------------------------------------
160
+
161
+ # Real-time chunking masking modes, as named on the wire, mapped to the
162
+ # weight schedules of ``rtc.prefix_weights``. "soft" and "hard" are the two
163
+ # the paper compares; "linear" and "ones" exist for its Figure 8 ablation.
164
+ _RTC_SCHEDULES = {
165
+ "soft": "exp",
166
+ "hard": "zeros",
167
+ "linear": "linear",
168
+ "ones": "ones",
169
+ }
170
+
171
+ def predict(
172
+ self,
173
+ obs_dict: Dict[str, np.ndarray],
174
+ prev_action: np.ndarray | None = None,
175
+ inference_delay: int = 0,
176
+ prefix_horizon: int | None = None,
177
+ beta: float | None = None,
178
+ masking: str = "soft",
179
+ **kwargs: Any,
180
+ ) -> Dict[str, Any]:
181
+ """Predict one action chunk, optionally inpainted onto ``prev_action``.
182
+
183
+ Without ``prev_action`` this is the stock policy path. With it, the
184
+ chunk is generated under real-time chunking: ``prev_action`` is the
185
+ previous chunk already aligned to this chunk's indices (index ``i`` of
186
+ both names the same instant), ``inference_delay`` is how many leading
187
+ actions are already committed, and ``prefix_horizon`` is ``H - s``, one
188
+ past the last index that overlaps the previous chunk.
189
+ """
190
+ import torch
191
+ from diffusion_policy.common.pytorch_util import dict_apply
192
+
193
+ from solarian_diffusion import rtc
194
+
195
+ device = self.device
196
+
197
+ def to_tensor(x):
198
+ tensor = torch.from_numpy(np.asarray(x))
199
+ if device.type == "mps" and tensor.dtype == torch.float64:
200
+ tensor = tensor.float()
201
+ return tensor.unsqueeze(0).to(device)
202
+
203
+ obs_dict_torch = dict_apply(obs_dict, to_tensor)
204
+
205
+ if prev_action is None or masking == "off":
206
+ with torch.no_grad():
207
+ result = self.policy.predict_action(obs_dict_torch)
208
+ info = None
209
+ else:
210
+ try:
211
+ schedule = self._RTC_SCHEDULES[masking]
212
+ except KeyError:
213
+ raise ValueError(
214
+ f"Unknown masking {masking!r}; expected one of "
215
+ f"{sorted(self._RTC_SCHEDULES)} or 'off'"
216
+ ) from None
217
+ horizon = self.policy.action_horizon
218
+ result = rtc.predict_action(
219
+ self.policy,
220
+ obs_dict_torch,
221
+ prev_action=np.asarray(prev_action, dtype=np.float32),
222
+ inference_delay=int(inference_delay),
223
+ prefix_horizon=horizon if prefix_horizon is None
224
+ else int(prefix_horizon),
225
+ beta=rtc.DEFAULT_BETA if beta is None else float(beta),
226
+ schedule=schedule,
227
+ )
228
+ info = result["info"]
229
+
230
+ # (1, H, A) -> (H, A); both action and raw_action are the unnormalized
231
+ # model output. The client applies get_real_umi_action() to convert
232
+ # to executable world poses.
233
+ raw_action = result["action_pred"][0].detach().to("cpu").numpy()
234
+ return {"action": raw_action, "raw_action": raw_action, "info": info}
235
+
236
+ def reset(self) -> None:
237
+ import torch
238
+
239
+ if self.policy is None:
240
+ return
241
+ self.policy.reset()
242
+ torch.manual_seed(0)
243
+ np.random.seed(0)
244
+ if torch.cuda.is_available():
245
+ torch.cuda.manual_seed_all(0)
246
+ try:
247
+ self.policy.noise_scheduler.set_timesteps(self.policy.num_inference_steps)
248
+ except Exception as exc: # noqa: BLE001
249
+ logger.warning("Could not reset noise scheduler timesteps: %s", exc)
250
+ self.policy.eval()
251
+ if self.device is not None and self.device.type == "cuda":
252
+ torch.cuda.empty_cache()
253
+
254
+ # ------------------------------------------------------------------
255
+ # Metadata
256
+ # ------------------------------------------------------------------
257
+
258
+ @property
259
+ def model_info(self) -> Dict[str, Any]:
260
+ from omegaconf import OmegaConf
261
+
262
+ return {
263
+ "model_name": OmegaConf.select(self.cfg, "policy.obs_encoder.model_name"),
264
+ "dataset_path": OmegaConf.select(self.cfg, "task.dataset.dataset_path"),
265
+ "obs_pose_repr": self._obs_pose_repr,
266
+ "action_pose_repr": self._action_pose_repr,
267
+ "num_inference_steps": int(self.policy.num_inference_steps),
268
+ "device": str(self.device),
269
+ "checkpoint": self._checkpoint_file or "",
270
+ }
271
+
272
+ @property
273
+ def shape_meta(self) -> Dict[str, Any]:
274
+ from omegaconf import OmegaConf
275
+
276
+ return OmegaConf.to_container(self.cfg.task.shape_meta, resolve=True)
277
+
278
+ # ------------------------------------------------------------------
279
+ # CLI
280
+ # ------------------------------------------------------------------
281
+
282
+ @classmethod
283
+ def add_arguments(cls, parser):
284
+ parser.add_argument(
285
+ "--input", "-i", required=True,
286
+ help="Path to checkpoint (.ckpt file or a run dir containing checkpoints/latest.ckpt)",
287
+ )
288
+ parser.add_argument(
289
+ "--num-inference-steps", type=int, default=16,
290
+ help="DDIM denoising steps per action chunk (default 16; UMI often fine at 8)",
291
+ )
292
+ parser.add_argument(
293
+ "--no-fast-init", dest="fast_init", action="store_false", default=True,
294
+ help="Disable fast-init (full random init + EMA deepcopy); for debugging",
295
+ )
296
+ parser.add_argument(
297
+ "--device", default=None,
298
+ help="Torch device override (default: cuda > mps > cpu)",
299
+ )
300
+
301
+ @classmethod
302
+ def from_args(cls, args):
303
+ return cls(
304
+ checkpoint=args.input,
305
+ num_inference_steps=args.num_inference_steps,
306
+ fast_init=args.fast_init,
307
+ device=args.device,
308
+ )
309
+
310
+
311
+ def main():
312
+ DiffusionPolicyServer.main()
313
+
314
+
315
+ if __name__ == "__main__":
316
+ main()
@@ -0,0 +1,272 @@
1
+ """Real-time chunking (RTC) for the UMI DDIM diffusion policy.
2
+
3
+ Black, Galliker & Levine, *Real-Time Execution of Action Chunking Flow
4
+ Policies* (NeurIPS 2025) — https://www.pi.website/research/real_time_chunking
5
+
6
+ RTC generates the next action chunk while the previous one is still executing.
7
+ The actions that will unavoidably have been consumed by the time inference
8
+ finishes are "frozen" to the values the controller is already committed to, and
9
+ the rest is inpainted around them, so consecutive chunks cannot bifurcate into
10
+ different strategies at the seam.
11
+
12
+ The paper writes its guidance term for flow matching, but this checkpoint is
13
+ DDIM with epsilon prediction. Rather than swap the tuned sampler for a flow one,
14
+ the corrections below are mapped into the DDIM parameterisation; see
15
+ ``guidance_weight`` and ``guided_sample`` for the two places the map shows up.
16
+ Every formula is cross-checked against the authors' reference implementation,
17
+ https://github.com/Physical-Intelligence/real-time-chunking-kinetix (``src/model.py``).
18
+ """
19
+
20
+ import math
21
+ from typing import Any, Dict, Literal
22
+
23
+ import numpy as np
24
+
25
+ # The paper's max_guidance_weight, from Table 4 and the ablation in Figure 7.
26
+ DEFAULT_BETA = 5.0
27
+
28
+ PrefixSchedule = Literal["exp", "linear", "ones", "zeros"]
29
+
30
+
31
+ def prefix_weights(
32
+ total: int,
33
+ start: int,
34
+ end: int,
35
+ schedule: PrefixSchedule = "exp",
36
+ ) -> np.ndarray:
37
+ """Soft mask over chunk indices (paper Eq. 5).
38
+
39
+ ``start`` is the inference delay ``d``: up to there the previous chunk is
40
+ already committed, so those actions are attended to at full weight.
41
+ ``end`` is ``H - s``, one past the last index that overlaps the previous
42
+ chunk; from there on there is nothing to agree with and the weight is 0.
43
+ In between the weight decays, because an action further into the future was
44
+ predicted from a staler observation and deserves less trust.
45
+
46
+ With ``start=2, end=6, total=10`` the "linear" schedule gives
47
+ ``1 1 4/5 3/5 2/5 1/5 0 0 0 0``. "exp" is the paper's default; "zeros" is
48
+ hard masking (attend to the committed prefix only) and "ones" attends to
49
+ the whole overlap at full weight. Both are kept for the Figure 8 ablation.
50
+
51
+ ``end`` wins over ``start``: if ``end < start`` then ``start`` is pushed
52
+ down to it, so ``end == 0`` ignores the previous chunk entirely.
53
+ """
54
+ if total <= 0:
55
+ raise ValueError(f"total must be positive, got {total}")
56
+ start = min(start, end)
57
+ index = np.arange(total, dtype=np.float64)
58
+ if schedule == "ones":
59
+ w = np.ones(total, dtype=np.float64)
60
+ elif schedule == "zeros":
61
+ w = (index < start).astype(np.float64)
62
+ elif schedule in ("linear", "exp"):
63
+ w = np.clip((start - 1 - index) / (end - start + 1) + 1, 0.0, 1.0)
64
+ if schedule == "exp":
65
+ # Normalised by expm1(1) rather than the algebraically equal e - 1,
66
+ # which lands a ULP the wrong side of it and leaves the committed
67
+ # prefix at 1.0000000000000002.
68
+ w = w * np.expm1(w) / np.expm1(1.0)
69
+ else:
70
+ raise ValueError(f"Invalid schedule: {schedule}")
71
+ return np.where(index >= end, 0.0, w)
72
+
73
+
74
+ def guidance_weight(alpha_bar: float, beta: float = DEFAULT_BETA) -> float:
75
+ """The paper's ΠGDM guidance weight, in DDIM's noise schedule.
76
+
77
+ In flow time the weight is ``min(beta, (1 - t) / (t * r_t^2))`` with
78
+ ``r_t^2 = (1 - t)^2 / (t^2 + (1 - t)^2)``, which simplifies to
79
+ ``(t^2 + (1 - t)^2) / (t * (1 - t))``. Substituting the standard
80
+ flow/variance-preserving correspondence ``t = sqrt(a) / (sqrt(a) + sqrt(1 - a))``
81
+ for ``a = alpha_bar`` collapses the numerator to ``a + (1 - a) = 1``:
82
+
83
+ weight = 1 / (sqrt(a) * sqrt(1 - a))
84
+
85
+ Same U shape as Figure 7 and the same minimum of 2 at ``alpha_bar = 0.5``,
86
+ diverging at both ends of the schedule — which is why the clip at ``beta``
87
+ is not optional. The paper's value of 5 transfers unchanged, because the
88
+ substitution is an identity rather than a reparameterisation.
89
+ """
90
+ denom = math.sqrt(max(alpha_bar, 0.0)) * math.sqrt(max(1.0 - alpha_bar, 0.0))
91
+ if denom <= 0.0:
92
+ return float(beta)
93
+ return float(min(beta, 1.0 / denom))
94
+
95
+
96
+ def guided_sample(
97
+ policy,
98
+ global_cond,
99
+ target,
100
+ weights,
101
+ beta: float = DEFAULT_BETA,
102
+ generator=None,
103
+ ):
104
+ """DDIM sampling with the previous chunk inpainted via ΠGDM guidance.
105
+
106
+ ``target`` and the returned trajectory are in the policy's normalized
107
+ action space; ``weights`` is the per-timestep mask of shape ``(H,)``.
108
+
109
+ Mirrors ``DiffusionUnetTimmPolicy.conditional_sample`` step for step, with
110
+ one addition: before each ``scheduler.step`` the denoised estimate is nudged
111
+ toward ``target`` along the pseudoinverse-guidance direction.
112
+ """
113
+ import torch
114
+
115
+ model = policy.model
116
+ # Kept as the checkpoint was tuned, clip_sample and all. The clip bounds
117
+ # how hard the prefix can be pinned — it truncates the correction once the
118
+ # denoised estimate reaches +-1 — but every legitimate target is a real
119
+ # action and so already inside that range, and sampling without it lets the
120
+ # freshly generated tail leave the range the normalizer was fitted to,
121
+ # which is an out-of-distribution pose on a real arm.
122
+ scheduler = policy.noise_scheduler
123
+ prediction_type = scheduler.config.prediction_type
124
+ if prediction_type not in ("epsilon", "sample"):
125
+ raise NotImplementedError(
126
+ f"RTC guidance supports epsilon and sample prediction, "
127
+ f"not {prediction_type!r}"
128
+ )
129
+
130
+ trajectory = torch.randn(
131
+ size=target.shape,
132
+ dtype=target.dtype,
133
+ device=target.device,
134
+ generator=generator,
135
+ )
136
+
137
+ scheduler.set_timesteps(policy.num_inference_steps)
138
+ alphas_cumprod = scheduler.alphas_cumprod.to(
139
+ device=target.device, dtype=target.dtype
140
+ )
141
+ # (H,) -> (1, H, 1): one weight per chunk index, shared by every action dim,
142
+ # which is how the paper applies diag(W).
143
+ mask = weights.reshape(1, -1, 1)
144
+
145
+ for t in scheduler.timesteps:
146
+ alpha_bar = alphas_cumprod[t]
147
+ sqrt_alpha = alpha_bar.sqrt()
148
+ sqrt_one_minus = (1.0 - alpha_bar).sqrt()
149
+
150
+ # Grad is needed only through this one forward pass, and only with
151
+ # respect to the trajectory; the observation encoder ran once, outside.
152
+ with torch.enable_grad():
153
+ x = trajectory.detach().requires_grad_(True)
154
+ model_output = model(x, t, local_cond=None, global_cond=global_cond)
155
+ if prediction_type == "epsilon":
156
+ denoised = (x - sqrt_one_minus * model_output) / sqrt_alpha
157
+ else:
158
+ denoised = model_output
159
+ error = ((target - denoised) * mask).detach()
160
+ # sum(denoised * error) differentiates to J^T error, the
161
+ # vector-Jacobian product of Eq. 2.
162
+ (correction,) = torch.autograd.grad((denoised * error).sum(), x)
163
+
164
+ weight = guidance_weight(float(alpha_bar), beta)
165
+ # The paper corrects the velocity field, which moves the denoised
166
+ # estimate by (1 - t) * weight * g_flow. The flow latent is the DDIM
167
+ # latent divided by sqrt(a) + sqrt(1 - a), so g_flow picks up that same
168
+ # factor by the chain rule and (1 - t) * (sqrt(a) + sqrt(1 - a))
169
+ # collapses to sqrt(1 - a). Pushing that through
170
+ # denoised = (x - sqrt(1 - a) * eps) / sqrt(a) cancels the sqrt(1 - a),
171
+ # which is what keeps this stable at the low-noise end where it is tiny.
172
+ if prediction_type == "epsilon":
173
+ model_output = model_output.detach() - sqrt_alpha * weight * correction
174
+ else:
175
+ model_output = (
176
+ model_output.detach() + sqrt_one_minus * weight * correction
177
+ )
178
+
179
+ trajectory = scheduler.step(
180
+ model_output,
181
+ t,
182
+ trajectory.detach(),
183
+ generator=generator,
184
+ **policy.kwargs,
185
+ ).prev_sample
186
+
187
+ return trajectory
188
+
189
+
190
+ def predict_action(
191
+ policy,
192
+ obs_dict: Dict[str, Any],
193
+ prev_action,
194
+ inference_delay: int,
195
+ prefix_horizon: int,
196
+ beta: float = DEFAULT_BETA,
197
+ schedule: PrefixSchedule = "exp",
198
+ ) -> Dict[str, Any]:
199
+ """``DiffusionUnetTimmPolicy.predict_action``, with RTC guidance.
200
+
201
+ ``prev_action`` is the previous chunk already aligned to this chunk's
202
+ indices — index ``i`` of both names the same instant — in unnormalized
203
+ action units, shape ``(B, H, A)``. ``prefix_horizon`` is ``H - s``.
204
+
205
+ Returns the same ``action`` / ``action_pred`` keys as the stock path, plus
206
+ the weights actually used and how closely the frozen prefix was matched.
207
+ """
208
+ import torch
209
+
210
+ assert "past_action" not in obs_dict, "not implemented yet"
211
+
212
+ horizon = policy.action_horizon
213
+ if prefix_horizon < 0 or prefix_horizon > horizon:
214
+ raise ValueError(
215
+ f"prefix_horizon must be in [0, {horizon}], got {prefix_horizon}"
216
+ )
217
+ if inference_delay < 0 or inference_delay > horizon:
218
+ raise ValueError(
219
+ f"inference_delay must be in [0, {horizon}], got {inference_delay}"
220
+ )
221
+
222
+ nobs = policy.normalizer.normalize(obs_dict)
223
+ batch = next(iter(nobs.values())).shape[0]
224
+ with torch.no_grad():
225
+ global_cond = policy.obs_encoder(nobs)
226
+
227
+ target = torch.as_tensor(
228
+ np.asarray(prev_action), device=policy.device, dtype=policy.dtype
229
+ )
230
+ if target.ndim == 2:
231
+ target = target.unsqueeze(0)
232
+ if target.shape != (batch, horizon, policy.action_dim):
233
+ raise ValueError(
234
+ f"prev_action must be {(batch, horizon, policy.action_dim)}, "
235
+ f"got {tuple(target.shape)}"
236
+ )
237
+ ntarget = policy.normalizer["action"].normalize(target)
238
+
239
+ weights = prefix_weights(horizon, inference_delay, prefix_horizon, schedule)
240
+ weights_t = torch.as_tensor(weights, device=policy.device, dtype=policy.dtype)
241
+
242
+ nsample = guided_sample(
243
+ policy,
244
+ global_cond=global_cond,
245
+ target=ntarget,
246
+ weights=weights_t,
247
+ beta=beta,
248
+ )
249
+ action_pred = policy.normalizer["action"].unnormalize(nsample)
250
+
251
+ # How far the committed prefix moved despite being pinned. Small means the
252
+ # guidance bound; large means it did not, and the seam will still jump. The
253
+ # worst single action dimension, so it mixes the units the action space
254
+ # mixes and is a diagnostic rather than a distance.
255
+ frozen = weights >= 1.0
256
+ if frozen.any():
257
+ residual = (
258
+ (action_pred[:, frozen] - target[:, frozen]).abs().max().item()
259
+ )
260
+ else:
261
+ residual = 0.0
262
+
263
+ return {
264
+ "action": action_pred,
265
+ "action_pred": action_pred,
266
+ "info": {
267
+ "rtc_weights": weights.tolist(),
268
+ "rtc_frozen_residual": residual,
269
+ "rtc_inference_delay": int(inference_delay),
270
+ "rtc_prefix_horizon": int(prefix_horizon),
271
+ },
272
+ }
@@ -0,0 +1,16 @@
1
+ from solarian.services import Service
2
+
3
+
4
+ class DiffusionPolicyService(Service):
5
+ """A trained UMI diffusion checkpoint, served by :class:`DiffusionPolicyServer`."""
6
+
7
+ server_module = "solarian_diffusion.policy_server"
8
+ default_port = 8001
9
+ checkpoint_arg = "--input"
10
+
11
+ @classmethod
12
+ def modal_spec(cls):
13
+ # Served in the environment its checkpoints were trained in.
14
+ from solarian_diffusion.trainer import DiffusionTrainer
15
+
16
+ return DiffusionTrainer.modal_spec()