Skip to content
Merged
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
4 changes: 1 addition & 3 deletions packages/qvac-lib-infer-nmtcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ endif()

option(BUILD_TESTING "Build tests" OFF)
option(USE_BERGAMOT "Enable Bergamot backend support" ON)
# Disabled by default until the Adreno 830 q4_0 transpose assertion
# (GGML_ASSERT(M % 4 == 0) in ggml-opencl.cpp) is fixed upstream —
# Bergamot/IndicTrans tensors trip it on NMT vocab dimensions. See QVAC-17790.
# Off by default: Adreno 830 ggml-opencl M%4 assertion (QVAC-17790)
option(USE_OPENCL "Prefer/allow OpenCL backend for GPU inference" OFF)

if(BUILD_TESTING)
Expand Down
42 changes: 31 additions & 11 deletions packages/qvac-lib-infer-nmtcpp/addon/src/addon/AddonJs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace {
using namespace qvac_lib_inference_addon_cpp;
static std::unordered_map<
std::string, std::variant<double, int64_t, std::string>>
getConfigMap(
getConfigMap( // NOLINT(readability-static-definition-in-anonymous-namespace)
js_env_t* env, js::Object configurationParams, const char* propertyName) {
auto configOpt =
configurationParams.getOptionalProperty<js::Object>(env, propertyName);
Expand All @@ -31,7 +31,7 @@ getConfigMap(
}

auto config = configOpt.value();
js_value_t* configKeys;
js_value_t* configKeys; // NOLINT(cppcoreguidelines-init-variables)
JS(js_get_property_names(env, config, &configKeys));

js::Array configKeysArray(env, configKeys);
Expand All @@ -40,19 +40,21 @@ getConfigMap(
bool hasPivotModel = false;
while (configKeysSz > 0) {
configKeysSz--;
js_value_t* key;
js_value_t* key; // NOLINT(cppcoreguidelines-init-variables)
JS(js_get_element(env, configKeys, configKeysSz, &key));
auto value = config.getProperty(env, key);
auto value = // NOLINT(readability-qualified-auto)
config.getProperty(env, key);

std::string keyString = js::String::fromValue(key).as<std::string>(env);
std::string keyString = // NOLINT(hicpp-use-auto,modernize-use-auto)
js::String::fromValue(key).as<std::string>(env);

std::transform(
std::transform( // NOLINT(modernize-use-ranges)
keyString.begin(),
keyString.end(),
keyString.begin(),
[](unsigned char c) { return std::tolower(c); });
[](unsigned char chr) { return std::tolower(chr); });
if (keyString == "pivotmodel") {
hasPivotModel = true;
hasPivotModel = true; // NOLINT(clang-analyzer-deadcode.DeadStores)
continue;
}
if (js::is<js::Boolean>(env, value)) {
Expand Down Expand Up @@ -175,6 +177,24 @@ getActiveBackendName(js_env_t* env, js_callback_info_t* info) try {
}
JSCATCH

inline js_value_t*
getActiveBackendDescription(js_env_t* env, js_callback_info_t* info) try {
using namespace qvac_lib_inference_addon_cpp;

JsArgsParser args(env, info);
AddonJs& instance = JsInterface::getInstance(env, args.get(0, "instance"));

auto& model = instance.addonCpp->model.get();
auto* translationModel =
dynamic_cast<qvac_lib_inference_addon_nmt::TranslationModel*>(&model);
if (translationModel != nullptr) {
return js::String::create(
env, translationModel->getActiveBackendDescription().c_str());
}
return js::String::create(env, "");
}
JSCATCH

inline js_value_t* runJob(js_env_t* env, js_callback_info_t* info) try {
using namespace qvac_lib_inference_addon_cpp;

Expand All @@ -192,12 +212,12 @@ inline js_value_t* runJob(js_env_t* env, js_callback_info_t* info) try {
std::vector<std::string> inputSequence;
inputSequence.reserve(vectorOfJsValues.size());

std::transform(
std::transform( // NOLINT(modernize-use-ranges)
vectorOfJsValues.begin(),
vectorOfJsValues.end(),
std::back_inserter(inputSequence),
[&env](js_value_t* const string_value) {
return js::String(env, string_value).as<std::string>(env);
[&env](js_value_t* const stringValue) {
return js::String(env, stringValue).as<std::string>(env);
});

anyInput = inputSequence;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include "../addon/AddonJs.hpp"

js_value_t* qvac_lib_infer_nmtcpp_exports(
js_env_t* env,
js_value_t* exports) { // NOLINT(readability-identifier-naming)
js_value_t*
qvac_lib_infer_nmtcpp_exports( // NOLINT(readability-identifier-naming)
js_env_t* env, js_value_t* exports) {

// NOLINTBEGIN(cppcoreguidelines-macro-usage)
#define V(name, fn) \
Expand All @@ -21,6 +21,8 @@ js_value_t* qvac_lib_infer_nmtcpp_exports(
V("createInstance", qvac_lib_inference_addon_nmt::createInstance)
V("runJob", qvac_lib_inference_addon_nmt::runJob)
V("getActiveBackendName", qvac_lib_inference_addon_nmt::getActiveBackendName)
V("getActiveBackendDescription",
qvac_lib_inference_addon_nmt::getActiveBackendDescription)

V("loadWeights", qvac_lib_inference_addon_cpp::JsInterface::loadWeights)
V("activate", qvac_lib_inference_addon_cpp::JsInterface::activate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ggml-backend.h>
#include <ggml.h>

#include "nmt_utils.hpp"
#include "qvac-lib-inference-addon-cpp/Logger.hpp"

#ifdef __ANDROID__
Expand All @@ -22,26 +23,19 @@ std::string NmtLazyInitializeBackend::g_recordedBackendsDir;
std::string NmtLazyInitializeBackend::g_recordedOpenclCacheDir;
std::string NmtLazyInitializeBackend::g_recordedOpenclCacheDirInput;
int NmtLazyInitializeBackend::g_refCount = 0;
std::atomic<bool> NmtLazyInitializeBackend::g_backendsLoaded{false};

// Forward ggml's internal log stream to QLOG so diagnostic lines
// (Adreno detection, CL_CHECK errors, OpenCL driver info, etc.) reach
// logcat on Android instead of silently going to stderr. Mirrors what
// llama_log_set does in the llamacpp-llm addon. See QVAC-17790.
namespace {

std::string sanitizePrintableAscii(const std::string& input) {
std::string out;
out.reserve(input.size());
for (char raw : input) {
unsigned char c = static_cast<unsigned char>(raw);
out.push_back((c >= 0x20 && c < 0x7F) ? static_cast<char>(c) : '?');
}
return out;
}

void nmtGgmlLogCallback(
enum ggml_log_level level, const char* text, void* /*user_data*/) {
if (text == nullptr || text[0] == '\0') {
if (text == nullptr ||
text[0] == // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
'\0') {
return;
}

Expand All @@ -66,7 +60,9 @@ void nmtGgmlLogCallback(

// Compute the trimmed length without heap allocation.
size_t len = std::strlen(text);
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
while (len > 0 && (text[len - 1] == '\n' || text[len - 1] == '\r')) {
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
--len;
}
if (len == 0) {
Expand All @@ -85,7 +81,9 @@ void nmtGgmlLogCallback(
#endif

std::string message;
message.reserve(7 + len);
message.reserve(
7 + // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
len);
message.append("[ggml] ");
message.append(text, len);
QLOG(priority, message);
Expand Down Expand Up @@ -193,8 +191,11 @@ bool NmtLazyInitializeBackend::initializeAndRef(
return didInit;
}

// NOLINTBEGIN(readability-function-cognitive-complexity,bugprone-easily-swappable-parameters)
bool NmtLazyInitializeBackend::initializeLocked(
const std::string& backendsDir, const std::string& openclCacheDir) {
const std::string& backendsDir,
const std::string& openclCacheDir [[maybe_unused]]) {
// NOLINTEND(readability-function-cognitive-complexity,bugprone-easily-swappable-parameters)
if (g_initialized) {
if (!backendsDir.empty() && !g_recordedBackendsDir.empty() &&
backendsDir != g_recordedBackendsDir) {
Expand Down Expand Up @@ -283,76 +284,84 @@ bool NmtLazyInitializeBackend::initializeLocked(
}
#endif

if (!backendsDir.empty()) {
std::filesystem::path requested(backendsDir);
bool validBackendsDir = requested.is_absolute();
if (validBackendsDir) {
for (const auto& seg : requested) {
if (seg == "..") {
validBackendsDir = false;
break;
if (!g_backendsLoaded) {
if (!backendsDir.empty()) {
std::filesystem::path requested(backendsDir);
bool validBackendsDir = requested.is_absolute();
if (validBackendsDir) {
for (const auto& seg : requested) {
if (seg == "..") {
validBackendsDir = false;
break;
}
}
}
}
if (!validBackendsDir) {
QLOG(
Priority::WARNING,
"Rejecting suspicious backendsDir (must be absolute and free of "
"'..' segments): " +
sanitizePrintableAscii(backendsDir) +
" — falling back to default backend loading");
ggml_backend_load_all();
} else {
std::error_code ec;
std::filesystem::path backendsDirPath =
std::filesystem::canonical(requested, ec);
if (ec) {
if (!validBackendsDir) {
QLOG(
Priority::WARNING,
"backendsDir canonical() failed (" + ec.message() +
"): " + sanitizePrintableAscii(backendsDir) +
"Rejecting suspicious backendsDir (must be absolute and free of "
"'..' segments): " +
sanitizePrintableAscii(backendsDir) +
" — falling back to default backend loading");
ggml_backend_load_all();
} else {
auto resolvedStr = backendsDirPath.string();
#ifdef __ANDROID__
if (resolvedStr.rfind("/data/", 0) != 0) {
std::error_code errCode;
std::filesystem::path backendsDirPath =
std::filesystem::canonical(requested, errCode);
if (errCode) {
QLOG(
Priority::WARNING,
"Rejecting backendsDir — resolved path outside /data/ prefix: " +
sanitizePrintableAscii(resolvedStr) +
"backendsDir canonical() failed (" + errCode.message() +
"): " + sanitizePrintableAscii(backendsDir) +
" — falling back to default backend loading");
ggml_backend_load_all();
} else {
#endif
#ifdef BACKENDS_SUBDIR
std::filesystem::path subdirPath(BACKENDS_SUBDIR);
backendsDirPath = backendsDirPath / subdirPath;
backendsDirPath = std::filesystem::canonical(backendsDirPath, ec);
if (ec) {
auto
resolvedStr = // NOLINT(bugprone-unused-local-non-trivial-variable)
backendsDirPath.string();
#ifdef __ANDROID__
if (resolvedStr.rfind("/data/", 0) != 0) {
QLOG(
Priority::WARNING,
"backendsDir+subdir canonical() failed (" + ec.message() +
") — falling back to default backend loading");
"Rejecting backendsDir — resolved path outside /data/ "
"prefix: " +
sanitizePrintableAscii(resolvedStr) +
" — falling back to default backend loading");
ggml_backend_load_all();
} else {
#endif
QLOG(
Priority::INFO,
"Loading backends from directory: " +
sanitizePrintableAscii(backendsDirPath.string()));
ggml_backend_load_all_from_path(backendsDirPath.string().c_str());
#ifdef BACKENDS_SUBDIR
}
std::filesystem::path subdirPath(BACKENDS_SUBDIR);
backendsDirPath = backendsDirPath / subdirPath;
backendsDirPath =
std::filesystem::canonical(backendsDirPath, errCode);
if (errCode) {
QLOG(
Priority::WARNING,
"backendsDir+subdir canonical() failed (" +
errCode.message() +
") — falling back to default backend loading");
ggml_backend_load_all();
} else {
#endif
QLOG(
Priority::INFO,
"Loading backends from directory: " +
sanitizePrintableAscii(backendsDirPath.string()));
ggml_backend_load_all_from_path(backendsDirPath.string().c_str());
#ifdef BACKENDS_SUBDIR
}
#endif
#ifdef __ANDROID__
}
}
#endif
}
}
} else {
QLOG(Priority::DEBUG, "Loading backends using default path");
ggml_backend_load_all();
}
} else {
QLOG(Priority::DEBUG, "Loading backends using default path");
ggml_backend_load_all();
g_backendsLoaded = true;
}
#ifdef __ANDROID__
// Must run after backend loading (the backend .sos are only mapped into
Expand All @@ -379,6 +388,7 @@ void NmtLazyInitializeBackend::decrementRefCount() {
Priority::DEBUG,
"Resetting backend state (reference count reached zero)");
g_initialized = false;
g_backendsLoaded = false;
g_recordedBackendsDir.clear();
#ifdef __ANDROID__
// Clear the process-global GGML_OPENCL_CACHE_DIR set during
Expand All @@ -403,7 +413,7 @@ NmtBackendsHandle::NmtBackendsHandle(
NmtLazyInitializeBackend::initializeAndRef(backendsDir, openclCacheDir);
}

NmtBackendsHandle::~NmtBackendsHandle() {
NmtBackendsHandle::~NmtBackendsHandle() { // NOLINT(bugprone-exception-escape)
if (ownsHandle_) {
NmtLazyInitializeBackend::decrementRefCount();
}
Expand All @@ -415,7 +425,8 @@ NmtBackendsHandle::NmtBackendsHandle(NmtBackendsHandle&& other) noexcept
}

NmtBackendsHandle&
NmtBackendsHandle::operator=(NmtBackendsHandle&& other) noexcept {
NmtBackendsHandle::operator=( // NOLINT(bugprone-exception-escape)
NmtBackendsHandle&& other) noexcept {
if (this != &other) {
if (ownsHandle_) {
NmtLazyInitializeBackend::decrementRefCount();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <atomic>
#include <mutex>
#include <string>

Expand Down Expand Up @@ -62,6 +63,7 @@ class NmtLazyInitializeBackend {
static std::string g_recordedOpenclCacheDir;
static std::string g_recordedOpenclCacheDirInput;
static int g_refCount;
static std::atomic<bool> g_backendsLoaded;
};

/**
Expand Down Expand Up @@ -95,8 +97,8 @@ class NmtBackendsHandle {
NmtBackendsHandle& operator=(const NmtBackendsHandle&) = delete;

// Movable
NmtBackendsHandle(NmtBackendsHandle&&) noexcept;
NmtBackendsHandle& operator=(NmtBackendsHandle&&) noexcept;
NmtBackendsHandle(NmtBackendsHandle&& other) noexcept;
NmtBackendsHandle& operator=(NmtBackendsHandle&& other) noexcept;

private:
bool ownsHandle_;
Expand Down
Loading
Loading