Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GGUF] Load split models without merging shards (#38818)

Maxim Vafin54d8bd3c2c09
diff --git a/src/frontends/gguf/README.md b/src/frontends/gguf/README.md
index d98f5990352..470def01747 100644
--- a/src/frontends/gguf/README.md
+++ b/src/frontends/gguf/README.md
@@ -23,6 +23,10 @@ frontend = FrontEndManager().load_by_framework("gguf")
model = frontend.convert(frontend.load("model.gguf"))
```
+For split GGUF models, pass any standard shard filename to discover the remaining files in
+the same directory, or supply a complete list of paths. No merged GGUF copy is created.
+See [split-file loading](docs/runtime.md#split-gguf-files) for Python and C++ usage and validation.
+
The result is a stateless graph with llama.cpp-style inputs and explicit cache inputs/outputs.
In C++, register [`GenAIExtension`](include/openvino/frontend/gguf/extension/genai.hpp) to obtain a stateful model with the
OpenVINO GenAI interface; this extension is not available from Python. Tokenizer metadata is attached
@@ -44,7 +48,7 @@ Projector files convert to encoder models; see [multimodal conversion](docs/mmpr
## Guides
-* [Run converted models: state, GenAI, tokenizer, internal operations](docs/runtime.md)
+* [Load split files and run converted models: state, GenAI, tokenizer, internal operations](docs/runtime.md)
* [Supported models](docs/supported_models.md) and [multimodal projectors](docs/mmproj.md)
* [Add or port an architecture](docs/architectures.md)
* [Register extensions and build plugins](docs/extensions.md), with [runnable examples](examples/architecture_extension/README.md)
diff --git a/src/frontends/gguf/docs/runtime.md b/src/frontends/gguf/docs/runtime.md
index 081671ecc0a..2a43a17610e 100644
--- a/src/frontends/gguf/docs/runtime.md
+++ b/src/frontends/gguf/docs/runtime.md
@@ -4,6 +4,47 @@
turns a language model into a stateful model with the OpenVINO GenAI interface. Multimodal projector
files use their own [encoder contract](mmproj.md#graph-boundary).
+## Split GGUF files
+
+The native frontend loads split models directly without writing a merged GGUF. Pass any shard
+with the standard `model-00001-of-00003.gguf` filename; the loader reads `split.count` and
+`split.no` and opens the other expected shards in the same directory. All shards of the
+chosen quantization must be available locally.
+
+```python
+from openvino.frontend import FrontEndManager
+
+frontend = FrontEndManager().load_by_framework("gguf")
+model = frontend.convert(frontend.load(
+ "Q4_K_M/Qwen3.5-122B-A10B-Q4_K_M-00001-of-00003.gguf"
+))
+```
+
+For renamed shards or files in different directories, supply a complete list of string paths.
+List order does not matter; the loader uses `split.no` to select shard zero's model metadata.
+
+```python
+model = frontend.convert(frontend.load([
+ "/models/part-a.gguf", "/other-disk/part-b.gguf", "/models/part-c.gguf"
+]))
+```
+
+C++ accepts a `std::vector<std::string>` or `std::vector<std::filesystem::path>`, and also
+separate path arguments to `load()`:
+
+```cpp
+auto input = frontend->load(std::vector<std::string>{
+ "/models/part-a.gguf", "/other-disk/part-b.gguf", "/models/part-c.gguf"});
+auto model = frontend->convert(input);
+```
+
+An explicit list must contain every shard; the loader does not fill in omitted entries.
+Inputs must belong to one split model. Missing shards,
+duplicate shard indices or tensor names, conflicting metadata and inconsistent total tensor
+counts produce load errors. Each shard keeps its own mapping; quantized weights use the same
+repacking as single-file inputs. This avoids a merged disk copy but does not reduce conversion
+or compilation memory requirements or extend architecture and quantization coverage.
+
## Stateless contract
The default decoder graph mirrors the llama.cpp backend interface:
diff --git a/src/frontends/gguf/docs/testing.md b/src/frontends/gguf/docs/testing.md
index b60464bdcba..3143866a8fa 100644
--- a/src/frontends/gguf/docs/testing.md
+++ b/src/frontends/gguf/docs/testing.md
@@ -29,12 +29,19 @@ Multi-configuration generators can add a configuration subdirectory.
| mmproj | `GGUFMMProj`, `GGUFMMProjAccuracy`, `GGUFMMProjDynamicAccuracy`; relevant real-checkpoint tests |
| Extension API or packaging | Builder/extension tests plus the separate architecture-library binary and actual shipped plugin |
| Weight conversion | Weight/dequantization tests and checkpoint comparisons with recorded precision settings |
+| GGUF parsing or shard discovery | `GGUFParser`, `GGUFShards`, then the full frontend binary |
Use `--gtest_list_tests` for exact parameterized names; filter while iterating, for example
`--gtest_filter='GGUFOps.Scale*'`. A narrowed filter skips the operation-coverage gate. That gate
runs in environment teardown, so inspect the process exit status even if no individual test failed.
Shared changes require the affected architecture/projector numerical and structural regressions.
+`GGUFShards` covers discovery from either shard, unordered explicit lists, renamed files in
+different directories, metadata-only first shards, mapping lifetime, invalid or incomplete
+shard sets, repeated scalar/array metadata, and conversion equivalence with a single-file model,
+including Unicode filenames. Its generated fixtures use
+the standard `split.no` (u16), `split.count` (u16) and `split.tensors.count` (i32) metadata.
+
## Fixtures and oracles
Small NPZ/NPY numerical references are committed and work offline. Missing numerical references
diff --git a/src/frontends/gguf/src/builder/gguf_builder.cpp b/src/frontends/gguf/src/builder/gguf_builder.cpp
index ef28f9a888f..08ac115a677 100644
--- a/src/frontends/gguf/src/builder/gguf_builder.cpp
+++ b/src/frontends/gguf/src/builder/gguf_builder.cpp
@@ -48,10 +48,8 @@ ov::AnyMap extract_tokenizer_config(const std::unordered_map<std::string, GGUFMe
return cfg;
}
-} // namespace
-
-GraphBuilder load_gguf_builder(const std::string& file, const ArchRegistry& registry) {
- auto data = std::make_shared<decltype(get_gguf_data(file))>(get_gguf_data(file));
+GraphBuilder make_gguf_builder(GGUFLoad loaded, const ArchRegistry& registry) {
+ auto data = std::make_shared<GGUFLoad>(std::move(loaded));
auto& [metadata, weights, qtypes, mmap] = *data;
const detail::MetadataStore meta_store{metadata};
@@ -94,4 +92,14 @@ GraphBuilder load_gguf_builder(const std::string& file, const ArchRegistry& regi
};
}
+} // namespace
+
+GraphBuilder load_gguf_builder(const std::filesystem::path& file, const ArchRegistry& registry) {
+ return make_gguf_builder(get_gguf_data(file), registry);
+}
+
+GraphBuilder load_gguf_builder(const std::vector<std::filesystem::path>& files, const ArchRegistry& registry) {
+ return make_gguf_builder(get_gguf_data(files), registry);
+}
+
} // namespace ov::frontend::gguf
diff --git a/src/frontends/gguf/src/builder/gguf_builder.hpp b/src/frontends/gguf/src/builder/gguf_builder.hpp
index 8703e769e36..2cdeafc938d 100644
--- a/src/frontends/gguf/src/builder/gguf_builder.hpp
+++ b/src/frontends/gguf/src/builder/gguf_builder.hpp
@@ -4,9 +4,11 @@
#pragma once
+#include <filesystem>
#include <functional>
#include <memory>
#include <string>
+#include <vector>
#include "node_context.hpp"
@@ -17,6 +19,7 @@ class ArchRegistry;
using GraphBuilder = std::function<std::shared_ptr<GgufGraph>(const std::unordered_map<std::string, CreatorFunction>&)>;
// Parse the file and select a definition; invoke the builder with the converters active at conversion.
-GraphBuilder load_gguf_builder(const std::string& file, const ArchRegistry& registry);
+GraphBuilder load_gguf_builder(const std::filesystem::path& file, const ArchRegistry& registry);
+GraphBuilder load_gguf_builder(const std::vector<std::filesystem::path>& files, const ArchRegistry& registry);
} // namespace ov::frontend::gguf
diff --git a/src/frontends/gguf/src/frontend.cpp b/src/frontends/gguf/src/frontend.cpp
index ff6f99aa134..d45076a5d15 100644
--- a/src/frontends/gguf/src/frontend.cpp
+++ b/src/frontends/gguf/src/frontend.cpp
@@ -4,6 +4,7 @@
#include "openvino/frontend/gguf/frontend.hpp"
+#include <algorithm>
#include <filesystem>
#include <fstream>
@@ -22,6 +23,7 @@
#include "openvino/frontend/gguf/extension/genai.hpp"
#include "openvino/frontend/gguf/make_stateful.hpp"
#include "openvino/frontend/manager.hpp"
+#include "openvino/util/file_util.hpp"
#include "translate_session.hpp"
namespace ov::frontend::gguf {
@@ -76,6 +78,24 @@ bool has_gguf_magic(const std::filesystem::path& path) {
return f.gcount() == 4 && magic[0] == 'G' && magic[1] == 'G' && magic[2] == 'U' && magic[3] == 'F';
}
+std::optional<std::vector<std::filesystem::path>> model_paths(const std::vector<ov::Any>& variants) {
+ const auto size = variants.size() - (variants.back().is<bool>() ? 1 : 0);
+ if (size == 1) {
+ if (auto paths = ov::frontend::get_path_vec_from_any(variants.front())) {
+ return paths;
+ }
+ }
+ std::vector<std::filesystem::path> paths;
+ for (size_t i = 0; i < size; ++i) {
+ auto path = ov::frontend::get_path_from_any(variants[i]);
+ if (!path) {
+ return std::nullopt;
+ }
+ paths.push_back(std::move(*path));
+ }
+ return paths;
+}
+
} // namespace
FrontEnd::FrontEnd() : m_impl(std::make_shared<Impl>()) {}
@@ -151,9 +171,10 @@ bool FrontEnd::supported_impl(const std::vector<ov::Any>& variants) const {
if (variants[0].is<std::shared_ptr<GgufDecoder>>()) {
return true;
}
- if (auto path = ov::frontend::get_path_from_any(variants[0])) {
- std::filesystem::path model_path = std::move(*path);
- return model_path.extension() == ".gguf" && has_gguf_magic(model_path);
+ if (auto paths = model_paths(variants)) {
+ return !paths->empty() && std::all_of(paths->begin(), paths->end(), [](const auto& path) {
+ return path.extension() == ".gguf" && has_gguf_magic(path);
+ });
}
return false;
}
@@ -169,19 +190,23 @@ InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& variants) const
return std::make_shared<InputModel>(decoder);
}
- // Path 2: a .gguf file path -> native builder -> GgufBuilderDecoder.
- if (auto path = ov::frontend::get_path_from_any(variants[0])) {
- std::filesystem::path model_path = std::move(*path);
- FRONT_END_GENERAL_CHECK(model_path.extension() == ".gguf",
- "GGUF Frontend file loading expects a .gguf file, got: ",
- model_path.string());
- return std::make_shared<InputModel>(load_gguf_builder(model_path.string(), m_impl->arch_registry),
- m_extensions);
+ // Path 2: GGUF file paths -> native builder -> GgufBuilderDecoder.
+ if (auto paths = model_paths(variants)) {
+ FRONT_END_GENERAL_CHECK(!paths->empty(), "GGUF Frontend file list is empty");
+ for (const auto& path : *paths) {
+ FRONT_END_GENERAL_CHECK(path.extension() == ".gguf",
+ "GGUF Frontend file loading expects a .gguf file, got: ",
+ ov::util::path_to_string(path));
+ }
+ const auto discover = paths->size() == 1 && ov::frontend::get_path_from_any(variants.front()).has_value();
+ const auto builder = discover ? load_gguf_builder(paths->front(), m_impl->arch_registry)
+ : load_gguf_builder(*paths, m_impl->arch_registry);
+ return std::make_shared<InputModel>(builder, m_extensions);
}
FRONT_END_GENERAL_CHECK(false,
"GGUF Frontend doesn't support the provided model representation. Provide a GgufDecoder "
- "or a path to a .gguf file.");
+ "or paths to GGUF files.");
}
} // namespace ov::frontend::gguf
diff --git a/src/frontends/gguf/src/quant/gguf.cpp b/src/frontends/gguf/src/quant/gguf.cpp
index 82131069f16..68df8785b9f 100644
--- a/src/frontends/gguf/src/quant/gguf.cpp
+++ b/src/frontends/gguf/src/quant/gguf.cpp
@@ -14,9 +14,14 @@
#include <cmath>
#include <cstdint>
#include <cstring>
+#include <filesystem>
+#include <iomanip>
#include <limits>
#include <optional>
+#include <sstream>
#include <string_view>
+#include <type_traits>
+#include <unordered_set>
#include "openvino/core/except.hpp"
#include "openvino/core/memory_util.hpp"
@@ -25,8 +30,10 @@
#include "openvino/runtime/aligned_buffer.hpp"
#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/util/common_util.hpp"
+#include "openvino/util/file_util.hpp"
#include "openvino/util/math_util.hpp"
#include "openvino/util/mmap_object.hpp"
+#include "openvino/util/variant_visitor.hpp"
#include "weights.hpp"
namespace ov::frontend::gguf {
@@ -431,13 +438,29 @@ ov::Shape get_shape(const GgufTensor& tensor) {
return shape;
}
-GGUFLoad get_gguf_data(const std::string& file) {
+namespace {
+
+struct TensorInfo {
+ std::string name;
+ uint32_t type = 0;
+ uint32_t ndim = 0;
+ uint64_t dim[4] = {1, 1, 1, 1};
+ uint64_t offset = 0;
+};
+
+struct GgufFile {
+ std::unordered_map<std::string, GGUFMetaData> metadata;
+ std::vector<TensorInfo> infos;
+ std::shared_ptr<ov::MappedMemory> mapped;
+ uint64_t alignment;
+ uint64_t data_off;
+};
+
+GgufFile read_gguf_file(const std::filesystem::path& file) {
std::unordered_map<std::string, GGUFMetaData> metadata;
- std::unordered_map<std::string, ov::Tensor> arrays;
- std::unordered_map<std::string, GgufTensorType> qtype;
auto mapped = ov::load_mmap_object(file);
- OPENVINO_ASSERT(mapped && mapped->data(), "[load_gguf] failed to mmap '", file, "'");
+ OPENVINO_ASSERT(mapped && mapped->data(), "[load_gguf] failed to mmap '", ov::util::path_to_string(file), "'");
const auto* base = reinterpret_cast<const uint8_t*>(mapped->data());
const uint64_t fsize = mapped->size();
@@ -445,7 +468,10 @@ GGUFLoad get_gguf_data(const std::string& file) {
// ---- Header ----
uint32_t magic = cur.read<uint32_t>();
- OPENVINO_ASSERT(magic == GGUF_MAGIC, "[load_gguf] '", file, "' is not a GGUF file (bad magic)");
+ OPENVINO_ASSERT(magic == GGUF_MAGIC,
+ "[load_gguf] '",
+ ov::util::path_to_string(file),
+ "' is not a GGUF file (bad magic)");
uint32_t version = cur.read<uint32_t>();
OPENVINO_ASSERT(version == 2 || version == 3, "[load_gguf] unsupported GGUF version ", version);
uint64_t tensor_count = cur.read<uint64_t>();
@@ -473,13 +499,6 @@ GGUFLoad get_gguf_data(const std::string& file) {
alignment);
// ---- Tensor info section ----
- struct TensorInfo {
- std::string name;
- uint32_t type = 0;
- uint32_t ndim = 0;
- uint64_t dim[4] = {1, 1, 1, 1};
- uint64_t offset = 0;
- };
// A minimal tensor-info record (0-length name, ndim=0) is 8 (name len) + 4 (ndim) + 4 (type) +
// 8 (offset) = 24 bytes; bound the file-controlled `tensor_count` by that before sizing the
// vector, so a tiny malformed file can't request an effectively unbounded allocation.
@@ -502,56 +521,74 @@ GGUFLoad get_gguf_data(const std::string& file) {
// Tensor data starts at the next `alignment`-aligned offset after the info section.
uint64_t data_off = cur.offset() + ov::util::align_padding_size(alignment, cur.offset());
+ OPENVINO_ASSERT(data_off <= fsize, "[load_gguf] tensor data section starts past EOF");
+ return {std::move(metadata), std::move(infos), std::move(mapped), alignment, data_off};
+}
- // Older Prism files used the upstream Q2_0 id for group-128 payloads.
- std::vector<uint64_t> offsets;
- for (const auto& ti : infos)
- offsets.push_back(ti.offset);
- std::sort(offsets.begin(), offsets.end());
- OPENVINO_ASSERT(data_off <= fsize && (offsets.empty() || offsets.back() <= fsize - data_off),
- "[load_gguf] tensor offset runs past EOF");
+void detect_legacy_q2_layout(std::vector<GgufFile>& files) {
std::vector<TensorInfo*> ambiguous_q2;
bool legacy_g128 = false;
- for (auto& ti : infos) {
- if (ti.type != GGUF_TYPE_Q2_0 || ti.dim[0] == 0)
- continue;
- const auto next = std::upper_bound(offsets.begin(), offsets.end(), ti.offset);
- OPENVINO_ASSERT(data_off <= fsize && ti.offset <= fsize - data_off,
- "[load_gguf] tensor '",
- ti.name,
- "' offset runs past EOF");
- const auto span = (next == offsets.end() ? fsize - data_off : *next) - ti.offset;
- const auto size_for = [&](uint32_t type) {
- const auto traits = type_traits(type);
- uint64_t blocks = ti.dim[0] / traits.items_per_block;
- for (uint32_t d = 1; d < ti.ndim; ++d) {
- OPENVINO_ASSERT(ti.dim[d] == 0 || blocks <= std::numeric_limits<uint64_t>::max() / ti.dim[d],
+ for (auto& file : files) {
+ auto& infos = file.infos;
+ const auto alignment = file.alignment;
+ const auto data_off = file.data_off;
+ const uint64_t fsize = file.mapped->size();
+ std::vector<uint64_t> offsets;
+ for (const auto& ti : infos)
+ offsets.push_back(ti.offset);
+ std::sort(offsets.begin(), offsets.end());
+ OPENVINO_ASSERT(data_off <= fsize && (offsets.empty() || offsets.back() <= fsize - data_off),
+ "[load_gguf] tensor offset runs past EOF");
+ for (auto& ti : infos) {
+ if (ti.type != GGUF_TYPE_Q2_0 || ti.dim[0] == 0)
+ continue;
+ const auto next = std::upper_bound(offsets.begin(), offsets.end(), ti.offset);
+ OPENVINO_ASSERT(data_off <= fsize && ti.offset <= fsize - data_off,
+ "[load_gguf] tensor '",
+ ti.name,
+ "' offset runs past EOF");
+ const auto span = (next == offsets.end() ? fsize - data_off : *next) - ti.offset;
+ const auto size_for = [&](uint32_t type) {
+ const auto traits = type_traits(type);
+ uint64_t blocks = ti.dim[0] / traits.items_per_block;
+ for (uint32_t d = 1; d < ti.ndim; ++d) {
+ OPENVINO_ASSERT(ti.dim[d] == 0 || blocks <= std::numeric_limits<uint64_t>::max() / ti.dim[d],
+ "[load_gguf] tensor '",
+ ti.name,
+ "' byte count overflows");
+ blocks *= ti.dim[d];
+ }
+ OPENVINO_ASSERT(blocks <= std::numeric_limits<uint64_t>::max() / traits.bytes_per_block,
"[load_gguf] tensor '",
ti.name,
"' byte count overflows");
- blocks *= ti.dim[d];
+ const auto bytes = blocks * traits.bytes_per_block;
+ return bytes <= span &&
+ (bytes == span || ov::util::align_padding_size(alignment, bytes) == span - bytes);
+ };
+ const bool g64 = ti.dim[0] % 64 == 0 && size_for(GGUF_TYPE_Q2_0);
+ const bool g128 = ti.dim[0] % 128 == 0 && size_for(GGUF_TYPE_PQ2_0);
+ OPENVINO_ASSERT(g64 || g128, "[load_gguf] tensor '", ti.name, "' has invalid Q2_0 packing");
+ if (g64 && g128) {
+ ambiguous_q2.push_back(&ti);
+ } else if (g128) {
+ ti.type = GGUF_TYPE_PQ2_0;
+ legacy_g128 = true;
}
- OPENVINO_ASSERT(blocks <= std::numeric_limits<uint64_t>::max() / traits.bytes_per_block,
- "[load_gguf] tensor '",
- ti.name,
- "' byte count overflows");
- const auto bytes = blocks * traits.bytes_per_block;
- return bytes <= span && (bytes == span || ov::util::align_padding_size(alignment, bytes) == span - bytes);
- };
- const bool g64 = ti.dim[0] % 64 == 0 && size_for(GGUF_TYPE_Q2_0);
- const bool g128 = ti.dim[0] % 128 == 0 && size_for(GGUF_TYPE_PQ2_0);
- OPENVINO_ASSERT(g64 || g128, "[load_gguf] tensor '", ti.name, "' has invalid Q2_0 packing");
- if (g64 && g128) {
- ambiguous_q2.push_back(&ti);
- } else if (g128) {
- ti.type = GGUF_TYPE_PQ2_0;
- legacy_g128 = true;
}
}
if (legacy_g128) {
for (auto* ti : ambiguous_q2)
ti->type = GGUF_TYPE_PQ2_0;
}
+}
+
+void load_gguf_tensors(GgufFile& file,
+ std::unordered_map<std::string, ov::Tensor>& arrays,
+ std::unordered_map<std::string, GgufTensorType>& qtype) {
+ auto& [metadata, infos, mapped, alignment, data_off] = file;
+ const auto* base = reinterpret_cast<const uint8_t*>(mapped->data());
+ const uint64_t fsize = mapped->size();
// Validate file-controlled shapes before allocating repacked tensors.
auto quant_sizes = [](const TensorInfo& ti, const QuantLayout& ql) -> std::array<size_t, 3> {
@@ -675,8 +712,153 @@ GGUFLoad get_gguf_data(const std::string& file) {
}
qtype.emplace(name_prefix + ".qtype", static_cast<GgufTensorType>(ti.type));
}
+}
+
+uint64_t split_value(const GgufFile& file, const std::string& key, const ov::element::Type& type) {
+ const auto& value = metadata_scalar_tensor(file.metadata, key, type);
+ OPENVINO_ASSERT(value.get_shape().empty(), "[load_gguf] '", key, "' must be a scalar");
+ if (type == ov::element::u16) {
+ return *value.data<const uint16_t>();
+ }
+ const auto count = *value.data<const int32_t>();
+ OPENVINO_ASSERT(count >= 0, "[load_gguf] '", key, "' must be nonnegative");
+ return static_cast<uint64_t>(count);
+}
+
+uint64_t split_count(const GgufFile& file) {
+ return file.metadata.count("split.count") ? split_value(file, "split.count", ov::element::u16) : 0;
+}
+
+bool same_metadata(const GGUFMetaData& lhs, const GGUFMetaData& rhs) {
+ if (lhs.index() != rhs.index()) {
+ return false;
+ }
+ return std::visit(ov::util::VariantVisitor{[&](const ov::Tensor& a) {
+ const auto& b = std::get<ov::Tensor>(rhs);
+ return a.get_element_type() == b.get_element_type() &&
+ a.get_shape() == b.get_shape() &&
+ (a.get_byte_size() == 0 ||
+ std::memcmp(a.data(), b.data(), a.get_byte_size()) == 0);
+ },
+ [&](const auto& a) {
+ return a == std::get<std::decay_t<decltype(a)>>(rhs);
+ }},
+ lhs);
+}
+
+std::string split_suffix(uint64_t index, uint64_t count) {
+ std::ostringstream suffix;
+ suffix << '-' << std::setfill('0') << std::setw(5) << index + 1 << "-of-" << std::setw(5) << count << ".gguf";
+ return suffix.str();
+}
+
+GGUFLoad load_gguf_files(std::vector<GgufFile> files) {
+ const uint64_t count = split_count(files.front());
+ if (count != 0) {
+ OPENVINO_ASSERT(files.size() == count, "[load_gguf] expected ", count, " GGUF shards, got ", files.size());
+ std::vector<bool> seen(count);
+ uint64_t tensor_count = 0;
+ const auto total = split_value(files.front(), "split.tensors.count", ov::element::i32);
+ for (const auto& file : files) {
+ OPENVINO_ASSERT(split_count(file) == count, "[load_gguf] inconsistent split.count across GGUF shards");
+ const auto index = split_value(file, "split.no", ov::element::u16);
+ OPENVINO_ASSERT(index < count, "[load_gguf] split.no is out of range: ", index);
+ OPENVINO_ASSERT(!seen[index], "[load_gguf] duplicate GGUF shard index: ", index);
+ seen[index] = true;
+ OPENVINO_ASSERT(split_value(file, "split.tensors.count", ov::element::i32) == total,
+ "[load_gguf] inconsistent split.tensors.count across GGUF shards");
+ tensor_count += file.infos.size();
+ }
+ OPENVINO_ASSERT(tensor_count == total,
+ "[load_gguf] split.tensors.count declares ",
+ total,
+ " tensors, found ",
+ tensor_count);
+ std::sort(files.begin(), files.end(), [](const auto& a, const auto& b) {
+ return split_value(a, "split.no", ov::element::u16) < split_value(b, "split.no", ov::element::u16);
+ });
+ } else {
+ OPENVINO_ASSERT(files.size() == 1, "[load_gguf] multiple inputs must belong to one split GGUF model");
+ }
+
+ const auto& metadata = files.front().metadata;
+ std::unordered_set<std::string> names;
+ for (const auto& file : files) {
+ for (const auto& [key, value] : file.metadata) {
+ if (key.compare(0, 6, "split.") == 0) {
+ continue;
+ }
+ const auto it = metadata.find(key);
+ OPENVINO_ASSERT(it != metadata.end() && same_metadata(it->second, value),
+ "[load_gguf] inconsistent metadata across GGUF shards: '",
+ key,
+ "'");
+ }
+ for (const auto& info : file.infos) {
+ OPENVINO_ASSERT(names.insert(info.name).second, "[load_gguf] duplicate tensor name '", info.name, "'");
+ }
+ }
- return {std::move(metadata), std::move(arrays), std::move(qtype), std::move(mapped)};
+ std::unordered_map<std::string, ov::Tensor> arrays;
+ std::unordered_map<std::string, GgufTensorType> qtype;
+ std::vector<std::shared_ptr<ov::MappedMemory>> mappings;
+ mappings.reserve(files.size());
+ detect_legacy_q2_layout(files);
+ for (auto& file : files) {
+ load_gguf_tensors(file, arrays, qtype);
+ mappings.push_back(std::move(file.mapped));
+ }
+ return {std::move(files.front().metadata), std::move(arrays), std::move(qtype), std::move(mappings)};
+}
+
+} // namespace
+
+GGUFLoad get_gguf_data(const std::filesystem::path& file) {
+ auto initial = read_gguf_file(file);
+ const auto count = split_count(initial);
+ if (count <= 1) {
+ std::vector<GgufFile> files;
+ files.push_back(std::move(initial));
+ return load_gguf_files(std::move(files));
+ }
+ const auto index = split_value(initial, "split.no", ov::element::u16);
+ OPENVINO_ASSERT(index < count, "[load_gguf] split.no is out of range: ", index);
+ const auto filename = file.filename().native();
+ const auto suffix = std::filesystem::path(split_suffix(index, count)).native();
+ OPENVINO_ASSERT(filename.size() >= suffix.size() &&
+ filename.compare(filename.size() - suffix.size(), suffix.size(), suffix) == 0,
+ "[load_gguf] split GGUF filename does not match its metadata; provide an explicit list of shards");
+ const auto prefix = filename.substr(0, filename.size() - suffix.size());
+ std::vector<GgufFile> files;
+ files.reserve(count);
+ for (uint64_t i = 0; i < count; ++i) {
+ if (i == index) {
+ files.push_back(std::move(initial));
+ continue;
+ }
+ const auto sibling = file.parent_path() / (prefix + std::filesystem::path(split_suffix(i, count)).native());
+ OPENVINO_ASSERT(std::filesystem::is_regular_file(sibling),
+ "[load_gguf] missing GGUF shard: '",
+ ov::util::path_to_string(sibling),
+ "'");
+ auto next = read_gguf_file(sibling);
+ OPENVINO_ASSERT(split_value(next, "split.no", ov::element::u16) == i,
+ "[load_gguf] split.no does not match shard filename: '",
+ ov::util::path_to_string(sibling),
+ "'");
+ files.push_back(std::move(next));
+ }
+ return load_gguf_files(std::move(files));
+}
+
+GGUFLoad get_gguf_data(const std::vector<std::filesystem::path>& paths) {
+ OPENVINO_ASSERT(!paths.empty(), "[load_gguf] GGUF shard list is empty");
+ std::vector<GgufFile> files;
+ files.reserve(paths.size());
+ for (const auto& path : paths) {
+ files.push_back(read_gguf_file(path));
+ }
+ return load_gguf_files(std::move(files));
}
std::map<std::string, GGUFMetaData> decoder_config_from_meta(
diff --git a/src/frontends/gguf/src/quant/gguf.hpp b/src/frontends/gguf/src/quant/gguf.hpp
index b502a989378..cf7385094ef 100644
--- a/src/frontends/gguf/src/quant/gguf.hpp
+++ b/src/frontends/gguf/src/quant/gguf.hpp
@@ -5,6 +5,7 @@
#pragma once
#include <cstdint>
+#include <filesystem>
#include <map>
#include <string>
#include <unordered_map>
@@ -91,7 +92,7 @@ using GGUFMetaData =
using GGUFLoad = std::tuple<std::unordered_map<std::string, GGUFMetaData>,
std::unordered_map<std::string, ov::Tensor>,
std::unordered_map<std::string, GgufTensorType>,
- std::shared_ptr<ov::MappedMemory>>;
+ std::vector<std::shared_ptr<ov::MappedMemory>>>;
// Fill pre-allocated weights and f16 scales from a symmetric GGUF tensor
// (Q8_0/Q5_0/Q6_K: i8 weights; Q4_0/Q3_K: i4 weights; Q1_0: u1 weights).
@@ -132,7 +133,8 @@ void dequant_row_q5_k_f32_for_test(const uint8_t* row, size_t cols, float* y);
void dequant_row_q6_k_f32_for_test(const uint8_t* row, size_t cols, float* y);
// Parse GGUF metadata and tensors. Each repacked quantized tensor owns its buffer.
-GGUFLoad get_gguf_data(const std::string& file);
+GGUFLoad get_gguf_data(const std::filesystem::path& file);
+GGUFLoad get_gguf_data(const std::vector<std::filesystem::path>& files);
// Extract the DECODER-family architecture config (architecture, layer_num, head_num, head_size,
// head_num_kv, hidden_size, max_position_embeddings, rms_norm_eps, rope_freq_base, file_type, ...)
diff --git a/src/frontends/gguf/tests/gguf_writer.hpp b/src/frontends/gguf/tests/gguf_writer.hpp
index fd5660ed61c..f195ddcc1be 100644
--- a/src/frontends/gguf/tests/gguf_writer.hpp
+++ b/src/frontends/gguf/tests/gguf_writer.hpp
@@ -18,7 +18,9 @@ namespace ov_gguf_test {
// GGUF metadata value types (gguf.h GGUF_VALUE_TYPE_*).
enum : uint32_t {
+ GGUF_TYPE_UINT16 = 2,
GGUF_TYPE_UINT32 = 4,
+ GGUF_TYPE_INT32 = 5,
GGUF_TYPE_FLOAT32 = 6,
GGUF_TYPE_BOOL = 7,
GGUF_TYPE_STRING = 8,
@@ -30,6 +32,14 @@ constexpr uint32_t GGML_TYPE_F32 = 0;
class GgufWriter {
public:
+ void kv_u16(const std::string& key, uint16_t v) {
+ kv_key(key, GGUF_TYPE_UINT16);
+ put(v);
+ }
+ void kv_i32(const std::string& key, int32_t v) {
+ kv_key(key, GGUF_TYPE_INT32);
+ put(v);
+ }
void kv_u32(const std::string& key, uint32_t v) {
kv_key(key, GGUF_TYPE_UINT32);
put(v);
@@ -54,6 +64,13 @@ public:
for (auto value : values)
put(value);
}
+ void kv_u16_array(const std::string& key, const std::vector<uint16_t>& values) {
+ kv_key(key, GGUF_TYPE_ARRAY);
+ put(uint32_t(GGUF_TYPE_UINT16));
+ put(uint64_t(values.size()));
+ for (auto value : values)
+ put(value);
+ }
void kv_str_array(const std::string& key, const std::vector<std::string>& values) {
kv_key(key, GGUF_TYPE_ARRAY);
put(uint32_t(GGUF_TYPE_STRING));
@@ -106,6 +123,25 @@ public:
++m_n_tensors;
}
+ void raw_tensor(const std::string& name,
+ const std::vector<uint64_t>& dims,
+ uint32_t type,
+ const std::vector<uint8_t>& data) {
+ put_str(m_ti, name);
+ put(m_ti, static_cast<uint32_t>(dims.size()));
+ for (auto d : dims) {
+ put(m_ti, d);
+ }
+ put(m_ti, type);
+ put(m_ti, m_data_size);
+ m_data.insert(m_data.end(), data.begin(), data.end());
+ if (const auto padding = data.size() % kAlignment) {
+ m_data.resize(m_data.size() + kAlignment - padding, 0);
+ }
+ m_data_size = m_data.size();
+ ++m_n_tensors;
+ }
+
bool write(const std::string& path) const {
std::ofstream out(path, std::ios::binary);
if (!out) {
diff --git a/src/frontends/gguf/tests/test_parser.cpp b/src/frontends/gguf/tests/test_parser.cpp
index 917d8bf5e20..d9a2d25d367 100644
--- a/src/frontends/gguf/tests/test_parser.cpp
+++ b/src/frontends/gguf/tests/test_parser.cpp
@@ -3,14 +3,18 @@
//
#include <array>
+#include <filesystem>
#include <fstream>
#include <tuple>
#include <utility>
#include "builder/graph_emitter.hpp"
#include "common_test_utils/common_utils.hpp"
+#include "common_test_utils/graph_comparator.hpp"
+#include "common_test_utils/test_assertions.hpp"
#include "gguf_writer.hpp"
#include "op_test_utils.hpp"
+#include "openvino/frontend/gguf/frontend.hpp"
#include "quant/weights.hpp"
using namespace ov_gguf_test;
@@ -261,4 +265,393 @@ TEST_F(GGUFParser, RejectsMXFP4WithZeroColumns) {
}
}
+class GGUFShards : public ::testing::Test {
+protected:
+ void SetUp() override {
+ m_base = ov::test::utils::generateTestFilePrefix();
+ }
+
+ void TearDown() override {
+ for (const auto& path : m_paths) {
+ if (std::filesystem::exists(path)) {
+ EXPECT_TRUE(std::filesystem::is_regular_file(path)) << path;
+ std::filesystem::remove(path);
+ }
+ }
+ }
+
+ std::string path(size_t index) const {
+ return m_base + "-0000" + std::to_string(index + 1) + "-of-00002.gguf";
+ }
+
+ void write_shard(size_t index,
+ uint16_t number,
+ uint16_t count = 2,
+ int32_t total = 2,
+ const std::string& name = {},
+ const std::string& architecture = {}) {
+ GgufWriter writer;
+ writer.kv_u16("split.no", number);
+ writer.kv_u16("split.count", count);
+ writer.kv_i32("split.tensors.count", total);
+ if (index == 0 || !architecture.empty()) {
+ writer.kv_str("general.architecture", architecture.empty() ? "qwen3" : architecture);
+ }
+ writer.tensor(name.empty() ? "weight" + std::to_string(index) + ".weight" : name,
+ {2},
+ {float(index + 1), float(index + 2)});
+ const auto file = path(index);
+ m_paths.push_back(file);
+ ASSERT_TRUE(writer.write(file));
+ }
+
+ std::string m_base;
+ std::vector<std::filesystem::path> m_paths;
+};
+
+TEST_F(GGUFShards, DiscoversSiblingsFromEitherShardAndKeepsMappingsAlive) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0));
+ ASSERT_NO_FATAL_FAILURE(write_shard(1, 1));
+ ov::Tensor retained;
+ for (size_t index : {0, 1}) {
+ const auto loaded = get_gguf_data(path(index));
+ EXPECT_EQ(std::get<3>(loaded).size(), 2);
+ EXPECT_EQ(std::get<std::string>(std::get<0>(loaded).at("general.architecture")), "qwen3");
+ const auto& arrays = std::get<1>(loaded);
+ ASSERT_EQ(arrays.size(), 2);
+ EXPECT_FLOAT_EQ(arrays.at("weight0.weight").data<const float>()[0], 1.f);
+ EXPECT_FLOAT_EQ(arrays.at("weight1.weight").data<const float>()[0], 2.f);
+ retained = arrays.at("weight1.weight");
+ }
+ EXPECT_FLOAT_EQ(retained.data<const float>()[1], 3.f);
+}
+
+TEST_F(GGUFShards, ExplicitListAllowsRenamedFilesInDifferentDirectoriesAndReversedOrder) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0));
+ ASSERT_NO_FATAL_FAILURE(write_shard(1, 1));
+ const auto renamed =
+ (std::filesystem::temp_directory_path() / (std::filesystem::path(m_base).filename().string() + "_renamed.gguf"))
+ .string();
+ m_paths.push_back(renamed);
+ ASSERT_TRUE(std::filesystem::copy_file(path(1), renamed));
+ const auto loaded = get_gguf_data(std::vector<std::filesystem::path>{renamed, path(0)});
+ EXPECT_EQ(std::get<1>(loaded).size(), 2);
+ EXPECT_EQ(std::get<std::string>(std::get<0>(loaded).at("general.architecture")), "qwen3");
+ OV_EXPECT_THROW(get_gguf_data(renamed), ov::Exception, testing::HasSubstr("explicit list"));
+}
+
+TEST_F(GGUFShards, LegacyGroup128PackingMatchesMergedFileAcrossShards) {
+ const auto oracle_bytes = load_npy<uint8_t>("pq2_0_qbytes");
+ const auto oracle_values = load_npy<float>("pq2_0_deq");
+ ASSERT_GE(oracle_bytes.size(), 34);
+ ASSERT_GE(oracle_values.size(), 128);
+ const std::vector<uint8_t> block(oracle_bytes.begin(), oracle_bytes.begin() + 34);
+ std::vector<uint8_t> large;
+ for (size_t i = 0; i < 16; ++i) {
+ large.insert(large.end(), block.begin(), block.end());
+ }
+ const auto merged_path = m_base + ".gguf";
+ m_paths.push_back(merged_path);
+ for (bool ambiguous_first : {false, true}) {
+ SCOPED_TRACE(ambiguous_first);
+ GgufWriter merged;
+ for (size_t index : {0, 1}) {
+ GgufWriter shard;
+ shard.kv_u16("split.no", static_cast<uint16_t>(index));
+ shard.kv_u16("split.count", 2);
+ shard.kv_i32("split.tensors.count", 2);
+ const bool ambiguous = (index == 0) == ambiguous_first;
+ const auto name = ambiguous ? "small.weight" : "large.weight";
+ const std::vector<uint64_t> dims{128, ambiguous ? 1u : 16u};
+ const auto& data = ambiguous ? block : large;
+ shard.raw_tensor(name, dims, GGUF_TYPE_Q2_0, data);
+ merged.raw_tensor(name, dims, GGUF_TYPE_Q2_0, data);
+ m_paths.push_back(path(index));
+ ASSERT_TRUE(shard.write(path(index)));
+ }
+ ASSERT_TRUE(merged.write(merged_path));
+ const auto reference = get_gguf_data(merged_path);
+ for (const auto& loaded : {get_gguf_data(path(0)),
+ get_gguf_data(path(1)),
+ get_gguf_data(std::vector<std::filesystem::path>{path(1), path(0)})}) {
+ for (const std::string stem : {"small", "large"}) {
+ EXPECT_EQ(std::get<2>(loaded).at(stem + ".qtype"), GGUF_TYPE_PQ2_0);
+ const auto& arrays = std::get<1>(loaded);
+ const auto& expected = std::get<1>(reference);
+ for (const auto& suffix : {".weight", ".scales", ".zp"}) {
+ const auto& actual = arrays.at(stem + suffix);
+ const auto& value = expected.at(stem + suffix);
+ ASSERT_EQ(actual.get_element_type(), value.get_element_type());
+ ASSERT_EQ(actual.get_shape(), value.get_shape());
+ EXPECT_EQ(std::memcmp(actual.data(), value.data(), actual.get_byte_size()), 0);
+ }
+ const auto node = make_weight_node(
+ {arrays.at(stem + ".weight"), arrays.at(stem + ".scales"), arrays.at(stem + ".zp")},
+ GGUF_TYPE_PQ2_0);
+ const auto model = std::make_shared<ov::Model>(ov::OutputVector{node}, ov::ParameterVector{});
+ const auto output = run_on_cpu(model, {});
+ for (size_t i = 0; i < output.get_size(); ++i) {
+ EXPECT_FLOAT_EQ(output.data<float>()[i], oracle_values[i % 128]);
+ }
+ }
+ }
+ }
+}
+
+TEST_F(GGUFShards, ReportsMissingSibling) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("missing GGUF shard"));
+}
+
+TEST_F(GGUFShards, RejectsIncompleteExplicitList) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0));
+ OV_EXPECT_THROW(get_gguf_data(std::vector<std::filesystem::path>{path(0)}),
+ ov::Exception,
+ testing::HasSubstr("expected 2 GGUF shards"));
+ ov::frontend::gguf::FrontEnd frontend;
+ OV_EXPECT_THROW(frontend.load(std::vector<std::string>{path(0)}),
+ ov::Exception,
+ testing::HasSubstr("expected 2 GGUF shards"));
+}
+
+TEST_F(GGUFShards, RejectsDuplicateShardIndices) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0));
+ ASSERT_NO_FATAL_FAILURE(write_shard(1, 0));
+ OV_EXPECT_THROW(get_gguf_data(std::vector<std::filesystem::path>{path(0), path(1)}),
+ ov::Exception,
+ testing::HasSubstr("duplicate GGUF shard index"));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("split.no does not match"));
+}
+
+TEST_F(GGUFShards, RejectsInconsistentShardCounts) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0));
+ ASSERT_NO_FATAL_FAILURE(write_shard(1, 1, 3));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("inconsistent split.count"));
+}
+
+TEST_F(GGUFShards, RejectsInconsistentTensorCounts) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0));
+ ASSERT_NO_FATAL_FAILURE(write_shard(1, 1, 2, 3));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("inconsistent split.tensors.count"));
+}
+
+TEST_F(GGUFShards, RejectsMissingTensors) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0, 2, 3));
+ ASSERT_NO_FATAL_FAILURE(write_shard(1, 1, 2, 3));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("declares 3 tensors, found 2"));
+}
+
+TEST_F(GGUFShards, RejectsDuplicateTensorNames) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0));
+ ASSERT_NO_FATAL_FAILURE(write_shard(1, 1, 2, 2, "weight0.weight"));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("duplicate tensor name"));
+}
+
+TEST_F(GGUFShards, RejectsConflictingModelMetadata) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0));
+ ASSERT_NO_FATAL_FAILURE(write_shard(1, 1, 2, 2, {}, "llama"));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("inconsistent metadata"));
+}
+
+TEST_F(GGUFShards, ComparesRepeatedNumericAndStringArrayMetadata) {
+ for (size_t index : {0, 1}) {
+ GgufWriter writer;
+ writer.kv_u16("split.no", static_cast<uint16_t>(index));
+ writer.kv_u16("split.count", 2);
+ writer.kv_i32("split.tensors.count", 2);
+ writer.kv_str("general.architecture", "qwen3");
+ writer.kv_f32("test.scalar", 1.5f);
+ writer.kv_u32_array("test.array", {1, 2});
+ writer.kv_u32_array("test.empty", {});
+ writer.kv_str_array("test.strings", {"one", "two"});
+ writer.tensor("weight" + std::to_string(index), {2});
+ m_paths.push_back(path(index));
+ ASSERT_TRUE(writer.write(path(index)));
+ }
+ EXPECT_EQ(std::get<1>(get_gguf_data(path(0))).size(), 2);
+}
+
+TEST_F(GGUFShards, RejectsConflictingNumericMetadata) {
+ enum class Difference { ScalarValue, ScalarType, ArrayValue, ArrayType, ArrayShape };
+ for (auto difference : {Difference::ScalarValue,
+ Difference::ScalarType,
+ Difference::ArrayValue,
+ Difference::ArrayType,
+ Difference::ArrayShape}) {
+ SCOPED_TRACE(static_cast<int>(difference));
+ for (size_t index : {0, 1}) {
+ GgufWriter writer;
+ writer.kv_u16("split.no", static_cast<uint16_t>(index));
+ writer.kv_u16("split.count", 2);
+ writer.kv_i32("split.tensors.count", 2);
+ writer.kv_str("general.architecture", "qwen3");
+ if (index == 1 && difference == Difference::ScalarType) {
+ writer.kv_u32("test.scalar", 1);
+ } else {
+ writer.kv_f32("test.scalar", index == 1 && difference == Difference::ScalarValue ? 2.f : 1.f);
+ }
+ if (index == 1 && difference == Difference::ArrayType) {
+ writer.kv_u16_array("test.array", {1, 2});
+ } else {
+ std::vector<uint32_t> values{1, 2};
+ if (index == 1 && difference == Difference::ArrayValue) {
+ values[1] = 3;
+ } else if (index == 1 && difference == Difference::ArrayShape) {
+ values.resize(1);
+ }
+ writer.kv_u32_array("test.array", values);
+ }
+ writer.tensor("weight" + std::to_string(index), {2});
+ m_paths.push_back(path(index));
+ ASSERT_TRUE(writer.write(path(index)));
+ }
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("inconsistent metadata"));
+ }
+}
+
+TEST_F(GGUFShards, RejectsOutOfRangeShardNumber) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 2));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("split.no is out of range"));
+}
+
+TEST_F(GGUFShards, RejectsInvalidSplitMetadataType) {
+ GgufWriter writer;
+ writer.kv_u32("split.count", 2);
+ m_paths.push_back(path(0));
+ ASSERT_TRUE(writer.write(path(0)));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("split.count"));
+}
+
+TEST_F(GGUFShards, RejectsNegativeTensorCountAndArraySplitMetadata) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0, 2, -1));
+ ASSERT_NO_FATAL_FAILURE(write_shard(1, 1, 2, -1));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("must be nonnegative"));
+ GgufWriter invalid;
+ invalid.kv_u16("split.count", 2);
+ invalid.kv_u16_array("split.no", {0});
+ ASSERT_TRUE(invalid.write(path(0)));
+ OV_EXPECT_THROW(get_gguf_data(path(0)), ov::Exception, testing::HasSubstr("must be a scalar"));
+}
+
+TEST_F(GGUFShards, AcceptsMetadataOnlyFirstShard) {
+ GgufWriter writer;
+ writer.kv_str("general.architecture", "qwen3");
+ writer.kv_u16("split.no", 0);
+ writer.kv_u16("split.count", 2);
+ writer.kv_i32("split.tensors.count", 1);
+ m_paths.push_back(path(0));
+ ASSERT_TRUE(writer.write(path(0)));
+ ASSERT_NO_FATAL_FAILURE(write_shard(1, 1, 2, 1));
+ const auto loaded = get_gguf_data(path(1));
+ EXPECT_EQ(std::get<1>(loaded).size(), 1);
+ EXPECT_EQ(std::get<std::string>(std::get<0>(loaded).at("general.architecture")), "qwen3");
+}
+
+TEST_F(GGUFShards, AcceptsSingleShardAndMergedSplitMetadata) {
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0, 1, 1));
+ EXPECT_EQ(std::get<1>(get_gguf_data(path(0))).size(), 1);
+ ASSERT_NO_FATAL_FAILURE(write_shard(0, 0, 0, 1));
+ EXPECT_EQ(std::get<1>(get_gguf_data(path(0))).size(), 1);
+}
+
+TEST_F(GGUFShards, RejectsUnrelatedModelsAndEmptyLists) {
+ GgufWriter writer;
+ writer.tensor("weight", {1}, {1.f});
+ for (size_t index : {0, 1}) {
+ m_paths.push_back(path(index));
+ ASSERT_TRUE(writer.write(path(index)));
+ }
+ OV_EXPECT_THROW(get_gguf_data(std::vector<std::filesystem::path>{path(0), path(1)}),
+ ov::Exception,
+ testing::HasSubstr("one split GGUF model"));
+ OV_EXPECT_THROW(get_gguf_data(std::vector<std::filesystem::path>{}), ov::Exception, testing::HasSubstr("empty"));
+}
+
+TEST_F(GGUFShards, FrontendConvertsTheSameConstantsFromSingleFileAndShardInputs) {
+ GgufWriter complete;
+ GgufWriter first;
+ GgufWriter second;
+ for (auto* writer : {&complete, &first}) {
+ writer->kv_str("general.architecture", "qwen3");
+ writer->kv_u32("qwen3.block_count", 1);
+ writer->kv_u32("qwen3.embedding_length", 8);
+ writer->kv_u32("qwen3.attention.head_count", 2);
+ writer->kv_u32("qwen3.attention.head_count_kv", 1);
+ writer->kv_u32("qwen3.rope.dimension_count", 4);
+ writer->kv_u32("qwen3.context_length", 32);
+ writer->kv_f32("qwen3.attention.layer_norm_rms_epsilon", 1e-5f);
+ }
+ const std::vector<std::pair<std::string, std::vector<uint64_t>>> tensors{
+ {"token_embd.weight", {8, 16}},
+ {"output_norm.weight", {8}},
+ {"blk.0.attn_norm.weight", {8}},
+ {"blk.0.attn_q.weight", {8, 8}},
+ {"blk.0.attn_k.weight", {8, 4}},
+ {"blk.0.attn_v.weight", {8, 4}},
+ {"blk.0.attn_output.weight", {8, 8}},
+ {"blk.0.attn_q_norm.weight", {4}},
+ {"blk.0.attn_k_norm.weight", {4}},
+ {"blk.0.ffn_norm.weight", {8}},
+ {"blk.0.ffn_gate.weight", {8, 12}},
+ {"blk.0.ffn_up.weight", {8, 12}},
+ {"blk.0.ffn_down.weight", {12, 8}},
+ };
+ size_t i = 0;
+ for (auto* writer : {&first, &second}) {
+ writer->kv_u16("split.no", static_cast<uint16_t>(i++));
+ writer->kv_u16("split.count", 2);
+ writer->kv_i32("split.tensors.count", static_cast<int32_t>(tensors.size()));
+ }
+ i = 0;
+ for (const auto& [name, shape] : tensors) {
+ const auto norm = name.find("norm") != std::string::npos;
+ complete.filled_tensor(name, shape, norm);
+ (i++ % 2 == 0 ? first : second).filled_tensor(name, shape, norm);
+ }
+ const auto reference_path = m_base + ".gguf";
+ m_paths = {reference_path, path(0), path(1)};
+ ASSERT_TRUE(complete.write(reference_path));
+ ASSERT_TRUE(first.write(path(0)));
+ ASSERT_TRUE(second.write(path(1)));
+ ov::frontend::gguf::FrontEnd frontend;
+ const auto reference = frontend.convert(frontend.load(reference_path));
+ auto comparator = FunctionsComparator::with_default();
+ comparator.enable(FunctionsComparator::CONST_VALUES).enable(FunctionsComparator::ATTRIBUTES);
+ const std::vector<ov::AnyVector> representations{
+ {path(0), true},
+ {path(1)},
+ {std::vector<std::string>{path(1), path(0)}, true},
+ {path(0), path(1)},
+ {std::vector<std::filesystem::path>{path(1), path(0)}},
+ };
+ for (const auto& representation : representations) {
+ EXPECT_TRUE(frontend.supported(representation));
+ const auto model = frontend.convert(frontend.load(representation));
+ EXPECT_EQ(model->get_ordered_ops().size(), reference->get_ordered_ops().size());
+ const auto comparison = comparator.compare(model, reference);
+ EXPECT_TRUE(comparison.valid) << comparison.message;
+ }
+ const auto unicode_prefix = ov::util::make_path(m_base + u8"_模型_żółć");
+ const auto unicode_first =
+ std::filesystem::path(unicode_prefix.native() + std::filesystem::path("-00001-of-00002.gguf").native());
+ const auto unicode_second =
+ std::filesystem::path(unicode_prefix.native() + std::filesystem::path("-00002-of-00002.gguf").native());
+ const auto unicode_single =
+ std::filesystem::path(unicode_prefix.native() + std::filesystem::path(".gguf").native());
+ std::filesystem::rename(path(0), unicode_first);
+ std::filesystem::rename(path(1), unicode_second);
+ std::filesystem::rename(reference_path, unicode_single);
+ m_paths.insert(m_paths.end(), {unicode_first, unicode_second, unicode_single});
+ for (const ov::Any& representation : {ov::Any(unicode_first),
+ ov::Any(unicode_second),
+ ov::Any(unicode_single),
+ ov::Any(std::vector<std::filesystem::path>{unicode_second, unicode_first})}) {
+ EXPECT_TRUE(frontend.supported(representation));
+ const auto comparison = comparator.compare(frontend.convert(frontend.load(representation)), reference);
+ EXPECT_TRUE(comparison.valid) << comparison.message;
+ }
+ EXPECT_FALSE(frontend.supported(std::vector<std::string>{}));
+ EXPECT_FALSE(frontend.supported(std::vector<std::string>{path(0), m_base + "_missing.gguf"}));
+}
+
} // namespace