Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified assets/qwen/qwen-image-2.1-alpha-out2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion src/core/ggml_extend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,11 @@ ggml_tensor* ggml_ext_conv_3d(ggml_context* ctx,
int d1,
int d2,
bool force_prec_f32,
bool direct) {
bool direct,
float scale) {
if (scale != 1.f) {
x = ggml_ext_scale(ctx, x, scale);
}
if (direct) {
int64_t OC = w->ne[3] / IC;
int64_t N = x->ne[3] / IC;
Expand Down Expand Up @@ -502,6 +506,9 @@ ggml_tensor* ggml_ext_conv_3d(ggml_context* ctx,
}
}

if (scale != 1.f) {
x = ggml_ext_scale(ctx, x, 1.f / scale);
}
if (b != nullptr) {
b = ggml_reshape_4d(ctx, b, 1, 1, 1, b->ne[0]); // [OC, 1, 1, 1]
x = ggml_add_inplace(ctx, x, b);
Expand Down
3 changes: 2 additions & 1 deletion src/core/ggml_extend.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ ggml_tensor* ggml_ext_conv_3d(ggml_context* ctx,
int d1 = 1,
int d2 = 1,
bool force_prec_f32 = false,
bool direct = false);
bool direct = false,
float scale = 1.f);

// w: [OC,IC, KD, 1 * 1]
// x: [N, IC, ID, IH*IW]
Expand Down
22 changes: 20 additions & 2 deletions src/model/vae/wan_vae.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace WAN {
std::tuple<int, int, int> padding;
std::tuple<int, int, int> dilation;
bool bias;
float scale = 1.f;

void init_params(ggml_context* ctx, const String2TensorStorage& tensor_storage_map = {}, const std::string prefix = "") override {
auto weight = tensor_storage_map.find(prefix + "weight");
Expand Down Expand Up @@ -60,6 +61,10 @@ namespace WAN {
dilation(std::move(dilation)),
bias(bias) {}

void set_scale(float scale_value) {
scale = scale_value;
}

ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x, ggml_tensor* cache_x = nullptr) {
// x: [N*IC, ID, IH, IW]
// result: x: [N*OC, ID, IH, IW]
Expand Down Expand Up @@ -93,14 +98,14 @@ namespace WAN {
x2 = ggml_ext_conv_2d(ctx->ggml_ctx, x2, w2, b,
std::get<2>(stride), std::get<1>(stride), 0, 0,
std::get<2>(dilation), std::get<1>(dilation),
ctx->conv2d_direct_enabled);
ctx->conv2d_direct_enabled, false, false, scale);
return ggml_reshape_4d(ctx->ggml_ctx, x2, x2->ne[0], x2->ne[1], 1, out_channels);
}
return ggml_ext_conv_3d(ctx->ggml_ctx, ctx->backend, x, w, b, in_channels,
std::get<2>(stride), std::get<1>(stride), std::get<0>(stride),
0, 0, 0,
std::get<2>(dilation), std::get<1>(dilation), std::get<0>(dilation),
false, ctx->conv3d_direct_enabled);
false, ctx->conv3d_direct_enabled, scale);
}
};

Expand Down Expand Up @@ -1117,6 +1122,19 @@ namespace WAN {
} else {
blocks["conv2"] = std::shared_ptr<GGMLBlock>(new CausalConv3d(z_dim, z_dim, {1, 1, 1}));
}
if (version == VERSION_QWEN_IMAGE_2_1) {
// Keep large VAE activations within the FP16 convolution range.
const float conv_scale = 1.f / 128.f;
std::vector<GGMLBlock*> all_blocks;
get_all_blocks(all_blocks);
for (auto block : all_blocks) {
if (auto conv = dynamic_cast<Conv2d*>(block)) {
conv->set_scale(conv_scale);
} else if (auto conv = dynamic_cast<CausalConv3d*>(block)) {
conv->set_scale(conv_scale);
}
}
}
}

static ggml_tensor* patchify(ggml_context* ctx,
Expand Down
Loading