Commit 31948fb
Enable SFT for HF Transformer backed trainer (#3243)
## Summary
Enables supervised fine-tuning (SFT) for any HuggingFace model in the
transformers_modeling_backend experiment, building on the native SFT
infrastructure from #2556.
- Uses FlexAttention with BlockMask for packed sequences. Builds the
BlockMask in
`post_dataloading_process` (before parallelism sharding) using
TorchTitan's existing
`get_causal_mask_mod` + `get_document_mask_mod` — same mask
infrastructure as native TorchTitan SFT.
- `HFFlexAttention` subclasses TorchTitan's `FlexAttention` kernel
module, handling HF's tensor
layout via transposes. Registered as a submodule on each HF attention
layer so
`apply_cp_to_forward` works naturally — no globals needed.
- Registers `flex_torchtitan` as a custom attention implementation that
bypasses HF's internal
mask creation (which builds at the wrong point for TP+SP) and routes
through a compile-friendly
`flex_attention` call.
- `SFTTrainer` subclass overrides `post_dataloading_process` to build
the BlockMask at full
sequence length before any parallelism touches the data.
- `HFTransformerStateDictAdapter` enables loading pretrained HF weights
(trivial `model.` prefix
mapping, handles weight tying).
- `HFBackendTokenizer` passes `bos_token`/`eos_token` to chat templates
and fixes an edge case
where BOS and EOS share the same token string.
- TitanDenseModelConfig defaults changed to None so HF config values
aren't overridden when using
full-size models.
- No dynamo `cache_size_limit` workaround needed: relies on the upstream
nested-`AsyncCollectiveTensor`
fix (pytorch#186442), so the flex path stays under the default recompile
limit across all parallelisms.
- All changes within
`torchtitan/experiments/transformers_modeling_backend/` — no core
modifications.
## Files changed
| File | Description |
|---|---|
| trainer.py (new) | `SFTTrainer` builds BlockMask before parallelism
sharding, handles CP load balancer |
| state_dict_adapter.py (new) | `model.` prefix strip/add for HF weight
loading, handles weight tying |
| tokenizer.py (new) | Passes special tokens to chat templates, fixes
shared BOS/EOS edge case |
| model.py | `HFFlexAttention` subclass +
`_flex_torchtitan_attention_forward`; `forward()` accepts
positions/attention_masks; guard
`intermediate_size`/`head_dim` recalculation; disable
`attention_dropout` for FlexAttention |
| configs.py | `build()` override to use `SFTTrainer` |
| parallelize.py | Collects `HFFlexAttention` modules and calls
`apply_cp_to_forward` for CP |
| __init__.py | `sft_debugmodel`/`sft_full` flavors with
`attn_implementation="flex_torchtitan"`; wire `state_dict_adapter`; None
defaults for
TitanDenseModelConfig |
| config_registry.py | SFT config functions (random init + pretrained
weight loading) using `ChatDataLoader` and `HFBackendTokenizer` |
| tests/integration_tests.py | 2-GPU SFT integration test |
## Parallelism support
| Parallelism | Status |
|---|---|
| FSDP | Works |
| FSDP + TP | Works |
| FSDP + TP + PP | Works |
| FSDP + AC (selective/full) | Works |
| FSDP + compile | Works |
| FSDP + TP + compile | Works |
| FSDP + CP | Works |
| CP + compile | Works |
| TP + CP | Works |
| FSDP + TP + CP | Works |
| FSDP + TP + CP + compile | Works |
## Test plan
- [x] Pretraining regression: `transformers_modeling_backend_debugmodel`
identical loss before/after
- [x] SFT debugmodel (Qwen3 arch): loss 11.0 → 3.3, 2 GPUs
- [x] SFT debugmodel (Mistral arch): loss 10.9 → 8.3 —
architecture-agnostic
- [x] SFT with pretrained Qwen3-0.6B: loss 13.4 → 6.1
- [x] SFT with pretrained Qwen2.5-0.5B-Instruct: loss 5.3 → 1.7
- [x] SFT with pretrained Llama-3.2-1B-Instruct: loss 7.6 → 0.0001
- [x] SFT with pretrained Seed-Coder-8B-Instruct: loss 6.8 → 2.1
- [x] SFT with pretrained Granite-3.1-2B-Instruct: loss 50.2 → 12.2
- [x] FSDP + TP (4 GPU): works
- [x] FSDP + TP + PP (8 GPU): works
- [x] FSDP + compile (2 GPU): works
- [x] FSDP + TP + compile (4 GPU): works
- [x] FSDP + CP (2 GPU): works
- [x] CP + compile (2 GPU): works
- [x] TP + CP (4 GPU): works
- [x] FSDP + TP + CP (8 GPU): works
- [x] FSDP + TP + CP + compile (8 GPU, full 28-layer qwen3_0.6b): works
— 0 cache-limit hits at default recompile limit
- [x] Reproducibility: compare loss curves against native SFT on same
model/data
- [x] SFT loss/KL parity vs native (#2556), qwen3_0.6b, same weights +
packed batch: max |loss diff| 3.4e-06, max KL 5.0e-09
Numeric verification results:
### Native TorchTitan vs HuggingFace — logit parity (qwen3_0.6b, same
weights + input, fp32, 161 positions)
| metric | value |
|---|---|
| top1 match | 100.0000% |
| top5 overlap | 100.0000% |
| KL(HF ‖ native) | 1.60e-06 |
| KL(native ‖ HF) | 6.78e-07 |
| max \|logit diff\| | 9.06e-05 |
| mean \|logit diff\| | 5.14e-06 |
| cosine similarity | 1.000000 |
---------
Co-authored-by: Hossein Kavianihamedani <hosseinkh@fb.com>1 parent 1d2d58c commit 31948fb
8 files changed
Lines changed: 393 additions & 17 deletions
File tree
- torchtitan/experiments/transformers_modeling_backend
- tests
Lines changed: 16 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
62 | 72 | | |
63 | 73 | | |
64 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
65 | 81 | | |
66 | 82 | | |
67 | 83 | | |
| |||
Lines changed: 100 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
| 24 | + | |
21 | 25 | | |
22 | 26 | | |
23 | 27 | | |
| |||
81 | 85 | | |
82 | 86 | | |
83 | 87 | | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
Lines changed: 6 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
Lines changed: 123 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
| 23 | + | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
23 | 27 | | |
24 | 28 | | |
25 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
26 | 108 | | |
27 | 109 | | |
28 | 110 | | |
| |||
178 | 260 | | |
179 | 261 | | |
180 | 262 | | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
185 | 275 | | |
186 | 276 | | |
187 | 277 | | |
| |||
264 | 354 | | |
265 | 355 | | |
266 | 356 | | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
267 | 365 | | |
268 | 366 | | |
269 | 367 | | |
| |||
363 | 461 | | |
364 | 462 | | |
365 | 463 | | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
366 | 473 | | |
367 | 474 | | |
368 | 475 | | |
| |||
656 | 763 | | |
657 | 764 | | |
658 | 765 | | |
659 | | - | |
| 766 | + | |
660 | 767 | | |
661 | 768 | | |
662 | 769 | | |
663 | 770 | | |
664 | 771 | | |
665 | 772 | | |
666 | | - | |
667 | | - | |
668 | | - | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
669 | 784 | | |
670 | 785 | | |
671 | 786 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
104 | 113 | | |
105 | 114 | | |
106 | 115 | | |
| |||
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
37 | 48 | | |
38 | 49 | | |
39 | 50 | | |
| |||
0 commit comments