Skip to content

Commit

Permalink
feat: improve performance and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Oct 24, 2024
1 parent c8eee68 commit d91058b
Show file tree
Hide file tree
Showing 25 changed files with 9,829 additions and 9,620 deletions.
17 changes: 14 additions & 3 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ project(BarkRn)
set (CMAKE_VERBOSE_MAKEFILE ON)
set (CMAKE_CXX_STANDARD 17)

set(CMAKE_C_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")


if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfma -mf16c -mavx -mavx2")
Expand All @@ -28,9 +27,21 @@ add_library(bark-rn SHARED
cpp-adapter.cpp
)

if (CMAKE_SYSTEM_PROCESSOR MATCHES "arm64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
target_compile_options(bark-rn PRIVATE -march=armv8.2-a+fp16)
endif()

target_compile_options(bark-rn PRIVATE -O3 -DNDEBUG -pthread)
# target_compile_options(bark-rn PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden)
# target_compile_options(bark-rn PRIVATE -ffunction-sections -fdata-sections)

target_link_options(bark-rn PRIVATE -Wl,--gc-sections)
target_link_options(bark-rn PRIVATE -Wl,--exclude-libs,ALL)
target_link_options(bark-rn PRIVATE -flto)

# Specifies a path to native header files.
include_directories(
../cpp
)

target_link_libraries(bark-rn PUBLIC m)
target_link_libraries(bark-rn PUBLIC log)
26 changes: 15 additions & 11 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <jni.h>
#include <thread>
#include <tuple>
#include <android/log.h>

template <typename T>
T get_map_value(JNIEnv *env, jobject params, const char *key) {
Expand Down Expand Up @@ -104,7 +105,8 @@ extern "C" JNIEXPORT jlong JNICALL Java_com_barkrn_BarkContext_nativeInitContext
if (has_map_key(env, jParams, "seed")) {
seed = get_map_value<jint>(env, jParams, "seed");
}
auto model_path = env->GetStringUTFChars(jPath, nullptr);
const char *model_path = env->GetStringUTFChars(jPath, nullptr);
__android_log_print(ANDROID_LOG_INFO, "BarkRN", "Loading model from %s", model_path);
bark_context *context = bark_load_model(model_path, params, seed);
env->ReleaseStringUTFChars(jPath, model_path);
return reinterpret_cast<jlong>(context);
Expand All @@ -116,23 +118,25 @@ extern "C" JNIEXPORT jobject JNICALL Java_com_barkrn_BarkContext_nativeGenerate(
auto context = reinterpret_cast<bark_context *>(jCtx);
int threads = jThreads;
if (threads < 0) {
threads = std::thread::hardware_concurrency() << 1;
threads = std::thread::hardware_concurrency() >> 1;
} else if (threads == 0) {
threads = 1;
}
auto text = env->GetStringUTFChars(jText, nullptr);
const char *text = env->GetStringUTFChars(jText, nullptr);
__android_log_print(ANDROID_LOG_INFO, "BarkRN", "Generating %s with %d threads", text, threads);
auto success = bark_generate_audio(context, text, threads);
env->ReleaseStringUTFChars(jText, text);
const float *audio_data = bark_get_audio_data(context);
const int audio_samples = bark_get_audio_data_size(context);
if (success) {
auto dest_path = env->GetStringUTFChars(jOutPath, nullptr);
std::vector<float> audio_data_vec(audio_data, audio_data + audio_samples);
barkrn::pcmToWav(audio_data_vec, sample_rate, dest_path);
env->ReleaseStringUTFChars(jOutPath, dest_path);
}
float *audio_data = bark_get_audio_data(context);
int audio_samples = bark_get_audio_data_size(context);
const char *dest_path = env->GetStringUTFChars(jOutPath, nullptr);
__android_log_print(ANDROID_LOG_INFO, "BarkRN", "Generated %d audio samples", audio_samples);
if (success && audio_samples > 0) {
barkrn::pcmToWav(audio_data, audio_samples, sample_rate, dest_path);
}
env->ReleaseStringUTFChars(jOutPath, dest_path);
const auto load_time = bark_get_load_time(context);
const auto eval_time = bark_get_eval_time(context);
__android_log_print(ANDROID_LOG_INFO, "BarkRN", "Load time: %f, Eval time: %f", load_time, eval_time);
auto result_class = env->FindClass("com/barkrn/BarkContext$BarkResult");
jobject result = env->NewObject(
result_class, env->GetMethodID(result_class, "<init>", "(ZII)V"), success,
Expand Down
40 changes: 22 additions & 18 deletions android/src/main/java/com/barkrn/BarkRnModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,33 @@ class BarkRnModule internal constructor(context: ReactApplicationContext) :

@ReactMethod
override fun init_context(model_path: String, params: ReadableMap, promise: Promise) {
try {
val id = next_id
next_id += 1
contexts[id] = BarkContext(model_path, params.toHashMap())
promise.resolve(id)
} catch (e: Exception) {
promise.reject(e)
}
Thread {
try {
val id = next_id
next_id += 1
contexts[id] = BarkContext(model_path, params.toHashMap())
promise.resolve(id)
} catch (e: Exception) {
promise.reject(e)
}
}.start()
}

@ReactMethod
override fun generate(id: Int, text: String, audio_path: String, promise: Promise) {
contexts[id]?.let { context ->
try {
val result = context.generate(text, audio_path)
val resultMap = Arguments.createMap()
resultMap.putBoolean("success", result.success)
resultMap.putInt("load_time", result.load_time)
resultMap.putInt("eval_time", result.eval_time)
promise.resolve(resultMap)
} catch (e: Exception) {
promise.reject(e)
}
Thread {
try {
val result = context.generate(text, audio_path)
val resultMap = Arguments.createMap()
resultMap.putBoolean("success", result.success)
resultMap.putInt("load_time", result.load_time)
resultMap.putInt("eval_time", result.eval_time)
promise.resolve(resultMap)
} catch (e: Exception) {
promise.reject(e)
}
}.start()
} ?: promise.reject("Context not found")
}

Expand Down
19 changes: 17 additions & 2 deletions bark-rn.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ require "json"
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'

base_compiler_flags = "-DWSP_GGML_USE_ACCELERATE -Wno-shorten-64-to-32"
folly_compiler_flags = "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma"

# Use base_optimizer_flags = "" for debug builds
# base_optimizer_flags = ""
base_optimizer_flags = "-O3 -DNDEBUG" +
" -fvisibility=hidden -fvisibility-inlines-hidden" +
" -ffunction-sections -fdata-sections"

Pod::Spec.new do |s|
s.name = "bark-rn"
s.version = package["version"]
Expand All @@ -11,7 +20,7 @@ Pod::Spec.new do |s|
s.license = package["license"]
s.authors = package["author"]

s.platforms = { :ios => "14.0", :tvos => "14.0" }
s.platforms = { :ios => "11.0", :tvos => "11.0" }
s.source = { :git => "https://github.com/mybigday/bark.rn.git", :tag => "#{s.version}" }

s.source_files = "ios/**/*.{h,m,mm}",
Expand All @@ -22,6 +31,12 @@ Pod::Spec.new do |s|
"cpp/bark.{cpp,h}",
"cpp/dr_wav.h",
"cpp/utils.{cpp,h}"

s.compiler_flags = base_compiler_flags
s.pod_target_xcconfig = {
"OTHER_CFLAGS" => base_optimizer_flags,
"OTHER_CPLUSPLUSFLAGS" => base_optimizer_flags
}

# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
Expand All @@ -32,7 +47,7 @@ Pod::Spec.new do |s|

# Don't install the dependencies when we run `pod install` in the old architecture.
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
s.compiler_flags = base_compiler_flags + " " + folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
s.pod_target_xcconfig = {
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
Expand Down
Loading

0 comments on commit d91058b

Please sign in to comment.