Skip to content

Commit 8800e00

Browse files
mkopcinsmsluszniakbenITo47
authored
chore: Add [[nodiscard]] attribute to native functions (#660)
## Description As in the title, add `[[nodiscard]]` attribute to exported functions in native implementation. ### Introduces a breaking change? - [ ] Yes - [x] No ### Type of change - [ ] Bug fix (change which fixes an issue) - [ ] New feature (change which adds functionality) - [ ] Documentation update (improves or adds clarity to existing documentation) - [x] Other (chores, tests, code style improvements etc.) ### Tested on - [x] iOS - [ ] Android ### Testing instructions Check if all demo apps work the same as previously ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues Closes #448 ### Checklist - [x] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [x] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. --> --------- Co-authored-by: Mateusz Słuszniak <mateusz.sluszniak@swmansion.com> Co-authored-by: Mateusz Sluszniak <56299341+msluszniak@users.noreply.github.com> Co-authored-by: Bartek <116185412+benITo47@users.noreply.github.com>
1 parent d3fbd61 commit 8800e00

15 files changed

Lines changed: 42 additions & 39 deletions

File tree

packages/react-native-executorch/common/rnexecutorch/TokenizerModule.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ class TokenizerModule {
1111
public:
1212
explicit TokenizerModule(std::string source,
1313
std::shared_ptr<react::CallInvoker> callInvoker);
14-
std::vector<int32_t> encode(std::string s) const;
15-
std::string decode(std::vector<int32_t> vec, bool skipSpecialTokens) const;
16-
std::string idToToken(int32_t tokenId) const;
17-
int32_t tokenToId(std::string token) const;
18-
std::size_t getVocabSize() const;
14+
[[nodiscard("Registered non-void function")]] std::vector<int32_t>
15+
encode(std::string s) const;
16+
[[nodiscard("Registered non-void function")]] std::string
17+
decode(std::vector<int32_t> vec, bool skipSpecialTokens) const;
18+
[[nodiscard("Registered non-void function")]] std::string
19+
idToToken(int32_t tokenId) const;
20+
[[nodiscard("Registered non-void function")]] int32_t
21+
tokenToId(std::string token) const;
22+
[[nodiscard("Registered non-void function")]] std::size_t
23+
getVocabSize() const;
1924
std::size_t getMemoryLowerBound() const noexcept;
2025

2126
private:

packages/react-native-executorch/common/rnexecutorch/host_objects/ModelHostObject.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
3434
if constexpr (meta::DerivedFromOrSameAs<Model, models::BaseModel>) {
3535
addFunctions(
3636
JSI_EXPORT_FUNCTION(ModelHostObject<Model>, unload, "unload"));
37-
}
3837

39-
if constexpr (meta::DerivedFromOrSameAs<Model, models::BaseModel>) {
4038
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
4139
promiseHostFunction<&Model::forwardJS>,
4240
"forward"));
43-
}
4441

45-
if constexpr (meta::DerivedFromOrSameAs<Model, models::BaseModel>) {
4642
addFunctions(JSI_EXPORT_FUNCTION(
4743
ModelHostObject<Model>, promiseHostFunction<&Model::getInputShape>,
4844
"getInputShape"));
@@ -89,13 +85,6 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
8985
}
9086

9187
if constexpr (meta::SameAs<Model, TokenizerModule>) {
92-
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
93-
promiseHostFunction<&Model::encode>,
94-
"encode"));
95-
96-
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
97-
promiseHostFunction<&Model::decode>,
98-
"decode"));
9988
addFunctions(JSI_EXPORT_FUNCTION(
10089
ModelHostObject<Model>, promiseHostFunction<&Model::getVocabSize>,
10190
"getVocabSize"));
@@ -108,10 +97,6 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
10897
}
10998

