Skip to content

Commit db6f479

Browse files
authored
feat: add wtype stat (#899)
1 parent b25785b commit db6f479

File tree

3 files changed

+60
-71
lines changed

3 files changed

+60
-71
lines changed

model.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,24 +1892,25 @@ SDVersion ModelLoader::get_sd_version() {
18921892
return VERSION_COUNT;
18931893
}
18941894

1895-
ggml_type ModelLoader::get_sd_wtype() {
1895+
std::map<ggml_type, uint32_t> ModelLoader::get_wtype_stat() {
1896+
std::map<ggml_type, uint32_t> wtype_stat;
18961897
for (auto& tensor_storage : tensor_storages) {
18971898
if (is_unused_tensor(tensor_storage.name)) {
18981899
continue;
18991900
}
19001901

1901-
if (ggml_is_quantized(tensor_storage.type)) {
1902-
return tensor_storage.type;
1903-
}
1904-
1905-
if (tensor_should_be_converted(tensor_storage, GGML_TYPE_Q4_K)) {
1906-
return tensor_storage.type;
1902+
auto iter = wtype_stat.find(tensor_storage.type);
1903+
if (iter != wtype_stat.end()) {
1904+
iter->second++;
1905+
} else {
1906+
wtype_stat[tensor_storage.type] = 1;
19071907
}
19081908
}
1909-
return GGML_TYPE_COUNT;
1909+
return wtype_stat;
19101910
}
19111911

1912-
ggml_type ModelLoader::get_conditioner_wtype() {
1912+
std::map<ggml_type, uint32_t> ModelLoader::get_conditioner_wtype_stat() {
1913+
std::map<ggml_type, uint32_t> wtype_stat;
19131914
for (auto& tensor_storage : tensor_storages) {
19141915
if (is_unused_tensor(tensor_storage.name)) {
19151916
continue;
@@ -1922,18 +1923,18 @@ ggml_type ModelLoader::get_conditioner_wtype() {
19221923
continue;
19231924
}
19241925

1925-
if (ggml_is_quantized(tensor_storage.type)) {
1926-
return tensor_storage.type;
1927-
}
1928-
1929-
if (tensor_should_be_converted(tensor_storage, GGML_TYPE_Q4_K)) {
1930-
return tensor_storage.type;
1926+
auto iter = wtype_stat.find(tensor_storage.type);
1927+
if (iter != wtype_stat.end()) {
1928+
iter->second++;
1929+
} else {
1930+
wtype_stat[tensor_storage.type] = 1;
19311931
}
19321932
}
1933-
return GGML_TYPE_COUNT;
1933+
return wtype_stat;
19341934
}
19351935

1936-
ggml_type ModelLoader::get_diffusion_model_wtype() {
1936+
std::map<ggml_type, uint32_t> ModelLoader::get_diffusion_model_wtype_stat() {
1937+
std::map<ggml_type, uint32_t> wtype_stat;
19371938
for (auto& tensor_storage : tensor_storages) {
19381939
if (is_unused_tensor(tensor_storage.name)) {
19391940
continue;
@@ -1943,18 +1944,18 @@ ggml_type ModelLoader::get_diffusion_model_wtype() {
19431944
continue;
19441945
}
19451946

1946-
if (ggml_is_quantized(tensor_storage.type)) {
1947-
return tensor_storage.type;
1948-
}
1949-
1950-
if (tensor_should_be_converted(tensor_storage, GGML_TYPE_Q4_K)) {
1951-
return tensor_storage.type;
1947+
auto iter = wtype_stat.find(tensor_storage.type);
1948+
if (iter != wtype_stat.end()) {
1949+
iter->second++;
1950+
} else {
1951+
wtype_stat[tensor_storage.type] = 1;
19521952
}
19531953
}
1954-
return GGML_TYPE_COUNT;
1954+
return wtype_stat;
19551955
}
19561956

1957-
ggml_type ModelLoader::get_vae_wtype() {
1957+
std::map<ggml_type, uint32_t> ModelLoader::get_vae_wtype_stat() {
1958+
std::map<ggml_type, uint32_t> wtype_stat;
19581959
for (auto& tensor_storage : tensor_storages) {
19591960
if (is_unused_tensor(tensor_storage.name)) {
19601961
continue;
@@ -1965,15 +1966,14 @@ ggml_type ModelLoader::get_vae_wtype() {
19651966
continue;
19661967
}
19671968

1968-
if (ggml_is_quantized(tensor_storage.type)) {
1969-
return tensor_storage.type;
1970-
}
1971-
1972-
if (tensor_should_be_converted(tensor_storage, GGML_TYPE_Q4_K)) {
1973-
return tensor_storage.type;
1969+
auto iter = wtype_stat.find(tensor_storage.type);
1970+
if (iter != wtype_stat.end()) {
1971+
iter->second++;
1972+
} else {
1973+
wtype_stat[tensor_storage.type] = 1;
19741974
}
19751975
}
1976-
return GGML_TYPE_COUNT;
1976+
return wtype_stat;
19771977
}
19781978

19791979
void ModelLoader::set_wtype_override(ggml_type wtype, std::string prefix) {

model.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ class ModelLoader {
259259
bool init_from_file(const std::string& file_path, const std::string& prefix = "");
260260
bool model_is_unet();
261261
SDVersion get_sd_version();
262-
ggml_type get_sd_wtype();
263-
ggml_type get_conditioner_wtype();
264-
ggml_type get_diffusion_model_wtype();
265-
ggml_type get_vae_wtype();
262+
std::map<ggml_type, uint32_t> get_wtype_stat();
263+
std::map<ggml_type, uint32_t> get_conditioner_wtype_stat();
264+
std::map<ggml_type, uint32_t> get_diffusion_model_wtype_stat();
265+
std::map<ggml_type, uint32_t> get_vae_wtype_stat();
266266
void set_wtype_override(ggml_type wtype, std::string prefix = "");
267267
bool load_tensors(on_new_tensor_cb_t on_new_tensor_cb, int n_threads = 0);
268268
bool load_tensors(std::map<std::string, struct ggml_tensor*>& tensors,

stable-diffusion.cpp

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ class StableDiffusionGGML {
8686
ggml_backend_t clip_backend = NULL;
8787
ggml_backend_t control_net_backend = NULL;
8888
ggml_backend_t vae_backend = NULL;
89-
ggml_type model_wtype = GGML_TYPE_COUNT;
90-
ggml_type conditioner_wtype = GGML_TYPE_COUNT;
91-
ggml_type diffusion_model_wtype = GGML_TYPE_COUNT;
92-
ggml_type vae_wtype = GGML_TYPE_COUNT;
9389

9490
SDVersion version;
9591
bool vae_decode_only = false;
@@ -294,37 +290,33 @@ class StableDiffusionGGML {
294290
ggml_type wtype = (int)sd_ctx_params->wtype < std::min<int>(SD_TYPE_COUNT, GGML_TYPE_COUNT)
295291
? (ggml_type)sd_ctx_params->wtype
296292
: GGML_TYPE_COUNT;
297-
if (wtype == GGML_TYPE_COUNT) {
298-
model_wtype = model_loader.get_sd_wtype();
299-
if (model_wtype == GGML_TYPE_COUNT) {
300-
model_wtype = GGML_TYPE_F32;
301-
LOG_WARN("can not get mode wtype frome weight, use f32");
302-
}
303-
conditioner_wtype = model_loader.get_conditioner_wtype();
304-
if (conditioner_wtype == GGML_TYPE_COUNT) {
305-
conditioner_wtype = wtype;
306-
}
307-
diffusion_model_wtype = model_loader.get_diffusion_model_wtype();
308-
if (diffusion_model_wtype == GGML_TYPE_COUNT) {
309-
diffusion_model_wtype = wtype;
310-
}
311-
vae_wtype = model_loader.get_vae_wtype();
312-
313-
if (vae_wtype == GGML_TYPE_COUNT) {
314-
vae_wtype = wtype;
315-
}
316-
} else {
317-
model_wtype = wtype;
318-
conditioner_wtype = wtype;
319-
diffusion_model_wtype = wtype;
320-
vae_wtype = wtype;
293+
if (wtype != GGML_TYPE_COUNT) {
321294
model_loader.set_wtype_override(wtype);
322295
}
323296

324-
LOG_INFO("Weight type: %s", ggml_type_name(model_wtype));
325-
LOG_INFO("Conditioner weight type: %s", ggml_type_name(conditioner_wtype));
326-
LOG_INFO("Diffusion model weight type: %s", ggml_type_name(diffusion_model_wtype));
327-
LOG_INFO("VAE weight type: %s", ggml_type_name(vae_wtype));
297+
std::map<ggml_type, uint32_t> wtype_stat = model_loader.get_wtype_stat();
298+
std::map<ggml_type, uint32_t> conditioner_wtype_stat = model_loader.get_conditioner_wtype_stat();
299+
std::map<ggml_type, uint32_t> diffusion_model_wtype_stat = model_loader.get_diffusion_model_wtype_stat();
300+
std::map<ggml_type, uint32_t> vae_wtype_stat = model_loader.get_vae_wtype_stat();
301+
302+
auto wtype_stat_to_str = [](const std::map<ggml_type, uint32_t>& m, int key_width = 8, int value_width = 5) -> std::string {
303+
std::ostringstream oss;
304+
bool first = true;
305+
for (const auto& [type, count] : m) {
306+
if (!first)
307+
oss << "|";
308+
first = false;
309+
oss << std::right << std::setw(key_width) << ggml_type_name(type)
310+
<< ": "
311+
<< std::left << std::setw(value_width) << count;
312+
}
313+
return oss.str();
314+
};
315+
316+
LOG_INFO("Weight type stat: %s", wtype_stat_to_str(wtype_stat).c_str());
317+
LOG_INFO("Conditioner weight type stat: %s", wtype_stat_to_str(conditioner_wtype_stat).c_str());
318+
LOG_INFO("Diffusion model weight type stat: %s", wtype_stat_to_str(diffusion_model_wtype_stat).c_str());
319+
LOG_INFO("VAE weight type stat: %s", wtype_stat_to_str(vae_wtype_stat).c_str());
328320

329321
LOG_DEBUG("ggml tensor size = %d bytes", (int)sizeof(ggml_tensor));
330322

@@ -938,9 +930,6 @@ class StableDiffusionGGML {
938930
}
939931

940932
void apply_loras(const std::unordered_map<std::string, float>& lora_state) {
941-
if (lora_state.size() > 0 && model_wtype != GGML_TYPE_F16 && model_wtype != GGML_TYPE_F32) {
942-
LOG_WARN("In quantized models when applying LoRA, the images have poor quality.");
943-
}
944933
std::unordered_map<std::string, float> lora_state_diff;
945934
for (auto& kv : lora_state) {
946935
const std::string& lora_name = kv.first;

0 commit comments

Comments
 (0)