xtuner icon indicating copy to clipboard operation
xtuner copied to clipboard

[Refactor] refactor packing in RL train controller and train worker

Open YanhuiDua opened this issue 11 months ago • 2 comments

Motivation

Current xtuner data distribution mechanism has a pack allocation issue that leads to unstable training steps and affects training effectiveness.

The data distribution pipeline consists of three stages:

  1. Packing Stage: Split input data_batch by token count, creating one pack per 32K tokens, resulting in N packs
  2. Distribution Stage: Evenly distribute N packs across M workers, each worker receives N/M packs
  3. Step Division: Divide packs per worker into steps based on optimizer_step parameter

When N/M is not divisible by optimizer_step, the actual training steps fail to match the expected value. For example:

N/M = 44                          # packs per worker
optimizer_step = 16               # expected training steps
packs_per_step = ⌈44/16⌉ = 3      # packs allocated per step

# Actual result:
actual_steps = ⌊44/3⌋ = 14        # complete steps
# Total: 15 steps with inconsistent batch sizes

Key Changes: Use the RLDataPacker module in TrainingController

Use the DataBatchPacker module in TrainingController to pack data batches. Note that DataBatchPacker was introduced in #1438. DataBatchPacker support pack strategy in ["balance", "greedy", "natice"]

YanhuiDua avatar Dec 24 '25 12:12 YanhuiDua

Motivation

Current xtuner data distribution mechanism has a pack allocation issue that leads to unstable training steps and affects training effectiveness.

The data distribution pipeline consists of three stages:

  1. Packing Stage: Split input data_batch by token count, creating one pack per 32K tokens, resulting in N packs
  2. Distribution Stage: Evenly distribute N packs across M workers, each worker receives N/M packs
  3. Step Division: Divide packs per worker into steps based on optimizer_step parameter

When N/M is not divisible by optimizer_step, the actual training steps fail to match the expected value. For example:

N/M = 44                          # packs per worker
optimizer_step = 16               # expected training steps
packs_per_step = ⌈44/16⌉ = 3      # packs allocated per step

# Actual result:
actual_steps = ⌊44/3⌋ = 14        # complete steps
# Total: 15 steps with inconsistent batch sizes

Key Changes

This PR refactors the pipeline to: Allocate → Pack & Pad and wrappers some methods from TrainController and TrainWorker

  1. Token-aware pre-allocation:Evenly distribute samples into M workers (optional) × optimizer_step buckets based on token count
  2. Pack & pad per bucket: Apply packing and padding within each pre-allocated bucket

Great PR description! Maybe you can add the calling chain of core packing functions responding to the workflow in key changes, such as

controller.py: 
RawTrainingController.fit() 
# 1. Token-aware pre-allocation:Evenly distribute samples into M workers (optional) × optimizer_step buckets based on token count
-> batches_per_dp_group = self._balance_split_batch(data_batches, dp_size)
-> mini_batch_for_steps = self._balance_split_batch(dp_worker_data_batches, optimizer_steps)
# 2. Pack & pad per bucket: Apply packing and padding within each pre-allocated bucket
-> batch4pack_list = self._rearrange_batch_for_pack(step_mini_batch, pack_max_length)   # the old version: pack_mini_batch = self._pack(step_mini_batch, pack_max_length)
-> self._pack_batches()  # pieces of packing code which is better to be wrapped in the new function `_pack_batches()`
worker.py:
-> self._create_padding_sample()  # pieces of padding code which is better to be wrapped in a new function `_pad_batches()`
# 3. use the packed padded data batches
->TrainingWorker.fit()

Then I can review easily as the same order above : )

Additionally, when you write the core function calling chain responding to your original design in the "Key Changes", you will find that there are some high-level functions missing in your implementation, just like the _pad_batches(). If you add the high-level function, then you can tell others more clearly, and others can read code more easily, because code readers can read and think this in the high level ignoring the messy details.

The unit test can play the same role sometimes. For example, if you want to write unit test to test the core padding function, then you need to abstract the related code pieces into the function _pad_batches() and test it.

jayhenry avatar Dec 26 '25 03:12 jayhenry

rl_trainer 里面有 random.shuffle(data_batches) 要删掉

hhaAndroid avatar Jan 06 '26 06:01 hhaAndroid