11099
if constexpr (meta::SameAs<Model, models::llm::LLM>) {
111-
addFunctions(JSI_EXPORT_FUNCTION(ModelHostObject<Model>,
112-
promiseHostFunction<&Model::generate>,
113-
"generate"));
114-
115100
addFunctions(JSI_EXPORT_FUNCTION(
116101
ModelHostObject<Model>, synchronousHostFunction<&Model::interrupt>,
117102
"interrupt"));

packages/react-native-executorch/common/rnexecutorch/models/BaseModel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ class BaseModel {
2525
Module::LoadMode loadMode = Module::LoadMode::MmapUseMlockIgnoreErrors);
2626
std::size_t getMemoryLowerBound() const noexcept;
2727
void unload() noexcept;
28-
std::vector<int32_t> getInputShape(std::string method_name,
29-
int32_t index) const;
28+
[[nodiscard("Registered non-void function")]] std::vector<int32_t>
29+
getInputShape(std::string method_name, int32_t index) const;
3030
std::vector<std::vector<int32_t>>
3131
getAllInputShapes(std::string methodName = "forward") const;
32-
std::vector<JSTensorViewOut>
32+
[[nodiscard("Registered non-void function")]] std::vector<JSTensorViewOut>
3333
forwardJS(std::vector<JSTensorViewIn> tensorViewVec) const;
3434
Result<std::vector<EValue>> forward(const EValue &input_value) const;
3535
Result<std::vector<EValue>>

packages/react-native-executorch/common/rnexecutorch/models/classification/Classification.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ class Classification : public BaseModel {
1717
public:
1818
Classification(const std::string &modelSource,
1919
std::shared_ptr<react::CallInvoker> callInvoker);
20-
std::unordered_map<std::string_view, float> generate(std::string imageSource);
20+
[[nodiscard("Registered non-void function")]] std::unordered_map<
21+
std::string_view, float>
22+
generate(std::string imageSource);
2123

2224
private:
2325
std::unordered_map<std::string_view, float> postprocess(const Tensor &tensor);

packages/react-native-executorch/common/rnexecutorch/models/embeddings/image/ImageEmbeddings.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class ImageEmbeddings final : public BaseEmbeddings {
1616
public:
1717
ImageEmbeddings(const std::string &modelSource,
1818
std::shared_ptr<react::CallInvoker> callInvoker);
19-
std::shared_ptr<OwningArrayBuffer> generate(std::string imageSource);
19+
[[nodiscard(
20+
"Registered non-void function")]] std::shared_ptr<OwningArrayBuffer>
21+
generate(std::string imageSource);
2022

2123
private:
2224
cv::Size modelImageSize{0, 0};

packages/react-native-executorch/common/rnexecutorch/models/embeddings/text/TextEmbeddings.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ class TextEmbeddings final : public BaseEmbeddings {
1717
TextEmbeddings(const std::string &modelSource,
1818
const std::string &tokenizerSource,
1919
std::shared_ptr<react::CallInvoker> callInvoker);
20-
std::shared_ptr<OwningArrayBuffer> generate(const std::string input);
20+
[[nodiscard(
21+
"Registered non-void function")]] std::shared_ptr<OwningArrayBuffer>
22+
generate(const std::string input);
2123

2224
private:
2325
std::vector<std::vector<int32_t>> inputShapes;

packages/react-native-executorch/common/rnexecutorch/models/image_segmentation/ImageSegmentation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ImageSegmentation : public BaseModel {
2323
public:
2424
ImageSegmentation(const std::string &modelSource,
2525
std::shared_ptr<react::CallInvoker> callInvoker);
26-
std::shared_ptr<jsi::Object>
26+
[[nodiscard("Registered non-void function")]] std::shared_ptr<jsi::Object>
2727
generate(std::string imageSource,
2828
std::set<std::string, std::less<>> classesOfInterest, bool resize);
2929

packages/react-native-executorch/common/rnexecutorch/models/object_detection/ObjectDetection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class ObjectDetection : public BaseModel {
2020
public:
2121
ObjectDetection(const std::string &modelSource,
2222
std::shared_ptr<react::CallInvoker> callInvoker);
23-
std::vector<types::Detection> generate(std::string imageSource,
24-
double detectionThreshold);
23+
[[nodiscard("Registered non-void function")]] std::vector<types::Detection>
24+
generate(std::string imageSource, double detectionThreshold);
2525

2626
private:
2727
std::vector<types::Detection> postprocess(const std::vector<EValue> &tensors,

packages/react-native-executorch/common/rnexecutorch/models/ocr/Detector.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ class Detector : public BaseModel {
2121
public:
2222
explicit Detector(const std::string &modelSource,
2323
std::shared_ptr<react::CallInvoker> callInvoker);
24-
virtual std::vector<types::DetectorBBox> generate(const cv::Mat &inputImage,
25-
int32_t inputWidth);
24+
[[nodiscard("Registered non-void function")]]
25+
virtual std::vector<types::DetectorBBox>
26+
generate(const cv::Mat &inputImage, int32_t inputWidth);
2627

2728
cv::Size calculateModelImageSize(int32_t methodInputWidth);
2829

packages/react-native-executorch/common/rnexecutorch/models/ocr/OCR.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class OCR final {
2727
explicit OCR(const std::string &detectorSource,
2828
const std::string &recognizerSource, const std::string &symbols,
2929
std::shared_ptr<react::CallInvoker> callInvoker);
30-
std::vector<types::OCRDetection> generate(std::string input);
30+
[[nodiscard("Registered non-void function")]] std::vector<types::OCRDetection>
31+
generate(std::string input);
3132
std::size_t getMemoryLowerBound() const noexcept;
3233
void unload() noexcept;
3334

0 commit comments

Comments
 (0)