mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-26 23:29:53 +00:00
feat: support -mlp variants of ClipProj-MiniMax-H3 (#2424)
This commit is contained in:
parent
514e786d84
commit
439e77a52e
4 changed files with 96 additions and 21 deletions
|
|
@ -70,8 +70,7 @@ small learned matrix that replaces the 32B encoder:
|
|||
```
|
||||
|
||||
The `mmh3-4b-*` matrices expect a Qwen3-VL-4B encoder and the `mmh3-8b-*`
|
||||
matrices a Qwen3-VL-8B encoder; match the two. Only the linear matrices are
|
||||
supported, not the `-mlp` residual variants. ClipProj is a community proof of
|
||||
matrices a Qwen3-VL-8B encoder; match the two. ClipProj is a community proof of
|
||||
concept: outputs are close to, not identical to, the 32B encoder. See the
|
||||
[model card](https://huggingface.co/NicoLab28/ClipProj-MiniMax-H3) for
|
||||
measurements and limitations.
|
||||
|
|
|
|||
|
|
@ -31,8 +31,7 @@ small encoder with `--llm` and the matching matrix with
|
|||
`--embeddings-connectors`. It needs much less memory (about 4.5 GB with a 4B
|
||||
INT8 encoder, versus 18 GB for the 32B encoder in Q4_K_M). The `mmh3-4b-*`
|
||||
matrices expect a Qwen3-VL-4B encoder and the `mmh3-8b-*` matrices a
|
||||
Qwen3-VL-8B encoder; match the two. Only the linear matrices are supported,
|
||||
not the `-mlp` residual variants. Reference-image editing needs the encoder's
|
||||
Qwen3-VL-8B encoder; match the two. Reference-image editing needs the encoder's
|
||||
vision tower in the text encoder or in `--llm_vision`. ClipProj is a community
|
||||
proof of concept: outputs are close to, not identical to, the 32B encoder. See
|
||||
the [model card](https://huggingface.co/NicoLab28/ClipProj-MiniMax-H3) for
|
||||
|
|
|
|||
|
|
@ -2068,22 +2068,27 @@ 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;
|
||||
const bool has_clipproj_connector = tensor_storage_map.count(std::string(ClipProjConnectorRunner::prefix) + ".mean_in") != 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);
|
||||
clipproj_connector = std::make_shared<ClipProjConnectorRunner>(backend, llm->config.hidden_size, 5120, 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; "
|
||||
sd_format("%s with Qwen3-VL-4B requires a text connector; pass the P-H3-Image text connector "
|
||||
"or a ClipProj projection with --embeddings-connectors, or use the MiniMax-H3 Qwen3-VL-32B text encoder",
|
||||
sd_version_name(version)));
|
||||
} else if (llm->config.hidden_size == 4096) {
|
||||
throw std::runtime_error(
|
||||
sd_format("%s with Qwen3-VL-8B requires a ClipProj text connector; "
|
||||
"pass it with --embeddings-connectors or use the MiniMax-H3 Qwen3-VL-32B text encoder",
|
||||
sd_version_name(version)));
|
||||
} else if (llm->config.hidden_size != 5120 || (llm->config.num_layers != 50 && llm->config.num_layers != 64)) {
|
||||
throw std::runtime_error(
|
||||
sd_format("%s requires the MiniMax-H3 Qwen3-VL-32B text encoder or Qwen3-VL-4B with the P-H3-Image text connector",
|
||||
sd_format("%s requires the MiniMax-H3 Qwen3-VL-32B text encoder or Qwen3-VL-4B with the P-H3-Image (or a ClipProj) text connector, or Qwen3-VL-8B with a ClipProj projection",
|
||||
sd_version_name(version)));
|
||||
}
|
||||
}
|
||||
|
|
@ -2662,6 +2667,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.
|
||||
// clipproj taps out at layer 24 in all versions so far, so it should be fine to hardcode it there
|
||||
out_layers = ph3_connector ? std::set<int>{13, 19, 25, 31, 37} : std::set<int>{clipproj_connector ? 24 : 50};
|
||||
prompt_attn_range = {0, 0};
|
||||
|
||||
|
|
|
|||
|
|
@ -4,35 +4,105 @@
|
|||
#include "core/ggml_runner.h"
|
||||
#include "model/common/ggml_block.hpp"
|
||||
|
||||
struct MLP : public UnaryBlock {
|
||||
MLP(int64_t input_dim, int64_t hidden_dim, int64_t output_dim) {
|
||||
blocks["0"] = std::shared_ptr<GGMLBlock>(new Linear(input_dim, hidden_dim));
|
||||
blocks["2"] = std::shared_ptr<GGMLBlock>(new Linear(hidden_dim, output_dim));
|
||||
}
|
||||
|
||||
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x) {
|
||||
auto layer0 = std::dynamic_pointer_cast<Linear>(blocks["0"]);
|
||||
auto layer2 = std::dynamic_pointer_cast<Linear>(blocks["2"]);
|
||||
|
||||
x = layer0->forward(ctx, x);
|
||||
x = ggml_ext_gelu(ctx, x, true);
|
||||
x = layer2->forward(ctx, x);
|
||||
return x;
|
||||
}
|
||||
};
|
||||
|
||||
struct ClipProjConnector : public UnaryBlock {
|
||||
int64_t input_dim = 2560;
|
||||
int64_t output_dim = 5120;
|
||||
bool has_linear = true;
|
||||
bool has_mlp = false;
|
||||
bool has_sink = true;
|
||||
|
||||
ClipProjConnector(int64_t input_dim = 2560, int64_t output_dim = 5120) : input_dim(input_dim), output_dim(output_dim) {}
|
||||
int64_t mlp_hidden = 0;
|
||||
|
||||
void config_from_weights(const String2TensorStorage& tensor_storage_map = {}) {
|
||||
static constexpr const char* prefix = "text_encoders.connector";
|
||||
|
||||
has_linear = false;
|
||||
has_mlp = false;
|
||||
has_sink = false;
|
||||
for (const auto& [name, tensor_storage] : tensor_storage_map) {
|
||||
if (!starts_with(name, prefix)) {
|
||||
continue;
|
||||
}
|
||||
if (name.find("W") != std::string::npos) {
|
||||
has_linear = true;
|
||||
}
|
||||
if (name.find("mlp.0.bias") != std::string::npos) {
|
||||
has_mlp = true;
|
||||
mlp_hidden = tensor_storage.ne[0];
|
||||
}
|
||||
// optional in the original code, but always present in the model weights...
|
||||
if (name.find("sink_out") != std::string::npos) {
|
||||
has_sink = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ClipProjConnector(int64_t input_dim = 2560, int64_t output_dim = 5120, const String2TensorStorage& tensor_storage_map = {}) : input_dim(input_dim), output_dim(output_dim) {
|
||||
config_from_weights(tensor_storage_map);
|
||||
if (has_mlp) {
|
||||
if(mlp_hidden <= 0){
|
||||
throw std::runtime_error("Cannot find hidden size for ClipProj mlp");
|
||||
}
|
||||
blocks["mlp"] = std::shared_ptr<GGMLBlock>(new MLP(input_dim, mlp_hidden, output_dim));
|
||||
}
|
||||
}
|
||||
|
||||
void init_params(ggml_context* ctx,
|
||||
const String2TensorStorage& tensor_storage_map = {},
|
||||
const std::string prefix = "") override {
|
||||
params["W"] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, output_dim, input_dim);
|
||||
if (has_linear) {
|
||||
params["W"] = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 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"}) {
|
||||
for (const auto* name : {"mean_out", "std_out"}) {
|
||||
params[name] = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, output_dim);
|
||||
}
|
||||
if (has_sink) {
|
||||
params["sink_out"] = 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;
|
||||
auto x_std = ggml_div(ctx->ggml_ctx, ggml_sub(ctx->ggml_ctx, x, params["mean_in"]), params["std_in"]);
|
||||
if (has_linear) {
|
||||
auto projW = params["W"];
|
||||
projW = ggml_cont(ctx->ggml_ctx, ggml_transpose(ctx->ggml_ctx, projW));
|
||||
x = ggml_mul_mat(ctx->ggml_ctx, projW, x_std);
|
||||
}
|
||||
if (has_mlp) {
|
||||
auto mlp = std::dynamic_pointer_cast<MLP>(blocks["mlp"]);
|
||||
auto x_mlp = mlp->forward(ctx, x_std);
|
||||
x = has_linear ? ggml_add_inplace(ctx->ggml_ctx, x, x_mlp) : x_mlp;
|
||||
}
|
||||
x = ggml_add(ctx->ggml_ctx, ggml_mul(ctx->ggml_ctx, x, params["std_out"]), params["mean_out"]);
|
||||
if(has_sink){
|
||||
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);
|
||||
} else {
|
||||
return x;
|
||||
}
|
||||
return ggml_concat(ctx->ggml_ctx, sink, ggml_ext_slice(ctx, x, 1, 1, x->ne[1]), 1);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -42,9 +112,10 @@ struct ClipProjConnectorRunner : public GGMLRunner {
|
|||
|
||||
ClipProjConnectorRunner(ggml_backend_t backend,
|
||||
int64_t input_dim,
|
||||
int64_t output_dim,
|
||||
const String2TensorStorage& tensor_storage_map,
|
||||
std::shared_ptr<RunnerWeightManager> weight_manager = nullptr)
|
||||
: GGMLRunner(backend, weight_manager), model(input_dim) {
|
||||
: GGMLRunner(backend, weight_manager), model(input_dim, output_dim, tensor_storage_map) {
|
||||
model.init(params_ctx, tensor_storage_map, prefix);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue