Skip to content

Commit 7b5cf71

Browse files
committed
handle gguf already containing renamed diffusion tensors prefix
1 parent 0d06e95 commit 7b5cf71

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

model_adapter.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,39 @@ void print_tok_vec(std::vector<float> &embd)
8282
std::cout << "]\n";
8383
}
8484

85+
bool gguf_tensor_exists(const std::string & gguf_filename, std::string tensor_name, bool exactmatch)
86+
{
87+
struct gguf_init_params ggufparams;
88+
ggufparams.no_alloc = true;
89+
ggufparams.ctx = NULL;
90+
struct gguf_context * ctx = gguf_init_from_file(gguf_filename.c_str(), ggufparams);
91+
if (!ctx) return false;
92+
93+
bool found = false;
94+
95+
int n_tensors = gguf_get_n_tensors(ctx);
96+
for (int i = 0; i < n_tensors; i++) {
97+
std::string curr_name = gguf_get_tensor_name(ctx, i);
98+
if(exactmatch)
99+
{
100+
if (curr_name == tensor_name) {
101+
found = true;
102+
break;
103+
}
104+
}
105+
else
106+
{
107+
if (curr_name.find(tensor_name) != std::string::npos) {
108+
found = true;
109+
break;
110+
}
111+
}
112+
}
113+
114+
gguf_free(ctx);
115+
return found;
116+
}
117+
85118
//return val: 0=fail, 1=(original ggml, alpaca), 2=(ggmf), 3=(ggjt)
86119
FileFormat check_file_format(const std::string & fname, FileFormatExtraMeta * fileformatmeta)
87120
{

model_adapter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ FileFormat check_file_format(const std::string & fname, FileFormatExtraMeta * fi
131131
void ContextFastForward(std::vector<int> &current_context_tokens, std::vector<int> &embd_inp,
132132
int &n_past, std::vector<int> &last_n_tokens, const int nctx, std::vector<int> &smartcontext,
133133
const bool useSmartContext, const bool requireFullSubset);
134+
bool gguf_tensor_exists(const std::string & filename, std::string tensor_name, bool exactmatch);
134135

135136
size_t gpttype_calc_new_state_kv();
136137
size_t gpttype_calc_new_state_tokencount();

otherarch/sdcpp/sdtype_adapter.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,13 @@ bool sdtype_load_model(const sd_load_model_inputs inputs) {
233233
bool endswithgguf = (sd_params->model_path.rfind(".gguf") == sd_params->model_path.size() - 5);
234234
if(sd_params->t5xxl_path!="" && endswithgguf)
235235
{
236-
printf("\nSwap to Diffusion Model Path:%s",sd_params->model_path.c_str());
237-
sd_params->diffusion_model_path = sd_params->model_path;
238-
sd_params->model_path = "";
236+
//extra check - make sure there is no diffusion model prefix already inside!
237+
if(!gguf_tensor_exists(sd_params->model_path,"model.diffusion_model.",false))
238+
{
239+
printf("\nSwap to Diffusion Model Path:%s",sd_params->model_path.c_str());
240+
sd_params->diffusion_model_path = sd_params->model_path;
241+
sd_params->model_path = "";
242+
}
239243
}
240244

241245
sddebugmode = inputs.debugmode;

0 commit comments

Comments
 (0)