speculators.models.dflash2.model_definitions
MIT License
Copyright © 2026 Z Lab
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DFlash2 architecture adapted from: https://github.com/z-lab/dflash/blob/07ebd93db9f472af339b644bb70221ad8428328a/dflash/model.py
Classes:
-
CandidateSelector–Bilinear predecessor/hidden/candidate scorer used after unary top-k.
-
GroupedDynamicCausalConv–Input/output local convolutions driven by one hidden-state projection.
-
Qwen3DFlash2DecoderLayer–DFlash decoder layer with local convolution around attention and MLP.
Functions:
-
grouped_dynamic_conv–Apply a token-conditioned grouped causal convolution within draft blocks.
CandidateSelector
CandidateSelector(
*,
vocab_size: int,
hidden_size: int,
rank: int,
top_k: int,
initializer_range: float = 0.02,
)
Bases: Module
Bilinear predecessor/hidden/candidate scorer used after unary top-k.
Methods:
-
context–Return the elementwise predecessor and hidden-state interaction.
-
score_candidates–Add transition scores to a selected subset of unary logits.
-
select–Return unary top-k IDs and their selector-corrected edge scores.
-
transition_scores–Score selected candidate IDs.
Source code in speculators/models/dflash2/model_definitions.py
context
Return the elementwise predecessor and hidden-state interaction.
Source code in speculators/models/dflash2/model_definitions.py
score_candidates
score_candidates(
unary_logits: Tensor,
hidden_states: Tensor,
predecessor_ids: Tensor,
candidate_ids: Tensor,
) -> torch.Tensor
Add transition scores to a selected subset of unary logits.
Source code in speculators/models/dflash2/model_definitions.py
select
select(
unary_logits: Tensor,
hidden_states: Tensor,
predecessor_ids: Tensor,
top_k: int | None = None,
) -> tuple[torch.Tensor, torch.Tensor]
Return unary top-k IDs and their selector-corrected edge scores.
Source code in speculators/models/dflash2/model_definitions.py
transition_scores
transition_scores(
hidden_states: Tensor,
predecessor_ids: Tensor,
candidate_ids: Tensor,
) -> torch.Tensor
Score selected candidate IDs.
Source code in speculators/models/dflash2/model_definitions.py
GroupedDynamicCausalConv
GroupedDynamicCausalConv(
hidden_size: int,
*,
block_size: int,
kernel_size: int,
group_size: int,
)
Bases: Module
Input/output local convolutions driven by one hidden-state projection.
Methods:
-
finish–Convolve sublayer outputs with the saved output-side kernel.
-
prepare–Convolve sublayer inputs and return its output-side dynamic kernel.
-
reset_parameters–Initialize both convolutions as identity transforms.
Source code in speculators/models/dflash2/model_definitions.py
finish
Convolve sublayer outputs with the saved output-side kernel.
Source code in speculators/models/dflash2/model_definitions.py
prepare
Convolve sublayer inputs and return its output-side dynamic kernel.
Source code in speculators/models/dflash2/model_definitions.py
reset_parameters
Initialize both convolutions as identity transforms.
Qwen3DFlash2DecoderLayer
Qwen3DFlash2DecoderLayer(
config: Qwen3Config,
layer_idx: int,
*,
block_size: int,
conv_kernel_size: int,
conv_group_size: int,
)
Bases: Qwen3DFlashDecoderLayer
DFlash decoder layer with local convolution around attention and MLP.
Methods:
-
reset_convolutions–Restore the DFlash-equivalent identity initialization.
Source code in speculators/models/dflash2/model_definitions.py
reset_convolutions
Restore the DFlash-equivalent identity initialization.
grouped_dynamic_conv
grouped_dynamic_conv(
hidden_states: Tensor,
delta_kernel: Tensor,
base_kernel: Tensor,
*,
block_size: int,
group_size: int,
) -> torch.Tensor
Apply a token-conditioned grouped causal convolution within draft blocks.