[Bug] Per-sample train_metadata is lost during conversion and DP transport
Bug description
At 5bae5bb7928d65906e24e99f5b5d3f99a4daaf93, per-sample training metadata is lost at two points in RolloutManager:
_convert_samples_to_train_data()only checkssamples[0].train_metadata. If the first sample hasNoneand a later sample has metadata, the entire field is omitted.- When conversion does produce
data["metadata"],_split_train_data_by_dp()omitsmetadatafrom the per-sample fields it partitions and sends to the trainer. Consequently the field disappears even when every sample provided it.
The customization documentation lists metadata as a supported return field of --custom-convert-samples-to-train-data-path (docs/en/get_started/customization.md). This also affects the built-in Sample.train_metadata conversion path. A training-side rollout-data postprocessing hook cannot access those fields; a hook that uses defaults may silently apply the wrong per-sample settings.
Reproduction
Use a four-sample batch, global_batch_size=4, with unique metadata identifying each sample:
samples[i].train_metadata = {"sample_id": i, "loss_weight": i + 0.5}
data = manager._convert_samples_to_train_data(samples)
assert data["metadata"] == [sample.train_metadata for sample in samples]
refs = manager._split_train_data_by_dp(data)
local = process_rollout_data(refs, dp_rank=0, dp_size=dp_size)
assert "metadata" in local # fails: field was not included in the payloadAlso set samples[0].train_metadata = None while leaving metadata on later samples: the first assertion fails because conversion already removed the field.
Expected behavior
Include the metadata list if any sample supplies it; retain None placeholders for other samples. Partition it with the same sample indices as tokens and rewards. Omit the optional field when every sample has None.
Validation
Regression tests exercise the real conversion, DP scheduler, tensorization and training-side process_rollout_data functions, with Ray put/get replaced by identity operations and unused serving/telemetry imports stubbed. The matrix covers DP sizes 1/2, static/dynamic batching, and complete/mixed/absent metadata.
On unmodified production code: 8 failed, 12 passed, with all failures caused by missing metadata. A two-line production fix makes all 20 tests pass, including the eight existing transport tests. No real distributed cluster or GPU run was performed.
Environment: macOS arm64, Python 3.12.13, PyTorch 2.14.0, pytest 9.1.1. I read CONTRIBUTING.md and searched issues/PRs for train_metadata, lost metadata and metadata partitioning; no matching fix was found.
Source: THUDM/slime