Follows the same header -> KV -> tensor parsing sequence as gguf() huggingface/gguf
| 223 | |
| 224 | // Follows the same header -> KV -> tensor parsing sequence as gguf() huggingface/gguf |
| 225 | static std::optional<gguf_remote_model> gguf_parse_meta(const std::vector<char> & buf) { |
| 226 | gguf_buf_reader r(buf); |
| 227 | |
| 228 | // Header: magic(4) + version(4) + tensor_count(8) + kv_count(8) = 24 bytes minimum |
| 229 | uint32_t magic_raw; |
| 230 | if (!r.read_val(magic_raw)) { |
| 231 | return std::nullopt; |
| 232 | } |
| 233 | if (memcmp(&magic_raw, "GGUF", 4) != 0) { |
| 234 | fprintf(stderr, "gguf_parse_meta: invalid magic\n"); |
| 235 | return std::nullopt; |
| 236 | } |
| 237 | |
| 238 | uint32_t version; |
| 239 | if (!r.read_val(version)) { |
| 240 | return std::nullopt; |
| 241 | } |
| 242 | if (version < 2 || version > 3) { |
| 243 | fprintf(stderr, "gguf_parse_meta: unsupported version %u\n", version); |
| 244 | return std::nullopt; |
| 245 | } |
| 246 | |
| 247 | int64_t tensor_count_raw; |
| 248 | int64_t kv_count_raw; |
| 249 | if (!r.read_val(tensor_count_raw)) { |
| 250 | return std::nullopt; |
| 251 | } |
| 252 | if (!r.read_val(kv_count_raw)) { |
| 253 | return std::nullopt; |
| 254 | } |
| 255 | |
| 256 | uint64_t tensor_count = (uint64_t)tensor_count_raw; |
| 257 | uint64_t kv_count = (uint64_t)kv_count_raw; |
| 258 | |
| 259 | gguf_remote_model model; |
| 260 | |
| 261 | std::string arch_prefix; |
| 262 | |
| 263 | // Parse KV pairs |
| 264 | for (uint64_t i = 0; i < kv_count; i++) { |
| 265 | std::string key; |
| 266 | if (!r.read_str(key)) { |
| 267 | return std::nullopt; |
| 268 | } |
| 269 | |
| 270 | int32_t vtype; |
| 271 | if (!r.read_val(vtype)) { |
| 272 | return std::nullopt; |
| 273 | } |
| 274 | |
| 275 | if (key == "general.architecture" && vtype == GGUF_TYPE_STRING) { |
| 276 | if (!r.read_str(model.architecture)) { |
| 277 | return std::nullopt; |
| 278 | } |
| 279 | arch_prefix = model.architecture + "."; |
| 280 | continue; |
| 281 | } |
| 282 |
no test coverage detected