Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ inline std::string loadBytesFromFile(const std::string &path) {
}
std::string data;
fs.seekg(0, std::ios::end);
size_t size = static_cast<size_t>(fs.tellg());
auto size = static_cast<size_t>(fs.tellg());
fs.seekg(0, std::ios::beg);
data.resize(size);
fs.read(data.data(), size);
return data;
};
}

} // namespace rnexecutorch::file_utils
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstddef>
#include <limits>
#include <math.h>
#include <numbers>
#include <rnexecutorch/data_processing/FFT.h>
#include <rnexecutorch/data_processing/dsp.h>

namespace rnexecutorch::dsp {

using namespace rnexecutorch::dsp;
using std::numbers::pi_v;

std::vector<float> hannWindow(size_t size) {
// https://www.mathworks.com/help/signal/ref/hann.html
std::vector<float> window(size);
for (size_t i = 0; i < size; i++) {
window[i] = 0.5f * (1 - std::cos(2 * M_PI * i / size));
}
return window;
}
namespace {
std::vector<float> hannWindow(size_t size) {
std::vector<float> window(size);
for (size_t i = 0; i < size; i++) {
window[i] = 0.5f * (1.0f - std::cosf(2.0f * pi_v<float> * static_cast<float>(i) / static_cast<float>(size)));
}
return window;
}
} // namespace

std::vector<float> stftFromWaveform(std::span<const float> waveform,
size_t fftWindowSize, size_t hopSize) {
Expand All @@ -26,8 +29,8 @@ std::vector<float> stftFromWaveform(std::span<const float> waveform,
const auto numFrames = 1 + (waveform.size() - fftWindowSize) / hopSize;
const auto numBins = fftWindowSize / 2;
const auto hann = hannWindow(fftWindowSize);
auto inBuffer = std::vector<float>(fftWindowSize);
auto outBuffer = std::vector<std::complex<float>>(fftWindowSize);
std::vector<float> inBuffer(fftWindowSize);
std::vector<std::complex<float>> outBuffer(fftWindowSize);

// Output magnitudes in dB
std::vector<float> magnitudes;
Expand All @@ -54,7 +57,7 @@ std::vector<float> stftFromWaveform(std::span<const float> waveform,
for (size_t i = 0; i < numBins; i++) {
const auto magnitude = std::abs(outBuffer[i]) * magnitudeScale;
const auto magnitude_db =
dbConversionFactor * log10f(magnitude + epsilon);
dbConversionFactor * std::log10f(magnitude + epsilon);
magnitudes.push_back(magnitude_db);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace rnexecutorch::dsp {

std::vector<float> hannWindow(size_t size);

std::vector<float> stftFromWaveform(std::span<const float> waveform,
size_t fftWindowSize, size_t hopSize);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ include_directories(${CMAKE_SOURCE_DIR}/../data_processing)
include_directories(${CMAKE_SOURCE_DIR}/..)

# Source files
set(SOURCE_FILES ${CMAKE_SOURCE_DIR}/../data_processing/Numerical.cpp)
set(SOURCE_FILES ${CMAKE_SOURCE_DIR}/../data_processing/Numerical.cpp
${CMAKE_SOURCE_DIR}/../data_processing/FileUtils.h)

# Executables for the tests
add_executable(NumericalTests NumericalTest.cpp ${SOURCE_FILES})
add_executable(FileUtilsTests FileUtilsTest.cpp ${SOURCE_FILES})
add_executable(LogTests LogTest.cpp)

# Libraries linking
target_link_libraries(NumericalTests gtest gtest_main)
target_link_libraries(FileUtilsTests gtest gtest_main)
target_link_libraries(LogTests gtest gtest_main)

# Testing functionalities
enable_testing()
add_test(NAME NumericalTests COMMAND NumericalTests)
add_test(NAME FileUtilsTests COMMAND FileUtilsTests)
add_test(NAME LogTests COMMAND LogTests)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "../data_processing/FileUtils.h"
#include <fstream>
#include <gtest/gtest.h>
#include <iostream>

namespace rnexecutorch::file_utils {
class FileIOTest : public ::testing::Test {
protected:
std::string tempFileName = "temp_test_file.txt";

void SetUp() override {
std::ofstream out(tempFileName, std::ios::binary);
out << "Hello, world";
out.close();
}

void TearDown() override {
std::remove(tempFileName.c_str());
}
};

TEST_F(FileIOTest, LoadBytesFromFileSuccessfully) {
std::string data = loadBytesFromFile(tempFileName);
EXPECT_EQ(data, "Hello, world");
}

TEST_F(FileIOTest, LoadBytesFromFileFailOnNonExistentFile) {
EXPECT_THROW(
{ loadBytesFromFile("non_existent_file.txt"); }, std::runtime_error);
}
} // namespace rnexecutorch::file_utils
17 changes: 2 additions & 15 deletions packages/react-native-executorch/common/runner/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <ctime>
#include <fstream>
#include <iostream>
#include <rnexecutorch/data_processing/FileUtils.h>

namespace example {

Expand All @@ -23,20 +24,6 @@ using ::executorch::runtime::Result;

namespace llm = ::executorch::extension::llm;

std::string loadBytesFromFile(const std::string &path) {
std::ifstream fs(path, std::ios::in | std::ios::binary);
if (fs.fail()) {
throw std::runtime_error("Failed to open tokenizer file");
}
std::string data;
fs.seekg(0, std::ios::end);
size_t size = static_cast<size_t>(fs.tellg());
fs.seekg(0, std::ios::beg);
data.resize(size);
fs.read(data.data(), size);
return data;
}

namespace {
static constexpr auto kEnableDynamicShape = "enable_dynamic_shape";
static constexpr auto kEosIds = "get_eos_ids";
Expand Down Expand Up @@ -83,7 +70,7 @@ Error Runner::load() {
ET_CHECK_OK_OR_RETURN_ERROR(module_->load_method("forward"));
// load tokenizer.

auto blob = loadBytesFromFile(tokenizer_path_);
auto blob = rnexecutorch::file_utils::loadBytesFromFile(tokenizer_path_);
tokenizer_ = tokenizers::Tokenizer::FromBlobJSON(blob);

ET_LOG(Info, "Reading metadata from model");
Expand Down
Loading