Skip to content
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class RnExecutorchPackage : TurboReactPackage() {
OCR(reactContext)
} else if (name == VerticalOCR.NAME) {
VerticalOCR(reactContext)
} else if (name == ImageSegmentation.NAME) {
ImageSegmentation(reactContext)
} else if (name == ETInstaller.NAME) {
ETInstaller(reactContext)
} else if (name == Tokenizer.NAME) {
Expand Down Expand Up @@ -119,17 +117,6 @@ class RnExecutorchPackage : TurboReactPackage() {
true,
)

moduleInfos[ImageSegmentation.NAME] =
ReactModuleInfo(
ImageSegmentation.NAME,
ImageSegmentation.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
true,
)

moduleInfos[Tokenizer.NAME] =
ReactModuleInfo(
Tokenizer.NAME,
Expand Down

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions common/rnexecutorch/RnExecutorchInstaller.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "RnExecutorchInstaller.h"

#include <rnexecutorch/host_objects/JsiConversions.h>
#include <rnexecutorch/models/image_segmentation/ImageSegmentation.h>
#include <rnexecutorch/models/style_transfer/StyleTransfer.h>

namespace rnexecutorch {
Expand All @@ -19,5 +20,10 @@ void RnExecutorchInstaller::injectJSIBindings(
*jsiRuntime, "loadStyleTransfer",
RnExecutorchInstaller::loadModel<StyleTransfer>(jsiRuntime, jsCallInvoker,
"loadStyleTransfer"));

jsiRuntime->global().setProperty(
*jsiRuntime, "loadImageSegmentation",
RnExecutorchInstaller::loadModel<ImageSegmentation>(
jsiRuntime, jsCallInvoker, "loadImageSegmentation"));
}
} // namespace rnexecutorch
2 changes: 1 addition & 1 deletion common/rnexecutorch/RnExecutorchInstaller.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class RnExecutorchInstaller {
jsiconversion::getValue<std::string>(args[0], runtime);

auto modelImplementationPtr =
std::make_shared<ModelT>(source, &runtime);
std::make_shared<ModelT>(source, jsCallInvoker);
auto modelHostObject = std::make_shared<ModelHostObject<ModelT>>(
modelImplementationPtr, jsCallInvoker);

Expand Down
19 changes: 19 additions & 0 deletions common/rnexecutorch/data_processing/Numerical.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "Numerical.h"

#include <algorithm>
#include <numeric>

namespace rnexecutorch::numerical {
void softmax(std::vector<float> &v) {
float max = *std::max_element(v.begin(), v.end());

float sum = 0.0f;
for (float &x : v) {
x = std::exp(x - max);
sum += x;
}
for (float &x : v) {
x /= sum;
}
}
} // namespace rnexecutorch::numerical
7 changes: 7 additions & 0 deletions common/rnexecutorch/data_processing/Numerical.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <vector>

namespace rnexecutorch::numerical {
void softmax(std::vector<float> &v);
} // namespace rnexecutorch::numerical
32 changes: 24 additions & 8 deletions common/rnexecutorch/host_objects/JsiConversions.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <jsi/jsi.h>
#include <set>
#include <type_traits>

#include <jsi/jsi.h>

namespace rnexecutorch::jsiconversion {

using namespace facebook;
Expand Down Expand Up @@ -43,19 +45,33 @@ getValue<std::vector<std::string>>(const jsi::Value &val,
return result;
}

// C++ set from JS array. Set with heterogenerous look-up (adding std::less<>
// enables querying with std::string_view).
template <>
inline std::set<std::string, std::less<>>
getValue<std::set<std::string, std::less<>>>(const jsi::Value &val,
jsi::Runtime &runtime) {

jsi::Array array = val.asObject(runtime).asArray(runtime);
size_t length = array.size(runtime);
std::set<std::string, std::less<>> result;

for (size_t i = 0; i < length; ++i) {
jsi::Value element = array.getValueAtIndex(runtime, i);
result.insert(getValue<std::string>(element, runtime));
}
return result;
}

// Conversion from C++ types to jsi --------------------------------------------

// Implementation functions might return any type, but in a promise we can only
// return jsi::Value or jsi::Object. For each type being returned
// we add a function here.

// Identity function for the sake of completeness
inline jsi::Value getJsiValue(jsi::Value &&value, jsi::Runtime &runtime) {
return std::move(value);
}

inline jsi::Value getJsiValue(jsi::Object &&value, jsi::Runtime &runtime) {
return jsi::Value(std::move(value));
inline jsi::Value getJsiValue(std::shared_ptr<jsi::Object> valuePtr,
jsi::Runtime &runtime) {
return std::move(*valuePtr);
}

inline jsi::Value getJsiValue(const std::string &str, jsi::Runtime &runtime) {
Expand Down
7 changes: 4 additions & 3 deletions common/rnexecutorch/host_objects/ModelHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
try {
auto result =
std::apply(std::bind_front(FnPtr, model), argsConverted);

callInvoker->invokeAsync([promise, result = std::move(result)](
jsi::Runtime &runtime) {
// The result is copied. It should either be quickly copiable,
// or passed with a shared_ptr.
callInvoker->invokeAsync([promise,
result](jsi::Runtime &runtime) {
promise->resolve(
jsiconversion::getJsiValue(std::move(result), runtime));
});
Expand Down
Loading