feat: add --sft-assistant-turns option for multi-turn SFT - #313
Open
Liu14159 wants to merge 1 commit into
Open
Conversation
Liu14159
force-pushed
the
feat/sft-assistant-turns
branch
5 times, most recently
from
July 30, 2026 08:38
8519faf to
c701277
Compare
…sionAI#224) Add the ability to train on every assistant turn (all, default) or only the final assistant turn (last) in multi-turn chat data. Changes: - areno/api/trainer_config.py: add sft_assistant_turns field with validation - areno/api/data_utils.py: add messages_to_tokens_and_mask with last_assistant_only, chat-template encoding with prefix-stability guard and plain-text fallback - areno/api/trainers/sft.py: integrate multi-turn messages format and turn selection - areno/cli/train.py: expose --sft-assistant-turns CLI option - tests/test_trainer_dataset_utils_cpu.py: 13 new CPU tests - docs/cli/training.rst, docs/cli/dataset_loaders.rst: document the option - examples/sft/multiturn/: runnable example with dataset loader and README Edge cases handled: messages=None, empty messages, no assistant turn, invalid sft_assistant_turns value, single-turn backward compat, tokenizer chat_template not prefix-stable (warn + fallback). Default is 'all' to preserve backward compatibility.
Liu14159
force-pushed
the
feat/sft-assistant-turns
branch
from
August 4, 2026 03:21
c701277 to
5654897
Compare
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
Add
--sft-assistant-turns [all|last]option for multi-turn SFT, allowing users to train on every assistant turn (defaultall) or only the final assistant turn (last). Earlier assistant turns inlastmode are treated as context. User, system, and tool-result tokens are always excluded from the loss.Usage
Multi-turn data format (JSONL, one conversation per line):
{"messages": [{"role": "user", "content": "What is 2+2?"}, {"role": "assistant", "content": "4"}, {"role": "user", "content": "And 3+3?"}, {"role": "assistant", "content": "6"}]}Related issue
Fixes #224
Type of change
What changed
areno/api/trainer_config.pysft_assistant_turnsfield with__post_init__validationareno/api/data_utils.pymessages_to_tokens_and_mask()withlast_assistant_onlysupportareno/api/trainers/sft.pymessagesformat +sft_assistant_turnspassthrough into_record_to_train_sequenceareno/cli/train.py--sft-assistant-turnsCLI option, config summary, dashboard settingstests/test_trainer_dataset_utils_cpu.pyMultiTurnSFTMaskTest)docs/cli/training.rstdocs/cli/dataset_loaders.rstmessagesformat documentationexamples/sft/multiturn/README.md+dataset_loader.pyEdge cases handled
{"messages": None}None(filtered out){"messages": []}None(empty, no trainable tokens)None(nothing to train)sft_assistant_turnsvalueValueErrorwith clear messageprompt/responserowssft_assistant_turnshas no effectmessagesnorprompt/responseValueErrorwith schema hintlastmode with single assistant turnallmodeDesign decisions
True= do not train (context),False= train (assistant response). Matches existingprompt_response_to_tokens_and_maskcontract.all: preserves backward compatibility — existing single-turn SFT runs are unaffected.messages[:i+1]) to attribute tokens to individual turns. O(N) calls toapply_chat_templatefor N messages; acceptable for typical SFT data (2-10 turns).chat_template, concatenatesrole: contentper turn.data_utils,tokenizer, andTrainerConfigcontracts.How was it tested?
Tested on Kaggle (Python 3.12, T4 GPU,
ARENO_BUILD_EXT=0):25 tests collected, all passed. Config validation and CLI option verified separately:
Checklist
pytest tests/ -k cpu).