Skip to content

Commit 47e14c0

Browse files
fitzsimngxson
andauthored
whisper : restore big endian support (#2816)
* whisper : fix BYTESWAP whitespace * whisper : make byteswap useable with C++17 * cmake : define WHISPER_BIG_ENDIAN for big-endian targets * ci : fix (again) arm64 build fails * docker : attempt fixing arm64 build on ci * qemu v7.0.0-28 [imported from https://github.com/ggml-org/llama.cpp /commit/818a340ea8be55b3706e1772527cb8738e90a8c7 (#11895)] --------- Co-authored-by: Xuan-Son Nguyen <[email protected]>
1 parent d682e15 commit 47e14c0

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

.github/workflows/docker.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828

2929
- name: Set up QEMU
3030
uses: docker/setup-qemu-action@v3
31+
with:
32+
image: tonistiigi/binfmt:qemu-v7.0.0-28
3133

3234
- name: Set up Docker Buildx
3335
uses: docker/setup-buildx-action@v3

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ set_target_properties(whisper PROPERTIES
9494
target_include_directories(whisper PUBLIC . ../include)
9595
target_compile_features (whisper PUBLIC cxx_std_11) # don't bump
9696

97+
if (CMAKE_CXX_BYTE_ORDER STREQUAL "BIG_ENDIAN")
98+
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DWHISPER_BIG_ENDIAN)
99+
endif()
100+
97101
if (WHISPER_EXTRA_FLAGS)
98102
target_compile_options(whisper PRIVATE ${WHISPER_EXTRA_FLAGS})
99103
endif()

src/whisper.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@
3939
#pragma warning(disable: 4244 4267) // possible loss of data
4040
#endif
4141

42-
#if defined(GGML_BIG_ENDIAN)
43-
#include <bit>
44-
42+
#if defined(WHISPER_BIG_ENDIAN)
4543
template<typename T>
4644
static T byteswap(T value) {
47-
return std::byteswap(value);
48-
}
49-
50-
template<>
51-
float byteswap(float value) {
52-
return std::bit_cast<float>(byteswap(std::bit_cast<std::uint32_t>(value)));
45+
T value_swapped;
46+
char * source = reinterpret_cast<char *>(&value);
47+
char * target = reinterpret_cast<char *>(&value_swapped);
48+
int size = sizeof(T);
49+
for (int i = 0; i < size; i++) {
50+
target[size - 1 - i] = source[i];
51+
}
52+
return value_swapped;
5353
}
5454

5555
template<typename T>
@@ -85,14 +85,14 @@ static void byteswap_tensor(ggml_tensor * tensor) {
8585
}
8686

8787
#define BYTESWAP_VALUE(d) d = byteswap(d)
88-
#define BYTESWAP_FILTERS(f) \
88+
#define BYTESWAP_FILTERS(f) \
8989
do { \
9090
for (auto & datum : f.data) { \
9191
datum = byteswap(datum); \
9292
} \
9393
} while (0)
94-
#define BYTESWAP_TENSOR(t) \
95-
do { \
94+
#define BYTESWAP_TENSOR(t) \
95+
do { \
9696
byteswap_tensor(t); \
9797
} while (0)
9898
#else

0 commit comments

Comments
 (0)