|
@@ -371,10 +371,13 @@ class GGUFWriter:
|
|
|
|
|
|
|
|
def add_tensor(
|
|
def add_tensor(
|
|
|
self, name: str, tensor: np.ndarray[Any, Any], raw_shape: Sequence[int] | None = None,
|
|
self, name: str, tensor: np.ndarray[Any, Any], raw_shape: Sequence[int] | None = None,
|
|
|
- raw_dtype: GGMLQuantizationType | None = None,
|
|
|
|
|
|
|
+ raw_dtype: GGMLQuantizationType | None = None, tensor_endianess: GGUFEndian | None = None
|
|
|
) -> None:
|
|
) -> None:
|
|
|
- if (self.endianess == GGUFEndian.BIG and sys.byteorder != 'big') or \
|
|
|
|
|
- (self.endianess == GGUFEndian.LITTLE and sys.byteorder != 'little'):
|
|
|
|
|
|
|
+ # if tensor endianness is not passed, assume it's native to system
|
|
|
|
|
+ if tensor_endianess is None:
|
|
|
|
|
+ tensor_endianess = GGUFEndian.BIG if sys.byteorder == 'big' else GGUFEndian.LITTLE
|
|
|
|
|
+
|
|
|
|
|
+ if tensor_endianess != self.endianess:
|
|
|
# Don't byteswap inplace since lazy copies cannot handle it
|
|
# Don't byteswap inplace since lazy copies cannot handle it
|
|
|
tensor = tensor.byteswap(inplace=False)
|
|
tensor = tensor.byteswap(inplace=False)
|
|
|
if self.use_temp_file and self.temp_file is None:
|
|
if self.use_temp_file and self.temp_file is None:
|
|
@@ -397,13 +400,16 @@ class GGUFWriter:
|
|
|
if pad != 0:
|
|
if pad != 0:
|
|
|
fp.write(bytes([0] * pad))
|
|
fp.write(bytes([0] * pad))
|
|
|
|
|
|
|
|
- def write_tensor_data(self, tensor: np.ndarray[Any, Any]) -> None:
|
|
|
|
|
|
|
+ def write_tensor_data(self, tensor: np.ndarray[Any, Any], tensor_endianess: GGUFEndian | None = None) -> None:
|
|
|
if self.state is not WriterState.TI_DATA and self.state is not WriterState.WEIGHTS:
|
|
if self.state is not WriterState.TI_DATA and self.state is not WriterState.WEIGHTS:
|
|
|
raise ValueError(f'Expected output file to contain tensor info or weights, got {self.state}')
|
|
raise ValueError(f'Expected output file to contain tensor info or weights, got {self.state}')
|
|
|
assert self.fout is not None
|
|
assert self.fout is not None
|
|
|
|
|
|
|
|
- if (self.endianess == GGUFEndian.BIG and sys.byteorder != 'big') or \
|
|
|
|
|
- (self.endianess == GGUFEndian.LITTLE and sys.byteorder != 'little'):
|
|
|
|
|
|
|
+ # if tensor endianness is not passed, assume it's native to system
|
|
|
|
|
+ if tensor_endianess is None:
|
|
|
|
|
+ tensor_endianess = GGUFEndian.BIG if sys.byteorder == 'big' else GGUFEndian.LITTLE
|
|
|
|
|
+
|
|
|
|
|
+ if tensor_endianess != self.endianess:
|
|
|
# Don't byteswap inplace since lazy copies cannot handle it
|
|
# Don't byteswap inplace since lazy copies cannot handle it
|
|
|
tensor = tensor.byteswap(inplace=False)
|
|
tensor = tensor.byteswap(inplace=False)
|
|
|
|
|
|