refactor: remove obsolete unused tensor filtering (#1984)

This commit is contained in:
leejet 2026-09-18 23:41:37 +08:00 committed by GitHub
parent 269e726015
commit 656a1354c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 2 additions and 80 deletions

View File

@ -102,7 +102,7 @@ namespace sd::backend_fit {
for (const auto& [name, stored_tensor] : loader.get_tensor_storage_map()) { for (const auto& [name, stored_tensor] : loader.get_tensor_storage_map()) {
TensorStorage ts = stored_tensor; TensorStorage ts = stored_tensor;
ComponentKind kind; ComponentKind kind;
if (is_unused_tensor(ts.name) || !classify_tensor(ts.name, kind)) { if (!classify_tensor(ts.name, kind)) {
continue; continue;
} }
if (ts.expected_type != GGML_TYPE_COUNT) { if (ts.expected_type != GGML_TYPE_COUNT) {

View File

@ -67,7 +67,7 @@ struct LoraModel : public GGMLRunner {
std::map<std::string, ggml_tensor*> scalars; std::map<std::string, ggml_tensor*> scalars;
std::set<std::string> scalar_names; std::set<std::string> scalar_names;
for (const auto& [name, source] : sources) { for (const auto& [name, source] : sources) {
if (is_unused_tensor(name) || (filter && !filter(name))) if (filter && !filter(name))
continue; continue;
const bool scalar = source.nelements() == 1 && (ends_with(name, ".alpha") || ends_with(name, ".scale")); const bool scalar = source.nelements() == 1 && (ends_with(name, ".alpha") || ends_with(name, ".scale"));
auto* tensor = ggml_new_tensor(params_ctx, scalar ? GGML_TYPE_F32 : source.type, source.n_dims, source.ne); auto* tensor = ggml_new_tensor(params_ctx, scalar ? GGML_TYPE_F32 : source.type, source.n_dims, source.ne);

View File

@ -35,52 +35,6 @@
/*================================================= Preprocess ==================================================*/ /*================================================= Preprocess ==================================================*/
const char* unused_tensors[] = {
"betas",
"alphas_cumprod_prev",
"sqrt_alphas_cumprod",
"sqrt_one_minus_alphas_cumprod",
"log_one_minus_alphas_cumprod",
"sqrt_recip_alphas_cumprod",
"sqrt_recipm1_alphas_cumprod",
"posterior_variance",
"posterior_log_variance_clipped",
"posterior_mean_coef1",
"posterior_mean_coef2",
"cond_stage_model.transformer.text_model.embeddings.position_ids",
"cond_stage_model.1.model.text_model.embeddings.position_ids",
"cond_stage_model.transformer.vision_model.embeddings.position_ids",
"cond_stage_model.model.logit_scale",
"conditioner.embedders.0.transformer.text_model.embeddings.position_ids",
"conditioner.embedders.0.model.logit_scale",
"conditioner.embedders.1.model.logit_scale",
"model.diffusion_model.time_embedding.cond_proj.weight",
"unet.time_embedding.cond_proj.weight",
"model_ema.decay",
"model_ema.num_updates",
"model_ema.diffusion_model",
"embedding_manager",
"denoiser.sigmas",
"text_encoders.t5xxl.transformer.encoder.embed_tokens.weight", // only used during training
"ztsnr", // Found in some SDXL vpred models
"edm_vpred.sigma_min", // Found in CosXL
// TODO: find another way to avoid the "unknown tensor" for these two
// "edm_vpred.sigma_max", // Used to detect CosXL
// "v_pred", // Used to detect SDXL vpred models
"text_encoders.llm.output.weight",
"text_encoders.llm.lm_head.",
"language_model.lm_head.",
};
bool is_unused_tensor(const std::string& name) {
for (size_t i = 0; i < sizeof(unused_tensors) / sizeof(const char*); i++) {
if (starts_with(name, unused_tensors[i])) {
return true;
}
}
return false;
}
void f64_to_f32_vec(double* src, float* dst, int64_t n) { void f64_to_f32_vec(double* src, float* dst, int64_t n) {
// support inplace op // support inplace op
for (int64_t i = 0; i < n; i++) { for (int64_t i = 0; i < n; i++) {
@ -284,10 +238,6 @@ bool ModelLoader::init_from_safetensors_file(const std::string& file_path, const
size_t file_index = add_file_path(file_path); size_t file_index = add_file_path(file_path);
for (auto& tensor_storage : tensor_storages) { for (auto& tensor_storage : tensor_storages) {
if (is_unused_tensor(tensor_storage.name)) {
continue;
}
if (!starts_with(tensor_storage.name, prefix)) { if (!starts_with(tensor_storage.name, prefix)) {
tensor_storage.name = prefix + tensor_storage.name; tensor_storage.name = prefix + tensor_storage.name;
} }
@ -356,10 +306,6 @@ bool ModelLoader::init_from_torch_legacy_file(const std::string& file_path, cons
size_t file_index = add_file_path(file_path); size_t file_index = add_file_path(file_path);
for (auto& tensor_storage : tensor_storages) { for (auto& tensor_storage : tensor_storages) {
if (is_unused_tensor(tensor_storage.name)) {
continue;
}
if (!starts_with(tensor_storage.name, prefix)) { if (!starts_with(tensor_storage.name, prefix)) {
tensor_storage.name = prefix + tensor_storage.name; tensor_storage.name = prefix + tensor_storage.name;
} }
@ -674,10 +620,6 @@ SDVersion ModelLoader::get_sd_version() const {
std::map<ggml_type, uint32_t> ModelLoader::get_wtype_stat() const { std::map<ggml_type, uint32_t> ModelLoader::get_wtype_stat() const {
std::map<ggml_type, uint32_t> wtype_stat; std::map<ggml_type, uint32_t> wtype_stat;
for (auto& [name, tensor_storage] : tensor_storage_map) { for (auto& [name, tensor_storage] : tensor_storage_map) {
if (is_unused_tensor(tensor_storage.name)) {
continue;
}
auto iter = wtype_stat.find(tensor_storage.type); auto iter = wtype_stat.find(tensor_storage.type);
if (iter != wtype_stat.end()) { if (iter != wtype_stat.end()) {
iter->second++; iter->second++;
@ -691,10 +633,6 @@ std::map<ggml_type, uint32_t> ModelLoader::get_wtype_stat() const {
std::map<ggml_type, uint32_t> ModelLoader::get_conditioner_wtype_stat() const { std::map<ggml_type, uint32_t> ModelLoader::get_conditioner_wtype_stat() const {
std::map<ggml_type, uint32_t> wtype_stat; std::map<ggml_type, uint32_t> wtype_stat;
for (auto& [name, tensor_storage] : tensor_storage_map) { for (auto& [name, tensor_storage] : tensor_storage_map) {
if (is_unused_tensor(tensor_storage.name)) {
continue;
}
if ((tensor_storage.name.find("text_encoders") == std::string::npos && if ((tensor_storage.name.find("text_encoders") == std::string::npos &&
tensor_storage.name.find("cond_stage_model") == std::string::npos && tensor_storage.name.find("cond_stage_model") == std::string::npos &&
tensor_storage.name.find("te.text_model.") == std::string::npos && tensor_storage.name.find("te.text_model.") == std::string::npos &&
@ -715,10 +653,6 @@ std::map<ggml_type, uint32_t> ModelLoader::get_conditioner_wtype_stat() const {
std::map<ggml_type, uint32_t> ModelLoader::get_diffusion_model_wtype_stat() const { std::map<ggml_type, uint32_t> ModelLoader::get_diffusion_model_wtype_stat() const {
std::map<ggml_type, uint32_t> wtype_stat; std::map<ggml_type, uint32_t> wtype_stat;
for (auto& [name, tensor_storage] : tensor_storage_map) { for (auto& [name, tensor_storage] : tensor_storage_map) {
if (is_unused_tensor(tensor_storage.name)) {
continue;
}
if (tensor_storage.name.find("model.diffusion_model.") == std::string::npos && tensor_storage.name.find("unet.") == std::string::npos) { if (tensor_storage.name.find("model.diffusion_model.") == std::string::npos && tensor_storage.name.find("unet.") == std::string::npos) {
continue; continue;
} }
@ -736,10 +670,6 @@ std::map<ggml_type, uint32_t> ModelLoader::get_diffusion_model_wtype_stat() cons
std::map<ggml_type, uint32_t> ModelLoader::get_vae_wtype_stat() const { std::map<ggml_type, uint32_t> ModelLoader::get_vae_wtype_stat() const {
std::map<ggml_type, uint32_t> wtype_stat; std::map<ggml_type, uint32_t> wtype_stat;
for (auto& [name, tensor_storage] : tensor_storage_map) { for (auto& [name, tensor_storage] : tensor_storage_map) {
if (is_unused_tensor(tensor_storage.name)) {
continue;
}
if (tensor_storage.name.find("vae.") == std::string::npos && if (tensor_storage.name.find("vae.") == std::string::npos &&
tensor_storage.name.find("first_stage_model") == std::string::npos) { tensor_storage.name.find("first_stage_model") == std::string::npos) {
continue; continue;
@ -823,9 +753,6 @@ void ModelLoader::process_model_files(bool enable_mmap, bool writable_mmap) {
std::vector<TensorStorage> processed_tensor_storages; std::vector<TensorStorage> processed_tensor_storages;
for (const auto& [name, tensor_storage] : tensor_storage_map) { for (const auto& [name, tensor_storage] : tensor_storage_map) {
if (is_unused_tensor(tensor_storage.name)) {
continue;
}
processed_tensor_storages.push_back(tensor_storage); processed_tensor_storages.push_back(tensor_storage);
} }
@ -1551,9 +1478,6 @@ int64_t ModelLoader::get_params_mem_size(ggml_backend_t backend, ggml_type type)
int64_t mem_size = 0; int64_t mem_size = 0;
std::vector<TensorStorage> processed_tensor_storages; std::vector<TensorStorage> processed_tensor_storages;
for (auto [name, tensor_storage] : tensor_storage_map) { for (auto [name, tensor_storage] : tensor_storage_map) {
if (is_unused_tensor(tensor_storage.name)) {
continue;
}
if (tensor_should_be_converted(tensor_storage, type)) { if (tensor_should_be_converted(tensor_storage, type)) {
tensor_storage.type = type; tensor_storage.type = type;
} }

View File

@ -28,8 +28,6 @@ struct MmapTensorStore {
std::shared_ptr<struct ggml_backend_buffer> mmbuffer; std::shared_ptr<struct ggml_backend_buffer> mmbuffer;
}; };
bool is_unused_tensor(const std::string& name);
class ModelLoader { class ModelLoader {
public: public:
using FileId = uint64_t; using FileId = uint64_t;