Spaces:
Running
Running
File size: 17,732 Bytes
5f923cd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | // Copyright 2025 The ODML Authors.
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 "runtime/executor/fake_llm_executor.h"
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/status/status.h" // from @com_google_absl
#include "absl/time/clock.h" // from @com_google_absl
#include "absl/time/time.h" // from @com_google_absl
#include "absl/types/span.h" // from @com_google_absl
#include "litert/test/matchers.h" // from @litert
#include "runtime/components/constrained_decoding/constrained_decoder.h"
#include "runtime/components/constrained_decoding/fake_constraint.h"
#include "runtime/executor/llm_executor_io_types.h"
#include "runtime/util/convert_tensor_buffer.h"
#include "runtime/util/test_utils.h" // IWYU pragma: keep
namespace litert::lm {
namespace {
using ::testing::status::StatusIs;
TEST(FakeLlmExecutorTest, ExecutorSettings) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}};
const std::vector<std::vector<int>> decode_tokens_set = {{3, 2}, {0, 0}};
FakeLlmExecutor fake_llm_executor(3, prefill_tokens_set, decode_tokens_set);
EXPECT_OK(fake_llm_executor.GetExecutorSettings());
EXPECT_EQ(fake_llm_executor.GetExecutorSettings()->GetMaxNumTokens(), 1024);
// Set the max num tokens to 100.
fake_llm_executor.GetMutableExecutorSettings().value()->SetMaxNumTokens(100);
EXPECT_EQ(fake_llm_executor.GetExecutorSettings()->GetMaxNumTokens(), 100);
}
TEST(FakeLlmExecutorTest, UpdateExecutorSettings) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}};
const std::vector<std::vector<int>> decode_tokens_set = {{3, 2}, {0, 0}};
FakeLlmExecutor fake_llm_executor(3, prefill_tokens_set, decode_tokens_set);
ASSERT_OK_AND_ASSIGN(auto new_settings,
fake_llm_executor.GetExecutorSettings());
new_settings.SetMaxNumTokens(200);
// The default implementation should return OK.
EXPECT_OK(fake_llm_executor.UpdateExecutorSettings(new_settings));
}
TEST(FakeLlmExecutorTest, Prefill) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}};
const std::vector<std::vector<int>> decode_tokens_set = {{3, 2}, {0, 0}};
FakeLlmExecutor fake_llm_executor(3, prefill_tokens_set, decode_tokens_set);
ExecutorInputs inputs;
// Create a tensor buffer with 3 elements but only the first two elements
// match the expected prefill tokens.
const std::vector<int> input_tokens = {1, 2, 0};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(input_tokens), {1, 3}));
inputs.SetTextData(ExecutorTextData(std::move(input_tokens_buffer)));
// Fail because the input tokens do not match the expected prefill tokens.
EXPECT_THAT(fake_llm_executor.Prefill(inputs),
StatusIs(absl::StatusCode::kInvalidArgument));
// Succeed because the input tokens match the expected prefill tokens.
auto ids_span = ReferTensorBufferAsSpan<int>(*(*inputs.GetTextTokenIdsPtr()));
(*ids_span)[2] = 3;
EXPECT_OK(fake_llm_executor.Prefill(inputs));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 3);
}
TEST(FakeLlmExecutorTest, PrefillWithAudio) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}};
const std::vector<std::vector<int>> decode_tokens_set = {{3, 2}, {0, 0}};
std::vector<float> audio_embeddings_set = {1.0f, 2.0f, 3.0f, 4.0f};
FakeLlmExecutor fake_llm_executor(3, prefill_tokens_set, decode_tokens_set,
/*batch_size=*/1, audio_embeddings_set);
ExecutorInputs inputs;
// Create a tensor buffer with 3 elements but only the first two elements
// match the expected prefill tokens.
const std::vector<int> input_tokens = {1, 2, 3};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(input_tokens), {1, 3}));
inputs.SetTextData(ExecutorTextData(std::move(input_tokens_buffer)));
const std::vector<float> input_audio_embedding = {1.0f, 2.0f, 3.0f, 0.0f};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_audio_embedding_buffer,
CopyToTensorBuffer<float>(absl::MakeSpan(input_audio_embedding),
{1, 4, 1}));
inputs.SetAudioData(
ExecutorAudioData(std::move(input_audio_embedding_buffer), std::nullopt));
// Fail because the input audio embedding does not match the expected the
// audio embedding set.
EXPECT_THAT(fake_llm_executor.Prefill(inputs),
StatusIs(absl::StatusCode::kInvalidArgument));
// Succeed because the input audio embedding matches the expected audio
// embedding set.
auto audio_embedding_span =
ReferTensorBufferAsSpan<float>(*(*inputs.GetAudioEmbeddingsPtr()));
(*audio_embedding_span)[3] = 4.0f;
EXPECT_OK(fake_llm_executor.Prefill(inputs));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 3);
}
TEST(FakeLlmExecutorTest, DecodeWithoutPrefillFailed) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}};
const std::vector<std::vector<int>> decode_tokens_set = {{3}, {0}};
FakeLlmExecutor fake_llm_executor(/*vocab_size=*/4, prefill_tokens_set,
decode_tokens_set);
EXPECT_THAT(fake_llm_executor.Decode(),
StatusIs(absl::StatusCode::kFailedPrecondition));
}
TEST(FakeLlmExecutorTest, DecodeToIds) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}};
const std::vector<std::vector<int>> decode_tokens_set = {{3}, {0}};
FakeLlmExecutor fake_llm_executor(4, prefill_tokens_set, decode_tokens_set);
ExecutorInputs inputs;
const std::vector<int> input_tokens = {1, 2, 3};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(input_tokens), {1, 3}));
inputs.SetTextData(ExecutorTextData(std::move(input_tokens_buffer)));
EXPECT_OK(fake_llm_executor.Prefill(inputs));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 3);
// Call Decode for the 1st time. The output tokens should be the 1st decode
// tokens: 3.
ASSERT_OK_AND_ASSIGN(auto output_tokens, fake_llm_executor.Decode());
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 4);
EXPECT_EQ(output_tokens[0][0], 3);
// Call Decode for the 2nd time. The output tokens should be the 2nd decode
// tokens: 0.
ASSERT_OK_AND_ASSIGN(output_tokens, fake_llm_executor.Decode());
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 5);
EXPECT_EQ(output_tokens[0][0], 0);
// Call Decode for the 3nd time. Should fail.
EXPECT_THAT(fake_llm_executor.Decode(),
StatusIs(absl::StatusCode::kInvalidArgument));
}
TEST(FakeLlmExecutorTest, DecodeToLogits) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}};
const std::vector<std::vector<int>> decode_tokens_set = {{3}, {0}};
FakeLlmExecutor fake_llm_executor(/*vocab_size=*/4, prefill_tokens_set,
decode_tokens_set);
ExecutorInputs inputs;
const std::vector<int> input_tokens = {1, 2, 3};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(input_tokens), {1, 3}));
inputs.SetTextData(ExecutorTextData(std::move(input_tokens_buffer)));
EXPECT_OK(fake_llm_executor.Prefill(inputs));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 3);
// Create a tensor buffer with 3 elements but only the first two elements
// match the expected prefill tokens.
const std::vector<int> decode_input_tokens = {3};
LITERT_ASSERT_OK_AND_ASSIGN(
auto decode_input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(decode_input_tokens), {1, 1}));
inputs.SetTextData(ExecutorTextData(std::move(decode_input_tokens_buffer)));
auto output_logits = CreateTensorBuffer<float>({1, 1, 4});
// Call Decode for the 1st time. The output logits should have values:
// [-inf, -inf, -inf, inf].
EXPECT_OK(fake_llm_executor.Decode(inputs, *output_logits));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 4);
auto output_logits_span = ReferTensorBufferAsSpan<float>(*output_logits);
EXPECT_LE((*output_logits_span)[0], 0.0f);
EXPECT_LE((*output_logits_span)[1], 0.0f);
EXPECT_LE((*output_logits_span)[2], 0.0f);
EXPECT_GE((*output_logits_span)[3], 0.0f);
// Call Decode for the 2nd time. The output logits should have values:
// [inf, -inf, -inf, -inf].
EXPECT_OK(fake_llm_executor.Decode(inputs, *output_logits));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 5);
EXPECT_GE((*output_logits_span)[0], 0.0f);
EXPECT_LE((*output_logits_span)[1], 0.0f);
EXPECT_LE((*output_logits_span)[2], 0.0f);
EXPECT_LE((*output_logits_span)[3], 0.0f);
// Call Decode for the 3nd time. Should fail.
EXPECT_THAT(fake_llm_executor.Decode(inputs, *output_logits),
StatusIs(absl::StatusCode::kInvalidArgument));
}
TEST(FakeLlmExecutorTest, DecodeLogits) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}};
const std::vector<std::vector<int>> decode_tokens_set = {{3}, {0}};
FakeLlmExecutor fake_llm_executor(/*vocab_size=*/4, prefill_tokens_set,
decode_tokens_set);
ExecutorInputs inputs;
const std::vector<int> input_tokens = {1, 2, 3};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(input_tokens), {1, 3}));
inputs.SetTextData(ExecutorTextData(std::move(input_tokens_buffer)));
EXPECT_OK(fake_llm_executor.Prefill(inputs));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 3);
// Create a tensor buffer with 3 elements but only the first two elements
// match the expected prefill tokens.
const std::vector<int> decode_input_tokens = {3};
LITERT_ASSERT_OK_AND_ASSIGN(
auto decode_input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(decode_input_tokens), {1, 1}));
inputs.SetTextData(ExecutorTextData(std::move(decode_input_tokens_buffer)));
auto output_logits = fake_llm_executor.DecodeLogits(inputs);
// Call Decode for the 1st time. The output logits should have values:
// [-inf, -inf, -inf, inf].
EXPECT_TRUE(output_logits.ok());
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 4);
auto output_logits_span = ReferTensorBufferAsSpan<float>(*output_logits);
EXPECT_LE((*output_logits_span)[0], 0.0f);
EXPECT_LE((*output_logits_span)[1], 0.0f);
EXPECT_LE((*output_logits_span)[2], 0.0f);
EXPECT_GE((*output_logits_span)[3], 0.0f);
output_logits = fake_llm_executor.DecodeLogits(inputs);
// Call Decode for the 2nd time. The output logits should have values:
// [inf, -inf, -inf, -inf].
EXPECT_TRUE(output_logits.ok());
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 5);
output_logits_span = ReferTensorBufferAsSpan<float>(*output_logits);
EXPECT_GE((*output_logits_span)[0], 0.0f);
EXPECT_LE((*output_logits_span)[1], 0.0f);
EXPECT_LE((*output_logits_span)[2], 0.0f);
EXPECT_LE((*output_logits_span)[3], 0.0f);
// Call Decode for the 3nd time. Should fail.
EXPECT_THAT(fake_llm_executor.Decode(inputs, *output_logits),
StatusIs(absl::StatusCode::kInvalidArgument));
}
TEST(FakeLlmExecutorTest, DecodeDelay) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}};
const std::vector<std::vector<int>> decode_tokens_set = {{3}, {0}};
FakeLlmExecutor fake_llm_executor(/*vocab_size=*/4, prefill_tokens_set,
decode_tokens_set);
constexpr absl::Duration delay = absl::Milliseconds(100);
fake_llm_executor.SetDecodeDelay(delay);
ExecutorInputs inputs;
const std::vector<int> input_tokens = {1, 2, 3};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(input_tokens), {1, 3}));
inputs.SetTextData(ExecutorTextData(std::move(input_tokens_buffer)));
EXPECT_OK(fake_llm_executor.Prefill(inputs));
const absl::Time start = absl::Now();
ASSERT_OK_AND_ASSIGN(auto output_tokens, fake_llm_executor.Decode());
const absl::Duration elapsed = absl::Now() - start;
EXPECT_GE(elapsed, delay);
}
TEST(FakeLlmExecutorTest, MultiplePrefillTriggers) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}, {4, 5}};
const std::vector<std::vector<int>> decode_tokens_set = {{6}, {7}, {8}, {9}};
FakeLlmExecutor fake_llm_executor(/*vocab_size=*/10, prefill_tokens_set,
decode_tokens_set);
// Trigger the first prefill/decode sequence.
{
ExecutorInputs inputs;
const std::vector<int> input_tokens = {1, 2, 3};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(input_tokens), {1, 3}));
inputs.SetTextData(ExecutorTextData(std::move(input_tokens_buffer)));
EXPECT_OK(fake_llm_executor.Prefill(inputs));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 3);
ASSERT_OK_AND_ASSIGN(auto output_tokens, fake_llm_executor.Decode());
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 4);
EXPECT_EQ(output_tokens[0][0], 6);
ASSERT_OK_AND_ASSIGN(output_tokens, fake_llm_executor.Decode());
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 5);
EXPECT_EQ(output_tokens[0][0], 7);
}
// Trigger the second prefill/decode sequence.
{
ExecutorInputs inputs;
const std::vector<int> input_tokens = {4, 5};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(input_tokens), {1, 2}));
inputs.SetTextData(ExecutorTextData(std::move(input_tokens_buffer)));
EXPECT_OK(fake_llm_executor.Prefill(inputs));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 7);
ASSERT_OK_AND_ASSIGN(auto output_tokens, fake_llm_executor.Decode());
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 8);
EXPECT_EQ(output_tokens[0][0], 8);
ASSERT_OK_AND_ASSIGN(output_tokens, fake_llm_executor.Decode());
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 9);
EXPECT_EQ(output_tokens[0][0], 9);
}
// Call Prefill for the 3rd time. Should fail.
{
ExecutorInputs inputs;
const std::vector<int> input_tokens = {6};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(input_tokens), {1, 1}));
inputs.SetTextData(ExecutorTextData(std::move(input_tokens_buffer)));
EXPECT_THAT(fake_llm_executor.Prefill(inputs),
StatusIs(absl::StatusCode::kInvalidArgument));
}
}
TEST(FakeLlmExecutorTest, DecodeWithConstraint) {
const std::vector<std::vector<int>> prefill_tokens_set = {{1, 2, 3}};
const std::vector<std::vector<int>> decode_tokens_set = {{4}, {0}, {4}, {0}};
FakeLlmExecutor fake_llm_executor(/*vocab_size=*/10, prefill_tokens_set,
decode_tokens_set);
// Fake constraint that expects "4, 0".
const std::vector<int> expected_token_ids = {4, 0};
auto constraint = FakeConstraint(expected_token_ids, /*vocabulary_size=*/10);
ExecutorInputs inputs;
const std::vector<int> input_tokens = {1, 2, 3};
LITERT_ASSERT_OK_AND_ASSIGN(
auto input_tokens_buffer,
CopyToTensorBuffer<int>(absl::MakeSpan(input_tokens), {1, 3}));
inputs.SetTextData(ExecutorTextData(std::move(input_tokens_buffer)));
EXPECT_OK(fake_llm_executor.Prefill(inputs));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 3);
auto constrained_decoder =
std::make_unique<ConstrainedDecoder>(&constraint,
/*num_output_candidates=*/1);
auto decode_params = ExecutorDecodeParams();
decode_params.SetConstraintDecoder(constrained_decoder.get());
// Call Decode for the 1st time. The output tokens should be the 1st decode
// tokens: 4. (first constraint token)
ASSERT_OK_AND_ASSIGN(auto output_tokens,
fake_llm_executor.Decode(decode_params));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 4);
EXPECT_EQ(output_tokens[0][0], 4);
// Call Decode for the 2nd time. The output tokens should be the 2nd decode
// tokens: 0. (second constraint token)
ASSERT_OK_AND_ASSIGN(output_tokens, fake_llm_executor.Decode(decode_params));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 5);
EXPECT_EQ(output_tokens[0][0], 0);
// Call Decode for the 3rd time. The output tokens should be the 3rd decode
// tokens: 4. (first constraint token again)
ASSERT_OK_AND_ASSIGN(output_tokens, fake_llm_executor.Decode(decode_params));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 6);
EXPECT_EQ(output_tokens[0][0], 4);
// Call Decode for the 2nd time. The output tokens should be the 2nd decode
// tokens: 0. (second constraint token again)
ASSERT_OK_AND_ASSIGN(output_tokens, fake_llm_executor.Decode(decode_params));
EXPECT_EQ(fake_llm_executor.GetCurrentStep().value(), 7);
EXPECT_EQ(output_tokens[0][0], 0);
// Call Decode for the 5nd time. Should fail.
EXPECT_THAT(fake_llm_executor.Decode(decode_params),
StatusIs(absl::StatusCode::kInvalidArgument));
}
} // namespace
} // namespace litert::lm
|