1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// transcribe-loader.h - internal GGUF loader (architecture-agnostic).
//
// This header is INTERNAL. It is C++17 (not C) on purpose: it is consumed
// only from other .cpp files inside src/. The public C ABI lives in
// include/transcribe.h and never references anything declared here.
//
// The Loader exists so transcribe_model_load_file can answer two
// questions before it has to commit to a per-family code path:
//
// 1. Is this file a readable GGUF at all?
// 2. What architecture does it claim (general.architecture / stt.variant)?
//
// Once the architecture is known the loader is handed off to the per-arch
// handler, which may take ownership of the underlying gguf_context via
// release_gguf(). If the handler does not take ownership (e.g. because it
// returned NOT_IMPLEMENTED in pass 2A), the Loader's destructor frees the
// context normally on stack unwinding.
#pragma once
#include "transcribe.h"
#include <string>
struct gguf_context;
namespace transcribe {
class Loader {
public:
Loader() = default;
~Loader();
// Non-copyable. Move semantics are not needed in 0.x because the
// loader is always either constructed-then-released-by-handler or
// constructed-then-destroyed-on-the-stack.
Loader(const Loader &) = delete;
Loader & operator=(const Loader &) = delete;
Loader(Loader &&) = delete;
Loader & operator=(Loader &&) = delete;
// Open a GGUF file and read its identification fields.
//
// Returns:
// TRANSCRIBE_OK on success.
// TRANSCRIBE_ERR_INVALID_ARG if path is null.
// TRANSCRIBE_ERR_FILE_NOT_FOUND if (and ONLY if) a stat() pre-check
// proves the path does not exist —
// i.e. stat() fails with ENOENT or
// ENOTDIR. Other stat() failures
// (EACCES, ELOOP, ...) and a
// successful stat() of a non-regular
// file (e.g. directory) fall through
// to gguf_init_from_file and surface
// as TRANSCRIBE_ERR_GGUF below.
// TRANSCRIBE_ERR_GGUF if gguf_init_from_file rejects the
// file (corrupt magic, truncated
// header, unsupported version, IO
// error, path is a directory, etc.)
// or if the required
// general.architecture KV is missing
// / wrong type.
transcribe_status open(const char * path);
// Identification fields, populated on a successful open(). The arch
// string is required by the GGUF schema we accept; the variant string
// is optional and may be empty for older or partial files.
const std::string & path() const { return path_; }
const std::string & arch() const { return arch_; }
const std::string & variant() const { return variant_; }
// Borrowed pointer to the underlying gguf_context. Valid until the
// Loader is destroyed or release_gguf() is called. Returns nullptr
// before a successful open().
gguf_context * gguf() const { return gguf_; }
// Hand the gguf_context off to the per-arch handler. After this call
// the Loader no longer owns the context and the caller must
// gguf_free() it. The Loader is still safe to destroy.
gguf_context * release_gguf();
private:
std::string path_;
std::string arch_;
std::string variant_;
gguf_context * gguf_ = nullptr;
};
} // namespace transcribe