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
4 changes: 0 additions & 4 deletions tests/core/framework/batch/batch_packed_input_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,6 @@ TEST(BatchPackedInputTest, LlmSnapshotTransferMovesSpecExecutionTensors) {
params.embedding.mtp_bootstrap_embeddings = values;
params.embedding.extra_token_ids = {3};
params.embedding.mtp_bootstrap_row_idxes = {4};
params.graph.use_expanded_decode_for_spec_verify_attention = true;
params.graph.expanded_kv_seq_lens = values;
params.graph.expanded_block_tables = values;
params.graph.expanded_paged_kv_indptr = values;
Expand All @@ -1211,7 +1210,6 @@ TEST(BatchPackedInputTest, LlmSnapshotTransferMovesSpecExecutionTensors) {
params.graph.spec_verify_source_addresses_stable = true;
params.graph.spec_verify_static_graph_tasks_prepared = true;
params.multi_block_tables = {values};
params.is_spec_verify = true;
params.num_accepted_tokens = values;
params.num_accepted_tokens_host = {6};
params.mtp_topk_state = MtpTopkState::from_tensor(values);
Expand Down Expand Up @@ -1239,8 +1237,6 @@ TEST(BatchPackedInputTest, LlmSnapshotTransferMovesSpecExecutionTensors) {
ASSERT_NE(converted.mtp_topk_state, nullptr);
EXPECT_EQ(converted.mtp_topk_state->device(), target_device);
EXPECT_FALSE(converted.has_multimodal());
EXPECT_TRUE(converted.is_spec_verify);
EXPECT_TRUE(converted.graph.use_expanded_decode_for_spec_verify_attention);
EXPECT_TRUE(converted.graph.spec_verify_source_addresses_stable);
EXPECT_TRUE(converted.graph.spec_verify_static_graph_tasks_prepared);
EXPECT_EQ(converted.embedding.extra_token_ids, (std::vector<int32_t>{3}));
Expand Down
38 changes: 22 additions & 16 deletions tests/core/framework/batch/decode_input_builder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,14 @@ TEST(DecodeInputBuilderTest, PreservesRequestGroupsAndAbsoluteHistory) {

TEST(DecodeInputBuilderTest,
VerifyRowsPreserveFinalTargetAndLogicalDraftPages) {
for (const bool chunked : {false, true}) {
SCOPED_TRACE(chunked);
for (const bool per_request : {false, true}) {
SCOPED_TRACE(per_request);
LlmForwardInput input = make_shared_forward_input();
const DecodeBatchContext context = make_decode_batch_context(input);
const DecodeExpansionOptions options =
make_verify_layout(1, /*block_size=*/4, chunked);
make_verify_layout(/*num_speculative_tokens=*/1,
/*block_size=*/4,
per_request);
DecodeBuildBuffers buffers = expand_decode_inputs(context, options);
EXPECT_EQ(buffers.out_new_cache_slots,
std::vector<int32_t>({7, 8, 23, 24}));
Expand All @@ -156,12 +158,13 @@ TEST(DecodeInputBuilderTest,

build_decode_metadata(input.input_params, buffers, options);
const torch::Tensor expected_pages =
chunked ? torch::tensor({{120, 122, 124}, {180, 182, 184}}, torch::kInt)
: torch::tensor({{120, 122, 124},
{120, 122, 124},
{180, 182, 184},
{180, 182, 184}},
torch::kInt);
per_request
? torch::tensor({{120, 122, 124}, {180, 182, 184}}, torch::kInt)
: torch::tensor({{120, 122, 124},
{120, 122, 124},
{180, 182, 184},
{180, 182, 184}},
torch::kInt);
EXPECT_TRUE(
torch::equal(input.input_params.attention.host.shared_full_block_table,
expected_pages));
Expand All @@ -183,7 +186,9 @@ TEST(DecodeInputBuilderTest, VerifyPaddingNeverWritesFiniteReadPage) {
torch::tensor({kPadSlotId}, torch::kInt);
const DecodeBatchContext context = make_decode_batch_context(input);
const DecodeExpansionOptions options =
make_verify_layout(0, /*block_size=*/4, /*chunked=*/false);
make_verify_layout(/*num_speculative_tokens=*/0,
/*block_size=*/4,
/*per_request=*/false);
DecodeBuildBuffers buffers = expand_decode_inputs(context, options);
build_decode_metadata(input.input_params, buffers, options);

Expand Down Expand Up @@ -260,7 +265,6 @@ TEST(DecodeInputBuilderTest, DraftSlotsUpdateWithoutRebuildingPageTables) {
TEST(DecodeInputBuilderTest, VlmVerifyRowsPreserveVisionState) {
VlmForwardInput input;
input.input_params.meta.num_sequences = 2;
input.input_params.is_spec_verify = true;
input.token_ids_host = torch::tensor({10, 20}, torch::kInt);
input.positions_host = torch::tensor({4, 8}, torch::kInt);
input.input_params.attention.host.block_tables =
Expand All @@ -280,7 +284,6 @@ TEST(DecodeInputBuilderTest, VlmVerifyRowsPreserveVisionState) {

EXPECT_EQ(input.input_params.attention.host.q_seq_lens,
std::vector<int32_t>({1, 1}));
EXPECT_TRUE(input.input_params.is_spec_verify);
const auto retained_pixels =
input.input_params.multimodal.mm_data.get<torch::Tensor>("pixel_values");
ASSERT_TRUE(retained_pixels.has_value());
Expand All @@ -294,7 +297,9 @@ TEST(DecodeInputBuilderTest, ChunkedExpansionCommitsPerRequestLengths) {
auto [input, ctx] = make_two_request_ctx();

const DecodeExpansionOptions decode_options =
make_verify_layout(2, /*block_size=*/4, /*chunked=*/true);
make_verify_layout(/*num_speculative_tokens=*/2,
/*block_size=*/4,
/*per_request=*/true);
DecodeBuildBuffers buf = expand_decode_inputs(ctx, decode_options);
build_decode_metadata(input.input_params, buf, decode_options);

Expand All @@ -303,14 +308,16 @@ TEST(DecodeInputBuilderTest, ChunkedExpansionCommitsPerRequestLengths) {
EXPECT_EQ(input.input_params.attention.host.q_seq_lens,
std::vector<int32_t>({3, 3}));
EXPECT_EQ(input.input_params.meta.num_sequences, 2);
EXPECT_TRUE(input.input_params.meta.batch_forward_type.is_chunked_prefill());
EXPECT_TRUE(is_multi_token_decode(input.input_params.meta));
}

TEST(DecodeInputBuilderTest, FlatExpansionCommitsOneRowPerToken) {
auto [input, ctx] = make_two_request_ctx();

const DecodeExpansionOptions decode_options =
make_verify_layout(1, /*block_size=*/4, /*chunked=*/false);
make_verify_layout(/*num_speculative_tokens=*/1,
/*block_size=*/4,
/*per_request=*/false);
DecodeBuildBuffers buf = expand_decode_inputs(ctx, decode_options);

EXPECT_EQ(buf.out_token_ids, std::vector<int32_t>({10, -1, 20, -1}));
Expand All @@ -337,7 +344,6 @@ TEST(DecodeInputBuilderTest, NonCausalTokensShareFullQueryKvLength) {

const DecodeExpansionOptions decode_options{{2,
false,
BatchForwardType::DECODE,
/*leading_zero_cu=*/false,
/*causal=*/false},
/*block_size=*/4};
Expand Down
16 changes: 13 additions & 3 deletions tests/core/framework/speculative/embedding_cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.

#include <gtest/gtest.h>

#include "core/framework/model/model_input_types.h"
#include "core/framework/model/model_output.h"
#include "core/framework/sampling/sampling_params.h"
#include "core/framework/speculative/draft_extend_input.h"
Expand Down Expand Up @@ -299,6 +300,10 @@ TEST(EmbeddingCacheTest, SpeculativeHiddenSelectionPreservesDrafterSource) {
const ModelOutput model_output(primary, torch::Tensor(), aux);

for (const bool is_target_prefill : {false, true}) {
BatchInputMeta meta;
meta.batch_forward_type = is_target_prefill ? BatchForwardType::PREFILL
: BatchForwardType::DECODE;
meta.q_max_seq_len = 1;
for (const bool cp_enabled : {false, true}) {
SCOPED_TRACE(::testing::Message()
<< "prefill=" << is_target_prefill << ", cp=" << cp_enabled);
Expand All @@ -307,8 +312,9 @@ TEST(EmbeddingCacheTest, SpeculativeHiddenSelectionPreservesDrafterSource) {
model_output,
indices,
gathered_primary,
is_target_prefill,
cp_enabled);
meta,
/*is_draft_engine=*/false,
/*cp_enabled=*/cp_enabled);
EXPECT_TRUE(tensor_equal(
is_target_prefill ? output.selected_embeddings : output.embeddings,
selected_aux));
Expand All @@ -319,11 +325,15 @@ TEST(EmbeddingCacheTest, SpeculativeHiddenSelectionPreservesDrafterSource) {
}

SampleOutput output;
BatchInputMeta meta;
meta.batch_forward_type = BatchForwardType::DECODE;
meta.q_max_seq_len = 1;
output_spec_hidden_states(output,
ModelOutput(primary.narrow(0, 0, 1)),
indices,
gathered_primary,
/*is_target_prefill=*/false,
meta,
/*is_draft_engine=*/false,
/*cp_enabled=*/true);
EXPECT_TRUE(tensor_equal(output.embeddings, gathered_primary));
}
Expand Down
34 changes: 21 additions & 13 deletions tests/core/framework/speculative/mtp_async_input_builder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ limitations under the License.
#include "core/kernels/npu/tilelang/mtp_prepare_next_draft.h"
#include "core/kernels/xllm_torch_ops.h"
#include "core/layers/common/attention_metadata.h"
#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/py_attention_metadata.h"
Expand Down Expand Up @@ -322,7 +321,11 @@ 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,
/*num_speculative_tokens=*/0,
/*is_draft_engine=*/false));
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 +378,11 @@ 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,
/*num_speculative_tokens=*/0,
/*is_draft_engine=*/false));
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 +442,11 @@ 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,
/*num_speculative_tokens=*/0,
/*is_draft_engine=*/false));
EXPECT_EQ(
py_metadata.attr("dp_global_sequence_nums").cast<std::vector<int32_t>>(),
(std::vector<int32_t>{3, 0}));
Expand All @@ -461,10 +472,12 @@ TEST(MtpAsyncInputBuilderTest, PybindViewsPreserveGraphMetadataStorage) {
const torch::Tensor table = torch::tensor({{10, 11}}, torch::kInt);
const torch::Tensor lengths = torch::tensor({128}, torch::kInt);
const torch::Tensor slots = torch::tensor({1407}, torch::kInt);
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));
auto sparse = std::make_shared<layer::AttentionMetadata>();
sparse->block_table = table;
sparse->kv_seq_lens = lengths;
sparse->slot_mapping = slots;
py::object sparse_view = py::cast(PyAttentionMetadataView(
sparse, params, /*num_speculative_tokens=*/0, /*is_draft_engine=*/false));
const torch::Tensor view_table =
sparse_view.attr("block_table").cast<torch::Tensor>();
const torch::Tensor view_lengths =
Expand All @@ -474,11 +487,6 @@ TEST(MtpAsyncInputBuilderTest, PybindViewsPreserveGraphMetadataStorage) {
EXPECT_EQ(view_table.data_ptr(), table.data_ptr());
EXPECT_EQ(view_lengths.data_ptr(), lengths.data_ptr());
EXPECT_EQ(view_slots.data_ptr(), slots.data_ptr());
EXPECT_TRUE(sparse->kv_seq_lens_vec.empty());
EXPECT_FALSE(sparse->kv_seq_lens_host.defined());
EXPECT_FALSE(sparse->paged_kv_indptr.defined());
EXPECT_FALSE(sparse->paged_kv_indices.defined());
EXPECT_FALSE(sparse->expanded_decode.enabled);

// The Python view retains the owner as the request crosses a page boundary.
sparse.reset();
Expand Down
4 changes: 2 additions & 2 deletions tests/core/layers/attention_metadata_builder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ TEST(AttentionMetadataBuilderTest, PreservesSharedFullFinalAddresses) {

TEST(AttentionMetadataBuilderTest, PreservesExpandedSharedFullFinalAddresses) {
ModelParamsView params = make_params();
params.meta.batch_forward_type = BatchForwardType::CHUNKED_PREFILL;
params.is_spec_verify = true;
params.meta.batch_forward_type = BatchForwardType::DECODE;
params.meta.q_max_seq_len = 2;
params.attention.device.shared_full_block_table = torch::tensor(
{{120, 60}, {120, 60}, {122, 124}, {122, 124}}, torch::kInt32);
params.attention.device.shared_full_slot_mapping =
Expand Down
Loading
Loading