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

fix: issue with isModelGenerating when switching between multiple models #73

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 3 additions & 6 deletions android/src/main/java/com/swmansion/rnexecutorch/LLM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class LLM(reactContext: ReactApplicationContext) :
private fun initializeLlamaModule(modelPath: String, tokenizerPath: String, promise: Promise) {
llamaModule = LlamaModule(1, modelPath, tokenizerPath, 0.7f)
isFetching = false
this.tempLlamaResponse.clear()
promise.resolve("Model loaded successfully")
}

Expand All @@ -74,8 +75,8 @@ class LLM(reactContext: ReactApplicationContext) :
contextWindowLength: Double,
promise: Promise
) {
if (llamaModule != null || isFetching) {
promise.reject("Model already loaded", "Model is already loaded or fetching")
if (isFetching) {
promise.reject("Model already loaded", "Model is already fetching")
Copy link
Contributor

Choose a reason for hiding this comment

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

The first argument seems wrong

return
}

Expand Down Expand Up @@ -148,10 +149,6 @@ class LLM(reactContext: ReactApplicationContext) :
llamaModule!!.stop()
}

override fun deleteModule() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we getting rid of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed that it doesn't really change memory usage, but let's leave it for now as it's not breaking anything

llamaModule = null
}

companion object {
const val NAME = "LLM"
}
Expand Down
25 changes: 11 additions & 14 deletions ios/RnExecutorch/LLM.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ - (instancetype)init {
isFetching = NO;
tempLlamaResponse = [[NSMutableString alloc] init];
}

return self;
}

Expand All @@ -38,7 +38,7 @@ - (void)onResult:(NSString *)token prompt:(NSString *)prompt {
if ([token isEqualToString:prompt]) {
return;
}

dispatch_async(dispatch_get_main_queue(), ^{
[self emitOnToken:token];
[self->tempLlamaResponse appendString:token];
Expand All @@ -54,8 +54,8 @@ - (void)updateDownloadProgress:(NSNumber *)progress {
- (void)loadLLM:(NSString *)modelSource tokenizerSource:(NSString *)tokenizerSource systemPrompt:(NSString *)systemPrompt contextWindowLength:(double)contextWindowLength resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
NSURL *modelURL = [NSURL URLWithString:modelSource];
NSURL *tokenizerURL = [NSURL URLWithString:tokenizerSource];
if(self->runner || isFetching){

if(isFetching){
reject(@"model_already_loaded", @"Model and tokenizer already loaded", nil);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this error message correct? you're not checking if its loaded

return;
}
Expand All @@ -78,10 +78,11 @@ - (void)loadLLM:(NSString *)modelSource tokenizerSource:(NSString *)tokenizerSou

modelFetcher.onFinish = ^(NSString *modelFilePath) {
self->runner = [[LLaMARunner alloc] initWithModelPath:modelFilePath tokenizerPath:tokenizerFilePath];
NSUInteger contextWindowLengthUInt = (NSUInteger)round(contextWindowLength);
NSUInteger contextWindowLengthUInt = (NSUInteger)round(contextWindowLength);

self->conversationManager = [[ConversationManager alloc] initWithNumMessagesContextWindow: contextWindowLengthUInt systemPrompt: systemPrompt];
self->isFetching = NO;
self->tempLlamaResponse = [NSMutableString string];
resolve(@"Model and tokenizer loaded successfully");
return;
};
Expand All @@ -94,23 +95,23 @@ - (void)loadLLM:(NSString *)modelSource tokenizerSource:(NSString *)tokenizerSou
- (void) runInference:(NSString *)input resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
[conversationManager addResponse:input senderRole:ChatRole::USER];
NSString *prompt = [conversationManager getConversation];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
[self->runner generate:prompt withTokenCallback:^(NSString *token) {
[self onResult:token prompt:prompt];
[self onResult:token prompt:prompt];
} error:&error];

// make sure to add eot token once generation is done
if (![self->tempLlamaResponse hasSuffix:END_OF_TEXT_TOKEN_NS]) {
[self onResult:END_OF_TEXT_TOKEN_NS prompt:prompt];
}

if (self->tempLlamaResponse) {
[self->conversationManager addResponse:self->tempLlamaResponse senderRole:ChatRole::ASSISTANT];
self->tempLlamaResponse = [NSMutableString string];
}

if (error) {
reject(@"error_in_generation", error.localizedDescription, nil);
return;
Expand All @@ -125,10 +126,6 @@ -(void)interrupt {
[self->runner stop];
}

-(void)deleteModule {
self->runner = nil;
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeLLMSpecJSI>(params);
Expand Down
4 changes: 2 additions & 2 deletions ios/RnExecutorch/utils/LargeFileFetcher.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ @implementation LargeFileFetcher {
- (instancetype)init {
self = [super init];
if (self) {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.swmansion.rnexecutorch"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"com.swmansion.rnexecutorch.%@", [[NSUUID UUID] UUIDString]]];
_session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
}
return self;
Expand Down Expand Up @@ -111,7 +111,7 @@ - (void)startDownloadingFileFromURL:(NSURL *)url {

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSFileManager *fileManager = [NSFileManager defaultManager];

[fileManager removeItemAtPath:_destination error:nil];

NSError *error;
Expand Down
11 changes: 3 additions & 8 deletions src/LLM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,8 @@ export const useLLM = ({
const [downloadProgress, setDownloadProgress] = useState(0);
const downloadProgressListener = useRef<null | EventSubscription>(null);
const tokenGeneratedListener = useRef<null | EventSubscription>(null);
const initialized = useRef(false);

useEffect(() => {
if (initialized.current) {
return;
}

initialized.current = true;

const loadModel = async () => {
try {
let modelUrl = modelSource;
Expand All @@ -57,6 +50,7 @@ export const useLLM = ({
}
}
);
setIsReady(false);

await LLM.loadLLM(
modelUrl as string,
Expand All @@ -83,6 +77,8 @@ export const useLLM = ({
const message = (err as Error).message;
setIsReady(false);
setError(message);
} finally {
setDownloadProgress(0);
}
};

Expand All @@ -93,7 +89,6 @@ export const useLLM = ({
downloadProgressListener.current = null;
tokenGeneratedListener.current?.remove();
tokenGeneratedListener.current = null;
LLM.deleteModule();
};
}, [contextWindowLength, modelSource, systemPrompt, tokenizerSource]);

Expand Down
1 change: 0 additions & 1 deletion src/native/NativeLLM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export interface Spec extends TurboModule {
contextWindowLength: number
): Promise<string>;
runInference(input: string): Promise<string>;
deleteModule(): void;
interrupt(): void;

readonly onToken: EventEmitter<string>;
Expand Down
Loading