-
Notifications
You must be signed in to change notification settings - Fork 110
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
Initialize Objective-C bindings #709
base: main
Are you sure you want to change the base?
Conversation
@@ -0,0 +1,27 @@ | |||
// Copyright (c) Microsoft Corporation. All rights reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied from onnxruntime repo: https://github.com/microsoft/onnxruntime/blob/main/objectivec/error_utils.h
|
||
#import "oga_internal.h" | ||
|
||
@implementation OGAInt32Span { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to mimic std::span
which is not available until C++ 20, also not suitable in an ObjC environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please document public APIs and add unit tests
OGA_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error) | ||
} | ||
|
||
- (BOOL)setInputSequences:(OGASequences*)sequences error:(NSError**)error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the C# bindings as the base example, should there also be an equivalent of setInputIds?
public void SetInputIDs(ReadOnlySpan<int> inputIDs, ulong sequenceLength, ulong batchSize) |
- (int32_t)last { | ||
return *(_ptr + (_size - 1)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the use case for this? std::span has things like begin
and end
but that's slightly different to returning the value of the last element (and this would also do the wrong thing if size was 0 wouldn't it)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use case of this is to retrieve the last element of the generated sequence for streaming output. The [^1]
part of the following code:
Console.Write(tokenizerStream.Decode(generator.GetSequence(0)[^1])); |
You're right it would fail if size is 0. Will try to improve this.
- (nullable OGASequences*)encode:(NSString*)str error:(NSError**)error { | ||
OGASequences* sequences = [[OGASequences alloc] initWithError:error]; | ||
if (*error) { | ||
return nil; | ||
} | ||
try { | ||
_tokenizer->Encode([str UTF8String], [sequences CXXAPIOgaSequences]); | ||
return sequences; | ||
} | ||
OGA_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error) | ||
} | ||
|
||
- (nullable NSString*)decode:(OGAInt32Span*)data error:(NSError**)error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The C# API has encode/decode of batches. Is there a reason for this to be inconsistent and only support a single input?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. This PR is just the first step towards a usable example. More methods including batches support will be added.
|
||
@end | ||
|
||
@implementation OGATokenizerStream { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could this be in oga_tokenizer_stream.mm? we usually keep it 1:1 between non-trivial types and the file they're in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure. will do
This PR contains only Objective-C wrapper code. Examples, podspec and building infras are yet to be done.
Internal workitem: https://task.ms/aii/35199