-
Notifications
You must be signed in to change notification settings - Fork 285
feat: add Qwen3.8 DFlash2 decoding support. #2279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
pjgao
wants to merge
9
commits into
xLLM-AI:main
Choose a base branch
from
pjgao:feat/qwen38-dflash2-npu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
58c437c
feat: add Qwen3.8 DFlash2 decoding support.
pjgao 64cfd06
feat: align Qwen3.8 DFlash2 with the official decoding contract
pjgao 5cffc7a
Merge branch 'main' into feat/qwen38-dflash2-npu
pjgao d96f8c8
fix: correct speculative metrics and hybrid MTP prefill
pjgao 279af73
Merge remote-tracking branch 'pjgao/feat/qwen38-dflash2-npu' into fea…
pjgao 935285a
style: format DFlash2 acceptance metrics
pjgao ffbe66b
style: preserve legacy license links
pjgao 130bd7d
refactor: centralize DFlash2 identifiers
pjgao a0c2641
fix: address DFlash2 review findings
pjgao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* Copyright 2026 The xLLM Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://github.com/jd-opensource/xllm/blob/main/LICENSE | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ==============================================================================*/ | ||
|
|
||
| #include "core/layers/common/dflash2_grouped_conv.h" | ||
|
|
||
| #include <glog/logging.h> | ||
|
|
||
| #include "core/framework/state_dict/utils.h" | ||
|
|
||
| namespace xllm::layer { | ||
|
|
||
| torch::Tensor dflash2_grouped_conv(const torch::Tensor& hidden_states, | ||
| const torch::Tensor& delta, | ||
| const torch::Tensor& base, | ||
| int32_t block_size, | ||
| int32_t num_groups, | ||
| int32_t group_size, | ||
| int32_t taps) { | ||
| CHECK_EQ(hidden_states.dim(), 2); | ||
| CHECK_EQ(delta.dim(), 3); | ||
| CHECK_EQ(base.dim(), 2); | ||
| CHECK_GT(block_size, 0); | ||
| CHECK_EQ(hidden_states.size(1), | ||
| static_cast<int64_t>(num_groups) * group_size); | ||
| CHECK_EQ(delta.size(0), hidden_states.size(0)); | ||
| CHECK_EQ(delta.size(1), taps); | ||
| CHECK_EQ(delta.size(2), num_groups); | ||
| CHECK_EQ(base.size(0), taps); | ||
| CHECK_EQ(base.size(1), hidden_states.size(1)); | ||
|
|
||
| const int64_t num_tokens = hidden_states.size(0); | ||
| torch::Tensor blocks = | ||
| hidden_states.view({num_tokens, num_groups, group_size}); | ||
| torch::Tensor coefficients = | ||
| base.view({1, taps, num_groups, group_size}) + delta.unsqueeze(-1); | ||
| torch::Tensor output = coefficients.select(/*dim=*/1, /*index=*/0) * blocks; | ||
| torch::Tensor positions = torch::arange(num_tokens, | ||
| torch::TensorOptions() | ||
| .dtype(torch::kLong) | ||
| .device(hidden_states.device())) % | ||
| block_size; | ||
|
|
||
| for (int32_t tap = 1; tap < taps; ++tap) { | ||
| CHECK_GT(num_tokens, tap) | ||
| << "DFlash2 convolution token count must exceed its tap offset."; | ||
| torch::Tensor padding = | ||
| torch::zeros({tap, num_groups, group_size}, hidden_states.options()); | ||
| torch::Tensor shifted = torch::cat( | ||
| {padding, blocks.slice(/*dim=*/0, /*start=*/0, num_tokens - tap)}, | ||
| /*dim=*/0); | ||
| torch::Tensor valid = | ||
| positions.ge(tap).view({num_tokens, 1, 1}).to(hidden_states.dtype()); | ||
| output.add_(coefficients.select(/*dim=*/1, /*index=*/tap) * shifted * | ||
| valid); | ||
| } | ||
| return output.flatten(/*start_dim=*/1); | ||
| } | ||
|
|
||
| DFlash2GroupedConvImpl::DFlash2GroupedConvImpl( | ||
| int64_t hidden_size, | ||
| int32_t taps, | ||
| int32_t group_size, | ||
| int32_t block_size, | ||
| const torch::TensorOptions& options) | ||
| : block_size_(block_size), taps_(taps), group_size_(group_size) { | ||
| CHECK_GT(hidden_size, 0); | ||
| CHECK_GT(taps_, 0); | ||
| CHECK_GT(group_size_, 0); | ||
| CHECK_GT(block_size_, 0); | ||
| CHECK_EQ(hidden_size % group_size_, 0) | ||
| << "DFlash2 conv_group_size must divide hidden_size."; | ||
| num_groups_ = static_cast<int32_t>(hidden_size / group_size_); | ||
| base_kernel_ = register_parameter( | ||
| "base_kernel", torch::empty({2, taps_, hidden_size}, options), false); | ||
| kernel_projection_ = register_module("kernel_projection", | ||
| AddMatmul(hidden_size, | ||
| 2LL * taps_ * num_groups_, | ||
| /*with_bias=*/false, | ||
| options)); | ||
| } | ||
|
|
||
| std::tuple<torch::Tensor, torch::Tensor> DFlash2GroupedConvImpl::prepare( | ||
| const torch::Tensor& hidden_states) { | ||
| torch::Tensor coefficients = | ||
| kernel_projection_->forward(hidden_states) | ||
| .view({hidden_states.size(0), 2, taps_, num_groups_}); | ||
| return {convolve(hidden_states, | ||
| coefficients.select(/*dim=*/1, /*index=*/0), | ||
| /*side=*/0), | ||
| coefficients.select(/*dim=*/1, /*index=*/1)}; | ||
| } | ||
|
|
||
| torch::Tensor DFlash2GroupedConvImpl::finish( | ||
| const torch::Tensor& hidden_states, | ||
| const torch::Tensor& coefficients) { | ||
| return convolve(hidden_states, coefficients, /*side=*/1); | ||
| } | ||
|
|
||
| void DFlash2GroupedConvImpl::load_state_dict(const StateDict& state_dict) { | ||
| weight::load_weight( | ||
| state_dict, "base_kernel", base_kernel_, base_kernel_is_loaded_); | ||
| kernel_projection_->load_state_dict( | ||
| state_dict.get_dict_with_prefix("kernel_projection.")); | ||
| } | ||
|
|
||
| void DFlash2GroupedConvImpl::verify_loaded_weights( | ||
| const std::string& prefix) const { | ||
| CHECK(base_kernel_is_loaded_) | ||
| << "weight is not loaded for " << prefix + "base_kernel"; | ||
| kernel_projection_->verify_loaded_weights(prefix + "kernel_projection."); | ||
| } | ||
|
|
||
| torch::Tensor DFlash2GroupedConvImpl::convolve( | ||
| const torch::Tensor& hidden_states, | ||
| const torch::Tensor& delta, | ||
| int32_t side) const { | ||
| return dflash2_grouped_conv(hidden_states, | ||
| delta, | ||
| base_kernel_.select(/*dim=*/0, side), | ||
| block_size_, | ||
| num_groups_, | ||
| group_size_, | ||
| taps_); | ||
| } | ||
|
|
||
| } // namespace xllm::layer | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* Copyright 2026 The xLLM Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://github.com/jd-opensource/xllm/blob/main/LICENSE | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ==============================================================================*/ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <torch/torch.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <tuple> | ||
|
|
||
| #include "core/framework/state_dict/state_dict.h" | ||
| #include "core/layers/common/add_matmul.h" | ||
|
|
||
| namespace xllm::layer { | ||
|
|
||
| torch::Tensor dflash2_grouped_conv(const torch::Tensor& hidden_states, | ||
| const torch::Tensor& delta, | ||
| const torch::Tensor& base, | ||
| int32_t block_size, | ||
| int32_t num_groups, | ||
| int32_t group_size, | ||
| int32_t taps); | ||
|
|
||
| class DFlash2GroupedConvImpl final : public torch::nn::Module { | ||
| public: | ||
| DFlash2GroupedConvImpl(int64_t hidden_size, | ||
| int32_t taps, | ||
| int32_t group_size, | ||
| int32_t block_size, | ||
| const torch::TensorOptions& options); | ||
|
|
||
| std::tuple<torch::Tensor, torch::Tensor> prepare( | ||
| const torch::Tensor& hidden_states); | ||
|
|
||
| torch::Tensor finish(const torch::Tensor& hidden_states, | ||
| const torch::Tensor& coefficients); | ||
|
|
||
| void load_state_dict(const StateDict& state_dict); | ||
| void verify_loaded_weights(const std::string& prefix) const; | ||
|
|
||
| private: | ||
| torch::Tensor convolve(const torch::Tensor& hidden_states, | ||
| const torch::Tensor& delta, | ||
| int32_t side) const; | ||
|
|
||
| AddMatmul kernel_projection_{nullptr}; | ||
| torch::Tensor base_kernel_; | ||
| bool base_kernel_is_loaded_ = false; | ||
| int32_t block_size_ = 0; | ||
| int32_t taps_ = 0; | ||
| int32_t group_size_ = 0; | ||
| int32_t num_groups_ = 0; | ||
| }; | ||
| TORCH_MODULE(DFlash2GroupedConv); | ||
|
|
||
| } // namespace xllm::layer |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.