Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion docs/user-guide/rewards.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For `--custom-rm-path`, `--custom-reward-post-process-path`, and other

| Stage | Flag | Role |
|---|---|---|
| Reward type | `--rm-type` | Selects built-in scorer (`pickscore`, `hps`, `hpsv3`, `ocr`, `openai_api`, `custom_api`), or `none` for a constant 0 when the loss reads no reward (DMD2), which also skips decoding training rollouts; ignored when `--custom-rm-path` is set |
| Reward type | `--rm-type` | Selects built-in scorer (`pickscore`, `hps`, `hpsv3`, `ocr`, `openai_api`, `custom_api`, `videoalign`), or `none` for a constant 0 when the loss reads no reward (DMD2), which also skips decoding training rollouts; ignored when `--custom-rm-path` is set |
| API reward config | `--openai-api-rm-config`, `--custom-api-rm-config` | YAML file or inline `base64:<payload>` for the two API rewards |
| Per-sample override | `metadata.rm_type` in JSONL | Overrides global `--rm-type` |
| Custom reward / norm | see [Customization](customization.md) | `--custom-rm-path`, `--custom-reward-post-process-path` |
Expand Down Expand Up @@ -141,6 +141,66 @@ Example from `scripts/run_diffusion_grpo_sd3_hpsv3_sglang.py`:
--hpsv3-reward-colocate
```

### VideoAlign (`--rm-type videoalign`)

Implementation: `miles/rollout/rm_hub/videoalign.py`.

VideoAlign / [VideoReward](https://github.com/KwaiVGI/VideoAlign) scores a whole generated video against its prompt with a Qwen2-VL-2B-Instruct fine-tuned as a preference model:

- Model: `Qwen/Qwen2-VL-2B-Instruct` + a linear `rm_head` read at three added reward tokens `<|VQ_reward|>` / `<|MQ_reward|>` / `<|TA_reward|>`
- Checkpoint: `--videoalign-checkpoint-path` (optional; defaults to `KwaiVGI/VideoReward`, downloaded on first use)

One forward returns three raw scores — Visual Quality, Motion Quality, Text Alignment. With the
published `KwaiVGI/VideoReward` normalisation constants (`--videoalign-no-normalize` turns it off), each sample's
reward is a dict:

```
{"vq": ..., "mq": ..., "ta": ..., "overall": vq + mq + ta}
```

`--reward-key` picks the entry GRPO trains on (e.g. `--reward-key overall`, or `--reward-key ta` for
text-alignment only); every key also gets its own `rollout/reward/<name>_mean` panel. The actor scores
`--videoalign-num-frames` evenly spaced frames per video (None = every frame).

| Flag | Default | Description |
|---|---|---|
| `--videoalign-num-workers` | 1 | Ray actor count |
| `--videoalign-num-gpus-per-worker` | 1.0 | GPU per worker (non-colocate) |
| `--videoalign-batch-size` | 4 | Batch size per actor call |
| `--videoalign-checkpoint-path` | None | `KwaiVGI/VideoReward` repo id or local dir with `checkpoint-11352/model.pth` |
| `--videoalign-model-path` | None | Qwen2-VL-2B base model (defaults to `Qwen/Qwen2-VL-2B-Instruct`) |
| `--videoalign-num-frames` | None | Evenly spaced frames scored per video (None = every frame) |
| `--videoalign-max-pixels` | 200704 | Max pixels per frame for the visual encoder |
| `--videoalign-no-normalize` | False | Return raw VQ/MQ/TA instead of the checkpoint's normalisation |
| `--videoalign-reward-colocate` | False | Seat actors on rollout GPUs, one per placement-group bundle (requires `--colocate`) |

#### LTX-2.3 Flow-GRPO smoke recipe

`scripts/run_diffusion_grpo_ltx23_videoalign_sglang.py` keeps the existing LTX-2.3
PickScore recipe's generator and optimizer settings, but selects VideoAlign:

- Four colocated train/rollout GPUs plus one dedicated reward GPU.
- Text-only prompts from `rockdu/miles-diffusion-datasets`, `flowgrpo_pickscore/train.jsonl`.
- 8 prompts × 8 samples = 64 videos per rollout, at 768×512, 57 frames, 24 FPS and 24 denoising steps.
- VideoAlign jointly scores 8 uniformly sampled frames, batch size 4.
- `--reward-key overall` optimizes the sum of normalized VQ, MQ and TA.
- Defaults to two rollouts (128 videos); this is an integration test configuration, not a verified training curve.

A local two-rollout run completes end-to-end: both rollouts return finite `overall`
rewards (raw mean ≈ -3.54 / -3.64; early LTX-2.3 videos score negatively on temporal/
text alignment), four optimizer steps execute, and checkpoints are written. Training
metrics match the registered LTX-2.3 PickScore e2e: `log_prob_new_idx_0` / `log_prob_old_idx_0`
agree to ~5-6 decimal places and `grad_norm` stays in the baseline ~1e-5 range. Two
rollouts are not enough to show reward improvement; treat this as a reward-integration
smoke gate, not a convergence curve.

The GPU alignment tests compare input IDs, video grids, pixel hashes and bf16 scores
against upstream revision `219ab9db64c045e5181a2202d11f686439351292`, including a padded batch.
They do not establish fp32 parity, training convergence or output-store integration.

Like other example scripts, the launcher uses `execute_train`, which performs machine-wide
process cleanup. Run it only on a dedicated allocation; do not use it unchanged on a shared host.

### Reward placement

Every GPU reward pool is placed one of two ways:
Expand Down
12 changes: 12 additions & 0 deletions miles/rollout/rm_hub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ async def async_rm(args, sample: Sample, **kwargs):
from .openai_api import openai_api_rm

return (await openai_api_rm(args, [sample]))[0]
elif rm_type == "videoalign":
from .videoalign import videoalign_rm

return (await videoalign_rm(args, [sample]))[0]
else:
raise NotImplementedError(f"Rule-based RM for {rm_type!r} is not implemented.")

Expand All @@ -57,6 +61,10 @@ def create_colocated_reward_pools(args, placement_group, slots) -> list:
from .hpsv3 import AsyncHPSv3Pool

pools.append(AsyncHPSv3Pool(args, placement_group=placement_group, slots=slots))
if args.videoalign_reward_colocate:
from .videoalign import AsyncVideoAlignPool

pools.append(AsyncVideoAlignPool(args, placement_group=placement_group, slots=slots))
return pools


Expand Down Expand Up @@ -97,6 +105,10 @@ async def batched_async_rm(
from .openai_api import openai_api_rm

return await openai_api_rm(args, samples)
if all(rm_type == "videoalign" for rm_type in rm_types):
from .videoalign import videoalign_rm

return await videoalign_rm(args, samples)

tasks = [async_rm(args, sample, **kwargs) for sample in samples]
rewards = await asyncio.gather(*tasks)
Expand Down
21 changes: 5 additions & 16 deletions miles/rollout/rm_hub/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, *, scorer_cls, scorer_kwargs: dict, args) -> None:
self._scorer = scorer_cls(**scorer_kwargs)
self._reader = StoredTensorReader(connect_output_store(args))

def score_batch(self, outputs: list[GeneratedMedia], prompts: list[str]) -> list[float]:
def score_batch(self, outputs: list[GeneratedMedia], prompts: list[str]) -> list[float | dict[str, float]]:
# eval outputs arrive inline
stored_visuals = [output.visual for output in outputs if isinstance(output.visual, StoredTensor)]
if stored_visuals:
Expand Down Expand Up @@ -189,7 +189,7 @@ def _next_actor_idx(self) -> int:
self._round_robin_index += 1
return i

async def score(self, outputs: list[GeneratedMedia], prompts: list[str]) -> tuple[list[float], int]:
async def score(self, outputs: list[GeneratedMedia], prompts: list[str]) -> tuple[list[float | dict[str, float]], int]:
"""Score samples in batches; also report the deepest dispatch-time backlog this call saw."""
outputs = [self._only_required_fields(output) for output in outputs]
refs, idxs, max_queue_depth = [], [], 0
Expand All @@ -207,17 +207,6 @@ async def score(self, outputs: list[GeneratedMedia], prompts: list[str]) -> tupl
finally:
for i in idxs:
self._inflight[i] -= 1
return [float(score) for chunk in chunked_scores for score in chunk], max_queue_depth

def _only_required_fields(self, output: GeneratedMedia) -> GeneratedMedia:
"""``output`` without the fields this pool's scorer does not require, so they neither cross Ray nor are read
from the store."""
if self._requires_visual and output.visual is None:
raise ValueError(f"the {self._name} reward requires visual, but a sample generated none")
if self._requires_audio and output.audio is None:
raise ValueError(f"the {self._name} reward requires audio, but a sample generated none")
return replace(
output,
visual=output.visual if self._requires_visual else None,
audio=output.audio if self._requires_audio else None,
)
return [
score if isinstance(score, dict) else float(score) for chunk in chunked_scores for score in chunk
], max_queue_depth
Loading