Skip to content

Commit 1125482

Browse files
committed
Expose flex_attention kernel options in DFlash and Domino training
1 parent e1f10ff commit 1125482

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

scripts/train_dflash.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import argparse
66
import functools
7+
import json
78
import logging
89
import math
910
import os
@@ -67,6 +68,12 @@ def parse_args():
6768
choices=["eager", "sdpa", "flex_attention"],
6869
help="Attention backend for draft model.",
6970
)
71+
model_group.add_argument(
72+
"--flex-kernel-options-json",
73+
type=json.loads,
74+
default=None,
75+
help="JSON dict forwarded as kernel_options when attention-backend=flex_attention.",
76+
)
7077
model_group.add_argument(
7178
"--trust-remote-code", action="store_true", help="Trust remote code"
7279
)
@@ -375,6 +382,17 @@ def main():
375382
)
376383

377384
args = parse_args()
385+
flex_kernel_options = args.flex_kernel_options_json
386+
if flex_kernel_options is not None:
387+
if args.attention_backend != "flex_attention":
388+
raise ValueError(
389+
"--flex-kernel-options-json can only be used when "
390+
"--attention-backend is 'flex_attention'."
391+
)
392+
if not isinstance(flex_kernel_options, dict):
393+
raise ValueError(
394+
"--flex-kernel-options-json must decode to a JSON object."
395+
)
378396
set_seed(args.seed)
379397

380398
init_distributed(timeout=args.dist_timeout, tp_size=args.tp_size)
@@ -460,6 +478,7 @@ def main():
460478
loss_decay_gamma=args.loss_decay_gamma,
461479
loss_type=args.loss_type,
462480
dpace_alpha=args.dpace_alpha,
481+
flex_kernel_options=flex_kernel_options,
463482
)
464483

465484
# Wrap each transformer block as its own FSDP unit so that all-gather /

scripts/train_domino.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""Domino Training Script."""
44

55
import argparse
6+
import json
67
import logging
78
import math
89
import os
@@ -64,6 +65,12 @@ def parse_args():
6465
choices=["eager", "sdpa", "flex_attention"],
6566
help="Attention backend for draft model.",
6667
)
68+
model_group.add_argument(
69+
"--flex-kernel-options-json",
70+
type=json.loads,
71+
default=None,
72+
help="JSON dict forwarded as kernel_options when attention-backend=flex_attention.",
73+
)
6774
model_group.add_argument(
6875
"--trust-remote-code", action="store_true", help="Trust remote code"
6976
)
@@ -444,6 +451,17 @@ def main():
444451
)
445452

446453
args = parse_args()
454+
flex_kernel_options = args.flex_kernel_options_json
455+
if flex_kernel_options is not None:
456+
if args.attention_backend != "flex_attention":
457+
raise ValueError(
458+
"--flex-kernel-options-json can only be used when "
459+
"--attention-backend is 'flex_attention'."
460+
)
461+
if not isinstance(flex_kernel_options, dict):
462+
raise ValueError(
463+
"--flex-kernel-options-json must decode to a JSON object."
464+
)
447465
set_seed(args.seed)
448466

449467
init_distributed(timeout=args.dist_timeout, tp_size=args.tp_size)
@@ -528,6 +546,7 @@ def main():
528546
num_anchors=args.num_anchors,
529547
loss_decay_gamma=args.loss_decay_gamma,
530548
shift_label=draft_model.shift_label,
549+
flex_kernel_options=flex_kernel_options,
531550
)
532551

533552
domino_model = FSDP(

specforge/core/dflash.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding=utf-8
22
"""DFlash Training Wrapper."""
33

4-
from typing import Optional, Tuple
4+
from typing import Dict, Optional, Tuple
55

66
import torch
77
import torch.nn as nn
@@ -117,6 +117,7 @@ def __init__(
117117
loss_decay_gamma: Optional[float] = None,
118118
loss_type: str = "dflash",
119119
dpace_alpha: float = 0.5,
120+
flex_kernel_options: Optional[Dict] = None,
120121
):
121122
super().__init__()
122123
if loss_type not in _VALID_LOSS_TYPES:
@@ -136,6 +137,7 @@ def __init__(
136137
self.loss_decay_gamma = loss_decay_gamma
137138
self.loss_type = loss_type
138139
self.dpace_alpha = dpace_alpha
140+
self.flex_kernel_options = flex_kernel_options
139141

140142
self._cached_block_mask: Optional[BlockMask] = None
141143
self._cached_seq_len: Optional[int] = None
@@ -304,11 +306,19 @@ def forward(
304306
device=device,
305307
)
306308

309+
draft_forward_kwargs = {}
310+
if (
311+
self.attention_backend == "flex_attention"
312+
and self.flex_kernel_options is not None
313+
):
314+
draft_forward_kwargs["kernel_options"] = self.flex_kernel_options
315+
307316
output_hidden = self.draft_model(
308317
position_ids=full_position_ids,
309318
noise_embedding=noise_embedding,
310319
target_hidden=hidden_states,
311320
attention_mask=dflash_attn_mask,
321+
**draft_forward_kwargs,
312322
)
313323

314324
logits = self.lm_head(output_hidden)

specforge/core/domino.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def __init__(
4444
num_anchors: int = 512,
4545
loss_decay_gamma: Optional[float] = None,
4646
shift_label: bool = False,
47+
flex_kernel_options: Optional[Dict] = None,
4748
):
4849
super().__init__()
4950
self.draft_model = draft_model
@@ -55,6 +56,7 @@ def __init__(
5556
self.num_anchors = num_anchors
5657
self.loss_decay_gamma = loss_decay_gamma
5758
self.shift_label = shift_label
59+
self.flex_kernel_options = flex_kernel_options
5860

5961
self._cached_block_mask: Optional[BlockMask] = None
6062
self._cached_seq_len: Optional[int] = None
@@ -332,11 +334,19 @@ def forward(
332334
device=device,
333335
)
334336

337+
draft_forward_kwargs = {}
338+
if (
339+
self.attention_backend == "flex_attention"
340+
and self.flex_kernel_options is not None
341+
):
342+
draft_forward_kwargs["kernel_options"] = self.flex_kernel_options
343+
335344
output_hidden = self.draft_model(
336345
position_ids=full_position_ids,
337346
noise_embedding=noise_embedding,
338347
target_hidden=hidden_states,
339348
attention_mask=dflash_attn_mask,
349+
**draft_forward_kwargs,
340350
)
341351

342352
# --- Labels ---

0 commit comments

Comments
 (0)