Skip to content

Commit fcd7a6e

Browse files
committed
fix: fix unload in LLM and ModelHostObject to properly free memory
1 parent 6dd8fb6 commit fcd7a6e

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

packages/react-native-executorch/common/rnexecutorch/host_objects/ModelHostObject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
436436
JSI_HOST_FUNCTION(unload) {
437437
try {
438438
model->unload();
439+
thisValue.asObject(runtime).setExternalMemoryPressure(runtime, 0);
439440
} catch (const RnExecutorchError &e) {
440441
jsi::Object errorData(runtime);
441442
errorData.setProperty(runtime, "code", e.getNumericCode());

packages/react-native-executorch/common/rnexecutorch/models/llm/LLM.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ int32_t LLM::getMaxContextLength() const {
255255
return runner_->get_max_context_length();
256256
}
257257

258-
void LLM::unload() noexcept { runner_.reset(nullptr); }
258+
void LLM::unload() noexcept {
259+
runner.reset(nullptr);
260+
BaseModel::unload();
261+
}
259262

260263
} // namespace rnexecutorch::models::llm

packages/react-native-executorch/src/controllers/LLMController.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ export class LLMController {
121121
await ResourceFetcher.fs.readAsString(tokenizerConfigPath!)
122122
);
123123

124+
125+
if (this.nativeModule) {
126+
this.nativeModule.unload();
127+
}
128+
124129
this.nativeModule = await global.loadLLM(
125130
modelPath,
126131
tokenizerPath,

0 commit comments

Comments
 (0)