Skip to content

Commit e21a85d

Browse files
committed
fix: switch to callAsync for CallInvoker
1 parent c2064bb commit e21a85d

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

common/rnexecutorch/host_objects/JsiConversions.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ getValue<std::set<std::string, std::less<>>>(const jsi::Value &val,
7070
// return jsi::Value or jsi::Object. For each type being returned
7171
// we add a function here.
7272

73-
// Identity function for the sake of completeness
74-
inline jsi::Value getJsiValue(std::unique_ptr<jsi::Object> &&valuePtr,
73+
inline jsi::Value getJsiValue(std::shared_ptr<jsi::Object> valuePtr,
7574
jsi::Runtime &runtime) {
7675
return std::move(*valuePtr);
7776
}

common/rnexecutorch/host_objects/ModelHostObject.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
4848
std::thread([this, promise,
4949
argsConverted = std::move(argsConverted)]() {
5050
try {
51+
5152
auto result = std::apply(
5253
std::bind_front(&Model::forward, model), argsConverted);
5354

54-
callInvoker->invokeSync([promise,
55-
&result](jsi::Runtime &runtime) {
55+
callInvoker->invokeAsync([promise,
56+
result](jsi::Runtime &runtime) {
5657
promise->resolve(
5758
jsiconversion::getJsiValue(std::move(result), runtime));
5859
});

common/rnexecutorch/models/image_segmentation/ImageSegmentation.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "ImageSegmentation.h"
22

3+
#include <future>
4+
35
#include <executorch/extension/tensor/tensor.h>
46

57
#include <rnexecutorch/data_processing/ImageProcessing.h>
@@ -20,7 +22,7 @@ ImageSegmentation::ImageSegmentation(
2022
numModelPixels = modelImageSize.area();
2123
}
2224

23-
std::unique_ptr<jsi::Object>
25+
std::shared_ptr<jsi::Object>
2426
ImageSegmentation::forward(std::string imageSource,
2527
std::set<std::string, std::less<>> classesOfInterest,
2628
bool resize) {
@@ -50,7 +52,7 @@ ImageSegmentation::preprocess(const std::string &imageSource) {
5052
inputSize};
5153
}
5254

53-
std::unique_ptr<jsi::Object> ImageSegmentation::postprocess(
55+
std::shared_ptr<jsi::Object> ImageSegmentation::postprocess(
5456
const Tensor &tensor, cv::Size originalSize,
5557
std::set<std::string, std::less<>> classesOfInterest, bool resize) {
5658

@@ -128,16 +130,21 @@ std::unique_ptr<jsi::Object> ImageSegmentation::postprocess(
128130
return populateDictionary(argmax, buffersToReturn);
129131
}
130132

131-
std::unique_ptr<jsi::Object> ImageSegmentation::populateDictionary(
133+
std::shared_ptr<jsi::Object> ImageSegmentation::populateDictionary(
132134
std::shared_ptr<OwningArrayBuffer> argmax,
133135
std::shared_ptr<std::unordered_map<std::string_view,
134136
std::shared_ptr<OwningArrayBuffer>>>
135137
classesToOutput) {
136-
std::unique_ptr<jsi::Object> dictPtr;
138+
// Synchronize the invoked thread to return when the dict is constructed
139+
auto promisePtr = std::make_shared<std::promise<void>>();
140+
std::future<void> doneFuture = promisePtr->get_future();
141+
142+
log(LOG_LEVEL::Debug, "To sie po prostu w pale nie miesci!");
137143

138-
callInvoker->invokeSync(
139-
[argmax, classesToOutput, &dictPtr](jsi::Runtime &runtime) {
140-
dictPtr = std::make_unique<jsi::Object>(runtime);
144+
std::shared_ptr<jsi::Object> dictPtr = nullptr;
145+
callInvoker->invokeAsync(
146+
[argmax, classesToOutput, &dictPtr, promisePtr](jsi::Runtime &runtime) {
147+
dictPtr = std::make_shared<jsi::Object>(runtime);
141148
auto argmaxArrayBuffer = jsi::ArrayBuffer(runtime, argmax);
142149

143150
auto int32ArrayCtor =
@@ -160,8 +167,10 @@ std::unique_ptr<jsi::Object> ImageSegmentation::populateDictionary(
160167
runtime, jsi::String::createFromAscii(runtime, classLabel.data()),
161168
float32Array);
162169
}
170+
promisePtr->set_value();
163171
});
164172

173+
doneFuture.wait();
165174
return dictPtr;
166175
}
167176

common/rnexecutorch/models/image_segmentation/ImageSegmentation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ class ImageSegmentation : public BaseModel {
2121
public:
2222
ImageSegmentation(const std::string &modelSource,
2323
std::shared_ptr<react::CallInvoker> callInvoker);
24-
std::unique_ptr<jsi::Object>
24+
std::shared_ptr<jsi::Object>
2525
forward(std::string imageSource,
2626
std::set<std::string, std::less<>> classesOfInterest, bool resize);
2727

2828
private:
2929
std::pair<TensorPtr, cv::Size> preprocess(const std::string &imageSource);
30-
std::unique_ptr<jsi::Object>
30+
std::shared_ptr<jsi::Object>
3131
postprocess(const Tensor &tensor, cv::Size originalSize,
3232
std::set<std::string, std::less<>> classesOfInterest,
3333
bool resize);
34-
std::unique_ptr<jsi::Object> populateDictionary(
34+
std::shared_ptr<jsi::Object> populateDictionary(
3535
std::shared_ptr<OwningArrayBuffer> argmax,
3636
std::shared_ptr<std::unordered_map<std::string_view,
3737
std::shared_ptr<OwningArrayBuffer>>>

examples/computer-vision/screens/ImageSegmentationScreen.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getImage } from '../utils';
44
import {
55
useImageSegmentation,
66
DEEPLAB_V3_RESNET50,
7+
DeeplabLabel,
78
} from 'react-native-executorch';
89
import {
910
Canvas,
@@ -79,14 +80,16 @@ export const ImageSegmentationScreen = ({
7980
const runForward = async () => {
8081
if (imageUri) {
8182
try {
82-
const output = await model.forward(imageUri, [], false);
83+
const output = await model.forward(imageUri);
8384
pixels = new Uint8Array(width * height * 4);
8485

8586
for (let x = 0; x < width; x++) {
8687
for (let y = 0; y < height; y++) {
8788
for (let i = 0; i < 3; i++) {
8889
pixels[(x * height + y) * 4 + i] =
89-
numberToColor[(output['ARGMAX'] || [])[x * height + y]][i];
90+
numberToColor[
91+
(output[DeeplabLabel.ARGMAX] || [])[x * height + y]
92+
][i];
9093
}
9194
pixels[(x * height + y) * 4 + 3] = 255;
9295
}

0 commit comments

Comments
 (0)