mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-28 11:11:21 +00:00
Feat: Support ClipProj-MiniMax-H3 (no mlp)
This commit is contained in:
parent
79dc7ca907
commit
23f71cd301
2 changed files with 104 additions and 1 deletions
|
|
@ -25,6 +25,7 @@
|
|||
#include "model/te/ltx_connector.hpp"
|
||||
#include "model/te/ming_image_te.hpp"
|
||||
#include "model/te/ph3_image_connector.hpp"
|
||||
#include "model/te/clipproj_connector.hpp"
|
||||
#include "model/te/t5.hpp"
|
||||
#include "model_loader.h"
|
||||
#include "tokenizers/sensenova_u1_tokenizer.h"
|
||||
|
|
@ -1984,6 +1985,7 @@ struct LLMEmbedder : public Conditioner {
|
|||
std::shared_ptr<LLM::LLMRunner> llm;
|
||||
std::shared_ptr<T5Runner> byt5;
|
||||
std::shared_ptr<PH3ImageTextConnectorRunner> ph3_connector;
|
||||
std::shared_ptr<ClipProjConnectorRunner> clipproj_connector;
|
||||
|
||||
bool supports_prompt_images() const override {
|
||||
return llm->supports_prompt_images();
|
||||
|
|
@ -2066,11 +2068,14 @@ struct LLMEmbedder : public Conditioner {
|
|||
weight_manager);
|
||||
if (sd_version_is_minimax_h3(version)) {
|
||||
const bool has_ph3_connector = tensor_storage_map.count(std::string(PH3ImageTextConnectorRunner::prefix) + ".proj.weight") != 0;
|
||||
const bool has_clipproj_connector = tensor_storage_map.count(std::string(ClipProjConnectorRunner::prefix) + ".W") != 0;
|
||||
if (has_ph3_connector) {
|
||||
if (llm->config.num_layers != 36 || llm->config.hidden_size != 2560) {
|
||||
throw std::runtime_error("The P-H3-Image text connector requires the Qwen3-VL-4B text encoder");
|
||||
}
|
||||
ph3_connector = std::make_shared<PH3ImageTextConnectorRunner>(backend, tensor_storage_map, weight_manager);
|
||||
} else if(has_clipproj_connector){
|
||||
clipproj_connector = std::make_shared<ClipProjConnectorRunner>(backend, llm->config.hidden_size, tensor_storage_map, weight_manager);
|
||||
} else if (llm->config.hidden_size == 2560) {
|
||||
throw std::runtime_error(
|
||||
sd_format("%s with Qwen3-VL-4B requires the P-H3-Image text connector; "
|
||||
|
|
@ -2120,6 +2125,9 @@ struct LLMEmbedder : public Conditioner {
|
|||
if (ph3_connector) {
|
||||
ph3_connector->get_param_tensors(tensors);
|
||||
}
|
||||
if(clipproj_connector){
|
||||
clipproj_connector->get_param_tensors(tensors);
|
||||
}
|
||||
if (byt5) {
|
||||
byt5->get_param_tensors(tensors, "text_encoders.t5xxl.transformer");
|
||||
}
|
||||
|
|
@ -2134,6 +2142,9 @@ struct LLMEmbedder : public Conditioner {
|
|||
if (ph3_connector) {
|
||||
ph3_connector->set_max_graph_vram_bytes(max_vram_bytes);
|
||||
}
|
||||
if (clipproj_connector) {
|
||||
clipproj_connector->set_max_graph_vram_bytes(max_vram_bytes);
|
||||
}
|
||||
if (byt5) {
|
||||
byt5->set_max_graph_vram_bytes(max_vram_bytes);
|
||||
}
|
||||
|
|
@ -2144,6 +2155,9 @@ struct LLMEmbedder : public Conditioner {
|
|||
if (ph3_connector) {
|
||||
ph3_connector->set_runtime_backends(backends);
|
||||
}
|
||||
if (clipproj_connector) {
|
||||
clipproj_connector->set_runtime_backends(backends);
|
||||
}
|
||||
if (byt5) {
|
||||
byt5->set_runtime_backends(backends);
|
||||
}
|
||||
|
|
@ -2179,6 +2193,9 @@ struct LLMEmbedder : public Conditioner {
|
|||
if (ph3_connector) {
|
||||
ph3_connector->set_flash_attention_enabled(enabled);
|
||||
}
|
||||
if (clipproj_connector) {
|
||||
clipproj_connector->set_flash_attention_enabled(enabled);
|
||||
}
|
||||
if (byt5) {
|
||||
byt5->set_flash_attention_enabled(enabled);
|
||||
}
|
||||
|
|
@ -2189,6 +2206,9 @@ struct LLMEmbedder : public Conditioner {
|
|||
if (ph3_connector) {
|
||||
ph3_connector->set_scale_overrides(linear_scale, attn_scale);
|
||||
}
|
||||
if (clipproj_connector) {
|
||||
clipproj_connector->set_scale_overrides(linear_scale, attn_scale);
|
||||
}
|
||||
if (byt5) {
|
||||
byt5->set_scale_overrides(linear_scale, attn_scale);
|
||||
}
|
||||
|
|
@ -2198,6 +2218,9 @@ struct LLMEmbedder : public Conditioner {
|
|||
if (ph3_connector) {
|
||||
ph3_connector->set_weight_adapter(adapter);
|
||||
}
|
||||
if (clipproj_connector) {
|
||||
clipproj_connector->set_weight_adapter(adapter);
|
||||
}
|
||||
if (llm) {
|
||||
llm->set_weight_adapter(adapter);
|
||||
}
|
||||
|
|
@ -2210,6 +2233,9 @@ struct LLMEmbedder : public Conditioner {
|
|||
if (ph3_connector) {
|
||||
ph3_connector->runner_end();
|
||||
}
|
||||
if (clipproj_connector) {
|
||||
clipproj_connector->runner_end();
|
||||
}
|
||||
if (llm) {
|
||||
llm->runner_end();
|
||||
}
|
||||
|
|
@ -2467,6 +2493,11 @@ struct LLMEmbedder : public Conditioner {
|
|||
if (hidden_states.empty()) {
|
||||
return {};
|
||||
}
|
||||
} else if(clipproj_connector){
|
||||
hidden_states = clipproj_connector->compute(n_threads, hidden_states);
|
||||
if (hidden_states.empty()) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
hidden_states = apply_token_weights(std::move(hidden_states), weights);
|
||||
GGML_ASSERT(hidden_states.shape()[1] > prompt_template_encode_start_idx);
|
||||
|
|
@ -2631,7 +2662,7 @@ struct LLMEmbedder : public Conditioner {
|
|||
prompt = system_prefix("");
|
||||
prompt_template_encode_start_idx = 0;
|
||||
// The final tap is normalized; LLMRunner exposes it at num_layers + 1.
|
||||
out_layers = ph3_connector ? std::set<int>{13, 19, 25, 31, 37} : std::set<int>{50};
|
||||
out_layers = ph3_connector ? std::set<int>{13, 19, 25, 31, 37} : std::set<int>{clipproj_connector ? 24 : 50};
|
||||
prompt_attn_range = {0, 0};
|
||||
|
||||
if (llm->enable_vision) {
|
||||
|
|
|
|||
72
src/model/te/clipproj_connector.hpp
Normal file
72
src/model/te/clipproj_connector.hpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#ifndef __SD_MODEL_TE_CLIPPROJ_CONNECTOR_HPP__
|
||||
#define __SD_MODEL_TE_CLIPPROJ_CONNECTOR_HPP__
|
||||
|
||||
#include "core/ggml_runner.h"
|
||||
#include "model/common/ggml_block.hpp"
|
||||
|
||||
struct ClipProjConnector : public UnaryBlock {
|
||||
int64_t input_dim = 2560;
|
||||
int64_t output_dim = 5120;
|
||||
|
||||
ClipProjConnector(int64_t input_dim = 2560, int64_t output_dim = 5120) : input_dim(input_dim), output_dim(output_dim) {}
|
||||
|
||||
void init_params(ggml_context* ctx,
|
||||
const String2TensorStorage& tensor_storage_map = {},
|
||||
const std::string prefix = "") override {
|
||||
enum ggml_type wtype = get_type(prefix + "W", tensor_storage_map, GGML_TYPE_F32);
|
||||
params["W"] = ggml_new_tensor_2d(ctx, wtype, output_dim, input_dim);
|
||||
for (const auto* name : {"mean_in", "std_in"}) {
|
||||
params[name] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, input_dim);
|
||||
}
|
||||
for (const auto* name : {"mean_out", "std_out", "sink_out"}) {
|
||||
params[name] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, output_dim);
|
||||
}
|
||||
}
|
||||
|
||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) override {
|
||||
auto projW = params["W"];
|
||||
projW = ggml_cont(ctx->ggml_ctx, ggml_transpose(ctx->ggml_ctx, projW));
|
||||
x = ggml_div(ctx->ggml_ctx, ggml_sub(ctx->ggml_ctx, x, params["mean_in"]), params["std_in"]);
|
||||
x = ggml_mul_mat(ctx->ggml_ctx, projW, x);
|
||||
x = ggml_add(ctx->ggml_ctx, ggml_mul(ctx->ggml_ctx, x, params["std_out"]), params["mean_out"]);
|
||||
auto sink = ggml_repeat(ctx->ggml_ctx, params["sink_out"], ggml_ext_slice(ctx, x, 1, 0, 1));
|
||||
if (x->ne[1] == 1) {
|
||||
return sink;
|
||||
}
|
||||
return ggml_concat(ctx->ggml_ctx, sink, ggml_ext_slice(ctx, x, 1, 1, x->ne[1]), 1);
|
||||
}
|
||||
};
|
||||
|
||||
struct ClipProjConnectorRunner : public GGMLRunner {
|
||||
ClipProjConnector model;
|
||||
static constexpr const char* prefix = "text_encoders.connector";
|
||||
|
||||
ClipProjConnectorRunner(ggml_backend_t backend,
|
||||
int64_t input_dim,
|
||||
const String2TensorStorage& tensor_storage_map,
|
||||
std::shared_ptr<RunnerWeightManager> weight_manager = nullptr)
|
||||
: GGMLRunner(backend, weight_manager), model(input_dim) {
|
||||
model.init(params_ctx, tensor_storage_map, prefix);
|
||||
}
|
||||
|
||||
std::string get_desc() override {
|
||||
return "clipproj_text_connector";
|
||||
}
|
||||
|
||||
void get_param_tensors(std::map<std::string, ggml_tensor*>& tensors) {
|
||||
model.get_param_tensors(tensors, prefix);
|
||||
}
|
||||
|
||||
sd::Tensor<float> compute(int n_threads, const sd::Tensor<float>& hidden_states) {
|
||||
auto get_graph = [&]() {
|
||||
auto gf = ggml_new_graph(compute_ctx);
|
||||
auto x = make_input(hidden_states);
|
||||
auto runner_ctx = get_context();
|
||||
ggml_build_forward_expand(gf, model.forward(&runner_ctx, x));
|
||||
return gf;
|
||||
};
|
||||
return take_or_empty(GGMLRunner::compute(get_graph, n_threads, false));
|
||||
}
|
||||
};
|
||||
|
||||
#endif // __SD_MODEL_TE_CLIPPROJ_CONNECTOR_HPP__
|
||||
Loading…
Add table
Reference in a new issue