Skip to content

Commit a791a68

Browse files
authored
move file magic/version to header, print expected version (#319)
1 parent 0f1b21c commit a791a68

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

main.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,22 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
106106
{
107107
uint32_t magic;
108108
fin.read((char *) &magic, sizeof(magic));
109-
if (magic == 0x67676d6c) {
109+
if (magic == FILE_MAGIC_UNVERSIONED) {
110110
fprintf(stderr, "%s: invalid model file '%s' (too old, regenerate your model files!)\n",
111111
__func__, fname.c_str());
112112
return false;
113113
}
114-
if (magic != 0x67676d66) {
114+
if (magic != FILE_MAGIC) {
115115
fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n", __func__, fname.c_str());
116116
return false;
117117
}
118118

119119
uint32_t format_version;
120120
fin.read((char *) &format_version, sizeof(format_version));
121121

122-
if (format_version != 1) {
123-
fprintf(stderr, "%s: invalid model file '%s' (unsupported format version %" PRIu32 ")\n",
124-
__func__, fname.c_str(), format_version);
122+
if (format_version != FILE_VERSION) {
123+
fprintf(stderr, "%s: invalid model file '%s' (unsupported format version %" PRIu32 ", expected %d)\n",
124+
__func__, fname.c_str(), format_version, FILE_VERSION);
125125
return false;
126126
}
127127
}

quantize.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ bool llama_model_quantize(const std::string & fname_inp, const std::string & fna
6464
{
6565
uint32_t magic;
6666
finp.read((char *) &magic, sizeof(magic));
67-
if (magic == 0x67676d6c) {
67+
if (magic == FILE_MAGIC_UNVERSIONED) {
6868
fprintf(stderr, "%s: invalid model file '%s' (too old, regenerate your model files!)\n",
6969
__func__, fname_inp.c_str());
7070
return false;
7171
}
72-
if (magic != 0x67676d66) {
72+
if (magic != FILE_MAGIC) {
7373
fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n", __func__, fname_inp.c_str());
7474
return false;
7575
}
@@ -79,9 +79,9 @@ bool llama_model_quantize(const std::string & fname_inp, const std::string & fna
7979
uint32_t format_version;
8080
finp.read((char *) &format_version, sizeof(format_version));
8181

82-
if (format_version != 1) {
83-
fprintf(stderr, "%s: invalid model file '%s' (unsupported format version %" PRIu32 ")\n",
84-
__func__, fname_inp.c_str(), format_version);
82+
if (format_version != FILE_VERSION) {
83+
fprintf(stderr, "%s: invalid model file '%s' (unsupported format version %" PRIu32 ", expected %d)\n",
84+
__func__, fname_inp.c_str(), format_version, FILE_VERSION);
8585
return false;
8686
}
8787

utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ void gpt_print_usage(int argc, char ** argv, const gpt_params & params);
4848

4949
std::string gpt_random_prompt(std::mt19937 & rng);
5050

51+
//
52+
// Model file parsing
53+
//
54+
55+
#define FILE_MAGIC_UNVERSIONED 0x67676d6c // pre-versioned files
56+
#define FILE_MAGIC 0x67676d66 // 'ggmf' in hex
57+
#define FILE_VERSION 1
58+
5159
//
5260
// Vocab utils
5361
//

0 commit comments

Comments
 (0)