diff --git a/docs/models/minimax_h3.md b/docs/models/minimax_h3.md index 9b71cf189d..0ca4da6c93 100644 --- a/docs/models/minimax_h3.md +++ b/docs/models/minimax_h3.md @@ -12,7 +12,7 @@ Pass the four MiniMax-H3 components separately: - `--diffusion-model`: MiniMax-H3 diffusion transformer - `--vae`: MiniMax-H3 video VAE - `--audio-vae`: MiniMax-H3 audio VAE -- `--llm`: the MiniMax-H3 Qwen3-VL-32B text encoder, or Qwen3-VL-4B with the P-H3 text connector +- `--llm`: the MiniMax-H3 Qwen3-VL-32B text encoder, Qwen3-VL-4B with the P-H3 text connector, or Qwen3-VL-4B/8B with the ClipProj projection For the 32B option, use the MiniMax-H3 variant: Qwen3-VL-32B truncated to 50 language layers and exported without the final language-model normalization. @@ -51,10 +51,35 @@ or: --embeddings-connectors ../models/text_encoders/p-h3-image_text_connector.safetensors ``` +or use a plain Qwen3-VL-4B or Qwen3-VL-8B with the +[ClipProj projection](https://huggingface.co/NicoLab28/ClipProj-MiniMax-H3), a +small learned matrix that replaces the 32B encoder: + +**Windows (PowerShell) arguments** + +```text +--llm ..\models\text_encoders\qwen3vl_4b.gguf ` + --embeddings-connectors ..\models\text_encoders\mmh3-4b-ClipProj-v3.1.safetensors +``` + +**Linux/macOS arguments** + +```text +--llm ../models/text_encoders/qwen3vl_4b.gguf \ + --embeddings-connectors ../models/text_encoders/mmh3-4b-ClipProj-v3.1.safetensors +``` + +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 +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. + Reference-image and reference-video conditioning needs the matching encoder's -Qwen3-VL vision tower, including the three DeepStack mergers. If stored -separately, pass it with `--llm_vision`. Keep using the MiniMax-H3 video and audio -VAEs with either text encoder. +Qwen3-VL vision tower; the official 32B encoder includes the three DeepStack mergers. +If stored separately, pass it with `--llm_vision`. Keep using the MiniMax-H3 +video and audio VAEs with any text encoder. In DGL, use `load_text_encoder` with `family = "minimax_h3"`. For a separate 4B connector or vision projector, pass `connectors_path` or `vision_path` to @@ -76,6 +101,7 @@ are detected from their weights. | minimax_h3_fl2va/minimax_h3_ref2va | [Safetensors](https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/diffusion_models)
[GGUF](https://huggingface.co/leejet/MiniMax-H3-GGUF/tree/main) | `--diffusion-model` | Required | | qwen3vl_32b_minimax_h3 | [Safetensors](https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/text_encoders)
[GGUF](https://huggingface.co/leejet/MiniMax-H3-GGUF/tree/main) | `--llm` | Use matching text encoder | | P-H3-Image text encoder (Qwen3-VL-4B with the text connector) | [Safetensors](https://huggingface.co/PrunaAI/P-H3-Image-Comfy/tree/main/text_encoders) | `--llm` | Use matching text encoder | +| ClipProj projection (for a plain Qwen3-VL-4B or Qwen3-VL-8B) | [Safetensors](https://huggingface.co/NicoLab28/ClipProj-MiniMax-H3) | `--embeddings-connectors` | Pair with a matching Qwen3-VL-4B/8B `--llm` | | vae | [Safetensors](https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/vae) | `--vae` | Use matching VAE | | audio vae | [Safetensors](https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/vae) | `--audio-vae` | Required | diff --git a/docs/models/minimax_h3_image.md b/docs/models/minimax_h3_image.md index abc2264f4c..75f6171830 100644 --- a/docs/models/minimax_h3_image.md +++ b/docs/models/minimax_h3_image.md @@ -10,7 +10,7 @@ Pass the three MiniMax-H3-Image components separately: - `--diffusion-model`: MiniMax-H3-Image diffusion transformer - `--vae`: MiniMax-H3-Image VAE -- `--llm`: the MiniMax-H3 Qwen3-VL-32B text encoder checkpoint +- `--llm`: the MiniMax-H3 Qwen3-VL-32B text encoder checkpoint, or a Qwen3-VL-4B/8B with a text connector Use the MiniMax-H3 text encoder, including its Qwen3-VL vision tower and DeepStack mergers. An existing MiniMax-H3 text encoder can be reused. If the @@ -25,6 +25,19 @@ with `--llm` and the [separate text connector](https://huggingface.co/PrunaAI/P- with `--embeddings-connectors`. Reference-image editing needs the Qwen3-VL-4B vision tower in the text encoder or in `--llm_vision`. +A [ClipProj projection](https://huggingface.co/NicoLab28/ClipProj-MiniMax-H3) +can replace the 32B encoder with a plain Qwen3-VL-4B or Qwen3-VL-8B: pass the +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 +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 +measurements and limitations. + Use a single-file diffusion checkpoint with the six-step PDD output heads included, such as `minimax_h3_image_pruned_bf16.safetensors` or its INT8 ConvRot version. Use the MiniMax-H3-Image VAE; the MiniMax-H3 video VAE and P-H3-Image @@ -37,6 +50,7 @@ VAE are incompatible. No audio VAE is needed. | minimax_h3_image_pruned | [Safetensors](https://huggingface.co/Comfy-Org/MiniMax-H3-Image/tree/main/diffusion_models) | `--diffusion-model` | Required | | qwen3vl_32b_minimax_h3 | [Safetensors](https://huggingface.co/Comfy-Org/MiniMax-H3-Image/tree/main/text_encoders)
[GGUF](https://huggingface.co/leejet/MiniMax-H3-GGUF/tree/main) | `--llm` | Use matching text encoder | | P-H3-Image text encoder (Qwen3-VL-4B with the text connector) | [Safetensors](https://huggingface.co/PrunaAI/P-H3-Image-Comfy/tree/main/text_encoders) | `--llm` | Use matching text encoder | +| ClipProj projection (for a plain Qwen3-VL-4B or Qwen3-VL-8B) | [Safetensors](https://huggingface.co/NicoLab28/ClipProj-MiniMax-H3) | `--embeddings-connectors` | Pair with a matching Qwen3-VL-4B/8B `--llm` | | vae | [Safetensors](https://huggingface.co/Comfy-Org/MiniMax-H3-Image/tree/main/vae) | `--vae` | Use matching VAE | ## Usage @@ -147,6 +161,23 @@ With the P-H3-Image text encoder, replace the `--llm` argument: --llm ../models/text_encoders/p-h3-image_text_encoder_int8_convrot.safetensors ``` +With a ClipProj projection, replace the `--llm` argument with the small +encoder plus the matching matrix: + +**Windows (PowerShell) arguments** + +```text +--llm ..\models\text_encoders\qwen3vl_4b_bf16.safetensors ` + --embeddings-connectors ..\models\text_encoders\mmh3-4b-ClipProj-v3.1.safetensors +``` + +**Linux/macOS arguments** + +```text +--llm ../models/text_encoders/qwen3vl_4b_bf16.safetensors \ + --embeddings-connectors ../models/text_encoders/mmh3-4b-ClipProj-v3.1.safetensors +``` + Use Euler, CFG 1, and `--sigmas minimax-h3-image` for the model's six-step schedule. The default discrete scheduler also produces this schedule with `--steps 6`. Prompt expansion is not included. diff --git a/src/conditioning/conditioner.hpp b/src/conditioning/conditioner.hpp index 64f8007743..331e33b71d 100644 --- a/src/conditioning/conditioner.hpp +++ b/src/conditioning/conditioner.hpp @@ -20,6 +20,7 @@ #include "core/util.h" #include "model/diffusion/model.hpp" #include "model/te/clip.hpp" +#include "model/te/clipproj_connector.hpp" #include "model/te/llada_image_te.hpp" #include "model/te/llm.hpp" #include "model/te/ltx_connector.hpp" @@ -1984,6 +1985,7 @@ struct LLMEmbedder : public Conditioner { std::shared_ptr llm; std::shared_ptr byt5; std::shared_ptr ph3_connector; + std::shared_ptr clipproj_connector; bool supports_prompt_images() const override { return llm->supports_prompt_images(); @@ -2065,12 +2067,15 @@ struct LLMEmbedder : public Conditioner { enable_vision, 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_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(backend, tensor_storage_map, weight_manager); + } else if (has_clipproj_connector) { + clipproj_connector = std::make_shared(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{13, 19, 25, 31, 37} : std::set{50}; + out_layers = ph3_connector ? std::set{13, 19, 25, 31, 37} : std::set{clipproj_connector ? 24 : 50}; prompt_attn_range = {0, 0}; if (llm->enable_vision) { diff --git a/src/model/te/clipproj_connector.hpp b/src/model/te/clipproj_connector.hpp new file mode 100644 index 0000000000..28d60e815e --- /dev/null +++ b/src/model/te/clipproj_connector.hpp @@ -0,0 +1,71 @@ +#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 { + 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"}) { + 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 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& tensors) { + model.get_param_tensors(tensors, prefix); + } + + sd::Tensor compute(int n_threads, const sd::Tensor& 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__