takeshi-inoue commited on
Commit
6958128
·
unverified ·
1 Parent(s): fa946a3

whisper.android : enable fp16 instrinsics (FP16_VA) which is supported by ARMv8.2 or later. (#572)

Browse files
examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/LibWhisper.kt CHANGED
@@ -74,6 +74,7 @@ private class WhisperLib {
74
  init {
75
  Log.d(LOG_TAG, "Primary ABI: ${Build.SUPPORTED_ABIS[0]}")
76
  var loadVfpv4 = false
 
77
  if (isArmEabiV7a()) {
78
  // armeabi-v7a needs runtime detection support
79
  val cpuInfo = cpuInfo()
@@ -84,11 +85,24 @@ private class WhisperLib {
84
  loadVfpv4 = true
85
  }
86
  }
 
 
 
 
 
 
 
 
 
 
87
  }
88
 
89
  if (loadVfpv4) {
90
  Log.d(LOG_TAG, "Loading libwhisper_vfpv4.so")
91
  System.loadLibrary("whisper_vfpv4")
 
 
 
92
  } else {
93
  Log.d(LOG_TAG, "Loading libwhisper.so")
94
  System.loadLibrary("whisper")
@@ -110,6 +124,10 @@ private fun isArmEabiV7a(): Boolean {
110
  return Build.SUPPORTED_ABIS[0].equals("armeabi-v7a")
111
  }
112
 
 
 
 
 
113
  private fun cpuInfo(): String? {
114
  return try {
115
  File("/proc/cpuinfo").inputStream().bufferedReader().use {
 
74
  init {
75
  Log.d(LOG_TAG, "Primary ABI: ${Build.SUPPORTED_ABIS[0]}")
76
  var loadVfpv4 = false
77
+ var loadV8fp16 = false
78
  if (isArmEabiV7a()) {
79
  // armeabi-v7a needs runtime detection support
80
  val cpuInfo = cpuInfo()
 
85
  loadVfpv4 = true
86
  }
87
  }
88
+ } else if (isArmEabiV8a()) {
89
+ // ARMv8.2a needs runtime detection support
90
+ val cpuInfo = cpuInfo()
91
+ cpuInfo?.let {
92
+ Log.d(LOG_TAG, "CPU info: $cpuInfo")
93
+ if (cpuInfo.contains("fphp")) {
94
+ Log.d(LOG_TAG, "CPU supports fp16 arithmetic")
95
+ loadV8fp16 = true
96
+ }
97
+ }
98
  }
99
 
100
  if (loadVfpv4) {
101
  Log.d(LOG_TAG, "Loading libwhisper_vfpv4.so")
102
  System.loadLibrary("whisper_vfpv4")
103
+ } else if (loadV8fp16) {
104
+ Log.d(LOG_TAG, "Loading libwhisper_v8fp16_va.so")
105
+ System.loadLibrary("whisper_v8fp16_va")
106
  } else {
107
  Log.d(LOG_TAG, "Loading libwhisper.so")
108
  System.loadLibrary("whisper")
 
124
  return Build.SUPPORTED_ABIS[0].equals("armeabi-v7a")
125
  }
126
 
127
+ private fun isArmEabiV8a(): Boolean {
128
+ return Build.SUPPORTED_ABIS[0].equals("arm64-v8a")
129
+ }
130
+
131
  private fun cpuInfo(): String? {
132
  return try {
133
  File("/proc/cpuinfo").inputStream().bufferedReader().use {
examples/whisper.android/app/src/main/jni/whisper/Android.mk CHANGED
@@ -12,4 +12,15 @@ ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
12
  # https://android.googlesource.com/platform/ndk/+/master/sources/android/cpufeatures/cpu-features.h
13
  LOCAL_CFLAGS += -mfpu=neon-vfpv4
14
  include $(BUILD_SHARED_LIBRARY)
15
- endif
 
 
 
 
 
 
 
 
 
 
 
 
12
  # https://android.googlesource.com/platform/ndk/+/master/sources/android/cpufeatures/cpu-features.h
13
  LOCAL_CFLAGS += -mfpu=neon-vfpv4
14
  include $(BUILD_SHARED_LIBRARY)
15
+ endif
16
+
17
+ ifeq ($(TARGET_ARCH_ABI),arm64-v8a)
18
+ include $(CLEAR_VARS)
19
+ LOCAL_MODULE := libwhisper_v8fp16_va
20
+ include $(LOCAL_PATH)/Whisper.mk
21
+ # Allow building NEON FMA code.
22
+ # https://android.googlesource.com/platform/ndk/+/master/sources/android/cpufeatures/cpu-features.h
23
+ LOCAL_CFLAGS += -march=armv8.2-a+fp16
24
+ include $(BUILD_SHARED_LIBRARY)
25
+ endif
26
+