1010#include < string>
1111#include < vector>
1212
13+ #if defined(__gnu_linux__)
14+ #include < endian.h>
15+ #else
16+ #define le64toh (x ) (x)
17+ #define le32toh (x ) (x)
18+ #define le16toh (x ) (x)
19+ #endif
20+
21+ // endianness conversion
22+ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
23+ #define convert_to_le (x )
24+ #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
25+ #include < type_traits>
26+
27+ template <typename T, std::enable_if_t <sizeof (T) == 1 , int > = 0 >
28+ static inline void convert_to_le (T * /* value*/ )
29+ {
30+ }
31+
32+ template <typename T, std::enable_if_t <sizeof (T) == 2 , int > = 0 >
33+ static inline void convert_to_le (T * value) {
34+ *((uint16_t *)value) = htole16 (*((uint16_t *)value));
35+ }
36+
37+ template <typename T, std::enable_if_t <sizeof (T) == 4 , int > = 0 >
38+ static inline void convert_to_le (T * value) {
39+ *((uint32_t *)value) = htole32 (*((uint32_t *)value));
40+ }
41+
42+ template <typename T, std::enable_if_t <sizeof (T) == 8 , int > = 0 >
43+ static inline void convert_to_le (T * value) {
44+ *((uint64_t *)value) = htole64 (*((uint64_t *)value));
45+ }
46+ #else
47+ #error Unexpected or undefined __BYTE_ORDER__
48+ #endif
49+
1350constexpr int offset_has_kv = 1000 ;
1451constexpr int offset_has_tensors = 2000 ;
1552constexpr int offset_has_data = 3000 ;
@@ -146,7 +183,8 @@ static std::vector<std::pair<enum gguf_type, enum gguf_type>> get_kv_types(std::
146183}
147184
148185template <typename T>
149- static void helper_write (FILE * file, const T & val) {
186+ static void helper_write (FILE * file, T val) {
187+ convert_to_le (&val);
150188 GGML_ASSERT (fwrite (&val, 1 , sizeof (val), file) == sizeof (val));
151189}
152190
@@ -363,7 +401,9 @@ static FILE * get_handcrafted_file(const unsigned int seed, const enum handcraft
363401 helper_write (file, big_dim);
364402 }
365403 } else {
366- helper_write (file, shape.data (), n_dims*sizeof (int64_t ));
404+ for (uint32_t j = 0 ; j < n_dims; ++j) {
405+ helper_write (file, shape[j]);
406+ }
367407 }
368408
369409 {
@@ -533,6 +573,33 @@ static bool handcrafted_check_kv(const gguf_context * gguf_ctx, const unsigned i
533573 continue ;
534574 }
535575
576+ #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
577+ switch (type_arr) {
578+ case GGUF_TYPE_UINT16:
579+ case GGUF_TYPE_INT16:
580+ for (size_t j = 0 ; j < arr_n; ++j) {
581+ convert_to_le ((uint16_t *)(data8 + j * 2 ));
582+ }
583+ break ;
584+
585+ case GGUF_TYPE_UINT32:
586+ case GGUF_TYPE_INT32:
587+ case GGUF_TYPE_FLOAT32:
588+ for (size_t j = 0 ; j < arr_n; ++j) {
589+ convert_to_le ((uint32_t *)(data8 + j * 4 ));
590+ }
591+ break ;
592+
593+ case GGUF_TYPE_UINT64:
594+ case GGUF_TYPE_INT64:
595+ case GGUF_TYPE_FLOAT64:
596+ for (size_t j = 0 ; j < arr_n; ++j) {
597+ convert_to_le ((uint64_t *)(data8 + j * 8 ));
598+ }
599+ break ;
600+ }
601+ #endif
602+
536603 if (!std::equal (data8, data8 + arr_n*type_size, data_gguf)) {
537604 ok = false ;
538605 }
@@ -548,6 +615,27 @@ static bool handcrafted_check_kv(const gguf_context * gguf_ctx, const unsigned i
548615 continue ;
549616 }
550617
618+ #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
619+ switch (type) {
620+ case GGUF_TYPE_UINT16:
621+ case GGUF_TYPE_INT16:
622+ convert_to_le ((uint16_t *)(data8));
623+ break ;
624+
625+ case GGUF_TYPE_UINT32:
626+ case GGUF_TYPE_INT32:
627+ case GGUF_TYPE_FLOAT32:
628+ convert_to_le ((uint32_t *)(data8));
629+ break ;
630+
631+ case GGUF_TYPE_UINT64:
632+ case GGUF_TYPE_INT64:
633+ case GGUF_TYPE_FLOAT64:
634+ convert_to_le ((uint64_t *)(data8));
635+ break ;
636+ }
637+ #endif
638+
551639 if (!std::equal (data8, data8 + gguf_type_size (type), data_gguf)) {
552640 ok = false ;
553641 }
0 commit comments