Skip to content

Conversation

mishig25
Copy link
Collaborator

@mishig25 mishig25 commented Sep 19, 2025

buildGgufHeader: GGUF header reconstruction with proper alignment

🎯 Why serializeGgufMetadata #1740 isn't enough

serializeGgufMetadata only handles metadata serialization but lacks critical functionality:

  • No tensor info preservation - loses original tensor definitions
  • No alignment calculation - creates invalid memory layout
  • Incomplete headers - generates metadata-only files
  • Framework incompatible - resulting files can't be loaded

🚀 buildGgufHeader: Complete solution

buildGgufHeader is a comprehensive function that uses serializeGgufMetadata internally but adds essential missing pieces:

export async function buildGgufHeader(originalFileBlob, updatedMetadata, options) {
  // 1. ✅ Use serializeGgufMetadata for metadata
  const newHeaderBytes = serializeGgufMetadata(updatedMetadata, {
    littleEndian: options.littleEndian,
    alignment,
  });

  // 2. ✅ Preserve original tensor info (missing in serializeGgufMetadata)
  const originalTensorInfoBlob = originalFileBlob.slice(tensorInfoStart, tensorInfoEnd);

  // 3. ✅ Calculate proper alignment (missing in serializeGgufMetadata)  
  const prePadLenNew = kvEndOffset + tensorInfoSize;
  const GGML_PAD = (x, n) => (x + n - 1) & ~(n - 1);
  const targetTensorDataOffset = GGML_PAD(prePadLenNew, alignment);
  const padLen = targetTensorDataOffset - prePadLenNew;

  // 4. ✅ Reconstruct complete, valid header
  return new Blob([
    newHeaderBytes.slice(0, kvEndOffset),  // Metadata from serializeGgufMetadata
    originalTensorInfoBlob,                // Preserved tensor info
    new Uint8Array(padLen)                 // Correct alignment padding
  ]);
}

📊 Comparison

Function Metadata Tensor Info Alignment Complete Header
serializeGgufMetadata
buildGgufHeader

🎯 Key Innovation

buildGgufHeader = serializeGgufMetadata + tensor preservation + alignment calculation

This creates production-ready GGUF files that maintain framework compatibility while enabling metadata updates.

@mishig25 mishig25 marked this pull request as ready for review September 19, 2025 14:43
@mishig25 mishig25 requested a review from coyotte508 September 19, 2025 14:43
@mishig25 mishig25 merged commit 8491512 into main Sep 19, 2025
5 checks passed
@mishig25 mishig25 deleted the gguf_build_header branch September 19, 2025 14:45
@mishig25
Copy link
Collaborator Author

Merging it to get the moon PR going. Happy to address comments in follow up PR

Copy link
Member

@julien-c julien-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok 🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants