-
Notifications
You must be signed in to change notification settings - Fork 75
feat: port LLMs to C++ #415
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
e299c3f
chore: add runner to includes
chmjkb 45938f3
wip: add an example header
chmjkb 5cda73f
feat: support void functions in ModelHostObject
chmjkb 0383fcc
feat: adjust installer to work with llms
chmjkb 6a217bf
feat: add JSI conversion for js callbacks
chmjkb d985c25
feat: add llm to modelhostobject
chmjkb 189440a
feat: add LLM runner
chmjkb 6e6703d
feat: adjust controller to match the new native impl
chmjkb 6fdd91b
fix: check if native llm is installed
chmjkb bc83f01
remove a bunch of code 💅🏻
chmjkb 158265f
remove a bunch of code 💅🏻
chmjkb b00c5f0
chore: move runner to common/
chmjkb 6fdf271
chore: update runner.h
chmjkb 9264242
chore: update xcframework
chmjkb 3740b5b
chore: get rid of tokenizers_c.h
chmjkb 2c5bd57
chore: update executorchlib xcodeproj
chmjkb 23d61ff
chore: rename runner.{h,cpp} to LLM.{h,cpp}
chmjkb ec40ff8
fix: define moduleInfos
chmjkb 4f2810e
fix: fix includes after renaming runner
chmjkb 64785cc
chore: move executorch ios libs, add tokenizers-cpp static libs
chmjkb c3a7d17
fix: update podspec to match the new ios libs structure
chmjkb fcda895
wip: android cmake
chmjkb 50b19cc
chore: unify memory lower bound member naming with BaseModel.cpp
chmjkb d80e855
chore: remove Android static-libs for tokenizers-cpp as they are not …
chmjkb 5117e65
chore: remove --force_load flag from tokenizers-cpp static libs
chmjkb c3b1a84
fix: Ensure corectness of podspec libs path, add typing to llm contro…
chmjkb afb1912
chore: remove outdated comment
chmjkb 2cf6c6a
feat: add generic synchronous host function wraper
chmjkb 2acd171
chore: remove accidental export of getMemoryLowerBound
chmjkb ffe6387
fix: handle void returns in synchronous host function wrapper
chmjkb 3826a29
chore: remove log includesw
chmjkb cf72d6a
chore: make functions noexcept & const
chmjkb ec82b0e
logging: improve error messages
chmjkb f23587b
chore: remove unused include
chmjkb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
packages/react-native-executorch/android/src/main/java/com/swmansion/rnexecutorch/LLM.kt
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/react-native-executorch/common/rnexecutorch/models/llm/LLM.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #include "LLM.h" | ||
|
|
||
| #include <executorch/extension/tensor/tensor.h> | ||
| #include <filesystem> | ||
|
|
||
| namespace rnexecutorch { | ||
| using namespace facebook; | ||
| using executorch::extension::TensorPtr; | ||
| using executorch::runtime::Error; | ||
|
|
||
| LLM::LLM(const std::string &modelSource, const std::string &tokenizerSource, | ||
| std::shared_ptr<react::CallInvoker> callInvoker) | ||
| : runner(std::make_unique<example::Runner>(modelSource, tokenizerSource)), | ||
| callInvoker(callInvoker) { | ||
| auto loadResult = runner->load(); | ||
| if (loadResult != Error::Ok) { | ||
| throw std::runtime_error("Failed to load LLM runner, error code: " + | ||
| std::to_string(static_cast<int>(loadResult))); | ||
| } | ||
| memorySizeLowerBound = | ||
| std::filesystem::file_size(std::filesystem::path(modelSource)) + | ||
| std::filesystem::file_size(std::filesystem::path(tokenizerSource)); | ||
| } | ||
|
|
||
| void LLM::generate(std::string input, std::shared_ptr<jsi::Function> callback) { | ||
| if (!runner || !runner->is_loaded()) { | ||
| throw std::runtime_error("Runner is not loaded"); | ||
| } | ||
|
|
||
| // Create a native callback that will invoke the JS callback on the JS thread | ||
| auto nativeCallback = [this, callback](const std::string &token) { | ||
| callInvoker->invokeAsync([callback, token](jsi::Runtime &runtime) { | ||
| callback->call(runtime, jsi::String::createFromUtf8(runtime, token)); | ||
| }); | ||
| }; | ||
|
|
||
| auto error = runner->generate(input, nativeCallback, {}, false); | ||
| if (error != executorch::runtime::Error::Ok) { | ||
| throw std::runtime_error("Failed to generate text, error code: " + | ||
| std::to_string(static_cast<int>(error))); | ||
| } | ||
| } | ||
|
|
||
| void LLM::interrupt() { | ||
| if (!runner || !runner->is_loaded()) { | ||
| throw std::runtime_error("Can't interrupt a model that's not loaded!"); | ||
| } | ||
| runner->stop(); | ||
| } | ||
|
|
||
| std::size_t LLM::getMemoryLowerBound() const noexcept { | ||
| return memorySizeLowerBound; | ||
| } | ||
|
|
||
| void LLM::unload() noexcept { runner.reset(nullptr); } | ||
|
|
||
| } // namespace rnexecutorch | ||
29 changes: 29 additions & 0 deletions
29
packages/react-native-executorch/common/rnexecutorch/models/llm/LLM.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include <ReactCommon/CallInvoker.h> | ||
| #include <jsi/jsi.h> | ||
| #include <runner/runner.h> | ||
|
|
||
| namespace rnexecutorch { | ||
| using namespace facebook; | ||
|
|
||
| class LLM { | ||
| public: | ||
| explicit LLM(const std::string &modelSource, | ||
| const std::string &tokenizerSource, | ||
| std::shared_ptr<react::CallInvoker> callInvoker); | ||
|
|
||
| void generate(std::string input, std::shared_ptr<jsi::Function> callback); | ||
| void interrupt(); | ||
| void unload() noexcept; | ||
| std::size_t getMemoryLowerBound() const noexcept; | ||
|
|
||
| private: | ||
| size_t memorySizeLowerBound; | ||
| std::unique_ptr<example::Runner> runner; | ||
| std::shared_ptr<react::CallInvoker> callInvoker; | ||
| }; | ||
| } // namespace rnexecutorch |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.