feat(train): Add SequenceLength support for SFT, DPO, RLVR, RLAIF trainers#5965
Open
guanweim wants to merge 16 commits into
Open
feat(train): Add SequenceLength support for SFT, DPO, RLVR, RLAIF trainers#5965guanweim wants to merge 16 commits into
guanweim wants to merge 16 commits into
Conversation
zachgk
approved these changes
Jun 22, 2026
…iners Add optional sequence_length parameter to all four trainers that enables customers to specify their desired context length for serverless training jobs. The parameter is passed in ServerlessJobConfig for recipe filtering. During trainer initialization, _get_fine_tuning_options_and_model_arn filters recipes by SequenceLength field, picking the smallest recipe with context length >= the requested value. Raises ValueError if no sufficient recipe exists or if recipes lack SequenceLength metadata. Changes: - ServerlessJobConfig: add sequence_length field - _parse_context_length: parse values like '8K' to integers - _get_fine_tuning_options_and_model_arn: filter by SequenceLength - _create_serverless_config: conditionally include sequence_length - SFTTrainer, DPOTrainer, RLVRTrainer, RLAIFTrainer: accept and thread sequence_length through init and train methods - Unit tests for all new functionality
… edit Add SequenceLength to service-2.json and regenerate shapes.py via codegen (python -m sagemaker.core.tools.codegen) instead of editing shapes.py manually.
…config - Move sequence_length filtering above recipe selection to reduce recipes_with_template before existing logic runs - Always pass sequence_length to ServerlessJobConfig (no None guard)
Bare integers like "128" were silently parsed as token counts (128), causing every recipe to pass the >= filter and silently selecting the shortest recipe instead of failing. Now raises ValueError with an actionable message for any input not ending in 'K'.
mohamedzeidan2021
had a problem deploying
to
manual-approval
July 20, 2026 21:06 — with
GitHub Actions
Error
mohamedzeidan2021
had a problem deploying
to
manual-approval
July 20, 2026 21:07 — with
GitHub Actions
Error
Enforce that customer-selected lengths fit the recipe's supported sequence length:
- RL: max_prompt_length + max_response_length <= SequenceLength
- SFT/DPO: max_length (dataset_max_len) <= SequenceLength
FineTuningOptions.validate_length_constraints() checks this and is wired into the
RL/base trainers. The ceiling comes from the recipe's SequenceLength metadata
('<n>K'), parsed to an int via _parse_context_length; no-op when absent.
(Recipes side unified on the SequenceLength metadata field; the former
ContextLength field was dropped, so read SequenceLength here.)
Recipes vend only SequenceLength, so use that name throughout the fine-tuning options and validation path (context_length -> sequence_length, _parse_context_length -> _parse_sequence_length). Filter recipes by exact SequenceLength match and keep all candidates at that length, so _select_recipe_by_training_type picks the right recipe when LORA and FULL variants share the same sequence length. The client sends only descriptors in ServerlessJobConfig (including SequenceLength); the selectable-length cap is derived from the recipe catalog rather than a hardcoded enum.
validate_length_constraints() checked only the RL prompt+response sum, so the single-example ceiling for SFT/DPO recipes was never enforced. verl and llmft recipes vend that ceiling as dataset_max_len; check it against the recipe's SequenceLength so an over-limit value is rejected client-side. Verified against the recipe catalog: dataset_max_len is the param verl/llmft SFT/DPO recipes actually render (max_context_length appears in Nova overrides but is never rendered into a recipe body; max_length is Nova-RFT only).
mohamedzeidan2021
had a problem deploying
to
manual-approval
July 23, 2026 16:47 — with
GitHub Actions
Error
mohamedzeidan2021
had a problem deploying
to
manual-approval
July 23, 2026 16:48 — with
GitHub Actions
Error
mohamedzeidan2021
had a problem deploying
to
manual-approval
July 23, 2026 16:48 — with
GitHub Actions
Error
guanweim
requested a deployment
to
manual-approval
July 24, 2026 17:08 — with
GitHub Actions
Waiting
guanweim
requested a deployment
to
manual-approval
July 24, 2026 17:08 — with
GitHub Actions
Waiting
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.
Issue #, if available:
Description of changes:
Add optional sequence_length parameter to SFTTrainer, DPOTrainer, RLVRTrainer, and RLAIFTrainer for specifying context length in serverless training jobs
Recipes are filtered by SequenceLength field, picking the smallest recipe with context length >= the requested value
Add SequenceLength to service-2.json and regenerate shapes via codegen
Unit tests for _parse_context_length helper (K suffix, lowercase, integer, None, empty)
Unit tests for recipe filtering by sequence length
Unit tests for ValueError when no sufficient context length exists
Unit tests for each trainer passing sequence_length through
Unit tests for _create_serverless_config with/without sequence_length
Integ test for sft recipe with different context length
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Previous pr that were not merged #5898