Repository navigation
Feat: Support ClipProj-MiniMax-H3 (no mlp) #2410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<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__ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep the transposed connector weight out of quantized
CONT.When
--typeselects a quantized type,set_wtype_overridecan assign that type to the connectorWtensor. No exclusion rule in the inspected conversion code matchestext_encoders.connector.W.forwardthen callsggml_conton a transposed view ofW. On the CPU backend, a quantized, non-contiguous source takes the element-wise path inggml_compute_forward_dup_bytes. That path copiestype_sizebytes per element and advances the destination byne00 * type_sizeper row. For Q8_0, each row writes 139,264 bytes into a destination row of 4,352 bytes. The copy writes about 32 times the allocated destination, so prompt encoding can corrupt the heap or crash.The connector weight must reach this
CONTin a type that the transpose copies correctly, such as F16 or F32. Conversion must not quantizeWwhen it is transposed this way. Apply that exclusion before the loader assignsexpected_type.