react-native-executorch 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/common/rnexecutorch/TokenizerModule.cpp +3 -2
- package/common/rnexecutorch/TokenizerModule.h +1 -1
- package/package.json +2 -1
- package/third-party/android/libs/executorch/arm64-v8a/libexecutorch.so +0 -0
- package/third-party/android/libs/executorch/x86_64/libexecutorch.so +0 -0
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/bpe_model.h +84 -0
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/bpe_tokenizer_base.h +6 -87
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/hf_tokenizer.h +28 -176
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/map_utils.h +174 -0
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/model.h +151 -0
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/normalizer.h +55 -1
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/padding.h +112 -0
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/post_processor.h +101 -42
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/pre_tokenizer.h +25 -9
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/token_decoder.h +33 -6
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/tokenizer.h +2 -2
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/truncation.h +92 -0
- package/third-party/include/executorch/extension/llm/tokenizers/include/pytorch/tokenizers/wordpiece_model.h +74 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib +0 -0
- package/third-party/ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib +0 -0
- package/common/rnexecutorch/tests/CMakeLists.txt +0 -253
- package/common/rnexecutorch/tests/README.md +0 -73
- package/common/rnexecutorch/tests/integration/BaseModelTest.cpp +0 -207
- package/common/rnexecutorch/tests/integration/BaseModelTests.h +0 -120
- package/common/rnexecutorch/tests/integration/ClassificationTest.cpp +0 -117
- package/common/rnexecutorch/tests/integration/ImageEmbeddingsTest.cpp +0 -122
- package/common/rnexecutorch/tests/integration/ImageSegmentationTest.cpp +0 -152
- package/common/rnexecutorch/tests/integration/LLMTest.cpp +0 -155
- package/common/rnexecutorch/tests/integration/OCRTest.cpp +0 -128
- package/common/rnexecutorch/tests/integration/ObjectDetectionTest.cpp +0 -135
- package/common/rnexecutorch/tests/integration/SpeechToTextTest.cpp +0 -97
- package/common/rnexecutorch/tests/integration/StyleTransferTest.cpp +0 -112
- package/common/rnexecutorch/tests/integration/TextEmbeddingsTest.cpp +0 -164
- package/common/rnexecutorch/tests/integration/TextToImageTest.cpp +0 -149
- package/common/rnexecutorch/tests/integration/TokenizerModuleTest.cpp +0 -98
- package/common/rnexecutorch/tests/integration/VerticalOCRTest.cpp +0 -238
- package/common/rnexecutorch/tests/integration/VoiceActivityDetectionTest.cpp +0 -99
- package/common/rnexecutorch/tests/integration/assets/test_audio_float.raw +0 -0
- package/common/rnexecutorch/tests/integration/assets/we_are_software_mansion.jpg +0 -0
- package/common/rnexecutorch/tests/integration/libs/libfbjni.so +0 -0
- package/common/rnexecutorch/tests/integration/stubs/jsi_stubs.cpp +0 -45
- package/common/rnexecutorch/tests/integration/utils/TestUtils.h +0 -36
- package/common/rnexecutorch/tests/run_tests.sh +0 -333
- package/common/rnexecutorch/tests/unit/FileUtilsTest.cpp +0 -32
- package/common/rnexecutorch/tests/unit/LogTest.cpp +0 -529
- package/common/rnexecutorch/tests/unit/NumericalTest.cpp +0 -107
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
#include "BaseModelTests.h"
|
|
2
|
-
#include <executorch/extension/tensor/tensor.h>
|
|
3
|
-
#include <gtest/gtest.h>
|
|
4
|
-
#include <limits>
|
|
5
|
-
#include <rnexecutorch/Error.h>
|
|
6
|
-
#include <rnexecutorch/models/BaseModel.h>
|
|
7
|
-
#include <vector>
|
|
8
|
-
|
|
9
|
-
using namespace rnexecutorch;
|
|
10
|
-
using namespace rnexecutorch::models;
|
|
11
|
-
using namespace executorch::extension;
|
|
12
|
-
using namespace model_tests;
|
|
13
|
-
using executorch::runtime::EValue;
|
|
14
|
-
|
|
15
|
-
constexpr auto kValidStyleTransferModelPath =
|
|
16
|
-
"style_transfer_candy_xnnpack.pte";
|
|
17
|
-
|
|
18
|
-
// ============================================================================
|
|
19
|
-
// Common tests via typed test suite
|
|
20
|
-
// ============================================================================
|
|
21
|
-
namespace model_tests {
|
|
22
|
-
template <> struct ModelTraits<BaseModel> {
|
|
23
|
-
using ModelType = BaseModel;
|
|
24
|
-
|
|
25
|
-
static ModelType createValid() {
|
|
26
|
-
return ModelType(kValidStyleTransferModelPath, nullptr);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
static ModelType createInvalid() {
|
|
30
|
-
return ModelType("nonexistent.pte", nullptr);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
static void callGenerate(ModelType &model) {
|
|
34
|
-
std::vector<int32_t> shape = {1, 3, 640, 640};
|
|
35
|
-
size_t numElements = 1 * 3 * 640 * 640;
|
|
36
|
-
std::vector<float> inputData(numElements, 0.5f);
|
|
37
|
-
auto tensorPtr = make_tensor_ptr(shape, inputData.data());
|
|
38
|
-
EValue input(*tensorPtr);
|
|
39
|
-
(void)model.forward(input);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
} // namespace model_tests
|
|
43
|
-
|
|
44
|
-
using BaseModelTypes = ::testing::Types<BaseModel>;
|
|
45
|
-
INSTANTIATE_TYPED_TEST_SUITE_P(BaseModel, CommonModelTest, BaseModelTypes);
|
|
46
|
-
|
|
47
|
-
// ============================================================================
|
|
48
|
-
// BaseModel-specific tests (methods not in all models)
|
|
49
|
-
// ============================================================================
|
|
50
|
-
|
|
51
|
-
TEST(BaseModelGetInputShapeTests, ValidMethodCorrectShape) {
|
|
52
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
53
|
-
auto forwardShape = model.getInputShape("forward", 0);
|
|
54
|
-
std::vector<int32_t> expectedShape = {1, 3, 640, 640};
|
|
55
|
-
EXPECT_EQ(forwardShape, expectedShape);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
TEST(BaseModelGetInputShapeTests, InvalidMethodThrows) {
|
|
59
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
60
|
-
EXPECT_THROW((void)model.getInputShape("this_method_does_not_exist", 0),
|
|
61
|
-
RnExecutorchError);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
TEST(BaseModelGetInputShapeTests, ValidMethodInvalidIndexThrows) {
|
|
65
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
66
|
-
EXPECT_THROW(
|
|
67
|
-
(void)model.getInputShape("forward", std::numeric_limits<int32_t>::min()),
|
|
68
|
-
RnExecutorchError);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
TEST(BaseModelGetAllInputShapesTests, ValidMethodReturnsShapes) {
|
|
72
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
73
|
-
auto allShapes = model.getAllInputShapes("forward");
|
|
74
|
-
EXPECT_FALSE(allShapes.empty());
|
|
75
|
-
std::vector<int32_t> expectedFirstShape = {1, 3, 640, 640};
|
|
76
|
-
EXPECT_EQ(allShapes[0], expectedFirstShape);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
TEST(BaseModelGetAllInputShapesTests, InvalidMethodThrows) {
|
|
80
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
81
|
-
EXPECT_THROW(model.getAllInputShapes("non_existent_method"),
|
|
82
|
-
RnExecutorchError);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
TEST(BaseModelGetMethodMetaTests, ValidMethodReturnsOk) {
|
|
86
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
87
|
-
auto result = model.getMethodMeta("forward");
|
|
88
|
-
EXPECT_TRUE(result.ok());
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
TEST(BaseModelGetMethodMetaTests, InvalidMethodReturnsError) {
|
|
92
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
93
|
-
auto result = model.getMethodMeta("non_existent_method");
|
|
94
|
-
EXPECT_FALSE(result.ok());
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
TEST(BaseModelForwardTests, ForwardWithValidInputReturnsOk) {
|
|
98
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
99
|
-
std::vector<int32_t> shape = {1, 3, 640, 640};
|
|
100
|
-
size_t numElements = 1 * 3 * 640 * 640;
|
|
101
|
-
std::vector<float> inputData(numElements, 0.5f);
|
|
102
|
-
auto tensorPtr = make_tensor_ptr(shape, inputData.data());
|
|
103
|
-
EValue input(*tensorPtr);
|
|
104
|
-
|
|
105
|
-
auto result = model.forward(input);
|
|
106
|
-
EXPECT_TRUE(result.ok());
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
TEST(BaseModelForwardTests, ForwardWithVectorInputReturnsOk) {
|
|
110
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
111
|
-
std::vector<int32_t> shape = {1, 3, 640, 640};
|
|
112
|
-
size_t numElements = 1 * 3 * 640 * 640;
|
|
113
|
-
std::vector<float> inputData(numElements, 0.5f);
|
|
114
|
-
auto tensorPtr = make_tensor_ptr(shape, inputData.data());
|
|
115
|
-
std::vector<EValue> inputs;
|
|
116
|
-
inputs.emplace_back(*tensorPtr);
|
|
117
|
-
|
|
118
|
-
auto result = model.forward(inputs);
|
|
119
|
-
EXPECT_TRUE(result.ok());
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
TEST(BaseModelForwardTests, ForwardReturnsCorrectOutputShape) {
|
|
123
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
124
|
-
std::vector<int32_t> shape = {1, 3, 640, 640};
|
|
125
|
-
size_t numElements = 1 * 3 * 640 * 640;
|
|
126
|
-
std::vector<float> inputData(numElements, 0.5f);
|
|
127
|
-
auto tensorPtr = make_tensor_ptr(shape, inputData.data());
|
|
128
|
-
EValue input(*tensorPtr);
|
|
129
|
-
|
|
130
|
-
auto result = model.forward(input);
|
|
131
|
-
ASSERT_TRUE(result.ok());
|
|
132
|
-
ASSERT_FALSE(result->empty());
|
|
133
|
-
|
|
134
|
-
auto &outputTensor = result->at(0).toTensor();
|
|
135
|
-
auto outputSizes = outputTensor.sizes();
|
|
136
|
-
EXPECT_EQ(outputSizes.size(), 4);
|
|
137
|
-
EXPECT_EQ(outputSizes[0], 1);
|
|
138
|
-
EXPECT_EQ(outputSizes[1], 3);
|
|
139
|
-
EXPECT_EQ(outputSizes[2], 640);
|
|
140
|
-
EXPECT_EQ(outputSizes[3], 640);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
TEST(BaseModelForwardTests, ForwardAfterUnloadThrows) {
|
|
144
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
145
|
-
model.unload();
|
|
146
|
-
|
|
147
|
-
std::vector<int32_t> shape = {1, 3, 640, 640};
|
|
148
|
-
size_t numElements = 1 * 3 * 640 * 640;
|
|
149
|
-
std::vector<float> inputData(numElements, 0.5f);
|
|
150
|
-
auto tensorPtr = make_tensor_ptr(shape, inputData.data());
|
|
151
|
-
EValue input(*tensorPtr);
|
|
152
|
-
|
|
153
|
-
EXPECT_THROW(model.forward(input), RnExecutorchError);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
TEST(BaseModelForwardJSTests, ForwardJSWithValidInputReturnsOutput) {
|
|
157
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
158
|
-
std::vector<int32_t> shape = {1, 3, 640, 640};
|
|
159
|
-
size_t numElements = 1 * 3 * 640 * 640;
|
|
160
|
-
std::vector<float> inputData(numElements, 0.5f);
|
|
161
|
-
|
|
162
|
-
JSTensorViewIn tensorView;
|
|
163
|
-
tensorView.dataPtr = inputData.data();
|
|
164
|
-
tensorView.sizes = shape;
|
|
165
|
-
tensorView.scalarType = executorch::aten::ScalarType::Float;
|
|
166
|
-
|
|
167
|
-
std::vector<JSTensorViewIn> inputs = {tensorView};
|
|
168
|
-
auto outputs = model.forwardJS(inputs);
|
|
169
|
-
|
|
170
|
-
EXPECT_FALSE(outputs.empty());
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
TEST(BaseModelForwardJSTests, ForwardJSReturnsCorrectOutputShape) {
|
|
174
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
175
|
-
std::vector<int32_t> shape = {1, 3, 640, 640};
|
|
176
|
-
size_t numElements = 1 * 3 * 640 * 640;
|
|
177
|
-
std::vector<float> inputData(numElements, 0.5f);
|
|
178
|
-
|
|
179
|
-
JSTensorViewIn tensorView;
|
|
180
|
-
tensorView.dataPtr = inputData.data();
|
|
181
|
-
tensorView.sizes = shape;
|
|
182
|
-
tensorView.scalarType = executorch::aten::ScalarType::Float;
|
|
183
|
-
|
|
184
|
-
std::vector<JSTensorViewIn> inputs = {tensorView};
|
|
185
|
-
auto outputs = model.forwardJS(inputs);
|
|
186
|
-
|
|
187
|
-
ASSERT_EQ(outputs.size(), 1);
|
|
188
|
-
std::vector<int32_t> expectedShape = {1, 3, 640, 640};
|
|
189
|
-
EXPECT_EQ(outputs[0].sizes, expectedShape);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
TEST(BaseModelForwardJSTests, ForwardJSAfterUnloadThrows) {
|
|
193
|
-
BaseModel model(kValidStyleTransferModelPath, nullptr);
|
|
194
|
-
model.unload();
|
|
195
|
-
|
|
196
|
-
std::vector<int32_t> shape = {1, 3, 640, 640};
|
|
197
|
-
size_t numElements = 1 * 3 * 640 * 640;
|
|
198
|
-
std::vector<float> inputData(numElements, 0.5f);
|
|
199
|
-
|
|
200
|
-
JSTensorViewIn tensorView;
|
|
201
|
-
tensorView.dataPtr = inputData.data();
|
|
202
|
-
tensorView.sizes = shape;
|
|
203
|
-
tensorView.scalarType = executorch::aten::ScalarType::Float;
|
|
204
|
-
|
|
205
|
-
std::vector<JSTensorViewIn> inputs = {tensorView};
|
|
206
|
-
EXPECT_THROW((void)model.forwardJS(inputs), RnExecutorchError);
|
|
207
|
-
}
|
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "gtest/gtest.h"
|
|
4
|
-
#include <rnexecutorch/Error.h>
|
|
5
|
-
|
|
6
|
-
namespace facebook::react {
|
|
7
|
-
class CallInvoker;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
namespace rnexecutorch {
|
|
11
|
-
std::shared_ptr<facebook::react::CallInvoker> createMockCallInvoker();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
namespace model_tests {
|
|
15
|
-
|
|
16
|
-
inline auto getMockInvoker() { return rnexecutorch::createMockCallInvoker(); }
|
|
17
|
-
|
|
18
|
-
/// Helper macro to access Traits in typed tests
|
|
19
|
-
#define SETUP_TRAITS() using Traits = typename TestFixture::Traits
|
|
20
|
-
|
|
21
|
-
/// Trait struct that each model must specialize
|
|
22
|
-
/// This defines how to construct and test each model type
|
|
23
|
-
template <typename T> struct ModelTraits;
|
|
24
|
-
|
|
25
|
-
/// Example of what a specialization looks like:
|
|
26
|
-
///
|
|
27
|
-
/// template<>
|
|
28
|
-
/// struct ModelTraits<Classification> {
|
|
29
|
-
/// using ModelType = Classification;
|
|
30
|
-
///
|
|
31
|
-
/// // Create valid model instance
|
|
32
|
-
/// static ModelType createValid() {
|
|
33
|
-
/// return ModelType("valid_model.pte", nullptr);
|
|
34
|
-
/// }
|
|
35
|
-
///
|
|
36
|
-
/// // Create invalid model instance (should throw in constructor)
|
|
37
|
-
/// static ModelType createInvalid() {
|
|
38
|
-
/// return ModelType("nonexistent.pte", nullptr);
|
|
39
|
-
/// }
|
|
40
|
-
///
|
|
41
|
-
/// // Call the model's generate/forward function with valid input
|
|
42
|
-
/// // Used to test that generate throws after unload
|
|
43
|
-
/// static void callGenerate(ModelType& model) {
|
|
44
|
-
/// (void)model.generate("valid_input.jpg");
|
|
45
|
-
/// }
|
|
46
|
-
/// };
|
|
47
|
-
// Typed test fixture for common model tests
|
|
48
|
-
template <typename T> class CommonModelTest : public ::testing::Test {
|
|
49
|
-
protected:
|
|
50
|
-
using Traits = ModelTraits<T>;
|
|
51
|
-
using ModelType = typename Traits::ModelType;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
// Define the test suite
|
|
55
|
-
TYPED_TEST_SUITE_P(CommonModelTest);
|
|
56
|
-
|
|
57
|
-
// Constructor tests
|
|
58
|
-
TYPED_TEST_P(CommonModelTest, InvalidPathThrows) {
|
|
59
|
-
SETUP_TRAITS();
|
|
60
|
-
EXPECT_THROW(Traits::createInvalid(), rnexecutorch::RnExecutorchError);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
TYPED_TEST_P(CommonModelTest, ValidPathDoesntThrow) {
|
|
64
|
-
SETUP_TRAITS();
|
|
65
|
-
EXPECT_NO_THROW(Traits::createValid());
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Memory tests
|
|
69
|
-
TYPED_TEST_P(CommonModelTest, GetMemoryLowerBoundValue) {
|
|
70
|
-
SETUP_TRAITS();
|
|
71
|
-
auto model = Traits::createValid();
|
|
72
|
-
EXPECT_GT(model.getMemoryLowerBound(), 0u);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
TYPED_TEST_P(CommonModelTest, GetMemoryLowerBoundConsistent) {
|
|
76
|
-
SETUP_TRAITS();
|
|
77
|
-
auto model = Traits::createValid();
|
|
78
|
-
auto bound1 = model.getMemoryLowerBound();
|
|
79
|
-
auto bound2 = model.getMemoryLowerBound();
|
|
80
|
-
EXPECT_EQ(bound1, bound2);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Unload tests
|
|
84
|
-
TYPED_TEST_P(CommonModelTest, UnloadDoesntThrow) {
|
|
85
|
-
SETUP_TRAITS();
|
|
86
|
-
auto model = Traits::createValid();
|
|
87
|
-
EXPECT_NO_THROW(model.unload());
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
TYPED_TEST_P(CommonModelTest, MultipleUnloadsSafe) {
|
|
91
|
-
SETUP_TRAITS();
|
|
92
|
-
auto model = Traits::createValid();
|
|
93
|
-
EXPECT_NO_THROW(model.unload());
|
|
94
|
-
EXPECT_NO_THROW(model.unload());
|
|
95
|
-
EXPECT_NO_THROW(model.unload());
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
TYPED_TEST_P(CommonModelTest, GenerateAfterUnloadThrows) {
|
|
99
|
-
SETUP_TRAITS();
|
|
100
|
-
auto model = Traits::createValid();
|
|
101
|
-
model.unload();
|
|
102
|
-
EXPECT_THROW(Traits::callGenerate(model), rnexecutorch::RnExecutorchError);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
TYPED_TEST_P(CommonModelTest, MultipleGeneratesWork) {
|
|
106
|
-
SETUP_TRAITS();
|
|
107
|
-
auto model = Traits::createValid();
|
|
108
|
-
EXPECT_NO_THROW(Traits::callGenerate(model));
|
|
109
|
-
EXPECT_NO_THROW(Traits::callGenerate(model));
|
|
110
|
-
EXPECT_NO_THROW(Traits::callGenerate(model));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Register all tests in the suite
|
|
114
|
-
REGISTER_TYPED_TEST_SUITE_P(CommonModelTest, InvalidPathThrows,
|
|
115
|
-
ValidPathDoesntThrow, GetMemoryLowerBoundValue,
|
|
116
|
-
GetMemoryLowerBoundConsistent, UnloadDoesntThrow,
|
|
117
|
-
MultipleUnloadsSafe, GenerateAfterUnloadThrows,
|
|
118
|
-
MultipleGeneratesWork);
|
|
119
|
-
|
|
120
|
-
} // namespace model_tests
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
#include "BaseModelTests.h"
|
|
2
|
-
#include <gtest/gtest.h>
|
|
3
|
-
#include <rnexecutorch/Error.h>
|
|
4
|
-
#include <rnexecutorch/models/classification/Classification.h>
|
|
5
|
-
#include <rnexecutorch/models/classification/Constants.h>
|
|
6
|
-
|
|
7
|
-
using namespace rnexecutorch;
|
|
8
|
-
using namespace rnexecutorch::models::classification;
|
|
9
|
-
using namespace model_tests;
|
|
10
|
-
|
|
11
|
-
constexpr auto kValidClassificationModelPath = "efficientnet_v2_s_xnnpack.pte";
|
|
12
|
-
constexpr auto kValidTestImagePath =
|
|
13
|
-
"file:///data/local/tmp/rnexecutorch_tests/test_image.jpg";
|
|
14
|
-
|
|
15
|
-
// ============================================================================
|
|
16
|
-
// Common tests via typed test suite
|
|
17
|
-
// ============================================================================
|
|
18
|
-
namespace model_tests {
|
|
19
|
-
template <> struct ModelTraits<Classification> {
|
|
20
|
-
using ModelType = Classification;
|
|
21
|
-
|
|
22
|
-
static ModelType createValid() {
|
|
23
|
-
return ModelType(kValidClassificationModelPath, nullptr);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
static ModelType createInvalid() {
|
|
27
|
-
return ModelType("nonexistent.pte", nullptr);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
static void callGenerate(ModelType &model) {
|
|
31
|
-
(void)model.generate(kValidTestImagePath);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
} // namespace model_tests
|
|
35
|
-
|
|
36
|
-
using ClassificationTypes = ::testing::Types<Classification>;
|
|
37
|
-
INSTANTIATE_TYPED_TEST_SUITE_P(Classification, CommonModelTest,
|
|
38
|
-
ClassificationTypes);
|
|
39
|
-
|
|
40
|
-
// ============================================================================
|
|
41
|
-
// Model-specific tests
|
|
42
|
-
// ============================================================================
|
|
43
|
-
TEST(ClassificationGenerateTests, InvalidImagePathThrows) {
|
|
44
|
-
Classification model(kValidClassificationModelPath, nullptr);
|
|
45
|
-
EXPECT_THROW((void)model.generate("nonexistent_image.jpg"),
|
|
46
|
-
RnExecutorchError);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
TEST(ClassificationGenerateTests, EmptyImagePathThrows) {
|
|
50
|
-
Classification model(kValidClassificationModelPath, nullptr);
|
|
51
|
-
EXPECT_THROW((void)model.generate(""), RnExecutorchError);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
TEST(ClassificationGenerateTests, MalformedURIThrows) {
|
|
55
|
-
Classification model(kValidClassificationModelPath, nullptr);
|
|
56
|
-
EXPECT_THROW((void)model.generate("not_a_valid_uri://bad"),
|
|
57
|
-
RnExecutorchError);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
TEST(ClassificationGenerateTests, ValidImageReturnsResults) {
|
|
61
|
-
Classification model(kValidClassificationModelPath, nullptr);
|
|
62
|
-
auto results = model.generate(kValidTestImagePath);
|
|
63
|
-
EXPECT_FALSE(results.empty());
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
TEST(ClassificationGenerateTests, ResultsHaveCorrectSize) {
|
|
67
|
-
Classification model(kValidClassificationModelPath, nullptr);
|
|
68
|
-
auto results = model.generate(kValidTestImagePath);
|
|
69
|
-
auto expectedNumClasses = constants::kImagenet1kV1Labels.size();
|
|
70
|
-
EXPECT_EQ(results.size(), expectedNumClasses);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
TEST(ClassificationGenerateTests, ResultsContainValidProbabilities) {
|
|
74
|
-
Classification model(kValidClassificationModelPath, nullptr);
|
|
75
|
-
auto results = model.generate(kValidTestImagePath);
|
|
76
|
-
|
|
77
|
-
float sum = 0.0f;
|
|
78
|
-
for (const auto &[label, prob] : results) {
|
|
79
|
-
EXPECT_GE(prob, 0.0f);
|
|
80
|
-
EXPECT_LE(prob, 1.0f);
|
|
81
|
-
sum += prob;
|
|
82
|
-
}
|
|
83
|
-
EXPECT_NEAR(sum, 1.0f, 0.01f);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
TEST(ClassificationGenerateTests, TopPredictionHasReasonableConfidence) {
|
|
87
|
-
Classification model(kValidClassificationModelPath, nullptr);
|
|
88
|
-
auto results = model.generate(kValidTestImagePath);
|
|
89
|
-
|
|
90
|
-
float maxProb = 0.0f;
|
|
91
|
-
for (const auto &[label, prob] : results) {
|
|
92
|
-
if (prob > maxProb) {
|
|
93
|
-
maxProb = prob;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
EXPECT_GT(maxProb, 0.0f);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
TEST(ClassificationInheritedTests, GetInputShapeWorks) {
|
|
100
|
-
Classification model(kValidClassificationModelPath, nullptr);
|
|
101
|
-
auto shape = model.getInputShape("forward", 0);
|
|
102
|
-
EXPECT_EQ(shape.size(), 4);
|
|
103
|
-
EXPECT_EQ(shape[0], 1);
|
|
104
|
-
EXPECT_EQ(shape[1], 3);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
TEST(ClassificationInheritedTests, GetAllInputShapesWorks) {
|
|
108
|
-
Classification model(kValidClassificationModelPath, nullptr);
|
|
109
|
-
auto shapes = model.getAllInputShapes("forward");
|
|
110
|
-
EXPECT_FALSE(shapes.empty());
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
TEST(ClassificationInheritedTests, GetMethodMetaWorks) {
|
|
114
|
-
Classification model(kValidClassificationModelPath, nullptr);
|
|
115
|
-
auto result = model.getMethodMeta("forward");
|
|
116
|
-
EXPECT_TRUE(result.ok());
|
|
117
|
-
}
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
#include "BaseModelTests.h"
|
|
2
|
-
#include <cmath>
|
|
3
|
-
#include <gtest/gtest.h>
|
|
4
|
-
#include <rnexecutorch/Error.h>
|
|
5
|
-
#include <rnexecutorch/models/embeddings/image/ImageEmbeddings.h>
|
|
6
|
-
|
|
7
|
-
using namespace rnexecutorch;
|
|
8
|
-
using namespace rnexecutorch::models::embeddings;
|
|
9
|
-
using namespace model_tests;
|
|
10
|
-
|
|
11
|
-
constexpr auto kValidImageEmbeddingsModelPath =
|
|
12
|
-
"clip-vit-base-patch32-vision_xnnpack.pte";
|
|
13
|
-
constexpr auto kValidTestImagePath =
|
|
14
|
-
"file:///data/local/tmp/rnexecutorch_tests/test_image.jpg";
|
|
15
|
-
|
|
16
|
-
// ============================================================================
|
|
17
|
-
// Common tests via typed test suite
|
|
18
|
-
// ============================================================================
|
|
19
|
-
namespace model_tests {
|
|
20
|
-
template <> struct ModelTraits<ImageEmbeddings> {
|
|
21
|
-
using ModelType = ImageEmbeddings;
|
|
22
|
-
|
|
23
|
-
static ModelType createValid() {
|
|
24
|
-
return ModelType(kValidImageEmbeddingsModelPath, nullptr);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
static ModelType createInvalid() {
|
|
28
|
-
return ModelType("nonexistent.pte", nullptr);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
static void callGenerate(ModelType &model) {
|
|
32
|
-
(void)model.generate(kValidTestImagePath);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
} // namespace model_tests
|
|
36
|
-
|
|
37
|
-
using ImageEmbeddingsTypes = ::testing::Types<ImageEmbeddings>;
|
|
38
|
-
INSTANTIATE_TYPED_TEST_SUITE_P(ImageEmbeddings, CommonModelTest,
|
|
39
|
-
ImageEmbeddingsTypes);
|
|
40
|
-
|
|
41
|
-
// ============================================================================
|
|
42
|
-
// Model-specific tests
|
|
43
|
-
// ============================================================================
|
|
44
|
-
TEST(ImageEmbeddingsGenerateTests, InvalidImagePathThrows) {
|
|
45
|
-
ImageEmbeddings model(kValidImageEmbeddingsModelPath, nullptr);
|
|
46
|
-
EXPECT_THROW((void)model.generate("nonexistent_image.jpg"),
|
|
47
|
-
RnExecutorchError);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
TEST(ImageEmbeddingsGenerateTests, EmptyImagePathThrows) {
|
|
51
|
-
ImageEmbeddings model(kValidImageEmbeddingsModelPath, nullptr);
|
|
52
|
-
EXPECT_THROW((void)model.generate(""), RnExecutorchError);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
TEST(ImageEmbeddingsGenerateTests, MalformedURIThrows) {
|
|
56
|
-
ImageEmbeddings model(kValidImageEmbeddingsModelPath, nullptr);
|
|
57
|
-
EXPECT_THROW((void)model.generate("not_a_valid_uri://bad"),
|
|
58
|
-
RnExecutorchError);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
TEST(ImageEmbeddingsGenerateTests, ValidImageReturnsResults) {
|
|
62
|
-
ImageEmbeddings model(kValidImageEmbeddingsModelPath, nullptr);
|
|
63
|
-
auto result = model.generate(kValidTestImagePath);
|
|
64
|
-
EXPECT_NE(result, nullptr);
|
|
65
|
-
EXPECT_GT(result->size(), 0u);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
TEST(ImageEmbeddingsGenerateTests, ResultsHaveCorrectSize) {
|
|
69
|
-
ImageEmbeddings model(kValidImageEmbeddingsModelPath, nullptr);
|
|
70
|
-
auto result = model.generate(kValidTestImagePath);
|
|
71
|
-
size_t numFloats = result->size() / sizeof(float);
|
|
72
|
-
constexpr size_t kClipEmbeddingDimensions = 512;
|
|
73
|
-
EXPECT_EQ(numFloats, kClipEmbeddingDimensions);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
TEST(ImageEmbeddingsGenerateTests, ResultsAreNormalized) {
|
|
77
|
-
ImageEmbeddings model(kValidImageEmbeddingsModelPath, nullptr);
|
|
78
|
-
auto result = model.generate(kValidTestImagePath);
|
|
79
|
-
|
|
80
|
-
const float *data = reinterpret_cast<const float *>(result->data());
|
|
81
|
-
size_t numFloats = result->size() / sizeof(float);
|
|
82
|
-
|
|
83
|
-
float sumOfSquares = 0.0f;
|
|
84
|
-
for (size_t i = 0; i < numFloats; ++i) {
|
|
85
|
-
sumOfSquares += data[i] * data[i];
|
|
86
|
-
}
|
|
87
|
-
float norm = std::sqrt(sumOfSquares);
|
|
88
|
-
EXPECT_NEAR(norm, 1.0f, 0.01f);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
TEST(ImageEmbeddingsGenerateTests, ResultsContainValidValues) {
|
|
92
|
-
ImageEmbeddings model(kValidImageEmbeddingsModelPath, nullptr);
|
|
93
|
-
auto result = model.generate(kValidTestImagePath);
|
|
94
|
-
|
|
95
|
-
const float *data = reinterpret_cast<const float *>(result->data());
|
|
96
|
-
size_t numFloats = result->size() / sizeof(float);
|
|
97
|
-
|
|
98
|
-
for (size_t i = 0; i < numFloats; ++i) {
|
|
99
|
-
EXPECT_FALSE(std::isnan(data[i]));
|
|
100
|
-
EXPECT_FALSE(std::isinf(data[i]));
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
TEST(ImageEmbeddingsInheritedTests, GetInputShapeWorks) {
|
|
105
|
-
ImageEmbeddings model(kValidImageEmbeddingsModelPath, nullptr);
|
|
106
|
-
auto shape = model.getInputShape("forward", 0);
|
|
107
|
-
EXPECT_EQ(shape.size(), 4);
|
|
108
|
-
EXPECT_EQ(shape[0], 1);
|
|
109
|
-
EXPECT_EQ(shape[1], 3);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
TEST(ImageEmbeddingsInheritedTests, GetAllInputShapesWorks) {
|
|
113
|
-
ImageEmbeddings model(kValidImageEmbeddingsModelPath, nullptr);
|
|
114
|
-
auto shapes = model.getAllInputShapes("forward");
|
|
115
|
-
EXPECT_FALSE(shapes.empty());
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
TEST(ImageEmbeddingsInheritedTests, GetMethodMetaWorks) {
|
|
119
|
-
ImageEmbeddings model(kValidImageEmbeddingsModelPath, nullptr);
|
|
120
|
-
auto result = model.getMethodMeta("forward");
|
|
121
|
-
EXPECT_TRUE(result.ok());
|
|
122
|
-
}
|