| 1738 | const uint8_t value = v ? 1u : 0u; |
| 1739 | if (fwrite(&value, sizeof(value), 1, fp) != 1) die("write bool failed"); |
| 1740 | } |
| 1741 | |
| 1742 | static void write_gguf_string(FILE *fp, const char *s) { |
| 1743 | uint64_t n = strlen(s); |
| 1744 | write_u64(fp, n); |
| 1745 | if (n && fwrite(s, 1, (size_t)n, fp) != (size_t)n) die("write string failed"); |
| 1746 | } |
| 1747 | |
| 1748 | static bool is_imatrix_kv_key(const char *key) { |
| 1749 | return str_starts(key, "quantize.imatrix."); |
| 1750 | } |
| 1751 | |
| 1752 | static size_t extra_imatrix_kv_size(const imatrix_store *im) { |
| 1753 | if (!imatrix_enabled(im)) return 0; |
| 1754 | size_t n = 0; |
| 1755 | n += gguf_string_size(DS4_KV_QUANTIZE_IMATRIX_FILE) + 4 + gguf_string_size(im->file); |
| 1756 | n += gguf_string_size(DS4_KV_QUANTIZE_IMATRIX_N_ENTRIES) + 4 + 8; |
| 1757 | if (im->dataset) n += gguf_string_size(DS4_KV_QUANTIZE_IMATRIX_DATASET) + 4 + gguf_string_size(im->dataset); |
| 1758 | if (im->chunks > 0) n += gguf_string_size(DS4_KV_QUANTIZE_IMATRIX_N_CHUNKS) + 4 + 8; |
| 1759 | return n; |
| 1760 | } |
| 1761 | |
| 1762 | static uint64_t extra_imatrix_kv_count(const imatrix_store *im) { |
| 1763 | if (!imatrix_enabled(im)) return 0; |
| 1764 | return 2 + (im->dataset ? 1 : 0) + (im->chunks > 0 ? 1 : 0); |
| 1765 | } |
| 1766 | |
| 1767 | static void write_imatrix_kvs(FILE *fp, const imatrix_store *im) { |
| 1768 | if (!imatrix_enabled(im)) return; |
| 1769 | write_gguf_string(fp, DS4_KV_QUANTIZE_IMATRIX_FILE); |
| 1770 | write_u32(fp, GGUF_TYPE_STRING); |
| 1771 | write_gguf_string(fp, im->file); |
| 1772 | |
| 1773 | write_gguf_string(fp, DS4_KV_QUANTIZE_IMATRIX_N_ENTRIES); |
| 1774 | write_u32(fp, GGUF_TYPE_UINT64); |
| 1775 | write_u64(fp, (uint64_t)im->n_entries); |
| 1776 | |
| 1777 | if (im->dataset) { |
| 1778 | write_gguf_string(fp, DS4_KV_QUANTIZE_IMATRIX_DATASET); |
| 1779 | write_u32(fp, GGUF_TYPE_STRING); |
| 1780 | write_gguf_string(fp, im->dataset); |
| 1781 | } |
| 1782 | if (im->chunks > 0) { |
| 1783 | write_gguf_string(fp, DS4_KV_QUANTIZE_IMATRIX_N_CHUNKS); |
| 1784 | write_u32(fp, GGUF_TYPE_UINT64); |
| 1785 | write_u64(fp, (uint64_t)im->chunks); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | static gguf_file load_gguf_metadata_with_override(const char *path, |
| 1790 | const hf_model_metadata *metadata) { |
| 1791 | gguf_file g = {0}; |
| 1792 | g.path = xstrdup(path); |
| 1793 | FILE *fp = fopen(path, "rb"); |
| 1794 | if (!fp) die_errno("open GGUF", path); |
| 1795 | char magic[4]; |
| 1796 | if (fread(magic, 1, sizeof(magic), fp) != sizeof(magic) || memcmp(magic, "GGUF", 4) != 0) { |
| 1797 | die("bad GGUF template"); |
no test coverage detected