CISCai commited on
Commit
37e11f9
1 Parent(s): 73fd84b

Ensure tensor alignment is preserved by adding dummy padding metadata if necessary

Browse files
Files changed (2) hide show
  1. _hf_gguf.py +31 -0
  2. app.py +1 -0
_hf_gguf.py CHANGED
@@ -187,7 +187,9 @@ class HuggingGGUFstream:
187
  self.header = {}
188
  self.metadata = {}
189
  self.endian = '<'
 
190
  self.metaend = 0
 
191
  self.filesize = fp.details.get('size')
192
 
193
  if (data := self.fp.read(4)) != b'GGUF':
@@ -356,7 +358,36 @@ class HuggingGGUFstream:
356
 
357
  yield key.value, val
358
 
 
 
 
359
  self.metaend = self.fp.loc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360
 
361
  def add_metadata(
362
  self,
 
187
  self.header = {}
188
  self.metadata = {}
189
  self.endian = '<'
190
+ self.alignment = 32
191
  self.metaend = 0
192
+ self.offset = 0
193
  self.filesize = fp.details.get('size')
194
 
195
  if (data := self.fp.read(4)) != b'GGUF':
 
358
 
359
  yield key.value, val
360
 
361
+ if (alignment := self.metadata.get('general.alignment')) is not None:
362
+ self.alignment = alignment.value
363
+
364
  self.metaend = self.fp.loc
365
+ self.offset = self.metaend % self.alignment
366
+
367
+ def adjust_padding(
368
+ self,
369
+ ) -> None:
370
+ if self.header['tensors'].value == 0:
371
+ return
372
+
373
+ dummy_key = 'dummy.padding'
374
+ cur_metaend = 0
375
+
376
+ for data in self.header.values():
377
+ cur_metaend += len(data.data)
378
+
379
+ for k, v in self.metadata.items():
380
+ if k != dummy_key:
381
+ cur_metaend += len(v.data)
382
+
383
+ if (cur_metaend % self.alignment) != self.offset:
384
+ dummy_len = 8 + len(dummy_key) + 4 + 4 + 8
385
+ dummy_offset = (cur_metaend + dummy_len) % self.alignment
386
+ dummy_padding = (32 + (self.offset - dummy_offset)) % self.alignment
387
+
388
+ self.add_metadata(dummy_key, GGUFValueType.UINT8, [0] * dummy_padding)
389
+ else:
390
+ self.remove_metadata(dummy_key)
391
 
392
  def add_metadata(
393
  self,
app.py CHANGED
@@ -1279,6 +1279,7 @@ def stream_repo_file(
1279
  k[2] = []
1280
 
1281
  gguf.add_metadata(*k)
 
1282
 
1283
  yield gguf.filesize
1284
 
 
1279
  k[2] = []
1280
 
1281
  gguf.add_metadata(*k)
1282
+ gguf.adjust_padding()
1283
 
1284
  yield gguf.filesize
1285