Skip to content

Commit ed94707

Browse files
Make GGUFWriter accept tensors in native endianness instead of little-endian
With this change if no byteswapping is actually needed, 2 excessive byteswaps can be omitted on s390x
1 parent 137c702 commit ed94707

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

convert_hf_to_gguf.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,6 @@ def prepare_tensors(self):
615615
# reverse shape to make it similar to the internal ggml dimension order
616616
shape_str = f"{{{', '.join(str(n) for n in reversed(shape))}}}"
617617

618-
if sys.byteorder == 'big':
619-
# Switch data back to little-endian.
620-
# gguf_writer.add_tensor later switches it back to big endian if needed.
621-
# Don't byteswap inplace since it cannot handle lazy copies
622-
data = data.byteswap(inplace=False)
623-
624618
# n_dims is implicit in the shape
625619
logger.info(f"{f'%-{max_name_len}s' % f'{new_name},'} {old_dtype} --> {data_qtype.name}, shape = {shape_str}")
626620

gguf-py/gguf/gguf_writer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import shutil
66
import struct
7+
import sys
78
import tempfile
89
from dataclasses import dataclass
910
from enum import Enum, auto
@@ -372,7 +373,8 @@ def add_tensor(
372373
self, name: str, tensor: np.ndarray[Any, Any], raw_shape: Sequence[int] | None = None,
373374
raw_dtype: GGMLQuantizationType | None = None,
374375
) -> None:
375-
if self.endianess == GGUFEndian.BIG:
376+
if (self.endianess == GGUFEndian.BIG and sys.byteorder != 'big') or \
377+
(self.endianess == GGUFEndian.LITTLE and sys.byteorder != 'little'):
376378
# Don't byteswap inplace since lazy copies cannot handle it
377379
tensor = tensor.byteswap(inplace=False)
378380
if self.use_temp_file and self.temp_file is None:
@@ -400,7 +402,8 @@ def write_tensor_data(self, tensor: np.ndarray[Any, Any]) -> None:
400402
raise ValueError(f'Expected output file to contain tensor info or weights, got {self.state}')
401403
assert self.fout is not None
402404

403-
if self.endianess == GGUFEndian.BIG:
405+
if (self.endianess == GGUFEndian.BIG and sys.byteorder != 'big') or \
406+
(self.endianess == GGUFEndian.LITTLE and sys.byteorder != 'little'):
404407
# Don't byteswap inplace since lazy copies cannot handle it
405408
tensor = tensor.byteswap(inplace=False)
406409

0 commit comments

Comments
 (0)