Skip to content
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

Add an interface for LLM runner #6356

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 2 additions & 1 deletion examples/models/llama/runner/runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string>
#include <unordered_map>

#include <executorch/extension/llm/runner/irunner.h>
#include <executorch/extension/llm/runner/stats.h>
#include <executorch/extension/llm/runner/text_decoder_runner.h>
#include <executorch/extension/llm/runner/text_prefiller.h>
Expand All @@ -26,7 +27,7 @@

namespace example {

class ET_EXPERIMENTAL Runner {
class ET_EXPERIMENTAL Runner : public executorch::extension::llm::IRunner {
public:
explicit Runner(
const std::string& model_path,
Expand Down
1 change: 1 addition & 0 deletions examples/models/llama/runner/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def define_common_targets():
# qnn_executorch_backend can be added below //executorch/backends/qualcomm:qnn_executorch_backend
exported_deps = [
"//executorch/backends/xnnpack:xnnpack_backend",
"//executorch/extension/llm/runner:irunner",
"//executorch/extension/llm/runner:stats",
"//executorch/extension/llm/runner:text_decoder_runner" + aten_suffix,
"//executorch/extension/llm/runner:text_prefiller" + aten_suffix,
Expand Down
50 changes: 50 additions & 0 deletions extension/llm/runner/irunner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

// An interface for LLM runners. Developers can create their own runner that
// implements their own load and generation logic to run the model.

#pragma once

#include <functional>
#include <string>

#include <executorch/extension/llm/runner/stats.h>
#include <executorch/extension/module/module.h>

namespace executorch {
namespace extension {
namespace llm {

class ET_EXPERIMENTAL IRunner {
public:
virtual ~IRunner() = default;

// Checks if the model is loaded.
virtual bool is_loaded() const = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can have a default implementation right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not.

bool Runner::is_loaded() const {
return module_->is_loaded() && tokenizer_ && text_decoder_runner_ &&
text_prefiller_ && text_token_generator_;
}

it's quite impl detail


// Load the model and tokenizer.
virtual ::executorch::runtime::Error load() = 0;

// Generate the output tokens.
virtual ::executorch::runtime::Error generate(
const std::string& prompt,
int32_t seq_len,
std::function<void(const std::string&)> token_callback = {},
std::function<void(const ::executorch::extension::llm::Stats&)>
stats_callback = {},
bool echo = true,
bool warming = false) = 0;

// Stop the generation.
virtual void stop() = 0;
};

} // namespace llm
} // namespace extension
} // namespace executorch
10 changes: 10 additions & 0 deletions extension/llm/runner/targets.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

def define_common_targets():
runtime.cxx_library(
name = "irunner",
exported_headers = [
"irunner.h",
],
visibility = [
"@EXECUTORCH_CLIENTS",
],
)

runtime.cxx_library(
name = "stats",
exported_headers = [
Expand Down
Loading