# 🧪 Training Core Examples [Project overview](../README.md) · [Inference guide](../docs/inference.md) · [Objective specification](specification.json) Inspect how RoboGesture learns from speech and motion: this small, runnable package exposes **Stage 0 audio pretraining** and **Stage 1 joint flow-matching updates** using RoboGesture's checkpoint-compatible `MotionModel`. The boundary is deliberately narrow: **Prepared tensors → forward pass → objective → backward pass → optimizer step.** Datasets, preprocessing, annotation, motion retargeting, audio token extraction, statistics computation, training/validation splits and full training orchestration are not included. The examples do not train Mimi or Qwen/LoRA or access robot control. Run them with `python -m training_examples.demo`; the `robogesture` CLI provides `infer`, `stream` and `render`. ## Try it without data or model downloads After the [environment setup](../README.md#quick-start), run from the repository root: ```bash .venv/bin/python -m training_examples.demo ``` The default runs two steps of each stage on CPU, using a smaller configuration of the `MotionModel` class and random tensors. It uses 54 semantic classes, 41 motion coordinates and deployment window shapes. Every model starts with random parameters; **no checkpoint is read or written**, and no cloud or robot is accessed. The JSON output reports individual losses, gradient norms, learning rates, changed parameter probes and an unchanged frozen-parameter probe. Success means finite, nonzero gradients and the expected parameter updates. It does not establish convergence, meaningful generated motion or reproduction of paper results. To exercise the full deployment architecture on a GPU: ```bash .venv/bin/python -m training_examples.demo \ --device cuda --size deployed --batch-size 1 --steps 1 ``` `deployed` selects the full architecture, **not** the released checkpoint. You can also select `--stage 0` or `--stage 1`, change `--seed`, or choose the number of updates per stage with `--steps`. ## What the two stages do | Stage | Trainable modules | Objective and update | | --- | --- | --- | | 0: audio pretraining | Two token embeddings, audio encoder, semantic head and beat head | `10 × beat MSE + semantic CE`; AdamW at `1e-4`; no gradient clipping | | 1: joint motion training | All motion-path modules and the audio/semantic modules; beat head frozen | Weighted flow loss + `0.5 × kinetic loss + 0.5 × semantic CE`; two AdamW learning rates; gradient norm clipped to 1 | These are the example defaults, not a complete experimental recipe for the paper. AdamW uses its PyTorch defaults, including weight decay `0.01` and betas `(0.9, 0.999)`. The examples use FP32 without a learning-rate scheduler or AMP. Stage 1 uses the following objectives and settings: - **Time and noise:** `t = sigmoid(N(0,1))`, Gaussian `x_0`, `x_t = t × x_1 + (1−t) × x_0`, with velocity target `x_1 − x_0`. - **Hand emphasis:** coordinate slices `10:22` and `29:41` receive weight `4`. - **Frame emphasis:** supplied frame weights multiply per-element losses before an ordinary mean; the loss is not divided by the sum of those weights. - **Kinetic term:** compare adjacent-frame differences of the reconstructed endpoint `x_t + (1−t) × predicted_velocity` and the target motion. Neighboring frame weights are averaged. This is not a velocity derivative divided by time. - **Semantic weighting:** class index `0` receives weight `0.6`. Labels use numeric class IDs; class names and the private label vocabulary are not included. - **History masking:** one CPU Bernoulli draw with probability `0.15` masks the entire batch's history to normalized zero. Audio tokens are not masked; this is not per-sample dropout. - **Learning rates:** audio encoder, semantic head and token embeddings use `5e-6`; the remaining trainable motion-path parameters use `3e-4`. ## Prepared batch contract All tensors must already be on the same device as the FP32 model. The examples validate shapes, types and finite values; they do not convert or prepare data. | Key | Stage | Type / shape | Meaning | | --- | --- | --- | --- | | `audio_tokens` | Both | int64 `[B,38,2]` | A roughly three-second Mimi-token context; the first two codebooks, IDs `0…2047` | | `semantic_label` | Both | int64 `[B]` | Class index `0…53` in the deployment vocabulary | | `beat_target` | 0 | float32 `[B,38,2]` | Already aligned/scaled motion-change strength and audio-onset strength, in that order | | `past_motion` | 1 | float32 `[B,60,41]` | Two seconds of already normalized 30 FPS joint history | | `target_motion` | 1 | float32 `[B,30,41]` | The following second of already normalized motion | | `frame_weight` | 1 | float32 `[B,30]` | Nonnegative, already supplied per-target-frame importance weights | Motion follows the model's 41-coordinate ordering, not the 30-channel robot command layout or the complete 60-column replay NPZ. Raw joint coordinates are angular quantities; the accepted normalized coordinates are dimensionless. Use the normalization convention associated with the intended checkpoint. The training input convention also bounds normalized motion; preprocessing and statistics computation are not part of this release. Normalized zero is not necessarily a physical zero-angle pose. The core intentionally does **not** call `model.normalize_motion()` on these already normalized batches. It never computes or changes `qpos_mean` or `qpos_std`. Labels, beat targets and frame weights must come from the caller; the random fixtures are only demonstrations of the input contract. The 15 bundled NPZ/WAV demos are replay examples, not training shards. They are not loaded by this package or presented as a training dataset. ## Read or reuse the core - [losses.py](losses.py): flow targets, Stage 0 losses and weighted Stage 1 losses. - [steps.py](steps.py): stage-specific parameter selection, forward passes, gradient checks, clipping and optimizer updates. - [demo.py](demo.py): synthetic inputs and an observable two-stage smoke test. For a caller that already owns a model and prepared batches, the API is: ```python from training_examples.steps import configure_stage0, configure_stage1 from training_examples.steps import stage0_step, stage1_step # model, audio_batch and motion_batch are supplied by the caller. optimizer = configure_stage0(model) audio_metrics = stage0_step(model, optimizer, audio_batch) optimizer = configure_stage1(model) # Clears stale gradients and resets Adam state. motion_metrics = stage1_step(model, optimizer, motion_batch) ``` Switching stages reuses learned parameters in memory but creates a fresh optimizer. Do not reuse a Stage 0 optimizer for a Stage 1 update. Construct the model on its intended device before configuring the optimizer. ## Implementation notes These examples demonstrate RoboGesture's two-stage training objectives with its checkpoint-compatible deployment model. [specification.json](specification.json) records the public tensor contract, objective settings and release scope. `MotionModel` includes the input/output projections used by Stage 1, which calls the model's forward method directly. Stage 0 calls its audio encoder, semantic head and beat head. Both stages use the model's shared encoder layers. The stage factories omit inactive/frozen parameters from the optimizer and clear stale gradients when switching stages. The examples fail explicitly on invalid batches, nonfinite loss or nonfinite gradients before the optimizer advances. No distributed launcher, dataloader, dataset paths, evaluation loop or checkpoint-saving policy is included. This is a **training-core example**, not a turnkey trainer or a full experimental reproduction package. ## Checks ```bash .venv/bin/python -m unittest discover -s tests -p 'test_training_core.py' -v ``` Tests cover objective values and gradients, reference parameter updates, stage freezing/switching, history masking, input validation and checkpoint-free execution. Current acceptance evidence is in the [training-core verification section](../docs/verification.md#training-core-verification).