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

External libs #34

Merged
merged 3 commits into from
Nov 26, 2024
Merged
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
2 changes: 2 additions & 0 deletions third-party/ios/ExecutorchLib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
output
build
608 changes: 608 additions & 0 deletions third-party/ios/ExecutorchLib/ExecutorchLib.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions third-party/ios/ExecutorchLib/ExecutorchLib/Exported/ETModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// ETModle.h
// ExecutorchLib
//
// Created by NorbertKlockiewicz on 21/11/2024.
//
#import <UIKit/UIKit.h>

@interface ETModel: NSObject

-(instancetype)loadModel:(NSString *)filePath;

-(void)forward;

@end
25 changes: 25 additions & 0 deletions third-party/ios/ExecutorchLib/ExecutorchLib/Exported/ETModel.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// ETModle.mm
// ExecutorchLib
//
// Created by NorbertKlockiewicz on 21/11/2024.
//
#import "ETModel.h"
#import "model.h"

@implementation ETModel {
std::unique_ptr<Model> _model;
}

-(instancetype)loadModel:(NSString *)filePath {
_model = std::make_unique<Model>(filePath.UTF8String);

return self;
}

-(void)forward{
_model->forward();
return;
}

@end
54 changes: 54 additions & 0 deletions third-party/ios/ExecutorchLib/ExecutorchLib/Exported/LLaMARunner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.
*/

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

FOUNDATION_EXPORT NSErrorDomain const LLaMARunnerErrorDomain;
FOUNDATION_EXPORT NSErrorDomain const LLaVARunnerErrorDomain;

NS_SWIFT_NAME(Runner)
@interface LLaMARunner : NSObject

- (instancetype)initWithModelPath:(NSString*)filePath
tokenizerPath:(NSString*)tokenizerPath;
- (BOOL)isloaded;
- (BOOL)loadWithError:(NSError**)error;
- (BOOL)generate:(NSString*)prompt
withTokenCallback:(nullable void (^)(NSString*))callback
error:(NSError**)error;
- (void)stop;

+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;

@end

NS_SWIFT_NAME(LLaVARunner)
@interface LLaVARunner : NSObject

- (instancetype)initWithModelPath:(NSString*)filePath
tokenizerPath:(NSString*)tokenizerPath;
- (BOOL)isloaded;
- (BOOL)loadWithError:(NSError**)error;
- (BOOL)generate:(void*)imageBuffer
width:(CGFloat)width
height:(CGFloat)height
prompt:(NSString*)prompt
sequenceLength:(NSInteger)seq_len
withTokenCallback:(nullable void (^)(NSString*))callback
error:(NSError**)error;
- (void)stop;

+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;

@end

NS_ASSUME_NONNULL_END
104 changes: 104 additions & 0 deletions third-party/ios/ExecutorchLib/ExecutorchLib/Exported/LLaMARunner.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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.
*/

#import "LLaMARunner.h"

#import <ExecuTorch/ExecuTorchLog.h>
#import "runner.h"

using namespace ::torch::executor;

NSErrorDomain const LLaMARunnerErrorDomain = @"LLaMARunnerErrorDomain";
NSErrorDomain const LLaVARunnerErrorDomain = @"LLaVARunnerErrorDomain";

@interface LLaMARunner ()<ExecuTorchLogSink>
@end

@implementation LLaMARunner {
std::unique_ptr<example::Runner> _runner;
}

- (instancetype)initWithModelPath:(NSString*)modelPath
tokenizerPath:(NSString*)tokenizerPath {
self = [super init];
if (self) {
[ExecuTorchLog.sharedLog addSink:self];
_runner = std::make_unique<example::Runner>(
modelPath.UTF8String, tokenizerPath.UTF8String);
}
return self;
}

- (void)dealloc {
[ExecuTorchLog.sharedLog removeSink:self];
}

- (BOOL)isloaded {
return _runner->is_loaded();
}

- (BOOL)loadWithError:(NSError**)error {
const auto status = _runner->load();
if (status != Error::Ok) {
if (error) {
*error = [NSError errorWithDomain:LLaMARunnerErrorDomain
code:(NSInteger)status
userInfo:nil];
}
return NO;
}
return YES;
}

- (BOOL)generate:(NSString*)prompt
withTokenCallback:(nullable void (^)(NSString*))callback
error:(NSError**)error {
const auto status = _runner->generate(
prompt.UTF8String, [callback](const std::string& token) {
callback(@(token.c_str()));
});
if (status != Error::Ok) {
if (error) {
*error = [NSError errorWithDomain:LLaMARunnerErrorDomain
code:(NSInteger)status
userInfo:nil];
return NO;
}
}
return YES;
}

- (void)stop {
_runner->stop();
}

#pragma mark - ExecuTorchLogSink

- (void)logWithLevel:(ExecuTorchLogLevel)level
timestamp:(NSTimeInterval)timestamp
filename:(NSString*)filename
line:(NSUInteger)line
message:(NSString*)message {
NSUInteger totalSeconds = (NSUInteger)timestamp;
NSUInteger hours = (totalSeconds / 3600) % 24;
NSUInteger minutes = (totalSeconds / 60) % 60;
NSUInteger seconds = totalSeconds % 60;
NSUInteger microseconds = (timestamp - totalSeconds) * 1000000;
NSLog(
@"%c %02lu:%02lu:%02lu.%06lu executorch:%s:%zu] %s",
(char)level,
hours,
minutes,
seconds,
microseconds,
filename.UTF8String,
line,
message.UTF8String);
}

@end
15 changes: 15 additions & 0 deletions third-party/ios/ExecutorchLib/ExecutorchLib/model/Model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "Model.h"

#include <executorch/extension/module/module.h>

using ::executorch::extension::Module;

Model::Model(
const std::string& file_path
){
module_ = std::make_unique<Module>(file_path);
}

void Model::forward(){
return;
}
11 changes: 11 additions & 0 deletions third-party/ios/ExecutorchLib/ExecutorchLib/model/Model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <executorch/extension/module/module.h>

class Model {
public:
explicit Model(
const std::string& file_path);

void forward();
private:
std::unique_ptr<::executorch::extension::Module> module_;
};
Loading
Loading