Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ class TokenizerModule {
public:
explicit TokenizerModule(std::string source,
std::shared_ptr<react::CallInvoker> callInvoker);
std::vector<int32_t> encode(std::string s) const;
std::string decode(std::vector<int32_t> vec, bool skipSpecialTokens) const;
std::string idToToken(int32_t tokenId) const;
int32_t tokenToId(std::string token) const;
std::size_t getVocabSize() const;
[[nodiscard("Registered non-void function")]] std::vector<int32_t>
encode(std::string s) const;
[[nodiscard("Registered non-void function")]] std::string
decode(std::vector<int32_t> vec, bool skipSpecialTokens) const;
[[nodiscard("Registered non-void function")]] std::string
idToToken(int32_t tokenId) const;
[[nodiscard("Registered non-void function")]] int32_t
tokenToId(std::string token) const;
[[nodiscard("Registered non-void function")]] std::size_t
getVocabSize() const;
std::size_t getMemoryLowerBound() const noexcept;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
if constexpr (meta::DerivedFromOrSameAs<Model, models::BaseModel>) {
addFunctions(
JSI_EXPORT_FUNCTION(ModelHostObject<Model>, unload, "unload"));
}

if constexpr (meta::DerivedFromOrSameAs<Model, models::BaseModel>) {
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
promiseHostFunction<&Model::forwardJS>,
"forward"));
}

if constexpr (meta::DerivedFromOrSameAs<Model, models::BaseModel>) {
addFunctions(JSI_EXPORT_FUNCTION(
ModelHostObject<Model>, promiseHostFunction<&Model::getInputShape>,
"getInputShape"));
Expand Down Expand Up @@ -86,13 +82,6 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
}

if constexpr (meta::SameAs<Model, TokenizerModule>) {
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
promiseHostFunction<&Model::encode>,
"encode"));

addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
promiseHostFunction<&Model::decode>,
"decode"));
addFunctions(JSI_EXPORT_FUNCTION(
ModelHostObject<Model>, promiseHostFunction<&Model::getVocabSize>,
"getVocabSize"));
Expand All @@ -105,10 +94,6 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
}

if constexpr (meta::SameAs<Model, models::llm::LLM>) {
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
promiseHostFunction<&Model::generate>,
"generate"));

