Skip to content
Open
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
7 changes: 7 additions & 0 deletions examples/models/llama/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# Copyright 2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -169,6 +170,12 @@ if(TARGET vulkan_backend)
executorch_target_link_options_shared_lib(vulkan_backend)
endif()

# VGF backend
if(TARGET vgf_backend)
list(APPEND link_libraries vgf_backend)
executorch_target_link_options_shared_lib(vgf_backend)
endif()

# Qnn backend
if(TARGET qnn_executorch_backend)
list(APPEND link_libraries qnn_executorch_backend)
Expand Down
10 changes: 10 additions & 0 deletions extension/llm/runner/multimodal_decoder_runner.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2026 Arm Limited and/or its affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -26,6 +27,15 @@ class ET_EXPERIMENTAL MultimodalDecoderRunner
std::move(method_name),
std::move(sampler)) {}

inline executorch::runtime::Result<executorch::aten::ScalarType>
token_input_scalar_type() const override {
auto method_meta = module_->method_meta(kTokenEmbeddingMethod);
ET_CHECK_OK_OR_RETURN_ERROR(method_meta.error());
auto input_meta = method_meta->input_tensor_meta(0);
ET_CHECK_OK_OR_RETURN_ERROR(input_meta.error());
return input_meta->scalar_type();
}

/**
* Step the LLM Decoder with the given tokens and start position.
* @param tokens The tokens to the LLM.
Expand Down
33 changes: 33 additions & 0 deletions extension/llm/runner/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# Copyright 2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -70,13 +71,45 @@ et_cxx_test(
test_runner SOURCES ${_test_srcs} EXTRA_LIBS executorch extension_llm_runner
)
add_dependencies(test_runner generated_model_metadata_test_files)
set(_int32_pte_files
"${CMAKE_CURRENT_BINARY_DIR}/static_int32.pte"
"${CMAKE_CURRENT_BINARY_DIR}/kv_int32.pte"
"${CMAKE_CURRENT_BINARY_DIR}/static_int64.pte"
"${CMAKE_CURRENT_BINARY_DIR}/kv_int64.pte"
"${CMAKE_CURRENT_BINARY_DIR}/multimodal_int32.pte"
"${CMAKE_CURRENT_BINARY_DIR}/multimodal_int64.pte"
)
add_custom_command(
OUTPUT ${_int32_pte_files}
COMMAND
${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/export_int32_runner_models.py --outdir
${CMAKE_CURRENT_BINARY_DIR}
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/export_int32_runner_models.py
)
add_custom_target(generated_int32_runner_models DEPENDS ${_int32_pte_files})
add_dependencies(test_runner generated_int32_runner_models)
set_property(
TEST test_runner
PROPERTY
ENVIRONMENT
"ET_MODEL_METADATA_FULL_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_full.pte;ET_MODEL_METADATA_LAST_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_last.pte;ET_MODEL_METADATA_SELECTED_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_selected.pte;ET_MODEL_METADATA_INVALID_CONTEXT_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_invalid_context.pte;ET_MODEL_METADATA_INVALID_PREFILL_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_invalid_prefill.pte;ET_MODEL_METADATA_INVALID_VOCAB_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_invalid_vocab.pte;ET_MODEL_METADATA_MISSING_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_missing.pte;ET_MODEL_METADATA_GEOMETRY_WRONG_TYPE_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_geometry_wrong_type.pte;ET_MODEL_METADATA_GEOMETRY_MISMATCHED_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_geometry_mismatched.pte;ET_MODEL_METADATA_GEOMETRY_INVALID_HEADS_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_geometry_invalid_heads.pte;ET_MODEL_METADATA_GEOMETRY_INVALID_DIMS_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_geometry_invalid_dims.pte;ET_MODEL_METADATA_GEOMETRY_INVALID_WINDOWS_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_geometry_invalid_windows.pte;ET_MODEL_METADATA_GEOMETRY_EMPTY_PATH=${CMAKE_CURRENT_BINARY_DIR}/ModelMetadata_geometry_empty.pte"
)

set_property(
TEST test_runner
APPEND
PROPERTY
ENVIRONMENT
"ET_STATIC_INT32_MODEL=${CMAKE_CURRENT_BINARY_DIR}/static_int32.pte"
"ET_KV_INT32_MODEL=${CMAKE_CURRENT_BINARY_DIR}/kv_int32.pte"
"ET_STATIC_INT64_MODEL=${CMAKE_CURRENT_BINARY_DIR}/static_int64.pte"
"ET_KV_INT64_MODEL=${CMAKE_CURRENT_BINARY_DIR}/kv_int64.pte"
"ET_MULTIMODAL_INT32_MODEL=${CMAKE_CURRENT_BINARY_DIR}/multimodal_int32.pte"
"ET_MULTIMODAL_INT64_MODEL=${CMAKE_CURRENT_BINARY_DIR}/multimodal_int64.pte"
)

# Override sanitizer to this issue:
# https://github.com/abseil/abseil-cpp/issues/841 Root issue:
# https://github.com/llvm/llvm-project/issues/16778
Expand Down
62 changes: 62 additions & 0 deletions extension/llm/runner/test/export_int32_runner_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

import argparse
from pathlib import Path

import torch
from executorch.exir import to_edge


class StaticTokens(torch.nn.Module):
def forward(self, tokens):
values = tokens.to(torch.float32)
return torch.stack((values, values + 1, values + 2), dim=-1)


class TokensAndPosition(torch.nn.Module):
def forward(self, tokens, position):
values = tokens.to(torch.float32)
positions = position.reshape(1, 1).to(torch.float32)
return torch.stack((values, positions, values + positions), dim=-1)


def main():
parser = argparse.ArgumentParser()
parser.add_argument("--outdir", type=Path, required=True)
args = parser.parse_args()
args.outdir.mkdir(parents=True, exist_ok=True)
cases = [
("static_int32.pte", StaticTokens(), (torch.ones((1, 4), dtype=torch.int32),)),
(
"kv_int32.pte",
TokensAndPosition(),
(torch.ones((1, 1), dtype=torch.int32), torch.ones(1, dtype=torch.int32)),
),
("static_int64.pte", StaticTokens(), (torch.ones((1, 4), dtype=torch.int64),)),
(
"kv_int64.pte",
TokensAndPosition(),
(torch.ones((1, 1), dtype=torch.int64), torch.ones(1, dtype=torch.int64)),
),
]
for name, model, inputs in cases:
program = to_edge(torch.export.export(model, inputs)).to_executorch()
(args.outdir / name).write_bytes(program.buffer)

for dtype in (torch.int32, torch.int64):
name = f"multimodal_{str(dtype).removeprefix('torch.')}.pte"
model = StaticTokens()
inputs = (torch.ones((1, 1), dtype=dtype),)
program = to_edge(
{
"token_embedding": torch.export.export(model, inputs),
}
).to_executorch()
(args.outdir / name).write_bytes(program.buffer)


if __name__ == "__main__":
main()
195 changes: 194 additions & 1 deletion extension/llm/runner/test/test_text_decoder_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
*/

