forked from huggingface/leLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
188 lines (156 loc) · 6.89 KB
/
Copy pathtrain.py
File metadata and controls
188 lines (156 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Copyright 2025 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Training-specific helpers: the request schema and the LeRobot CLI builder.
The actual job lifecycle (subprocess management, registry, log streaming)
lives in app/jobs.py.
"""
import re
from pydantic import BaseModel
_SLUG_RE = re.compile(r"[^a-zA-Z0-9._-]+")
class TrainingRequest(BaseModel):
# Dataset configuration
dataset_repo_id: str
dataset_revision: str | None = None
dataset_root: str | None = None
dataset_episodes: list[int] | None = None
# Policy configuration
policy_type: str = "act"
# Core training parameters
steps: int = 10000
batch_size: int = 8
seed: int | None = 1000
num_workers: int = 4
# Logging and checkpointing
log_freq: int = 250
save_freq: int = 1000
eval_freq: int = 0
save_checkpoint: bool = True
# Output configuration
output_dir: str = "outputs/train"
resume: bool = False
job_name: str | None = None
# Weights & Biases
wandb_enable: bool = False
wandb_project: str | None = None
wandb_entity: str | None = None
wandb_notes: str | None = None
wandb_run_id: str | None = None
wandb_mode: str | None = "online"
wandb_disable_artifact: bool = False
# Environment / evaluation
env_type: str | None = None
env_task: str | None = None
eval_n_episodes: int = 10
eval_batch_size: int = 50
eval_use_async_envs: bool = False
# Policy-specific
policy_device: str | None = "cuda"
policy_use_amp: bool = False
# Hub upload (set by HfCloudJobRunner; not exposed in the form)
policy_push_to_hub: bool = False
policy_repo_id: str | None = None
# Optimizer
optimizer_type: str | None = "adam"
optimizer_lr: float | None = None
optimizer_weight_decay: float | None = None
optimizer_grad_clip_norm: float | None = None
# Advanced
use_policy_training_preset: bool = True
config_path: str | None = None
def build_training_command(
request: TrainingRequest, output_dir: str, python_executable: str = "python"
) -> list[str]:
"""Build the argv list to invoke `<python_executable> -m lerobot.scripts.lerobot_train`.
`output_dir` is supplied separately from the request so the caller (the
JobRegistry) can pin it to the per-job directory rather than relying on
request.output_dir, which the frontend doesn't even send in the new world.
`python_executable` defaults to "python" for the cloud runner (whose
container has lerobot on PATH); the local runner must pass sys.executable
so the subprocess uses the same interpreter as lelab itself — otherwise
PATH lookup picks up a different env (uv tool venv, miniforge3 base, etc.)
that lacks lerobot.
"""
cmd: list[str] = [python_executable, "-m", "lerobot.scripts.lerobot_train"]
# Dataset
cmd.extend(["--dataset.repo_id", request.dataset_repo_id])
if request.dataset_revision:
cmd.extend(["--dataset.revision", request.dataset_revision])
if request.dataset_root:
cmd.extend(["--dataset.root", request.dataset_root])
if request.dataset_episodes:
cmd.extend(["--dataset.episodes"] + [str(ep) for ep in request.dataset_episodes])
# Policy
cmd.extend(["--policy.type", request.policy_type])
# Core training params
cmd.extend(["--steps", str(request.steps)])
cmd.extend(["--batch_size", str(request.batch_size)])
cmd.extend(["--num_workers", str(request.num_workers)])
if request.seed is not None:
cmd.extend(["--seed", str(request.seed)])
# Policy device / AMP / hub
if request.policy_device:
cmd.extend(["--policy.device", request.policy_device])
cmd.extend(["--policy.use_amp", "true" if request.policy_use_amp else "false"])
# LeRobot defaults push_to_hub=True and demands --policy.repo_id when so.
# Local jobs keep it off; HF Cloud jobs flip it on via the runner.
cmd.extend(["--policy.push_to_hub", "true" if request.policy_push_to_hub else "false"])
if request.policy_push_to_hub and request.policy_repo_id:
cmd.extend(["--policy.repo_id", request.policy_repo_id])
# Logging / checkpointing
cmd.extend(["--log_freq", str(request.log_freq)])
cmd.extend(["--save_freq", str(request.save_freq)])
cmd.extend(["--eval_freq", str(request.eval_freq)])
cmd.extend(["--save_checkpoint", "true" if request.save_checkpoint else "false"])
# Output
cmd.extend(["--output_dir", output_dir])
cmd.extend(["--resume", "true" if request.resume else "false"])
if request.job_name:
cmd.extend(["--job_name", request.job_name])
# W&B
cmd.extend(["--wandb.enable", "true" if request.wandb_enable else "false"])
if request.wandb_enable:
if request.wandb_project:
cmd.extend(["--wandb.project", request.wandb_project])
if request.wandb_entity:
cmd.extend(["--wandb.entity", request.wandb_entity])
if request.wandb_notes:
cmd.extend(["--wandb.notes", request.wandb_notes])
if request.wandb_run_id:
cmd.extend(["--wandb.run_id", request.wandb_run_id])
if request.wandb_mode:
cmd.extend(["--wandb.mode", request.wandb_mode])
cmd.extend(["--wandb.disable_artifact", "true" if request.wandb_disable_artifact else "false"])
# Env
if request.env_type:
cmd.extend(["--env.type", request.env_type])
if request.env_task:
cmd.extend(["--env.task", request.env_task])
# Eval
cmd.extend(["--eval.n_episodes", str(request.eval_n_episodes)])
cmd.extend(["--eval.batch_size", str(request.eval_batch_size)])
cmd.extend(["--eval.use_async_envs", "true" if request.eval_use_async_envs else "false"])
# Optimizer
if request.optimizer_type:
cmd.extend(["--optimizer.type", request.optimizer_type])
if request.optimizer_lr is not None:
cmd.extend(["--optimizer.lr", str(request.optimizer_lr)])
if request.optimizer_weight_decay is not None:
cmd.extend(["--optimizer.weight_decay", str(request.optimizer_weight_decay)])
if request.optimizer_grad_clip_norm is not None:
cmd.extend(["--optimizer.grad_clip_norm", str(request.optimizer_grad_clip_norm)])
# Advanced
cmd.extend(["--use_policy_training_preset", "true" if request.use_policy_training_preset else "false"])
if request.config_path:
cmd.extend(["--config_path", request.config_path])
return cmd