Skip to content

Commit fa72aa9

Browse files
author
Mateusz Kopciński
committed
Implemented GlobalThreadPool and added setting xnnpack threadpool for iOS
1 parent 3504761 commit fa72aa9

15 files changed

Lines changed: 282 additions & 455 deletions

File tree

.cspell-wordlist.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ softmax
6666
logit
6767
logits
6868
probs
69+
threadpool
70+
chrono
71+
setpriority
72+
errno
73+
ifdef
74+
elif
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<div align="right">
2+
<h1 align="left" style="display:inline-block">React Native ExecuTorch
3+
<!-- Discord Badge -->
4+
<a href="https://discord.gg/ZGqqY55qkP">
5+
<img src="https://img.shields.io/badge/Discord-Join%20Us-00008B?logo=discord&logoColor=white&style=for-the-badge" alt="Join our Discord community">
6+
</a>
7+
</h1>
8+
</div>
9+
10+
![Software Mansion banner](https://github.com/user-attachments/assets/fa2c4735-e75c-4cc1-970d-88905d95e3a4)
11+
12+
**React Native ExecuTorch** provides a declarative way to run AI models on-device using React Native, powered by **ExecuTorch** :rocket:. It offers out-of-the-box support for a wide range of LLMs, computer vision models, and more. Visit our [HuggingFace](https://huggingface.co/software-mansion) page to explore these models.
13+
14+
**ExecuTorch**, developed by Meta, is a novel framework allowing AI model execution on devices like mobile phones or microcontrollers.
15+
16+
React Native ExecuTorch bridges the gap between React Native and native platform capabilities, enabling developers to efficiently run local AI models on mobile devices. This can be achieved without the need for extensive expertise in native programming or machine learning.
17+
18+
[![npm version](https://img.shields.io/npm/v/react-native-executorch?color=00008B)](https://www.npmjs.com/package/react-native-executorch)
19+
[![CI](https://github.com/software-mansion/react-native-executorch/actions/workflows/ci.yml/badge.svg)](https://github.com/software-mansion/react-native-executorch/actions/workflows/ci.yml)
20+
21+
**Table of contents:**
22+
23+
- [:yin_yang: Supported versions](#yin_yang-supported-versions)
24+
- [:books: Documentation](#books-documentation)
25+
- [:earth_africa: Real-World Example](#earth_africa-real-world-example)
26+
- [:llama: Quickstart - Running Llama](#llama-quickstart---running-llama)
27+
- [:calling: Demo apps](#calling-demo-apps)
28+
- [:robot: Ready-made models](#robot-ready-made-models)
29+
- [:balance_scale: License](#balance_scale-license)
30+
- [:soon: What's next?](#soon-whats-next)
31+
32+
## :yin_yang: Supported versions
33+
34+
The minimal supported version are:
35+
* iOS 17.0
36+
* Android 13
37+
* React Native 0.76
38+
39+
> [!IMPORTANT]
40+
> React Native Executorch supports only the [New React Native architecture](https://reactnative.dev/architecture/landing-page).
41+
42+
## :books: Documentation
43+
44+
Check out how our library can help you build your React Native AI features by visiting our docs:
45+
https://docs.swmansion.com/react-native-executorch
46+
47+
## :earth_africa: Real-World Example
48+
49+
React Native ExecuTorch is powering [Private Mind](https://github.com/software-mansion-labs/private-mind), a privacy-first mobile AI app available on [App Store](https://apps.apple.com/gb/app/private-mind/id6746713439) and [Google Play](https://play.google.com/store/apps/details?id=com.swmansion.privatemind).
50+
51+
<img width="2720" height="1085" alt="Private Mind promo" src="https://github.com/user-attachments/assets/b12296fe-19ac-48fc-9726-da9242700346" />
52+
53+
## :llama: **Quickstart - Running Llama**
54+
55+
**Get started with AI-powered text generation in 3 easy steps!**
56+
57+
### :one: **Installation**
58+
59+
```bash
60+
# Install the package
61+
yarn add react-native-executorch
62+
# Depending on the platform, choose either iOS or Android
63+
yarn expo run:< ios | android >
64+
```
65+
66+
### :two: **Setup & Initialization**
67+
68+
Add this to your component file:
69+
70+
```tsx
71+
import {
72+
useLLM,
73+
LLAMA3_2_1B,
74+
Message
75+
} from 'react-native-executorch';
76+
77+
function MyComponent() {
78+
// Initialize the model 🚀
79+
const llm = useLLM({ model: LLAMA3_2_1B });
80+
// ... rest of your component
81+
}
82+
```
83+
84+
### :three: **Run the model!**
85+
86+
```tsx
87+
const handleGenerate = async () => {
88+
const chat: Message[] = [
89+
{ role: 'system', content: 'You are a helpful assistant' },
90+
{ role: 'user', content: 'What is the meaning of life?' }
91+
];
92+
93+
// Chat completion
94+
await llm.generate(chat);
95+
console.log('Llama says:', llm.response);
96+
};
97+
```
98+
99+
## :calling: Demo apps
100+
101+
We currently host a few example [apps](https://github.com/software-mansion/react-native-executorch/tree/main/apps) demonstrating use cases of our library:
102+
103+
- `llm` - Chat application showcasing use of LLMs
104+
- `speech-to-text` - Whisper model ready for transcription tasks
105+
- `computer-vision` - Computer vision related tasks
106+
- `text-embeddings` - Computing text representations for semantic search
107+
108+
If you would like to run demo app, navigate to its project directory and install dependencies with:
109+
110+
```bash
111+
yarn
112+
```
113+
114+
Then, depending on the platform, choose either iOS or Android:
115+
116+
```bash
117+
yarn expo run:< ios | android >
118+
```
119+
120+
> [!WARNING]
121+
> Running LLMs requires a significant amount of RAM. If you are encountering unexpected app crashes, try to increase the amount of RAM allocated to the emulator.
122+
123+
## :robot: Ready-made models
124+
125+
Our library has a number of ready-to-use AI models; a complete list is available in the documentation. If you're interested in running your own AI model, you need to first export it to the `.pte` format. Instructions on how to do this are available in the [Python API](https://pypi.org/project/executorch/).
126+
127+
## :balance_scale: License
128+
129+
This library is licensed under [The MIT License](./LICENSE).
130+
131+
## :soon: What's next?
132+
133+
To learn about our upcoming plans and developments, please visit our [milestones](https://github.com/software-mansion/react-native-executorch/milestones).
134+
135+
## React Native ExecuTorch is created by Software Mansion
136+
137+
Since 2012, [Software Mansion](https://swmansion.com) is a software agency with experience in building web and mobile apps. We are Core React Native Contributors and experts in dealing with all kinds of React Native issues. We can help you build your next dream product – [Hire us](https://swmansion.com/contact/projects?utm_source=react-native-executorch&utm_medium=readme).
138+
139+
[![swm](https://logo.swmansion.com/logo?color=white&variant=desktop&width=150&tag=react-native-executorch-github 'Software Mansion')](https://swmansion.com)

packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "RnExecutorchInstaller.h"
22

3-
#include <rnexecutorch/GlobalThreadPool.h>
43
#include <rnexecutorch/Log.h>
54
#include <rnexecutorch/TokenizerModule.h>
65
#include <rnexecutorch/host_objects/JsiConversions.h>
@@ -14,12 +13,8 @@
1413
#include <rnexecutorch/models/speech_to_text/SpeechToText.h>
1514
#include <rnexecutorch/models/style_transfer/StyleTransfer.h>
1615
#include <rnexecutorch/models/vertical_ocr/VerticalOCR.h>
17-
18-
#if defined(__ANDROID__) && defined(__aarch64__)
19-
#include <executorch/extension/threadpool/cpuinfo_utils.h>
20-
#include <executorch/extension/threadpool/threadpool.h>
21-
#include <rnexecutorch/Log.h>
22-
#endif
16+
#include <rnexecutorch/threads/GlobalThreadPool.h>
17+
#include <rnexecutorch/threads/ThreadUtils.h>
2318

2419
namespace rnexecutorch {
2520

@@ -94,24 +89,8 @@ void RnExecutorchInstaller::injectJSIBindings(
9489
RnExecutorchInstaller::loadModel<models::speech_to_text::SpeechToText>(
9590
jsiRuntime, jsCallInvoker, "loadSpeechToText"));
9691

97-
#if defined(__ANDROID__) && defined(__aarch64__)
98-
auto num_of_perf_cores =
99-
::executorch::extension::cpuinfo::get_num_performant_cores();
100-
log(LOG_LEVEL::Info, "Detected ", num_of_perf_cores, " performant cores");
101-
// setting num_of_cores to floor(num_of_perf_cores / 2) + 1) because depending
102-
// on cpu arch as when possible we want to leave at least 2 performant cores
103-
// for other tasks (setting more actually results in drop of performance). For
104-
// older devices (i.e. samsung s22) resolves to 3 cores, and for newer ones
105-
// (like OnePlus 12) resolves to 4, which when benchamrked gives highest
106-
// throughput.
107-
auto num_of_cores = static_cast<uint32_t>(num_of_perf_cores / 2) + 1;
108-
::executorch::extension::threadpool::get_threadpool()
109-
->_unsafe_reset_threadpool(num_of_cores);
110-
log(LOG_LEVEL::Info, "Configuring xnnpack for ", num_of_cores, " threads");
111-
#endif
112-
113-
ThreadConfig config;
114-
GlobalThreadPool::initialize(2, config);
92+
threads::ThreadUtils::unsafeSetupThreadPool();
93+
threads::GlobalThreadPool::initialize();
11594
}
11695

117-
} // namespace rnexecutorch
96+
} // namespace rnexecutorch

0 commit comments

Comments
 (0)