-
Notifications
You must be signed in to change notification settings - Fork 45
Improve numerical util files and testing infastructure #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chmjkb
merged 37 commits into
software-mansion:main
from
msluszniak:@ms/improve_numerical_utils_files
Aug 28, 2025
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
cefe6b2
Improve numerical utils
90369ea
Improve testing using CMakeLists.txt
1027fc2
Add newlines
7e00965
Small fixes
ccbf247
Add more tests and clear implementation
993e58c
Update packages/react-native-executorch/common/rnexecutorch/tests/REA…
msluszniak 26bf042
Change z-normalization to norm normalization
b55ef43
Update packages/react-native-executorch/common/rnexecutorch/tests/REA…
msluszniak d8bfe2c
Update NumericalTest.cpp
msluszniak 2c26e47
Apply suggestions from code review
msluszniak e8098b9
Apply suggestions from code review
msluszniak 92f5931
Remove [[unlikely]] attributes
msluszniak 12730f2
Merge branch 'main' into @ms/improve_numerical_utils_files
msluszniak 3b72f45
Apply suggestions from code review
msluszniak b7435c4
Apply suggestions from code review
msluszniak e445bbc
Update packages/react-native-executorch/common/rnexecutorch/data_proc…
msluszniak 36c26e5
Merge branch 'main' into @ms/improve_numerical_utils_files
msluszniak a1a93d3
Apply suggestions from code review
msluszniak 88bea9f
Apply suggestions from code review
msluszniak c705722
Removed googletest submodule from old location
msluszniak f37a441
Added googletest submodule at new location
msluszniak fafa6c9
Change structure of CMakeLists.txt
msluszniak 60ec9e8
Update packages/react-native-executorch/common/rnexecutorch/tests/CMa…
msluszniak 9c1facc
Improve README.md
msluszniak 85f92dd
NITs to README.md
msluszniak b51fc33
Update packages/react-native-executorch/common/rnexecutorch/data_proc…
msluszniak 146f745
Update packages/react-native-executorch/common/rnexecutorch/tests/Num…
msluszniak 5ec963e
Update packages/react-native-executorch/common/rnexecutorch/tests/CMa…
msluszniak 0c460f2
Change name of variable to correct one
msluszniak 61f3fcd
Change the way epsilon is handled
msluszniak bc823ea
Update NumericalTest.cpp after changing way we handle epsilons
msluszniak a130513
Update packages/react-native-executorch/common/rnexecutorch/data_proc…
msluszniak 7e4b5dd
Remove redundant check and remove redundant newlines
msluszniak 6e421ad
Add tests for softmax and normalization of empty vector
msluszniak 2a139c7
Remove duplicated doxygen comment
msluszniak 7b28a58
Apply suggestions from code review
msluszniak 969eeda
Fix typo in readme
msluszniak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,10 @@ QINT | |
FNUZ | ||
wordlist | ||
jitpack | ||
googletest | ||
ctest | ||
gtest | ||
rnexecutorch | ||
coreml | ||
mobilenetv | ||
flac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/react-native-executorch/common/rnexecutorch/tests/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
project(RNExecutorchTests) | ||
|
||
# C++ standard | ||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_CXX_STANDARD_REQUIRED TRUE) | ||
|
||
# googletest subdirectory | ||
# Using an absolute path from the top-level source directory | ||
add_subdirectory(${CMAKE_SOURCE_DIR}/../../../../../third-party/googletest ${PROJECT_BINARY_DIR}/googletest) | ||
|
||
# Directories to include | ||
include_directories(${CMAKE_SOURCE_DIR}/../data_processing) | ||
include_directories(${CMAKE_SOURCE_DIR}/..) | ||
|
||
# Source files | ||
set(SOURCE_FILES ${CMAKE_SOURCE_DIR}/../data_processing/Numerical.cpp) | ||
|
||
# Executables for the tests | ||
add_executable(NumericalTests NumericalTest.cpp ${SOURCE_FILES}) | ||
add_executable(LogTests LogTest.cpp) | ||
|
||
# Libraries linking | ||
target_link_libraries(NumericalTests gtest gtest_main) | ||
target_link_libraries(LogTests gtest gtest_main) | ||
|
||
# Testing functionalities | ||
enable_testing() | ||
add_test(NAME NumericalTests COMMAND NumericalTests) | ||
add_test(NAME LogTests COMMAND LogTests) |
110 changes: 110 additions & 0 deletions
110
packages/react-native-executorch/common/rnexecutorch/tests/NumericalTest.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#include "../data_processing/Numerical.h" | ||
#include <gtest/gtest.h> | ||
#include <limits> | ||
msluszniak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <span> | ||
#include <stdexcept> | ||
#include <vector> | ||
|
||
namespace rnexecutorch::numerical { | ||
|
||
// Helper function to check if two float vectors are approximately equal | ||
void expect_vectors_eq(const std::vector<float> &vector1, | ||
const std::vector<float> &vector2, float atol = 1.0e-6F) { | ||
ASSERT_EQ(vector1.size(), vector2.size()); | ||
for (size_t i = 0; i < vector1.size(); i++) { | ||
EXPECT_NEAR(vector1[i], vector2[i], atol); | ||
} | ||
} | ||
|
||
TEST(SoftmaxTests, SoftmaxBasic) { | ||
std::vector<float> input = {1.0F, 2.0F, 3.0F}; | ||
softmax(input); | ||
const std::vector<float> expected = {0.09003057F, 0.24472847F, 0.66524095F}; | ||
expect_vectors_eq(input, expected); | ||
} | ||
|
||
TEST(SoftmaxTests, SoftmaxWithBigValues) { | ||
std::vector<float> input = {100000.0F, 100000.0F, 100000.0F}; | ||
softmax(input); | ||
const std::vector<float> expected = {0.3333333F, 0.3333333F, 0.3333333F}; | ||
expect_vectors_eq(input, expected); | ||
} | ||
|
||
TEST(SoftmaxTests, SoftmaxOfEmptyVector) { | ||
std::vector<float> emptyVector{}; | ||
EXPECT_NO_THROW(softmax(emptyVector)); | ||
} | ||
|
||
TEST(NormalizeTests, NormalizeBasic) { | ||
std::vector<float> input = {1.0F, 2.0F, 3.0F}; | ||
normalize(input); | ||
const auto normOfInput = std::sqrtf(14.0F); | ||
const std::vector<float> expected = {1.0F / normOfInput, 2.0F / normOfInput, | ||
3.0F / normOfInput}; | ||
expect_vectors_eq(input, expected); | ||
} | ||
|
||
TEST(NormalizeTests, NormalizationOfExtremelySmallValues) { | ||
constexpr auto epsilon = std::numeric_limits<float>::epsilon(); | ||
std::vector<float> input(3, epsilon); | ||
const auto normOfInput = std::sqrtf(3.0F); | ||
const std::vector<float> expected(3, 1.0F / normOfInput); | ||
normalize(input); | ||
expect_vectors_eq(input, expected); | ||
} | ||
|
||
TEST(NormalizeTests, NormalizationOfZeroVector) { | ||
std::vector<float> zeroVector(3, 0.0F); | ||
EXPECT_NO_THROW(normalize(zeroVector)); | ||
} | ||
|
||
TEST(NormalizeTests, NormalizationOfEmptyVector) { | ||
std::vector<float> emptyVector{}; | ||
EXPECT_NO_THROW(normalize(emptyVector)); | ||
} | ||
|
||
TEST(MeanPoolingTests, MeanPoolingBasic) { | ||
const std::vector<float> modelOutputVec = {1.0F, 2.0F, 3.0F, | ||
4.0F, 5.0F, 6.0F}; | ||
const std::vector<int64_t> attnMaskVec = {1, 1, 0}; | ||
|
||
std::span<const float> modelOutput(modelOutputVec); | ||
std::span<const int64_t> attnMask(attnMaskVec); | ||
|
||
const auto result = meanPooling(modelOutput, attnMask); | ||
const std::vector<float> expected = {2.0F, 3.0F}; | ||
expect_vectors_eq(result, expected); | ||
} | ||
|
||
TEST(MeanPoolingTests, MeanPoolingWithZeroAttentionMask) { | ||
const std::vector<float> modelOutputVec = {1.0F, 2.0F, 3.0F, | ||
4.0F, 5.0F, 6.0F}; | ||
const std::vector<int64_t> attnMaskVec = {0, 0, 0}; | ||
|
||
std::span<const float> modelOutput(modelOutputVec); | ||
std::span<const int64_t> attnMask(attnMaskVec); | ||
|
||
const auto result = meanPooling(modelOutput, attnMask); | ||
const std::vector<float> expected = {0.0F, 0.0F}; | ||
expect_vectors_eq(result, expected); | ||
} | ||
|
||
TEST(MeanPoolingTests, InvalidDimensionSize) { | ||
const std::vector<float> modelOutput = {1.0F, 2.0F, 3.0F, 4.0F}; | ||
const std::vector<int64_t> attnMask = {1, 1, 1}; | ||
|
||
EXPECT_THROW( | ||
{ meanPooling(modelOutput, attnMask); }, | ||
std::invalid_argument); | ||
} | ||
|
||
TEST(MeanPoolingTests, EmptyAttentionMask) { | ||
const std::vector<float> modelOutput = {1.0F, 2.0F, 3.0F, 4.0F}; | ||
const std::vector<int64_t> attnMask = {}; | ||
|
||
EXPECT_THROW( | ||
{ meanPooling(modelOutput, attnMask); }, | ||
std::invalid_argument); | ||
} | ||
|
||
} // namespace rnexecutorch::numerical |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.