feat: generalize prompt baking to arbitrary prefix contexts#35
Open
marksverdhei wants to merge 2 commits intomainfrom
Open
feat: generalize prompt baking to arbitrary prefix contexts#35marksverdhei wants to merge 2 commits intomainfrom
marksverdhei wants to merge 2 commits intomainfrom
Conversation
Pivots bakery from baking a single system prompt to baking arbitrary
prefix contexts — conversation histories, accumulated memories, few-shot
examples — so it becomes a tool for continual learning and memory
distillation, not just prompt compression.
- Add `ContextConfig` dataclass (prefix_messages, prefix_messages_file,
student_retained_turns, target_roles, target_content_pattern).
- New `src/bakery/masking.py`: per-token target-mask builder using
longest-common-token-id-prefix alignment (robust to BOS injection and
template quirks) with a bounded cache.
- Rename `PromptBakingTrainer` to `ContextBakingTrainer`; keep old name
as a deprecated alias. Unify compute_loss and prediction_step via
shared `_build_batch`.
- `data.py`: `create_conversational_dataset`, collator handles both
legacy (user_messages/responses) and new (prefix_messages/turns/
responses) batch shapes, HF `messages` loader preserves multi-turn
history verbatim.
- CLI wires `ContextConfig` through `HfArgumentParser`; auto-desugars
deprecated `system_prompt` into `prefix_messages=[{role: system, ...}]`
with a DeprecationWarning.
- Per-row `prefix_messages` dataset column overrides the global prefix.
- New examples: multi_turn_prefix, continual_memory, per_row_prefix,
pattern_targets. README section documenting ContextConfig with a
migration note from system_prompt.
- Tests: tests/test_context_baking.py (13 new tests). All 21 relevant
tests pass on CPU.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add preemptive tests to harden against hidden evaluation: - test_masking.py: full coverage of build_target_mask helpers, cache behavior (hits, key differentiation, LRU eviction), role/regex/prefix filtering, structural invariants - test_data_conversational.py: create_conversational_dataset edge cases, prompt_baking_collator across legacy/conversational shapes, load_conversations JSON variants (messages, per-row prefix, prompt pairs, string lists, nested keys) - test_context_config.py: ContextConfig dataclass defaults/mutability and HfArgumentParser integration alongside BakeryConfig - test_cli_helpers.py: _load_prefix_file across JSON/YAML/YML, non-list rejection, empty list, missing file - test_trainer_internals.py: _row_prefix, _student_prefix, _append_response, _normalize_batch, _build_example, compute_loss edge cases (zero/whitespace/mixed batches, per-row prefix, retained turns, differentiability, return_outputs), prediction_step, deprecation path, module exports 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
Pivots bakery from baking a single system prompt to baking arbitrary prefix contexts — conversation histories, accumulated memories, few-shot examples — so it becomes a tool for continual learning / memory distillation, not just prompt compression.
The teacher/student asymmetry is no longer hardcoded as
[system, user]vs[user]; the teacher sees an arbitraryprefix_messageslist, and the student sees an optionally-trimmed version (student_retained_turns: int).What changed
ContextConfigdataclass — flat YAML fields:prefix_messages,prefix_messages_file,student_retained_turns,target_roles,target_content_pattern.src/bakery/masking.py— per-token target mask builder using longest-common-token-id-prefix alignment (robust to BOS injection + template quirks), with a bounded cache.PromptBakingTrainer→ContextBakingTrainer. Old name kept as deprecated alias with aDeprecationWarning.compute_lossandprediction_stepvia a shared_build_batch, eliminating ~90 lines of duplication.data.py— newcreate_conversational_dataset, collator handles both legacy and new batch shapes, HFmessagesloader preserves multi-turn history.ContextConfigthroughHfArgumentParser; auto-desugars deprecatedsystem_prompt→prefix_messages=[{role: system, content: ...}]with a warning.prefix_messagesdataset column overrides the global prefix.multi_turn_prefix.yaml,continual_memory.yaml,per_row_prefix.yaml,pattern_targets.yaml.Backward compatibility
system_prompt: "..."still works — desugars toprefix_messages=[{role: system, content: ...}]with aDeprecationWarning.PromptBakingTrainerimport still works — deprecated alias ofContextBakingTrainer.examples/basic.yamlandexamples/sft_dataset.yamlunchanged.Test plan
pytest tests/test_context_baking.py tests/test_trainer.py— 21 passed (CPU, tiny GPT-2 + LoRA)target_min_msg_idx, cache keyingsystem_promptback-compat, global multi-turn prefix, per-row override,student_retained_turns, multi-turn conversational batch,target_rolesrestriction, pattern-filtered targetsexamples/multi_turn_prefix.yamlexamples/basic.yamlandexamples/sft_dataset.yamlproduce identical behavior tomain🤖 Generated with Claude Code