diff --git a/specforge/algorithms/common/dflash_family_model.py b/specforge/algorithms/common/dflash_family_model.py index 1f3106517..2c0bdfe62 100644 --- a/specforge/algorithms/common/dflash_family_model.py +++ b/specforge/algorithms/common/dflash_family_model.py @@ -25,7 +25,6 @@ if hasattr(torch, "npu") and torch.npu.is_available(): FLEX_ATTENTION_AVAILABLE = False - _VALID_LOSS_TYPES = { "dflash", "dpace", @@ -363,9 +362,20 @@ def _forward_draft_blocks( # DFlash's dynamic short-query batches are training/prefill shaped, # not autoregressive decoding. AUTO may route q_len < 128 to the # more restrictive flex-decoding kernel, whose config set can be - # empty for DFlash's sparse BlockMask. Keep the general Triton + # empty for DFlash's sparse BlockMask. Force the general Triton # Flex Attention kernel for every DFlash-family batch. - draft_kwargs["kernel_options"] = {"BACKEND": "TRITON"} + # + # The "BACKEND" kernel_option only exists on torch >= 2.11, where + # the inductor lowering sanitizes it out of the generated Triton + # constexprs. On older builds (including current torch ROCm wheels) + # the string leaks into the kernel as a bare identifier and fails to + # compile (NameError: 'TRITON' is not defined), so we fall back to + # FORCE_USE_FLEX_ATTENTION, which selects the same kernel and has + # been supported since torch 2.5. + if torch.__version__ >= "2.11": + draft_kwargs["kernel_options"] = {"BACKEND": "TRITON"} + else: + draft_kwargs["kernel_options"] = {"FORCE_USE_FLEX_ATTENTION": True} output_hidden = self.draft_model( position_ids=full_position_ids, noise_embedding=noise_embedding, diff --git a/tests/test_modeling/test_dflash_sliding.py b/tests/test_modeling/test_dflash_sliding.py index 34cbc1487..b679ebf22 100644 --- a/tests/test_modeling/test_dflash_sliding.py +++ b/tests/test_modeling/test_dflash_sliding.py @@ -169,7 +169,12 @@ def test_online_wrapper_forces_standard_triton_flex_backend(self): loss_mask=torch.ones(1, 4), ) - self.assertEqual(layers[0].kernel_options, {"BACKEND": "TRITON"}) + expected_kernel_options = ( + {"BACKEND": "TRITON"} + if torch.__version__ >= "2.11" + else {"FORCE_USE_FLEX_ATTENTION": True} + ) + self.assertEqual(layers[0].kernel_options, expected_kernel_options) class TestDFlashSlidingConfig(unittest.TestCase):