From: Takeshi Inoue Date: Mon, 6 Mar 2023 17:15:57 +0000 (+0900) Subject: whisper.android : enable fp16 instrinsics (FP16_VA) which is supported by ARMv8.2... X-Git-Tag: upstream/1.7.4~1552 X-Git-Url: https://git.djapps.eu/?a=commitdiff_plain;h=a3fb6c507f6c30b9ab9e6b04da210ff03b45219a;p=pkg%2Fggml%2Fsources%2Fwhisper.cpp whisper.android : enable fp16 instrinsics (FP16_VA) which is supported by ARMv8.2 or later. (#572) --- diff --git a/examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/LibWhisper.kt b/examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/LibWhisper.kt index 9a9a9631..b0b42003 100644 --- a/examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/LibWhisper.kt +++ b/examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/LibWhisper.kt @@ -74,6 +74,7 @@ private class WhisperLib { init { Log.d(LOG_TAG, "Primary ABI: ${Build.SUPPORTED_ABIS[0]}") var loadVfpv4 = false + var loadV8fp16 = false if (isArmEabiV7a()) { // armeabi-v7a needs runtime detection support val cpuInfo = cpuInfo() @@ -84,11 +85,24 @@ private class WhisperLib { loadVfpv4 = true } } + } else if (isArmEabiV8a()) { + // ARMv8.2a needs runtime detection support + val cpuInfo = cpuInfo() + cpuInfo?.let { + Log.d(LOG_TAG, "CPU info: $cpuInfo") + if (cpuInfo.contains("fphp")) { + Log.d(LOG_TAG, "CPU supports fp16 arithmetic") + loadV8fp16 = true + } + } } if (loadVfpv4) { Log.d(LOG_TAG, "Loading libwhisper_vfpv4.so") System.loadLibrary("whisper_vfpv4") + } else if (loadV8fp16) { + Log.d(LOG_TAG, "Loading libwhisper_v8fp16_va.so") + System.loadLibrary("whisper_v8fp16_va") } else { Log.d(LOG_TAG, "Loading libwhisper.so") System.loadLibrary("whisper") @@ -110,6 +124,10 @@ private fun isArmEabiV7a(): Boolean { return Build.SUPPORTED_ABIS[0].equals("armeabi-v7a") } +private fun isArmEabiV8a(): Boolean { + return Build.SUPPORTED_ABIS[0].equals("arm64-v8a") +} + private fun cpuInfo(): String? { return try { File("/proc/cpuinfo").inputStream().bufferedReader().use { diff --git a/examples/whisper.android/app/src/main/jni/whisper/Android.mk b/examples/whisper.android/app/src/main/jni/whisper/Android.mk index 3e3da8b5..22fc77f7 100644 --- a/examples/whisper.android/app/src/main/jni/whisper/Android.mk +++ b/examples/whisper.android/app/src/main/jni/whisper/Android.mk @@ -12,4 +12,15 @@ ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) # https://android.googlesource.com/platform/ndk/+/master/sources/android/cpufeatures/cpu-features.h LOCAL_CFLAGS += -mfpu=neon-vfpv4 include $(BUILD_SHARED_LIBRARY) -endif \ No newline at end of file +endif + +ifeq ($(TARGET_ARCH_ABI),arm64-v8a) + include $(CLEAR_VARS) + LOCAL_MODULE := libwhisper_v8fp16_va + include $(LOCAL_PATH)/Whisper.mk + # Allow building NEON FMA code. + # https://android.googlesource.com/platform/ndk/+/master/sources/android/cpufeatures/cpu-features.h + LOCAL_CFLAGS += -march=armv8.2-a+fp16 + include $(BUILD_SHARED_LIBRARY) +endif +