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

Initialize Objective-C bindings #709

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

skyline75489
Copy link
Contributor

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

@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
Copy link
Contributor Author

Choose a reason for hiding this comment

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


#import "oga_internal.h"

@implementation OGAInt32Span {
Copy link
Contributor Author

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.

src/objectivec/oga_internal.h Show resolved Hide resolved
Copy link
Contributor

@edgchen1 edgchen1 left a 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 {
Copy link
Contributor

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)

Comment on lines +25 to +27
- (int32_t)last {
return *(_ptr + (_size - 1));
}
Copy link
Contributor

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)?

Copy link
Contributor Author

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.

src/objectivec/oga_internal.h Show resolved Hide resolved
Comment on lines +24 to +36
- (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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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 {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure. will do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants