Remove batch-level padding from tokenize_sft_batch#582
Merged
Conversation
tokenize_sft_batch was padding all trajectories to the longest sequence in the batch, but every consumer (unsloth, megatron) processes them individually. This wasted CPU memory and GPU compute on padding tokens. Now each trajectory tensor keeps its natural length. The unsloth training loop strips any residual padding before .to(device) for robustness. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Match the serverless-training microbatch approach: process trajectories in configurable microbatch groups with padding trimmed to the longest in each group. Changing microbatch_size is a one-line change. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
angkywilliam
approved these changes
Feb 25, 2026
src/art/unsloth/service.py
Outdated
| # Process trajectories in microbatches, trimming padding to the | ||
| # longest sequence in each microbatch to avoid wasted GPU compute. | ||
| microbatch_size = 1 | ||
| for i in range(0, len(batch.trajectory_tensors), microbatch_size): |
Collaborator
There was a problem hiding this comment.
Not sure if we need this flow.
We already remove the extra padding logic in the processing flow above.
Collaborator
Author
There was a problem hiding this comment.
You are right, no need to have it. Removed!
Padding is now removed at the source in tokenize_sft_batch, so the training loop doesn't need microbatch trimming logic. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
tokenize_sft_batch: each trajectory tensor now keeps its natural sequence length instead of being padded to the longest in the batchservice.py): trims any trailing padding before.to(device)for robustnessContext
tokenize_sft_batchwas padding all trajectories to the longest sequence in the batch. Since every consumer (unsloth, megatron) processes trajectories individually with gradient accumulation, this padding was pure waste — inflating CPU tensors and forcing extra GPU compute on padding tokens. The megatron trainer already stripped padding on its own.Now padding is eliminated at the source. The unsloth training loop also defensively strips padding before moving to GPU, so it works correctly whether tensors arrive padded or not.