22#include " ggml-backend.h"
33#include " ggml-impl.h"
44#include " gguf.h"
5+ #include " gguf-reader.h"
56
67#include < cinttypes>
78#include < cstddef>
@@ -216,82 +217,12 @@ struct gguf_context {
216217 void * data = nullptr ;
217218};
218219
219- struct gguf_reader {
220- FILE * file;
221-
222- gguf_reader (FILE * file) : file(file) {}
223-
224- template <typename T>
225- bool read (T & dst) const {
226- return fread (&dst, 1 , sizeof (dst), file) == sizeof (dst);
227- }
228-
229- template <typename T>
230- bool read (std::vector<T> & dst, const size_t n) const {
231- dst.resize (n);
232- for (size_t i = 0 ; i < dst.size (); ++i) {
233- if constexpr (std::is_same<T, bool >::value) {
234- bool tmp;
235- if (!read (tmp)) {
236- return false ;
237- }
238- dst[i] = tmp;
239- } else {
240- if (!read (dst[i])) {
241- return false ;
242- }
243- }
244- }
245- return true ;
246- }
247-
248- bool read (bool & dst) const {
249- int8_t tmp = -1 ;
250- if (!read (tmp)) {
251- return false ;
252- }
253- dst = tmp != 0 ;
254- return true ;
255- }
256-
257- bool read (enum ggml_type & dst) const {
258- int32_t tmp = -1 ;
259- if (!read (tmp)) {
260- return false ;
261- }
262- dst = ggml_type (tmp);
263- return true ;
264- }
265-
266- bool read (enum gguf_type & dst) const {
267- int32_t tmp = -1 ;
268- if (!read (tmp)) {
269- return false ;
270- }
271- dst = gguf_type (tmp);
272- return true ;
273- }
274-
275- bool read (std::string & dst) const {
276- uint64_t size = -1 ;
277- if (!read (size)) {
278- return false ;
279- }
280- dst.resize (size);
281- return fread (dst.data (), 1 , dst.length (), file) == dst.length ();
282- }
283-
284- bool read (void * dst, const size_t size) const {
285- return fread (dst, 1 , size, file) == size;
286- }
287- };
288-
289220struct gguf_context * gguf_init_empty (void ) {
290221 return new gguf_context;
291222}
292223
293224template <typename T>
294- bool gguf_read_emplace_helper (const struct gguf_reader & gr, std::vector<struct gguf_kv > & kv, const std::string & key, const bool is_array, const size_t n) {
225+ bool gguf_read_emplace_helper (struct gguf_reader & gr, std::vector<struct gguf_kv > & kv, const std::string & key, const bool is_array, const size_t n) {
295226 if (is_array) {
296227 std::vector<T> value;
297228 try {
@@ -316,8 +247,15 @@ bool gguf_read_emplace_helper(const struct gguf_reader & gr, std::vector<struct
316247 return true ;
317248}
318249
319- struct gguf_context * gguf_init_from_file_impl (FILE * file, struct gguf_init_params params) {
320- const struct gguf_reader gr (file);
250+ struct gguf_context * gguf_init_from_file (const char * fname, struct gguf_init_params params)
251+ {
252+ gguf_reader gr (fname);
253+
254+ if (!gr.open ()) {
255+ GGML_LOG_ERROR (" %s: failed to open reader\n " , __func__);
256+ return nullptr ;
257+ }
258+
321259 struct gguf_context * ctx = new gguf_context;
322260
323261 bool ok = true ;
@@ -610,14 +548,14 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
610548 GGML_ASSERT (int64_t (ctx->info .size ()) == n_tensors);
611549
612550 // we require the data section to be aligned, so take into account any padding
613- if (fseek (file, GGML_PAD (ftell (file ), ctx->alignment ), SEEK_SET) != 0 ) {
551+ if (!gr. position_set ( GGML_PAD (gr. position ( ), ctx->alignment )) ) {
614552 GGML_LOG_ERROR (" %s: failed to seek to beginning of data section\n " , __func__);
615553 gguf_free (ctx);
616554 return nullptr ;
617555 }
618556
619557 // store the current file offset - this is where the data section starts
620- ctx->offset = ftell (file );
558+ ctx->offset = gr. position ( );
621559
622560 // compute the total size of the data section, taking into account the alignment
623561 {
@@ -681,7 +619,7 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
681619 }
682620
683621 // read the binary blob with the tensor data
684- ok = ok && gr.read (data->data , ctx->size );
622+ ok = ok && gr.read (data->data , ctx->size ) == ctx-> size ;
685623
686624 if (!ok) {
687625 GGML_LOG_ERROR (" %s: failed to read tensor data binary blob\n " , __func__);
@@ -730,19 +668,6 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
730668 return ctx;
731669}
732670
733- struct gguf_context * gguf_init_from_file (const char * fname, struct gguf_init_params params) {
734- FILE * file = ggml_fopen (fname, " rb" );
735-
736- if (!file) {
737- GGML_LOG_ERROR (" %s: failed to open GGUF file '%s'\n " , __func__, fname);
738- return nullptr ;
739- }
740-
741- struct gguf_context * result = gguf_init_from_file_impl (file, params);
742- fclose (file);
743- return result;
744- }
745-
746671void gguf_free (struct gguf_context * ctx) {
747672 if (ctx == nullptr ) {
748673 return ;
0 commit comments