#include <executorch/extension/llm/runner/io_manager/io_manager.h>
#include <executorch/extension/llm/runner/multimodal_decoder_runner.h>
#include <executorch/extension/llm/runner/text_decoder_runner.h>
#include <executorch/extension/module/module.h>
#include <executorch/extension/tensor/tensor.h>
#include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <cstdlib>
#include <limits>

using namespace ::testing;
using executorch::extension::Module;
Expand All @@ -30,6 +32,16 @@ class MockModule : public Module {
MockModule() : Module("") {}
};

class TokenInputModule : public Module {
public:
explicit TokenInputModule(const std::string& path) : Module(path) {}
MOCK_METHOD(
executorch::runtime::Result<std::vector<executorch::runtime::EValue>>,
execute,
(const std::string&, const std::vector<executorch::runtime::EValue>&),
(override));
};

class TextDecoderRunnerTest : public Test {
protected:
void SetUp() override {
Expand All @@ -50,6 +62,187 @@ TEST_F(TextDecoderRunnerTest, MethodNameDefaultsToForward) {
EXPECT_EQ(runner_->method_name(), "forward");
}

TEST_F(TextDecoderRunnerTest, StaticInt32AcceptsTypedPrefixes) {
const char* path = std::getenv("ET_STATIC_INT32_MODEL");
if (!path) {
GTEST_SKIP() << "Set ET_STATIC_INT32_MODEL to the generated test fixture";
}
Module module(path);
IOManager io(module);
TextDecoderRunner runner(&module, &io, "forward", nullptr, nullptr, false);
auto dtype = runner.token_input_scalar_type();
ASSERT_TRUE(dtype.ok());
EXPECT_EQ(dtype.get(), executorch::aten::ScalarType::Int);
for (int size : {1, 2, 4}) {
auto tokens = executorch::extension::make_tensor_ptr(
{1, size}, std::vector<int32_t>(size, 11));
auto result = runner.step(tokens, 0);
ASSERT_TRUE(result.ok());
ASSERT_EQ(result->dim(), 2);
ASSERT_EQ(result->size(1), 3);
const auto* data = result->const_data_ptr<float>();
EXPECT_EQ(data[0], 11.0f);
EXPECT_EQ(data[1], 12.0f);
EXPECT_EQ(data[2], 13.0f);
}
auto wrong_type =
executorch::extension::make_tensor_ptr({1, 1}, std::vector<int64_t>{11});
EXPECT_EQ(runner.step(wrong_type, 0).error(), Error::InvalidArgument);
auto too_long = executorch::extension::make_tensor_ptr(
{1, 5}, std::vector<int32_t>(5, 11));
EXPECT_EQ(runner.step(too_long, 0).error(), Error::InvalidArgument);
}

TEST_F(TextDecoderRunnerTest, StaticInt64AcceptsTypedPrefixes) {
const char* path = std::getenv("ET_STATIC_INT64_MODEL");
if (!path) {
GTEST_SKIP() << "Set ET_STATIC_INT64_MODEL to the generated test fixture";
}
Module module(path);
IOManager io(module);
TextDecoderRunner runner(&module, &io, "forward", nullptr, nullptr, false);
auto dtype = runner.token_input_scalar_type();
ASSERT_TRUE(dtype.ok());
EXPECT_EQ(dtype.get(), executorch::aten::ScalarType::Long);
for (int size : {1, 2, 4}) {
auto tokens = executorch::extension::make_tensor_ptr(
{1, size}, std::vector<int64_t>(size, 11));
auto result = runner.step(tokens, 0);
ASSERT_TRUE(result.ok());
ASSERT_EQ(result->dim(), 2);
ASSERT_EQ(result->size(1), 3);
const auto* data = result->const_data_ptr<float>();
EXPECT_EQ(data[0], 11.0f);
EXPECT_EQ(data[1], 12.0f);
EXPECT_EQ(data[2], 13.0f);
}
auto wrong_type =
executorch::extension::make_tensor_ptr({1, 1}, std::vector<int32_t>{11});
EXPECT_EQ(runner.step(wrong_type, 0).error(), Error::InvalidArgument);
}

TEST_F(TextDecoderRunnerTest, Int32KvAcceptsTypedTokensAndPositions) {
const char* path = std::getenv("ET_KV_INT32_MODEL");
if (!path) {
GTEST_SKIP() << "Set ET_KV_INT32_MODEL to the generated test fixture";
}
Module module(path);
IOManager io(module);
TextDecoderRunner runner(&module, &io);
auto tokens =
executorch::extension::make_tensor_ptr({1, 1}, std::vector<int32_t>{11});
auto result = runner.step(tokens, 3);
ASSERT_TRUE(result.ok());
const auto* data = result->const_data_ptr<float>();
EXPECT_EQ(data[0], 11.0f);
EXPECT_EQ(data[1], 3.0f);
EXPECT_EQ(data[2], 14.0f);
auto int32_tokens =
executorch::extension::make_tensor_ptr({1, 1}, std::vector<int32_t>{12});
auto int32_result = runner.step(int32_tokens, 4);
ASSERT_TRUE(int32_result.ok());
data = int32_result->const_data_ptr<float>();
EXPECT_EQ(data[0], 12.0f);
EXPECT_EQ(data[1], 4.0f);
EXPECT_EQ(data[2], 16.0f);
EXPECT_EQ(
runner
.step(
tokens,
static_cast<int64_t>(std::numeric_limits<int32_t>::max()) + 1)
.error(),
Error::InvalidArgument);
tokens = executorch::extension::make_tensor_ptr(
{1, 1},
std::vector<int64_t>{
static_cast<int64_t>(std::numeric_limits<int32_t>::max()) + 1});
EXPECT_EQ(runner.step(tokens, 0).error(), Error::InvalidArgument);
}

TEST_F(TextDecoderRunnerTest, Int64KvAcceptsTypedTokensAndPositions) {
const char* path = std::getenv("ET_KV_INT64_MODEL");
if (!path) {
GTEST_SKIP() << "Set ET_KV_INT64_MODEL to the generated test fixture";
}
Module module(path);
IOManager io(module);
TextDecoderRunner runner(&module, &io);
auto tokens =
executorch::extension::make_tensor_ptr({1, 1}, std::vector<int64_t>{11});
auto result = runner.step(tokens, 3);
ASSERT_TRUE(result.ok());
const auto* data = result->const_data_ptr<float>();
EXPECT_EQ(data[0], 11.0f);
EXPECT_EQ(data[1], 3.0f);
EXPECT_EQ(data[2], 14.0f);
auto wrong_type =
executorch::extension::make_tensor_ptr({1, 1}, std::vector<int32_t>{11});
EXPECT_EQ(runner.step(wrong_type, 0).error(), Error::InvalidArgument);
}

TEST_F(TextDecoderRunnerTest, MultimodalReadsTokenEmbeddingInputType) {
for (bool use_int32 : {false, true}) {
const char* path = std::getenv(
use_int32 ? "ET_MULTIMODAL_INT32_MODEL" : "ET_MULTIMODAL_INT64_MODEL");
if (!path) {
GTEST_SKIP() << "Generate the multimodal token fixtures";
}
Module module(path);
IOManager io(module);
executorch::extension::llm::MultimodalDecoderRunner runner(&module, &io);
auto methods = module.method_names();
ASSERT_TRUE(methods.ok());
EXPECT_EQ(methods->count("forward"), 0);
auto dtype = runner.token_input_scalar_type();
ASSERT_TRUE(dtype.ok());
EXPECT_EQ(
dtype.get(),
use_int32 ? executorch::aten::ScalarType::Int
: executorch::aten::ScalarType::Long);
}
}

TEST_F(TextDecoderRunnerTest, UnpaddedTokenInputsAreNotCopied) {
for (const char* fixture :
{"ET_KV_INT32_MODEL",
"ET_KV_INT64_MODEL",
"ET_STATIC_INT32_MODEL",
"ET_STATIC_INT64_MODEL"}) {
const char* path = std::getenv(fixture);
if (!path) {
GTEST_SKIP() << "Generate the typed token fixtures";
}
for (bool dynamic : {false, true}) {
SCOPED_TRACE(fixture);
SCOPED_TRACE(dynamic);
TokenInputModule module(path);
IOManager io(module);
TextDecoderRunner runner(
&module, &io, "forward", nullptr, nullptr, dynamic);
auto meta = module.method_meta("forward");
ASSERT_TRUE(meta.ok());
auto input = meta->input_tensor_meta(0);
ASSERT_TRUE(input.ok());
auto length = input->sizes()[1];
auto tokens = input->scalar_type() == executorch::aten::ScalarType::Int
? executorch::extension::make_tensor_ptr(
{1, length}, std::vector<int32_t>(length, 11))
: executorch::extension::make_tensor_ptr(
{1, length}, std::vector<int64_t>(length, 11));
EXPECT_CALL(module, execute("forward", _))
.WillOnce(
[&](const std::string& name,
const std::vector<executorch::runtime::EValue>& inputs) {
EXPECT_EQ(
inputs[0].toTensor().const_data_ptr(),
tokens->const_data_ptr());
return module.Module::execute(name, inputs);
});
ASSERT_TRUE(runner.step(tokens, 0).ok());
}
}
}

// Test that method_name can be set to a custom value
TEST_F(TextDecoderRunnerTest, MethodNameCustomValue) {
auto custom_runner = std::make_unique<TextDecoderRunner>(
Expand Down Expand Up @@ -329,7 +522,7 @@ TEST_F(TextDecoderRunnerTest, StaticNonKVShortPrompt) {
ASSERT_EQ(runner.load(), Error::Ok);

auto tokens = executorch::extension::make_tensor_ptr(
{1, 2}, std::vector<int64_t>{7, 8});
{1, 2}, std::vector<int32_t>{7, 8});
auto result = runner.step(tokens, 0);
ASSERT_TRUE(result.ok()) << static_cast<int>(result.error());
ASSERT_EQ(result->dim(), 2);
Expand Down
Loading
Loading