Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
319 changes: 79 additions & 240 deletions tests/core/framework/hf_model_loader_test.cpp

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions tests/core/framework/model/model_type_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ TEST(ModelTypeTest, LooksUpBorrowedSubstringWithoutTerminator) {
}

TEST(ModelTypeTest, GatesPackedC8ByModelAndBackend) {
EXPECT_TRUE(supports_mla_kv_cache_quant("glm_moe_dsa"));
EXPECT_TRUE(supports_mla_kv_cache_quant("glm_moe_dsa_mtp"));
EXPECT_FALSE(supports_mla_kv_cache_quant("deepseek_v3"));
EXPECT_FALSE(
enable_mla_packed_c8(/*enable_kv_cache_quant=*/false, "glm_moe_dsa"));
EXPECT_FALSE(
enable_mla_packed_c8(/*enable_kv_cache_quant=*/true, "deepseek_v3"));
#if defined(USE_NPU)
EXPECT_TRUE(
enable_mla_packed_c8(/*enable_kv_cache_quant=*/true, "glm_moe_dsa"));
EXPECT_TRUE(enable_mla_packed_c8(/*enable_kv_cache_quant=*/true,
/*model_type=*/"glm_moe_dsa_mtp"));
#else
EXPECT_FALSE(
enable_mla_packed_c8(/*enable_kv_cache_quant=*/true, "glm_moe_dsa"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ limitations under the License.
#include "core/layers/common/attention_metadata_builder.h"
#include "core/layers/common/expanded_decode_metadata_builder.h"
#include "core/runtime/forward_params.h"
#include "core/runtime/options.h"
#include "core/runtime/py_attention_metadata.h"

namespace py = pybind11;
Expand Down Expand Up @@ -322,7 +323,7 @@ TEST(MtpAsyncInputBuilderTest, PybindViewOwnsGlobalDpKvMaxSequenceLengths) {
params.parallel.dp_global_token_nums = {expected[0] == 0 ? 0 : 1,
expected[1] == 0 ? 0 : 1};
params.parallel.dp_global_kv_max_seq_lens = expected;
py_metadata = py::cast(PyAttentionMetadataView(metadata, params));
py_metadata = py::cast(PyAttentionMetadataView(metadata, params, {}));
EXPECT_EQ(py_metadata.attr("raw_dp_execution_token_counts")
.cast<std::vector<int32_t>>(),
params.parallel.dp_global_token_nums);
Expand Down Expand Up @@ -375,7 +376,8 @@ TEST(MtpAsyncInputBuilderTest, PybindViewExposesLinearStateReadAndWriteSlots) {
params.embedding.linear_state_read_ids = {2, 6};
params.meta.batch_forward_type = BatchForwardType::PREFILL;

py::object py_metadata = py::cast(PyAttentionMetadataView(metadata, params));
py::object py_metadata =
py::cast(PyAttentionMetadataView(metadata, params, {}));
EXPECT_TRUE(torch::equal(
py_metadata.attr("linear_state_indices").cast<torch::Tensor>(),
torch::tensor({3, 7}, torch::kInt)));
Expand Down Expand Up @@ -435,7 +437,8 @@ TEST(MtpAsyncInputBuilderTest, PybindViewsPreserveGraphMetadataStorage) {
params.parallel.dp_global_token_nums = {6, 0};
params.parallel.dp_global_sequence_nums = {3, 0};
params.parallel = params.parallel.to(torch::Device(torch::kCPU));
py::object py_metadata = py::cast(PyAttentionMetadataView(metadata, params));
py::object py_metadata =
py::cast(PyAttentionMetadataView(metadata, params, {}));
EXPECT_EQ(
py_metadata.attr("dp_global_sequence_nums").cast<std::vector<int32_t>>(),
(std::vector<int32_t>{3, 0}));
Expand Down Expand Up @@ -464,7 +467,8 @@ TEST(MtpAsyncInputBuilderTest, PybindViewsPreserveGraphMetadataStorage) {
auto sparse = std::make_shared<layer::AttentionMetadata>(
layer::AttentionMetadataBuilder::build_mtp_sparse_decode(
table, lengths, slots, /*block_size=*/128));
py::object sparse_view = py::cast(PyAttentionMetadataView(sparse, params));
py::object sparse_view =
py::cast(PyAttentionMetadataView(sparse, params, {}));
const torch::Tensor view_table =
sparse_view.attr("block_table").cast<torch::Tensor>();
const torch::Tensor view_lengths =
Expand Down
2 changes: 1 addition & 1 deletion tests/core/kv_cache/layout/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ cc_test(
kv_cache_estimation_test.cpp
DEPS
:kv_cache_estimation
$<$<BOOL:${USE_NPU}>:models>
$<$<BOOL:${USE_NPU}>:model_loader>
GTest::gtest_main
)
target_link_libraries(kv_cache_estimation_test
Expand Down
13 changes: 4 additions & 9 deletions tests/core/kv_cache/layout/kv_cache_estimation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ limitations under the License.
#include "tests/core/kv_cache/dcp_indexer_test_fixture.h"

#if defined(USE_NPU)
#include "models/model_registry.h"
#include "core/framework/model_loader/hf_model_args.h"
#endif

namespace xllm {
Expand Down Expand Up @@ -1497,15 +1497,10 @@ TEST(KVCacheEstimationTest, Qwen35NativeMtpDraftUsesFullAttentionCacheBudget) {
"full_attention",
"linear_attention",
"full_attention"});
ModelRegistry::configure_mtp_args(target_args,
/*speculative_algorithm=*/"MTP",
/*is_draft_engine=*/false,
/*is_python_model=*/false);
ModelArgs draft_args = target_args;
ModelRegistry::configure_mtp_args(draft_args,
/*speculative_algorithm=*/"MTP",
/*is_draft_engine=*/true,
/*is_python_model=*/false);
configure_draft_model_args(draft_args,
/*algorithm=*/"MTP",
/*is_python_model=*/false);

KVCacheEstimateOptions target_options = make_linear_attention_options();
target_options.num_speculative_tokens = 3;
Expand Down
48 changes: 3 additions & 45 deletions tests/core/layers/npu_torch/deepseek_v4_indexer_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,37 +178,6 @@ TEST_F(DeepseekV4IndexerTest, DsaSwaUsesExplicitBlockParallelSlots) {
torch::tensor({5, 6, 7}, torch::kInt32)));
}

TEST_F(DeepseekV4IndexerTest, DSparkSparseTilingUsesSupportedWindow) {
ModelParamsView params = ModelInputSnapshot(LlmModelParams()).view();
params.meta.batch_forward_type = BatchForwardType::CHUNKED_PREFILL;
params.meta.q_max_seq_len = 1;

EXPECT_TRUE(params.meta.batch_forward_type.no_decode());
EXPECT_FALSE(deepseek_v4_use_native_sas(
/*dspark_block_size=*/5,
/*dspark_use_native_sas=*/true,
/*use_prefill_attention=*/params.meta.batch_forward_type.no_decode()));
EXPECT_EQ(deepseek_v4_ori_window_left(/*window_size=*/128,
/*dspark_block_size=*/5,
/*use_native_dspark_sas=*/false),
127);
EXPECT_EQ(deepseek_v4_ori_window_left(/*window_size=*/128,
/*dspark_block_size=*/0,
/*use_native_dspark_sas=*/true),
127);
EXPECT_EQ(deepseek_v4_ori_window_left(/*window_size=*/128,
/*dspark_block_size=*/5,
/*use_native_dspark_sas=*/true),
132);

params.meta.batch_forward_type = BatchForwardType::DECODE;
EXPECT_FALSE(params.meta.batch_forward_type.no_decode());
EXPECT_TRUE(deepseek_v4_use_native_sas(
/*dspark_block_size=*/5,
/*dspark_use_native_sas=*/true,
/*use_prefill_attention=*/params.meta.batch_forward_type.no_decode()));
}

TEST_F(DeepseekV4IndexerTest, DSparkNativeSwaIndicesAreSharedByQueryRows) {
const torch::Tensor block_table =
torch::tensor({{10, 11, 12}}, torch::kInt32);
Expand All @@ -220,7 +189,7 @@ TEST_F(DeepseekV4IndexerTest, DSparkNativeSwaIndicesAreSharedByQueryRows) {
query_cu_seq_lens,
seq_lens,
/*window_size=*/4,
/*dspark_block_size=*/3,
/*num_speculative_tokens=*/3,
/*cache_block_size=*/4);

ASSERT_EQ(indices.dim(), 3);
Expand All @@ -235,10 +204,6 @@ TEST_F(DeepseekV4IndexerTest, DSparkNativeSwaIndicesAreSharedByQueryRows) {
}

TEST_F(DeepseekV4IndexerTest, DSparkNativeSwaIndicesWrapAroundRingBuffer) {
// Two-block ring buffer with kv_len=10 forces the SWA window to span both
// ring entries and to wrap. Visible positions 5..9 map through
// block_column = pos/2 % ring_size(2) -> {0,1,1,0,0}, wrapping back to
// block_column 0 once the ring rotates.
const torch::Tensor block_table = torch::tensor({{20, 21}}, torch::kInt32);
const torch::Tensor query_cu_seq_lens = torch::tensor({0, 1}, torch::kInt32);
const torch::Tensor seq_lens = torch::tensor({10}, torch::kInt32);
Expand All @@ -248,16 +213,9 @@ TEST_F(DeepseekV4IndexerTest, DSparkNativeSwaIndicesWrapAroundRingBuffer) {
query_cu_seq_lens,
seq_lens,
/*window_size=*/4,
/*dspark_block_size=*/3,
/*num_speculative_tokens=*/3,
/*cache_block_size=*/2);

ASSERT_EQ(indices.dim(), 3);
ASSERT_EQ(indices.size(0), 1);
ASSERT_EQ(indices.size(1), 1);
// start_pos = max((kv - q_len) - window, 0) = (10-1)-4 = 5, so the visible
// window is positions 5,6,7,8,9. block_column = pos/2 % 2 -> {0,1,1,0,0};
// slot = block_id*2 + pos%2 -> {20*2+1, 21*2+0, 21*2+1, 20*2+0, 20*2+1} =
// {41, 42, 43, 40, 41}.
const torch::Tensor expected_prefix =
torch::tensor({41, 42, 43, 40, 41}, torch::kInt32);
EXPECT_TRUE(torch::equal(indices[0][0].slice(0, 0, 5), expected_prefix));
Expand All @@ -277,7 +235,7 @@ TEST_F(DeepseekV4IndexerTest, DSparkNativeSwaIndicesMapExpandedMetadataRing) {
query_cu_seq_lens,
seq_lens,
/*window_size=*/4,
/*dspark_block_size=*/3,
/*num_speculative_tokens=*/3,
/*cache_block_size=*/4);

const torch::Tensor expected_prefix =
Expand Down
43 changes: 23 additions & 20 deletions tests/core/layers/npu_torch/dflash2_grouped_conv_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ TEST(DFlash2GroupedConvTest, ResetsHistoryAtEachBlockBoundary) {
{5.0f, 6.0f, 7.0f, 8.0f},
{9.0f, 10.0f, 11.0f, 12.0f},
{13.0f, 14.0f, 15.0f, 16.0f}});
torch::Tensor delta = torch::zeros({4, 2, 2});
torch::Tensor base = torch::ones({2, 4});

torch::Tensor output = dflash2_grouped_conv(hidden,
delta,
base,
/*block_size=*/2,
/*num_groups=*/2,
/*group_size=*/2,
/*taps=*/2);
DFlash2GroupedConv convolution(/*hidden_size=*/4,
/*taps=*/2,
/*group_size=*/2,
/*num_speculative_tokens=*/1,
hidden.options());
{
torch::NoGradGuard guard;
auto parameters = convolution->named_parameters();
parameters["base_kernel"].fill_(/*value=*/1.0);
parameters["kernel_projection.weight"].zero_();
}
const torch::Tensor output = std::get<0>(convolution->prepare(hidden));
torch::Tensor expected = torch::stack(
{hidden[0], hidden[1] + hidden[0], hidden[2], hidden[3] + hidden[2]});
EXPECT_TRUE(torch::allclose(output, expected));
Expand All @@ -43,16 +45,17 @@ TEST(DFlash2GroupedConvTest, ResetsHistoryAtEachBlockBoundary) {
TEST(DFlash2GroupedConvTest, BroadcastsDynamicKernelWithinEachGroup) {
torch::Tensor hidden =
torch::tensor({{1.0f, 2.0f, 3.0f, 4.0f}, {5.0f, 6.0f, 7.0f, 8.0f}});
torch::Tensor delta = torch::tensor({{{1.0f, 2.0f}}, {{3.0f, 4.0f}}});
torch::Tensor base = torch::zeros({1, 4});

torch::Tensor output = dflash2_grouped_conv(hidden,
delta,
base,
/*block_size=*/2,
/*num_groups=*/2,
/*group_size=*/2,
/*taps=*/1);
DFlash2GroupedConv convolution(/*hidden_size=*/4,
/*taps=*/1,
/*group_size=*/2,
/*num_speculative_tokens=*/1,
hidden.options());
{
torch::NoGradGuard guard;
convolution->named_parameters()["base_kernel"].zero_();
}
const torch::Tensor output = convolution->finish(
hidden, torch::tensor({{{1.0f, 2.0f}}, {{3.0f, 4.0f}}}));
torch::Tensor expected =
torch::tensor({{1.0f, 2.0f, 6.0f, 8.0f}, {15.0f, 18.0f, 28.0f, 32.0f}});
EXPECT_TRUE(torch::allclose(output, expected));
Expand Down
21 changes: 1 addition & 20 deletions tests/core/runtime/acl_graph_executor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ limitations under the License.
#include "core/runtime/acl_graph_persistent_param.h"
#include "core/runtime/base_executor_impl.h"
#include "core/runtime/decode_graph_bucket.h"
#include "core/runtime/dflash_worker_impl.h"
#include "core/runtime/options.h"
#include "core/runtime/speculative_worker_impl.h"
#include "core/util/tensor_helper.h"
Expand Down Expand Up @@ -1475,30 +1474,12 @@ TEST(AuxHiddenCaptureTest, PreservesConfiguredLayerOrderAndResidual) {
torch::full({3, 2}, 20.0f)));
}

TEST(DSparkWorkerInputTest, InvalidatesTargetAttentionAndRecurrentMetadata) {
LlmModelParams params;
params.attn_metadata = std::make_shared<layer::AttentionMetadata>();
params.multi_block_tables.resize(3);
params.embedding.linear_state_ids = {4};
params.embedding.linear_state_indices = torch::tensor({4}, torch::kInt32);
params.linear_state_validity_mask = {1};

dflash_detail::invalidate_draft_model_geometry(params);

EXPECT_EQ(params.attn_metadata, nullptr);
EXPECT_EQ(params.multi_block_tables.size(), 3);
EXPECT_TRUE(params.embedding.linear_state_ids.empty());
EXPECT_FALSE(params.embedding.linear_state_indices.defined());
EXPECT_TRUE(params.linear_state_validity_mask.empty());
EXPECT_TRUE(params.embedding.linear_state_read_ids.empty());
}

TEST(DSparkWorkerInputTest, ScalesPaddedAndRawDpTokenCountsTogether) {
LlmModelParams params;
params.parallel.dp_global_token_nums = {2, 0, 4};
params.parallel.raw_dp_global_token_nums = {1, 0, 3};

scale_speculative_parallel_token_counts(params, 4);
scale_decode_parallel_token_counts(params, /*num_decoding_tokens=*/5);

EXPECT_EQ(params.parallel.dp_global_token_nums,
(std::vector<int32_t>{10, 0, 20}));
Expand Down
26 changes: 17 additions & 9 deletions tests/core/runtime/mlu_graph_executor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ TEST(MluGraphCapacityTest, GroupedRowsCoverRoundedRequests) {
.enable_speculative_decode(true)
.enable_graph_mode_decode_no_padding(false)
.speculative_algorithm("mtp");
MtpModelCapabilities capabilities;
EXPECT_EQ(mlu::get_graph_token_capacity(options, capabilities), 112);
capabilities.supports_grouped_graph = true;
EXPECT_EQ(mlu::get_graph_token_capacity(options, capabilities), 144);
EXPECT_EQ(mlu::get_graph_token_capacity(
options, /*attention_layout=*/mlu::GraphAttentionLayout::FLAT),
112);
EXPECT_EQ(mlu::get_graph_token_capacity(
options,
/*attention_layout=*/mlu::GraphAttentionLayout::
GROUPED_FULL_ATTENTION),
144);
}
TEST(MluGraphCapacityTest, DraftRowsCoverIntermediateTpAlignedPrefixes) {
runtime::Options options;
Expand All @@ -106,9 +110,11 @@ TEST(MluGraphCapacityTest, DraftRowsCoverIntermediateTpAlignedPrefixes) {
.is_draft_engine(true)
.world_size(16)
.dp_size(2);
MtpModelCapabilities capabilities;
capabilities.supports_grouped_graph = true;
EXPECT_EQ(mlu::get_graph_token_capacity(options, capabilities), 24);
EXPECT_EQ(mlu::get_graph_token_capacity(
options,
/*attention_layout=*/mlu::GraphAttentionLayout::
GROUPED_FULL_ATTENTION),
24);
}

} // namespace
Expand Down Expand Up @@ -609,8 +615,10 @@ TEST_F(MluGraphExecutorTest, OverPersistentGraphRowCapacityFallsBackToEager) {
}

TEST_F(MluGraphExecutorTest, LinearStatePaddingTailUsesPaddingId) {
::xllm::mlu::GraphPersistentParam param(
model_args_, tensor_options_.device(), options_, {});
::xllm::mlu::GraphPersistentParam param(model_args_,
tensor_options_.device(),
options_,
mlu::GraphAttentionLayout::FLAT);

LlmForwardInput first_input = prepare_inputs(/*batch_size=*/4, /*seed=*/83);
first_input.input_params.embedding.linear_state_ids = {10, 20, 30, 40};
Expand Down
Loading
Loading