addFunctions(JSI_EXPORT_FUNCTION(
ModelHostObject<Model>, synchronousHostFunction<&Model::interrupt>,
"interrupt"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ class BaseModel {
std::shared_ptr<react::CallInvoker> callInvoker);
std::size_t getMemoryLowerBound() const noexcept;
void unload() noexcept;
std::vector<int32_t> getInputShape(std::string method_name, int32_t index);
[[nodiscard("Registered non-void function")]] std::vector<int32_t>
getInputShape(std::string method_name, int32_t index);
std::vector<std::vector<int32_t>>
getAllInputShapes(std::string methodName = "forward");
std::vector<JSTensorViewOut>
[[nodiscard("Registered non-void function")]] std::vector<JSTensorViewOut>
forwardJS(std::vector<JSTensorViewIn> tensorViewVec);
Result<std::vector<EValue>> forward(const EValue &input_value) const;
Result<std::vector<EValue>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class Classification : public BaseModel {
public:
Classification(const std::string &modelSource,
std::shared_ptr<react::CallInvoker> callInvoker);
std::unordered_map<std::string_view, float> generate(std::string imageSource);
[[nodiscard("Registered non-void function")]] std::unordered_map<
std::string_view, float>
generate(std::string imageSource);

private:
std::unordered_map<std::string_view, float> postprocess(const Tensor &tensor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class ImageEmbeddings final : public BaseEmbeddings {
public:
ImageEmbeddings(const std::string &modelSource,
std::shared_ptr<react::CallInvoker> callInvoker);
std::shared_ptr<OwningArrayBuffer> generate(std::string imageSource);
[[nodiscard(
"Registered non-void function")]] std::shared_ptr<OwningArrayBuffer>
generate(std::string imageSource);

private:
cv::Size modelImageSize{0, 0};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class TextEmbeddings final : public BaseEmbeddings {
TextEmbeddings(const std::string &modelSource,
const std::string &tokenizerSource,
std::shared_ptr<react::CallInvoker> callInvoker);
std::shared_ptr<OwningArrayBuffer> generate(const std::string input);
[[nodiscard(
"Registered non-void function")]] std::shared_ptr<OwningArrayBuffer>
generate(const std::string input);

private:
std::vector<std::vector<int32_t>> inputShapes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ImageSegmentation : public BaseModel {
public:
ImageSegmentation(const std::string &modelSource,
std::shared_ptr<react::CallInvoker> callInvoker);
std::shared_ptr<jsi::Object>
[[nodiscard("Registered non-void function")]] std::shared_ptr<jsi::Object>
generate(std::string imageSource,
std::set<std::string, std::less<>> classesOfInterest, bool resize);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class ObjectDetection : public BaseModel {
public:
ObjectDetection(const std::string &modelSource,
std::shared_ptr<react::CallInvoker> callInvoker);
std::vector<types::Detection> generate(std::string imageSource,
double detectionThreshold);
[[nodiscard("Registered non-void function")]] std::vector<types::Detection>
generate(std::string imageSource, double detectionThreshold);

private:
std::vector<types::Detection> postprocess(const std::vector<EValue> &tensors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class Detector final : public BaseModel {
public:
explicit Detector(const std::string &modelSource,
std::shared_ptr<react::CallInvoker> callInvoker);
std::vector<types::DetectorBBox> generate(const cv::Mat &inputImage);
[[nodiscard("Registered non-void function")]] std::vector<types::DetectorBBox>
generate(const cv::Mat &inputImage);
cv::Size getModelImageSize() const noexcept;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class OCR final {
const std::string &recognizerSourceMedium,
const std::string &recognizerSourceSmall, std::string symbols,
std::shared_ptr<react::CallInvoker> callInvoker);
std::vector<types::OCRDetection> generate(std::string input);
[[nodiscard("Registered non-void function")]] std::vector<types::OCRDetection>
generate(std::string input);
std::size_t getMemoryLowerBound() const noexcept;
void unload() noexcept;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class Recognizer final : public BaseModel {
public:
explicit Recognizer(const std::string &modelSource,
std::shared_ptr<react::CallInvoker> callInvoker);
std::pair<std::vector<int32_t>, float> generate(const cv::Mat &grayImage);
[[nodiscard(
"Registered non-void function")]] std::pair<std::vector<int32_t>, float>
generate(const cv::Mat &grayImage);

private:
std::pair<std::vector<int32_t>, float>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ class SpeechToText {
std::shared_ptr<react::CallInvoker> callInvoker);

void unload() noexcept;
std::shared_ptr<OwningArrayBuffer> encode(std::span<float> waveform) const;
std::shared_ptr<OwningArrayBuffer>
[[nodiscard(
"Registered non-void function")]] std::shared_ptr<OwningArrayBuffer>
encode(std::span<float> waveform) const;
[[nodiscard(
"Registered non-void function")]] std::shared_ptr<OwningArrayBuffer>
decode(std::span<int32_t> tokens, std::span<float> encoderOutput) const;
std::string transcribe(std::span<float> waveform,
std::string languageOption) const;
[[nodiscard("Registered non-void function")]] std::string
transcribe(std::span<float> waveform, std::string languageOption) const;

size_t getMemoryLowerBound() const noexcept;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class StyleTransfer : public BaseModel {
public:
StyleTransfer(const std::string &modelSource,
std::shared_ptr<react::CallInvoker> callInvoker);
std::string generate(std::string imageSource);
[[nodiscard("Registered non-void function")]] std::string
generate(std::string imageSource);

private:
std::string postprocess(const Tensor &tensor, cv::Size originalSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class VerticalDetector final : public BaseModel {
explicit VerticalDetector(const std::string &modelSource,
bool detectSingleCharacters,
std::shared_ptr<react::CallInvoker> callInvoker);
std::vector<types::DetectorBBox> generate(const cv::Mat &inputImage);
[[nodiscard("Registered non-void function")]] std::vector<types::DetectorBBox>
generate(const cv::Mat &inputImage);
cv::Size getModelImageSize() const noexcept;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class VerticalOCR final {
const std::string &recognizerSource, std::string symbols,
bool indpendentCharacters,
std::shared_ptr<react::CallInvoker> callInvoker);
std::vector<types::OCRDetection> generate(std::string input);
[[nodiscard("Registered non-void function")]] std::vector<types::OCRDetection>
generate(std::string input);
std::size_t getMemoryLowerBound() const noexcept;
void unload() noexcept;

Expand Down